router.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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)
  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. # infers what this connection's (link's) destination is
  55. def infer(this, db):
  56. # search all known paths for one with this link
  57. for i in range(0, len(db.entries)):
  58. # don't search the list with this as the source, that's pointless
  59. if ((i + 1) == this.src):
  60. continue
  61. # check all entries for one w/ this link
  62. for j in db.entries[i]:
  63. # match! return
  64. if (j.link == this.link):
  65. this.dest = j.src
  66. j.dest = this.src
  67. return True
  68. return False
  69. class db:
  70. def __init__(this, routers):
  71. this.entries = [ [] for i in range(routers) ]
  72. def insert(this, connection):
  73. src = connection.src
  74. # check each entry in the source's list within entries
  75. # index is src - 1 since router #s start at 1
  76. for i in this.entries[src - 1]:
  77. # dupe entry
  78. if (connection.link == i.link):
  79. print("this should be a log")
  80. return False
  81. # not a dupe, insert
  82. this.entries[src - 1].append(connection)
  83. # update entries and connection to contain
  84. got_dest = connection.infer(this)
  85. # insert into graph if we have the endpoints for this edge
  86. if (got_dest):
  87. graph.insert(connection)
  88. graph.rebuild()
  89. # notify neighbours
  90. notify(connection)
  91. return True
  92. class graph:
  93. def __init__(this, routers):
  94. # stores shortest paths
  95. this.sssp = [inf] * routers
  96. # stores adjacency list (Graph)
  97. this.alist = [ [-1, -1, -1, -1, -1] for i in range(routers) ]
  98. def lookup(this, dest):
  99. return this.sssp[dest - 1]
  100. def insert(this, connection):
  101. src = connection.src
  102. dest = connection.dest
  103. weight = connection.cost
  104. this.alist[src - 1][dest - 1] = weight
  105. this.alist[dest - 1][src - 1] = weight
  106. def rebuild(this):
  107. this.sssp = [inf] * 5
  108. this.sssp[rid - 1] = 0
  109. curnode = rid - 1
  110. unvisited = [1] * 5
  111. while(unvisited):
  112. # iterate on routers adjacent to curnode
  113. for i in range(0, 5):
  114. # don't check curnode
  115. if (i == curnode):
  116. continue
  117. # not an edge/neighbour
  118. if (this.alist[curnode][i] == -1):
  119. continue
  120. # visited already
  121. if (unvisited[i] == 0):
  122. continue
  123. this.sssp[i] = min((this.sssp[curnode] + this.alist[curnode][i]), this.sssp[i])
  124. unvisited[curnode] = 0
  125. # if we visited all, we're done
  126. if (sum(unvisited) == 0):
  127. break
  128. minsofar = inf
  129. mindexsofar = curnode
  130. # select next curnode
  131. for i in range(0, 5):
  132. # visited already
  133. if (unvisited[i] == 0):
  134. continue
  135. minsofar = min(minsofar, this.sssp[i])
  136. mindexsofar = i
  137. # no change, we are disconnected. breka
  138. if (mindexsofar == curnode):
  139. break
  140. print("SSSP: this.sssp")
  141. # stores neighbours, topology db, and graph
  142. neighbours = []
  143. database = db(5) # change constant here to reflect network size
  144. graph = graph(5) # same as above
  145. # listen for network activity
  146. while (1):
  147. pack, addr = sock.recvfrom(4096)
  148. ptype, pack = unpacket(pack)
  149. # hello packet
  150. if (ptype == 1):
  151. log.write("received hello packet from: " + str(pack.rid) + " with link: " + str(pack.lid) + "\n")
  152. # save hello in hellodb
  153. neighbours.append((pack.rid, pack.lid))
  154. # reply with the links from circuit sent individually as lspdus
  155. send_links(pack.lid)
  156. continue
  157. # lspdu packet
  158. elif (ptype == 2):
  159. log.write("received lspdu packet: " + str(pack.sid) + " " + str(pack.rid) + " " + str(pack.lid) + " " + str(pack.cost) + " " + str(pack.slid) + "\n")
  160. # add router ID (link source), linkid, cost to db (only if not a dupe)
  161. conn = connection(pack.sid, pack.rid, None, pack.lid, pack.cost)
  162. database.insert(conn)
  163. # otherwise,send to all ppl that hello-d me (except who sent it) (but modify sid and slid)
  164. continue
  165. # undefined behaviour
  166. else:
  167. print("Recieved unexpected packet. Dropping.")
  168. continue
  169. print("Unexpected end of program.")