-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.py
More file actions
87 lines (73 loc) · 3.41 KB
/
node.py
File metadata and controls
87 lines (73 loc) · 3.41 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
import socket
from uuid import getnode as get_mac
class Node:
def __init__(self, ip, port):
self.node_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.ip = ip
self.port = port
def act_as_server(self):
self.node_socket.bind((self.ip, self.port))
self.node_socket.listen(5)
def send_packets(self, packets):
client_socket, client_address = self.node_socket.accept()
print("Got a connection from", client_address[0])
client_socket.send(str(len(packets)).encode('ascii'))
for packet in packets:
# packet['sender_mac'] = get_mac()
packet['receiver_ip'] = client_address[0]
print(packet)
client_socket.send(str(len(str(packet))).encode('ascii'))
client_socket.send(str(packet).encode('ascii'))
client_socket.close()
def send_number_of_packets(self, number, socket):
print(number)
socket.send(str(number).encode('ascii'))
def send_packet(self, packet, socket, address):
packet['receiver_ip'] = address[0]
print(packet)
socket.send(str(len(str(packet))).encode('ascii'))
socket.send(str(packet).encode('ascii'))
def act_as_client(self):
self.node_socket.connect((self.ip, self.port))
def receive_packets(self):
number_of_packets = int(self.node_socket.recv(7).decode('ascii'))
print(number_of_packets)
packets = []
for i in range(number_of_packets):
packet_size = int(self.node_socket.recv(3).decode('ascii'))
print('packet number ' + str(i + 1) + ' size', packet_size)
string = self.node_socket.recv(packet_size).decode('ascii')
packets.append(eval(string))
del string
return packets
def receive_number_of_packets(self):
return int(self.node_socket.recv(7).decode('ascii'))
def receive_packet(self, packet_number):
packet_size = int(self.node_socket.recv(3).decode('ascii'))
print('packet number ' + str(packet_number + 1) + ' size', packet_size)
string = self.node_socket.recv(packet_size).decode('ascii')
return eval(string)
def send_certificate(self, client_list):
client_socket, client_address = self.node_socket.accept()
print("Got a connection from", client_address)
if client_address[0] not in client_list.keys():
return 'client doesnt exist on certificate server'
certificate_number = client_list[client_address[0]]
print(certificate_number)
client_socket.send(str(len(str(certificate_number))).encode('ascii'))
client_socket.send(str(certificate_number).encode('ascii'))
client_socket.close()
return 'corresponding certificate number sent'
def receive_certificate(self):
packet_size = int(self.node_socket.recv(3).decode('ascii'))
print('certificate size', packet_size)
return self.node_socket.recv(packet_size).decode('ascii')
def verify_packets(packets, certificate):
result = True
for packet in packets:
if packet['certificate'] == certificate:
print('certificate verified for packet number', packet['seq_no'])
else:
print('certificate does not match for packet number', packet['seq_no'])
result = False
return result