github.com/sagernet/wireguard-go@v0.0.0-20231215174105-89dec3b2f3e8/conn/controlfns.go (about)

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