AdGuardHome/internal/arpdb/arpdb_bsd.go

75 lines
1.4 KiB
Go
Raw Normal View History

//go:build darwin || freebsd
2023-09-07 15:13:48 +01:00
package arpdb
import (
"bufio"
"net"
2022-11-02 13:18:02 +00:00
"net/netip"
"strings"
"sync"
"github.com/AdguardTeam/golibs/log"
)
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:
//
2022-09-07 16:03:18 +01:00
// 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
}
2023-09-07 15:13:48 +01:00
ipStr := fields[1]
if len(ipStr) < 2 {
continue
2023-09-07 15:13:48 +01:00
}
ip, err := netip.ParseAddr(ipStr[1 : len(ipStr)-1])
if err != nil {
2022-11-02 13:18:02 +00:00
log.Debug("arpdb: parsing arp output: ip: %s", err)
continue
}
hwStr := fields[3]
2022-11-02 13:18:02 +00:00
mac, err := net.ParseMAC(hwStr)
if err != nil {
log.Debug("arpdb: parsing arp output: mac: %s", err)
continue
}
2023-09-07 15:13:48 +01:00
ns = append(ns, Neighbor{
IP: ip,
MAC: mac,
Name: validatedHostname(fields[0]),
})
}
return ns
}