Documentation
¶
Overview ¶
Package helpers provides utility functions for network operations.
Index ¶
- Variables
- func AddrsEq(a, b net.Addr) bool
- func AddrsSameHost(a, b net.Addr) bool
- func CheckURLBoolKey(values map[string][]string, key string) (f bool, s bool)
- func CloseAll(closers []io.Closer)
- func ClosedNetworkErrToNil(err error) error
- func CopyPacket(dst, src net.PacketConn, bufSize int, pool bufpool.Pool) (written int64, err error)
- func DefaultInterface(ctx context.Context, n NetDefIface) (gonnect.NetworkInterface, error)
- func Drain[T any](c <-chan T)
- func FamilyFromNetwork(network string) string
- func File(a any) (f *os.File, err error)
- func IpEqual(a, b net.IP) bool
- func IsIPNetwork(network string) bool
- func IsLocal(addr string) bool
- func IsTCPNetwork(network string) bool
- func IsUDPNetwork(network string) bool
- func JointIPPort(ip net.IP, port int) string
- func MultipathTCP(c net.Conn) (bool, error)
- func NetworkFromConn(c net.Conn) string
- func NormalNet(network string) string
- func PickIP(ips []net.IP, prefer int) net.IP
- func PipeConn(inc, out net.Conn) (err error)
- func PipePacketConn(inc, out net.PacketConn, bufSize int, pool bufpool.Pool) (err error)
- func ReadNullTerminatedString(r io.Reader, buf []byte) (string, error)
- func ReadUntilClose(rc io.ReadCloser)
- func SplitHostPort(network, hostport string, defport uint16) (host string, port uint16)
- func SyscallConn(a any) (syscall.RawConn, error)
- type NetAddr
- type NetDefIface
Constants ¶
This section is empty.
Variables ¶
var (
ErrNoDefaultInterface = errors.New("failed to found default interface")
)
Functions ¶
func AddrsEq ¶
AddrsEq reports whether a and b are equal addresses (same host and port). It compares IP addresses and ports for TCP/UDP addr types, or string representation for other types.
func AddrsSameHost ¶
AddrsSameHost reports whether a and b represent addresses on the same host. It compares IP addresses for TCP/UDP addr types, or host strings for other types. Port numbers are ignored in the comparison.
func CheckURLBoolKey ¶
CheckURLBoolKey checks if a key exists in a URL values map and interprets its value as a boolean. It returns two booleans: f is true if the key exists with a truthy value, s is true if the key exists at all. Truthy values include: "true", "yes", "ok", "1", or an empty string.
func ClosedNetworkErrToNil ¶
ClosedNetworkErrToNil returns nil if err represents a closed network connection error. It unwraps err to find the root cause and checks for common closed connection messages. Returns the original error if it is not a closed connection error.
func CopyPacket ¶
func CopyPacket( dst, src net.PacketConn, bufSize int, pool bufpool.Pool, ) (written int64, err error)
CopyPacket copies packets from src to dst, preserving packet boundaries. It reads individual datagrams from src and writes them to dst. bufSize specifies the buffer size to use, and pool is used for buffer allocation.
func DefaultInterface ¶
func DefaultInterface( ctx context.Context, n NetDefIface, ) (gonnect.NetworkInterface, error)
func Drain ¶ added in v0.5.0
func Drain[T any](c <-chan T)
Drain reads all elements from c while there are some. It doesnt. Close channel.
func FamilyFromNetwork ¶
FamilyFromNetwork returns the IP family ("ip", "ip4", or "ip6") for a given network string. For example, "tcp", "udp", or "ip" all return "ip", while "tcp4", "udp4", "ip4" return "ip4".
func File ¶
File returns the underlying *os.File from a connection. It checks if the value implements the withFile interface, or unwraps the value to find one that does. Returns nil if the value is nil or doesn't implement the interface.
func IpEqual ¶
IpEqual reports whether a and b are equal IP addresses. Nil IPs are considered equal to each other, and not equal to non-nil IPs.
func IsIPNetwork ¶
IsIPNetwork reports whether network is an IP-based network (ip, ip4, or ip6).
func IsLocal ¶
IsLocal reports whether addr is a local address ("localhost" or a loopback IP). It returns true for "localhost", "127.0.0.1", "::1", or any IP in the 127.0.0.0/8 range.
func IsTCPNetwork ¶
IsTCPNetwork reports whether network is a TCP-based network (tcp, tcp4, or tcp6).
func IsUDPNetwork ¶
IsUDPNetwork reports whether network is a UDP-based network (udp, udp4, or udp6).
func JointIPPort ¶
JointIPPort joins an IP address and port into a host:port string.
func MultipathTCP ¶
MultipathTCP reports whether the connection uses Multipath TCP (MPTCP). It checks if the connection implements the withMultipathTCP interface, or unwraps the connection to find one that does.
func NetworkFromConn ¶
func NormalNet ¶
NormalNet normalizes a network string by removing the IP version suffix. For example, "tcp4" becomes "tcp", "udp6" becomes "udp", and "ip4" becomes "ip".
func PickIP ¶
PickIP selects an IP address from the given list based on preference. If prefer is 4, IPv4 addresses are preferred; if 6, IPv6 addresses are preferred. If no preference is specified (prefer != 4 && prefer != 6), a random IP is returned. If the preferred family is not available, an IP from the other family is returned.
func PipeConn ¶
PipeConn copies data bidirectionally between two connections.
PipeConn establishes a full-duplex pipe between two connections, copying data from inc->out and out->inc concurrently. It blocks until both directions complete or an error occurs and returns the first error encountered (if any).
func PipePacketConn ¶
PipePacketConn copies packets bidirectionally between two packet connections.
PipePacketConn establishes a full-duplex pipe between two packet connections, copying packets from inc->out and out->inc concurrently. Unlike PipeConn, this function preserves packet boundaries by reading and writing individual datagrams. It blocks until both directions complete or an error occurs and returns the first error encountered (if any).
func ReadNullTerminatedString ¶
ReadNullTerminatedString reads bytes from r until a null byte (0x00) is encountered. It returns the resulting string (excluding the null terminator). An error is returned if the string exceeds buf's capacity or if reading fails.
func ReadUntilClose ¶
func ReadUntilClose(rc io.ReadCloser)
ReadUntilClose reads from rc until an error occurs, then closes rc. This function is useful for draining a connection before closing it.
func SplitHostPort ¶
SplitHostPort splits a host:port string into host and port components. If no port is present or the port is invalid, defport is used as the default. The network parameter is used for service name lookup via LookupPortOffline.
func SyscallConn ¶
SyscallConn returns the underlying syscall.RawConn from a connection. It checks if the value implements the withSyscall interface, or unwraps the value to find one that does. Returns nil if the value is nil or doesn't implement the interface.