From 43de693f01360aa3033c0f5360e1a0e492d97884 Mon Sep 17 00:00:00 2001 From: Mark Qvist Date: Sat, 15 May 2021 10:57:54 +0200 Subject: [PATCH] Fixed typo --- Examples/Broadcast.py | 2 +- Examples/Echo.py | 2 +- Examples/Filetransfer.py | 2 +- Examples/Link.py | 2 +- Examples/Minimal.py | 2 +- RNS/Destination.py | 20 +++++++++++++++----- RNS/Transport.py | 40 +++++++++++++++++++++++++++++++++++++++- 7 files changed, 59 insertions(+), 11 deletions(-) diff --git a/Examples/Broadcast.py b/Examples/Broadcast.py index 5bd5bea..3388996 100644 --- a/Examples/Broadcast.py +++ b/Examples/Broadcast.py @@ -11,7 +11,7 @@ import RNS # destinations we create. Since this basic example # is part of a range of example utilities, we'll put # them all within the app namespace "example_utilities" -APP_NAME = "example_utilitites" +APP_NAME = "example_utilities" # This initialisation is executed when the program is started def program_setup(configpath, channel=None): diff --git a/Examples/Echo.py b/Examples/Echo.py index 2b84247..d4770d5 100644 --- a/Examples/Echo.py +++ b/Examples/Echo.py @@ -12,7 +12,7 @@ import RNS # destinations we create. Since this echo example # is part of a range of example utilities, we'll put # them all within the app namespace "example_utilities" -APP_NAME = "example_utilitites" +APP_NAME = "example_utilities" ########################################################## diff --git a/Examples/Filetransfer.py b/Examples/Filetransfer.py index 6c4711e..2f861af 100644 --- a/Examples/Filetransfer.py +++ b/Examples/Filetransfer.py @@ -28,7 +28,7 @@ import RNS.vendor.umsgpack as umsgpack # destinations we create. Since this echo example # is part of a range of example utilities, we'll put # them all within the app namespace "example_utilities" -APP_NAME = "example_utilitites" +APP_NAME = "example_utilities" # We'll also define a default timeout, in seconds APP_TIMEOUT = 45.0 diff --git a/Examples/Link.py b/Examples/Link.py index 1fdd650..aeb2d50 100644 --- a/Examples/Link.py +++ b/Examples/Link.py @@ -13,7 +13,7 @@ import RNS # destinations we create. Since this echo example # is part of a range of example utilities, we'll put # them all within the app namespace "example_utilities" -APP_NAME = "example_utilitites" +APP_NAME = "example_utilities" ########################################################## #### Server Part ######################################### diff --git a/Examples/Minimal.py b/Examples/Minimal.py index c9cf0eb..a4daf1f 100644 --- a/Examples/Minimal.py +++ b/Examples/Minimal.py @@ -11,7 +11,7 @@ import RNS # destinations we create. Since this basic example # is part of a range of example utilities, we'll put # them all within the app namespace "example_utilities" -APP_NAME = "example_utilitites" +APP_NAME = "example_utilities" # This initialisation is executed when the program is started def program_setup(configpath): diff --git a/RNS/Destination.py b/RNS/Destination.py index 7f1020e..958613d 100755 --- a/RNS/Destination.py +++ b/RNS/Destination.py @@ -36,7 +36,7 @@ class Destination: directions = [IN, OUT] @staticmethod - def getDestinationName(app_name, *aspects): + def full_name(app_name, *aspects): # Check input values and build name string if "." in app_name: raise ValueError("Dots can't be used in app names") @@ -49,8 +49,8 @@ class Destination: @staticmethod - def getDestinationHash(app_name, *aspects): - name = Destination.getDestinationName(app_name, *aspects) + def hash(app_name, *aspects): + name = Destination.full_name(app_name, *aspects) # Create a digest for the destination digest = hashes.Hash(hashes.SHA256(), backend=default_backend()) @@ -58,6 +58,16 @@ class Destination: return digest.finalize()[:10] + @staticmethod + def app_and_aspects_from_name(full_name): + components = full_name.split(".") + return (components[0], components[1:]) + + @staticmethod + def hash_from_name_and_identity(full_name, identity): + app_name, aspects = Destination.app_and_aspects_from_name(full_name) + aspects.append(identity.hexhash) + return Destination.hash(app_name, *aspects) def __init__(self, identity, direction, type, app_name, *aspects): # Check input values and build name string @@ -81,8 +91,8 @@ class Destination: self.identity = identity - self.name = Destination.getDestinationName(app_name, *aspects) - self.hash = Destination.getDestinationHash(app_name, *aspects) + self.name = Destination.full_name(app_name, *aspects) + self.hash = Destination.hash(app_name, *aspects) self.hexhash = self.hash.hex() self.callback = None diff --git a/RNS/Transport.py b/RNS/Transport.py index 7a8207e..803bc8f 100755 --- a/RNS/Transport.py +++ b/RNS/Transport.py @@ -56,6 +56,7 @@ class Transport: reverse_table = {} # A lookup table for storing packet hashes used to return proofs and replies link_table = {} # A lookup table containing hops for links held_announces = {} # A table containing temporarily held announce-table entries + announce_handlers = [] # A table storing externally registered announce handlers # Transport control destinations are used # for control purposes like path requests @@ -749,7 +750,33 @@ class Transport: Transport.destination_table[packet.destination_hash] = [now, received_from, announce_hops, expires, random_blobs, packet.receiving_interface, packet] RNS.log("Path to "+RNS.prettyhexrep(packet.destination_hash)+" is now "+str(announce_hops)+" hops away via "+RNS.prettyhexrep(received_from)+" on "+str(packet.receiving_interface), RNS.LOG_VERBOSE) - + + # Call externally registered callbacks from apps + # wanting to know when an announce arrives + for handler in Transport.announce_handlers: + try: + # Check that the announced destination matches + # the handlers aspect filter + execute_callback = False + if handler.aspect_filter == None: + # If the handlers aspect filter is set to + # None, we execute the callback in all cases + execute_callback = True + else: + announce_identity = RNS.Identity.recall(packet.destination_hash) + handler_expected_hash = RNS.Destination.hash_from_name_and_identity(handler.aspect_filter, announce_identity) + if packet.destination_hash == handler_expected_hash: + execute_callback = True + if execute_callback: + handler.received_announce( + destination_hash=packet.destination_hash, + announced_identity=announce_identity, + app_data=RNS.Identity.recall_app_data(packet.destination_hash) + ) + except Exception as e: + RNS.log("Error while processing external announce callback.", RNS.LOG_ERROR) + RNS.log("The contained exception was: "+str(e), RNS.LOG_ERROR) + # Handling for linkrequests to local destinations elif packet.packet_type == RNS.Packet.LINKREQUEST: for destination in Transport.destinations: @@ -878,6 +905,17 @@ class Transport: else: RNS.log("Attempted to activate a link that was not in the pending table", RNS.LOG_ERROR) + @staticmethod + def register_announce_handler(handler): + if hasattr(handler, "received_announce") and callable(handler.received_announce): + if hasattr(handler, "aspect_filter"): + Transport.announce_handlers.append(handler) + + @staticmethod + def deregister_announce_handler(handler): + while handler in Transport.announce_handlers: + Transport.announce_handlers.remove(handler) + @staticmethod def find_interface_from_hash(interface_hash): for interface in Transport.interfaces: