-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
144 lines (117 loc) · 3 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main
import (
"flag"
"log"
"net"
"syscall"
"time"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
)
var (
interfaceNames = StringSliceFlag{}
interval time.Duration
)
func main() {
flag.Var(&interfaceNames, "interface", "interface name for which to send ARP requests, valid multiple times")
flag.DurationVar(&interval, "interval", 15*time.Second, "how often to send a gratuitous ARP request")
flag.Parse()
interfaces := []net.Interface{}
allInterfaces, err := net.Interfaces()
if err != nil {
log.Fatalf("could not get all interfaces")
}
for _, inf := range allInterfaces {
for _, infName := range interfaceNames {
if inf.Name == infName {
if len(inf.HardwareAddr) == 0 {
log.Printf("skipping inf (%s) because it has no MAC address", inf.Name)
continue
}
interfaces = append(interfaces, inf)
}
}
}
if len(interfaces) == 0 {
log.Fatalf("no interfaces for which to send gratuitous ARP requests")
}
log.Printf("using %s as interval", interval)
immediate := time.After(0 * time.Second)
for {
select {
case <-immediate:
case <-time.After(interval):
}
for _, inf := range interfaces {
log.Printf("garp inf (%s)", inf.Name)
err := garp(inf)
if err != nil {
log.Printf("error garp inf (%s): %s", inf.Name, err)
}
}
}
}
func garp(inf net.Interface) error {
const (
ethernetAddressSize uint8 = 6
ipv4AddressSize uint8 = 4
)
var (
sent = 0
)
addrs, err := inf.Addrs()
if err != nil {
return err
}
for _, addr := range addrs {
var ip net.IP
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
}
ip = ip.To4()
if ip == nil {
continue
}
func() {
rawPacketBuf := gopacket.NewSerializeBuffer()
packet := layers.ARP{
AddrType: layers.LinkTypeEthernet,
Protocol: layers.EthernetTypeIPv4,
HwAddressSize: ethernetAddressSize,
ProtAddressSize: ipv4AddressSize,
Operation: layers.ARPRequest,
SourceHwAddress: inf.HardwareAddr,
SourceProtAddress: ip,
DstHwAddress: []byte([]uint8{0, 0, 0, 0, 0, 0}),
DstProtAddress: ip,
}
err := packet.SerializeTo(rawPacketBuf, gopacket.SerializeOptions{FixLengths: true})
if err != nil {
log.Printf("error crafting packet for inf (%s): %s", inf.Name, err)
return
}
fd, err := syscall.Socket(syscall.AF_INET, syscall.SOCK_RAW, syscall.IPPROTO_RAW)
if err != nil {
log.Printf("error opening raw socket for inf (%s): %s", inf.Name, err)
return
}
defer syscall.Close(fd)
addr := syscall.SockaddrInet4{
Port: 0,
Addr: [4]byte{ip[0], ip[1], ip[2], ip[3]},
}
err = syscall.Sendto(fd, rawPacketBuf.Bytes(), 0, &addr)
if err != nil {
log.Printf("error writing packet for inf (%s) for ip (%s): %s", inf.Name, ip, err)
return
}
log.Printf("garp inf (%s) sent ARP request for %s", inf.Name, ip)
sent += 1
}()
}
log.Printf("garp inf (%s) sent %d ARP requests", inf.Name, sent)
return nil
}