Documentation
¶
Overview ¶
Package aghnet contains networking utilities.
Index ¶
- Constants
- func BroadcastFromPref(p netip.Prefix) (bc netip.Addr)
- func CanBindPrivilegedPorts(ctx context.Context, l *slog.Logger) (can bool, err error)
- func CheckOtherDHCP(ctx context.Context, l *slog.Logger, ifaceName string) (ok4, ok6 bool, err4, err6 error)
- func CheckPort(network string, ipp netip.AddrPort) (err error)
- func CollectAllIfacesAddrs() (addrs []netip.Addr, err error)
- func GatewayIP(ctx context.Context, l *slog.Logger, cmdCons executil.CommandConstructor, ...) (ip netip.Addr)
- func GenerateHostname(ip netip.Addr) (hostname string)
- func GetSubnet(ctx context.Context, l *slog.Logger, ifaceName string) (p netip.Prefix)
- func IfaceDNSIPAddrs(ctx context.Context, l *slog.Logger, iface NetIface, ipv IPVersion, ...) (addrs []net.IP, err error)
- func IfaceHasStaticIP(ctx context.Context, cmdCons executil.CommandConstructor, ifaceName string) (has bool, err error)
- func IfaceIPAddrs(iface NetIface, ipv IPVersion) (ips []net.IP, err error)
- func IfaceSetStaticIP(ctx context.Context, l *slog.Logger, cmdCons executil.CommandConstructor, ...) (err error)
- func InterfaceByIP(ip netip.Addr) (ifaceName string)
- func IsAddrInUse(err error) (ok bool)
- func IsCommentOrEmpty(s string) (ok bool)
- func NormalizeDomain(host string) (norm string)
- func ParseAddrPort(s string, defaultPort uint16) (ipp netip.AddrPort, err error)
- func ParseBootstraps(addrs []string, opts *upstream.Options) (boots []*upstream.UpstreamResolver, err error)
- func ParseSubnet(s string) (p netip.Prefix, err error)
- func UpstreamHTTPVersions(http3 bool) (v []upstream.HTTPVersion)
- type DialContextFunc
- type HostsContainer
- type IPMut
- type IPMutFunc
- type IPVersion
- type IgnoreEngine
- type NetIface
- type NetInterface
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 whether the IP is 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. l must not be nil.
func CheckOtherDHCP ¶ added in v0.107.0
func CheckOtherDHCP( ctx context.Context, l *slog.Logger, ifaceName string, ) (ok4, ok6 bool, err4, err6 error)
CheckOtherDHCP tries to discover another DHCP server in the network. l must not be nil.
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 GatewayIP ¶
func GatewayIP( ctx context.Context, l *slog.Logger, cmdCons executil.CommandConstructor, ifaceName string, ) (ip netip.Addr)
GatewayIP returns the gateway IP address for the interface. l and cmdCons must not be nil.
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. l must not be nil.
TODO(e.burkov): See TODO on GetValidNetInterfacesForWeb.
func IfaceDNSIPAddrs ¶ added in v0.107.0
func IfaceDNSIPAddrs( ctx context.Context, l *slog.Logger, 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. l must not be nil.
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 ¶
func IfaceHasStaticIP( ctx context.Context, cmdCons executil.CommandConstructor, ifaceName string, ) (has bool, err error)
IfaceHasStaticIP reports whether the interface has a static IP. If the status is indeterminate, it returns false with an error matching ErrNoStaticIPInfo. cmdCons must not be nil.
func IfaceIPAddrs ¶ added in v0.107.0
IfaceIPAddrs returns the interface's IP addresses. iface must not be nil.
func IfaceSetStaticIP ¶
func IfaceSetStaticIP( ctx context.Context, l *slog.Logger, cmdCons executil.CommandConstructor, ifaceName string, ) (err error)
IfaceSetStaticIP sets a static IP address for network interface. l and cmdCons must not be nil.
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.
func IsCommentOrEmpty ¶ added in v0.107.58
IsCommentOrEmpty returns true if s starts with a "#" character or is empty. This function is useful for filtering out non-upstream lines from upstream configs.
func NormalizeDomain ¶ added in v0.107.34
NormalizeDomain returns a lowercased version of host without the final dot, unless host is ".", in which case it returns it unchanged. That is a special case that to allow matching queries like:
dig IN NS '.'
func ParseAddrPort ¶ added in v0.107.39
ParseAddrPort parses an netip.AddrPort from s, which should be either a valid IP, optionally with port, or a valid URL with plain IP address. The defaultPort is used if s doesn't contain port number.
func ParseBootstraps ¶ added in v0.107.42
func ParseBootstraps( addrs []string, opts *upstream.Options, ) (boots []*upstream.UpstreamResolver, err error)
ParseBootstraps returns the slice of upstream resolvers parsed from addrs. It additionally returns the closers for each resolver, that should be closed after use.
func ParseSubnet ¶ added in v0.107.42
ParseSubnet parses s either as a CIDR prefix itself, or as an IP address, returning the corresponding single-IP CIDR prefix.
TODO(e.burkov): Taken from dnsproxy, move to golibs.
func UpstreamHTTPVersions ¶ added in v0.107.58
func UpstreamHTTPVersions(http3 bool) (v []upstream.HTTPVersion)
UpstreamHTTPVersions returns the HTTP versions for upstream configuration depending on configuration.
Types ¶
type DialContextFunc ¶ added in v0.107.35
DialContextFunc is the semantic alias for dialing functions, such as [http.Transport.DialContext].
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( ctx context.Context, l *slog.Logger, 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 path should refer either a file or a directory in fsys. l, fsys, and w must be non-nil.
func (*HostsContainer) ByAddr ¶ added in v0.107.44
func (hc *HostsContainer) ByAddr(addr netip.Addr) (names []string)
ByAddr implements the hostsfile.Storage interface for *HostsContainer.
func (*HostsContainer) ByName ¶ added in v0.107.44
func (hc *HostsContainer) ByName(name string) (addrs []netip.Addr)
ByName implements the hostsfile.Storage interface for *HostsContainer.
func (*HostsContainer) Close ¶ added in v0.107.0
func (hc *HostsContainer) Close() (err error)
Close implements the io.Closer interface for *HostsContainer. It closes both itself and its aghos.FSWatcher. Close must only be called once.
TODO(s.chzhen): Implement [service.Interface].
func (*HostsContainer) Upd ¶ added in v0.107.0
func (hc *HostsContainer) Upd() (updates <-chan *hostsfile.DefaultStorage)
Upd returns the channel into which the updates are sent. The updates themselves must not be modified.
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 IgnoreEngine ¶ added in v0.107.37
type IgnoreEngine struct {
// contains filtered or unexported fields
}
IgnoreEngine contains the list of rules for ignoring hostnames and matches them.
TODO(s.chzhen): Move all urlfilter stuff to aghfilter.
func NewIgnoreEngine ¶ added in v0.107.37
func NewIgnoreEngine(ignored []string) (e *IgnoreEngine, err error)
NewIgnoreEngine creates a new instance of the IgnoreEngine and stores the list of rules for ignoring hostnames.
func (*IgnoreEngine) Has ¶ added in v0.107.37
func (e *IgnoreEngine) Has(host string) (ignore bool)
Has returns true if IgnoreEngine matches the host.
func (*IgnoreEngine) Values ¶ added in v0.107.37
func (e *IgnoreEngine) Values() (ignored []string)
Values returns a copy of list of rules for ignoring hostnames.
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)
NetInterfaceFrom converts a net.Interface to NetInterface, populating name, MAC address, flags, MTU, IP addresses, and subnets. iface must not be nil.
func (NetInterface) MarshalJSON ¶
func (iface NetInterface) MarshalJSON() ([]byte, error)
MarshalJSON implements the json.Marshaler interface for NetInterface.