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 5 package net 6 7 import ( 8 "os" 9 "syscall" 10 ) 11 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 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)) 18 } 19 // Allow broadcast. 20 syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_BROADCAST, 1) 21 return nil 22 } 23 24 func setDefaultListenerSockopts(s syscall.Handle) error { 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. 31 return nil 32 } 33 34 func setDefaultMulticastSockopts(s syscall.Handle) error { 35 // Allow multicast UDP and raw IP datagram sockets to listen 36 // concurrently across multiple listeners. 37 return os.NewSyscallError("setsockopt", syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1)) 38 } 39