From cf87b1352ababfc8f55ecf85a723e20d1ce343e1 Mon Sep 17 00:00:00 2001 From: Mark Qvist Date: Thu, 29 Aug 2024 11:17:07 +0200 Subject: [PATCH] Added max hops filter to rnpath table output --- RNS/Reticulum.py | 27 +++++++++++++++------------ RNS/Utilities/rnpath.py | 15 +++++++++++++-- 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/RNS/Reticulum.py b/RNS/Reticulum.py index 2a335e7..94e3b0e 100755 --- a/RNS/Reticulum.py +++ b/RNS/Reticulum.py @@ -1194,7 +1194,8 @@ class Reticulum: rpc_connection.send(self.get_interface_stats()) if path == "path_table": - rpc_connection.send(self.get_path_table()) + mh = call["max_hops"] + rpc_connection.send(self.get_path_table(max_hops=mh)) if path == "rate_table": rpc_connection.send(self.get_rate_table()) @@ -1340,25 +1341,27 @@ class Reticulum: return stats - def get_path_table(self): + def get_path_table(self, max_hops=None): if self.is_connected_to_shared_instance: rpc_connection = multiprocessing.connection.Client(self.rpc_addr, authkey=self.rpc_key) - rpc_connection.send({"get": "path_table"}) + rpc_connection.send({"get": "path_table", "max_hops": max_hops}) response = rpc_connection.recv() return response else: path_table = [] for dst_hash in RNS.Transport.destination_table: - entry = { - "hash": dst_hash, - "timestamp": RNS.Transport.destination_table[dst_hash][0], - "via": RNS.Transport.destination_table[dst_hash][1], - "hops": RNS.Transport.destination_table[dst_hash][2], - "expires": RNS.Transport.destination_table[dst_hash][3], - "interface": str(RNS.Transport.destination_table[dst_hash][5]), - } - path_table.append(entry) + path_hops = RNS.Transport.destination_table[dst_hash][2] + if max_hops == None or path_hops <= max_hops: + entry = { + "hash": dst_hash, + "timestamp": RNS.Transport.destination_table[dst_hash][0], + "via": RNS.Transport.destination_table[dst_hash][1], + "hops": path_hops, + "expires": RNS.Transport.destination_table[dst_hash][3], + "interface": str(RNS.Transport.destination_table[dst_hash][5]), + } + path_table.append(entry) return path_table diff --git a/RNS/Utilities/rnpath.py b/RNS/Utilities/rnpath.py index cf20bfa..2441bb0 100644 --- a/RNS/Utilities/rnpath.py +++ b/RNS/Utilities/rnpath.py @@ -30,7 +30,7 @@ import argparse from RNS._version import __version__ -def program_setup(configdir, table, rates, drop, destination_hexhash, verbosity, timeout, drop_queues, drop_via): +def program_setup(configdir, table, rates, drop, destination_hexhash, verbosity, timeout, drop_queues, drop_via, max_hops): if table: destination_hash = None if destination_hexhash != None: @@ -47,7 +47,7 @@ def program_setup(configdir, table, rates, drop, destination_hexhash, verbosity, sys.exit(1) reticulum = RNS.Reticulum(configdir = configdir, loglevel = 3+verbosity) - table = sorted(reticulum.get_path_table(), key=lambda e: (e["interface"], e["hops"]) ) + table = sorted(reticulum.get_path_table(max_hops=max_hops), key=lambda e: (e["interface"], e["hops"]) ) displayed = 0 for path in table: @@ -255,6 +255,16 @@ def main(): default=False ) + parser.add_argument( + "-m", + "--max", + action="store", + metavar="hops", + type=int, + help="maximum hops to filter path table by", + default=None + ) + parser.add_argument( "-r", "--rates", @@ -327,6 +337,7 @@ def main(): timeout = args.w, drop_queues = args.drop_announces, drop_via = args.drop_via, + max_hops = args.max, ) sys.exit(0)