Documentation ¶
Overview ¶
Package aghnet contains some utilities for networking.
Index ¶
- Constants
- func BroadcastFromPref(p netip.Prefix) (bc netip.Addr)
- func CanBindPrivilegedPorts() (can bool, err error)
- func CheckOtherDHCP(ifaceName string) (ok4, ok6 bool, err4, err6 error)
- func CheckPort(network string, ipp netip.AddrPort) (err error)
- func CollectAllIfacesAddrs() (addrs []string, err error)
- func DefaultHostsPaths() (paths []string)
- func GatewayIP(ifaceName string) (ip netip.Addr)
- func GenerateHostname(ip net.IP) (hostname string)
- func GetSubnet(ifaceName string) (p netip.Prefix)
- func IfaceDNSIPAddrs(iface NetIface, ipv IPVersion, maxAttempts int, backoff time.Duration) (addrs []net.IP, err error)
- func IfaceHasStaticIP(ifaceName string) (has bool, err error)
- func IfaceIPAddrs(iface NetIface, ipv IPVersion) (ips []net.IP, err error)
- func IfaceSetStaticIP(ifaceName string) (err error)
- func InterfaceByIP(ip netip.Addr) (ifaceName string)
- func IsAddrInUse(err error) (ok bool)
- type ARPDB
- type EmptyARPDB
- type HostGenFunc
- type HostsContainer
- type HostsRecord
- type HostsRecords
- type IPMut
- type IPMutFunc
- type IPVersion
- type IpsetManager
- type Neighbor
- type NetIface
- type NetInterface
- type SystemResolvers
Constants ¶
const ErrNoHostsPaths errors.Error = "no valid paths to hosts files provided"
ErrNoHostsPaths is returned when there are no valid paths to watch passed to the HostsContainer.
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 BroadcastFromPref ¶ added in v0.107.17
BroadcastFromPref calculates the broadcast IP address for p.
func CanBindPrivilegedPorts ¶ added in v0.107.0
CanBindPrivilegedPorts checks if current process can bind to privileged ports.
func CheckOtherDHCP ¶ added in v0.107.0
CheckOtherDHCP tries to discover another DHCP server in the network.
func CheckPort ¶ added in v0.107.0
CheckPort checks if the port is available for binding. network is expected to be one of "udp" and "tcp".
func CollectAllIfacesAddrs ¶
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 be used within fs.FS so the initial slash is omitted.
func GatewayIP ¶
GatewayIP returns IP address of interface's gateway.
TODO(e.burkov): Investigate if the gateway address may be fetched in another way since not every machine has the software installed.
func GenerateHostname ¶
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
ip must be either an IPv4 or an IPv6.
func GetSubnet ¶
GetSubnet returns the subnet corresponding to the interface of zero prefix if the search fails.
TODO(e.burkov): See TODO on GetValidNetInterfacesForWeb.
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 initialize.
func IfaceHasStaticIP ¶
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
IfaceIPAddrs returns the interface's IP addresses.
func IfaceSetStaticIP ¶
IfaceSetStaticIP sets static IP address for network interface.
func InterfaceByIP ¶ added in v0.107.10
InterfaceByIP returns the name of the interface bound to ip.
TODO(a.garipov, e.burkov): This function is technically incorrect, since one IP address can be shared by multiple interfaces in some configurations.
TODO(e.burkov): See TODO on GetValidNetInterfacesForWeb.
func IsAddrInUse ¶ added in v0.107.0
IsAddrInUse checks if err is about unsuccessful address binding.
Types ¶
type ARPDB ¶ added in v0.107.7
type ARPDB interface { // Refresh updates the stored data. It must be safe for concurrent use. Refresh() (err error) // Neighbors returnes the last set of data reported by ARP. Both the method // and it's result must be safe for concurrent use. Neighbors() (ns []Neighbor) }
ARPDB stores and refreshes the network neighborhood reported by ARP (Address Resolution Protocol).
type EmptyARPDB ¶ added in v0.107.7
type EmptyARPDB struct{}
EmptyARPDB is the ARPDB implementation that does nothing.
func (EmptyARPDB) Neighbors ¶ added in v0.107.7
func (EmptyARPDB) Neighbors() (ns []Neighbor)
Neighbors implements the ARPDB interface for EmptyARPContainer. It always returns nil.
func (EmptyARPDB) Refresh ¶ added in v0.107.7
func (EmptyARPDB) Refresh() (err error)
Refresh implements the ARPDB interface for EmptyARPContainer. It does nothing and always returns nil error.
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( listID int, fsys fs.FS, w aghos.FSWatcher, paths ...string, ) (hc *HostsContainer, err error)
NewHostsContainer creates a container of hosts, that watches the paths with w. listID is used as an identifier of the underlying rules list. paths shouldn't be empty and each of paths 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. Close must only be called once. The returned err is always nil.
func (*HostsContainer) MatchRequest ¶ added in v0.107.0
func (rm *HostsContainer) MatchRequest( req *urlfilter.DNSRequest, ) (res *urlfilter.DNSResult, ok bool)
MatchRequest processes the request rewriting hostnames and addresses read from the operating system's hosts files. res is nil for any request having not an A/AAAA or PTR type, see man 5 hosts.
It's safe for concurrent use.
func (*HostsContainer) Translate ¶ added in v0.107.0
Translate returns the source hosts-syntax rule for the generated dnsrewrite rule or an empty string if the last doesn't exist. The returned rules are in a processed format like:
ip host1 host2 ...
func (*HostsContainer) Upd ¶ added in v0.107.0
func (hc *HostsContainer) Upd() (updates <-chan HostsRecords)
Upd returns the channel into which the updates are sent.
type HostsRecord ¶ added in v0.107.8
type HostsRecord struct { Aliases *stringutil.Set Canonical string }
HostsRecord represents a single hosts file record.
type HostsRecords ¶ added in v0.107.17
type HostsRecords map[netip.Addr]*HostsRecord
HostsRecords is a mapping of an IP address to its hosts data.
type IPMut ¶ added in v0.107.0
type IPMut struct {
// contains filtered or unexported fields
}
IPMut is a type-safe wrapper of atomic.Value to store the IPMutFunc.
func NewIPMut ¶ added in v0.107.0
NewIPMut returns the new properly initialized *IPMut. The m is guaranteed to always store non-nil IPMutFunc which is safe to call.
type IPMutFunc ¶ added in v0.107.0
IPMutFunc is the signature of a function which modifies the IP address instance. It should be safe for concurrent use.
type IPVersion ¶ added in v0.107.0
type IPVersion = int
IPVersion is a alias for int for documentation purposes. Use it when the integer means IP version.
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]...
If ipsetConf is empty, msg and err are nil. The error is of type *aghos.UnsupportedError if the OS is not supported.
type Neighbor ¶ added in v0.107.7
type Neighbor struct { // Name is the hostname of the neighbor. Empty name is valid since not each // implementation of ARP is able to retrieve that. Name string // IP contains either IPv4 or IPv6. IP netip.Addr // MAC contains the hardware address. MAC net.HardwareAddr }
Neighbor is the pair of IP address and MAC address reported by ARP.
type NetInterface ¶
type NetInterface struct { // Addresses are the network interface addresses. Addresses []netip.Addr `json:"ip_addresses,omitempty"` // Subnets are the IP networks for this network interface. Subnets []netip.Prefix `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() (nifaces []*NetInterface, err error)
GetValidNetInterfacesForWeb returns interfaces that are eligible for DNS and WEB only we do not return link-local addresses here.
TODO(e.burkov): Can't properly test the function since it's nontrivial to substitute net.Interface.Addrs and the net.InterfaceAddrs can't be used.
func NetInterfaceFrom ¶ added in v0.107.17
func NetInterfaceFrom(iface *net.Interface) (niface *NetInterface, err error)
func (NetInterface) MarshalJSON ¶
func (iface NetInterface) MarshalJSON() ([]byte, error)
MarshalJSON implements the json.Marshaler interface for NetInterface.
type SystemResolvers ¶
type SystemResolvers interface { // Get returns the slice of local resolvers' addresses. It must 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( hostGenFunc HostGenFunc, ) (sr SystemResolvers, err error)
NewSystemResolvers returns a SystemResolvers with the cache refresh rate defined by refreshIvl. It disables auto-refreshing if refreshIvl is 0. If nil is passed for hostGenFunc, the default generator will be used.