packets.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import struct
  2. # Init Packets
  3. class ipacket:
  4. # creates an ipacket with routerid
  5. def __init__(this, rid):
  6. this.rid = rid
  7. # packs the packet into a packet (bytes)
  8. def package(this):
  9. return struct.pack("<I", this.rid)
  10. # Hello Packets
  11. class hpacket:
  12. # creates an hpacket with routerid and linkid
  13. def __init__(this, rid, lid):
  14. this.rid = rid
  15. this.lid = lid
  16. # packs the packet into a packet (bytes)
  17. def package(this):
  18. return struct.pack("<II", this.rid, this.lid)
  19. # LSPDU Packets
  20. class lpacket:
  21. # creates an lpacket with senderid, routerid, linkid, link cost, and sender link id
  22. def __init__(this, sid, rid, lid, cost, slid):
  23. this.sid = sid
  24. this.rid = rid
  25. this.lid = lid
  26. this.cost = cost
  27. this.slid = slid
  28. # packs the packet into a packet (bytes)
  29. def package(this):
  30. return struct.pack("<IIIII", this.sid, this.rid, this.lid, this.cost, this.slid)
  31. # Link Class/struct
  32. class link:
  33. # creates a link with id lid, cost cost
  34. def __init__(this, lid, cost):
  35. this.lid = lid
  36. this.cost = cost
  37. # packs the link into bytes
  38. def package(this):
  39. return struct.pack("<II", this.lid, this.cost)
  40. # returns link id
  41. def getlid(this):
  42. return this.lid
  43. # returns link cost
  44. def getcost(this):
  45. return this.cost
  46. # CircuitDB Packets
  47. class cpacket:
  48. # creates a cpacket with # of links nlinks, list of links links
  49. def __init__(this, nlinks, links):
  50. this.nklinks = nlinks
  51. this.links = links
  52. # packs the packet into a packet (bytes)
  53. def package(this):
  54. bytes = struct.pack("<I", this.nlinks)
  55. for i in this.links:
  56. bytes += i.package()
  57. return bytes
  58. # returns the number of links
  59. def getnlinks(this):
  60. return this.nlinks
  61. # returns the list of links
  62. def getlink(this):
  63. return this.links
  64. # unpacks a packet and returns a packet of the corresponding type
  65. def unpacket(bytes):
  66. size = len(bytes)
  67. # init packet
  68. if (size == 4):
  69. stuff = struct.unpack("<I", bytes)
  70. return (0, ipacket(stuff[0]))
  71. # hello packet
  72. elif (size == 8):
  73. stuff = struct.unpack("<II", bytes)
  74. return (1, hpacket(stuff[0], stuff[1]))
  75. # LSPDU Packet
  76. elif (size == 20):
  77. stuff = struct.unpack("<IIIII", bytes)
  78. return (2, lpacket(stuff[0], stuff[1], stuff[2], stuff[3], stuff[4]))
  79. # circuitDB packet
  80. else:
  81. nlinks = struct.unpack("<I", bytes)
  82. links = []
  83. for i in range(0, nlinks):
  84. stuff = struct.unpack("<II", bytes, i * 8 + 4)
  85. links.append(link(stuff[0], stuff[1]))
  86. return (3, cpacket(nlinks, links))