Files
gists/projects/go-tools/goipgrep/internal/resolve/neigh_linux.go
T
tobias 1cbf8afb4a chore: cleanup — untrack binaries, consolidate Go dirs, dedupe tools
- Untrack and delete compiled binaries (tarsum, gosoft.exe, rust uniq/uniq2);
  ignore build outputs (dist/, bin/, *.exe, *.test, .ruff_cache/)
- Merge tools/go/ and projects/go-tools/go/ into projects/go-tools/<name>/
- Fix goipgrep .gitignore: bare 'ipgrep' pattern was ignoring cmd/ipgrep/,
  so the main entrypoint was never tracked; now anchored to /ipgrep
- Archive duplicate implementations to archive/experimental/{rust,go}/
  (uniq, between, tarsum rewrites); canonical versions stay in tools/
- Update README tool catalog to match new layout

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-10 13:42:45 +02:00

76 lines
1.4 KiB
Go

//go:build linux
package resolve
import (
"bufio"
"io"
"os"
"strings"
"git.ktf.ninja/tabledevil/gists/projects/go-tools/go/goipgrep/internal/normalize"
)
type NeighborTable struct {
ByMAC map[string][]string
}
func LoadNeighborTable() (*NeighborTable, error) {
f, err := os.Open("/proc/net/arp")
if err != nil {
return nil, err
}
defer f.Close()
return parseProcNetARP(f), nil
}
func ResolveMACs(macs []string) ([]MACResolution, error) {
tab, err := LoadNeighborTable()
if err != nil {
return nil, err
}
var res []MACResolution
for _, mac := range macs {
res = append(res, MACResolution{MAC: mac, IPs: append([]string(nil), tab.ByMAC[mac]...)})
}
return res, nil
}
type MACResolution struct {
MAC string `json:"mac"`
IPs []string `json:"ips,omitempty"`
}
func parseProcNetARP(r io.Reader) *NeighborTable {
nt := &NeighborTable{ByMAC: make(map[string][]string)}
sc := bufio.NewScanner(r)
first := true
for sc.Scan() {
line := strings.TrimSpace(sc.Text())
if line == "" {
continue
}
if first {
// header
first = false
continue
}
fields := strings.Fields(line)
if len(fields) < 4 {
continue
}
ip := fields[0]
macRaw := fields[3]
mac, ok := normalize.NormalizeMAC(macRaw)
if !ok {
continue
}
// Skip incomplete/placeholder entries.
if mac == "00:00:00:00:00:00" {
continue
}
nt.ByMAC[mac] = append(nt.ByMAC[mac], ip)
}
return nt
}