controlfns.go raw

   1  /* SPDX-License-Identifier: MIT
   2   *
   3   * Copyright (C) 2017-2025 WireGuard LLC. All Rights Reserved.
   4   */
   5  
   6  package conn
   7  
   8  import (
   9  	"net"
  10  	"syscall"
  11  )
  12  
  13  // UDP socket read/write buffer size (7MB). The value of 7MB is chosen as it is
  14  // the max supported by a default configuration of macOS. Some platforms will
  15  // silently clamp the value to other maximums, such as linux clamping to
  16  // net.core.{r,w}mem_max (see _linux.go for additional implementation that works
  17  // around this limitation)
  18  const socketBufferSize = 7 << 20
  19  
  20  // controlFn is the callback function signature from net.ListenConfig.Control.
  21  // It is used to apply platform specific configuration to the socket prior to
  22  // bind.
  23  type controlFn func(network, address string, c syscall.RawConn) error
  24  
  25  // controlFns is a list of functions that are called from the listen config
  26  // that can apply socket options.
  27  var controlFns = []controlFn{}
  28  
  29  // listenConfig returns a net.ListenConfig that applies the controlFns to the
  30  // socket prior to bind. This is used to apply socket buffer sizing and packet
  31  // information OOB configuration for sticky sockets.
  32  func listenConfig() *net.ListenConfig {
  33  	return &net.ListenConfig{
  34  		Control: func(network, address string, c syscall.RawConn) error {
  35  			for _, fn := range controlFns {
  36  				if err := fn(network, address, c); err != nil {
  37  					return err
  38  				}
  39  			}
  40  			return nil
  41  		},
  42  	}
  43  }
  44