router.py 6.2 KB

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