def parse(self, header_bytes): # byte 0 -- protocol version, packet type, flags byte0 = header_bytes[0] flag_bits = 3 packet_type_bits = 3 protocol_version_bits = 2 self.parse_protocol_version(byte0, protocol_version_bits) byte0 = byte0 >> protocol_version_bits self.parse_packet_type(byte0, packet_type_bits) byte0 = byte0 >> packet_type_bits self.parse_flag(byte0, flag_bits) # byte 1..8 -- SRC ID byte1_8 = header_bytes[1:9] self.source = bytes_to_GPG(byte1_8) # byte 9..16 -- DST ID byte9_16 = header_bytes[9:17] self.destination = bytes_to_GPG(byte9_16) # byte 17 -- sessionID, identify flow between endpoints byte17 = header_bytes[17] self.session_id = byte17 # byte 18..19 -- sequence number byte18_19 = header_bytes[18:20] self.sequence_number = bytes_to_number(byte18_19)
def parse_file_metadata(payload): metadata_size = bytes_to_number(payload[0:METADATA_HEADER]) if (metadata_size != 0): metadata_str = payload[METADATA_HEADER:METADATA_HEADER + metadata_size].decode() metadata_dict = json.loads(metadata_str) return (metadata_dict, METADATA_HEADER + metadata_size) return ({}, 0)
def byte_to_table(self, table_byte, cost=0): array = [] table = {} table_key = '' for x in range(int(len(table_byte) / 10)): array.append(table_byte[:10]) for a in array: key = bytes_to_GPG(a[:8]) value = bytes_to_number(a[8:]) if value == MAX_ROUTE_COST: continue table[key] = value + cost if value == 0: table_key = key return {table_key: table}
def route_update(header, payload): log = 'Route update from: ' + str(header.source) print(colors.LOG, log) if (GlobalData.nodes.is_updated(header.source, payload)): GlobalData.nodes.update_table(header.source, payload) updated_node = bytes_to_GPG(payload[0:8]) updated_cost = bytes_to_number(payload[8:10]) payload[8:10] = number_to_bytes(updated_cost + 1, 2) neighbors = GlobalData.nodes.get_neighbors() Message.send_message_to_group(packet_types['route_update'], neighbors, payload) if (GlobalData.nodes.get_nickname(updated_node) == ""): Message.send_identity_request(updated_node)
def skip_index(payload, packet_type): if (packet_type == packet_types['metadata_message']): return METADATA_HEADER + bytes_to_number(payload[0:METADATA_HEADER]) return 0