-
Notifications
You must be signed in to change notification settings - Fork 8
/
PoisonReverse.py
194 lines (166 loc) · 7.36 KB
/
PoisonReverse.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#//==================================
#// Computer Networks & Applications
#// Student: Vandit Jyotindra Gajjar
#// Student ID: a1779153
#// Semester: 1
#// Year: 2020
#// Assignment: 3
#//===================================
#Importing utilities copy and sys
import copy
import sys
#Defining function min_column accepts two parameters: table and column_no
#This function returns best cost path.
def min_column(table, column_no):
value_list = []
for i in range(0,len(table)):
if table[i][column_no] != None:
value_list.append(table[i][column_no])
if(len(value_list) == 0):
return None
else:
return min(value_list)
#Defining route_through function accepts four parameters: value, table, column_no, and node_no
#This function adds value to the routing table.
def route_through(value, table, column_no, node_no):
for i in range(0, len(table)):
if(node_no != i):
if table[i][column_no] == value:
return i
#Defining initialize_tables function accepts four parameters: nodes, routing_table_node, links, and count
#This function initialize the table based on nodes and edge costs.
def initialize_tables(nodes, routing_table_node, links, count):
for i in range(0,len(routing_table_node)):
for j in links:
j = j.split()
if(i == nodes[j[0]]):
routing_table_node[i][i][nodes[j[1]]] = int(j[2])
print("t="+str(count)+" distance from "+ nodes[i] +" to "+j[1]+" via "+j[1]+" is "+str(j[2]))
if(i == nodes[j[1]]):
routing_table_node[i][i][nodes[j[0]]] = int(j[2])
print("t="+str(count)+" distance from "+ nodes[i] +" to "+j[0]+" via "+j[0]+" is "+str(j[2]))
nodes[str(j[0])+str(j[1])]= int(j[2])
nodes[str(j[1])+str(j[0])]= int(j[2])
routing_table_node[i][i][i] = 0
count += 1
return routing_table_node, count, nodes
#Defining findinList accepts three parameters: table, index, and value
#This function returns the index of the next hop for any node to reach the destination.
def findinList(table, index, value):
for i in range(0,len(table)):
if table[i][index]!=None:
if table[i][index]==value:
break
return i
#Defining update_table accepts three parameters: nodes, routing_table_node, and count
#This function update the routing table till convergence is achieved based on the algorithm.
def update_table(nodes, routing_table_node, count):
print(" ")
change = 0
old_table = copy.deepcopy(routing_table_node)
for i in range(0, len(routing_table_node)):
for j in range(0, len(routing_table_node[i])):
if(routing_table_node[i][i][j] != None): #check neighbour
for k in range(0, len(routing_table_node[i][j])):
if((min_column(routing_table_node[j],j) != None) and (min_column(old_table[j],k) != None)):
if(k != i and i!=j
and routing_table_node[i][j][k] !=
min_column(old_table[j],k) + nodes[nodes[i]+nodes[j]]):
routing_table_node[i][j][k] = min_column(old_table[j],k) + nodes[nodes[i]+nodes[j]]#min_column(old_table[i],j)
if(min_column(old_table[i],j) != min_column(old_table[j],k) + nodes[nodes[i]+nodes[j]] and j != k):
if(findinList(old_table[j],k,min_column(old_table[j],k))==i):
if(min_column(old_table[i],j) != min_column(old_table[j],k) + nodes[nodes[i]+nodes[j]] and j != k):
print("t="+str(count)+" distance from "+ nodes[i] +" to "+nodes[k]+" via "+nodes[j]+" is INF")
continue;
print("t="+str(count)+" distance from "+ nodes[i] +" to "+nodes[k]+" via "+nodes[j]+" is "+str(routing_table_node[i][j][k]))
change = 1
if(k == i):
routing_table_node[i][j][k] = 0
return change, routing_table_node
#Defining print_routing_routes accepts two parameters: nodes and routing_table_node
#This function prints best path for each router based on the routing table.
def print_routing_routes(nodes, routing_table_node):
for i in range(0, len(routing_table_node)):
for j in range(0, len(routing_table_node[i])):
if(i!=j):
print("router "+nodes[i]+": "+nodes[j]+" is "+str(min_column(routing_table_node[i], j))+" routing through "+ nodes[route_through(min_column(routing_table_node[i],j), routing_table_node[i], j, i)])
print(" ")
#Defining poison_reverse accepts two parameters: nodes and routing_table_node
#This function resets the weight in the routing table.
def poison_reverse(nodes, routing_table_node):
for i in range(0, len(routing_table_node)):
for k in range(0, len(routing_table_node[i][i])):
if(routing_table_node[i][i][k] != min_column(routing_table_node[i], k)):
routing_table_node[i][i][k] = None
return routing_table_node
#Defining reinitialize_tables accepts four parameters: nodes, routing_table_node, links, and count
#This function reinitialize the routing table based on changed link weights.
def reinitialize_tables(nodes, routing_table_node, links, count):
for i in range(0,len(routing_table_node)):
for j in links:
j = j.split()
if(len(j) != 3):
continue
if(i == nodes[j[0]]):
routing_table_node[i][i][nodes[j[1]]] = int(j[2])
print("t="+str(count)+" distance from "+ nodes[i] +" to "+j[1]+" via "+j[1]+" is "+str(j[2]))
if(i == nodes[j[1]]):
routing_table_node[i][i][nodes[j[0]]] = int(j[2])
print("t="+str(count)+" distance from "+ nodes[i] +" to "+j[0]+" via "+j[0]+" is "+str(j[2]))
nodes[str(j[0])+str(j[1])]= int(j[2])
nodes[str(j[1])+str(j[0])]= int(j[2])
count += 1
return routing_table_node, count, nodes
#Defining changed_configuration accepts three parameters: nodes, routing_table_node, and changed_config
#This function accepts changeConfigL file and update the routing table based on the changed link weight.
def changed_configuration(nodes, routing_table_node, changed_config):
count = 0
routing_table_node, count, nodes = reinitialize_tables(nodes, routing_table_node, changed_config, count)
change = 1
while(change == 1):
change, routing_table_node = update_table(nodes, routing_table_node, count)
count += 1
return routing_table_node
#This part of the code reads the configL file.
configL = open(str(sys.argv[1]), "r")
configuration = configL.read().split('\n')
configL.close()
#This part of the code reads the changeConfigL file.
changeConfigL = open(str(sys.argv[2]), "r")
changed_config = changeConfigL.read().split('\n')
changeConfigL.close()
next_line_no = 0
no_of_nodes = int(configuration[next_line_no])
next_line_no += 1
nodes = {}
for i in range(0, no_of_nodes):
nodes[configuration[next_line_no+i]] = i
nodes[i] = configuration[next_line_no+i]
print("\n#START\n")
next_line_no += no_of_nodes
no_of_links = int(configuration[next_line_no])
next_line_no += 1
routing_table_node = []
for i in range(0,no_of_nodes):
routing_table_node.append([])
for j in range(0,no_of_nodes):
routing_table_node[i].append([])
for k in range(0,no_of_nodes):
routing_table_node[i][j].append(None)
links = []
for i in range(0, no_of_links):
links.append(configuration[next_line_no+i])
count = 0
routing_table_node, count, nodes = initialize_tables(nodes, routing_table_node, links, count)
change = 1
while(change == 1):
change, routing_table_node = update_table(nodes, routing_table_node, count)
count += 1
routing_table_node = poison_reverse(nodes, routing_table_node)
print("\n#INITIAL \n")
print_routing_routes(nodes, routing_table_node)
print("\n#UPDATE\n")
routing_table_node = changed_configuration(nodes, routing_table_node, changed_config[1:len(changed_config)])
routing_table_node = poison_reverse(nodes, routing_table_node)
print("\n#FINAL\n")
print_routing_routes(nodes, routing_table_node)