mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-11-28 01:42:26 +03:00
b74b92fc27
Merge in DNS/adguard-home from imp-build-tags to master Squashed commit of the following: commit c15793e04c08097835692568a598b8a8d15f57f4 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Tue Sep 13 19:25:20 2022 +0300 home: imp build tags commit 2b9b68e9fe6942422951f50d90c70143a3509401 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Tue Sep 13 19:23:56 2022 +0300 version: imp build tags commit c0ade3d6ae8885c596fc31312360b25fe992d1e4 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Tue Sep 13 19:20:48 2022 +0300 dhcpd: imp build tags commit 0ca2a73b7c3b721400a0cc6383cc9e60f4961f22 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Tue Sep 13 19:17:22 2022 +0300 aghos: imp build tags commit 733a685b24b56153b96d59cb97c174ad322ff841 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Tue Sep 13 19:13:11 2022 +0300 aghnet: imp build tags
239 lines
4.6 KiB
Go
239 lines
4.6 KiB
Go
//go:build linux
|
|
|
|
package aghnet
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"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: rootDirFS,
|
|
filename: "proc/net/arp",
|
|
},
|
|
// Then, try "arp -a -n".
|
|
&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"},
|
|
},
|
|
// 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{}
|
|
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 -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{}
|
|
|
|
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 -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 := 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
|
|
}
|
|
|
|
// 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
|
|
}
|