AdGuardHome/internal/arpdb/arpdb_windows.go
Stanislav Chzhen 6fea7099a2 Pull request 1967: AG-24794-imp-arpdb
Squashed commit of the following:

commit 6f6f6cc5d9b9ae04e369e0b789aaab74f234e6a0
Merge: 9aa3ac58c 8fb76701f
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Thu Aug 24 13:29:47 2023 +0300

    Merge branch 'master' into AG-24794-imp-arpdb

commit 9aa3ac58c76fc4b2f950a988d63dfebd0652e507
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Wed Aug 23 16:14:02 2023 +0300

    scripts: gocognit: add arpdb

commit e99b0534be1891de1c13f4010beeedb4459ccd7c
Merge: 84893bc2d 3722c2846
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Wed Aug 23 16:08:25 2023 +0300

    Merge branch 'master' into AG-24794-imp-arpdb

commit 84893bc2d3018c9ee1e411578b33cdb6ba6d3d81
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Wed Aug 23 16:07:43 2023 +0300

    arpdb: add todo

commit ad4b3689b51324521bf47c478c61b6008332b4f5
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Wed Aug 23 14:02:07 2023 +0300

    arpdb: imp code

commit 9cdd17dadbb91ccc3f8e79ba7a21bc365647e089
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Fri Aug 18 19:05:10 2023 +0300

    all: imp arpdb
2023-08-24 13:42:17 +03:00

68 lines
1.2 KiB
Go

//go:build windows
package arpdb
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",
args: []string{"/a"},
}
}
// parseArpA parses the output of the "arp /a" command on Windows. The expected
// input format (the first line is empty):
//
// 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
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
}
ip, err := netip.ParseAddr(fields[0])
if err != nil {
log.Debug("arpdb: parsing arp output: ip: %s", err)
continue
}
mac, err := net.ParseMAC(fields[1])
if err != nil {
log.Debug("arpdb: parsing arp output: mac: %s", err)
continue
}
ns = append(ns, Neighbor{
IP: ip,
MAC: mac,
})
}
return ns
}