mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-11-21 20:45:33 +03:00
20531de760
Merge in DNS/adguard-home from 4884-upd-golibs to master
Updates #4884.
Squashed commit of the following:
commit 4d076021c2e500a75d0b3662643c0c26fd53f6c3
Merge: d780ad00 91dee098
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date: Tue Feb 21 16:47:08 2023 +0300
Merge branch 'master' into 4884-upd-golibs
commit d780ad008b8925a1f499d70f827b79be597c60b5
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date: Tue Feb 21 14:17:11 2023 +0300
dnsforward: imp tests
commit ff9963da35d0220af461cdec66a38134f85ec956
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date: Tue Feb 21 13:50:05 2023 +0300
all: log changes
commit 5703f7a52a364c2d075ed2d862a38587c2650cae
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date: Tue Feb 21 13:36:43 2023 +0300
all: upd golibs and fix breaking changes
81 lines
1.6 KiB
Go
81 lines
1.6 KiB
Go
//go:build darwin || freebsd
|
|
|
|
package aghnet
|
|
|
|
import (
|
|
"bufio"
|
|
"net"
|
|
"net/netip"
|
|
"strings"
|
|
"sync"
|
|
|
|
"github.com/AdguardTeam/golibs/log"
|
|
"github.com/AdguardTeam/golibs/netutil"
|
|
)
|
|
|
|
func newARPDB() (arp *cmdARPDB) {
|
|
return &cmdARPDB{
|
|
parse: parseArpA,
|
|
ns: &neighs{
|
|
mu: &sync.RWMutex{},
|
|
ns: make([]Neighbor, 0),
|
|
},
|
|
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"},
|
|
}
|
|
}
|
|
|
|
// parseArpA parses the output of the "arp -a -n" command on macOS and FreeBSD.
|
|
// The expected input format:
|
|
//
|
|
// host.name (192.168.0.1) at ff:ff:ff:ff:ff:ff on en0 ifscope [ethernet]
|
|
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]
|
|
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]
|
|
err = netutil.ValidateHostname(host)
|
|
if err != nil {
|
|
log.Debug("arpdb: parsing arp output: host: %s", err)
|
|
} else {
|
|
n.Name = host
|
|
}
|
|
|
|
ns = append(ns, n)
|
|
}
|
|
|
|
return ns
|
|
}
|