2022-03-15 17:57:46 +00:00
|
|
|
//go:build windows
|
|
|
|
|
2023-08-24 11:42:17 +01:00
|
|
|
package arpdb
|
2022-03-15 17:57:46 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2024-08-27 18:42:10 +01:00
|
|
|
"log/slog"
|
2022-03-15 17:57:46 +00:00
|
|
|
"strings"
|
|
|
|
"sync"
|
2023-08-24 11:42:17 +01:00
|
|
|
|
2024-08-27 18:42:10 +01:00
|
|
|
"github.com/AdguardTeam/golibs/logutil/slogutil"
|
2022-03-15 17:57:46 +00:00
|
|
|
)
|
|
|
|
|
2024-08-27 18:42:10 +01:00
|
|
|
func newARPDB(logger *slog.Logger) (arp *cmdARPDB) {
|
2022-03-15 17:57:46 +00:00
|
|
|
return &cmdARPDB{
|
2024-08-27 18:42:10 +01:00
|
|
|
logger: logger,
|
|
|
|
parse: parseArpA,
|
2022-03-15 17:57:46 +00:00
|
|
|
ns: &neighs{
|
|
|
|
mu: &sync.RWMutex{},
|
|
|
|
ns: make([]Neighbor, 0),
|
|
|
|
},
|
2022-03-30 13:11:57 +01:00
|
|
|
cmd: "arp",
|
|
|
|
args: []string{"/a"},
|
2022-03-15 17:57:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseArpA parses the output of the "arp /a" command on Windows. The expected
|
|
|
|
// input format (the first line is empty):
|
|
|
|
//
|
2022-08-31 16:57:02 +01: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
|
2024-08-27 18:42:10 +01:00
|
|
|
func parseArpA(logger *slog.Logger, sc *bufio.Scanner, lenHint int) (ns []Neighbor) {
|
2022-03-15 17:57:46 +00:00
|
|
|
ns = make([]Neighbor, 0, lenHint)
|
|
|
|
for sc.Scan() {
|
|
|
|
ln := sc.Text()
|
|
|
|
if ln == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
fields := strings.Fields(ln)
|
|
|
|
if len(fields) != 3 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-08-27 18:42:10 +01:00
|
|
|
n, err := newNeighbor("", fields[0], fields[1])
|
2022-10-25 13:08:12 +01:00
|
|
|
if err != nil {
|
2024-08-27 18:42:10 +01:00
|
|
|
logger.Debug("parsing arp output", "line", ln, slogutil.KeyError, err)
|
2023-08-24 11:42:17 +01:00
|
|
|
|
2022-03-15 17:57:46 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-08-27 18:42:10 +01:00
|
|
|
ns = append(ns, *n)
|
2022-03-15 17:57:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ns
|
|
|
|
}
|