forked from bettercap/bettercap
/
arp.go
42 lines (35 loc) · 1.19 KB
/
arp.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
package packets
import (
"net"
"github.com/google/gopacket/layers"
)
func NewARPTo(from net.IP, from_hw net.HardwareAddr, to net.IP, to_hw net.HardwareAddr, req uint16) (layers.Ethernet, layers.ARP) {
eth := layers.Ethernet{
SrcMAC: from_hw,
DstMAC: to_hw,
EthernetType: layers.EthernetTypeARP,
}
arp := layers.ARP{
AddrType: layers.LinkTypeEthernet,
Protocol: layers.EthernetTypeIPv4,
HwAddressSize: 6,
ProtAddressSize: 4,
Operation: req,
SourceHwAddress: from_hw,
SourceProtAddress: from.To4(),
DstHwAddress: to_hw,
DstProtAddress: to.To4(),
}
return eth, arp
}
func NewARP(from net.IP, from_hw net.HardwareAddr, to net.IP, req uint16) (layers.Ethernet, layers.ARP) {
return NewARPTo(from, from_hw, to, []byte{0, 0, 0, 0, 0, 0}, req)
}
func NewARPRequest(from net.IP, from_hw net.HardwareAddr, to net.IP) (error, []byte) {
eth, arp := NewARP(from, from_hw, to, layers.ARPRequest)
return Serialize(ð, &arp)
}
func NewARPReply(from net.IP, from_hw net.HardwareAddr, to net.IP, to_hw net.HardwareAddr) (error, []byte) {
eth, arp := NewARPTo(from, from_hw, to, to_hw, layers.ARPReply)
return Serialize(ð, &arp)
}