-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhw15.py
More file actions
92 lines (76 loc) · 3.14 KB
/
hw15.py
File metadata and controls
92 lines (76 loc) · 3.14 KB
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
class Router:
"""Router emulation class"""
def isValidAddress(self, address):
return True if (len(address.split(".")) == 4) else False
def getNetwork(self, address):
if (len(address.split("/")) > 1):
mask = int(address.split('/')[1])
ip = address.split('/')[0]
return ".".join([ \
str(int(a)&m) \
for a, m in list( \
zip(ip.split("."),
[int((("1"*mask).ljust(32, "0"))[8*x:(8*(x+1))],2) for x in range(4)]))]
) + "/" + str(mask)
return 0
def __init__(self, rountingTable = list(), interfaceTable = dict()):
self.routingTable = rountingTable
self.interfaceTable = interfaceTable
self.interfaceTable["ETH1"] = ""
self.interfaceTable["ETH2"] = ""
self.interfaceTable["ETH3"] = ""
self.interfaceTable["ETH4"] = ""
def showInterfaces(self):
print("======================Interfaces Table=======================")
for interface, device in self.interfaceTable.items():
print("Port {:<8} -> IP Address {}".format(interface, device))
def showRoutes(self):
print("=============================================================")
print("------------------------ROUTING TABLE------------------------")
print("=============================================================")
i = 0
for elem in self.routingTable:
print("#{:<3} Target: {:<18} -> To: {}".format(i, elem["target"], elem["to"]))
i += 1
def connectDevice(self, interface, address):
if (self.interfaceTable.get(interface, -1) != -1):
self.interfaceTable[interface] = address
self.addRoute(address, interface, False)
else:
print("Interface not found!")
def deleteDevice(self, interface):
self.connectDevice(interface, "")
def addRoute(self, tar, to, check = True):
mask = ""
if (check):
exist = False
for elem in self.routingTable:
mask = elem["target"][-3::]
if (self.isValidAddress(elem["target"]) and \
self.getNetwork(elem["target"]) == \
self.getNetwork(to + mask)
):
exist = True
break
mask = elem["to"][-3::]
if (self.isValidAddress(elem["to"]) and \
self.getNetwork(elem["to"]) == \
self.getNetwork(to + mask)
):
exist = True
break
if (not exist): raise Exception("Gateway not found!")
route = dict()
route["target"] = tar
route["to"] = to + mask
self.routingTable.append(route)
def deleteRoute(self, num):
self.routingTable.pop(num)
router = Router()
router.connectDevice("ETH1", "192.168.5.14/24")
router.addRoute("172.16.0.0/16", "192.168.5.1")
router.addRoute("172.24.0.0/16", "172.16.8.1")
router.showInterfaces()
router.showRoutes()
router.deleteRoute(0)
router.showRoutes()