def __listen_udp_sync(self): if common.DEBUG: print('Start listening on UDP') with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s: s.bind(('', common.PORT)) s.setblocking(0) while self.listening: try: result = select.select([s], [], []) data = result[0][0].recv(common.BUFFER_SIZE) if common.DEBUG: print('UDP Data recieved:', data) sender_name, sender_ip, message_type, message_payload = message.decode_message( data) if message_type == message_types.DISCOVER: # Send a respond message self.__send_message( sender_ip, message_types.RESPOND) self.__add_to_other_clients( sender_name, sender_ip) elif message_type == message_types.GOODBYE: self.__remove_from_other_clients(sender_name) except KeyboardInterrupt: print('Keyboard interrupt') self.__stop_listening()
def update(self): offs = 0 for c in self.out_commands: offs = message.encode_message(self.buffer, offs, c) self.out_commands = [] self.connection.send(self.buffer[:offs]) offs = 0 while True: tup = message.decode_message(self.connection.received_data, offs) offs, msg = tup[0], tup[1:] if msg[0] == message.MESSAGE_NONE: break else: self.in_commands.append(msg) if offs: self.connection.received_data = self.connection.received_data[offs:]
def test_decode_message(): try: message = decode_message([ '.--.', '-.--', '-', '....', '---', '-.', '/', '..', '...', '/', '..-.', '..-', '-.', '-.-.--' ]) assert message == 'python is fun!', f'Incorrect answer.' success() send_msg('🌟 Congratulations, you decoded the message:', f'Message: {message}') except AssertionError as e: fail() send_msg("Oops, something's not right! 🐞", e) send_msg( "Hint 💡", "Did you remember to return the message as a single lower-case string? 🤔" ) send_msg("Hint 💡", "Did you rememeber to include spaces and punctuation? 🤔")
def update(self): if not self.isopen(): raise NotConnected() sl = [self.sock] readable, writable, error = select.select(sl, sl, sl, 0) if readable: r = self.sock.recv(2048) if not len(r): error.append(sl) else: self.inputdata.extend(r) if writable and self.out_messages: outputdata = bytearray(1024 * 4096) offs = 0 for m in self.out_messages: print ('Client sent', m) offs = message.encode_message(outputdata, offs, m) self.out_messages = [] if offs: n = self.sock.send(outputdata[:offs]) if n < 1: error.append(sl) #if error: # self.sock.close() # self.sock = None offs = 0 while True: tup = message.decode_message(self.inputdata, offs) offs, msg = tup[0], tup[1:] if msg[0] != message.MESSAGE_NONE: self.in_messages.append(msg) else: break; if offs: self.inputdata = self.inputdata[offs:]
def _process_frame(self, frame): log.debug("Processing new frame") message = decode_message(frame) self.backmonitor.on_new_message(message) self.frames_received += 1
def __handle_received_data(self, data): sender_name, sender_ip, message_type, message_payload = message.decode_message( data) if message_type == message_types.DISCOVER: # Send a respond message self.__send_message( sender_ip, message_types.RESPOND) self.__add_to_other_clients( sender_name, sender_ip) elif message_type == message_types.RESPOND: self.__add_to_other_clients( sender_name, sender_ip) elif message_type == message_types.MESSAGE: # Read session session_ip = self.__get_session_ip(sender_ip) if session_ip in self.sessions: session = self.sessions[session_ip] cypher = session[session_keys.CYPHER] p_value = session[session_keys.P_VALUE] message_payload = security.decrypt_message( message_payload, cypher) # Update cypher for this session cypher = security.get_evolved_cypher( message_payload, cypher, p_value) session[session_keys.CYPHER] = cypher if common.DEBUG: print('Receiver updated session with evolved cypher', self.sessions) # Print out the message print(sender_name, ':', message_payload) self.__add_to_other_clients( sender_name, sender_ip) elif message_type == message_types.INIT_SEND: # Read public_key, g_value, p_value values of other client (g_value, p_value, other_public_key) = message_payload # Calculate public_key private_key = common.get_random() public_key = (g_value ** private_key) % p_value # Calculate cypher with public_key and other_public_key cypher = (other_public_key ** private_key) % p_value # Create session session_ip = self.__get_session_ip(sender_ip) session = self.sessions[session_ip] = {} session[session_keys.PRIVATE_KEY] = private_key session[session_keys.P_VALUE] = p_value # Store cypher of this session in dictionary session[session_keys.CYPHER] = cypher if common.DEBUG: print("Receiver stored cypher:", self.sessions) # Send INIT_RESPOND with public_key in it init_respond_payload = (g_value, p_value, public_key) self.__send_message( sender_ip, message_types.INIT_RESPOND, init_respond_payload) elif message_type == message_types.INIT_RESPOND: # Read public_key of other client (g_value, p_value, other_public_key) = message_payload # Calculate cypher with other_public_key session = self.sessions[sender_ip] private_key = session[session_keys.PRIVATE_KEY] cypher = (other_public_key ** private_key) % p_value # Store cypher of this session in dictionary session[session_keys.CYPHER] = cypher if common.DEBUG: print('Sender updated session with cypher', session) # Send messages in the queue for msg in session[session_keys.MESSAGE_QUEUE]: self.__send_message(sender_ip, message_types.MESSAGE, msg) else: if common.DEBUG: print('Message wasn\'t interpreted:', data)