Documentation
¶
Overview ¶
Package iface gathers the interface facts the daemon needs at startup and during operation: index, hardware address, IPv4 address, CIDR, and (best-effort) the IPv4 default-route gateway reachable on this iface.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Info ¶
type Info struct {
Name string
Index int
HwAddr net.HardwareAddr
IP net.IP
IPNet *net.IPNet // the iface's IPv4 CIDR
Gateway net.IP // best-effort IPv4 default gateway; nil if unknown
}
Info is the snapshot of facts about a single interface.
func Lookup ¶
Lookup returns Info for the named interface. On success, IP and IPNet are always non-nil — Lookup returns an error if the interface has no IPv4 address. The Gateway field is populated by parsing `ip route show default`; if that fails, Gateway is nil and the caller is expected to handle it (Gateway is best-effort, IP/IPNet are required).
Address selection: an iface can carry several IPv4 addresses (DHCP primary plus IPv4LL autoconf, secondaries from `ip addr add`, kernel admin addresses on lo, etc). The order returned by netIf.Addrs() is arbitrary, so we score and pick:
- skip 0.0.0.0 (unspecified)
- skip 169.254.0.0/16 (link-local, autoconf fallback)
- prefer the loopback canonical 127.0.0.1 when scanning lo
- otherwise take the first non-skipped address
Rationale: on a host with a routable address AND a stale link-local secondary, the previous "first non-nil ip4" picked whichever the kernel happened to return first, which on some systems was the link-local — leading to a daemon that bound the wrong source IP for ARP sweeps. Sorting/scoring is one-pass O(N) so cost is irrelevant.