github.com/noisysockets/noisysockets@v0.21.2-0.20240515114641-7f467e651c90/internal/conn/controlfns.go (about)

     1  // SPDX-License-Identifier: MPL-2.0
     2  /*
     3   * Copyright (C) 2024 The Noisy Sockets Authors.
     4   *
     5   * This Source Code Form is subject to the terms of the Mozilla Public
     6   * License, v. 2.0. If a copy of the MPL was not distributed with this
     7   * file, You can obtain one at http://mozilla.org/MPL/2.0/.
     8   *
     9   * Portions of this file are based on code originally from wireguard-go,
    10   *
    11   * Copyright (C) 2017-2023 WireGuard LLC. All Rights Reserved.
    12   *
    13   * Permission is hereby granted, free of charge, to any person obtaining a copy of
    14   * this software and associated documentation files (the "Software"), to deal in
    15   * the Software without restriction, including without limitation the rights to
    16   * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
    17   * of the Software, and to permit persons to whom the Software is furnished to do
    18   * so, subject to the following conditions:
    19   *
    20   * The above copyright notice and this permission notice shall be included in all
    21   * copies or substantial portions of the Software.
    22   *
    23   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    24   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    25   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    26   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    27   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    28   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    29   * SOFTWARE.
    30   */
    31  
    32  package conn
    33  
    34  import (
    35  	"net"
    36  	"syscall"
    37  )
    38  
    39  // UDP socket read/write buffer size (7MB). The value of 7MB is chosen as it is
    40  // the max supported by a default configuration of macOS. Some platforms will
    41  // silently clamp the value to other maximums, such as linux clamping to
    42  // net.core.{r,w}mem_max (see _linux.go for additional implementation that works
    43  // around this limitation)
    44  const socketBufferSize = 7 << 20
    45  
    46  // controlFn is the callback function signature from net.ListenConfig.Control.
    47  // It is used to apply platform specific configuration to the socket prior to
    48  // bind.
    49  type controlFn func(network, address string, c syscall.RawConn) error
    50  
    51  // controlFns is a list of functions that are called from the listen config
    52  // that can apply socket options.
    53  var controlFns = []controlFn{}
    54  
    55  // listenConfig returns a net.ListenConfig that applies the controlFns to the
    56  // socket prior to bind. This is used to apply socket buffer sizing and packet
    57  // information OOB configuration for sticky sockets.
    58  func listenConfig() *net.ListenConfig {
    59  	return &net.ListenConfig{
    60  		Control: func(network, address string, c syscall.RawConn) error {
    61  			for _, fn := range controlFns {
    62  				if err := fn(network, address, c); err != nil {
    63  					return err
    64  				}
    65  			}
    66  			return nil
    67  		},
    68  	}
    69  }