blob: 00e4dbf3761102994b2445ff6a05afa0b41670e7 [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 Hara484cc672014-09-18 19:17:55 +09005// +build darwin dragonfly freebsd netbsd openbsd
Russ Cox27159562011-09-15 16:48:57 -04006
Mikio Harabc926712011-05-02 10:50:12 -04007package net
8
9import (
Mikio Hara2f63afd2012-02-01 01:53:26 +090010 "os"
Mikio Hara83ac9012014-03-29 13:04:25 +090011 "runtime"
Mikio Harabc926712011-05-02 10:50:12 -040012 "syscall"
13)
14
Mikio Harabf61a97f2013-07-28 11:18:06 +090015func setDefaultSockopts(s, family, sotype int, ipv6only bool) error {
Mikio Hara83ac9012014-03-29 13:04:25 +090016 if runtime.GOOS == "dragonfly" && sotype != syscall.SOCK_RAW {
17 // On DragonFly BSD, we adjust the ephemeral port
18 // range because unlike other BSD systems its default
19 // port range doesn't conform to IANA recommendation
Mikio Hara6b112c22014-08-21 17:53:45 +090020 // as described in RFC 6056 and is pretty narrow.
Mikio Hara83ac9012014-03-29 13:04:25 +090021 switch family {
22 case syscall.AF_INET:
23 syscall.SetsockoptInt(s, syscall.IPPROTO_IP, syscall.IP_PORTRANGE, syscall.IP_PORTRANGE_HIGH)
24 case syscall.AF_INET6:
25 syscall.SetsockoptInt(s, syscall.IPPROTO_IPV6, syscall.IPV6_PORTRANGE, syscall.IPV6_PORTRANGE_HIGH)
26 }
27 }
Mikio Harabf61a97f2013-07-28 11:18:06 +090028 if family == syscall.AF_INET6 && sotype != syscall.SOCK_RAW {
29 // Allow both IP versions even if the OS default
30 // is otherwise. Note that some operating systems
31 // never admit this option.
32 syscall.SetsockoptInt(s, syscall.IPPROTO_IPV6, syscall.IPV6_V6ONLY, boolint(ipv6only))
Mikio Hara74199212012-01-15 14:19:44 +090033 }
Mikio Harabc926712011-05-02 10:50:12 -040034 // Allow broadcast.
Mikio Harabf61a97f2013-07-28 11:18:06 +090035 return os.NewSyscallError("setsockopt", syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_BROADCAST, 1))
Mikio Hara0e3514e2012-02-11 11:50:51 +090036}
Mikio Hara2f63afd2012-02-01 01:53:26 +090037
Mikio Hara0e3514e2012-02-11 11:50:51 +090038func setDefaultListenerSockopts(s int) error {
39 // Allow reuse of recently-used addresses.
Mikio Harabf61a97f2013-07-28 11:18:06 +090040 return os.NewSyscallError("setsockopt", syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1))
Mikio Hara74199212012-01-15 14:19:44 +090041}
Mikio Harabc926712011-05-02 10:50:12 -040042
Mikio Hara2f63afd2012-02-01 01:53:26 +090043func setDefaultMulticastSockopts(s int) error {
Mikio Hara74199212012-01-15 14:19:44 +090044 // Allow multicast UDP and raw IP datagram sockets to listen
45 // concurrently across multiple listeners.
Mikio Harabf61a97f2013-07-28 11:18:06 +090046 if err := syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1); err != nil {
Mikio Hara2f63afd2012-02-01 01:53:26 +090047 return os.NewSyscallError("setsockopt", err)
48 }
Mikio Hara6fa22962012-02-13 12:45:59 +090049 // Allow reuse of recently-used ports.
50 // This option is supported only in descendants of 4.4BSD,
51 // to make an effective multicast application that requires
52 // quick draw possible.
Mikio Harabf61a97f2013-07-28 11:18:06 +090053 return os.NewSyscallError("setsockopt", syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_REUSEPORT, 1))
Mikio Harabc926712011-05-02 10:50:12 -040054}