router.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. from packets import *
  2. import socket, sys, os
  3. # save values needed to talk to host emulator
  4. rid = int(sys.argv[1]) # RouterID
  5. haddr = sys.argv[2] # emulator host addr
  6. hport = int(sys.argv[3]) # emulator host port
  7. rport = int(sys.argv[4]) # router's port
  8. # random consts/globals
  9. inf = 65535
  10. # logfile; at end call log.close()
  11. logname = "router" + str(rid) + ".log"
  12. log = open(logname, "a")
  13. # create socket
  14. sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  15. sock.bind(('', rport))
  16. # send init packet to the emulator
  17. init = ipacket(rid)
  18. sock.sendto(init.package(), (haddr, hport))
  19. log.write("sent init packet" + "\n")
  20. # receive circuitDB packet from emulator
  21. pack, addr = sock.recvfrom(4096)
  22. circuit = unpacket(pack)[1]
  23. log.write("received circuit packet" + "\n")
  24. # send shit out to emulator
  25. for i in circuit.getlink():
  26. sock.sendto(hpacket(rid, i.getlid()).package(), (haddr, hport))
  27. log.write("sent hello packet to link" + str(i.getlid()) + "\n")
  28. # CLASS AND FUNCTION DEFINITIONS
  29. # sends out our circuit as lspdus over link
  30. def send_links(link):
  31. for i in circuit.getlink():
  32. # make lspdu with link info, router as
  33. sock.sendto(lpacket(rid, rid, i.getlid(), i.getcost(), link).package(), (haddr, hport))
  34. log.write("sent lspdu packet: " + str(rid) + " " + str(rid) + " " + str(i.getlid()) + " " + str(i.getcost()) + " " + str(link) + "\n")
  35. # notify neighbours of this new lspdu entry
  36. def notify(connection):
  37. for i in neighbours:
  38. # if this is the sender, continue
  39. if (i[0] == connection.sender):
  40. continue
  41. else:
  42. # lookup shortest path to i (the neighbour)
  43. #dlink = graph.lookup(i[0])
  44. # send to neighbour using dlink
  45. sock.sendto(lpacket(rid, connection.src, connection.link, connection.cost, i[0]).package(), (haddr, hport))
  46. log.write("sent lspdu packet: " + str(rid) + " " + str(connection.src) + " " + str(connection.link) + " " + str(connection.cost) + " " + str(i[0]) + "\n")
  47. class connection:
  48. def __init__(this, sender, src, dest, link, cost):
  49. this.sender = sender
  50. this.src = src
  51. this.dest = dest
  52. this.link = link
  53. this.cost = cost
  54. def show(this):
  55. print("Link: " + str(this.sender) + " " + str(this.src) + " " + str(this.link) + " " + str(this.cost))
  56. # infers what this connection's (link's) destination is
  57. def infer(this, db):
  58. # search all known paths for one with this link
  59. for i in range(0, len(db.entries)):
  60. # don't search the list with this as the source, that's pointless
  61. if ((i + 1) == this.src):
  62. continue
  63. # check all entries for one w/ this link
  64. for j in db.entries[i]:
  65. # match! return
  66. if (j.link == this.link):
  67. this.dest = j.src
  68. j.dest = this.src
  69. return True
  70. return False
  71. class db:
  72. def __init__(this, routers):
  73. this.entries = [ [] for i in range(routers) ]
  74. def show(this):
  75. for i in this.entries:
  76. show(i)
  77. def insert(this, connection):
  78. src = connection.src
  79. # check each entry in the source's list within entries
  80. # index is src - 1 since router #s start at 1
  81. for i in this.entries[src - 1]:
  82. # dupe entry
  83. if (connection.link == i.link):
  84. log.write("This was a duplicate packet. Ignoring.")
  85. return False
  86. # not a dupe, insert
  87. this.entries[src - 1].append(connection)
  88. this.show()
  89. # update entries and connection to contain
  90. got_dest = connection.infer(this)
  91. # insert into graph if we have the endpoints for this edge
  92. if (got_dest):
  93. graph.insert(connection)
  94. graph.rebuild()
  95. graph.show()
  96. # notify neighbours
  97. notify(connection)
  98. return True
  99. class graph:
  100. def __init__(this, routers):
  101. # stores shortest paths
  102. this.sssp = [inf] * routers
  103. # stores adjacency list (Graph)
  104. this.alist = [ [-1, -1, -1, -1, -1] for i in range(routers) ]
  105. def lookup(this, dest):
  106. return this.sssp[dest - 1]
  107. def show(this):
  108. print("RIB: ")
  109. for i in range(0, 5):
  110. print("R" + str(rid) + " -> R"+ str(i) + " = " + str(this.sssp[i]))
  111. def insert(this, connection):
  112. src = connection.src
  113. dest = connection.dest
  114. weight = connection.cost
  115. this.alist[src - 1][dest - 1] = weight
  116. this.alist[dest - 1][src - 1] = weight
  117. def rebuild(this):
  118. this.sssp = [inf] * 5
  119. this.sssp[rid - 1] = 0
  120. curnode = rid - 1
  121. unvisited = [1] * 5
  122. while(unvisited):
  123. # iterate on routers adjacent to curnode
  124. for i in range(0, 5):
  125. # don't check curnode
  126. if (i == curnode):
  127. continue
  128. # not an edge/neighbour
  129. if (this.alist[curnode][i] == -1):
  130. continue
  131. # visited already
  132. if (unvisited[i] == 0):
  133. continue
  134. this.sssp[i] = min((this.sssp[curnode] + this.alist[curnode][i]), this.sssp[i])
  135. unvisited[curnode] = 0
  136. # if we visited all, we're done
  137. if (sum(unvisited) == 0):
  138. break
  139. minsofar = inf
  140. mindexsofar = curnode
  141. # select next curnode
  142. for i in range(0, 5):
  143. # visited already
  144. if (unvisited[i] == 0):
  145. continue
  146. minsofar = min(minsofar, this.sssp[i])
  147. mindexsofar = i
  148. # no change, we are disconnected. breka
  149. if (mindexsofar == curnode):
  150. break
  151. # stores neighbours, topology db, and graph
  152. neighbours = []
  153. database = db(5) # change constant here to reflect network size
  154. graph = graph(5) # same as above
  155. # listen for network activity
  156. while (1):
  157. pack, addr = sock.recvfrom(4096)
  158. ptype, pack = unpacket(pack)
  159. # hello packet
  160. if (ptype == 1):
  161. log.write("received hello packet from: " + str(pack.rid) + " with link: " + str(pack.lid) + "\n")
  162. # save hello in hellodb
  163. neighbours.append((pack.rid, pack.lid))
  164. # reply with the links from circuit sent individually as lspdus
  165. send_links(pack.lid)
  166. continue
  167. # lspdu packet
  168. elif (ptype == 2):
  169. log.write("received lspdu packet: " + str(pack.sid) + " " + str(pack.rid) + " " + str(pack.lid) + " " + str(pack.cost) + " " + str(pack.slid) + "\n")
  170. # add router ID (link source), linkid, cost to db (only if not a dupe)
  171. conn = connection(pack.sid, pack.rid, None, pack.lid, pack.cost)
  172. database.insert(conn)
  173. # otherwise,send to all ppl that hello-d me (except who sent it) (but modify sid and slid)
  174. continue
  175. # undefined behaviour
  176. else:
  177. print("Recieved unexpected packet. Dropping.")
  178. continue
  179. print("Unexpected end of program.")