-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharp_linux.go
More file actions
100 lines (88 loc) · 2.2 KB
/
Copy patharp_linux.go
File metadata and controls
100 lines (88 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//go:build linux
package gomap
import (
"bufio"
"errors"
"fmt"
"net"
"os"
"strings"
"sync"
)
const arpFile = "/proc/net/arp"
// arpCache stores the most recently loaded ARP table.
var (
arpCache map[string]ARPEntry
arpCacheMu sync.RWMutex
)
// ARPEntry represents a single entry in the system ARP table.
type ARPEntry struct {
IP net.IP
MAC net.HardwareAddr
Device *net.Interface
}
// String returns a human-readable representation of the ARP entry.
func (a ARPEntry) String() string {
return fmt.Sprintf("%s\t%s\t%s", a.IP.String(), a.MAC.String(), a.Device.Name)
}
// LoadARPTable reads and parses the system ARP table from /proc/net/arp.
// This function is only available on Linux.
func LoadARPTable() (map[string]ARPEntry, error) {
f, err := os.Open(arpFile)
if err != nil {
return nil, fmt.Errorf("opening ARP table: %w", err)
}
defer f.Close()
table := make(map[string]ARPEntry)
scanner := bufio.NewScanner(f)
scanner.Split(bufio.ScanLines)
count := 0
for scanner.Scan() {
count++
if count == 1 {
continue // skip header
}
line := scanner.Text()
entry, err := ParseARPEntry(line)
if err != nil {
return nil, fmt.Errorf("parsing ARP entry: %w", err)
}
table[entry.MAC.String()] = entry
}
if err := scanner.Err(); err != nil {
return nil, err
}
arpCacheMu.Lock()
arpCache = table
arpCacheMu.Unlock()
return table, nil
}
// GetARPCache returns the most recently loaded ARP table.
// Call LoadARPTable first to populate the cache.
func GetARPCache() map[string]ARPEntry {
arpCacheMu.RLock()
defer arpCacheMu.RUnlock()
return arpCache
}
// ParseARPEntry parses a single line from /proc/net/arp.
func ParseARPEntry(line string) (ARPEntry, error) {
var a ARPEntry
entries := strings.Fields(line)
if len(entries) != 6 {
return a, errors.New("invalid ARP entry: expected 6 fields")
}
a.IP = net.ParseIP(entries[0])
if a.IP == nil {
return a, fmt.Errorf("invalid IP address: %s", entries[0])
}
var err error
a.MAC, err = net.ParseMAC(entries[3])
if err != nil {
return a, fmt.Errorf("invalid MAC address: %w", err)
}
a.Device, err = net.InterfaceByName(entries[5])
if err != nil {
return a, fmt.Errorf("interface %s: %w", entries[5], err)
}
return a, nil
}