Documentation
¶
Overview ¶
Package helpers provides utility functions and types for working with gVisor's netstack. It includes helpers for creating and managing network interfaces, converting between address formats, wrapping netstack errors to implement the error interface, and managing NIC (Network Interface Card) creation.
Index ¶
- Variables
- func AddrPortFromString(hostport string) (netip.AddrPort, error)
- func ConvertToFullAddr(endpoint netip.AddrPort) (tcpip.FullAddress, tcpip.NetworkProtocolNumber)
- func CreateNIC(st *stack.Stack, id *tcpip.NICID, ep stack.LinkEndpoint) (tcpip.NICID, error)
- func NIC2Iface(id tcpip.NICID, info stack.NICInfo) gonnect.NetworkInterface
- func WrapErr(err error, ne NetstackErr) error
- type NetstackErr
- type Opts
- type TCPConn
- func (t *TCPConn) LocalAddr() net.Addr
- func (t *TCPConn) ReadFrom(r io.Reader) (int64, error)
- func (t *TCPConn) RemoteAddr() net.Addr
- func (t *TCPConn) SetKeepAlive(keepalive bool) error
- func (t *TCPConn) SetKeepAliveConfig(config net.KeepAliveConfig) error
- func (t *TCPConn) SetKeepAlivePeriod(d time.Duration) error
- func (t *TCPConn) SetLinger(sec int) error
- func (t *TCPConn) SetNoDelay(noDelay bool) error
- func (t *TCPConn) WriteTo(w io.Writer) (int64, error)
- type TCPListener
- type UDPConn
- func (u *UDPConn) LocalAddr() net.Addr
- func (u *UDPConn) ReadFromUDP(b []byte) (n int, addr *net.UDPAddr, err error)
- func (u *UDPConn) ReadFromUDPAddrPort(b []byte) (n int, addr netip.AddrPort, err error)
- func (u *UDPConn) ReadMsgUDP(b, oob []byte) (n, oobn, flags int, addr *net.UDPAddr, err error)
- func (u *UDPConn) ReadMsgUDPAddrPort(b, oob []byte) (n, oobn, flags int, addr netip.AddrPort, err error)
- func (u *UDPConn) RemoteAddr() net.Addr
- func (u *UDPConn) WriteMsgUDP(b, oob []byte, addr *net.UDPAddr) (n, oobn int, err error)
- func (u *UDPConn) WriteMsgUDPAddrPort(b, oob []byte, addr netip.AddrPort) (n, oobn int, err error)
- func (u *UDPConn) WriteToUDP(b []byte, addr *net.UDPAddr) (int, error)
- func (u *UDPConn) WriteToUDPAddrPort(b []byte, addr netip.AddrPort) (int, error)
Constants ¶
This section is empty.
Variables ¶
var ( // ErrSack is returned when setting TCP SACK (Selective Acknowledgment) fails. ErrSack = errors.New("set tcp sack") // ErrNIC is returned when creating a network interface card fails. ErrNIC = errors.New("create nic") // ErrNewAddr is returned when adding a protocol address fails. ErrNewAddr = errors.New("add protocol address") // ErrProm is returned when setting promiscuous mode fails. ErrProm = errors.New("set promiscuous mode") // ErrNotTCP is returned when a connection is expected to be TCP but is not. ErrNotTCP = errors.New("not a tcp conn") // ErrNotUDP is returned when a connection is expected to be UDP but is not. ErrNotUDP = errors.New("not a udp conn") // ErrUp is returned when bringing the stack up fails. ErrUp = errors.New("bring stack up") // ErrDown is returned when bringing the stack down fails. ErrDown = errors.New("bring stack down") )
Functions ¶
func ConvertToFullAddr ¶
func ConvertToFullAddr(endpoint netip.AddrPort) (tcpip.FullAddress, tcpip.NetworkProtocolNumber)
ConvertToFullAddr converts a netip.AddrPort to a tcpip.FullAddress and the corresponding network protocol number (IPv4 or IPv6).
func CreateNIC ¶
CreateNIC creates a network interface card (NIC) on the given stack. If id is nil, it generates a new NIC ID automatically. Returns the NIC ID and any error that occurred during creation.
func NIC2Iface ¶
NIC2Iface converts a gVisor NIC ID and its associated information into a gonnect.NetworkInterface implementation. It extracts protocol addresses (IPv4/IPv6) and populates the interface with standard network properties.
func WrapErr ¶
func WrapErr(err error, ne NetstackErr) error
WrapErr wraps a netstack error with a standard error to create a joined error. If either err or ne is nil, it returns nil.
Types ¶
type NetstackErr ¶
type NetstackErr interface {
String() string
}
NetstackErr is an interface for netstack error types that do not implement the standard error interface. It provides a String() method to retrieve the error message.
type Opts ¶
type Opts struct {
// MTU specifies the maximum transmission unit size. Defaults to 1500 if not set.
MTU int
// DisableSACK disables TCP Selective Acknowledgment when set to true.
DisableSACK bool
// IPAlloc provides IP address allocation for the network interfaces.
IPAlloc subnet.IPAllocator
}
Opts contains configuration options for building a gVisor netstack.
func (*Opts) BuildStack ¶
BuildStack creates and configures a new gVisor network stack with IPv4, IPv6, TCP, UDP, and ICMP protocols enabled. It applies the configured options for TCP SACK and returns the initialized stack.
func (*Opts) GetIPAlloc ¶
func (o *Opts) GetIPAlloc() subnet.IPAllocator
GetIPAlloc returns the configured IP allocator, or creates a new random one if not set.
type TCPConn ¶
TCPConn wraps a gonet.TCPConn and implements the gonnect.TCPConn interface. It provides methods for reading, writing, and configuring TCP connection parameters (though most configuration methods are no-ops for this implementation).
func (*TCPConn) RemoteAddr ¶ added in v0.3.7
func (*TCPConn) SetKeepAlive ¶
SetKeepAlive enables or disables TCP keep-alive (no-op for this implementation).
func (*TCPConn) SetKeepAliveConfig ¶
func (t *TCPConn) SetKeepAliveConfig(config net.KeepAliveConfig) error
SetKeepAliveConfig configures TCP keep-alive parameters (no-op for this implementation).
func (*TCPConn) SetKeepAlivePeriod ¶
SetKeepAlivePeriod sets the TCP keep-alive period (no-op for this implementation).
func (*TCPConn) SetLinger ¶
SetLinger sets the linger behavior for socket close (no-op for this implementation).
func (*TCPConn) SetNoDelay ¶
SetNoDelay enables or disables Nagle's algorithm (no-op for this implementation).
type TCPListener ¶
type TCPListener struct {
*gonet.TCPListener
Address net.Addr
}
TCPListener wraps a gonet.TCPListener and implements the gonnect.TCPListener interface. It provides methods for accepting TCP connections.
func (*TCPListener) Accept ¶
func (l *TCPListener) Accept() (net.Conn, error)
Accept accepts the next incoming connection and returns it wrapped as a TCPConn.
func (*TCPListener) AcceptTCP ¶
func (l *TCPListener) AcceptTCP() (gonnect.TCPConn, error)
AcceptTCP accepts the next incoming TCP connection and returns it.
func (*TCPListener) Addr ¶ added in v0.3.7
func (l *TCPListener) Addr() net.Addr
func (*TCPListener) SetDeadline ¶
func (l *TCPListener) SetDeadline(t time.Time) error
type UDPConn ¶
UDPConn wraps a gonet.UDPConn and implements the gonnect.UDPConn interface. It provides methods for reading and writing UDP packets with various address formats (net.UDPAddr and netip.AddrPort).
func (*UDPConn) ReadFromUDP ¶
ReadFromUDP reads a UDP packet and returns the number of bytes read, the remote address, and any error that occurred.
func (*UDPConn) ReadFromUDPAddrPort ¶
ReadFromUDPAddrPort reads a UDP packet and returns the number of bytes read, the remote address as a netip.AddrPort, and any error that occurred.
func (*UDPConn) ReadMsgUDP ¶
ReadMsgUDP reads a UDP packet with out-of-band data and returns the number of bytes read, out-of-band bytes read, flags, remote address, and any error. Note: Out-of-band data is not supported.
func (*UDPConn) ReadMsgUDPAddrPort ¶
func (u *UDPConn) ReadMsgUDPAddrPort( b, oob []byte, ) (n, oobn, flags int, addr netip.AddrPort, err error)
ReadMsgUDPAddrPort reads a UDP packet with out-of-band data and returns the number of bytes read, out-of-band bytes read, flags, remote address as netip.AddrPort, and any error. Note: Out-of-band data is not supported.
func (*UDPConn) RemoteAddr ¶ added in v0.3.7
func (*UDPConn) WriteMsgUDP ¶
WriteMsgUDP writes a UDP packet with optional out-of-band data to the specified remote address. Note: Out-of-band data is not supported and will return an error if provided.
func (*UDPConn) WriteMsgUDPAddrPort ¶
func (u *UDPConn) WriteMsgUDPAddrPort( b, oob []byte, addr netip.AddrPort, ) (n, oobn int, err error)
WriteMsgUDPAddrPort writes a UDP packet with optional out-of-band data to the specified remote address given as netip.AddrPort. Note: Out-of-band data is not supported and will return an error if provided.
func (*UDPConn) WriteToUDP ¶
WriteToUDP writes a UDP packet to the specified remote address.