aghnet

package
v0.107.0-b.14 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2021 License: GPL-3.0 Imports: 35 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.

Variables

This section is empty.

Functions

func BroadcastFromIPNet added in v0.107.0

func BroadcastFromIPNet(n *net.IPNet) (dc net.IP)

BroadcastFromIPNet calculates the broadcast IP address for n.

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 CheckOtherDHCP added in v0.107.0

func CheckOtherDHCP(ifaceName string) (ok4, ok6 bool, err4, err6 error)

CheckOtherDHCP tries to discover another DHCP server in the network.

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 CollectAllIfacesAddrs

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

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

func DefaultHostsPaths added in v0.107.0

func DefaultHostsPaths() (paths []string)

DefaultHostsPaths returns the slice of paths default for the operating system to files and directories which are containing the hosts database. The result is intended to use within fs.FS so the initial slash is omitted.

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 IfaceDNSIPAddrs added in v0.107.0

func IfaceDNSIPAddrs(
	iface NetIface,
	ipv IPVersion,
	maxAttempts int,
	backoff time.Duration,
) (addrs []net.IP, err error)

IfaceDNSIPAddrs returns IP addresses of the interface suitable to send to clients as DNS addresses. If err is nil, addrs contains either no addresses or at least two.

It makes up to maxAttempts attempts to get the addresses if there are none, each time using the provided backoff. Sometimes an interface needs a few seconds to really ititialize.

See https://github.com/AdguardTeam/AdGuardHome/issues/2304.

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 IfaceIPAddrs added in v0.107.0

func IfaceIPAddrs(iface NetIface, ipv IPVersion) (ips []net.IP, err error)

IfaceIPAddrs returns the interface's IP addresses.

func IfaceSetStaticIP

func IfaceSetStaticIP(ifaceName string) (err error)

IfaceSetStaticIP sets static IP address for network interface.

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.

Types

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 HostsContainer added in v0.107.0

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

HostsContainer stores the relevant hosts database provided by the OS and processes both A/AAAA and PTR DNS requests for those.

func NewHostsContainer added in v0.107.0

func NewHostsContainer(
	fsys fs.FS,
	w aghos.FSWatcher,
	paths ...string,
) (hc *HostsContainer, err error)

NewHostsContainer creates a container of hosts, that watches the paths with w. paths shouldn't be empty and each of them should locate either a file or a directory in fsys. fsys and w must be non-nil.

func (*HostsContainer) Close added in v0.107.0

func (hc *HostsContainer) Close() (err error)

Close implements the io.Closer interface for *HostsContainer.

func (*HostsContainer) MatchRequest added in v0.107.0

func (hc *HostsContainer) MatchRequest(
	req urlfilter.DNSRequest,
) (res urlfilter.DNSResult, ok bool)

MatchRequest is the request processing method to resolve hostnames and addresses from the operating system's hosts files. Any request not of A/AAAA or PTR type will return with an empty result. It's safe for concurrent use.

func (*HostsContainer) Upd added in v0.107.0

func (hc *HostsContainer) Upd() (updates <-chan *netutil.IPMap)

Upd returns the channel into which the updates are sent.

type IPVersion added in v0.107.0

type IPVersion = int

IPVersion is a documentational alias for int. Use it when the integer means IP version.

const (
	IPVersion4 IPVersion = 4
	IPVersion6 IPVersion = 6
)

IP version constants.

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 NetIface added in v0.107.0

type NetIface interface {
	Addrs() ([]net.Addr, error)
}

NetIface is the interface for network interface methods.

type NetInterface

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

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