blob: 54c20b1409b1447aedf4135a25930fb01f58bd0a [file] [log] [blame]
Mikio Harabc926712011-05-02 10:50:12 -04001// Copyright 2011 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
Mikio Harabc926712011-05-02 10:50:12 -04005package net
6
7import (
Mikio Hara2f63afd2012-02-01 01:53:26 +09008 "os"
Mikio Harabc926712011-05-02 10:50:12 -04009 "syscall"
10)
11
Mikio Harabf61a97f2013-07-28 11:18:06 +090012func setDefaultSockopts(s, family, sotype int, ipv6only bool) error {
13 if family == syscall.AF_INET6 && sotype != syscall.SOCK_RAW {
14 // Allow both IP versions even if the OS default
15 // is otherwise. Note that some operating systems
16 // never admit this option.
17 syscall.SetsockoptInt(s, syscall.IPPROTO_IPV6, syscall.IPV6_V6ONLY, boolint(ipv6only))
Mikio Hara74199212012-01-15 14:19:44 +090018 }
Mikio Harabc926712011-05-02 10:50:12 -040019 // Allow broadcast.
Mikio Harabf61a97f2013-07-28 11:18:06 +090020 return os.NewSyscallError("setsockopt", syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_BROADCAST, 1))
Mikio Hara0e3514e2012-02-11 11:50:51 +090021}
Mikio Harabc926712011-05-02 10:50:12 -040022
Mikio Hara0e3514e2012-02-11 11:50:51 +090023func setDefaultListenerSockopts(s int) error {
24 // Allow reuse of recently-used addresses.
Mikio Harabf61a97f2013-07-28 11:18:06 +090025 return os.NewSyscallError("setsockopt", syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1))
Mikio Hara74199212012-01-15 14:19:44 +090026}
27
Mikio Hara2f63afd2012-02-01 01:53:26 +090028func setDefaultMulticastSockopts(s int) error {
Mikio Hara74199212012-01-15 14:19:44 +090029 // Allow multicast UDP and raw IP datagram sockets to listen
30 // concurrently across multiple listeners.
Mikio Harabf61a97f2013-07-28 11:18:06 +090031 return os.NewSyscallError("setsockopt", syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1))
Mikio Harabc926712011-05-02 10:50:12 -040032}