123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- import struct
-
- # Init Packets
- class ipacket:
- # creates an ipacket with routerid
- def __init__(this, rid):
- this.rid = rid
-
- # packs the packet into a packet (bytes)
- def package(this):
- return struct.pack("<I", this.rid)
- # Hello Packets
- class hpacket:
- # creates an hpacket with routerid and linkid
- def __init__(this, rid, lid):
- this.rid = rid
- this.lid = lid
-
- # packs the packet into a packet (bytes)
- def package(this):
- return struct.pack("<II", this.rid, this.lid)
-
-
- # LSPDU Packets
- class lpacket:
- # creates an lpacket with senderid, routerid, linkid, link cost, and sender link id
- def __init__(this, sid, rid, lid, cost, slid):
- this.sid = sid
- this.rid = rid
- this.lid = lid
- this.cost = cost
- this.slid = slid
-
- # packs the packet into a packet (bytes)
- def package(this):
- return struct.pack("<IIIII", this.sid, this.rid, this.lid, this.cost, this.slid)
-
-
- # Link Class/struct
- class link:
- # creates a link with id lid, cost cost
- def __init__(this, lid, cost):
- this.lid = lid
- this.cost = cost
-
- # packs the link into bytes
- def package(this):
- return struct.pack("<II", this.lid, this.cost)
-
- # returns link id
- def getlid(this):
- return this.lid
-
- # returns link cost
- def getcost(this):
- return this.cost
-
- # CircuitDB Packets
- class cpacket:
- # creates a cpacket with # of links nlinks, list of links links
- def __init__(this, nlinks, links):
- this.nklinks = nlinks
- this.links = links
-
- # packs the packet into a packet (bytes)
- def package(this):
- bytes = struct.pack("<I", this.nlinks)
- for i in this.links:
- bytes += i.package()
-
- return bytes
-
- # returns the number of links
- def getnlinks(this):
- return this.nlinks
-
- # returns the list of links
- def getlink(this):
- return this.links
- # unpacks a packet and returns a packet of the corresponding type
- def unpacket(bytes):
- size = len(bytes)
-
- # init packet
- if (size == 4):
- stuff = struct.unpack("<I", bytes)
- return (0, ipacket(stuff[0]))
- # hello packet
- elif (size == 8):
- stuff = struct.unpack("<II", bytes)
- return (1, hpacket(stuff[0], stuff[1]))
- # LSPDU Packet
- elif (size == 20):
- stuff = struct.unpack("<IIIII", bytes)
- return (2, lpacket(stuff[0], stuff[1], stuff[2], stuff[3], stuff[4]))
- # circuitDB packet
- else:
- nlinks = struct.unpack("<I", bytes)
- links = []
- for i in range(0, nlinks):
- stuff = struct.unpack("<II", bytes, i * 8 + 4)
- links.append(link(stuff[0], stuff[1]))
-
- return (3, cpacket(nlinks, links))
|