rip.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. """Routing Information Protocol."""
  2. from pypacker import pypacker, triggerlist
  3. import logging
  4. logger = logging.getLogger("pypacker")
  5. # RIP v2 - RFC 2453
  6. # http://tools.ietf.org/html/rfc2453
  7. REQUEST = 1
  8. RESPONSE = 2
  9. class RIP(pypacker.Packet):
  10. __hdr__ = (
  11. ("cmd", "B", REQUEST),
  12. ("v", "B", 2),
  13. ("rsvd", "H", 0),
  14. ("rte_auth", None, triggerlist.TriggerList)
  15. )
  16. def _dissect(self, buf):
  17. self._init_triggerlist("rte_auth", buf[4:], self.__parse_auths)
  18. return len(buf)
  19. def __parse_auths(self, buf):
  20. off = 0
  21. auths = []
  22. while off + 20 <= len(buf):
  23. if buf[off: off + 2] == b"\xff\xff":
  24. auth_rte = Auth(buf[off: off + 20])
  25. else:
  26. auth_rte = RTE(buf[off: off + 20])
  27. # logger.debug("RIP: adding auth/rte: %s" % auth_rte)
  28. auths.append(auth_rte)
  29. off += 20
  30. return auths
  31. class RTE(pypacker.Packet):
  32. __hdr__ = (
  33. ("family", "H", 2),
  34. ("route_tag", "H", 0),
  35. ("addr", "I", 0),
  36. ("subnet", "I", 0),
  37. ("next_hop", "I", 0),
  38. ("metric", "I", 1)
  39. )
  40. class Auth(pypacker.Packet):
  41. __hdr__ = (
  42. ("rsvd", "H", 0xFFFF),
  43. ("type", "H", 2),
  44. ("auth", "16s", b"\x00" * 16)
  45. )