pnet

package
v0.4.60 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2023 License: ISC Imports: 18 Imported by: 0

Documentation

Overview

Package pnet provides IP-related functions with few dependencies beyond the net package

Index

Constants

View Source
const (
	HardwareAddrMac48  = 6
	HardwareAddrEui64  = 8
	HardwareAddrInfini = 20
)
View Source
const (
	// DefaultRouteIPv4 is the default route 0/0 for IPv4
	DefaultRouteIPv4 = "0.0.0.0/0"
	// VPNRoute0IPv4 is overriding VPN route 0/1 for IPv4
	VPNRoute0IPv4 = "0.0.0.0/1"
	// VPNRoute128IPv4 is overriding VPN route 128/1 for IPv4
	VPNRoute128IPv4 = "128.0.0.0/1"
	// DefaultRouteIPv6 is the default route ::/0 for IPv6
	DefaultRouteIPv6 = "::/0"
	// VPNRouteIPv6 is overriding VPN route ::/3 for IPv6
	VPNRouteIPv6 = "::/3"
)
View Source
const (
	TCPNetwork = "tcp"
)

Variables

View Source
var HardwareAddrLengthsWithZero = append([]int{0}, HardwareAddrLengths...)

Functions

func CheckRedirect

func CheckRedirect(req *http.Request, via []*http.Request) (err error)

func Get

func Get(requestURL string, tlsConfig *tls.Config, ctx context.Context) (resp *http.Response, err error)

func IPAddr

func IPAddr(IP net.IP, index IfIndex, zone string) (ipa *net.IPAddr, err error)

IPAddr returns IPAddr from IP and IfIndex to IPAddr

func IPNetString

func IPNetString(ipNet net.IPNet) (s string)

IPNetString is abbreviated form 0/0

func InvertMask

func InvertMask(IPMask net.IPMask) (out net.IPMask)

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

func IsBroadcast(IP net.IP, IPMask net.IPMask) (isBroadcast bool)

IsBroadcast determines IP is the last address for Mask for 1.2.3.4/24 the network address 1.2.3.255 returns true

func IsDirect

func IsDirect(IP net.IP, mask *net.IPMask) bool

IsDirect determines if the route is direct

func IsDirectIPNet

func IsDirectIPNet(IPNet *net.IPNet) bool

IsDirectIPNet determines if the IPNET represents a direct route

func IsErrClosed added in v0.4.26

func IsErrClosed(err error) (isErrNetClosing bool)

func IsHardwareAddrLength added in v0.4.28

func IsHardwareAddrLength(byts []byte) (isHardwareAddrLength bool)

func IsIPv4

func IsIPv4(ip net.IP) (isIPv4 bool)

IsIPv4 determines if net.IP value is IPv4

func IsIPv6

func IsIPv6(ip net.IP) (isIPv6 bool)

IsIPv6 determines if net.IP value is IPv6

func IsNetwork

func IsNetwork(IP net.IP, IPMask net.IPMask) (isNetwork bool)

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 IsNzIP

func IsNzIP(ip net.IP) bool

IsNzIP is ip set and not zero

func IsZeros

func IsZeros(p net.IP) bool

IsZeros determines if every byte of the IP address is zero

func NetworkPrefixBitCount added in v0.4.29

func NetworkPrefixBitCount(byts []byte) (bits int)

func NewTLSConfig

func NewTLSConfig(cert *x509.Certificate) (tlsConfig *tls.Config)

func NewTransport

func NewTransport(tlsConfig *tls.Config) (httpTransport *http.Transport)

Types

type Callback

type Callback func(msg Message)

Callback allows for processing of routing message,eg. populating a map

type Destination

type Destination struct {
	netip.Prefix
}

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, err error)

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) 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)

type HandlerFunc

type HandlerFunc func(http.ResponseWriter, *http.Request)

type Http

type Http struct {
	Network string // "tcp", "tcp4", "tcp6", "unix" or "unixpacket"
	http.Server
	ListenInvoked parl.AtomicBool
	ReadyWg       sync.WaitGroup
	ErrCh         chan<- error
	ErrChMutex    sync.Mutex
	ErrChClosed   parl.AtomicBool
	net.Addr      // interface
	IsListening   parl.AtomicBool
	IsShutdown    parl.AtomicBool
}

func NewHttp

func NewHttp(host, network string) (hp *Http)

NewHttp creates http server host is host:port, default ":http"

func (*Http) CloseErr

func (hp *Http) CloseErr()

func (*Http) HandleFunc

func (hp *Http) HandleFunc(pattern string, handler HandlerFunc)

func (*Http) Listen

func (hp *Http) Listen() (errCh <-chan error)

func (*Http) Listener

func (hp *Http) Listener() (listener net.Listener, err error)

func (*Http) SendErr

func (hp *Http) SendErr(err error)

func (*Http) Shutdown

func (hp *Http) Shutdown()

func (*Http) SubListen

func (hp *Http) SubListen() (errCh <-chan error)

func (*Http) WaitForUp

func (hp *Http) WaitForUp() (isUp bool, addr net.Addr)

type HttpClient

type HttpClient struct {
	http.Client
}

func NewHttpClient

func NewHttpClient(tlsConfig *tls.Config) (httpClient *HttpClient)

func (*HttpClient) Get

func (ct *HttpClient) Get(requestURL string, ctx context.Context) (resp *http.Response, err error)

type Https

type Https struct {
	Http
	Cert    parl.CertificateDer
	Private crypto.Signer
}

func NewHttps

func NewHttps(host, network string, certDER parl.CertificateDer, signer crypto.Signer) (hp *Https)

func (*Https) Listen

func (hp *Https) Listen() (errCh <-chan error)

func (*Https) TLS

func (hp *Https) TLS() (tlsListener net.Listener, err error)

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 NewIfIndex(value int) (ifIndex IfIndex, err error)

func (IfIndex) Interface

func (ifIndex IfIndex) Interface() (*net.Interface, error)

Interface gets net.Interface for ifIndex

func (IfIndex) InterfaceIndex added in v0.4.28

func (ifIndex IfIndex) InterfaceIndex() (interfaceIndex int)

Interface gets net.Interface for ifIndex

func (IfIndex) IsValid added in v0.4.28

func (ifIndex IfIndex) IsValid() (isValid bool)

IsValid determines if interface index value is set, ie. > 0

func (IfIndex) String added in v0.4.28

func (ifIndex IfIndex) String() (s string)

func (IfIndex) Zone

func (ifIndex IfIndex) Zone() (zone string, err error)

Zone gets net.IPAddr.Zone string for ifIndex

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

func NewLinkAddr(index IfIndex, name string, hw net.HardwareAddr) (linkAddr *LinkAddr, err error)

NewLinkAddr instantiates LinkAddr

func (*LinkAddr) OneString

func (linkA *LinkAddr) OneString() string

OneString picks the most meaningful value

func (*LinkAddr) String

func (linkA *LinkAddr) String() (s string)

"en8(28)00:11:22:33:44:55:66"

func (*LinkAddr) UpdateName

func (linkA *LinkAddr) UpdateName() (linkAddr *LinkAddr, err error)

UpdateName attempts to populate interface name if not already present

func (*LinkAddr) ZoneID

func (linkA *LinkAddr) ZoneID() string

ZoneID is the IPv6 ZoneID for this interface

type Message

type Message interface {
	fmt.Stringer
}

Message is a portable routing message emitted by netlink socket

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 EmptyNextHop

func EmptyNextHop() *NextHop

EmptyNextHop provides empty NextHop

func NewNextHop

func NewNextHop(gateway netip.Addr, linkAddr *LinkAddr, src netip.Addr) (nextHop *NextHop)

NewNextHop assembles a route destination

func NewNextHop2

func NewNextHop2(index IfIndex, gateway netip.Addr, src netip.Addr) (next *NextHop, err error)

NewNextHop2 assembles a route destination based on IfIndex

func NewNextHopCounts added in v0.4.28

func NewNextHopCounts(gateway netip.Addr, linkAddr *LinkAddr, src netip.Addr) (nextHop *NextHop, err error)

func (*NextHop) HasGateway

func (nh *NextHop) HasGateway() bool

HasGateway determines if next hop uses a remote gateway

func (*NextHop) HasSrc

func (nh *NextHop) HasSrc() bool

HasSrc determines if next hop has src specified

func (*NextHop) String

func (nextHop *NextHop) String() (s string)

func (*NextHop) Target

func (nh *NextHop) Target() (gateway netip.Addr, s string)

Target describes the detination for this next hop

type Route

type Route struct {
	Destination
	NextHop
}

Route describes a routing table route with destination and next hop

func NewRoute

func NewRoute(d *Destination, nextHop *NextHop) *Route

NewRoute instantiates Route

func (*Route) String added in v0.4.28

func (rt *Route) String() (s string)

type Socket

type Socket struct {
	*net.TCPListener
	// contains filtered or unexported fields
}

Socket embeds net.TCPListener

func ListenTCP4

func ListenTCP4(socketString string) (socket *Socket, err error)

ListenTCP4 listens on local network interfaces with ipv4 tcp socket: ":8080" or "1.2.3.4:80"

func (*Socket) RunHandler

func (s *Socket) RunHandler(handler func(net.Conn)) (errCh <-chan error)

RunHandler handles inbound connections

func (*Socket) Wait

func (s *Socket) Wait()

Wait waits for all connections and the handler thread to exit. ListenTCP4.Close needs to be invoked

type Tcp

type Tcp struct {
	net.Addr // interface
	http.ServeMux
	http.Server
	// contains filtered or unexported fields
}

type UDP

type UDP struct {
	Network        string
	F              UDPFunc
	MaxSize        int
	net.UDPAddr    // struct IP Port Zone
	ListenInvoked  parl.AtomicBool
	StartingListen sync.WaitGroup
	ErrCh          chan<- error
	IsListening    parl.AtomicBool
	NetUDPConn     *net.UDPConn

	Addr       net.Addr
	IsShutdown parl.AtomicBool
	// contains filtered or unexported fields
}

func NewUDP

func NewUDP(network, address string, udpFunc UDPFunc, maxSize int) (udp *UDP)

NewUDP network: "udp" "udp4" "udp6" address: "host:port"

func (*UDP) Listen

func (udp *UDP) Listen() (errCh <-chan error)

func (*UDP) Shutdown

func (udp *UDP) Shutdown()

func (*UDP) WaitForUp

func (udp *UDP) WaitForUp() (isUp bool, addr net.Addr)

type UDPFunc

type UDPFunc func(b []byte, oob []byte, flags int, addr *net.UDPAddr)

Jump to

Keyboard shortcuts

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