helpers

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Apr 9, 2026 License: CC0-1.0 Imports: 12 Imported by: 0

Documentation

Overview

Package helpers provides utility functions for network operations.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoDefaultInterface = errors.New("failed to found default interface")
)

Functions

func AddrsEq

func AddrsEq(a, b net.Addr) bool

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

func AddrsSameHost(a, b net.Addr) bool

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

func CheckURLBoolKey(values map[string][]string, key string) (f bool, s bool)

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 CloseAll

func CloseAll(closers []io.Closer)

func ClosedNetworkErrToNil

func ClosedNetworkErrToNil(err error) error

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 FamilyFromNetwork

func FamilyFromNetwork(network string) string

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

func File(a any) (f *os.File, err error)

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

func IpEqual(a, b net.IP) bool

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

func IsIPNetwork(network string) bool

IsIPNetwork reports whether network is an IP-based network (ip, ip4, or ip6).

func IsLocal

func IsLocal(addr string) bool

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

func IsTCPNetwork(network string) bool

IsTCPNetwork reports whether network is a TCP-based network (tcp, tcp4, or tcp6).

func IsUDPNetwork

func IsUDPNetwork(network string) bool

IsUDPNetwork reports whether network is a UDP-based network (udp, udp4, or udp6).

func JointIPPort

func JointIPPort(ip net.IP, port int) string

JointIPPort joins an IP address and port into a host:port string.

func MultipathTCP

func MultipathTCP(c net.Conn) (bool, error)

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 NetworkFromConn(c net.Conn) string

func NormalNet

func NormalNet(network string) string

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

func PickIP(ips []net.IP, prefer int) net.IP

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

func PipeConn(inc, out net.Conn) (err error)

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

func PipePacketConn(
	inc, out net.PacketConn,
	bufSize int,
	pool bufpool.Pool,
) (err error)

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

func ReadNullTerminatedString(r io.Reader, buf []byte) (string, error)

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

func SplitHostPort(
	network, hostport string,
	defport uint16,
) (host string, port uint16)

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

func SyscallConn(a any) (syscall.RawConn, error)

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.

Types

type NetAddr

type NetAddr struct {
	Net  string
	Addr string
}

NetAddr is a simple implementation of net.Addr using a network string and address string.

func (*NetAddr) Network

func (na *NetAddr) Network() string

func (*NetAddr) String

func (na *NetAddr) String() string

type NetDefIface

type NetDefIface interface {
	Dial(
		ctx context.Context,
		network, address string,
	) (net.Conn, error)
	Interfaces() ([]gonnect.NetworkInterface, error)
}

Jump to

Keyboard shortcuts

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