Pull request 1967: AG-24794-imp-arpdb

Squashed commit of the following:

commit 6f6f6cc5d9b9ae04e369e0b789aaab74f234e6a0
Merge: 9aa3ac58c 8fb76701f
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Thu Aug 24 13:29:47 2023 +0300

    Merge branch 'master' into AG-24794-imp-arpdb

commit 9aa3ac58c76fc4b2f950a988d63dfebd0652e507
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Wed Aug 23 16:14:02 2023 +0300

    scripts: gocognit: add arpdb

commit e99b0534be1891de1c13f4010beeedb4459ccd7c
Merge: 84893bc2d 3722c2846
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Wed Aug 23 16:08:25 2023 +0300

    Merge branch 'master' into AG-24794-imp-arpdb

commit 84893bc2d3018c9ee1e411578b33cdb6ba6d3d81
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Wed Aug 23 16:07:43 2023 +0300

    arpdb: add todo

commit ad4b3689b51324521bf47c478c61b6008332b4f5
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Wed Aug 23 14:02:07 2023 +0300

    arpdb: imp code

commit 9cdd17dadbb91ccc3f8e79ba7a21bc365647e089
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Fri Aug 18 19:05:10 2023 +0300

    all: imp arpdb
This commit is contained in:
Stanislav Chzhen 2023-08-24 13:42:17 +03:00
parent 8fb76701f4
commit 6fea7099a2
15 changed files with 203 additions and 136 deletions

View file

@ -7,7 +7,6 @@ import (
"io/fs"
"net"
"net/netip"
"os"
"strings"
"testing"
@ -18,9 +17,6 @@ import (
"github.com/stretchr/testify/require"
)
// testdata is the filesystem containing data for testing the package.
var testdata fs.FS = os.DirFS("./testdata")
// substRootDirFS replaces the aghos.RootDirFS function used throughout the
// package with fsys for tests ran under t.
func substRootDirFS(t testing.TB, fsys fs.FS) {

View file

@ -1,4 +1,5 @@
package aghnet
// Package arpdb implements the Network Neighborhood Database.
package arpdb
import (
"bufio"
@ -8,15 +9,25 @@ import (
"net/netip"
"sync"
"github.com/AdguardTeam/AdGuardHome/internal/aghos"
"github.com/AdguardTeam/golibs/errors"
"github.com/AdguardTeam/golibs/log"
"github.com/AdguardTeam/golibs/netutil"
"golang.org/x/exp/slices"
)
// ARPDB: The Network Neighborhood Database
// Variables and functions to substitute in tests.
var (
// aghosRunCommand is the function to run shell commands.
aghosRunCommand = aghos.RunCommand
// ARPDB stores and refreshes the network neighborhood reported by ARP (Address
// Resolution Protocol).
type ARPDB interface {
// rootDirFS is the filesystem pointing to the root directory.
rootDirFS = aghos.RootDirFS()
)
// Interface stores and refreshes the network neighborhood reported by ARP
// (Address Resolution Protocol).
type Interface interface {
// Refresh updates the stored data. It must be safe for concurrent use.
Refresh() (err error)
@ -25,28 +36,24 @@ type ARPDB interface {
Neighbors() (ns []Neighbor)
}
// NewARPDB returns the ARPDB properly initialized for the OS.
func NewARPDB() (arp ARPDB) {
// New returns the [Interface] properly initialized for the OS.
func New() (arp Interface) {
return newARPDB()
}
// Empty ARPDB implementation
// EmptyARPDB is the ARPDB implementation that does nothing.
type EmptyARPDB struct{}
// Empty is the [Interface] implementation that does nothing.
type Empty struct{}
// type check
var _ ARPDB = EmptyARPDB{}
var _ Interface = Empty{}
// Refresh implements the ARPDB interface for EmptyARPContainer. It does
// Refresh implements the [Interface] interface for EmptyARPContainer. It does
// nothing and always returns nil error.
func (EmptyARPDB) Refresh() (err error) { return nil }
func (Empty) Refresh() (err error) { return nil }
// Neighbors implements the ARPDB interface for EmptyARPContainer. It always
// returns nil.
func (EmptyARPDB) Neighbors() (ns []Neighbor) { return nil }
// ARPDB Helper Types
// Neighbors implements the [Interface] interface for EmptyARPContainer. It
// always returns nil.
func (Empty) Neighbors() (ns []Neighbor) { return nil }
// Neighbor is the pair of IP address and MAC address reported by ARP.
type Neighbor struct {
@ -70,8 +77,21 @@ func (n Neighbor) Clone() (clone Neighbor) {
}
}
// validatedHostname returns valid hostname. Otherwise returns empty string and
// logs the error if hostname is not valid.
func validatedHostname(h string) (host string) {
err := netutil.ValidateHostname(h)
if err != nil {
log.Debug("arpdb: parsing arp output: host: %s", err)
return ""
}
return h
}
// neighs is the helper type that stores neighbors to avoid copying its methods
// among all the ARPDB implementations.
// among all the [Interface] implementations.
type neighs struct {
mu *sync.RWMutex
ns []Neighbor
@ -108,14 +128,12 @@ func (ns *neighs) reset(with []Neighbor) {
ns.ns = with
}
// Command ARPDB
// 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.
type parseNeighsFunc func(sc *bufio.Scanner, lenHint int) (ns []Neighbor)
// cmdARPDB is the implementation of the ARPDB that uses command line to
// cmdARPDB is the implementation of the [Interface] that uses command line to
// retrieve data.
type cmdARPDB struct {
parse parseNeighsFunc
@ -125,9 +143,9 @@ type cmdARPDB struct {
}
// type check
var _ ARPDB = (*cmdARPDB)(nil)
var _ Interface = (*cmdARPDB)(nil)
// Refresh implements the ARPDB interface for *cmdARPDB.
// Refresh implements the [Interface] interface for *cmdARPDB.
func (arp *cmdARPDB) Refresh() (err error) {
defer func() { err = errors.Annotate(err, "cmd arpdb: %w") }()
@ -150,24 +168,22 @@ func (arp *cmdARPDB) Refresh() (err error) {
return nil
}
// Neighbors implements the ARPDB interface for *cmdARPDB.
// Neighbors implements the [Interface] interface for *cmdARPDB.
func (arp *cmdARPDB) Neighbors() (ns []Neighbor) {
return arp.ns.clone()
}
// Composite ARPDB
// arpdbs is the ARPDB that combines several ARPDB implementations and
// consequently switches between those.
// arpdbs is the [Interface] that combines several [Interface] implementations
// and consequently switches between those.
type arpdbs struct {
// arps is the set of ARPDB implementations to range through.
arps []ARPDB
// arps is the set of [Interface] implementations to range through.
arps []Interface
neighs
}
// newARPDBs returns a properly initialized *arpdbs. It begins refreshing from
// the first of arps.
func newARPDBs(arps ...ARPDB) (arp *arpdbs) {
func newARPDBs(arps ...Interface) (arp *arpdbs) {
return &arpdbs{
arps: arps,
neighs: neighs{
@ -178,9 +194,9 @@ func newARPDBs(arps ...ARPDB) (arp *arpdbs) {
}
// type check
var _ ARPDB = (*arpdbs)(nil)
var _ Interface = (*arpdbs)(nil)
// Refresh implements the ARPDB interface for *arpdbs.
// Refresh implements the [Interface] interface for *arpdbs.
func (arp *arpdbs) Refresh() (err error) {
var errs []error
@ -200,7 +216,7 @@ func (arp *arpdbs) Refresh() (err error) {
return errors.Annotate(errors.Join(errs...), "each arpdb failed: %w")
}
// Neighbors implements the ARPDB interface for *arpdbs.
// Neighbors implements the [Interface] interface for *arpdbs.
//
// TODO(e.burkov): Think of a way to avoid cloning the slice twice.
func (arp *arpdbs) Neighbors() (ns []Neighbor) {

View file

@ -1,6 +1,6 @@
//go:build darwin || freebsd
package aghnet
package arpdb
import (
"bufio"
@ -10,7 +10,6 @@ import (
"sync"
"github.com/AdguardTeam/golibs/log"
"github.com/AdguardTeam/golibs/netutil"
)
func newARPDB() (arp *cmdARPDB) {
@ -44,16 +43,16 @@ func parseArpA(sc *bufio.Scanner, lenHint int) (ns []Neighbor) {
continue
}
n := Neighbor{}
if ipStr := fields[1]; len(ipStr) < 2 {
ipStr := fields[1]
if len(ipStr) < 2 {
continue
} else if ip, err := netip.ParseAddr(ipStr[1 : len(ipStr)-1]); err != nil {
}
ip, err := netip.ParseAddr(ipStr[1 : len(ipStr)-1])
if err != nil {
log.Debug("arpdb: parsing arp output: ip: %s", err)
continue
} else {
n.IP = ip
}
hwStr := fields[3]
@ -62,19 +61,13 @@ func parseArpA(sc *bufio.Scanner, lenHint int) (ns []Neighbor) {
log.Debug("arpdb: parsing arp output: mac: %s", err)
continue
} else {
n.MAC = mac
}
host := fields[0]
err = netutil.ValidateHostname(host)
if err != nil {
log.Debug("arpdb: parsing arp output: host: %s", err)
} else {
n.Name = host
}
ns = append(ns, n)
ns = append(ns, Neighbor{
IP: ip,
MAC: mac,
Name: validatedHostname(fields[0]),
})
}
return ns

View file

@ -1,6 +1,6 @@
//go:build darwin || freebsd
package aghnet
package arpdb
import (
"net"

View file

@ -1,8 +1,12 @@
package aghnet
package arpdb
import (
"fmt"
"io/fs"
"net"
"net/netip"
"os"
"strings"
"sync"
"testing"
@ -12,30 +16,78 @@ import (
"github.com/stretchr/testify/require"
)
func TestNewARPDB(t *testing.T) {
var a ARPDB
require.NotPanics(t, func() { a = NewARPDB() })
// testdata is the filesystem containing data for testing the package.
var testdata fs.FS = os.DirFS("./testdata")
// RunCmdFunc is the signature of aghos.RunCommand function.
type RunCmdFunc func(cmd string, args ...string) (code int, out []byte, err error)
// substShell replaces the the aghos.RunCommand function used throughout the
// package with rc for tests ran under t.
func substShell(t testing.TB, rc RunCmdFunc) {
t.Helper()
prev := aghosRunCommand
t.Cleanup(func() { aghosRunCommand = prev })
aghosRunCommand = rc
}
// mapShell is a substitution of aghos.RunCommand that maps the command to it's
// execution result. It's only needed to simplify testing.
//
// TODO(e.burkov): Perhaps put all the shell interactions behind an interface.
type mapShell map[string]struct {
err error
out string
code int
}
// theOnlyCmd returns mapShell that only handles a single command and arguments
// combination from cmd.
func theOnlyCmd(cmd string, code int, out string, err error) (s mapShell) {
return mapShell{cmd: {code: code, out: out, err: err}}
}
// RunCmd is a RunCmdFunc handled by s.
func (s mapShell) RunCmd(cmd string, args ...string) (code int, out []byte, err error) {
key := strings.Join(append([]string{cmd}, args...), " ")
ret, ok := s[key]
if !ok {
return 0, nil, fmt.Errorf("unexpected shell command %q", key)
}
return ret.code, []byte(ret.out), ret.err
}
func Test_New(t *testing.T) {
var a Interface
require.NotPanics(t, func() { a = New() })
assert.NotNil(t, a)
}
// TestARPDB is the mock implementation of ARPDB to use in tests.
// TODO(s.chzhen): Consider moving mocks into aghtest.
// TestARPDB is the mock implementation of [Interface] to use in tests.
type TestARPDB struct {
OnRefresh func() (err error)
OnNeighbors func() (ns []Neighbor)
}
// Refresh implements the ARPDB interface for *TestARPDB.
// type check
var _ Interface = (*TestARPDB)(nil)
// Refresh implements the [Interface] interface for *TestARPDB.
func (arp *TestARPDB) Refresh() (err error) {
return arp.OnRefresh()
}
// Neighbors implements the ARPDB interface for *TestARPDB.
// Neighbors implements the [Interface] interface for *TestARPDB.
func (arp *TestARPDB) Neighbors() (ns []Neighbor) {
return arp.OnNeighbors()
}
func TestARPDBS(t *testing.T) {
func Test_NewARPDBs(t *testing.T) {
knownIP := netip.MustParseAddr("1.2.3.4")
knownMAC := net.HardwareAddr{0xAB, 0xCD, 0xEF, 0xAB, 0xCD, 0xEF}
@ -195,7 +247,7 @@ func TestCmdARPDB_arpa(t *testing.T) {
}
func TestEmptyARPDB(t *testing.T) {
a := EmptyARPDB{}
a := Empty{}
t.Run("refresh", func(t *testing.T) {
var err error

View file

@ -1,6 +1,6 @@
//go:build linux
package aghnet
package arpdb
import (
"bufio"
@ -13,7 +13,6 @@ import (
"github.com/AdguardTeam/AdGuardHome/internal/aghos"
"github.com/AdguardTeam/golibs/log"
"github.com/AdguardTeam/golibs/netutil"
"github.com/AdguardTeam/golibs/stringutil"
)
@ -68,9 +67,9 @@ type fsysARPDB struct {
}
// type check
var _ ARPDB = (*fsysARPDB)(nil)
var _ Interface = (*fsysARPDB)(nil)
// Refresh implements the ARPDB interface for *fsysARPDB.
// Refresh implements the [Interface] interface for *fsysARPDB.
func (arp *fsysARPDB) Refresh() (err error) {
var f fs.File
f, err = arp.fsys.Open(arp.filename)
@ -88,21 +87,10 @@ func (arp *fsysARPDB) Refresh() (err error) {
ns := make([]Neighbor, 0, arp.ns.len())
for sc.Scan() {
ln := sc.Text()
fields := stringutil.SplitTrimmed(ln, " ")
if len(fields) != 6 {
continue
n := parseNeighbor(sc.Text())
if n != nil {
ns = append(ns, *n)
}
n := Neighbor{}
n.IP, err = netip.ParseAddr(fields[0])
if err != nil || n.IP.IsUnspecified() {
continue
} else if n.MAC, err = net.ParseMAC(fields[3]); err != nil {
continue
}
ns = append(ns, n)
}
arp.ns.reset(ns)
@ -110,7 +98,30 @@ func (arp *fsysARPDB) Refresh() (err error) {
return nil
}
// Neighbors implements the ARPDB interface for *fsysARPDB.
// parseNeighbor parses line into *Neighbor.
func parseNeighbor(line string) (n *Neighbor) {
fields := stringutil.SplitTrimmed(line, " ")
if len(fields) != 6 {
return nil
}
ip, err := netip.ParseAddr(fields[0])
if err != nil || ip.IsUnspecified() {
return nil
}
mac, err := net.ParseMAC(fields[3])
if err != nil {
return nil
}
return &Neighbor{
IP: ip,
MAC: mac,
}
}
// Neighbors implements the [Interface] interface for *fsysARPDB.
func (arp *fsysARPDB) Neighbors() (ns []Neighbor) {
return arp.ns.clone()
}
@ -135,15 +146,11 @@ func parseArpAWrt(sc *bufio.Scanner, lenHint int) (ns []Neighbor) {
continue
}
n := Neighbor{}
ip, err := netip.ParseAddr(fields[0])
if err != nil || n.IP.IsUnspecified() {
if err != nil {
log.Debug("arpdb: parsing arp output: ip: %s", err)
continue
} else {
n.IP = ip
}
hwStr := fields[3]
@ -152,11 +159,12 @@ func parseArpAWrt(sc *bufio.Scanner, lenHint int) (ns []Neighbor) {
log.Debug("arpdb: parsing arp output: mac: %s", err)
continue
} else {
n.MAC = mac
}
ns = append(ns, n)
ns = append(ns, Neighbor{
IP: ip,
MAC: mac,
})
}
return ns
@ -176,35 +184,31 @@ func parseArpA(sc *bufio.Scanner, lenHint int) (ns []Neighbor) {
continue
}
n := Neighbor{}
if ipStr := fields[1]; len(ipStr) < 2 {
ipStr := fields[1]
if len(ipStr) < 2 {
continue
} else if ip, err := netip.ParseAddr(ipStr[1 : len(ipStr)-1]); err != nil {
}
ip, err := netip.ParseAddr(ipStr[1 : len(ipStr)-1])
if err != nil {
log.Debug("arpdb: parsing arp output: ip: %s", err)
continue
} else {
n.IP = ip
}
hwStr := fields[3]
if mac, err := net.ParseMAC(hwStr); err != nil {
mac, err := net.ParseMAC(hwStr)
if err != nil {
log.Debug("arpdb: parsing arp output: mac: %s", err)
continue
} else {
n.MAC = mac
}
host := fields[0]
if verr := netutil.ValidateHostname(host); verr != nil {
log.Debug("arpdb: parsing arp output: host: %s", verr)
} else {
n.Name = host
}
ns = append(ns, n)
ns = append(ns, Neighbor{
IP: ip,
MAC: mac,
Name: validatedHostname(fields[0]),
})
}
return ns

View file

@ -1,6 +1,6 @@
//go:build linux
package aghnet
package arpdb
import (
"net"

View file

@ -1,6 +1,6 @@
//go:build openbsd
package aghnet
package arpdb
import (
"bufio"

View file

@ -1,6 +1,6 @@
//go:build openbsd
package aghnet
package arpdb
import (
"net"

View file

@ -1,6 +1,6 @@
//go:build windows
package aghnet
package arpdb
import (
"bufio"
@ -8,6 +8,8 @@ import (
"net/netip"
"strings"
"sync"
"github.com/AdguardTeam/golibs/log"
)
func newARPDB() (arp *cmdARPDB) {
@ -42,23 +44,24 @@ func parseArpA(sc *bufio.Scanner, lenHint int) (ns []Neighbor) {
continue
}
n := Neighbor{}
ip, err := netip.ParseAddr(fields[0])
if err != nil {
log.Debug("arpdb: parsing arp output: ip: %s", err)
continue
} else {
n.IP = ip
}
mac, err := net.ParseMAC(fields[1])
if err != nil {
log.Debug("arpdb: parsing arp output: mac: %s", err)
continue
} else {
n.MAC = mac
}
ns = append(ns, n)
ns = append(ns, Neighbor{
IP: ip,
MAC: mac,
})
}
return ns

View file

@ -1,6 +1,6 @@
//go:build windows
package aghnet
package arpdb
import (
"net"

View file

@ -10,6 +10,7 @@ import (
"time"
"github.com/AdguardTeam/AdGuardHome/internal/aghnet"
"github.com/AdguardTeam/AdGuardHome/internal/arpdb"
"github.com/AdguardTeam/AdGuardHome/internal/client"
"github.com/AdguardTeam/AdGuardHome/internal/dhcpd"
"github.com/AdguardTeam/AdGuardHome/internal/dhcpsvc"
@ -65,8 +66,8 @@ type clientsContainer struct {
// hosts database.
etcHosts *aghnet.HostsContainer
// arpdb stores the neighbors retrieved from ARP.
arpdb aghnet.ARPDB
// arpDB stores the neighbors retrieved from ARP.
arpDB arpdb.Interface
// lock protects all fields.
//
@ -95,7 +96,7 @@ func (clients *clientsContainer) Init(
objects []*clientObject,
dhcpServer dhcpd.Interface,
etcHosts *aghnet.HostsContainer,
arpdb aghnet.ARPDB,
arpDB arpdb.Interface,
filteringConf *filtering.Config,
) (err error) {
if clients.list != nil {
@ -110,7 +111,7 @@ func (clients *clientsContainer) Init(
clients.dhcpServer = dhcpServer
clients.etcHosts = etcHosts
clients.arpdb = arpdb
clients.arpDB = arpDB
err = clients.addFromConfig(objects, filteringConf)
if err != nil {
// Don't wrap the error, because it's informative enough as is.
@ -164,7 +165,7 @@ func (clients *clientsContainer) Start() {
// reloadARP reloads runtime clients from ARP, if configured.
func (clients *clientsContainer) reloadARP() {
if clients.arpdb != nil {
if clients.arpDB != nil {
clients.addFromSystemARP()
}
}
@ -877,15 +878,15 @@ func (clients *clientsContainer) addFromHostsFile(hosts aghnet.Hosts) {
// addFromSystemARP adds the IP-hostname pairings from the output of the arp -a
// command.
func (clients *clientsContainer) addFromSystemARP() {
if err := clients.arpdb.Refresh(); err != nil {
if err := clients.arpDB.Refresh(); err != nil {
log.Error("refreshing arp container: %s", err)
clients.arpdb = aghnet.EmptyARPDB{}
clients.arpDB = arpdb.Empty{}
return
}
ns := clients.arpdb.Neighbors()
ns := clients.arpDB.Neighbors()
if len(ns) == 0 {
log.Debug("refreshing arp container: the update is empty")

View file

@ -22,6 +22,7 @@ import (
"github.com/AdguardTeam/AdGuardHome/internal/aghnet"
"github.com/AdguardTeam/AdGuardHome/internal/aghos"
"github.com/AdguardTeam/AdGuardHome/internal/aghtls"
"github.com/AdguardTeam/AdGuardHome/internal/arpdb"
"github.com/AdguardTeam/AdGuardHome/internal/dhcpd"
"github.com/AdguardTeam/AdGuardHome/internal/dnsforward"
"github.com/AdguardTeam/AdGuardHome/internal/filtering"
@ -289,16 +290,16 @@ func initContextClients() (err error) {
return fmt.Errorf("initing dhcp: %w", err)
}
var arpdb aghnet.ARPDB
var arpDB arpdb.Interface
if config.Clients.Sources.ARP {
arpdb = aghnet.NewARPDB()
arpDB = arpdb.New()
}
err = Context.clients.Init(
config.Clients.Persistent,
Context.dhcpServer,
Context.etcHosts,
arpdb,
arpDB,
config.DNS.DnsfilterConf,
)
if err != nil {

View file

@ -191,6 +191,7 @@ gocognit_paths="\
./internal/aghhttp/ 10
./internal/aghio/ 10
./internal/aghrenameio/ 10
./internal/arpdb/ 10
./internal/client/ 10
./internal/dhcpsvc 10
./internal/filtering/hashprefix/ 10