2022-03-15 17:57:46 +00:00
|
|
|
//go:build openbsd
|
|
|
|
|
2023-08-24 11:42:17 +01:00
|
|
|
package arpdb
|
2022-03-15 17:57:46 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"net"
|
2022-10-25 13:08:12 +01:00
|
|
|
"net/netip"
|
2022-03-15 17:57:46 +00:00
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/AdguardTeam/golibs/log"
|
|
|
|
)
|
|
|
|
|
2022-04-19 13:01:49 +01:00
|
|
|
func newARPDB() (arp *cmdARPDB) {
|
2022-03-15 17:57:46 +00:00
|
|
|
return &cmdARPDB{
|
2022-03-30 13:11:57 +01:00
|
|
|
parse: parseArpA,
|
2022-03-15 17:57:46 +00:00
|
|
|
ns: &neighs{
|
|
|
|
mu: &sync.RWMutex{},
|
|
|
|
ns: make([]Neighbor, 0),
|
|
|
|
},
|
2022-04-19 13:01:49 +01:00
|
|
|
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"},
|
2022-03-15 17:57:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-19 13:01:49 +01:00
|
|
|
// parseArpA parses the output of the "arp -a -n" command on OpenBSD. The
|
|
|
|
// expected input format:
|
2022-03-15 17:57:46 +00:00
|
|
|
//
|
2022-08-31 16:57:02 +01:00
|
|
|
// Host Ethernet Address Netif Expire Flags
|
|
|
|
// 192.168.1.1 ab:cd:ef:ab:cd:ef em0 19m59s
|
2022-03-15 17:57:46 +00:00
|
|
|
func parseArpA(sc *bufio.Scanner, lenHint int) (ns []Neighbor) {
|
|
|
|
// Skip the header.
|
|
|
|
if !sc.Scan() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ns = make([]Neighbor, 0, lenHint)
|
|
|
|
for sc.Scan() {
|
|
|
|
ln := sc.Text()
|
|
|
|
|
|
|
|
fields := strings.Fields(ln)
|
|
|
|
if len(fields) < 2 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
n := Neighbor{}
|
|
|
|
|
2022-10-25 13:08:12 +01:00
|
|
|
ip, err := netip.ParseAddr(fields[0])
|
|
|
|
if err != nil {
|
|
|
|
log.Debug("arpdb: parsing arp output: ip: %s", err)
|
|
|
|
|
2022-03-15 17:57:46 +00:00
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
n.IP = ip
|
|
|
|
}
|
|
|
|
|
2022-10-25 13:08:12 +01:00
|
|
|
mac, err := net.ParseMAC(fields[1])
|
|
|
|
if err != nil {
|
|
|
|
log.Debug("arpdb: parsing arp output: mac: %s", err)
|
2022-03-15 17:57:46 +00:00
|
|
|
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
n.MAC = mac
|
|
|
|
}
|
|
|
|
|
|
|
|
ns = append(ns, n)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ns
|
|
|
|
}
|