mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-12-15 03:02:07 +03:00
Pull request 1967: AG-24794-imp-arpdb
Squashed commit of the following: commit 6f6f6cc5d9b9ae04e369e0b789aaab74f234e6a0 Merge: 9aa3ac58c8fb76701f
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: 84893bc2d3722c2846
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:
parent
8fb76701f4
commit
6fea7099a2
@ -7,7 +7,6 @@ import (
|
|||||||
"io/fs"
|
"io/fs"
|
||||||
"net"
|
"net"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"os"
|
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@ -18,9 +17,6 @@ import (
|
|||||||
"github.com/stretchr/testify/require"
|
"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
|
// substRootDirFS replaces the aghos.RootDirFS function used throughout the
|
||||||
// package with fsys for tests ran under t.
|
// package with fsys for tests ran under t.
|
||||||
func substRootDirFS(t testing.TB, fsys fs.FS) {
|
func substRootDirFS(t testing.TB, fsys fs.FS) {
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
package aghnet
|
// Package arpdb implements the Network Neighborhood Database.
|
||||||
|
package arpdb
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
@ -8,15 +9,25 @@ import (
|
|||||||
"net/netip"
|
"net/netip"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
|
"github.com/AdguardTeam/AdGuardHome/internal/aghos"
|
||||||
"github.com/AdguardTeam/golibs/errors"
|
"github.com/AdguardTeam/golibs/errors"
|
||||||
|
"github.com/AdguardTeam/golibs/log"
|
||||||
|
"github.com/AdguardTeam/golibs/netutil"
|
||||||
"golang.org/x/exp/slices"
|
"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
|
// rootDirFS is the filesystem pointing to the root directory.
|
||||||
// Resolution Protocol).
|
rootDirFS = aghos.RootDirFS()
|
||||||
type ARPDB interface {
|
)
|
||||||
|
|
||||||
|
// 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 updates the stored data. It must be safe for concurrent use.
|
||||||
Refresh() (err error)
|
Refresh() (err error)
|
||||||
|
|
||||||
@ -25,28 +36,24 @@ type ARPDB interface {
|
|||||||
Neighbors() (ns []Neighbor)
|
Neighbors() (ns []Neighbor)
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewARPDB returns the ARPDB properly initialized for the OS.
|
// New returns the [Interface] properly initialized for the OS.
|
||||||
func NewARPDB() (arp ARPDB) {
|
func New() (arp Interface) {
|
||||||
return newARPDB()
|
return newARPDB()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Empty ARPDB implementation
|
// Empty is the [Interface] implementation that does nothing.
|
||||||
|
type Empty struct{}
|
||||||
// EmptyARPDB is the ARPDB implementation that does nothing.
|
|
||||||
type EmptyARPDB struct{}
|
|
||||||
|
|
||||||
// type check
|
// 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.
|
// 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
|
// Neighbors implements the [Interface] interface for EmptyARPContainer. It
|
||||||
// returns nil.
|
// always returns nil.
|
||||||
func (EmptyARPDB) Neighbors() (ns []Neighbor) { return nil }
|
func (Empty) Neighbors() (ns []Neighbor) { return nil }
|
||||||
|
|
||||||
// ARPDB Helper Types
|
|
||||||
|
|
||||||
// Neighbor is the pair of IP address and MAC address reported by ARP.
|
// Neighbor is the pair of IP address and MAC address reported by ARP.
|
||||||
type Neighbor struct {
|
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
|
// 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 {
|
type neighs struct {
|
||||||
mu *sync.RWMutex
|
mu *sync.RWMutex
|
||||||
ns []Neighbor
|
ns []Neighbor
|
||||||
@ -108,14 +128,12 @@ func (ns *neighs) reset(with []Neighbor) {
|
|||||||
ns.ns = with
|
ns.ns = with
|
||||||
}
|
}
|
||||||
|
|
||||||
// Command ARPDB
|
|
||||||
|
|
||||||
// parseNeighsFunc parses the text from sc as if it'd be an output of some
|
// 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
|
// ARP-related command. lenHint is a hint for the size of the allocated slice
|
||||||
// of Neighbors.
|
// of Neighbors.
|
||||||
type parseNeighsFunc func(sc *bufio.Scanner, lenHint int) (ns []Neighbor)
|
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.
|
// retrieve data.
|
||||||
type cmdARPDB struct {
|
type cmdARPDB struct {
|
||||||
parse parseNeighsFunc
|
parse parseNeighsFunc
|
||||||
@ -125,9 +143,9 @@ type cmdARPDB struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// type check
|
// 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) {
|
func (arp *cmdARPDB) Refresh() (err error) {
|
||||||
defer func() { err = errors.Annotate(err, "cmd arpdb: %w") }()
|
defer func() { err = errors.Annotate(err, "cmd arpdb: %w") }()
|
||||||
|
|
||||||
@ -150,24 +168,22 @@ func (arp *cmdARPDB) Refresh() (err error) {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Neighbors implements the ARPDB interface for *cmdARPDB.
|
// Neighbors implements the [Interface] interface for *cmdARPDB.
|
||||||
func (arp *cmdARPDB) Neighbors() (ns []Neighbor) {
|
func (arp *cmdARPDB) Neighbors() (ns []Neighbor) {
|
||||||
return arp.ns.clone()
|
return arp.ns.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Composite ARPDB
|
// arpdbs is the [Interface] that combines several [Interface] implementations
|
||||||
|
// and consequently switches between those.
|
||||||
// arpdbs is the ARPDB that combines several ARPDB implementations and
|
|
||||||
// consequently switches between those.
|
|
||||||
type arpdbs struct {
|
type arpdbs struct {
|
||||||
// arps is the set of ARPDB implementations to range through.
|
// arps is the set of [Interface] implementations to range through.
|
||||||
arps []ARPDB
|
arps []Interface
|
||||||
neighs
|
neighs
|
||||||
}
|
}
|
||||||
|
|
||||||
// newARPDBs returns a properly initialized *arpdbs. It begins refreshing from
|
// newARPDBs returns a properly initialized *arpdbs. It begins refreshing from
|
||||||
// the first of arps.
|
// the first of arps.
|
||||||
func newARPDBs(arps ...ARPDB) (arp *arpdbs) {
|
func newARPDBs(arps ...Interface) (arp *arpdbs) {
|
||||||
return &arpdbs{
|
return &arpdbs{
|
||||||
arps: arps,
|
arps: arps,
|
||||||
neighs: neighs{
|
neighs: neighs{
|
||||||
@ -178,9 +194,9 @@ func newARPDBs(arps ...ARPDB) (arp *arpdbs) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// type check
|
// 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) {
|
func (arp *arpdbs) Refresh() (err error) {
|
||||||
var errs []error
|
var errs []error
|
||||||
|
|
||||||
@ -200,7 +216,7 @@ func (arp *arpdbs) Refresh() (err error) {
|
|||||||
return errors.Annotate(errors.Join(errs...), "each arpdb failed: %w")
|
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.
|
// TODO(e.burkov): Think of a way to avoid cloning the slice twice.
|
||||||
func (arp *arpdbs) Neighbors() (ns []Neighbor) {
|
func (arp *arpdbs) Neighbors() (ns []Neighbor) {
|
@ -1,6 +1,6 @@
|
|||||||
//go:build darwin || freebsd
|
//go:build darwin || freebsd
|
||||||
|
|
||||||
package aghnet
|
package arpdb
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
@ -10,7 +10,6 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/AdguardTeam/golibs/log"
|
"github.com/AdguardTeam/golibs/log"
|
||||||
"github.com/AdguardTeam/golibs/netutil"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func newARPDB() (arp *cmdARPDB) {
|
func newARPDB() (arp *cmdARPDB) {
|
||||||
@ -44,16 +43,16 @@ func parseArpA(sc *bufio.Scanner, lenHint int) (ns []Neighbor) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
n := Neighbor{}
|
ipStr := fields[1]
|
||||||
|
if len(ipStr) < 2 {
|
||||||
if ipStr := fields[1]; len(ipStr) < 2 {
|
|
||||||
continue
|
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)
|
log.Debug("arpdb: parsing arp output: ip: %s", err)
|
||||||
|
|
||||||
continue
|
continue
|
||||||
} else {
|
|
||||||
n.IP = ip
|
|
||||||
}
|
}
|
||||||
|
|
||||||
hwStr := fields[3]
|
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)
|
log.Debug("arpdb: parsing arp output: mac: %s", err)
|
||||||
|
|
||||||
continue
|
continue
|
||||||
} else {
|
|
||||||
n.MAC = mac
|
|
||||||
}
|
}
|
||||||
|
|
||||||
host := fields[0]
|
ns = append(ns, Neighbor{
|
||||||
err = netutil.ValidateHostname(host)
|
IP: ip,
|
||||||
if err != nil {
|
MAC: mac,
|
||||||
log.Debug("arpdb: parsing arp output: host: %s", err)
|
Name: validatedHostname(fields[0]),
|
||||||
} else {
|
})
|
||||||
n.Name = host
|
|
||||||
}
|
|
||||||
|
|
||||||
ns = append(ns, n)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return ns
|
return ns
|
@ -1,6 +1,6 @@
|
|||||||
//go:build darwin || freebsd
|
//go:build darwin || freebsd
|
||||||
|
|
||||||
package aghnet
|
package arpdb
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net"
|
"net"
|
@ -1,8 +1,12 @@
|
|||||||
package aghnet
|
package arpdb
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io/fs"
|
||||||
"net"
|
"net"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@ -12,30 +16,78 @@ import (
|
|||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestNewARPDB(t *testing.T) {
|
// testdata is the filesystem containing data for testing the package.
|
||||||
var a ARPDB
|
var testdata fs.FS = os.DirFS("./testdata")
|
||||||
require.NotPanics(t, func() { a = NewARPDB() })
|
|
||||||
|
// 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)
|
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 {
|
type TestARPDB struct {
|
||||||
OnRefresh func() (err error)
|
OnRefresh func() (err error)
|
||||||
OnNeighbors func() (ns []Neighbor)
|
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) {
|
func (arp *TestARPDB) Refresh() (err error) {
|
||||||
return arp.OnRefresh()
|
return arp.OnRefresh()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Neighbors implements the ARPDB interface for *TestARPDB.
|
// Neighbors implements the [Interface] interface for *TestARPDB.
|
||||||
func (arp *TestARPDB) Neighbors() (ns []Neighbor) {
|
func (arp *TestARPDB) Neighbors() (ns []Neighbor) {
|
||||||
return arp.OnNeighbors()
|
return arp.OnNeighbors()
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestARPDBS(t *testing.T) {
|
func Test_NewARPDBs(t *testing.T) {
|
||||||
knownIP := netip.MustParseAddr("1.2.3.4")
|
knownIP := netip.MustParseAddr("1.2.3.4")
|
||||||
knownMAC := net.HardwareAddr{0xAB, 0xCD, 0xEF, 0xAB, 0xCD, 0xEF}
|
knownMAC := net.HardwareAddr{0xAB, 0xCD, 0xEF, 0xAB, 0xCD, 0xEF}
|
||||||
|
|
||||||
@ -195,7 +247,7 @@ func TestCmdARPDB_arpa(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestEmptyARPDB(t *testing.T) {
|
func TestEmptyARPDB(t *testing.T) {
|
||||||
a := EmptyARPDB{}
|
a := Empty{}
|
||||||
|
|
||||||
t.Run("refresh", func(t *testing.T) {
|
t.Run("refresh", func(t *testing.T) {
|
||||||
var err error
|
var err error
|
@ -1,6 +1,6 @@
|
|||||||
//go:build linux
|
//go:build linux
|
||||||
|
|
||||||
package aghnet
|
package arpdb
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
@ -13,7 +13,6 @@ import (
|
|||||||
|
|
||||||
"github.com/AdguardTeam/AdGuardHome/internal/aghos"
|
"github.com/AdguardTeam/AdGuardHome/internal/aghos"
|
||||||
"github.com/AdguardTeam/golibs/log"
|
"github.com/AdguardTeam/golibs/log"
|
||||||
"github.com/AdguardTeam/golibs/netutil"
|
|
||||||
"github.com/AdguardTeam/golibs/stringutil"
|
"github.com/AdguardTeam/golibs/stringutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -68,9 +67,9 @@ type fsysARPDB struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// type check
|
// 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) {
|
func (arp *fsysARPDB) Refresh() (err error) {
|
||||||
var f fs.File
|
var f fs.File
|
||||||
f, err = arp.fsys.Open(arp.filename)
|
f, err = arp.fsys.Open(arp.filename)
|
||||||
@ -88,21 +87,10 @@ func (arp *fsysARPDB) Refresh() (err error) {
|
|||||||
|
|
||||||
ns := make([]Neighbor, 0, arp.ns.len())
|
ns := make([]Neighbor, 0, arp.ns.len())
|
||||||
for sc.Scan() {
|
for sc.Scan() {
|
||||||
ln := sc.Text()
|
n := parseNeighbor(sc.Text())
|
||||||
fields := stringutil.SplitTrimmed(ln, " ")
|
if n != nil {
|
||||||
if len(fields) != 6 {
|
ns = append(ns, *n)
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
arp.ns.reset(ns)
|
||||||
@ -110,7 +98,30 @@ func (arp *fsysARPDB) Refresh() (err error) {
|
|||||||
return nil
|
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) {
|
func (arp *fsysARPDB) Neighbors() (ns []Neighbor) {
|
||||||
return arp.ns.clone()
|
return arp.ns.clone()
|
||||||
}
|
}
|
||||||
@ -135,15 +146,11 @@ func parseArpAWrt(sc *bufio.Scanner, lenHint int) (ns []Neighbor) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
n := Neighbor{}
|
|
||||||
|
|
||||||
ip, err := netip.ParseAddr(fields[0])
|
ip, err := netip.ParseAddr(fields[0])
|
||||||
if err != nil || n.IP.IsUnspecified() {
|
if err != nil {
|
||||||
log.Debug("arpdb: parsing arp output: ip: %s", err)
|
log.Debug("arpdb: parsing arp output: ip: %s", err)
|
||||||
|
|
||||||
continue
|
continue
|
||||||
} else {
|
|
||||||
n.IP = ip
|
|
||||||
}
|
}
|
||||||
|
|
||||||
hwStr := fields[3]
|
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)
|
log.Debug("arpdb: parsing arp output: mac: %s", err)
|
||||||
|
|
||||||
continue
|
continue
|
||||||
} else {
|
|
||||||
n.MAC = mac
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ns = append(ns, n)
|
ns = append(ns, Neighbor{
|
||||||
|
IP: ip,
|
||||||
|
MAC: mac,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
return ns
|
return ns
|
||||||
@ -176,35 +184,31 @@ func parseArpA(sc *bufio.Scanner, lenHint int) (ns []Neighbor) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
n := Neighbor{}
|
ipStr := fields[1]
|
||||||
|
if len(ipStr) < 2 {
|
||||||
if ipStr := fields[1]; len(ipStr) < 2 {
|
|
||||||
continue
|
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)
|
log.Debug("arpdb: parsing arp output: ip: %s", err)
|
||||||
|
|
||||||
continue
|
continue
|
||||||
} else {
|
|
||||||
n.IP = ip
|
|
||||||
}
|
}
|
||||||
|
|
||||||
hwStr := fields[3]
|
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)
|
log.Debug("arpdb: parsing arp output: mac: %s", err)
|
||||||
|
|
||||||
continue
|
continue
|
||||||
} else {
|
|
||||||
n.MAC = mac
|
|
||||||
}
|
}
|
||||||
|
|
||||||
host := fields[0]
|
ns = append(ns, Neighbor{
|
||||||
if verr := netutil.ValidateHostname(host); verr != nil {
|
IP: ip,
|
||||||
log.Debug("arpdb: parsing arp output: host: %s", verr)
|
MAC: mac,
|
||||||
} else {
|
Name: validatedHostname(fields[0]),
|
||||||
n.Name = host
|
})
|
||||||
}
|
|
||||||
|
|
||||||
ns = append(ns, n)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return ns
|
return ns
|
@ -1,6 +1,6 @@
|
|||||||
//go:build linux
|
//go:build linux
|
||||||
|
|
||||||
package aghnet
|
package arpdb
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net"
|
"net"
|
@ -1,6 +1,6 @@
|
|||||||
//go:build openbsd
|
//go:build openbsd
|
||||||
|
|
||||||
package aghnet
|
package arpdb
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
@ -1,6 +1,6 @@
|
|||||||
//go:build openbsd
|
//go:build openbsd
|
||||||
|
|
||||||
package aghnet
|
package arpdb
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net"
|
"net"
|
@ -1,6 +1,6 @@
|
|||||||
//go:build windows
|
//go:build windows
|
||||||
|
|
||||||
package aghnet
|
package arpdb
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
@ -8,6 +8,8 @@ import (
|
|||||||
"net/netip"
|
"net/netip"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
|
"github.com/AdguardTeam/golibs/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
func newARPDB() (arp *cmdARPDB) {
|
func newARPDB() (arp *cmdARPDB) {
|
||||||
@ -42,23 +44,24 @@ func parseArpA(sc *bufio.Scanner, lenHint int) (ns []Neighbor) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
n := Neighbor{}
|
|
||||||
|
|
||||||
ip, err := netip.ParseAddr(fields[0])
|
ip, err := netip.ParseAddr(fields[0])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.Debug("arpdb: parsing arp output: ip: %s", err)
|
||||||
|
|
||||||
continue
|
continue
|
||||||
} else {
|
|
||||||
n.IP = ip
|
|
||||||
}
|
}
|
||||||
|
|
||||||
mac, err := net.ParseMAC(fields[1])
|
mac, err := net.ParseMAC(fields[1])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.Debug("arpdb: parsing arp output: mac: %s", err)
|
||||||
|
|
||||||
continue
|
continue
|
||||||
} else {
|
|
||||||
n.MAC = mac
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ns = append(ns, n)
|
ns = append(ns, Neighbor{
|
||||||
|
IP: ip,
|
||||||
|
MAC: mac,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
return ns
|
return ns
|
@ -1,6 +1,6 @@
|
|||||||
//go:build windows
|
//go:build windows
|
||||||
|
|
||||||
package aghnet
|
package arpdb
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net"
|
"net"
|
@ -10,6 +10,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/AdguardTeam/AdGuardHome/internal/aghnet"
|
"github.com/AdguardTeam/AdGuardHome/internal/aghnet"
|
||||||
|
"github.com/AdguardTeam/AdGuardHome/internal/arpdb"
|
||||||
"github.com/AdguardTeam/AdGuardHome/internal/client"
|
"github.com/AdguardTeam/AdGuardHome/internal/client"
|
||||||
"github.com/AdguardTeam/AdGuardHome/internal/dhcpd"
|
"github.com/AdguardTeam/AdGuardHome/internal/dhcpd"
|
||||||
"github.com/AdguardTeam/AdGuardHome/internal/dhcpsvc"
|
"github.com/AdguardTeam/AdGuardHome/internal/dhcpsvc"
|
||||||
@ -65,8 +66,8 @@ type clientsContainer struct {
|
|||||||
// hosts database.
|
// hosts database.
|
||||||
etcHosts *aghnet.HostsContainer
|
etcHosts *aghnet.HostsContainer
|
||||||
|
|
||||||
// arpdb stores the neighbors retrieved from ARP.
|
// arpDB stores the neighbors retrieved from ARP.
|
||||||
arpdb aghnet.ARPDB
|
arpDB arpdb.Interface
|
||||||
|
|
||||||
// lock protects all fields.
|
// lock protects all fields.
|
||||||
//
|
//
|
||||||
@ -95,7 +96,7 @@ func (clients *clientsContainer) Init(
|
|||||||
objects []*clientObject,
|
objects []*clientObject,
|
||||||
dhcpServer dhcpd.Interface,
|
dhcpServer dhcpd.Interface,
|
||||||
etcHosts *aghnet.HostsContainer,
|
etcHosts *aghnet.HostsContainer,
|
||||||
arpdb aghnet.ARPDB,
|
arpDB arpdb.Interface,
|
||||||
filteringConf *filtering.Config,
|
filteringConf *filtering.Config,
|
||||||
) (err error) {
|
) (err error) {
|
||||||
if clients.list != nil {
|
if clients.list != nil {
|
||||||
@ -110,7 +111,7 @@ func (clients *clientsContainer) Init(
|
|||||||
|
|
||||||
clients.dhcpServer = dhcpServer
|
clients.dhcpServer = dhcpServer
|
||||||
clients.etcHosts = etcHosts
|
clients.etcHosts = etcHosts
|
||||||
clients.arpdb = arpdb
|
clients.arpDB = arpDB
|
||||||
err = clients.addFromConfig(objects, filteringConf)
|
err = clients.addFromConfig(objects, filteringConf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// Don't wrap the error, because it's informative enough as is.
|
// 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.
|
// reloadARP reloads runtime clients from ARP, if configured.
|
||||||
func (clients *clientsContainer) reloadARP() {
|
func (clients *clientsContainer) reloadARP() {
|
||||||
if clients.arpdb != nil {
|
if clients.arpDB != nil {
|
||||||
clients.addFromSystemARP()
|
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
|
// addFromSystemARP adds the IP-hostname pairings from the output of the arp -a
|
||||||
// command.
|
// command.
|
||||||
func (clients *clientsContainer) addFromSystemARP() {
|
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)
|
log.Error("refreshing arp container: %s", err)
|
||||||
|
|
||||||
clients.arpdb = aghnet.EmptyARPDB{}
|
clients.arpDB = arpdb.Empty{}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
ns := clients.arpdb.Neighbors()
|
ns := clients.arpDB.Neighbors()
|
||||||
if len(ns) == 0 {
|
if len(ns) == 0 {
|
||||||
log.Debug("refreshing arp container: the update is empty")
|
log.Debug("refreshing arp container: the update is empty")
|
||||||
|
|
||||||
|
@ -22,6 +22,7 @@ import (
|
|||||||
"github.com/AdguardTeam/AdGuardHome/internal/aghnet"
|
"github.com/AdguardTeam/AdGuardHome/internal/aghnet"
|
||||||
"github.com/AdguardTeam/AdGuardHome/internal/aghos"
|
"github.com/AdguardTeam/AdGuardHome/internal/aghos"
|
||||||
"github.com/AdguardTeam/AdGuardHome/internal/aghtls"
|
"github.com/AdguardTeam/AdGuardHome/internal/aghtls"
|
||||||
|
"github.com/AdguardTeam/AdGuardHome/internal/arpdb"
|
||||||
"github.com/AdguardTeam/AdGuardHome/internal/dhcpd"
|
"github.com/AdguardTeam/AdGuardHome/internal/dhcpd"
|
||||||
"github.com/AdguardTeam/AdGuardHome/internal/dnsforward"
|
"github.com/AdguardTeam/AdGuardHome/internal/dnsforward"
|
||||||
"github.com/AdguardTeam/AdGuardHome/internal/filtering"
|
"github.com/AdguardTeam/AdGuardHome/internal/filtering"
|
||||||
@ -289,16 +290,16 @@ func initContextClients() (err error) {
|
|||||||
return fmt.Errorf("initing dhcp: %w", err)
|
return fmt.Errorf("initing dhcp: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var arpdb aghnet.ARPDB
|
var arpDB arpdb.Interface
|
||||||
if config.Clients.Sources.ARP {
|
if config.Clients.Sources.ARP {
|
||||||
arpdb = aghnet.NewARPDB()
|
arpDB = arpdb.New()
|
||||||
}
|
}
|
||||||
|
|
||||||
err = Context.clients.Init(
|
err = Context.clients.Init(
|
||||||
config.Clients.Persistent,
|
config.Clients.Persistent,
|
||||||
Context.dhcpServer,
|
Context.dhcpServer,
|
||||||
Context.etcHosts,
|
Context.etcHosts,
|
||||||
arpdb,
|
arpDB,
|
||||||
config.DNS.DnsfilterConf,
|
config.DNS.DnsfilterConf,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -191,6 +191,7 @@ gocognit_paths="\
|
|||||||
./internal/aghhttp/ 10
|
./internal/aghhttp/ 10
|
||||||
./internal/aghio/ 10
|
./internal/aghio/ 10
|
||||||
./internal/aghrenameio/ 10
|
./internal/aghrenameio/ 10
|
||||||
|
./internal/arpdb/ 10
|
||||||
./internal/client/ 10
|
./internal/client/ 10
|
||||||
./internal/dhcpsvc 10
|
./internal/dhcpsvc 10
|
||||||
./internal/filtering/hashprefix/ 10
|
./internal/filtering/hashprefix/ 10
|
||||||
|
Loading…
Reference in New Issue
Block a user