aghnet

package
v0.107.0-b.4 Latest Latest
Warning

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

Go to latest
Published: Jul 7, 2021 License: GPL-3.0 Imports: 27 Imported by: 0

Documentation

Overview

Package aghnet contains some utilities for networking.

Index

Constants

View Source
const ErrNoStaticIPInfo errors.Error = "no information about static ip"

ErrNoStaticIPInfo is returned by IfaceHasStaticIP when no information about the IP being static is available.

View Source
const MaxDomainNameLen = 253

MaxDomainNameLen is the maximum allowed length of a full domain name according to RFC 1035.

See https://stackoverflow.com/a/32294443/1892060.

Variables

This section is empty.

Functions

func CanBindPort

func CanBindPort(port int) (can bool, err error)

CanBindPort checks if we can bind to the given port.

func CanBindPrivilegedPorts added in v0.107.0

func CanBindPrivilegedPorts() (can bool, err error)

CanBindPrivilegedPorts checks if current process can bind to privileged ports.

func CheckPacketPortAvailable

func CheckPacketPortAvailable(host net.IP, port int) error

CheckPacketPortAvailable - check if UDP port is available

func CheckPortAvailable

func CheckPortAvailable(host net.IP, port int) error

CheckPortAvailable - check if TCP port is available

func CloneIP

func CloneIP(ip net.IP) (clone net.IP)

CloneIP returns a clone of an IP address.

func CloneMAC

func CloneMAC(mac net.HardwareAddr) (clone net.HardwareAddr)

CloneMAC returns a clone of a MAC address.

func CollectAllIfacesAddrs

func CollectAllIfacesAddrs() (addrs []string, err error)

CollectAllIfacesAddrs returns the slice of all network interfaces IP addresses without port number.

func ErrorIsAddrInUse

func ErrorIsAddrInUse(err error) bool

ErrorIsAddrInUse - check if error is "address already in use"

func GatewayIP

func GatewayIP(ifaceName string) net.IP

GatewayIP returns IP address of interface's gateway.

func GenerateHostname

func GenerateHostname(ip net.IP) (hostname string)

GenerateHostname generates the hostname from ip. In case of using IPv4 the result should be like:

192-168-10-1

In case of using IPv6, the result is like:

ff80-f076-0000-0000-0000-0000-0000-0010

func GetInterfaceByIP

func GetInterfaceByIP(ip net.IP) string

GetInterfaceByIP returns the name of interface containing provided ip.

func GetSubnet

func GetSubnet(ifaceName string) *net.IPNet

GetSubnet returns pointer to net.IPNet for the specified interface or nil if the search fails.

func IPFromAddr

func IPFromAddr(addr net.Addr) (ip net.IP)

IPFromAddr returns an IP address from addr. If addr is neither a *net.TCPAddr nor a *net.UDPAddr, it returns nil.

func IfaceHasStaticIP

func IfaceHasStaticIP(ifaceName string) (has bool, err error)

IfaceHasStaticIP checks if interface is configured to have static IP address. If it can't give a definitive answer, it returns false and an error for which errors.Is(err, ErrNoStaticIPInfo) is true.

func IfaceSetStaticIP

func IfaceSetStaticIP(ifaceName string) (err error)

IfaceSetStaticIP sets static IP address for network interface.

func IsValidHostOuterRune

func IsValidHostOuterRune(r rune) (ok bool)

IsValidHostOuterRune returns true if r is a valid initial or final rune for a hostname label.

func JoinHostPort

func JoinHostPort(host string, port int) (hostport string)

JoinHostPort is a convinient wrapper for net.JoinHostPort with port of type int.

func ReverseAddr

func ReverseAddr(ip net.IP) (arpa string)

ReverseAddr returns the ARPA hostname of the ip suitable for reverse DNS (PTR) record lookups. This is the modified version of ReverseAddr from github.com/miekg/dns package with no error among returned values.

func SplitHost

func SplitHost(hostport string) (host string, err error)

SplitHost is a wrapper for net.SplitHostPort for the cases when the hostport does not necessarily contain a port.

func UnreverseAddr

func UnreverseAddr(arpa string) (unreversed net.IP)

UnreverseAddr tries to convert reversed ARPA to a normal IP address.

func ValidateDomainName

func ValidateDomainName(name string) (err error)

ValidateDomainName validates the domain name in accordance to RFC 952, RFC 1035, and with RFC-1123's inclusion of digits at the start of the host. It doesn't validate against two or more hyphens to allow punycode and internationalized domains.

TODO(a.garipov): After making sure that this works correctly, port this into module golibs.

func ValidateDomainNameLabel

func ValidateDomainNameLabel(label string) (err error)

ValidateDomainNameLabel returns an error if label is not a valid label of a domain name.

func ValidateHardwareAddress

func ValidateHardwareAddress(hwa net.HardwareAddr) (err error)

ValidateHardwareAddress returns an error if hwa is not a valid EUI-48, EUI-64, or 20-octet InfiniBand link-layer address.

Types

type EtcHostsContainer

type EtcHostsContainer struct {
	// contains filtered or unexported fields
}

EtcHostsContainer - automatic DNS records

TODO(e.burkov): Move the logic under interface. Refactor. Probably remove the resolving logic.

func (*EtcHostsContainer) Close

func (ehc *EtcHostsContainer) Close()

Close - close module

func (*EtcHostsContainer) Init

func (ehc *EtcHostsContainer) Init(hostsFn string)

Init - initialize hostsFn: Override default name for the hosts-file (optional)

func (*EtcHostsContainer) List

func (ehc *EtcHostsContainer) List() (ipToHosts *IPMap)

List returns an IP-to-hostnames table. The type of the values in the map is []string. It is safe for concurrent use.

func (*EtcHostsContainer) Process

func (ehc *EtcHostsContainer) Process(host string, qtype uint16) []net.IP

Process returns the list of IP addresses for the hostname or nil if nothing found.

func (*EtcHostsContainer) ProcessReverse

func (ehc *EtcHostsContainer) ProcessReverse(addr string, qtype uint16) (hosts []string)

ProcessReverse processes a PTR request. It returns nil if nothing is found.

func (*EtcHostsContainer) SetOnChanged

func (ehc *EtcHostsContainer) SetOnChanged(onChanged onChangedT)

SetOnChanged - set callback function that will be called when the data is changed

func (*EtcHostsContainer) Start

func (ehc *EtcHostsContainer) Start()

Start - start module

type HostGenFunc

type HostGenFunc func() (host string)

HostGenFunc is the signature for functions generating fake hostnames. The implementation must be safe for concurrent use.

type IPMap

type IPMap struct {
	// contains filtered or unexported fields
}

IPMap is a map of IP addresses.

func NewIPMap

func NewIPMap(hint int) (m *IPMap)

NewIPMap returns a new empty IP map using hint as a size hint for the underlying map.

func (*IPMap) Del

func (m *IPMap) Del(ip net.IP)

Del deletes ip from the map. Calling Del on a nil *IPMap has no effect, just like delete on an empty map doesn't.

func (*IPMap) Get

func (m *IPMap) Get(ip net.IP) (v interface{}, ok bool)

Get returns the value from the map. Calling Get on a nil *IPMap returns nil and false, just like indexing on an empty map does.

func (*IPMap) Len

func (m *IPMap) Len() (n int)

Len returns the length of the map. A nil *IPMap has a length of zero, just like an empty map.

func (*IPMap) Range

func (m *IPMap) Range(f func(ip net.IP, v interface{}) (cont bool))

Range calls f for each key and value present in the map in an undefined order. If cont is false, range stops the iteration. Calling Range on a nil *IPMap has no effect, just like ranging over a nil map.

func (*IPMap) Set

func (m *IPMap) Set(ip net.IP, v interface{})

Set sets the value. Set panics if the m is a nil *IPMap, just like a nil map does.

func (*IPMap) ShallowClone

func (m *IPMap) ShallowClone() (sclone *IPMap)

ShallowClone returns a shallow clone of the map.

func (*IPMap) String

func (m *IPMap) String() (s string)

String implements the fmt.Stringer interface for *IPMap.

type IpsetManager added in v0.107.0

type IpsetManager interface {
	Add(host string, ip4s, ip6s []net.IP) (n int, err error)
	Close() (err error)
}

IpsetManager is the ipset manager interface.

TODO(a.garipov): Perhaps generalize this into some kind of a NetFilter type, since ipset is exclusive to Linux?

func NewIpsetManager added in v0.107.0

func NewIpsetManager(ipsetConf []string) (mgr IpsetManager, err error)

NewIpsetManager returns a new ipset. IPv4 addresses are added to an ipset with an ipv4 family; IPv6 addresses, to an ipv6 ipset. ipset must exist.

The syntax of the ipsetConf is:

DOMAIN[,DOMAIN].../IPSET_NAME[,IPSET_NAME]...

The error is of type *aghos.UnsupportedError if the OS is not supported.

type NetInterface

type NetInterface struct {
	MTU          int              `json:"mtu"`
	Name         string           `json:"name"`
	HardwareAddr net.HardwareAddr `json:"hardware_address"`
	Flags        net.Flags        `json:"flags"`
	// Array with the network interface addresses.
	Addresses []net.IP `json:"ip_addresses,omitempty"`
	// Array with IP networks for this network interface.
	Subnets []*net.IPNet `json:"-"`
}

NetInterface represents an entry of network interfaces map.

func GetValidNetInterfacesForWeb

func GetValidNetInterfacesForWeb() ([]*NetInterface, error)

GetValidNetInterfacesForWeb returns interfaces that are eligible for DNS and WEB only we do not return link-local addresses here

func (NetInterface) MarshalJSON

func (iface NetInterface) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface for NetInterface.

type SubnetDetector

type SubnetDetector struct {
	// contains filtered or unexported fields
}

SubnetDetector describes IP address properties.

func NewSubnetDetector

func NewSubnetDetector() (snd *SubnetDetector, err error)

NewSubnetDetector returns a new IP detector.

func (*SubnetDetector) IsLocallyServedNetwork

func (snd *SubnetDetector) IsLocallyServedNetwork(ip net.IP) (is bool)

IsLocallyServedNetwork returns true if IP address is contained by any of locally-served IP address registries. It's safe for concurrent use.

func (*SubnetDetector) IsSpecialNetwork

func (snd *SubnetDetector) IsSpecialNetwork(ip net.IP) (is bool)

IsSpecialNetwork returns true if IP address is contained by any of special-purpose IP address registries. It's safe for concurrent use.

type SystemResolvers

type SystemResolvers interface {
	// Get returns the slice of local resolvers' addresses.  It should be
	// safe for concurrent use.
	Get() (rs []string)
	// contains filtered or unexported methods
}

SystemResolvers helps to work with local resolvers' addresses provided by OS.

func NewSystemResolvers

func NewSystemResolvers(
	refreshIvl time.Duration,
	hostGenFunc HostGenFunc,
) (sr SystemResolvers, err error)

NewSystemResolvers returns a SystemResolvers with the cache refresh rate defined by refreshIvl. It disables auto-resfreshing if refreshIvl is 0. If nil is passed for hostGenFunc, the default generator will be used.

Jump to

Keyboard shortcuts

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