arp.go 1.2 KB

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