Source file src/net/sockopt_bsd.go

     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  
     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 && supportsIPv4map() {
    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  	if (sotype == syscall.SOCK_DGRAM || sotype == syscall.SOCK_RAW) && family != syscall.AF_UNIX {
    35  		// Allow broadcast.
    36  		return os.NewSyscallError("setsockopt", syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_BROADCAST, 1))
    37  	}
    38  	return nil
    39  }
    40  
    41  func setDefaultListenerSockopts(s int) error {
    42  	// Allow reuse of recently-used addresses.
    43  	return os.NewSyscallError("setsockopt", syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1))
    44  }
    45  
    46  func setDefaultMulticastSockopts(s int) error {
    47  	// Allow multicast UDP and raw IP datagram sockets to listen
    48  	// concurrently across multiple listeners.
    49  	if err := syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1); err != nil {
    50  		return os.NewSyscallError("setsockopt", err)
    51  	}
    52  	// Allow reuse of recently-used ports.
    53  	// This option is supported only in descendants of 4.4BSD,
    54  	// to make an effective multicast application that requires
    55  	// quick draw possible.
    56  	return os.NewSyscallError("setsockopt", syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_REUSEPORT, 1))
    57  }
    58  

View as plain text