github.com/roboticscm/goman@v0.0.0-20210203095141-87c07b4a0a55/src/net/sockopt_bsd.go (about)

     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  // +build darwin dragonfly freebsd netbsd openbsd
     6  
     7  package net
     8  
     9  import (
    10  	"os"
    11  	"runtime"
    12  	"syscall"
    13  )
    14  
    15  func setDefaultSockopts(s, family, sotype int, ipv6only bool) error {
    16  	if runtime.GOOS == "dragonfly" && sotype != syscall.SOCK_RAW {
    17  		// On DragonFly BSD, we adjust the ephemeral port
    18  		// range because unlike other BSD systems its default
    19  		// port range doesn't conform to IANA recommendation
    20  		// as described in RFC 6056 and is pretty narrow.
    21  		switch family {
    22  		case syscall.AF_INET:
    23  			syscall.SetsockoptInt(s, syscall.IPPROTO_IP, syscall.IP_PORTRANGE, syscall.IP_PORTRANGE_HIGH)
    24  		case syscall.AF_INET6:
    25  			syscall.SetsockoptInt(s, syscall.IPPROTO_IPV6, syscall.IPV6_PORTRANGE, syscall.IPV6_PORTRANGE_HIGH)
    26  		}
    27  	}
    28  	if family == syscall.AF_INET6 && sotype != syscall.SOCK_RAW {
    29  		// Allow both IP versions even if the OS default
    30  		// is otherwise.  Note that some operating systems
    31  		// never admit this option.
    32  		syscall.SetsockoptInt(s, syscall.IPPROTO_IPV6, syscall.IPV6_V6ONLY, boolint(ipv6only))
    33  	}
    34  	// Allow broadcast.
    35  	return os.NewSyscallError("setsockopt", syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_BROADCAST, 1))
    36  }
    37  
    38  func setDefaultListenerSockopts(s int) error {
    39  	// Allow reuse of recently-used addresses.
    40  	return os.NewSyscallError("setsockopt", syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1))
    41  }
    42  
    43  func setDefaultMulticastSockopts(s int) error {
    44  	// Allow multicast UDP and raw IP datagram sockets to listen
    45  	// concurrently across multiple listeners.
    46  	if err := syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1); err != nil {
    47  		return os.NewSyscallError("setsockopt", err)
    48  	}
    49  	// Allow reuse of recently-used ports.
    50  	// This option is supported only in descendants of 4.4BSD,
    51  	// to make an effective multicast application that requires
    52  	// quick draw possible.
    53  	return os.NewSyscallError("setsockopt", syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_REUSEPORT, 1))
    54  }