2023-08-24 13:42:17 +03:00
|
|
|
// Package arpdb implements the Network Neighborhood Database.
|
|
|
|
package arpdb
|
2022-03-15 20:57:46 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2022-03-30 15:11:57 +03:00
|
|
|
"bytes"
|
2022-03-15 20:57:46 +03:00
|
|
|
"fmt"
|
2024-08-27 20:42:10 +03:00
|
|
|
"log/slog"
|
2022-03-15 20:57:46 +03:00
|
|
|
"net"
|
2022-10-25 15:08:12 +03:00
|
|
|
"net/netip"
|
2024-02-08 20:39:18 +03:00
|
|
|
"slices"
|
2022-03-15 20:57:46 +03:00
|
|
|
"sync"
|
|
|
|
|
2023-08-24 13:42:17 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghos"
|
2022-03-15 20:57:46 +03:00
|
|
|
"github.com/AdguardTeam/golibs/errors"
|
2024-08-27 20:42:10 +03:00
|
|
|
"github.com/AdguardTeam/golibs/logutil/slogutil"
|
2023-08-24 13:42:17 +03:00
|
|
|
"github.com/AdguardTeam/golibs/netutil"
|
2024-02-13 13:19:22 +03:00
|
|
|
"github.com/AdguardTeam/golibs/osutil"
|
2022-03-15 20:57:46 +03:00
|
|
|
)
|
|
|
|
|
2023-08-24 13:42:17 +03:00
|
|
|
// Variables and functions to substitute in tests.
|
|
|
|
var (
|
|
|
|
// aghosRunCommand is the function to run shell commands.
|
|
|
|
aghosRunCommand = aghos.RunCommand
|
2022-03-15 20:57:46 +03:00
|
|
|
|
2023-08-24 13:42:17 +03:00
|
|
|
// rootDirFS is the filesystem pointing to the root directory.
|
2024-02-13 13:19:22 +03:00
|
|
|
rootDirFS = osutil.RootDirFS()
|
2023-08-24 13:42:17 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// Interface stores and refreshes the network neighborhood reported by ARP
|
|
|
|
// (Address Resolution Protocol).
|
|
|
|
type Interface interface {
|
2022-03-15 20:57:46 +03:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2023-08-24 13:42:17 +03:00
|
|
|
// New returns the [Interface] properly initialized for the OS.
|
2024-08-27 20:42:10 +03:00
|
|
|
func New(logger *slog.Logger) (arp Interface) {
|
|
|
|
return newARPDB(logger)
|
2022-03-15 20:57:46 +03:00
|
|
|
}
|
|
|
|
|
2023-08-24 13:42:17 +03:00
|
|
|
// Empty is the [Interface] implementation that does nothing.
|
|
|
|
type Empty struct{}
|
2022-03-15 20:57:46 +03:00
|
|
|
|
|
|
|
// type check
|
2023-08-24 13:42:17 +03:00
|
|
|
var _ Interface = Empty{}
|
2022-03-15 20:57:46 +03:00
|
|
|
|
2023-08-24 13:42:17 +03:00
|
|
|
// Refresh implements the [Interface] interface for EmptyARPContainer. It does
|
2022-03-15 20:57:46 +03:00
|
|
|
// nothing and always returns nil error.
|
2023-08-24 13:42:17 +03:00
|
|
|
func (Empty) Refresh() (err error) { return nil }
|
2022-03-15 20:57:46 +03:00
|
|
|
|
2023-08-24 13:42:17 +03:00
|
|
|
// Neighbors implements the [Interface] interface for EmptyARPContainer. It
|
|
|
|
// always returns nil.
|
|
|
|
func (Empty) Neighbors() (ns []Neighbor) { return nil }
|
2022-03-15 20:57:46 +03:00
|
|
|
|
|
|
|
// Neighbor is the pair of IP address and MAC address reported by ARP.
|
|
|
|
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.
|
2022-10-25 15:08:12 +03:00
|
|
|
IP netip.Addr
|
2022-03-15 20:57:46 +03:00
|
|
|
|
|
|
|
// MAC contains the hardware address.
|
|
|
|
MAC net.HardwareAddr
|
|
|
|
}
|
|
|
|
|
2024-08-27 20:42:10 +03:00
|
|
|
// newNeighbor returns the new initialized [Neighbor] by parsing string
|
|
|
|
// representations of IP and MAC addresses.
|
|
|
|
func newNeighbor(host, ipStr, macStr string) (n *Neighbor, err error) {
|
|
|
|
defer func() { err = errors.Annotate(err, "getting arp neighbor: %w") }()
|
|
|
|
|
|
|
|
ip, err := netip.ParseAddr(ipStr)
|
|
|
|
if err != nil {
|
|
|
|
// Don't wrap the error, as it will get annotated.
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
mac, err := net.ParseMAC(macStr)
|
|
|
|
if err != nil {
|
|
|
|
// Don't wrap the error, as it will get annotated.
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Neighbor{
|
|
|
|
Name: host,
|
|
|
|
IP: ip,
|
|
|
|
MAC: mac,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2022-03-15 20:57:46 +03:00
|
|
|
// Clone returns the deep copy of n.
|
|
|
|
func (n Neighbor) Clone() (clone Neighbor) {
|
|
|
|
return Neighbor{
|
|
|
|
Name: n.Name,
|
2022-10-25 15:08:12 +03:00
|
|
|
IP: n.IP,
|
2022-10-21 20:42:00 +03:00
|
|
|
MAC: slices.Clone(n.MAC),
|
2022-03-15 20:57:46 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-01 15:24:44 +03:00
|
|
|
// validatedHostname returns h if it's a valid hostname, or an empty string
|
|
|
|
// otherwise, logging the validation error.
|
2024-08-27 20:42:10 +03:00
|
|
|
func validatedHostname(logger *slog.Logger, h string) (host string) {
|
2023-08-24 13:42:17 +03:00
|
|
|
err := netutil.ValidateHostname(h)
|
|
|
|
if err != nil {
|
2024-08-27 20:42:10 +03:00
|
|
|
logger.Debug("parsing host of arp output", slogutil.KeyError, err)
|
2023-08-24 13:42:17 +03:00
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
2022-03-15 20:57:46 +03:00
|
|
|
// neighs is the helper type that stores neighbors to avoid copying its methods
|
2023-08-24 13:42:17 +03:00
|
|
|
// among all the [Interface] implementations.
|
2022-03-15 20:57:46 +03:00
|
|
|
type neighs struct {
|
|
|
|
mu *sync.RWMutex
|
|
|
|
ns []Neighbor
|
|
|
|
}
|
|
|
|
|
|
|
|
// len returns the length of the neighbors slice. It's safe for concurrent use.
|
|
|
|
func (ns *neighs) len() (l int) {
|
|
|
|
ns.mu.RLock()
|
|
|
|
defer ns.mu.RUnlock()
|
|
|
|
|
|
|
|
return len(ns.ns)
|
|
|
|
}
|
|
|
|
|
|
|
|
// clone returns a deep copy of the underlying neighbors slice. It's safe for
|
|
|
|
// concurrent use.
|
|
|
|
func (ns *neighs) clone() (cloned []Neighbor) {
|
|
|
|
ns.mu.RLock()
|
|
|
|
defer ns.mu.RUnlock()
|
|
|
|
|
|
|
|
cloned = make([]Neighbor, len(ns.ns))
|
|
|
|
for i, n := range ns.ns {
|
|
|
|
cloned[i] = n.Clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
return cloned
|
|
|
|
}
|
|
|
|
|
|
|
|
// reset replaces the underlying slice with the new one. It's safe for
|
|
|
|
// concurrent use.
|
|
|
|
func (ns *neighs) reset(with []Neighbor) {
|
|
|
|
ns.mu.Lock()
|
|
|
|
defer ns.mu.Unlock()
|
|
|
|
|
|
|
|
ns.ns = with
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseNeighsFunc parses the text from sc as if it'd be an output of some
|
|
|
|
// ARP-related command. lenHint is a hint for the size of the allocated slice
|
|
|
|
// of Neighbors.
|
2024-08-27 20:42:10 +03:00
|
|
|
//
|
|
|
|
// TODO(s.chzhen): Return []*Neighbor instead.
|
|
|
|
type parseNeighsFunc func(logger *slog.Logger, sc *bufio.Scanner, lenHint int) (ns []Neighbor)
|
2022-03-15 20:57:46 +03:00
|
|
|
|
2023-08-24 13:42:17 +03:00
|
|
|
// cmdARPDB is the implementation of the [Interface] that uses command line to
|
2022-03-15 20:57:46 +03:00
|
|
|
// retrieve data.
|
|
|
|
type cmdARPDB struct {
|
2024-08-27 20:42:10 +03:00
|
|
|
logger *slog.Logger
|
|
|
|
parse parseNeighsFunc
|
|
|
|
ns *neighs
|
|
|
|
cmd string
|
|
|
|
args []string
|
2022-03-15 20:57:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// type check
|
2023-08-24 13:42:17 +03:00
|
|
|
var _ Interface = (*cmdARPDB)(nil)
|
2022-03-15 20:57:46 +03:00
|
|
|
|
2023-08-24 13:42:17 +03:00
|
|
|
// Refresh implements the [Interface] interface for *cmdARPDB.
|
2022-03-15 20:57:46 +03:00
|
|
|
func (arp *cmdARPDB) Refresh() (err error) {
|
|
|
|
defer func() { err = errors.Annotate(err, "cmd arpdb: %w") }()
|
|
|
|
|
2022-03-30 15:11:57 +03:00
|
|
|
code, out, err := aghosRunCommand(arp.cmd, arp.args...)
|
2022-03-15 20:57:46 +03:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("running command: %w", err)
|
2022-03-30 15:11:57 +03:00
|
|
|
} else if code != 0 {
|
|
|
|
return fmt.Errorf("running command: unexpected exit code %d", code)
|
2022-03-15 20:57:46 +03:00
|
|
|
}
|
|
|
|
|
2022-03-30 15:11:57 +03:00
|
|
|
sc := bufio.NewScanner(bytes.NewReader(out))
|
2024-08-27 20:42:10 +03:00
|
|
|
ns := arp.parse(arp.logger, sc, arp.ns.len())
|
2022-03-15 20:57:46 +03:00
|
|
|
if err = sc.Err(); err != nil {
|
2022-03-30 15:11:57 +03:00
|
|
|
// TODO(e.burkov): This error seems unreachable. Investigate.
|
2022-03-15 20:57:46 +03:00
|
|
|
return fmt.Errorf("scanning the output: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
arp.ns.reset(ns)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-08-24 13:42:17 +03:00
|
|
|
// Neighbors implements the [Interface] interface for *cmdARPDB.
|
2022-03-15 20:57:46 +03:00
|
|
|
func (arp *cmdARPDB) Neighbors() (ns []Neighbor) {
|
|
|
|
return arp.ns.clone()
|
|
|
|
}
|
|
|
|
|
2023-08-24 13:42:17 +03:00
|
|
|
// arpdbs is the [Interface] that combines several [Interface] implementations
|
|
|
|
// and consequently switches between those.
|
2022-03-15 20:57:46 +03:00
|
|
|
type arpdbs struct {
|
2023-08-24 13:42:17 +03:00
|
|
|
// arps is the set of [Interface] implementations to range through.
|
|
|
|
arps []Interface
|
2022-03-30 15:11:57 +03:00
|
|
|
neighs
|
2022-03-15 20:57:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// newARPDBs returns a properly initialized *arpdbs. It begins refreshing from
|
|
|
|
// the first of arps.
|
2023-08-24 13:42:17 +03:00
|
|
|
func newARPDBs(arps ...Interface) (arp *arpdbs) {
|
2022-03-15 20:57:46 +03:00
|
|
|
return &arpdbs{
|
|
|
|
arps: arps,
|
2022-03-30 15:11:57 +03:00
|
|
|
neighs: neighs{
|
|
|
|
mu: &sync.RWMutex{},
|
|
|
|
ns: make([]Neighbor, 0),
|
|
|
|
},
|
2022-03-15 20:57:46 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// type check
|
2023-08-24 13:42:17 +03:00
|
|
|
var _ Interface = (*arpdbs)(nil)
|
2022-03-15 20:57:46 +03:00
|
|
|
|
2023-08-24 13:42:17 +03:00
|
|
|
// Refresh implements the [Interface] interface for *arpdbs.
|
2022-03-15 20:57:46 +03:00
|
|
|
func (arp *arpdbs) Refresh() (err error) {
|
|
|
|
var errs []error
|
2022-03-30 15:11:57 +03:00
|
|
|
|
|
|
|
for _, a := range arp.arps {
|
|
|
|
err = a.Refresh()
|
|
|
|
if err != nil {
|
|
|
|
errs = append(errs, err)
|
|
|
|
|
|
|
|
continue
|
2022-03-15 20:57:46 +03:00
|
|
|
}
|
|
|
|
|
2022-03-30 15:11:57 +03:00
|
|
|
arp.reset(a.Neighbors())
|
|
|
|
|
|
|
|
return nil
|
2022-03-15 20:57:46 +03:00
|
|
|
}
|
|
|
|
|
2023-08-15 15:09:08 +03:00
|
|
|
return errors.Annotate(errors.Join(errs...), "each arpdb failed: %w")
|
2022-03-15 20:57:46 +03:00
|
|
|
}
|
|
|
|
|
2023-08-24 13:42:17 +03:00
|
|
|
// Neighbors implements the [Interface] interface for *arpdbs.
|
2022-03-30 15:11:57 +03:00
|
|
|
//
|
|
|
|
// TODO(e.burkov): Think of a way to avoid cloning the slice twice.
|
2022-03-15 20:57:46 +03:00
|
|
|
func (arp *arpdbs) Neighbors() (ns []Neighbor) {
|
2022-03-30 15:11:57 +03:00
|
|
|
return arp.clone()
|
2022-03-15 20:57:46 +03:00
|
|
|
}
|