blob: 80174265212daae6606e35fa55e1c7d69183c21b [file] [log] [blame]
Brad Fitzpatrick51947442016-03-01 22:57:46 +00001// Copyright 2011 The Go Authors. All rights reserved.
Mikio Harabc926712011-05-02 10:50:12 -04002// 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 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 Fitzpatrick5fea2cc2016-03-01 23:21:55 +000015 // is otherwise. Note that some operating systems
Mikio Harabf61a97f2013-07-28 11:18:06 +090016 // 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 Hara0e3514e2012-02-11 11:50:51 +090019 // Allow broadcast.
20 syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_BROADCAST, 1)
21 return nil
22}
Mikio Hara74199212012-01-15 14:19:44 +090023
Mikio Hara9387d112012-02-12 15:59:21 +090024func setDefaultListenerSockopts(s syscall.Handle) error {
Alex Brainmanc3733b22011-10-26 22:25:20 +110025 // 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 Hara2f63afd2012-02-01 01:53:26 +090031 return nil
Mikio Hara74199212012-01-15 14:19:44 +090032}
33
Mikio Hara2f63afd2012-02-01 01:53:26 +090034func setDefaultMulticastSockopts(s syscall.Handle) error {
Mikio Hara74199212012-01-15 14:19:44 +090035 // Allow multicast UDP and raw IP datagram sockets to listen
36 // concurrently across multiple listeners.
Mikio Harabf61a97f2013-07-28 11:18:06 +090037 return os.NewSyscallError("setsockopt", syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1))
Mikio Harabc926712011-05-02 10:50:12 -040038}