Pull request: 3597 arpdb
Merge in DNS/adguard-home from 3597-wrt-netlink to master
Updates #3597.
Squashed commit of the following:
commit 1709582cd204bb80c84775feabae8723ed3340f6
Merge: 0507b6ed e7b3c996
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date: Tue Mar 15 20:25:18 2022 +0300
Merge branch 'master' into 3597-wrt-netlink
commit 0507b6ede1162554ca8ac7399d827264aca64f98
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date: Tue Mar 15 20:21:29 2022 +0300
all: imp code
commit 71f9758d854b3e2cf90cbd3655ae4818cfbcf528
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date: Wed Mar 9 18:03:48 2022 +0500
aghnet: imp naming
commit c949e765104f130aa3e5ba465bdebc3286bebe44
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date: Wed Mar 9 17:26:30 2022 +0500
all: imp code, docs
commit cf605ddb401b6e7b0a7a4bb1b175a4dc588d253a
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date: Tue Mar 8 15:33:52 2022 +0500
all: imp code, docs
commit 2960c6549a7e4944cc0072ca47a0cd4e82ec850e
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date: Sun Mar 6 21:34:58 2022 +0500
all: imp code & docs, fix tests
commit 29c049f3aee91a826c3416961686396d562a7066
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date: Wed Mar 2 20:45:34 2022 +0300
all: add arpdb
This commit is contained in:
230
internal/aghnet/arpdb_linux.go
Normal file
230
internal/aghnet/arpdb_linux.go
Normal file
@@ -0,0 +1,230 @@
|
||||
//go:build linux
|
||||
// +build linux
|
||||
|
||||
package aghnet
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/fs"
|
||||
"net"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/AdguardTeam/AdGuardHome/internal/aghos"
|
||||
"github.com/AdguardTeam/golibs/log"
|
||||
"github.com/AdguardTeam/golibs/netutil"
|
||||
"github.com/AdguardTeam/golibs/stringutil"
|
||||
)
|
||||
|
||||
func newARPDB() (arp *arpdbs) {
|
||||
// Use the common storage among the implementations.
|
||||
ns := &neighs{
|
||||
mu: &sync.RWMutex{},
|
||||
ns: make([]Neighbor, 0),
|
||||
}
|
||||
|
||||
var parseF parseNeighsFunc
|
||||
if aghos.IsOpenWrt() {
|
||||
parseF = parseArpAWrt
|
||||
} else {
|
||||
parseF = parseArpA
|
||||
}
|
||||
|
||||
return newARPDBs(
|
||||
// Try /proc/net/arp first.
|
||||
&fsysARPDB{ns: ns, fsys: aghos.RootDirFS(), filename: "proc/net/arp"},
|
||||
// Try "arp -a" then.
|
||||
&cmdARPDB{parse: parseF, runcmd: rcArpA, ns: ns},
|
||||
// Try "ip neigh" finally.
|
||||
&cmdARPDB{parse: parseIPNeigh, runcmd: rcIPNeigh, ns: ns},
|
||||
)
|
||||
}
|
||||
|
||||
// fsysARPDB accesses the ARP cache file to update the database.
|
||||
type fsysARPDB struct {
|
||||
ns *neighs
|
||||
fsys fs.FS
|
||||
filename string
|
||||
}
|
||||
|
||||
// type check
|
||||
var _ ARPDB = (*fsysARPDB)(nil)
|
||||
|
||||
// Refresh implements the ARPDB interface for *fsysARPDB.
|
||||
func (arp *fsysARPDB) Refresh() (err error) {
|
||||
var f fs.File
|
||||
f, err = arp.fsys.Open(arp.filename)
|
||||
if err != nil {
|
||||
return fmt.Errorf("opening %q: %w", arp.filename, err)
|
||||
}
|
||||
|
||||
sc := bufio.NewScanner(f)
|
||||
// Skip the header.
|
||||
if !sc.Scan() {
|
||||
return nil
|
||||
} else if err = sc.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ns := make([]Neighbor, 0, arp.ns.len())
|
||||
for sc.Scan() {
|
||||
ln := sc.Text()
|
||||
fields := stringutil.SplitTrimmed(ln, " ")
|
||||
if len(fields) != 6 {
|
||||
continue
|
||||
}
|
||||
|
||||
n := Neighbor{}
|
||||
if n.IP = net.ParseIP(fields[0]); n.IP == 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)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Neighbors implements the ARPDB interface for *fsysARPDB.
|
||||
func (arp *fsysARPDB) Neighbors() (ns []Neighbor) {
|
||||
return arp.ns.clone()
|
||||
}
|
||||
|
||||
// parseArpAWrt parses the output of the "arp -a" command on OpenWrt. The
|
||||
// expected input format:
|
||||
//
|
||||
// IP address HW type Flags HW address Mask Device
|
||||
// 192.168.11.98 0x1 0x2 5a:92:df:a9:7e:28 * wan
|
||||
//
|
||||
func parseArpAWrt(sc *bufio.Scanner, lenHint int) (ns []Neighbor) {
|
||||
if !sc.Scan() {
|
||||
// Skip the header.
|
||||
return
|
||||
}
|
||||
|
||||
ns = make([]Neighbor, 0, lenHint)
|
||||
for sc.Scan() {
|
||||
ln := sc.Text()
|
||||
|
||||
fields := strings.Fields(ln)
|
||||
if len(fields) < 4 {
|
||||
continue
|
||||
}
|
||||
|
||||
n := Neighbor{}
|
||||
|
||||
if ip := net.ParseIP(fields[0]); ip == nil || n.IP.IsUnspecified() {
|
||||
continue
|
||||
} else {
|
||||
n.IP = ip
|
||||
}
|
||||
|
||||
hwStr := fields[3]
|
||||
if mac, err := net.ParseMAC(hwStr); err != nil {
|
||||
log.Debug("parsing arp output: %s", err)
|
||||
|
||||
continue
|
||||
} else {
|
||||
n.MAC = mac
|
||||
}
|
||||
|
||||
ns = append(ns, n)
|
||||
}
|
||||
|
||||
return ns
|
||||
}
|
||||
|
||||
// parseArpA parses the output of the "arp -a" command on Linux. The expected
|
||||
// input format:
|
||||
//
|
||||
// hostname (192.168.1.1) at ab:cd:ef:ab:cd:ef [ether] on enp0s3
|
||||
//
|
||||
func parseArpA(sc *bufio.Scanner, lenHint int) (ns []Neighbor) {
|
||||
ns = make([]Neighbor, 0, lenHint)
|
||||
for sc.Scan() {
|
||||
ln := sc.Text()
|
||||
|
||||
fields := strings.Fields(ln)
|
||||
if len(fields) < 4 {
|
||||
continue
|
||||
}
|
||||
|
||||
n := Neighbor{}
|
||||
|
||||
if ipStr := fields[1]; len(ipStr) < 2 {
|
||||
continue
|
||||
} else if ip := net.ParseIP(ipStr[1 : len(ipStr)-1]); ip == nil {
|
||||
continue
|
||||
} else {
|
||||
n.IP = ip
|
||||
}
|
||||
|
||||
hwStr := fields[3]
|
||||
if mac, err := net.ParseMAC(hwStr); err != nil {
|
||||
log.Debug("parsing arp output: %s", err)
|
||||
|
||||
continue
|
||||
} else {
|
||||
n.MAC = mac
|
||||
}
|
||||
|
||||
host := fields[0]
|
||||
if verr := netutil.ValidateDomainName(host); verr != nil {
|
||||
log.Debug("parsing arp output: %s", verr)
|
||||
} else {
|
||||
n.Name = host
|
||||
}
|
||||
|
||||
ns = append(ns, n)
|
||||
}
|
||||
|
||||
return ns
|
||||
}
|
||||
|
||||
// rcIPNeigh runs "ip neigh".
|
||||
func rcIPNeigh() (r io.Reader, err error) {
|
||||
return runCmd("ip", "neigh")
|
||||
}
|
||||
|
||||
// parseIPNeigh parses the output of the "ip neigh" command on Linux. The
|
||||
// expected input format:
|
||||
//
|
||||
// 192.168.1.1 dev enp0s3 lladdr ab:cd:ef:ab:cd:ef REACHABLE
|
||||
//
|
||||
func parseIPNeigh(sc *bufio.Scanner, lenHint int) (ns []Neighbor) {
|
||||
ns = make([]Neighbor, 0, lenHint)
|
||||
for sc.Scan() {
|
||||
ln := sc.Text()
|
||||
|
||||
fields := strings.Fields(ln)
|
||||
if len(fields) < 5 {
|
||||
continue
|
||||
}
|
||||
|
||||
n := Neighbor{}
|
||||
|
||||
if ip := net.ParseIP(fields[0]); ip == nil {
|
||||
continue
|
||||
} else {
|
||||
n.IP = ip
|
||||
}
|
||||
|
||||
if mac, err := net.ParseMAC(fields[4]); err != nil {
|
||||
log.Debug("parsing arp output: %s", err)
|
||||
|
||||
continue
|
||||
} else {
|
||||
n.MAC = mac
|
||||
}
|
||||
|
||||
ns = append(ns, n)
|
||||
}
|
||||
|
||||
return ns
|
||||
}
|
||||
Reference in New Issue
Block a user