sockopt

package
v0.15.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 22, 2026 License: CC0-1.0 Imports: 5 Imported by: 0

Documentation

Overview

Package sockopt provides platform-specific socket option manipulation.

This package offers a unified interface for setting and getting socket options across different operating systems including Linux, Darwin, FreeBSD, OpenBSD, other Unix-like systems and Windows. It supports buffer size configuration, routing marks (where available), and binding sockets to specific network interfaces.

Index

Constants

View Source
const (
	// Bitwise mark masks
	FwmarkCiliumMask     = 0xFFFF1FFF
	FwmarkAWSCNIMask     = 0x00000080
	FwmarkCNIPortmapMask = 0x00002000
	FwmarkKubernetesMask = 0x0000C000
	FwmarkCalicoMask     = 0xFFFF0000
	FwmarkWeaveMask      = 0x00060000
	FwmarkTailscaleMask  = 0x000C0000

	// Non-bitwise marks (integer values)
	FwmarkAntrea     = 0x00000800
	FwmarkIstio      = 0x1337
	FwmarkAWSAppMesh = 0x1E7700CE
)

Well known fwmark collection. Borrowed from https://github.com/fwmark/registry

View Source
const NOFD = -1

NOFD is a sentinel value indicating an invalid or unavailable file descriptor.

Variables

View Source
var ErrUnsupported = errors.New("option unsupported")

ErrUnsupported indicates that the requested socket option is not supported on the current platform or for the given socket type.

Functions

func Control

func Control(a any, f func(fd uintptr)) error

Control extracts the raw file descriptor from a network connection and executes the provided function with it. Returns ErrUnsupported if the connection type does not support raw file descriptor access.

func GetBuffSize

func GetBuffSize(a any) (recvSize, sendSize int, err error)

GetBuffSize returns the current receive and send buffer sizes for the socket.

func GetFd

func GetFd(a any) (fd int, err error)

GetFd extracts the raw file descriptor from a network connection. Returns NOFD if the file descriptor cannot be obtained.

WARN: The file descriptor may become invalid immediately after this function returns. Use Control instead for safer operation.

func GetRoutingMark

func GetRoutingMark(a any) (mark int, err error)

GetRoutingMark retrieves the routing mark (SO_MARK) from the socket.

func GetTCPRTT

func GetTCPRTT(a any) (rtt time.Duration, err error)

GetTCPRTT returns RTT for TCPConn.

func IgnoreUnsupported

func IgnoreUnsupported(err error) error

IgnoreUnsupported returns nil if the error is ErrUnsupported or contains "not supported", otherwise returns the original error. This is useful for optional socket options where unsupported platforms should be silently skipped.

func SetBindToInterface

func SetBindToInterface(a any, i gonnect.NetworkInterface) error

SetBindToInterface binds the socket to a specific network interface using the SO_BINDTODEVICE option. This requires appropriate privileges.

func SetBufSize

func SetBufSize(a any, size int) error

SetBufSize sets both receive and send buffer sizes for the socket. On Linux, this uses both unprivileged (SO_RCVBUF, SO_SNDBUF) and privileged (SO_RCVBUFFORCE, SO_SNDBUFFORCE) options. The privileged options require CAP_NET_ADMIN capability.

func SetRoutingMark

func SetRoutingMark(a any, mark int) error

SetRoutingMark sets the routing mark (SO_MARK) on the socket. This requires appropriate privileges (CAP_NET_ADMIN or net_admin capability).

func SetTCPTimeout

func SetTCPTimeout(a any, timeout time.Duration) error

SetTCPTimeout sets the TCP_USER_TIMEOUT option on the given file descriptor.

Types

type Support

type Support struct {
	BufSize         bool // Buffer size configuration support
	RoutingMark     bool // Routing mark (SO_MARK, SO_USER_COOKIE, etc.) support
	BindToInterface bool // Bind to device/interface support
	TCPUserTimeout  bool // TCP user timeout support
	TCPRtt          bool // TCP Round Trip Time getter
}

Support indicates which socket options are supported on the current platform.

func CheckSupport

func CheckSupport() Support

CheckSupport returns the set of supported socket options on this platform. Linux supports all socket options: buffer size, routing mark, and interface binding.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL