Mikio Hara | bc92671 | 2011-05-02 10:50:12 -0400 | [diff] [blame] | 1 | // 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 Hara | 484cc67 | 2014-09-18 19:17:55 +0900 | [diff] [blame] | 5 | // +build darwin dragonfly freebsd netbsd openbsd |
Russ Cox | 2715956 | 2011-09-15 16:48:57 -0400 | [diff] [blame] | 6 | |
Mikio Hara | bc92671 | 2011-05-02 10:50:12 -0400 | [diff] [blame] | 7 | package net |
| 8 | |
| 9 | import ( |
Mikio Hara | 2f63afd | 2012-02-01 01:53:26 +0900 | [diff] [blame] | 10 | "os" |
Mikio Hara | 83ac901 | 2014-03-29 13:04:25 +0900 | [diff] [blame] | 11 | "runtime" |
Mikio Hara | bc92671 | 2011-05-02 10:50:12 -0400 | [diff] [blame] | 12 | "syscall" |
| 13 | ) |
| 14 | |
Mikio Hara | bf61a97f | 2013-07-28 11:18:06 +0900 | [diff] [blame] | 15 | func setDefaultSockopts(s, family, sotype int, ipv6only bool) error { |
Mikio Hara | 83ac901 | 2014-03-29 13:04:25 +0900 | [diff] [blame] | 16 | 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 Hara | 6b112c2 | 2014-08-21 17:53:45 +0900 | [diff] [blame] | 20 | // as described in RFC 6056 and is pretty narrow. |
Mikio Hara | 83ac901 | 2014-03-29 13:04:25 +0900 | [diff] [blame] | 21 | 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 Hara | bf61a97f | 2013-07-28 11:18:06 +0900 | [diff] [blame] | 28 | 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 Hara | 7419921 | 2012-01-15 14:19:44 +0900 | [diff] [blame] | 33 | } |
Mikio Hara | bc92671 | 2011-05-02 10:50:12 -0400 | [diff] [blame] | 34 | // Allow broadcast. |
Mikio Hara | bf61a97f | 2013-07-28 11:18:06 +0900 | [diff] [blame] | 35 | return os.NewSyscallError("setsockopt", syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_BROADCAST, 1)) |
Mikio Hara | 0e3514e | 2012-02-11 11:50:51 +0900 | [diff] [blame] | 36 | } |
Mikio Hara | 2f63afd | 2012-02-01 01:53:26 +0900 | [diff] [blame] | 37 | |
Mikio Hara | 0e3514e | 2012-02-11 11:50:51 +0900 | [diff] [blame] | 38 | func setDefaultListenerSockopts(s int) error { |
| 39 | // Allow reuse of recently-used addresses. |
Mikio Hara | bf61a97f | 2013-07-28 11:18:06 +0900 | [diff] [blame] | 40 | return os.NewSyscallError("setsockopt", syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1)) |
Mikio Hara | 7419921 | 2012-01-15 14:19:44 +0900 | [diff] [blame] | 41 | } |
Mikio Hara | bc92671 | 2011-05-02 10:50:12 -0400 | [diff] [blame] | 42 | |
Mikio Hara | 2f63afd | 2012-02-01 01:53:26 +0900 | [diff] [blame] | 43 | func setDefaultMulticastSockopts(s int) error { |
Mikio Hara | 7419921 | 2012-01-15 14:19:44 +0900 | [diff] [blame] | 44 | // Allow multicast UDP and raw IP datagram sockets to listen |
| 45 | // concurrently across multiple listeners. |
Mikio Hara | bf61a97f | 2013-07-28 11:18:06 +0900 | [diff] [blame] | 46 | if err := syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1); err != nil { |
Mikio Hara | 2f63afd | 2012-02-01 01:53:26 +0900 | [diff] [blame] | 47 | return os.NewSyscallError("setsockopt", err) |
| 48 | } |
Mikio Hara | 6fa2296 | 2012-02-13 12:45:59 +0900 | [diff] [blame] | 49 | // 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 Hara | bf61a97f | 2013-07-28 11:18:06 +0900 | [diff] [blame] | 53 | return os.NewSyscallError("setsockopt", syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_REUSEPORT, 1)) |
Mikio Hara | bc92671 | 2011-05-02 10:50:12 -0400 | [diff] [blame] | 54 | } |