From 825c2e19934f14da4d135bbc65b8b5a4832cc06f Mon Sep 17 00:00:00 2001 From: Mark Qvist Date: Sun, 1 Nov 2026 23:07:03 +0100 Subject: [PATCH] Added basic destination type filtering --- RNS/Destination.py | 8 ++ RNS/Discovery.py | 1 + RNS/Reticulum.py | 171 ++++++++++++++++++++++++++------ RNS/Transport.py | 102 +++++++++++++++++++ RNS/Utilities/rncp.py | 2 + RNS/Utilities/rngit/client.py | 2 + RNS/Utilities/rngit/server.py | 7 +- RNS/Utilities/rnpath.py | 2 + RNS/Utilities/rnsh/initiator.py | 2 + RNS/Utilities/rnstatus.py | 5 +- RNS/Utilities/rnx.py | 2 + 11 files changed, 272 insertions(+), 32 deletions(-) diff --git a/RNS/Destination.py b/RNS/Destination.py index cb6fd7b1..6f018693 100755 --- a/RNS/Destination.py +++ b/RNS/Destination.py @@ -148,6 +148,14 @@ class Destination: return Destination.hash(identity, app_name, *aspects) + @staticmethod + def name_hash(full_name): + components = Destination.app_and_aspects_from_name(full_name) + app_name = str(components[0]) + aspects = f".{'.'.join(components[1])}" if components[1] else "" + name = f"{app_name}{aspects}".encode("utf-8") + return RNS.Identity.full_hash(name)[:(RNS.Identity.NAME_HASH_LENGTH//8)] + def __init__(self, identity, direction, type, app_name, *aspects): # Check input values and build name string if "." in app_name: raise ValueError("Dots can't be used in app names") diff --git a/RNS/Discovery.py b/RNS/Discovery.py index 13be0332..db4b9f93 100644 --- a/RNS/Discovery.py +++ b/RNS/Discovery.py @@ -905,6 +905,7 @@ class BlackholeUpdater(): def start(self): if not self.should_run: + RNS.Reticulum.get_instance().wants_destination_type("rnstransport.info.blackhole") source_count = len(RNS.Reticulum.blackhole_sources()) ms = "" if source_count == 1 else "s" RNS.log(f"Starting blackhole updater with {source_count} source{ms}", RNS.LOG_DEBUG) diff --git a/RNS/Reticulum.py b/RNS/Reticulum.py index 2cd7bfff..5842862c 100755 --- a/RNS/Reticulum.py +++ b/RNS/Reticulum.py @@ -186,6 +186,7 @@ class Reticulum: # out cleanup operations. if not Reticulum.__exit_handler_ran: Reticulum.__exit_handler_ran = True + Reticulum.__instance.unwants_all_destination_types() if not Reticulum.__interface_detach_ran: RNS.Transport.detach_interfaces() RNS.Transport.exit_handler() RNS.Identity.exit_handler() @@ -471,6 +472,20 @@ class Reticulum: value = self.config["logging"].as_bool(option) RNS.logtimestamps = bool(value) + if "filter" in self.config: + for option in self.config["filter"]: + if option == "required_only": + v = self.config["filter"].as_bool(option) + RNS.Transport.required_destinations_only = v + + if option == "ignored_types": + entries = self.config["filter"].as_list(option) + for entry in entries: RNS.Transport.block_destination_type(entry) + + if option == "allowed_types": + entries = self.config["filter"].as_list(option) + for entry in entries: RNS.Transport.allow_destination_type(entry) + if "stamps" in self.config: for option in self.config["stamps"]: if option == "enforce_stamps": @@ -1483,6 +1498,23 @@ class Reticulum: identity_hash = call["unblackhole_identity"] self.rpc_return(conn, self.unblackhole_identity(identity_hash)) + if "wants_destination_type" in call: + full_name = call["wants_destination_type"] + requester = call["requester"] + signature = call["signature"] + self.rpc_return(conn, self.wants_destination_type(full_name, requester, signature)) + + if "unwants_destination_type" in call: + full_name = call["unwants_destination_type"] + requester = call["requester"] + signature = call["signature"] + self.rpc_return(conn, self.unwants_destination_type(full_name, requester, signature)) + + if "unwants_all_destination_types" in call: + requester = call["requester"] + signature = call["signature"] + self.rpc_return(conn, self.unwants_all_destination_types(requester, signature)) + if "destination_data" in call: operation = call["destination_data"] destination_hash = call["destination_hash"] @@ -1561,10 +1593,15 @@ class Reticulum: def get_interface_stats(self): if self.is_connected_to_shared_instance: - rpc_connection = self.get_rpc_client() - rpc_connection.send_bytes(mp.packb({"get": "interface_stats"})) - response = mp.unpackb(rpc_connection.recv_bytes()) - return response + try: + rpc_connection = self.get_rpc_client() + rpc_connection.send_bytes(mp.packb({"get": "interface_stats"})) + response = mp.unpackb(rpc_connection.recv_bytes()) + return response + + except Exception as e: + RNS.log(f"Shared instance RPC failed while getting interface stats: {e}", RNS.LOG_ERROR) + return None else: interfaces = [] @@ -1817,10 +1854,14 @@ class Reticulum: def get_path_table(self, max_hops=None): if self.is_connected_to_shared_instance: - rpc_connection = self.get_rpc_client() - rpc_connection.send_bytes(mp.packb({"get": "path_table", "max_hops": max_hops})) - response = mp.unpackb(rpc_connection.recv_bytes()) - return response + try: + rpc_connection = self.get_rpc_client() + rpc_connection.send_bytes(mp.packb({"get": "path_table", "max_hops": max_hops})) + response = mp.unpackb(rpc_connection.recv_bytes()) + return response + except Exception as e: + RNS.log(f"Shared instance RPC failed while getting path table: {e}", RNS.LOG_ERROR) + return [] else: path_table = [] @@ -1841,10 +1882,14 @@ class Reticulum: def get_rate_table(self): if self.is_connected_to_shared_instance: - rpc_connection = self.get_rpc_client() - rpc_connection.send_bytes(mp.packb({"get": "rate_table"})) - response = mp.unpackb(rpc_connection.recv_bytes()) - return response + try: + rpc_connection = self.get_rpc_client() + rpc_connection.send_bytes(mp.packb({"get": "rate_table"})) + response = mp.unpackb(rpc_connection.recv_bytes()) + return response + except Exception as e: + RNS.log(f"Shared instance RPC failed while getting rate table: {e}", RNS.LOG_ERROR) + return [] else: rate_table = [] @@ -2003,20 +2048,28 @@ class Reticulum: def get_link_distribution(self): if self.is_connected_to_shared_instance: - rpc_connection = self.get_rpc_client() - rpc_connection.send_bytes(mp.packb({"get": "link_distribution"})) - response = mp.unpackb(rpc_connection.recv_bytes()) - return response + try: + rpc_connection = self.get_rpc_client() + rpc_connection.send_bytes(mp.packb({"get": "link_distribution"})) + response = mp.unpackb(rpc_connection.recv_bytes()) + return response + except Exception as e: + RNS.log(f"Shared instance RPC failed while getting link distribution: {e}", RNS.LOG_ERROR) + return None else: return RNS.Transport.link_distribution() def get_packet_rssi(self, packet_hash): if self.is_connected_to_shared_instance: - rpc_connection = self.get_rpc_client() - rpc_connection.send_bytes(mp.packb({"get": "packet_rssi", "packet_hash": packet_hash})) - response = mp.unpackb(rpc_connection.recv_bytes()) - return response + try: + rpc_connection = self.get_rpc_client() + rpc_connection.send_bytes(mp.packb({"get": "packet_rssi", "packet_hash": packet_hash})) + response = mp.unpackb(rpc_connection.recv_bytes()) + return response + except Exception as e: + RNS.log(f"Shared instance RPC failed while getting packet RSSI: {e}", RNS.LOG_ERROR) + return None else: for entry in RNS.Transport.local_client_rssi_cache: @@ -2027,10 +2080,14 @@ class Reticulum: def get_packet_snr(self, packet_hash): if self.is_connected_to_shared_instance: - rpc_connection = self.get_rpc_client() - rpc_connection.send_bytes(mp.packb({"get": "packet_snr", "packet_hash": packet_hash})) - response = mp.unpackb(rpc_connection.recv_bytes()) - return response + try: + rpc_connection = self.get_rpc_client() + rpc_connection.send_bytes(mp.packb({"get": "packet_snr", "packet_hash": packet_hash})) + response = mp.unpackb(rpc_connection.recv_bytes()) + return response + except Exception as e: + RNS.log(f"Shared instance RPC failed while getting packet SNR: {e}", RNS.LOG_ERROR) + return None else: for entry in RNS.Transport.local_client_snr_cache: @@ -2041,10 +2098,14 @@ class Reticulum: def get_packet_q(self, packet_hash): if self.is_connected_to_shared_instance: - rpc_connection = self.get_rpc_client() - rpc_connection.send_bytes(mp.packb({"get": "packet_q", "packet_hash": packet_hash})) - response = mp.unpackb(rpc_connection.recv_bytes()) - return response + try: + rpc_connection = self.get_rpc_client() + rpc_connection.send_bytes(mp.packb({"get": "packet_q", "packet_hash": packet_hash})) + response = mp.unpackb(rpc_connection.recv_bytes()) + return response + except Exception as e: + RNS.log(f"Shared instance RPC failed while getting packet link quality: {e}", RNS.LOG_ERROR) + return None else: for entry in RNS.Transport.local_client_q_cache: @@ -2136,6 +2197,60 @@ class Reticulum: else: return RNS.Transport.unblackhole_identity(identity_hash) + # For now, this is available on shared instance clients + # only, so an application cannot squelch announce ingress + # for other applications and the node as a whole. + def required_announces_only(self, enabled=True): + if not enabled in [True, False]: return False + if not self.is_connected_to_shared_instance: return False + else: RNS.Transport.required_destinations_only = enabled + return True + + def wants_destination_type(self, full_name, requester=None, signature=None): + if not requester: requester = RNS.Transport.application_identity.hash + if self.is_connected_to_shared_instance: + try: + RNS.Transport._wants_destination_type(full_name, requester, signature) + rpc_connection = self.get_rpc_client() + rpc_connection.send_bytes(mp.packb({"wants_destination_type": full_name, "requester": requester, "signature": signature})) + response = mp.unpackb(rpc_connection.recv_bytes()) + return response + except Exception as e: + RNS.log(f"Shared instance RPC failed while setting destination type filters: {e}", RNS.LOG_ERROR) + return False + + else: return RNS.Transport._wants_destination_type(full_name, requester, signature) + + def unwants_destination_type(self, full_name, requester=None, signature=None): + if not requester: requester = RNS.Transport.application_identity.hash + if self.is_connected_to_shared_instance: + try: + RNS.Transport._unwants_destination_type(full_name, requester, signature) + rpc_connection = self.get_rpc_client() + rpc_connection.send_bytes(mp.packb({"unwants_destination_type": full_name, "requester": requester, "signature": signature})) + response = mp.unpackb(rpc_connection.recv_bytes()) + return response + except Exception as e: + RNS.log(f"Shared instance RPC failed while setting destination type filters: {e}", RNS.LOG_ERROR) + return False + + else: return RNS.Transport._unwants_destination_type(full_name, requester, signature) + + def unwants_all_destination_types(self, requester=None, signature=None): + if not requester: requester = RNS.Transport.application_identity.hash + if self.is_connected_to_shared_instance: + try: + RNS.Transport._unwants_all_destination_types(requester, signature) + rpc_connection = self.get_rpc_client() + rpc_connection.send_bytes(mp.packb({"unwants_all_destination_types": True, "requester": requester, "signature": signature})) + response = mp.unpackb(rpc_connection.recv_bytes()) + return response + except Exception as e: + RNS.log(f"Shared instance RPC failed while setting destination type filters: {e}", RNS.LOG_ERROR) + return False + + else: return RNS.Transport._unwants_all_destination_types(requester, signature) + @staticmethod def should_use_implicit_proof(): """ diff --git a/RNS/Transport.py b/RNS/Transport.py index 3c60dee5..7ffc8217 100755 --- a/RNS/Transport.py +++ b/RNS/Transport.py @@ -264,6 +264,11 @@ class Transport: inflight_path_requests = {} # A table for tracking in-flight path request path_states = {} # A table for keeping track of path states blackholed_identities = {} # A table for keeping track of blackholed identities + blocked_destination_types = {} # A table for keeping track of ignored destination types + allowed_destination_types = {} # A table for keeping track of allowed destination types + client_destination_types = {} # A table for keeping track of destination types needed by applications + destination_type_map = {} # Lookup table for destination type names + required_destinations_only = False announce_rate_table = AnnounceRateTable(max_timestamps=MAX_RATE_TIMESTAMPS) discovery_path_requests = {} # A table for keeping track of path requests on behalf of other nodes @@ -398,6 +403,7 @@ class Transport: identity = None network_identity = None + application_identity = None _identity = None _should_run = True @@ -439,6 +445,8 @@ class Transport: if RNS.Reticulum.local_hops_delta(): Transport.local_hops_delta = (ord(os.urandom(1))%6)+2 + if Transport.application_identity == None: Transport.application_identity = RNS.Identity() + if not RNS.Reticulum.transport_enabled() or Transport.owner.is_connected_to_shared_instance: Transport.unused_path_linger = Transport.UNUSED_PATH_LINGER_LEAF else: @@ -2014,6 +2022,18 @@ class Transport: # Ingress limit announces early if packet.packet_type == RNS.Packet.ANNOUNCE: + name_hash = packet.data[RNS.Identity.KEYSIZE//8:RNS.Identity.KEYSIZE//8+RNS.Identity.NAME_HASH_LENGTH//8] + if len(name_hash) != RNS.Identity.NAME_HASH_LENGTH//8: return packet.receiving_interface.protocol_violation(f"Malformed announce packet with size {len(packet.data)}") + + if Transport.blocked_destination_types and name_hash in Transport.blocked_destination_types: return + if Transport.required_destinations_only: + if name_hash in Transport.allowed_destination_types: allowed = True + elif name_hash in Transport.client_destination_types: allowed = True + elif Transport.is_local_client_interface(packet.receiving_interface): allowed = True if packet.hops == 1 else False # 1 == 0 really, yes it's weird ~. . .~ + elif Transport.interface_to_shared_instance(packet.receiving_interface): allowed = True if packet.hops == 1 else False # 1 == 0 really, yes it's weird `-` + else: allowed = False + if not allowed: return + if len(raw) > RNS.Reticulum.MTU: return packet.receiving_interface.protocol_violation(f"Excessive announce packet frame size of {len(raw)} bytes") if packet.receiving_interface else None if traffic_class < Transport.TC_ANNOUNCE: traffic_class = Transport.TC_ANNOUNCE announce_signature_valid = RNS.Identity.validate_announce(packet, only_validate_signature=True, signal_blackholed=True, signal_stamp=True) @@ -4513,6 +4533,7 @@ class Transport: RNS.Identity._reset_validation_caches() Transport.remove_blackholed_paths() + @staticmethod def remove_blackholed_paths(): drop_destinations = [] for destination_hash in Transport.path_table.copy(): @@ -4562,6 +4583,87 @@ class Transport: RNS.log(f"Error while persisting blackhole list: {e}", RNS.LOG_ERROR) RNS.trace_exception(e) + ############################## + # Destination Type Filtering # + ############################## + + destination_filters_lock = threading.Lock() + + @staticmethod + def block_destination_type(full_name): + try: + name_hash = RNS.Destination.name_hash(full_name) + with Transport.destination_filters_lock: + Transport.blocked_destination_types[name_hash] = [True] + Transport.client_destination_types.pop(name_hash, None) + Transport.destination_type_map[name_hash] = full_name + RNS.log(f"Blocked {full_name} {RNS.prettyhexrep(name_hash)} destinations", RNS.LOG_DEBUG) + return True + except Exception as e: RNS.log(f"Could not add {full_name} destination type to blocked list: {e}", RNS.LOG_ERROR) + return False + + @staticmethod + def allow_destination_type(full_name): + try: + name_hash = RNS.Destination.name_hash(full_name) + with Transport.destination_filters_lock: + Transport.allowed_destination_types[name_hash] = [True] + Transport.destination_type_map[name_hash] = full_name + if Transport.required_destinations_only: RNS.log(f"Allowing {Transport.destination_type_name(name_hash)} {RNS.prettyhexrep(name_hash)} destinations", RNS.LOG_DEBUG) + return True + except Exception as e: RNS.log(f"Could not add {Transport.destination_type_name(name_hash)} destination type to allowed list: {e}", RNS.LOG_ERROR) + return False + + @staticmethod + def _wants_destination_type(full_name, requester, signature): + try: + name_hash = RNS.Destination.name_hash(full_name) + if not name_hash in Transport.blocked_destination_types: + with Transport.destination_filters_lock: + # TODO: Add requester signature validation + entry = Transport.client_destination_types.get(name_hash, [True, {}]) + entry[1][requester] = [True] + Transport.client_destination_types[name_hash] = entry + Transport.destination_type_map[name_hash] = full_name + RNS.log(f"Registered {Transport.destination_type_name(name_hash)} {RNS.prettyhexrep(name_hash)} destinations requirement for {RNS.prettyhexrep(requester)}", RNS.LOG_DEBUG) + return True + + except Exception as e: RNS.log(f"Could not add {Transport.destination_type_name(name_hash)} destination type to clients required list: {e}", RNS.LOG_ERROR) + return False + + @staticmethod + def _unwants_destination_type(full_name, requester, signature, name_hash=None): + try: + if not name_hash: name_hash = RNS.Destination.name_hash(full_name) + with Transport.destination_filters_lock: + # TODO: Add requester signature validation + entry = Transport.client_destination_types.get(name_hash, None) + if entry: + was_registered = entry[1].pop(requester, None) + if was_registered: RNS.log(f"Deregistered {Transport.destination_type_name(name_hash)} {RNS.prettyhexrep(name_hash)} destinations requirement for {RNS.prettyhexrep(requester)}", RNS.LOG_DEBUG) + if len(entry[1]) == 0: + Transport.client_destination_types.pop(name_hash, None) + RNS.log(f"No remaining requesters for {Transport.destination_type_name(name_hash)} {RNS.prettyhexrep(name_hash)} destinations, type deregistered", RNS.LOG_DEBUG) + if was_registered: return True + + except Exception as e: RNS.log(f"Could not remove {Transport.destination_type_name(name_hash)} destination type for {RNS.prettyhexrep(requester)} from clients required list: {e}", RNS.LOG_ERROR) + return False + + @staticmethod + def _unwants_all_destination_types(requester, signature): + try: + for name_hash in Transport.client_destination_types.copy(): + Transport._unwants_destination_type(None, requester, signature, name_hash=name_hash) + RNS.log(f"Deregistered all destination requirements for {RNS.prettyhexrep(requester)}", RNS.LOG_DEBUG) + return True + + except Exception as e: RNS.log(f"Could not deregister all destination types for {RNS.prettyhexrep(requester)} from clients required list: {e}", RNS.LOG_ERROR); RNS.trace_exception(e) + return False + + @staticmethod + def destination_type_name(name_hash): + return f"<{Transport.destination_type_map.get(name_hash, 'unknown')}>" + ################### # Various Helpers # ################### diff --git a/RNS/Utilities/rncp.py b/RNS/Utilities/rncp.py index e6f004ae..e56baacf 100644 --- a/RNS/Utilities/rncp.py +++ b/RNS/Utilities/rncp.py @@ -387,6 +387,7 @@ def fetch(configdir, identitypath = None, verbosity = 0, quietness = 0, destinat RNS.exit(1) reticulum = RNS.Reticulum(configdir=configdir, loglevel=targetloglevel) + reticulum.wants_destination_type(f"{APP_NAME}.receive") if identity == None: prepare_identity(identitypath) @@ -643,6 +644,7 @@ def send(configdir, identitypath = None, verbosity = 0, quietness = 0, destinati print(f"{erase_str}", end="") reticulum = RNS.Reticulum(configdir=configdir, loglevel=targetloglevel) + reticulum.wants_destination_type(f"{APP_NAME}.receive") if identity == None: prepare_identity(identitypath) diff --git a/RNS/Utilities/rngit/client.py b/RNS/Utilities/rngit/client.py index 454bea20..bc2681ee 100644 --- a/RNS/Utilities/rngit/client.py +++ b/RNS/Utilities/rngit/client.py @@ -154,6 +154,8 @@ class ReticulumGitClient(): print(f"Failed to initialize Reticulum: {e}", file=sys.stderr) return + self.reticulum.wants_destination_type(f"{APP_NAME}.repositories") + self.__apply_config() self.ready = True diff --git a/RNS/Utilities/rngit/server.py b/RNS/Utilities/rngit/server.py index 90cd9dc1..0eb609c1 100644 --- a/RNS/Utilities/rngit/server.py +++ b/RNS/Utilities/rngit/server.py @@ -57,14 +57,15 @@ def program_setup(configdir, rnsconfigdir=None, verbosity=0, quietness=0, servic targetverbosity = verbosity-quietness if service: - targetlogdest = RNS.LOG_FILE + targetlogdest = RNS.LOG_FILE targetverbosity = None else: - targetlogdest = RNS.LOG_STDOUT + targetlogdest = RNS.LOG_STDOUT if print_identity: git_node = ReticulumGitNode(configdir=configdir, print_identity=True) - reticulum = RNS.Reticulum(configdir=rnsconfigdir, verbosity=targetverbosity, logdest=targetlogdest) + reticulum = RNS.Reticulum(configdir=rnsconfigdir, verbosity=targetverbosity, logdest=targetlogdest) + reticulum.wants_destination_type(f"{APP_NAME}.repositories") if not task: RNS.log("Starting Reticulum Git Node...", RNS.LOG_NOTICE) diff --git a/RNS/Utilities/rnpath.py b/RNS/Utilities/rnpath.py index 8060eae9..a7bae9ef 100644 --- a/RNS/Utilities/rnpath.py +++ b/RNS/Utilities/rnpath.py @@ -106,6 +106,8 @@ def program_setup(configdir, table, rates, drop, destination_hexhash, verbosity, global remote_link, reticulum reticulum = RNS.Reticulum(configdir = configdir, loglevel = 3+verbosity) + reticulum.wants_destination_type("rnstransport.remote.management") + reticulum.wants_destination_type("rnstransport.info.blackhole") if remote: try: dest_len = (RNS.Reticulum.TRUNCATED_HASHLENGTH//8)*2 diff --git a/RNS/Utilities/rnsh/initiator.py b/RNS/Utilities/rnsh/initiator.py index 25cccfef..a29a7cb3 100644 --- a/RNS/Utilities/rnsh/initiator.py +++ b/RNS/Utilities/rnsh/initiator.py @@ -153,6 +153,8 @@ async def _initiate_link(configdir, rnsconfigdir, identitypath=None, logfile=Non RNS.logfile = logfile _reticulum = RNS.Reticulum(configdir=rnsconfigdir, loglevel=targetloglevel, logdest=RNS.LOG_FILE) + _reticulum.wants_destination_type(f"{rnsh.APP_NAME}") + if _identity is None: _identity = rnsh.prepare_identity(identity_path=identitypath, service_name=None, configdir=configdir) diff --git a/RNS/Utilities/rnstatus.py b/RNS/Utilities/rnstatus.py index 1f3ef77c..4107f247 100644 --- a/RNS/Utilities/rnstatus.py +++ b/RNS/Utilities/rnstatus.py @@ -171,6 +171,7 @@ def program_setup(configdir, dispall=False, verbosity=0, name_filter=None, json= must_exit = False else: reticulum = RNS.Reticulum(configdir=configdir, loglevel=3+verbosity, require_shared_instance=require_shared) + reticulum.wants_destination_type("rnstransport.remote.management") except Exception as e: print("No shared RNS instance available to get status from") @@ -922,7 +923,9 @@ def main(must_exit=True, rns_instance=None): if args.R: require_shared = False else: require_shared = True - try: reticulum = RNS.Reticulum(configdir=configarg, loglevel=3+args.verbose, require_shared_instance=require_shared) + try: + reticulum = RNS.Reticulum(configdir=configarg, loglevel=3+args.verbose, require_shared_instance=require_shared) + reticulum.wants_destination_type("rnstransport.remote.management") except Exception as e: print("No shared RNS instance available to get status from") exit(1) diff --git a/RNS/Utilities/rnx.py b/RNS/Utilities/rnx.py index 4623c595..3d694ce9 100644 --- a/RNS/Utilities/rnx.py +++ b/RNS/Utilities/rnx.py @@ -342,6 +342,8 @@ def execute(configdir, identitypath = None, verbosity = 0, quietness = 0, detail targetloglevel = 3+verbosity-quietness reticulum = RNS.Reticulum(configdir=configdir, loglevel=targetloglevel) + reticulum.wants_destination_type(f"{APP_NAME}.execute") + if identity == None: prepare_identity(identitypath)