Brad Fitzpatrick | 5194744 | 2016-03-01 22:57:46 +0000 | [diff] [blame] | 1 | // Copyright 2011 The Go Authors. All rights reserved. |
Mikio Hara | bc92671 | 2011-05-02 10:50:12 -0400 | [diff] [blame] | 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 | bc92671 | 2011-05-02 10:50:12 -0400 | [diff] [blame] | 5 | package net |
| 6 | |
| 7 | import ( |
Mikio Hara | 2f63afd | 2012-02-01 01:53:26 +0900 | [diff] [blame] | 8 | "os" |
Mikio Hara | bc92671 | 2011-05-02 10:50:12 -0400 | [diff] [blame] | 9 | "syscall" |
| 10 | ) |
| 11 | |
Mikio Hara | bf61a97f | 2013-07-28 11:18:06 +0900 | [diff] [blame] | 12 | func setDefaultSockopts(s syscall.Handle, 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 |
Brad Fitzpatrick | 5fea2cc | 2016-03-01 23:21:55 +0000 | [diff] [blame] | 15 | // is otherwise. Note that some operating systems |
Mikio Hara | bf61a97f | 2013-07-28 11:18:06 +0900 | [diff] [blame] | 16 | // never admit this option. |
| 17 | syscall.SetsockoptInt(s, syscall.IPPROTO_IPV6, syscall.IPV6_V6ONLY, boolint(ipv6only)) |
Mikio Hara | 7419921 | 2012-01-15 14:19:44 +0900 | [diff] [blame] | 18 | } |
Mikio Hara | 0e3514e | 2012-02-11 11:50:51 +0900 | [diff] [blame] | 19 | // Allow broadcast. |
| 20 | syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_BROADCAST, 1) |
| 21 | return nil |
| 22 | } |
Mikio Hara | 7419921 | 2012-01-15 14:19:44 +0900 | [diff] [blame] | 23 | |
Mikio Hara | 9387d11 | 2012-02-12 15:59:21 +0900 | [diff] [blame] | 24 | func setDefaultListenerSockopts(s syscall.Handle) error { |
Alex Brainman | c3733b2 | 2011-10-26 22:25:20 +1100 | [diff] [blame] | 25 | // Windows will reuse recently-used addresses by default. |
| 26 | // SO_REUSEADDR should not be used here, as it allows |
| 27 | // a socket to forcibly bind to a port in use by another socket. |
| 28 | // This could lead to a non-deterministic behavior, where |
| 29 | // connection requests over the port cannot be guaranteed |
| 30 | // to be handled by the correct socket. |
Mikio Hara | 2f63afd | 2012-02-01 01:53:26 +0900 | [diff] [blame] | 31 | return nil |
Mikio Hara | 7419921 | 2012-01-15 14:19:44 +0900 | [diff] [blame] | 32 | } |
| 33 | |
Mikio Hara | 2f63afd | 2012-02-01 01:53:26 +0900 | [diff] [blame] | 34 | func setDefaultMulticastSockopts(s syscall.Handle) error { |
Mikio Hara | 7419921 | 2012-01-15 14:19:44 +0900 | [diff] [blame] | 35 | // Allow multicast UDP and raw IP datagram sockets to listen |
| 36 | // concurrently across multiple listeners. |
Mikio Hara | bf61a97f | 2013-07-28 11:18:06 +0900 | [diff] [blame] | 37 | return os.NewSyscallError("setsockopt", syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1)) |
Mikio Hara | bc92671 | 2011-05-02 10:50:12 -0400 | [diff] [blame] | 38 | } |