Documentation
¶
Index ¶
- Constants
- Variables
- func AddrPortMappedEqual(l, r netip.AddrPort) bool
- func AddrPortToSockaddr(addrPort netip.AddrPort) (name *byte, namelen uint32)
- func AddrPortToSockaddrInet4(addrPort netip.AddrPort) unix.RawSockaddrInet4
- func AddrPortToSockaddrInet6(addrPort netip.AddrPort) unix.RawSockaddrInet6
- func AddrPortToSockaddrWithAddressFamily(addrPort netip.AddrPort, is4 bool) (name *byte, namelen uint32)
- func ParseFlagsForError(flags int) error
- func ResolveIP(ctx context.Context, network, host string) (netip.Addr, error)
- func SockaddrInet4ToAddrPort(sa *unix.RawSockaddrInet4) netip.AddrPort
- func SockaddrInet6ToAddrPort(sa *unix.RawSockaddrInet6) netip.AddrPort
- func SockaddrToAddrPort(name *byte, namelen uint32) (netip.AddrPort, error)
- type Addr
- func AddrFromDomainPort(domain string, port uint16) (Addr, error)
- func AddrFromHostPort(host string, port uint16) (Addr, error)
- func AddrFromIPPort(addrPort netip.AddrPort) (addr Addr)
- func MustAddrFromDomainPort(domain string, port uint16) Addr
- func ParseAddr[T ~[]byte | ~string](s T) (Addr, error)
- func (a Addr) AppendTo(b []byte) []byte
- func (a Addr) Domain() string
- func (a Addr) Equals(b Addr) bool
- func (a Addr) Host() string
- func (a Addr) IP() netip.Addr
- func (a Addr) IPPort() netip.AddrPort
- func (a Addr) IsDomain() bool
- func (a Addr) IsIP() bool
- func (a Addr) IsValid() bool
- func (a Addr) MarshalText() ([]byte, error)
- func (a Addr) Port() uint16
- func (a Addr) ResolveIP(ctx context.Context, network string) (netip.Addr, error)
- func (a Addr) ResolveIPPort(ctx context.Context, network string) (netip.AddrPort, error)
- func (a Addr) String() string
- func (a *Addr) UnmarshalText(text []byte) error
- type ListenConfig
- type ListenConfigCache
- type ListenerSocketOptions
- type MmsgConn
- type MmsgRConn
- type MmsgWConn
- type Mmsghdr
- type SocketControlMessage
- type SocketInfo
Constants ¶
const DefaultUDPSocketBufferSize = 7 << 20
DefaultUDPSocketBufferSize is the default send and receive buffer size of UDP sockets.
We use the same value of 7 MiB as wireguard-go: https://github.com/WireGuard/wireguard-go/blob/12269c2761734b15625017d8565745096325392f/conn/controlfns.go#L13-L18
const SYS_RECVMMSG = unix.SYS_RECVMMSG
const SocketControlMessageBufferSize = socketControlMessageBufferSize
SocketControlMessageBufferSize specifies the buffer size for receiving socket control messages.
Variables ¶
var ( // DefaultUDPServerSocketOptions is the default [ListenerSocketOptions] for UDP servers. DefaultUDPServerSocketOptions = ListenerSocketOptions{ SendBufferSize: DefaultUDPSocketBufferSize, ReceiveBufferSize: DefaultUDPSocketBufferSize, PathMTUDiscovery: true, ReceivePacketInfo: true, } // DefaultUDPServerListenConfig is the default [ListenConfig] for UDP servers. DefaultUDPServerListenConfig = DefaultUDPServerSocketOptions.ListenConfig() // DefaultUDPClientSocketOptions is the default [ListenerSocketOptions] for UDP clients. DefaultUDPClientSocketOptions = ListenerSocketOptions{ SendBufferSize: DefaultUDPSocketBufferSize, ReceiveBufferSize: DefaultUDPSocketBufferSize, PathMTUDiscovery: true, } // DefaultUDPClientListenConfig is the default [ListenConfig] for UDP clients. DefaultUDPClientListenConfig = DefaultUDPClientSocketOptions.ListenConfig() )
var ( ErrMessageTruncated = errors.New("the packet is larger than the supplied buffer") ErrControlMessageTruncated = errors.New("the control message is larger than the supplied buffer") )
var ALongTimeAgo = time.Unix(0, 0)
ALongTimeAgo is a non-zero time, far in the past, used for immediate deadlines.
Functions ¶
func AddrPortMappedEqual ¶ added in v1.3.0
AddrPortMappedEqual returns whether the two addresses point to the same endpoint. An IPv4 address and an IPv4-mapped IPv6 address pointing to the same endpoint are considered equal. For example, 1.1.1.1:53 and [::ffff:1.1.1.1]:53 are considered equal.
func AddrPortToSockaddr ¶ added in v1.1.0
func AddrPortToSockaddrInet4 ¶ added in v1.1.0
func AddrPortToSockaddrInet4(addrPort netip.AddrPort) unix.RawSockaddrInet4
func AddrPortToSockaddrInet6 ¶ added in v1.1.0
func AddrPortToSockaddrInet6(addrPort netip.AddrPort) unix.RawSockaddrInet6
func AddrPortToSockaddrWithAddressFamily ¶ added in v1.7.0
func ParseFlagsForError ¶
ParseFlagsForError parses the message flags returned by the ReadMsgUDPAddrPort method and returns an error if MSG_TRUNC is set, indicating that the returned packet was truncated.
The check is skipped on Windows, because an error (WSAEMSGSIZE) is also returned when MSG_PARTIAL is set.
func ResolveIP ¶ added in v1.5.0
ResolveIP resolves a domain name string into an IP address.
The network must be one of "ip", "ip4" or "ip6". String representations of IP addresses are not supported.
This function always returns the first IP address returned by the resolver, because the resolver takes care of sorting the IP addresses by address family availability and preference.
func SockaddrInet4ToAddrPort ¶ added in v1.4.0
func SockaddrInet4ToAddrPort(sa *unix.RawSockaddrInet4) netip.AddrPort
func SockaddrInet6ToAddrPort ¶ added in v1.4.0
func SockaddrInet6ToAddrPort(sa *unix.RawSockaddrInet6) netip.AddrPort
Types ¶
type Addr ¶ added in v1.5.0
type Addr struct {
// contains filtered or unexported fields
}
Addr is the base address type used throughout the package.
An Addr is a port number combined with either an IP address or a domain name.
For space efficiency, the IP address and the domain string share the same space. The netip.Addr is stored in its original layout. The domain string's data pointer is stored in the ip.z field. Its length is stored at the beginning of the structure. This is essentially an unsafe "enum".
func AddrFromDomainPort ¶ added in v1.5.0
AddrFromDomainPort returns an Addr from the provided domain name and port number.
func AddrFromHostPort ¶ added in v1.5.0
AddrFromHostPort returns an Addr from the provided host string and port number. The host string may be a string representation of an IP address or a domain name.
func AddrFromIPPort ¶ added in v1.5.0
AddrFromIPPort returns an Addr from the provided netip.AddrPort.
func MustAddrFromDomainPort ¶ added in v1.5.0
MustAddrFromDomainPort calls AddrFromDomainPort and panics on error.
func ParseAddr ¶ added in v1.5.0
ParseAddr parses the provided string representation of an address and returns the parsed address or an error.
func (Addr) AppendTo ¶ added in v1.5.0
AppendTo appends the string representation of the address to the provided buffer.
If the address is zero value, nothing is appended.
func (Addr) Domain ¶ added in v1.5.0
Domain returns the domain name.
If the address is an IP address or zero value, this method panics.
func (Addr) Host ¶ added in v1.5.0
Host returns the string representation of the IP address or the domain name.
If the address is zero value, this method panics.
func (Addr) IP ¶ added in v1.5.0
IP returns the IP address.
If the address is a domain name or zero value, this method panics.
func (Addr) IPPort ¶ added in v1.5.0
IPPort returns a netip.AddrPort.
If the address is a domain name or zero value, this method panics.
func (Addr) IsValid ¶ added in v1.5.0
IsValid returns whether the address is an initialized address (not a zero value).
func (Addr) MarshalText ¶ added in v1.5.0
MarshalText implements the encoding.TextMarshaler MarshalText method.
func (Addr) ResolveIP ¶ added in v1.5.0
ResolveIP returns the IP address itself or the resolved IP address of the domain name.
The network is only used for domain name resolution and must be one of "ip", "ip4" or "ip6".
If the address is zero value, this method panics.
func (Addr) ResolveIPPort ¶ added in v1.5.0
ResolveIPPort returns the IP address itself or the resolved IP address of the domain name and the port number as a netip.AddrPort.
The network is only used for domain name resolution and must be one of "ip", "ip4" or "ip6".
If the address is zero value, this method panics.
func (Addr) String ¶ added in v1.5.0
String returns the string representation of the address.
If the address is zero value, an empty string is returned.
func (*Addr) UnmarshalText ¶ added in v1.5.0
UnmarshalText implements the encoding.TextUnmarshaler UnmarshalText method.
type ListenConfig ¶ added in v1.4.0
type ListenConfig struct {
// contains filtered or unexported fields
}
ListenConfig is like net.ListenConfig but provides a subjectively nicer API.
func (*ListenConfig) ListenUDP ¶ added in v1.4.0
func (lc *ListenConfig) ListenUDP(ctx context.Context, network, address string) (uc *net.UDPConn, info SocketInfo, err error)
ListenUDP wraps net.ListenConfig.ListenPacket and returns a *net.UDPConn directly.
func (*ListenConfig) ListenUDPMmsgConn ¶ added in v1.6.0
func (lc *ListenConfig) ListenUDPMmsgConn(ctx context.Context, network, address string) (c MmsgConn, info SocketInfo, err error)
ListenUDPMmsgConn is like [ListenUDP] but wraps the *net.UDPConn in a MmsgConn for reading and writing multiple messages using the recvmmsg(2) and sendmmsg(2) system calls.
type ListenConfigCache ¶ added in v1.4.0
type ListenConfigCache map[ListenerSocketOptions]ListenConfig
ListenConfigCache is a map of ListenerSocketOptions to ListenConfig.
func NewListenConfigCache ¶ added in v1.4.0
func NewListenConfigCache() ListenConfigCache
NewListenConfigCache creates a new cache for ListenConfig with a few default entries.
func (ListenConfigCache) Get ¶ added in v1.4.0
func (cache ListenConfigCache) Get(lso ListenerSocketOptions) (lc ListenConfig)
Get returns a ListenConfig for the given ListenerSocketOptions.
type ListenerSocketOptions ¶ added in v1.4.0
type ListenerSocketOptions struct { // SendBufferSize sets the send buffer size of the listener. // // Available on POSIX systems. SendBufferSize int // ReceiveBufferSize sets the receive buffer size of the listener. // // Available on POSIX systems. ReceiveBufferSize int // Fwmark sets the listener's fwmark on Linux, or user cookie on FreeBSD. // // Available on Linux and FreeBSD. Fwmark int // TrafficClass sets the traffic class of the listener. // // Available on most platforms except Windows. TrafficClass int // PathMTUDiscovery enables Path MTU Discovery on the listener. // // Available on Linux, macOS, FreeBSD, and Windows. PathMTUDiscovery bool // ProbeUDPGSOSupport enables best-effort probing of // UDP Generic Segmentation Offload (GSO) support on the listener. // // Available on Linux and Windows. ProbeUDPGSOSupport bool // UDPGenericReceiveOffload enables UDP Generic Receive Offload (GRO) on the listener. // // Available on Linux and Windows. UDPGenericReceiveOffload bool // ReceivePacketInfo enables the reception of packet information control messages on the listener. // // Available on Linux, macOS, and Windows. ReceivePacketInfo bool }
ListenerSocketOptions contains listener-specific socket options.
func (ListenerSocketOptions) ListenConfig ¶ added in v1.4.0
func (lso ListenerSocketOptions) ListenConfig() ListenConfig
ListenConfig returns a ListenConfig that sets the socket options.
type MmsgConn ¶ added in v1.6.0
MmsgConn wraps a *net.UDPConn and provides methods for reading and writing multiple messages using the recvmmsg(2) and sendmmsg(2) system calls.
func NewMmsgConn ¶ added in v1.6.0
NewMmsgConn returns a new MmsgConn for udpConn.
func (MmsgConn) NewRConn ¶ added in v1.6.0
NewRConn returns the connection wrapped in a new *MmsgRConn for batch reading.
func (MmsgConn) NewWConn ¶ added in v1.6.0
NewWConn returns the connection wrapped in a new *MmsgWConn for batch writing.
type MmsgRConn ¶ added in v1.4.0
type MmsgRConn struct { MmsgConn // contains filtered or unexported fields }
MmsgRConn provides read access to the MmsgConn.
MmsgRConn is not safe for concurrent use. Always create a new MmsgRConn for each goroutine.
type MmsgWConn ¶ added in v1.4.0
type MmsgWConn struct { MmsgConn // contains filtered or unexported fields }
MmsgWConn provides write access to the MmsgConn.
MmsgWConn is not safe for concurrent use. Always create a new MmsgWConn for each goroutine.
type SocketControlMessage ¶ added in v1.6.0
type SocketControlMessage struct { // PktinfoAddr is the IP address of the network interface the packet was received from. PktinfoAddr netip.Addr // PktinfoIfindex is the index of the network interface the packet was received from. PktinfoIfindex uint32 // SegmentSize is the UDP GRO/GSO segment size. SegmentSize uint32 }
SocketControlMessage contains information that can be parsed from or put into socket control messages.
func ParseSocketControlMessage ¶ added in v1.6.0
func ParseSocketControlMessage(cmsg []byte) (m SocketControlMessage, err error)
ParseSocketControlMessage parses a sequence of socket control messages and returns the parsed information.
func (SocketControlMessage) AppendTo ¶ added in v1.6.0
func (m SocketControlMessage) AppendTo(b []byte) []byte
AppendTo appends the socket control message to the buffer.
type SocketInfo ¶ added in v1.6.0
type SocketInfo struct { // MaxUDPGSOSegments is the maximum number of UDP GSO segments supported by the socket. // // If UDP GSO is not enabled on the socket, or the system does not support UDP GSO, the value is 1. // // The value is 0 if the socket is not a UDP socket. MaxUDPGSOSegments uint32 // UDPGenericReceiveOffload indicates whether UDP GRO is enabled on the socket. UDPGenericReceiveOffload bool // IPv6Only reports whether the socket is IPv6-only. IPv6Only bool }
SocketInfo contains information about a socket.