Documentation ¶
Overview ¶
Package pnet provides IP-related functions with few dependencies beyond the net package
Index ¶
- Constants
- Variables
- func Addr46(addr netip.Addr) (addr46 netip.Addr)
- func AddrPortFromAddr(addr net.Addr) (near netip.AddrPort, err error)
- func AddrPortToTCPAddr(addrPort netip.AddrPort) (addrInterface net.Addr)
- func AddrPortToUDPAddr(addrPort netip.AddrPort) (addrInterface net.Addr)
- func AddrPortToUDPAddr2(addrPort netip.AddrPort) (addr *net.UDPAddr)
- func AddrSlicetoPrefix(addrs []net.Addr, do46 bool) (prefixes []netip.Prefix, err error)
- func AddrToIPAddr(addr netip.Addr) (addrInterface net.Addr)
- func CachedName(ifIndex IfIndex) (name string)
- func CheckRedirect(req *http.Request, via []*http.Request) (err error)
- func DumpHardwareAddr(a net.HardwareAddr) (s string)
- func EnsureZone(addr netip.Addr, ifName string, ifIndex IfIndex, acceptNumeric ...bool) (addr2 netip.Addr, didChange, isNumeric bool, err error)
- func Get(requestURL string, tlsConfig *tls.Config, ctx context.Context) (resp *http.Response, err error)
- func HardwareAddrInterface(a net.HardwareAddr) (netInterface *net.Interface, isErrNoSuchInterface bool, err error)
- func IPAddr(IP net.IP, index IfIndex, zone string) (ipa *net.IPAddr, err error)
- func IPNetString(ipNet net.IPNet) (s string)
- func IPNetToPrefix(netIPNet *net.IPNet, noIs4In6Translation ...bool) (prefix netip.Prefix, err error)
- func InterfaceAddrs(netInterface *net.Interface) (i4, i6 []netip.Prefix, err error)
- func InterfaceFromAddr(addr netip.Addr) (netInterface *net.Interface, prefix netip.Prefix, isNoSuchInterface bool, ...)
- func InterfacePrefix(netInterface *net.Interface, addr netip.Addr) (prefix netip.Prefix, err error)
- func Interfaces() (interfaces []net.Interface, err error)
- func InvertMask(IPMask net.IPMask) (out net.IPMask)
- func IsBroadcast(addr netip.Addr, IPMask net.IPMask) (isBroadcast bool)
- func IsDirect(route netip.Prefix) bool
- func IsErrClosed(err error) (isErrNetClosing bool)
- func IsHardwareAddrLength(byts []byte) (isHardwareAddrLength bool)
- func IsIPv4(ip net.IP) (isIPv4 bool)
- func IsIPv6(ip net.IP) (isIPv6 bool)
- func IsNetwork(IP net.IP, IPMask net.IPMask) (isNetwork bool)
- func IsNonGlobalIPv6(addr netip.Addr) (needsZone bool)
- func IsNzIP(ip net.IP) bool
- func IsZeros(p net.IP) bool
- func Listen(network Network, addr string, cancel *atomic.Pointer[context.CancelFunc]) (listener net.Listener, err error)
- func MaskToBits(byts []byte) (bits int, err error)
- func NameCache() (m map[IfIndex]string)
- func NetworkPrefixBitCount(byts []byte) (bits int)
- func NewTLSConfig(cert *x509.Certificate) (tlsConfig *tls.Config)
- func NewTransport(tlsConfig *tls.Config) (httpTransport *http.Transport)
- func Prefix46(prefix netip.Prefix) (prefix46 netip.Prefix)
- func SetTestMap(testMap map[IfIndex]string, disableUpdate bool) (oldMap map[IfIndex]string)
- func SplitAddrPort(addrPort netip.AddrPort) (IP net.IP, port int, zone string)
- func UpdateNameCache() (err error)
- func Zone(addr netip.Addr) (zone string, znum int, hasZone, isNumeric bool)
- type Action
- type Callback
- type Destination
- type HttpClient
- type IfIndex
- func (ifIndex IfIndex) Interface() (netInterface *net.Interface, isErrNoSuchInterface bool, err error)
- func (ifIndex IfIndex) InterfaceAddrs(useNameCache ...NameCacher) (name string, i4, i6 []netip.Prefix, err error)
- func (ifIndex IfIndex) InterfaceIndex() (interfaceIndex int)
- func (ifIndex IfIndex) IsValid() (isValid bool)
- func (ifIndex IfIndex) String() (s string)
- func (ifIndex IfIndex) Zone() (zone string, isNumeric bool, err error)
- type InterfaceCache
- func (i *InterfaceCache) CachedName(ifIndex IfIndex, noUpdate ...NameCacher) (name string, err error)
- func (i *InterfaceCache) CachedNameNoUpdate(ifIndex IfIndex) (name string)
- func (i *InterfaceCache) Init() (i0 *InterfaceCache)
- func (i *InterfaceCache) Map() (m map[IfIndex]string)
- func (i *InterfaceCache) SetMap(newMap map[IfIndex]string) (oldMap map[IfIndex]string)
- func (i *InterfaceCache) Update() (m map[IfIndex]string, err error)
- type LinkAddr
- func (a *LinkAddr) Dump() (s string)
- func (a *LinkAddr) Interface() (netInterface *net.Interface, isNoSuchInterface bool, err error)
- func (a *LinkAddr) IsComplete() (isComplete bool)
- func (a *LinkAddr) IsValid() (isValid bool)
- func (a *LinkAddr) IsZeroValue() (isZeroValue bool)
- func (a *LinkAddr) NonZero() (s string)
- func (a *LinkAddr) OneString() string
- func (a *LinkAddr) SetHw(hw net.HardwareAddr) (err error)
- func (a *LinkAddr) SetName(name string)
- func (a *LinkAddr) String() (s string)
- func (a *LinkAddr) UpdateFrom(b *LinkAddr) (isComplete bool)
- func (a *LinkAddr) UpdateName() (linkAddr *LinkAddr, err error)
- func (a *LinkAddr) ZoneID() string
- type Message
- type NameCacher
- type Network
- type NextHop
- func NewNextHop(gateway netip.Addr, linkAddr *LinkAddr, src netip.Addr) (nextHop *NextHop)
- func NewNextHop2(index IfIndex, gateway netip.Addr, src netip.Addr) (next *NextHop, err error)
- func NewNextHop3(gateway netip.Addr, linkAddr *LinkAddr, src netip.Addr, nIPv4, nIPv6 int) (nextHop *NextHop)
- func NewNextHopCounts(gateway netip.Addr, linkAddr *LinkAddr, src netip.Addr, ...) (nextHop *NextHop, err error)
- func (n *NextHop) Dump() (s string)
- func (n *NextHop) HasGateway() bool
- func (n *NextHop) HasSrc() bool
- func (n *NextHop) IsZeroValue() (isZeroValue bool)
- func (n *NextHop) Name(useNameCache ...NameCacher) (name string, err error)
- func (n *NextHop) String() (s string)
- func (n *NextHop) Target() (gateway netip.Addr, s string)
- type Route
- type SocketListener
- func (s *SocketListener[C]) AcceptConnections(handler func(C))
- func (s *SocketListener[C]) AddrPort() (addrPort netip.AddrPort, err error)
- func (s *SocketListener[C]) Ch() (ch <-chan error)
- func (s *SocketListener[C]) Close() (err error)
- func (s *SocketListener[C]) Err(errp *error)
- func (s *SocketListener[C]) IsAccept() (isAcceptThread bool)
- func (s *SocketListener[C]) Listen(socketString string) (err error)
- func (s *SocketListener[C]) WaitCh() (closeWait chan struct{})
- type SocketTransport
- type TCPListener
- type Tcp
- type UDP
- type UDPFunc
Constants ¶
const ( HardwareAddrMac48 = 6 HardwareAddrEui64 = 8 HardwareAddrInfini = 20 )
const ( TransportTCP = iota + 1 TransportUDP TransportIP TransportUnix )
const Do46 = true
const (
UseInterfaceNameCache = true
)
Variables ¶
var DefaultRouteIPv4 = netip.MustParsePrefix("0.0.0.0/0")
DefaultRouteIPv4 is the default route “0/0” for IPv4
var DefaultRouteIPv6 = netip.MustParsePrefix("::/0")
DefaultRouteIPv6 is the default route “::/0” for IPv6
var ErrNoSuchInterface = func() (err error) { for _, e := net.InterfaceByName("a b"); e != nil; e = errors.Unwrap(e) { err = e } if err == nil { panic(perrors.NewPF("failed to obtain NoSuchInterface from InterfaceByName")) } return }()
ErrNoSuchInterface is the value net.errNoSuchInterface
Usage:
if errors.Is(err, pnet.ErrNoSuchInterface) { …
var HardwareAddrLengths = []int{HardwareAddrMac48, HardwareAddrEui64, HardwareAddrInfini}
var HardwareAddrLengthsWithZero = append([]int{0}, HardwareAddrLengths...)
var IPv4DefaultNetwork = netip.MustParsePrefix("0.0.0.0/0")
var IPv4VpnPrefix = netip.MustParsePrefix("0.0.0.0/1")
var IPv6DefaultNetwork = netip.MustParsePrefix("::/0")
var IPv6VpnPrefix = netip.MustParsePrefix("::/3")
var VPNRoute0IPv4 = netip.MustParsePrefix("0.0.0.0/1")
VPNRoute0IPv4 is overriding VPN route “0/1” for IPv4
var VPNRoute128IPv4 = netip.MustParsePrefix("128.0.0.0/1")
VPNRoute128IPv4 is overriding VPN route “128/1” for IPv4
var VPNRouteIPv6 = netip.MustParsePrefix("::/3")
VPNRouteIPv6 is overriding VPN route “::/3” for IPv6
Functions ¶
func Addr46 ¶ added in v0.4.112
Addr46 converts 4in6 IPv6 addresses to IPv4 for consistent IPv4/IPv6
- IPv6 has a special class of addresses represnting an IPv4 address
- IPv6 “::ffff:192.0.2.128” represents the IPv4 address “192.0.2.128”
- Addr46 converts such addresses to IPv4
func AddrPortFromAddr ¶ added in v0.4.153
AddrPortFromAddr converts tcp-protocol string-based net.Addr to binary netip.AddrPort
- addr should be net.Addr for tcp-network address implemented by net.TCPAddr
- netip.AddrPort is a binary-coded socket address
- net.Addr is legacy for net.Dial using strings for Network and socket address
- net.TCPAddr returns
- — Network “tcp”
- — String like “[fe80::%eth0]:80”
func AddrPortToTCPAddr ¶ added in v0.4.87
AddrPortToTCPAddr: Network() "tcp"
func AddrPortToUDPAddr ¶ added in v0.4.87
AddrPortToUDPAddr: Network() "udp"
func AddrPortToUDPAddr2 ¶ added in v0.4.91
func AddrSlicetoPrefix ¶ added in v0.4.112
AddrSlicetoPrefix returns a netip.Prefix slice from an Addr slice
- net.Interface.Addrs returns []net.Addr which is really []*net.IPNet
- IPNet is cidr not address: netip.Prefix
func AddrToIPAddr ¶ added in v0.4.87
AddrToIPAddr returns the net.Addr for an netip.Addr
- net.Addr is legacy interface for net.Dial using strings
- — net.Addr.Network is string like “tcp”
- — net.Addr.String is socket address literal like “1.2.3.4:80”
- netip.Addr is binary value-literals based on integers
- net.IPAddr is a legacy implementation of net.Addr for IPv4 or IPv6 addresses
- — net.IPAddr.Network returns “ip”
- — net.IPAddr.String returns “1.2.3.4” or “fe80::%eth0”
func CachedName ¶ added in v0.4.88
func DumpHardwareAddr ¶ added in v0.4.86
func DumpHardwareAddr(a net.HardwareAddr) (s string)
func EnsureZone ¶ added in v0.4.86
func EnsureZone(addr netip.Addr, ifName string, ifIndex IfIndex, acceptNumeric ...bool) (addr2 netip.Addr, didChange, isNumeric bool, err error)
EnsureZone adds IPv6 zone as applicable
- only non-global IPv6 should have zone
- if acceptNumeric true no index to interface-name translation attempts take place. otherwise interface-name zone is preferred
- a non-numeric zone is attempted from: addr, ifName
- number cinversion to interfa e is attempted: addr, ifIndex
- acceptNumeric true leaves an existing numeric zone
func HardwareAddrInterface ¶ added in v0.4.87
func IPNetToPrefix ¶ added in v0.4.86
func IPNetToPrefix(netIPNet *net.IPNet, noIs4In6Translation ...bool) (prefix netip.Prefix, err error)
IPNetToPrefix returns the netip.Prefix that corresponds to older type net.IPNet
- net.IPNet input is "1.2.3.4/24" or "fe80::1/64"
- returned netip.Prefix values are valid
- returned IPv6 addresses has blank Zone
func InterfaceAddrs ¶ added in v0.4.87
InterfaceAddrs gets Addresses for interface
- netInterface.Name is interface name "eth0"
- netInterface.Addr() returns assigned IP addresses
func InterfaceFromAddr ¶ added in v0.4.88
func InterfaceFromAddr(addr netip.Addr) (netInterface *net.Interface, prefix netip.Prefix, isNoSuchInterface bool, err error)
InterfaceFromAddr finds network interface and prefix for addr
- returns interface and prefix or error
- first uses Zone, then scans all interfaces for prefixes
- addr must be valid
func InterfacePrefix ¶ added in v0.4.88
InterfacePrefix returns the network prefix assigned to netInterface that contains addr
- if addr is not part of any prefix, returned prefix is invalid
func Interfaces ¶ added in v0.4.87
func InvertMask ¶
InvertMask inverts the bits of a mask the mask for 1.2.3.4/24 is normally ffffff00 or []byte{255, 255, 255, 0}
func IsBroadcast ¶
IsBroadcast determines whether addr is the last address for Mask
- the last address is typically broadcast
- for 1.2.3.4/24 the network address 1.2.3.255 returns true
func IsDirect ¶
IsDirect determines if the route is direct
- a direct route has mask 32 or 128 bit length /32 /128
func IsErrClosed ¶ added in v0.4.26
IsErrClosed returns true if err is when waiting for accept and the socket is closed
- can be used with net.Conn.Accept
func IsHardwareAddrLength ¶ added in v0.4.28
func IsNetwork ¶
IsNetwork determines if IP is the network address (all zeros) for this Mask for 1.2.3.4/24 the network address 1.2.3.0 returns true
func IsNonGlobalIPv6 ¶ added in v0.4.86
IsNonGlobalIPv6 returns if addr is an IPv6 address that should have zone
func Listen ¶ added in v0.4.151
func Listen( network Network, addr string, cancel *atomic.Pointer[context.CancelFunc], ) (listener net.Listener, err error)
Listen obtains a tcp or other listener
- network must be set. NetworkDefault yields “panic: listen: unknown network”
- addr can be empty
- default port is ephemeral, a high number like 59321
- if IPv6 is allowed, default host typically becomes “::” not “::1”
- limited to IPv4, eg. NetworkTCP4, default host typically becomes “0.0.0.0” not “127.0.0.1”
func MaskToBits ¶ added in v0.4.86
MaskToBits returns number of leading 1-bits in byts
- convert from net.IPMask to netip.Prefix
func NetworkPrefixBitCount ¶ added in v0.4.29
func NewTLSConfig ¶
func NewTLSConfig(cert *x509.Certificate) (tlsConfig *tls.Config)
func SetTestMap ¶ added in v0.4.90
func SplitAddrPort ¶ added in v0.4.87
func UpdateNameCache ¶ added in v0.4.87
func UpdateNameCache() (err error)
Types ¶
type Action ¶ added in v0.4.86
type Action uint8
Action describes the message action
const ( AddRoute Action = iota + 1 // AddRoute describes a new route added to the routing table DeleteRoute // DeleteRoute describes a deleted route RouteReport // RouteReport describes an existing route Partition // Partition describes an existing route that may be overridden AddHost // AddHost is a host added to the routing table DeleteHost // DeleteHost is a deleted host HostReport // HostReport is an existing host IfAddAddr // IfAddAddr Add address to interface IfDeleteAddr // IfDeleteAddr Delete address from interface IfStatus // network-interface upDownStatus or down string AddMulticast // AddMulticast annouces a multicast address DeleteMulticast // DeleteMulticast annouces a multicast address disappearing )
func (Action) Description ¶ added in v0.4.88
type Callback ¶
type Callback func(msg Message)
Callback allows for processing of routing message,eg. populating a map
type Destination ¶
Destination represents a selector for routing, ie. an IPv4 or IPv6 address with zone and prefix. go1.18 introduced netip.Prefix for this purpose see Linux: ip route add. [ root PREFIX ] [ match PREFIX ] [ exact PREFIX ] [ table TABLE_ID ] [ vrf NAME ] [ proto RTPROTO ] [ type TYPE ] [ scope SCOPE ] contains IP, Zone and Mask
func NewDestination ¶
func NewDestination(prefix netip.Prefix) (d *Destination)
NewDestination instantiates Destination. addr is IPv4 address or IPv6 address with Zone. prefix is number of bits actually used 0…32 for IPv4, 0…128 for IPv6
func (Destination) IsDefaultRoute ¶ added in v0.4.83
func (d Destination) IsDefaultRoute() (isDefault bool)
IsDefaultRoute returns whether Destination is a default route:
- IPv4: 0/0 or 0.0.0.0/0
- IPv6: ::
func (Destination) IsValid ¶ added in v0.4.86
func (d Destination) IsValid() (err error)
func (Destination) Key ¶
func (d Destination) Key() (key netip.Prefix)
Key is a string suitable as a key in a map
func (Destination) String ¶
func (d Destination) String() (s string)
"1.2.3.4/24" or "2000::/3" - abbreviate IPv4: "127.0.0.0/8" → "127/8" - IPv4 default route: "0.0.0.0/0" → "0/0"
- IPv6 default route stays "::/0"
type HttpClient ¶
func NewHttpClient ¶
func NewHttpClient(tlsConfig *tls.Config) (httpClient *HttpClient)
type IfIndex ¶
type IfIndex uint32
IfIndex is a dynamic reference to a network interface on Linux systems
func NewIfIndex ¶ added in v0.4.28
func NewIfIndexInt ¶ added in v0.4.86
func (IfIndex) Interface ¶
func (ifIndex IfIndex) Interface() (netInterface *net.Interface, isErrNoSuchInterface bool, err error)
Interface gets net.Interface for ifIndex
- netInterface.Name is interface name "eth0"
- netInterface.Addr() returns assigned IP addresses
func (IfIndex) InterfaceAddrs ¶ added in v0.4.86
func (ifIndex IfIndex) InterfaceAddrs(useNameCache ...NameCacher) (name string, i4, i6 []netip.Prefix, err error)
InterfaceAddrs gets Addresses for interface
- netInterface.Name is interface name "eth0"
- netInterface.Addr() returns assigned IP addresses
func (IfIndex) InterfaceIndex ¶ added in v0.4.28
Interface gets net.Interface for ifIndex
- InterfaceIndex is unique for promting methods
type InterfaceCache ¶ added in v0.4.87
type InterfaceCache struct {
// contains filtered or unexported fields
}
InterfaceCache is a cache mapping per-boot stable network interface index #1 to name "lo0". Thread-Safe
func NewInterfaceCache ¶ added in v0.4.87
func NewInterfaceCache() (interfaceCache *InterfaceCache)
NewInterfaceCache returns a cache mapiing network-interfac eindex to name. Thread-Safe
func (*InterfaceCache) CachedName ¶ added in v0.4.87
func (i *InterfaceCache) CachedName(ifIndex IfIndex, noUpdate ...NameCacher) (name string, err error)
CachedName retrieves a name by index with optional cache update. Thread-Safe
- default is to update
- unknown index returns empty string
func (*InterfaceCache) CachedNameNoUpdate ¶ added in v0.4.87
func (i *InterfaceCache) CachedNameNoUpdate(ifIndex IfIndex) (name string)
CachedNameNoUpdate retrieves a name by index with no cache update. Thread-Safe
func (*InterfaceCache) Init ¶ added in v0.4.87
func (i *InterfaceCache) Init() (i0 *InterfaceCache)
Init loads the cache, an opeation that may fail. Funtional chaining. Thread-Safe
func (*InterfaceCache) Map ¶ added in v0.4.87
func (i *InterfaceCache) Map() (m map[IfIndex]string)
Map returns a copy of the current cache. Thread-Safe
type LinkAddr ¶
type LinkAddr struct { IfIndex // 0 is none Name string // "" none net.HardwareAddr // []byte }
LinkAddr contains an Ethernet mac address, its interface name and interface index
func NewLinkAddr ¶
NewLinkAddr instantiates LinkAddr
func (*LinkAddr) Interface ¶ added in v0.4.87
Interface returns net.Interface associated with LinkAddr
- order is index, name, mac
- if LinkAddr is zero-value, nil is returned
func (*LinkAddr) IsComplete ¶ added in v0.4.90
func (*LinkAddr) IsZeroValue ¶ added in v0.4.86
func (*LinkAddr) NonZero ¶ added in v0.4.86
"#13_en5_00:00:5e:00:53:01"
- zero-values are skipped
- zero-value: "zero-value"
func (*LinkAddr) OneString ¶
OneString picks the most meaningful value
- interface name or hardware address or #interface index or "0"
func (*LinkAddr) UpdateFrom ¶ added in v0.4.90
func (*LinkAddr) UpdateName ¶
UpdateName attempts to populate interface name if not already present
type Message ¶
Message is a portable routing message emitted by the netlink socket or obtain via sysctl
type NameCacher ¶ added in v0.4.90
type NameCacher uint8
NameCacher contaisn instructions for interface-name cache read: NoCache Update NoUpdate
const ( NoCache NameCacher = iota // name cache should not be used in the api call Update // use name cache and update the cache first NoUpdate // use name cache without update. Mechanic for ongoing name cache update is required )
type Network ¶ added in v0.4.94
type Network string
the type of Network
- NetworkTCP NetworkTCP4 NetworkTCP6
- NetworkUDP NetworkUDP4 NetworkUDP6
- NetworkIP NetworkIP4 NetworkIP6
- NetworkUnix NetworkUnixGram NetworkUnixPacket
const ( NetworkDefault Network = "" // default NetworkTCP Network = "tcp" // tcp IPv4 or IPv6 NetworkTCP4 Network = "tcp4" // net network tcp ipv4 NetworkTCP6 Network = "tcp6" // tcp IPv6 NetworkUDP Network = "udp" // udp is udp IPv4 or IPv6 NetworkUDP4 Network = "udp4" // udp4 is udp IPv4 NetworkUDP6 Network = "udp6" // udp6 is udp IPv6 NetworkIP Network = "ip" // ip is IP protocol IPv4 or IPv6 addressing NetworkIP4 Network = "ip4" // ip4 is IP protocol IPv4 NetworkIP6 Network = "ip6" // ip6 is IP protocol IPv6 NetworkUnix Network = "unix" // unix is tcp or udp over Unix socket NetworkUnixGram Network = "unixgram" // unixgram is udp over Unix socket NetworkUnixPacket Network = "unixpacket" // unixpacket is tcp over Unix socket )
func ParseNetwork ¶ added in v0.4.151
ParseNetwork checks if network is valid
- tcp tcp4 tcp6 udp udp4 udp6 ip ip4 ip6 unix unixgram unixpacket
type NextHop ¶
type NextHop struct { /* if NextHop is an address on the local host or on a local subnet, Gateway is nil LinkAddr describes the local interface Src is the address on that local interface If Nexthop is remote, beyond any local subnet, Gateway is an IP on a local subnet LinkAddr describes the local interface for that subnet Src is the address on that local interface */ Gateway netip.Addr LinkAddr // LinkAddr is the hosts’s network interface where to send packets Src netip.Addr // the source ip to use for originating packets on LinkAddr // contains filtered or unexported fields }
NextHop describes a route target
func NewNextHop ¶
NewNextHop assembles a route destination
func NewNextHop2 ¶
NewNextHop2 assembles a route destination based on IfIndex
func NewNextHop3 ¶ added in v0.4.90
func NewNextHopCounts ¶ added in v0.4.28
func NewNextHopCounts(gateway netip.Addr, linkAddr *LinkAddr, src netip.Addr, useNameCache ...NameCacher, ) (nextHop *NextHop, err error)
NewNextHopCounts returns NextHop with current IP address counts
- if input LinkAddr does not have interface name, interface name is added to output nextHop
- 6in4 are converted to IPv4
func (*NextHop) HasGateway ¶
HasGateway determines if next hop uses a remote gateway
func (*NextHop) IsZeroValue ¶ added in v0.4.86
func (*NextHop) Name ¶ added in v0.4.87
func (n *NextHop) Name(useNameCache ...NameCacher) (name string, err error)
Name returns nextHop interface name
- name can be returned empty
- name of Linkaddr, then interface from index, name, mac
func (*NextHop) Target ¶
Target describes the destination for this next hop
- gateway is invalid for local network targets or gateway unspecified address "0.0.0.0"
- s is:
- empty string for nil NextHop
- network interface name mac index or 0 for gateway missing, invalid or unspecified
- otherwise gateway ip, interface description and source IP or source cidr
type Route ¶
type Route struct { Destination NextHop }
Route describes a routing table route with destination and next hop
type SocketListener ¶ added in v0.4.94
TCPListener embeds net.TCPListener
- panic-handled connection threads
- Ch: real-time error channel or collecting errors after close: Err
- WaitCh: idempotent waitable thread-terminating Close
- C is net.Conn, *net.TCPConn, …
func NewSocketListener ¶ added in v0.4.94
func NewSocketListener[C net.Conn]( listener net.Listener, network Network, transport SocketTransport, ) (socket *SocketListener[C])
NewSocketListener returns object for receiving IPv4 tcp connections
- handler must invoke net.Conn.Close
func (*SocketListener[C]) AcceptConnections ¶ added in v0.4.94
func (s *SocketListener[C]) AcceptConnections(handler func(C))
AcceptConnections is a blocking function handling inbound connections
- AcceptConnections can only be invoked once
- accept of connections continues until Close is invoked
- handler must invoke net.Conn.Close
func (*SocketListener[C]) AddrPort ¶ added in v0.4.94
func (s *SocketListener[C]) AddrPort() (addrPort netip.AddrPort, err error)
func (*SocketListener[C]) Ch ¶ added in v0.4.94
func (s *SocketListener[C]) Ch() (ch <-chan error)
Ch returns a real-time error channel
- unread errors can also be collected using [TCPListener.Err]
func (*SocketListener[C]) Close ¶ added in v0.4.94
func (s *SocketListener[C]) Close() (err error)
func (*SocketListener[C]) Err ¶ added in v0.4.94
func (s *SocketListener[C]) Err(errp *error)
Err returns all unread errors
- errors can also be read using [TCPListener.Ch]
func (*SocketListener[C]) IsAccept ¶ added in v0.4.94
func (s *SocketListener[C]) IsAccept() (isAcceptThread bool)
IsAccept indicates whether the listener is functional and accepting incoming connections
func (*SocketListener[C]) Listen ¶ added in v0.4.94
func (s *SocketListener[C]) Listen(socketString string) (err error)
Listen binds listening to a near socket
- socketString is host:port "1.2.3.4:80" "wikipedia.com:443" "/some/unix/socket"
- — for TCP UDP IP host must resolve to an assigned near IP address
- — — if host is blank, it is for localhost
- — — to avoid DNS resolution host should be blank or literal IP address "1.2.3.4:0"
- — for TCP UDP port must be literal port number 0…65534 where 0 means a temporary port
- Listen can be repeatedly invoked until it succeeds
func (*SocketListener[C]) WaitCh ¶ added in v0.4.94
func (s *SocketListener[C]) WaitCh() (closeWait chan struct{})
WaitCh returns a channel that closes when [] completes
- ListenTCP4.Close needs to have been invoked for the channel to close
type SocketTransport ¶ added in v0.4.94
type SocketTransport uint8
func (SocketTransport) IsValid ¶ added in v0.4.94
func (t SocketTransport) IsValid() (isValid bool)
func (SocketTransport) String ¶ added in v0.4.94
func (t SocketTransport) String() (s string)
type TCPListener ¶ added in v0.4.62
type TCPListener struct { *net.TCPListener // the TCP IPv4 or IPv6 listening socket promoting TCP-specific methods SocketListener[*net.TCPConn] // connection-handler argument is *net.TCPConn }
TCPListener embeds net.TCPListener
- panic-handled connection threads receiving *net.TCPConn
- Ch: real-time error channel or
- Err: collecting errors after close
- idempotent panic-free awaitable Close
- WaitCh: channel closing on Close complete
func NewTCPListener ¶ added in v0.4.94
func NewTCPListener(network ...Network) (socket *TCPListener)
NewTCPListener returns object for receiving IPv4 tcp connections
func (*TCPListener) Close ¶ added in v0.4.93
func (s *TCPListener) Close() (err error)
- idempotent panic-free awaitable Close
type UDP ¶
type UDP struct { Network string F UDPFunc MaxSize int net.UDPAddr // struct IP Port Zone ListenInvoked atomic.Bool StartingListen sync.WaitGroup ErrCh chan<- error IsListening atomic.Bool NetUDPConn *net.UDPConn Addr net.Addr IsShutdown atomic.Bool // contains filtered or unexported fields }