| 1 | package packets |
| 2 | |
| 3 | import ( |
| 4 | "net" |
| 5 | |
| 6 | "github.com/google/gopacket/layers" |
| 7 | ) |
| 8 | |
| 9 | func NewARPTo(from net.IP, from_hw net.HardwareAddr, to net.IP, to_hw net.HardwareAddr, req uint16) (layers.Ethernet, layers.ARP) { |
| 10 | eth := layers.Ethernet{ |
| 11 | SrcMAC: from_hw, |
| 12 | DstMAC: to_hw, |
| 13 | EthernetType: layers.EthernetTypeARP, |
| 14 | } |
| 15 | arp := layers.ARP{ |
| 16 | AddrType: layers.LinkTypeEthernet, |
| 17 | Protocol: layers.EthernetTypeIPv4, |
| 18 | HwAddressSize: 6, |
| 19 | ProtAddressSize: 4, |
| 20 | Operation: req, |
| 21 | SourceHwAddress: from_hw, |
| 22 | SourceProtAddress: from.To4(), |
| 23 | DstHwAddress: to_hw, |
| 24 | DstProtAddress: to.To4(), |
| 25 | } |
| 26 | |
| 27 | return eth, arp |
| 28 | } |
| 29 | |
| 30 | func NewARP(from net.IP, from_hw net.HardwareAddr, to net.IP, req uint16) (layers.Ethernet, layers.ARP) { |
| 31 | return NewARPTo(from, from_hw, to, []byte{0, 0, 0, 0, 0, 0}, req) |
| 32 | } |
| 33 | |
| 34 | func NewARPRequest(from net.IP, from_hw net.HardwareAddr, to net.IP) (error, []byte) { |
| 35 | eth, arp := NewARP(from, from_hw, to, layers.ARPRequest) |
| 36 | return Serialize(ð, &arp) |
| 37 | } |
| 38 | |
| 39 | func NewARPReply(from net.IP, from_hw net.HardwareAddr, to net.IP, to_hw net.HardwareAddr) (error, []byte) { |
| 40 | eth, arp := NewARPTo(from, from_hw, to, to_hw, layers.ARPReply) |
| 41 | return Serialize(ð, &arp) |
| 42 | } |