-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnodeD.py
More file actions
185 lines (141 loc) · 5.56 KB
/
Copy pathnodeD.py
File metadata and controls
185 lines (141 loc) · 5.56 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# This is Node D
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 = "192.168.1.4"
MAC_Addr = "00:00:A1:05:0D:21"
SUBTNET_MASK = "255.255.255.128"
SERVER = socket.gethostbyname("localhost")
print("\n\t\tNODE D")
print("IP ADDRESS: {}\nMAC ADDRESS: {}\nSUBNET MASK: {}\n\n".format(IP_Addr, MAC_Addr, SUBTNET_MASK))
# ARP CACHE with IP: MAC value
ARP_CACHE = {
}
# ROUTING TABLE for D
ROUTING_TABLE = {
'default': {
"Gateway": "192.168.1.254",
"Netmask": "255.255.255.0",
"Device": "Router 1"
},
}
PACKET = {
'sourceIPAddr': IP_Addr,
'sourceMAC': MAC_Addr,
'destinationIP': "",
'nodeName': "Node D",
'type': ""
}
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 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 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']))
print()
def handle_message(message, conn):
print("")
# 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 B",
"type": "ARP_REQUEST",
}
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 B",
"type": "ARP_REPLY",
}
conn.send(str(arpReplyFrame).encode('utf-8'))
if message['type'] == "TCP":
print("RECEIVED A PACKET.....")
displayPACKETS(message)
replyMessage = input("Enter Message to send: ")
replyMessageFrame = PACKET
replyMessageFrame['destinatonMAC'] = message['sourceMAC']
replyMessageFrame['type'] = 'TCP'
replyMessageFrame['destinationIP'] = message['sourceIPAddr']
replyMessageFrame['message'] = replyMessage
replyMessageFrame['messageLength'] = len(replyMessage)
print("\n\t\tCURRENT ARP CACHE")
printARPCache()
print("\nSENDING A PACKET.....")
displayPACKETS(replyMessageFrame)
conn.send(str(replyMessageFrame).encode('utf-8'))
if message['type'] == 'ECHO_REQUEST':
print("RECEIVED ICMP - ECHO REQUEST")
pingReply = PACKET
pingReply['type'] = 'ECHO_REPLY'
pingReply['startTime'] = message['startTime']
pingReply['icmp_seq'] = message['icmp_seq']
conn.send(str(pingReply).encode('utf-8'))
# Connect to NODE A
nodeD = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
NODE_A_PORT = data['NODE_A']['PORT_NO']
NODEA_ADDR = (SERVER, NODE_A_PORT)
nodeD.connect(NODEA_ADDR)
# will send a connection message to node A
newConnectionMessage = PACKET
newConnectionMessage['type'] = 'NEW-CONNECTION'
newConnectionMessage['destinationIP'] = '192.168.1.1'
newConnectionMessage['Netmask'] = SUBTNET_MASK
nodeD.send(str(newConnectionMessage).encode('utf-8'))
newConnectionReply = nodeD.recv(2048).decode('utf-8')
print(newConnectionReply)
printRoutingTable()
print(nodeD.recv(2048).decode('utf-8'))
print()
while True:
packetReceived = nodeD.recv(2048).decode('utf-8')
packetReceived = packetReceived.replace('\'', '"')
packetReceived = json.loads(packetReceived)
handle_message(packetReceived, nodeD)