AdGuardHome/internal/aghnet/arpdb_linux.go

252 lines
4.8 KiB
Go
Raw Normal View History

//go:build linux
package aghnet
import (
"bufio"
"fmt"
"io/fs"
"net"
"net/netip"
"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.
Pull request: 2846 cover aghnet vol.3 Merge in DNS/adguard-home from 2846-cover-aghnet-vol.3 to master Updates #2846. Squashed commit of the following: commit cb22987c43c17bbc8d098e65639cc84e2284bc7b Merge: cf995e9d f31ffcc5 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Wed Mar 30 15:01:10 2022 +0300 Merge branch 'master' into 2846-cover-aghnet-vol.3 commit cf995e9dce635f16e10406a61e2ab12f06407f1f Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Mar 28 18:29:50 2022 +0300 aghnet: imp tests commit bc225fe8800633b29216840bc7d5b82d7c2d2bfb Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Mar 28 18:03:06 2022 +0300 aghnet: imp tests commit a82eb6045495b94a2e81ced9a3ef5bfe65788e56 Merge: f8081249 0d562a7b Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Mar 28 17:39:13 2022 +0300 Merge branch 'master' into 2846-cover-aghnet-vol.3 commit f80812490c49f69655d409c6f015b069affa2f19 Merge: edccaa79 3603b1fc Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Mar 28 17:29:20 2022 +0300 Merge branch 'master' into 2846-cover-aghnet-vol.3 commit edccaa79fca061ffeea1985c293eed123b16a09c Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Mar 28 13:53:40 2022 +0300 aghnet: imp tests commit 7c5028c92f0a6680516bda67c73e794182c9b825 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Fri Mar 25 18:01:28 2022 +0300 aghnet: imp code & docs commit 7897c6b13e9be340ae8a71947a8a0bab82c682eb Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Fri Mar 25 17:11:46 2022 +0300 aghnet: imp coverage commit 1eef110af3bf721a0275c695bf27c31815abff04 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Wed Mar 23 21:10:29 2022 +0300 all: return byte slice
2022-03-30 13:11:57 +01:00
&fsysARPDB{
ns: ns,
fsys: rootDirFS,
filename: "proc/net/arp",
},
// Then, try "arp -a -n".
Pull request: 2846 cover aghnet vol.3 Merge in DNS/adguard-home from 2846-cover-aghnet-vol.3 to master Updates #2846. Squashed commit of the following: commit cb22987c43c17bbc8d098e65639cc84e2284bc7b Merge: cf995e9d f31ffcc5 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Wed Mar 30 15:01:10 2022 +0300 Merge branch 'master' into 2846-cover-aghnet-vol.3 commit cf995e9dce635f16e10406a61e2ab12f06407f1f Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Mar 28 18:29:50 2022 +0300 aghnet: imp tests commit bc225fe8800633b29216840bc7d5b82d7c2d2bfb Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Mar 28 18:03:06 2022 +0300 aghnet: imp tests commit a82eb6045495b94a2e81ced9a3ef5bfe65788e56 Merge: f8081249 0d562a7b Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Mar 28 17:39:13 2022 +0300 Merge branch 'master' into 2846-cover-aghnet-vol.3 commit f80812490c49f69655d409c6f015b069affa2f19 Merge: edccaa79 3603b1fc Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Mar 28 17:29:20 2022 +0300 Merge branch 'master' into 2846-cover-aghnet-vol.3 commit edccaa79fca061ffeea1985c293eed123b16a09c Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Mar 28 13:53:40 2022 +0300 aghnet: imp tests commit 7c5028c92f0a6680516bda67c73e794182c9b825 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Fri Mar 25 18:01:28 2022 +0300 aghnet: imp code & docs commit 7897c6b13e9be340ae8a71947a8a0bab82c682eb Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Fri Mar 25 17:11:46 2022 +0300 aghnet: imp coverage commit 1eef110af3bf721a0275c695bf27c31815abff04 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Wed Mar 23 21:10:29 2022 +0300 all: return byte slice
2022-03-30 13:11:57 +01:00
&cmdARPDB{
parse: parseF,
ns: ns,
cmd: "arp",
// Use -n flag to avoid resolving the hostnames of the neighbors.
// By default ARP attempts to resolve the hostnames via DNS. See
// man 8 arp.
//
// See also https://github.com/AdguardTeam/AdGuardHome/issues/3157.
args: []string{"-a", "-n"},
Pull request: 2846 cover aghnet vol.3 Merge in DNS/adguard-home from 2846-cover-aghnet-vol.3 to master Updates #2846. Squashed commit of the following: commit cb22987c43c17bbc8d098e65639cc84e2284bc7b Merge: cf995e9d f31ffcc5 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Wed Mar 30 15:01:10 2022 +0300 Merge branch 'master' into 2846-cover-aghnet-vol.3 commit cf995e9dce635f16e10406a61e2ab12f06407f1f Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Mar 28 18:29:50 2022 +0300 aghnet: imp tests commit bc225fe8800633b29216840bc7d5b82d7c2d2bfb Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Mar 28 18:03:06 2022 +0300 aghnet: imp tests commit a82eb6045495b94a2e81ced9a3ef5bfe65788e56 Merge: f8081249 0d562a7b Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Mar 28 17:39:13 2022 +0300 Merge branch 'master' into 2846-cover-aghnet-vol.3 commit f80812490c49f69655d409c6f015b069affa2f19 Merge: edccaa79 3603b1fc Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Mar 28 17:29:20 2022 +0300 Merge branch 'master' into 2846-cover-aghnet-vol.3 commit edccaa79fca061ffeea1985c293eed123b16a09c Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Mar 28 13:53:40 2022 +0300 aghnet: imp tests commit 7c5028c92f0a6680516bda67c73e794182c9b825 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Fri Mar 25 18:01:28 2022 +0300 aghnet: imp code & docs commit 7897c6b13e9be340ae8a71947a8a0bab82c682eb Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Fri Mar 25 17:11:46 2022 +0300 aghnet: imp coverage commit 1eef110af3bf721a0275c695bf27c31815abff04 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Wed Mar 23 21:10:29 2022 +0300 all: return byte slice
2022-03-30 13:11:57 +01:00
},
// Finally, try "ip neigh".
&cmdARPDB{
parse: parseIPNeigh,
ns: ns,
cmd: "ip",
args: []string{"neigh"},
},
)
}
// 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{}
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)
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 -n" 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{}
ip, err := netip.ParseAddr(fields[0])
if err != nil || n.IP.IsUnspecified() {
log.Debug("arpdb: parsing arp output: ip: %s", err)
continue
} else {
n.IP = ip
}
hwStr := fields[3]
mac, err := net.ParseMAC(hwStr)
if err != nil {
log.Debug("arpdb: parsing arp output: mac: %s", err)
continue
} else {
n.MAC = mac
}
ns = append(ns, n)
}
return ns
}
// parseArpA parses the output of the "arp -a -n" 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, err := netip.ParseAddr(ipStr[1 : len(ipStr)-1]); 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 {
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)
}
return ns
}
// 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{}
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[4])
if err != nil {
log.Debug("arpdb: parsing arp output: mac: %s", err)
continue
} else {
n.MAC = mac
}
ns = append(ns, n)
}
return ns
}