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
- Variables
- func Control(a any, f func(fd uintptr)) error
- func GetBuffSize(a any) (recvSize, sendSize int, err error)
- func GetFd(a any) (fd int, err error)
- func GetRoutingMark(a any) (mark int, err error)
- func GetTCPRTT(a any) (rtt time.Duration, err error)
- func IgnoreUnsupported(err error) error
- func SetBindToInterface(a any, i gonnect.NetworkInterface) error
- func SetBufSize(a any, size int) error
- func SetRoutingMark(a any, mark int) error
- func SetTCPTimeout(a any, timeout time.Duration) error
- type Support
Constants ¶
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
const NOFD = -1
NOFD is a sentinel value indicating an invalid or unavailable file descriptor.
Variables ¶
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 ¶
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 ¶
GetBuffSize returns the current receive and send buffer sizes for the socket.
func GetFd ¶
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 ¶
GetRoutingMark retrieves the routing mark (SO_MARK) from the socket.
func IgnoreUnsupported ¶
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 ¶
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 ¶
SetRoutingMark sets the routing mark (SO_MARK) on the socket. This requires appropriate privileges (CAP_NET_ADMIN or net_admin capability).
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.