1 // Copyright 2018 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.
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
))
19 if (sotype
== syscall
.SOCK_DGRAM || sotype
== syscall
.SOCK_RAW
) && family
!= syscall
.AF_UNIX
{
21 return os
.NewSyscallError("setsockopt", syscall
.SetsockoptInt(s
, syscall
.SOL_SOCKET
, syscall
.SO_BROADCAST
, 1))
26 func setDefaultListenerSockopts(s
int) error
{
27 // Allow reuse of recently-used addresses.
28 return os
.NewSyscallError("setsockopt", syscall
.SetsockoptInt(s
, syscall
.SOL_SOCKET
, syscall
.SO_REUSEADDR
, 1))
31 func setDefaultMulticastSockopts(s
int) error
{
32 // Allow multicast UDP and raw IP datagram sockets to listen
33 // concurrently across multiple listeners.
34 if err
:= syscall
.SetsockoptInt(s
, syscall
.SOL_SOCKET
, syscall
.SO_REUSEADDR
, 1); err
!= nil {
35 return os
.NewSyscallError("setsockopt", err
)
37 // Allow reuse of recently-used ports.
38 return os
.NewSyscallError("setsockopt", syscall
.SetsockoptInt(s
, syscall
.SOL_SOCKET
, syscall
.SO_REUSEPORT
, 1))