This shows you the differences between two versions of the page.
ac:laboratoare:08 [2023/12/07 16:12] marios.choudary |
ac:laboratoare:08 [2023/12/07 16:20] (current) marios.choudary |
||
---|---|---|---|
Line 56: | Line 56: | ||
elif cmd[0] == "RECV": | elif cmd[0] == "RECV": | ||
msg = c.recv_message() | msg = c.recv_message() | ||
- | print msg | + | print(msg) |
</code> | </code> | ||
Line 144: | Line 144: | ||
# TODO 1.1 send public keys of I, S and the list of O to the server | # TODO 1.1 send public keys of I, S and the list of O to the server | ||
# e.g. self.s.send(self.I.get_public().public) | # e.g. self.s.send(self.I.get_public().public) | ||
- | # You may also print a log message, e.g. print "Sent I = " + self.I.get_public().public.encode('hex') | + | # You may also print a log message, e.g. print "Sent I = " + self.I.get_public().public.hex() |
# You can send the list of O by first sending its length and then the keys one by one | # You can send the list of O by first sending its length and then the keys one by one | ||
Line 347: | Line 347: | ||
class ClientRecord: | class ClientRecord: | ||
- | """Structure for keeping record of a client's public keys""" | + | """Structure for keeping record of a client's public keys""" |
- | def __init__(self, I, S, O_queue, clientsocket): | + | def __init__(self, I, S, O_queue, clientsocket): |
- | self.I = I | + | self.I = I |
- | self.S = S | + | self.S = S |
- | self.O_queue = O_queue | + | self.O_queue = O_queue |
- | self.s = clientsocket | + | self.s = clientsocket |
class Server: | class Server: | ||
- | def __init__(self, port): | + | def __init__(self, port): |
- | self.port = port | + | self.port = port |
- | self.next_id = 1 | + | self.next_id = 1 |
- | self.registered_clients = {} | + | self.registered_clients = {} |
- | def start(self): | + | def start(self): |
- | """Start listening to client connections""" | + | """Start listening to client connections""" |
- | print "Starting server on port %d" % self.port | + | print("Starting server on port %d" % self.port) |
- | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | + | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
- | s.bind((TCP_IP, self.port)) | + | s.bind((TCP_IP, self.port)) |
- | s.listen(MAX_CLIENTS) | + | s.listen(MAX_CLIENTS) |
- | while True: | + | while True: |
- | c, addr = s.accept() # connection with new client | + | c, addr = s.accept() # connection with new client |
- | print "New client from address " + str(addr) | + | print("New client from address " + str(addr)) |
- | threading.Thread(target = self.on_new_client, args = (c, addr)).start() | + | threading.Thread(target = self.on_new_client, args = (c, addr)).start() |
- | s.close() | + | s.close() |
+ | |||
+ | def on_new_client(self, clientsocket, addr): | ||
+ | """Runs when a new client connects to the server""" | ||
+ | I = PublicKey(clientsocket.recv(32)) | ||
+ | print("Received I = " + I.public.hex()) | ||
+ | S = PublicKey(clientsocket.recv(32)) | ||
+ | print("Received S = " + S.public.hex()) | ||
+ | o_num = struct.unpack('!i', clientsocket.recv(4))[0] | ||
+ | O_queue = [] | ||
+ | for i in xrange(o_num): | ||
+ | O_queue.append(PublicKey(clientsocket.recv(32))) | ||
+ | print("Received O = " + O_queue[i].public.hex()) | ||
- | def on_new_client(self, clientsocket, addr): | + | user_id = self.register_client(I, S, O_queue, clientsocket) |
- | """Runs when a new client connects to the server""" | + | clientsocket.send(struct.pack('!i', user_id)) |
- | I = PublicKey(clientsocket.recv(32)) | + | |
- | print "Received I = " + I.public.hex() | + | |
- | S = PublicKey(clientsocket.recv(32)) | + | |
- | print "Received S = " + S.public.hex() | + | |
- | o_num = struct.unpack('!i', clientsocket.recv(4))[0] | + | |
- | O_queue = [] | + | |
- | for i in xrange(o_num): | + | |
- | O_queue.append(PublicKey(clientsocket.recv(32))) | + | |
- | print "Received O = " + O_queue[i].public.encode('hex') | + | |
- | user_id = self.register_client(I, S, O_queue, clientsocket) | + | while True: |
- | clientsocket.send(struct.pack('!i', user_id)) | + | cmd = clientsocket.recv(4) |
+ | if cmd == "GENS": # GENerate Session | ||
+ | raw_id = clientsocket.recv(4) | ||
+ | rec_user_id = struct.unpack('!i', raw_id)[0] | ||
+ | if rec_user_id not in self.registered_clients: | ||
+ | clientsocket.send("FAIL") | ||
+ | continue | ||
+ | clientsocket.send(b"GOOD") | ||
+ | # Send Irec, Srec, Orec to initiator | ||
+ | Irec = self.registered_clients[rec_user_id].I | ||
+ | Srec = self.registered_clients[rec_user_id].S | ||
+ | Orec = self.registered_clients[rec_user_id].O_queue.pop() | ||
+ | clientsocket.send(Irec.public) | ||
+ | clientsocket.send(Srec.public) | ||
+ | clientsocket.send(Orec.public) | ||
- | while True: | + | # get Eini from initiator |
- | cmd = clientsocket.recv(4) | + | Eini = PublicKey(clientsocket.recv(32)) |
- | if cmd == "GENS": # GENerate Session | + | |
- | raw_id = clientsocket.recv(4) | + | |
- | rec_user_id = struct.unpack('!i', raw_id)[0] | + | |
- | if rec_user_id not in self.registered_clients: | + | |
- | clientsocket.send("FAIL") | + | |
- | continue | + | |
- | clientsocket.send(b"GOOD") | + | |
- | # Send Irec, Srec, Orec to initiator | + | |
- | Irec = self.registered_clients[rec_user_id].I | + | |
- | Srec = self.registered_clients[rec_user_id].S | + | |
- | Orec = self.registered_clients[rec_user_id].O_queue.pop() | + | |
- | clientsocket.send(Irec.public) | + | |
- | clientsocket.send(Srec.public) | + | |
- | clientsocket.send(Orec.public) | + | |
- | # get Eini from initiator | + | rec_clientsocket = self.registered_clients[rec_user_id].s |
- | Eini = PublicKey(clientsocket.recv(32)) | + | rec_clientsocket.send(b"NEWS") # NEW Session |
+ | rec_clientsocket.send(struct.pack('!i', user_id)) | ||
+ | # Send Eini, Iini, Orec to recipient | ||
+ | rec_clientsocket.send(Eini.public) | ||
+ | rec_clientsocket.send(I.public) | ||
+ | rec_clientsocket.send(Orec.public) | ||
+ | if cmd == "SEND": # SEND Message | ||
+ | # Get recipient used id | ||
+ | raw_id = clientsocket.recv(4) | ||
+ | rec_user_id = struct.unpack('!i', raw_id)[0] | ||
- | rec_clientsocket = self.registered_clients[rec_user_id].s | + | rec_clientsocket = self.registered_clients[rec_user_id].s |
- | rec_clientsocket.send(b"NEWS") # NEW Session | + | rec_clientsocket.send(b"MESG") # send message tag |
- | rec_clientsocket.send(struct.pack('!i', user_id)) | + | |
- | # Send Eini, Iini, Orec to recipient | + | |
- | rec_clientsocket.send(Eini.public) | + | |
- | rec_clientsocket.send(I.public) | + | |
- | rec_clientsocket.send(Orec.public) | + | |
- | if cmd == "SEND": # SEND Message | + | |
- | # Get recipient used id | + | |
- | raw_id = clientsocket.recv(4) | + | |
- | rec_user_id = struct.unpack('!i', raw_id)[0] | + | |
- | rec_clientsocket = self.registered_clients[rec_user_id].s | + | # Send sender user id |
- | rec_clientsocket.send(b"MESG") # send message tag | + | rec_clientsocket.send(struct.pack('!i', user_id)) |
- | # Send sender user id | + | # Forward sender ephemereal key |
- | rec_clientsocket.send(struct.pack('!i', user_id)) | + | eph_s = clientsocket.recv(32) |
+ | rec_clientsocket.send(eph_s) | ||
- | # Forward sender ephemereal key | + | # Forward message length |
- | eph_s = clientsocket.recv(32) | + | raw_mlen = clientsocket.recv(4) |
- | rec_clientsocket.send(eph_s) | + | rec_clientsocket.send(raw_mlen) |
- | # Forward message length | + | # Forward message |
- | raw_mlen = clientsocket.recv(4) | + | mlen = struct.unpack('!i', raw_mlen)[0] |
- | rec_clientsocket.send(raw_mlen) | + | raw_msg = clientsocket.recv(mlen) |
+ | rec_clientsocket.send(raw_msg) | ||
- | # Forward message | + | def register_client(self, I, S, O_queue, clientsocket): |
- | mlen = struct.unpack('!i', raw_mlen)[0] | + | """Registers new client's I, S, and O_queue and returns the new client's id""" |
- | raw_msg = clientsocket.recv(mlen) | + | new_client = ClientRecord(I, S, O_queue, clientsocket) |
- | rec_clientsocket.send(raw_msg) | + | |
- | def register_client(self, I, S, O_queue, clientsocket): | + | user_id = self.next_id |
- | """Registers new client's I, S, and O_queue and returns the new client's id""" | + | self.next_id += 1 |
- | new_client = ClientRecord(I, S, O_queue, clientsocket) | + | |
- | user_id = self.next_id | + | self.registered_clients[user_id] = new_client |
- | self.next_id += 1 | + | |
- | self.registered_clients[user_id] = new_client | + | return user_id |
- | + | ||
- | return user_id | + | |
</code> | </code> | ||