arp.py 1.0 KB

123456789101112131415161718192021222324252627282930313233343536
  1. """Address Resolution Protocol."""
  2. from pypacker import pypacker
  3. # Hardware address format
  4. ARP_HRD_ETH = 0x0001 # ethernet hardware
  5. ARP_HRD_IEEE802 = 0x0006 # IEEE 802 hardware
  6. # Protocol address format
  7. ARP_PRO_IP = 0x0800 # IP protocol
  8. # ARP operation
  9. ARP_OP_REQUEST = 1 # request to resolve ha given pa
  10. ARP_OP_REPLY = 2 # response giving hardware address
  11. ARP_OP_REVREQUEST = 3 # request to resolve pa given ha
  12. ARP_OP_REVREPLY = 4 # response giving protocol address
  13. class ARP(pypacker.Packet):
  14. __hdr__ = (
  15. ("hrd", "H", ARP_HRD_ETH),
  16. ("pro", "H", ARP_PRO_IP),
  17. ("hln", "B", 6), # hardware address length
  18. ("pln", "B", 4), # protocol address length
  19. ("op", "H", ARP_OP_REQUEST),
  20. ("sha", "6s", b"\x00" * 6), # sender mac
  21. ("spa", "4s", b"\x00" * 6), # sender ip
  22. ("tha", "6s", b"\x00" * 6), # target mac
  23. ("tpa", "4s", b"\x00" * 6) # target ip
  24. )
  25. # convenient access
  26. sha_s = pypacker.get_property_mac("sha")
  27. spa_s = pypacker.get_property_ip4("spa")
  28. tha_s = pypacker.get_property_mac("tha")
  29. tpa_s = pypacker.get_property_ip4("tpa")