2022-03-15 20:57:46 +03:00
|
|
|
//go:build windows
|
|
|
|
|
2023-08-24 13:42:17 +03:00
|
|
|
package arpdb
|
2022-03-15 20:57:46 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"net"
|
2022-10-25 15:08:12 +03:00
|
|
|
"net/netip"
|
2022-03-15 20:57:46 +03:00
|
|
|
"strings"
|
|
|
|
"sync"
|
2023-08-24 13:42:17 +03:00
|
|
|
|
|
|
|
"github.com/AdguardTeam/golibs/log"
|
2022-03-15 20:57:46 +03:00
|
|
|
)
|
|
|
|
|
2022-04-19 15:01:49 +03:00
|
|
|
func newARPDB() (arp *cmdARPDB) {
|
2022-03-15 20:57:46 +03:00
|
|
|
return &cmdARPDB{
|
2022-03-30 15:11:57 +03:00
|
|
|
parse: parseArpA,
|
2022-03-15 20:57:46 +03:00
|
|
|
ns: &neighs{
|
|
|
|
mu: &sync.RWMutex{},
|
|
|
|
ns: make([]Neighbor, 0),
|
|
|
|
},
|
2022-03-30 15:11:57 +03:00
|
|
|
cmd: "arp",
|
|
|
|
args: []string{"/a"},
|
2022-03-15 20:57:46 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseArpA parses the output of the "arp /a" command on Windows. The expected
|
|
|
|
// input format (the first line is empty):
|
|
|
|
//
|
2022-08-31 18:57:02 +03:00
|
|
|
// Interface: 192.168.56.16 --- 0x7
|
|
|
|
// Internet Address Physical Address Type
|
|
|
|
// 192.168.56.1 0a-00-27-00-00-00 dynamic
|
|
|
|
// 192.168.56.255 ff-ff-ff-ff-ff-ff static
|
2022-03-15 20:57:46 +03:00
|
|
|
func parseArpA(sc *bufio.Scanner, lenHint int) (ns []Neighbor) {
|
|
|
|
ns = make([]Neighbor, 0, lenHint)
|
|
|
|
for sc.Scan() {
|
|
|
|
ln := sc.Text()
|
|
|
|
if ln == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
fields := strings.Fields(ln)
|
|
|
|
if len(fields) != 3 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-10-25 15:08:12 +03:00
|
|
|
ip, err := netip.ParseAddr(fields[0])
|
|
|
|
if err != nil {
|
2023-08-24 13:42:17 +03:00
|
|
|
log.Debug("arpdb: parsing arp output: ip: %s", err)
|
|
|
|
|
2022-03-15 20:57:46 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-10-25 15:08:12 +03:00
|
|
|
mac, err := net.ParseMAC(fields[1])
|
|
|
|
if err != nil {
|
2023-08-24 13:42:17 +03:00
|
|
|
log.Debug("arpdb: parsing arp output: mac: %s", err)
|
|
|
|
|
2022-03-15 20:57:46 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-08-24 13:42:17 +03:00
|
|
|
ns = append(ns, Neighbor{
|
|
|
|
IP: ip,
|
|
|
|
MAC: mac,
|
|
|
|
})
|
2022-03-15 20:57:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return ns
|
|
|
|
}
|