def generate_outgoing_response(outgoing_message: Message, incoming_packet: IncomingPacketBundle = None) -> OutgoingPacketBundle: """ generate the outgoing packet and check the RelayServerMessage around it. :param outgoing_message: The reply message :param incoming_packet: The original received packet, only used to sanity-check the reply :return: The parsed message """ # Verify that the outer relay message makes sense if not isinstance(outgoing_message, RelayReplyMessage): raise ValueError("The reply has to be wrapped in a RelayReplyMessage") if incoming_packet is not None: # Verify the contents of the outgoing message if outgoing_message.link_address != incoming_packet.link_address: raise ValueError("The relay-reply link-address does not match the relay-forward link-address") interface_id_option = outgoing_message.get_option_of_type(InterfaceIdOption) if interface_id_option and interface_id_option.interface_id != incoming_packet.interface_id: # If there is an interface-id option its contents have to match raise ValueError("The interface-id in the reply does not match the interface-id of the request") reply = outgoing_message.relayed_message if not reply: raise ValueError("The RelayReplyMessage does not contain a message") # Down to network addresses and bytes port = isinstance(reply, RelayReplyMessage) and SERVER_PORT or CLIENT_PORT destination = outgoing_message.peer_address data = reply.save() return OutgoingPacketBundle(message_id=incoming_packet.message_id, data=data, destination=destination, port=port)
def generate_outgoing_response( outgoing_message: Message, incoming_packet: IncomingPacketBundle = None) -> OutgoingPacketBundle: """ generate the outgoing packet and check the RelayServerMessage around it. :param outgoing_message: The reply message :param incoming_packet: The original received packet, only used to sanity-check the reply :return: The parsed message """ # Verify that the outer relay message makes sense if not isinstance(outgoing_message, RelayReplyMessage): raise ValueError("The reply has to be wrapped in a RelayReplyMessage") if incoming_packet is not None: # Verify the contents of the outgoing message if outgoing_message.link_address != incoming_packet.link_address: raise ValueError( "The relay-reply link-address does not match the relay-forward link-address" ) interface_id_option = outgoing_message.get_option_of_type( InterfaceIdOption) if interface_id_option and interface_id_option.interface_id != incoming_packet.interface_name.encode( 'utf-8'): # If there is an interface-id option its contents have to match raise ValueError( "The interface-id in the reply does not match the interface-id of the request" ) reply = outgoing_message.relayed_message if not reply: raise ValueError("The RelayReplyMessage does not contain a message") # Down to network addresses and bytes port = isinstance(reply, RelayReplyMessage) and SERVER_PORT or CLIENT_PORT destination = outgoing_message.peer_address data = reply.save() return OutgoingPacketBundle(message_id=incoming_packet.message_id, data=data, destination=destination, port=port)