Black Lives Matter. Support the Equal Justice Initiative.

Source file src/net/sockopt_bsd.go

Documentation: net

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

View as plain text