forked from mdlayher/arp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
76 lines (63 loc) · 1.75 KB
/
main.go
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
package main
import (
"bytes"
"flag"
"io"
"log"
"net"
"net/netip"
"github.com/mdlayher/arp"
"github.com/mdlayher/ethernet"
)
var (
// ifaceFlag is used to set a network interface for ARP traffic
ifaceFlag = flag.String("i", "eth0", "network interface to use for ARP traffic")
// ipFlag is used to set an IPv4 address to proxy ARP on behalf of
ipFlag = flag.String("ip", "", "IP address for device to proxy ARP on behalf of")
)
func main() {
flag.Parse()
// Ensure valid interface and IPv4 address
ifi, err := net.InterfaceByName(*ifaceFlag)
if err != nil {
log.Fatal(err)
}
ip, err := netip.ParseAddr(*ipFlag)
if err != nil || !ip.Is4() {
log.Fatalf("invalid IPv4 address: %q", *ipFlag)
}
client, err := arp.Dial(ifi)
if err != nil {
log.Fatalf("couldn't create ARP client: %s", err)
}
// Handle ARP requests bound for designated IPv4 address, using proxy ARP
// to indicate that the address belongs to this machine
for {
pkt, eth, err := client.Read()
if err != nil {
if err == io.EOF {
log.Println("EOF")
break
}
log.Fatalf("error processing ARP requests: %s", err)
}
// Ignore ARP replies
if pkt.Operation != arp.OperationRequest {
continue
}
// Ignore ARP requests which are not broadcast or bound directly for
// this machine
if !bytes.Equal(eth.Destination, ethernet.Broadcast) && !bytes.Equal(eth.Destination, ifi.HardwareAddr) {
continue
}
log.Printf("request: who-has %s? tell %s (%s)", pkt.TargetIP, pkt.SenderIP, pkt.SenderHardwareAddr)
// Ignore ARP requests which do not indicate the target IP
if pkt.TargetIP != ip {
continue
}
log.Printf(" reply: %s is-at %s", ip, ifi.HardwareAddr)
if err := client.Reply(pkt, ifi.HardwareAddr, ip); err != nil {
log.Fatal(err)
}
}
}