socket

package
v2.7.1 Latest Latest
Warning

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

Go to latest
Published: Jan 5, 2025 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package socket provides some handy socket-related functions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Accept

func Accept(fd int) (int, unix.Sockaddr, error)

Accept accepts the next incoming socket along with setting O_NONBLOCK and O_CLOEXEC flags on it.

func Dup

func Dup(fd int) (int, error)

Dup duplicates the given fd and marks it close-on-exec.

func GetTCPSockAddr

func GetTCPSockAddr(proto, addr string) (sa unix.Sockaddr, family int, tcpAddr *net.TCPAddr, ipv6only bool, err error)

GetTCPSockAddr the structured addresses based on the protocol and raw address.

func GetUDPSockAddr

func GetUDPSockAddr(proto, addr string) (sa unix.Sockaddr, family int, udpAddr *net.UDPAddr, ipv6only bool, err error)

GetUDPSockAddr the structured addresses based on the protocol and raw address.

func GetUnixSockAddr

func GetUnixSockAddr(proto, addr string) (sa unix.Sockaddr, family int, unixAddr *net.UnixAddr, err error)

GetUnixSockAddr the structured addresses based on the protocol and raw address.

func SetBindToDevice

func SetBindToDevice(fd int, ifname string) error

SetBindToDevice binds the socket to a specific network interface.

SO_BINDTODEVICE on Linux works in both directions: only process packets received from the particular interface along with sending them through that interface, instead of following the default route.

func SetIPv4MulticastMembership

func SetIPv4MulticastMembership(fd int, mcast net.IP, ifIndex int) error

SetIPv4MulticastMembership joins fd to the specified multicast IPv4 address. ifIndex is the index of the interface where the multicast datagrams will be received. If ifIndex is 0 then the operating system will choose the default, it is usually needed when the host has multiple network interfaces configured.

func SetIPv6MulticastMembership

func SetIPv6MulticastMembership(fd int, mcast net.IP, ifIndex int) error

SetIPv6MulticastMembership joins fd to the specified multicast IPv6 address. ifIndex is the index of the interface where the multicast datagrams will be received. If ifIndex is 0 then the operating system will choose the default, it is usually needed when the host has multiple network interfaces configured.

func SetIPv6Only

func SetIPv6Only(fd, ipv6only int) error

SetIPv6Only restricts a IPv6 socket to only process IPv6 requests or both IPv4 and IPv6 requests.

func SetKeepAlivePeriod

func SetKeepAlivePeriod(fd, secs int) error

SetKeepAlivePeriod enables the SO_KEEPALIVE option on the socket and sets TCP_KEEPIDLE/TCP_KEEPALIVE to the specified duration in seconds, TCP_KEEPCNT to 5, and TCP_KEEPINTVL to secs/5.

func SetLinger

func SetLinger(fd, sec int) error

SetLinger sets the behavior of Close on a connection which still has data waiting to be sent or to be acknowledged.

If sec < 0 (the default), the operating system finishes sending the data in the background.

If sec == 0, the operating system discards any unsent or unacknowledged data.

If sec > 0, the data is sent in the background as with sec < 0. On some operating systems after sec seconds have elapsed any remaining unsent data may be discarded.

func SetMulticastMembership

func SetMulticastMembership(proto string, udpAddr *net.UDPAddr) func(int, int) error

SetMulticastMembership returns with a socket option function based on the IP version. Returns nil when multicast membership cannot be applied.

func SetNoDelay

func SetNoDelay(fd, noDelay int) error

SetNoDelay controls whether the operating system should delay packet transmission in hopes of sending fewer packets (Nagle's algorithm).

The default is true (no delay), meaning that data is sent as soon as possible after a Write.

func SetRecvBuffer

func SetRecvBuffer(fd, size int) error

SetRecvBuffer sets the size of the operating system's receive buffer associated with the connection.

func SetReuseAddr

func SetReuseAddr(fd, reuseAddr int) error

SetReuseAddr enables SO_REUSEADDR option on socket.

func SetReuseport

func SetReuseport(fd, reusePort int) error

SetReuseport enables SO_REUSEPORT option on socket.

func SetSendBuffer

func SetSendBuffer(fd, size int) error

SetSendBuffer sets the size of the operating system's transmit buffer associated with the connection.

func SockaddrToTCPOrUnixAddr

func SockaddrToTCPOrUnixAddr(sa unix.Sockaddr) net.Addr

SockaddrToTCPOrUnixAddr converts a unix.Sockaddr to a net.TCPAddr or net.UnixAddr. Returns nil if conversion fails.

func SockaddrToUDPAddr

func SockaddrToUDPAddr(sa unix.Sockaddr) net.Addr

SockaddrToUDPAddr converts a unix.Sockaddr to a net.UDPAddr Returns nil if conversion fails.

func TCPSocket

func TCPSocket(proto, addr string, passive bool, sockOptInts []Option[int], sockOptStrs []Option[string]) (int, net.Addr, error)

TCPSocket creates a TCP socket and returns a file descriptor that refers to it. The given socket options will be set on the returned file descriptor.

func UDPSocket

func UDPSocket(proto, addr string, connect bool, sockOptInts []Option[int], sockOptStrs []Option[string]) (int, net.Addr, error)

UDPSocket creates a UDP socket and returns a file descriptor that refers to it. The given socket options will be set on the returned file descriptor.

func UnixSocket

func UnixSocket(proto, addr string, passive bool, sockOptInts []Option[int], sockOptStrs []Option[string]) (int, net.Addr, error)

UnixSocket creates a Unix socket and returns a file descriptor that refers to it. The given socket options will be set on the returned file descriptor.

Types

type Option

type Option[T int | string] struct {
	SetSockOpt func(int, T) error
	Opt        T
}

Option is used for setting an option on socket.

Jump to

Keyboard shortcuts

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