mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-11-24 22:15:45 +03:00
04c8e3b288
Updates #5035. Squashed commit of the following: commit d1c4493ee4e28d05670c20532ebae1aa809d18da Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Tue Oct 25 14:26:52 2022 +0300 aghnet: imp hosts rec equal commit 0a7f40a64a819245fba20d3b481b0fc34e0c60e6 Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Mon Oct 24 18:10:09 2022 +0300 aghnet: move arp and hosts to netip.Addr
76 lines
1.4 KiB
Go
76 lines
1.4 KiB
Go
//go:build openbsd
|
|
|
|
package aghnet
|
|
|
|
import (
|
|
"bufio"
|
|
"net"
|
|
"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 OpenBSD. The
|
|
// expected input format:
|
|
//
|
|
// Host Ethernet Address Netif Expire Flags
|
|
// 192.168.1.1 ab:cd:ef:ab:cd:ef em0 19m59s
|
|
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{}
|
|
|
|
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[1])
|
|
if err != nil {
|
|
log.Debug("arpdb: parsing arp output: mac: %s", err)
|
|
|
|
continue
|
|
} else {
|
|
n.MAC = mac
|
|
}
|
|
|
|
ns = append(ns, n)
|
|
}
|
|
|
|
return ns
|
|
}
|