-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnodeF.py
More file actions
163 lines (125 loc) · 4.9 KB
/
Copy pathnodeF.py
File metadata and controls
163 lines (125 loc) · 4.9 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
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
# This is Node A
import socket
import json
# will load the port no. from JSON file
with open('./ports.json') as f:
data = json.load(f)
# IP and MAC Address
IP_Addr = "176.16.254.1"
MAC_Addr = "45:D4:01:25:00:00"
SUBTNET_MASK = "255.255.0.0"
# ARP CACHE with IP: MAC value
ARP_CACHE = {}
# ROUTING TABLE for F
ROUTING_TABLE = {
'default': {
"Gateway": "176.16.254.254",
"Netmask": "SUBTNET_MASK",
"Device": "Router 2"
},
}
PACKET = {
'sourceIPAddr': IP_Addr,
'sourceMAC': MAC_Addr,
'destinationIP': "",
'nodeName': "Node F",
'type': ""
}
def printRoutingTable():
print("\n\n\tROUTING TABLE........")
print("DESTINATION \tNETMASK \tGATEWAY \tDEVICE")
for keys in ROUTING_TABLE.keys():
if(keys == 'default'):
print(keys + "\t\t"+ROUTING_TABLE[keys]['Netmask']+"\t"+ROUTING_TABLE[keys]['Gateway']+" \t"+ROUTING_TABLE[keys]['Device'])
else:
print(keys + "\t"+ROUTING_TABLE[keys]['Netmask']+"\t"+ROUTING_TABLE[keys]['Gateway']+" \t"+ROUTING_TABLE[keys]['Device'])
print()
def printARPCache():
# print("\n\n\tARP CACHE........")
print("INTERNET ADDRESS \tPHYSICAL ADDRESS \tTYPE")
for keys in ARP_CACHE.keys():
print(keys + "\t\t" +ARP_CACHE[keys]['MAC_Address']+"\t"+ARP_CACHE[keys]['TYPE'])
print()
def displayPACKETS(message):
if(message['type'] == 'ARP_REQUEST'):
print("SOURCE IP ADDRESS: \t{}".format(message['sourceIP']))
print("SOURCE MAC ADDRESS: \t{}".format(message['sourceMAC']))
print("TARGET IP ADDRESS: \t{}".format(message['targetIP']))
print("TARGET MAC ADDRESS: \t{}".format(message['targetMAC']))
elif(message['type'] == 'TCP'):
print("SOURCE IP ADDRESS: \t{}".format(message['sourceIPAddr']))
print("SOURCE MAC ADDRESS: \t{}".format(message['sourceMAC']))
print("TARGET IP ADDRESS: \t{}".format(message['destinationIP']))
print("TARGET MAC ADDRESS: \t{}".format(message['destinatonMAC']))
print("MESSAGE PAYLOAD: \t{}".format(message['message']))
print("MESSAGE TYPE: \t\t{}".format(message['type']))
def handle_message(message, conn):
# check messag Type
if message['type'] == "DISCONNECT":
conn.close()
if message['type'] == "ARP_REQUEST":
print("RECEIVED {} PACKET".format(message['type']))
displayPACKETS(message)
if(message['targetIP'] == IP_Addr):
# will put the MAC ADDRESS and device name
arpReplyFrame = {
"sourceIP": IP_Addr,
"sourceMAC": MAC_Addr,
"targetIP": message['sourceIP'],
"targetMAC": message['sourceMAC'],
"deviceName": "Node F",
"type": "ARP_REPLY",
}
print("\nSince Source IP and it's IP matches Sending:")
print("UPDATE it's ARP CACHE")
ARP_CACHE[message['sourceIP']] = {
"MAC_Address": message["sourceMAC"],
"NODE_NAME": message["deviceName"],
"NODE_SOC": conn,
"TYPE": "dynamic"
}
printARPCache()
else:
# will put the MAC ADDRESS as 00:00:00:00:00:00:00 because the IP is not correct
arpReplyFrame = {
"sourceIP": IP_Addr,
"sourceMAC": "00:00:00:00:00:00",
"targetIP": message['sourceIP'],
"targetMAC": message['sourceMAC'],
"deviceName": "Node F",
"type": "ARP_REPLY",
}
conn.send(str(arpReplyFrame).encode('utf-8'))
if message['type'] == "TCP":
print("RECEIVED A PACKET.....")
displayPACKETS(message)
print()
replyMessage = input("Enter Reply: ")
print()
replyMessageFrame = PACKET
replyMessageFrame['destinatonMAC'] = message['sourceMAC']
replyMessageFrame['type'] = 'TCP'
replyMessageFrame['destinationIP'] = message['sourceIPAddr']
replyMessageFrame['message'] = replyMessage
replyMessageFrame['messageLength'] = len(replyMessage)
conn.send(str(replyMessageFrame).encode('utf-8'))
# Connect to Router 2
nodeF = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ROUTER2_PORT = data['ROUTER_2']['PORT_NO']
SERVER = socket.gethostbyname("localhost")
ROUTER2_ADDR = (SERVER, ROUTER2_PORT)
nodeF.connect(ROUTER2_ADDR)
# will send a connection message to router 2
newConnectionMessage = PACKET
newConnectionMessage['type'] = 'NEW-CONNECTION'
newConnectionMessage['destinationIP'] = '176.16.254.254'
nodeF.send(str(newConnectionMessage).encode('utf-8'))
newConnectionReply = nodeF.recv(2048).decode('utf-8')
print(newConnectionReply)
printRoutingTable()
print()
while True:
packetReceived = nodeF.recv(2048).decode('utf-8')
packetReceived = packetReceived.replace('\'', '"')
packetReceived = json.loads(packetReceived)
handle_message(packetReceived, nodeF)