-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNetworkApplications.py
358 lines (314 loc) · 14.4 KB
/
NetworkApplications.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
import argparse
import socket
import os
import sys
import struct
import time
import select
import random
def setupArgumentParser() -> argparse.Namespace:
parser = argparse.ArgumentParser(
description='A collection of Network Applications developed for SCC.203.')
parser.set_defaults(func=ICMPPing, hostname='lancaster.ac.uk')
subparsers = parser.add_subparsers(help='sub-command help')
parser_p = subparsers.add_parser('ping', aliases=['p'], help='run ping')
parser_p.set_defaults(timeout=4)
parser_p.add_argument('hostname', type=str, help='host to ping towards')
parser_p.add_argument('--count', '-c', nargs='?', type=int,
help='number of times to ping the host before stopping')
parser_p.add_argument('--timeout', '-t', nargs='?',
type=int,
help='maximum timeout before considering request lost')
parser_p.set_defaults(func=ICMPPing)
parser_t = subparsers.add_parser('traceroute', aliases=['t'],
help='run traceroute')
parser_t.set_defaults(timeout=4, protocol='icmp')
parser_t.add_argument('hostname', type=str, help='host to traceroute towards')
parser_t.add_argument('--timeout', '-t', nargs='?', type=int,
help='maximum timeout before considering request lost')
parser_t.add_argument('--protocol', '-p', nargs='?', type=str,
help='protocol to send request with (UDP/ICMP)')
parser_t.set_defaults(func=Traceroute)
parser_w = subparsers.add_parser('web', aliases=['w'], help='run web server')
parser_w.set_defaults(port=8080)
parser_w.add_argument('--port', '-p', type=int, nargs='?',
help='port number to start web server listening on')
parser_w.set_defaults(func=WebServer)
parser_x = subparsers.add_parser('proxy', aliases=['x'], help='run proxy')
parser_x.set_defaults(port=8000)
parser_x.add_argument('--port', '-p', type=int, nargs='?',
help='port number to start web server listening on')
parser_x.set_defaults(func=Proxy)
args = parser.parse_args()
return args
class NetworkApplication:
def checksum(self, dataToChecksum: str) -> str:
csum = 0
countTo = (len(dataToChecksum) // 2) * 2
count = 0
while count < countTo:
thisVal = dataToChecksum[count+1] * 256 + dataToChecksum[count]
csum = csum + thisVal
csum = csum & 0xffffffff
count = count + 2
if countTo < len(dataToChecksum):
csum = csum + dataToChecksum[len(dataToChecksum) - 1]
csum = csum & 0xffffffff
csum = (csum >> 16) + (csum & 0xffff)
csum = csum + (csum >> 16)
answer = ~csum
answer = answer & 0xffff
answer = answer >> 8 | (answer << 8 & 0xff00)
answer = socket.htons(answer)
return answer
def printOneResult(self, destinationAddress: str, packetLength: int, time: float, ttl: int, destinationHostname=''):
if destinationHostname:
print("%d bytes from %s (%s): ttl=%d time=%.2f ms" % (packetLength, destinationHostname, destinationAddress, ttl, time))
else:
print("%d bytes from %s: ttl=%d time=%.2f ms" % (packetLength, destinationAddress, ttl, time))
def printAdditionalDetails(self, packetLoss=0.0, minimumDelay=0.0, averageDelay=0.0, maximumDelay=0.0):
print("%.2f%% packet loss" % (packetLoss))
if minimumDelay > 0 and averageDelay > 0 and maximumDelay > 0:
print("rtt min/avg/max = %.2f/%.2f/%.2f ms" % (minimumDelay, averageDelay, maximumDelay))
class ICMPPing(NetworkApplication):
packetLength = 0
def receiveOnePing(self, icmpSocket, destinationAddress, ID, timeout):
# 1. Wait for the socket to receive a reply
while True:
ready = select.select([icmpSocket],[], [], timeout)
if ready[0] == []:
print("not ready")
return
packet, address = icmpSocket.recvfrom(1024)
print(packet)
print(address)
self.packetLength = len(packet)
icmpHeader = packet[20:28]
type, code, checksum, packetID, sequence = struct.unpack('bbHHh', icmpHeader)
print("type:")
print(type)
print("code")
print(code)
print("checksum:")
print(checksum)
print("id:")
print(id)
print("sequence")
print(sequence)
break
# 2. Once received, record time of receipt, otherwise, handle a timeout
if(packetID == ID):
print("id is the same")
else:
print("its fucked lmao")
# 3. Compare the time of receipt to time of sending, producing the total network delay
# 4. Unpack the packet header for useful information, including the ID
# 5. Check that the ID matches between the request and reply
# 6. Return total network delay
return time.time()
def sendOnePing(self, icmpSocket, destinationAddress, ID):
# 1. Build ICMP header
header = struct.pack('bbHHh', 8, 0, 0, ID, 1)
data = bytes('hi', 'ascii')
# 2. Checksum ICMP packet using given function
checksum = super().checksum(header + data)
# 3. Insert checksum into packet
header = struct.pack('bbHHh', 8, 0, checksum, ID, 1)
print(header)
# 4. Send packet using socket
dataToSend = header + data
icmpSocket.sendto(dataToSend,(destinationAddress, 1))
# 5. Record time of sending
return time.time()
def doOnePing(self, destinationAddress, timeout):
# 1. Create ICMP socket
icmpSocket = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_ICMP)
ID = int(random.random() * 65535)
# 2. Call sendOnePing function
timesent = self.sendOnePing(icmpSocket, destinationAddress, ID)
# 3. Call receiveOnePing function
timerecieved = self.receiveOnePing(icmpSocket, destinationAddress, ID, args.timeout)
# 4. Close ICMP socket
icmpSocket.close()
# 5. Return total network delay
print(timesent)
print(timerecieved)
return timerecieved - timesent
def __init__(self, args):
print('Ping to: %s...' % (args.hostname))
# 1. Look up hostname, resolving it to an IP address
destinationAddress = socket.gethostbyname(args.hostname)
print(destinationAddress)
if(args.count == None):
args.count = 1
for x in range(args.count):
# 2. Call doOnePing function, approxim
# ately every second
totalNetworkDelay = self.doOnePing(destinationAddress, args.timeout)
time.sleep(1)
# 3. Print out the returned delay (and other relevant details) using the printOneResult method
self.printOneResult(destinationAddress, self.packetLength, totalNetworkDelay*1000, 150) # Example use of printOneResult - complete as appropriate
# 4. Continue this process until stopped
class Traceroute(NetworkApplication):
flag = False #flag is true if echo reply message received
packetLength = 0
address = 0
hostname = None
def receiveOneRoute(self, icmpSocket, destinationAddress, ID, timeout):
while True:
ready = select.select([icmpSocket],[], [], timeout)
if ready[0] == []:
##print("Error")
return -1
packet, address = icmpSocket.recvfrom(1024)
self.packetLength = len(packet)
self.address = address
try:
self.hostname = socket.gethostbyaddr(address[0])
except socket.error:
address = "Hostname unresolved"
icmpHeader = packet[20:28]
type, code, checksum, packetID, sequence = struct.unpack('bbHHh', icmpHeader)
print(type)
if(type == 0):
self.flag = True
break
return time.time()
def sendOneRoute(self, icmpSocket, destinationAddress, ID):
# 1. Build ICMP header
header = struct.pack('bbHHh', 8, 0, 0, ID, 1)
data = bytes('hi', 'ascii')
# 2. Checksum ICMP packet using given function
checksum = super().checksum(header + data)
# 3. Insert checksum into packet
header = struct.pack('bbHHh', 8, 0, checksum, ID, 1)
# 4. Send packet using socket
dataToSend = header + data
icmpSocket.sendto(dataToSend,(destinationAddress, 1))
# 5. Record time of sending
return time.time()
def doOneRoute(self, destinationAddress, timeout, ttl):
# 1. Create ICMP socket
icmpSocket = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_ICMP)
icmpSocket.setsockopt(socket.SOL_IP, socket.IP_TTL, ttl)
ID = int(random.random() * 65535)
timesent = self.sendOneRoute(icmpSocket, destinationAddress, ID)
timerecieved = self.receiveOneRoute(icmpSocket, destinationAddress, ID, timeout)
if timerecieved == -1:
return -1
icmpSocket.close
return timerecieved - timesent
def __init__(self, args):
# Please ensure you print each result using the printOneResult method!
print('Traceroute to: %s...' % (args.hostname))
destinationAddress = socket.gethostbyname(args.hostname)
for ttl in range(1,255):
time = self.doOneRoute(destinationAddress, args.timeout, ttl)
if time == -1:
print("Packet lost with ttl", ttl)
else:
self.printOneResult(self.address[0], self.packetLength, time * 1000, ttl, self.hostname[0])
if self.flag == True:
break
class WebServer(NetworkApplication):
def handleRequest(self, tcpSocket):
print("we here")
error404 = "HTTP/1.1 404 NOT FOUND\n\nError 404\n"
# 1. Receive request message from the client on connection socket
request = tcpSocket.recv(1024)
print(request)
split = request.split()
print(split[0])
print(split[1])
# 2. Extract the path of the requested object from the message (second part of the HTTP header)
# 3. Read the corresponding file from disk
if(split[0] == b'GET' and split[1] == b'/index.html'):
print("tru1")
# 4. Store in temporary buffer
file = open("index.html", "r")
# 5. Send the correct HTTP response error
tcpSocket.sendall(b'HTTP/1.1 200 OK\n')
tcpSocket.sendall(b'Content-Type: text/html\n')
tcpSocket.send(b'\r\n')
# 6. Send the content of the file to the socket
for line in file.readlines():
tcpSocket.sendall(line.encode())
file.close()
else:
print("error 404")
tcpSocket.sendall(b'HTTP/1.1 404 NOT FOUND\n')
tcpSocket.sendall(b'Content-Type: text/html\n')
tcpSocket.send(b'\r\n')
tcpSocket.send(b"""
<html>
<body>
<h1>Error 404</h1>
</body>
</html>
""")
# 7. Close the connection socket
tcpSocket.close()
return
def __init__(self, args):
print('Web Server starting on port: %i...' % (args.port))
# 1. Create server socket
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) #makes sure address is reusable
# 2. Bind the server socket to server address and server port
server.bind(('127.0.0.1', args.port))
# 3. Continuously listen for connections to server socket
server.listen(5)
# 4. When a connection is accepted, call handleRequest function, passing new connection socket (see https://docs.python.org/3/library/socket.html#socket.socket.accept)
try:
while True:
(clientsocket, address) = server.accept()
print(clientsocket)
print(address)
self.handleRequest(clientsocket)
except KeyboardInterrupt:
# 5. Close server socket if keyboard interrupt
server.shutdown(socket.SHUT_RDWR)
server.close()
class Proxy(NetworkApplication):
def handleRequest(self, tcpSocket):
# 1. Receive request message from the client on connection socket
request = tcpSocket.recv(1024)
split = request.split()
url = split[1]
url = str(url)
httpPos = url.find("://")
webserver = url[httpPos+3:len(url)-2]
port = 80 #default port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
##print(webserver)
s.connect((webserver, port))
s.sendall(request)
data = s.recv(1024)
##print(data)
tcpSocket.send(data) #send data back
tcpSocket.close()
return
def __init__(self, args):
print('Web Proxy starting on port: %i...' % (args.port))
# 1. Create server socket
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) #makes sure address is reusable
# 2. Bind the server socket to server address and server port
server.bind(('127.0.0.1', args.port))
# 3. Continuously listen for connections to server socket
server.listen(5)
try:
while True:
(clientSocket, address) = server.accept()
##print(clientsocket)
##print(address)
self.handleRequest(clientSocket)
except KeyboardInterrupt:
# 5. Close server socket if keyboard interrupt
server.shutdown(socket.SHUT_RDWR)
server.close()
if __name__ == "__main__":
args = setupArgumentParser()
args.func(args)