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 | 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, 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 Hara | 7419921 | 2012-01-15 14:19:44 +0900 | [diff] [blame] | 18 | } |
Mikio Hara | bc92671 | 2011-05-02 10:50:12 -0400 | [diff] [blame] | 19 | // Allow broadcast. |
Mikio Hara | bf61a97f | 2013-07-28 11:18:06 +0900 | [diff] [blame] | 20 | 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] | 21 | } |
Mikio Hara | bc92671 | 2011-05-02 10:50:12 -0400 | [diff] [blame] | 22 | |
Mikio Hara | 0e3514e | 2012-02-11 11:50:51 +0900 | [diff] [blame] | 23 | func setDefaultListenerSockopts(s int) error { |
| 24 | // Allow reuse of recently-used addresses. |
Mikio Hara | bf61a97f | 2013-07-28 11:18:06 +0900 | [diff] [blame] | 25 | 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] | 26 | } |
| 27 | |
Mikio Hara | 2f63afd | 2012-02-01 01:53:26 +0900 | [diff] [blame] | 28 | func setDefaultMulticastSockopts(s int) error { |
Mikio Hara | 7419921 | 2012-01-15 14:19:44 +0900 | [diff] [blame] | 29 | // Allow multicast UDP and raw IP datagram sockets to listen |
| 30 | // concurrently across multiple listeners. |
Mikio Hara | bf61a97f | 2013-07-28 11:18:06 +0900 | [diff] [blame] | 31 | 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] | 32 | } |