network

package
v0.0.30 Latest Latest
Warning

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

Go to latest
Published: Jun 6, 2026 License: MIT Imports: 14 Imported by: 6

Documentation

Overview

Package network provides utility functions to extract some data about network

Index

Constants

View Source
const ETH_P_ALL int = 0x03

Variables

View Source
var (
	BroadcastMAC            = net.HardwareAddr{0xff, 0xff, 0xff, 0xff, 0xff, 0xff}
	LoopbackMAC             = net.HardwareAddr{0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
	IPv6MulticastMAC        = net.HardwareAddr{0x33, 0x33, 0x00, 0x00, 0x00, 0x01}
	IPv6MulticastAllNodes   = netip.MustParseAddr("ff02::1")
	IPv6MulticastAllRouters = netip.MustParseAddr("ff02::2")
)

Functions

func AddrEqual added in v0.0.13

func AddrEqual(a, b string) bool

AddrEqual compares two address strings and returns true if they are equal.

It treats loopback and unspecified IPs as equivalent. Returns false in case of inequality or error.

func BroadcastFromPrefix added in v0.0.30

func BroadcastFromPrefix(prefix netip.Prefix) (netip.Addr, error)

BroadcastFromPrefix calculates broadcast address from IPv4 prefix.Addr

func DisplayInterfaces

func DisplayInterfaces(includeAny bool) error

func GetDefaultGatewayIPv4

func GetDefaultGatewayIPv4() (netip.Addr, error)

func GetDefaultGatewayIPv4FromRoute added in v0.0.15

func GetDefaultGatewayIPv4FromRoute() (netip.Addr, error)

func GetDefaultGatewayIPv6 added in v0.0.20

func GetDefaultGatewayIPv6() (netip.Addr, error)

func GetDefaultGatewayIPv6FromRoute added in v0.0.20

func GetDefaultGatewayIPv6FromRoute() (netip.Addr, error)

func GetDefaultInterface

func GetDefaultInterface() (*net.Interface, error)

func GetDefaultInterfaceFromRoute added in v0.0.15

func GetDefaultInterfaceFromRoute() (*net.Interface, error)

func GetDefaultInterfaceFromRouteIPv6 added in v0.0.21

func GetDefaultInterfaceFromRouteIPv6() (*net.Interface, error)

func GetGatewayIPv4FromInterface

func GetGatewayIPv4FromInterface(iface string) (netip.Addr, error)

func GetGatewayIPv6FromInterface added in v0.0.20

func GetGatewayIPv6FromInterface(iface string) (netip.Addr, error)

func GetHostIPv6GlobalUnicastFromRoute added in v0.0.21

func GetHostIPv6GlobalUnicastFromRoute() (netip.Addr, error)

func GetHostName added in v0.0.25

func GetHostName(ip netip.Addr) (string, error)

GetHostName performs the reverse DNS lookup for given address

func GetIPv4PrefixFromInterface

func GetIPv4PrefixFromInterface(iface *net.Interface) (netip.Prefix, error)

func GetIPv6GlobalUnicastPrefixFromInterface added in v0.0.21

func GetIPv6GlobalUnicastPrefixFromInterface(iface *net.Interface) (netip.Prefix, error)

func GetIPv6LinkLocalUnicastPrefixFromInterface added in v0.0.21

func GetIPv6LinkLocalUnicastPrefixFromInterface(iface *net.Interface) (netip.Prefix, error)

func GetIPv6Resolver added in v0.0.28

func GetIPv6Resolver(dev *net.Interface) *net.UDPAddr

GetIPv6Resolver returns first suitable IPv6 address from resolv.conf or Google IPv6 DNS as fallback

func GetPromiscuous added in v0.0.22

func GetPromiscuous(iface string) int

GetPromiscuous returns promiscuous mode bit value for given interface or -1 on error

func GetSystemNameservers added in v0.0.21

func GetSystemNameservers() ([]netip.Addr, error)

func InterfaceByName

func InterfaceByName(name string) (*net.Interface, error)

InterfaceByName returns the interface specified by name.

func Is6 added in v0.0.21

func Is6(ip netip.Addr) bool

Is6 reports whether ip is an IPv6 address, excluding IPv4-mapped IPv6 addresses.

func IsLocalAddress added in v0.0.10

func IsLocalAddress(addr string) bool

func ListenPacket added in v0.0.13

func ListenPacket(conf *ListenConfig) (*packet.Conn, error)

func ParseAddrPort added in v0.0.20

func ParseAddrPort(v, defaultHost string) (netip.AddrPort, error)

ParseAddrPort parses provided address value and tries to convert it to netip.AddrPort.

If address contains no IP (e.g. "80" or ":443"), it uses defaultHost to form address.

func PrefixIsValid added in v0.0.21

func PrefixIsValid(prefix netip.Addr, length int) bool

func PrettifyBytes added in v0.0.22

func PrettifyBytes(b int64) string

func SetPromiscuous added in v0.0.22

func SetPromiscuous(iface string, enable bool) error

SetPromiscuous enables or disables promiscuous mode for given interface

func StripZone added in v0.0.27

func StripZone(ip netip.Addr) netip.Addr

StripZone removes zone from IPv6 address

Types

type ListenConfig added in v0.0.13

type ListenConfig struct {
	Device      *net.Interface // network interface which to bind to, if not specified default interface is used
	Protocol    int            // network protocol, defaults to ETH_P_ALL
	Promiscuous *bool          // enable or disable promiscuous mode
	FilterExpr  string         // packet filter expression like in tcpdump
}

Jump to

Keyboard shortcuts

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