diff --git a/RNS/Discovery.py b/RNS/Discovery.py index 2811e630..fd69c3c0 100644 --- a/RNS/Discovery.py +++ b/RNS/Discovery.py @@ -51,6 +51,8 @@ NAME = 0xFF TRANSPORT_ID = 0xFE TRANSPORT_IMPL = 0xFD TRANSPORT_VERS = 0xFC +DST_COST = 0xFB +PR_COST = 0xFA INTERFACE_TYPE = 0x00 TRANSPORT = 0x01 REACHABLE_ON = 0x02 @@ -166,6 +168,8 @@ class InterfaceAnnouncer(): TRANSPORT_ID: RNS.Transport.identity.hash, TRANSPORT_IMPL: IMPLEMENTATION_NAME, TRANSPORT_VERS: RNS_VERSION, + DST_COST: RNS.Stamps.quantize_cost(interface.destination_cost), + PR_COST: RNS.Stamps.quantize_cost(interface.pr_cost), NAME: self.sanitize(interface.discovery_name), LATITUDE: interface.discovery_latitude, LONGITUDE: interface.discovery_longitude, @@ -338,6 +342,8 @@ class InterfaceAnnounceHandler: name = self.sanitize_name(unpacked[NAME]) impl_name = unpacked[TRANSPORT_IMPL] if TRANSPORT_IMPL in unpacked else None impl_version = unpacked[TRANSPORT_VERS] if TRANSPORT_VERS in unpacked else None + dst_cost = RNS.Stamps.quantize_cost(unpacked[DST_COST]) if DST_COST in unpacked else None + pr_cost = RNS.Stamps.quantize_cost(unpacked[PR_COST]) if PR_COST in unpacked else None if type(unpacked[TRANSPORT]) != bool: raise ValueError("Invalid data in transport field of announce") if type(unpacked[LATITUDE]) not in [type(None), float]: raise ValueError("Invalid data in latitude field of announce") @@ -361,6 +367,8 @@ class InterfaceAnnounceHandler: "value": value, "transport_id": RNS.hexrep(unpacked[TRANSPORT_ID], delimit=False), "network_id": RNS.hexrep(announced_identity.hash, delimit=False), + "dst_cost": dst_cost, + "pr_cost": pr_cost, "hops": hops, "latitude": unpacked[LATITUDE], "longitude": unpacked[LONGITUDE], @@ -457,7 +465,7 @@ class InterfaceAnnounceHandler: if self.callback and callable(self.callback): self.callback(info) except Exception as e: - RNS.log(f"An error occurred while trying to decode discovered interface. The contained exception was: {e}", RNS.LOG_DEBUG) + RNS.log(f"Could not decode discovered interface. The contained exception was: {e}", RNS.LOG_DEBUG) class InterfaceDiscovery(): THRESHOLD_UNKNOWN = 24*60*60 @@ -475,7 +483,7 @@ class InterfaceDiscovery(): DISCOVERABLE_TYPES = ["BackboneInterface", "TCPServerInterface", "I2PInterface", "RNodeInterface", "WeaveInterface", "KISSInterface"] AUTOCONNECT_IMPLS = ["RNS"] - AUTOCONNECT_MIN_V = "1.5.2" + AUTOCONNECT_MIN_V = "1.5.7" AC_TRANSPORT_MODE = RNS.Interfaces.Interface.Interface.MODE_GATEWAY AC_GRAVITY = 0 @@ -787,12 +795,24 @@ class InterfaceDiscovery(): autoconnected_count = self.autoconnect_count() if autoconnected_count < RNS.Reticulum.max_autoconnected_interfaces(): interface_type = info["type"] + has_cost_info = "dst_cost" in info and "pr_cost" in info if interface_type in self.AUTOCONNECT_TYPES: + impl = f"{info['impl_name']} {info['version']}" if ("impl_name" in info and "version" in info and info["impl_name"] and info["version"]) else "unknown implementation" if not self.autoconnect_qualified(info): - impl = f"{info['impl_name']} {info['version']}" if ("impl_name" in info and "version" in info and info["impl_name"] and info["version"]) else "unknown implementation" RNS.log(f"Not auto-connecting discovered {interface_type} {info['name']} ({impl}), auto-connect criteria not satisfied", RNS.LOG_DEBUG) return + if has_cost_info: + dst_value = RNS.Stamps.quantize_cost(RNS.Transport.DESTINATION_VALUE) + pr_value = RNS.Stamps.quantize_cost(RNS.Transport.PR_VALUE) + dst_cost = RNS.Stamps.quantize_cost(info["dst_cost"] or 0) + pr_cost = RNS.Stamps.quantize_cost(info["pr_cost"] or 0) + dst_cost_ok = dst_cost <= dst_value + pr_cost_ok = pr_cost <= pr_value + if not dst_cost_ok: RNS.log(f"Destination cost {dst_cost} higher than configured value {dst_value} for {interface_type} {info['name']} ({impl}), not auto-connecting", RNS.LOG_DEBUG) + if not pr_cost_ok: RNS.log(f"Path request cost {pr_cost} higher than configured value {pr_value} for {interface_type} {info['name']} ({impl}), not auto-connecting", RNS.LOG_DEBUG) + if not dst_cost_ok or not pr_cost_ok: return + endpoint_hash = self.endpoint_hash(info) exists = self.interface_exists(info) diff --git a/RNS/Identity.py b/RNS/Identity.py index 03ed5d00..f508c470 100644 --- a/RNS/Identity.py +++ b/RNS/Identity.py @@ -674,7 +674,10 @@ class Identity: @staticmethod def validate_announce(packet, only_validate_signature=False, signal_blackholed=False, signal_stamp=False, required_value=None): - if required_value == None: required_value = RNS.Transport.DESTINATION_COST + if required_value == None: + if packet.receiving_interface: required_value = packet.receiving_interface.destination_cost + else: required_value = RNS.Transport.DESTINATION_COST + try: if packet.packet_type == RNS.Packet.ANNOUNCE: keysize = Identity.KEYSIZE//8 diff --git a/RNS/Interfaces/AutoInterface.py b/RNS/Interfaces/AutoInterface.py index 610b40d8..c32ef6c4 100644 --- a/RNS/Interfaces/AutoInterface.py +++ b/RNS/Interfaces/AutoInterface.py @@ -557,6 +557,9 @@ class AutoInterface(Interface): spawned_interface.ec_pr_freq = self.ec_pr_freq spawned_interface.ic_pr_burst_freq_new = self.ic_pr_burst_freq_new spawned_interface.ic_pr_burst_freq = self.ic_pr_burst_freq + + spawned_interface._destination_cost = self._destination_cost + spawned_interface._pr_cost = self._pr_cost spawned_interface.parent_interface = self spawned_interface.bitrate = self.bitrate diff --git a/RNS/Interfaces/BackboneInterface.py b/RNS/Interfaces/BackboneInterface.py index 17ffed89..d94ada7f 100644 --- a/RNS/Interfaces/BackboneInterface.py +++ b/RNS/Interfaces/BackboneInterface.py @@ -721,6 +721,9 @@ class BackboneInterface(Interface): spawned_interface.ic_pr_burst_freq_new = self.ic_pr_burst_freq_new spawned_interface.ic_pr_burst_freq = self.ic_pr_burst_freq + spawned_interface._destination_cost = self._destination_cost + spawned_interface._pr_cost = self._pr_cost + spawned_interface.socket = socket spawned_interface.target_ip = remote_ip spawned_interface.target_port = remote_port diff --git a/RNS/Interfaces/I2PInterface.py b/RNS/Interfaces/I2PInterface.py index 0561829d..55bb40d8 100644 --- a/RNS/Interfaces/I2PInterface.py +++ b/RNS/Interfaces/I2PInterface.py @@ -817,6 +817,10 @@ class I2PInterface(Interface): peer_interface.IN = True peer_interface.parent_interface = self peer_interface.parent_count = False + + peer_interface._destination_cost = self._destination_cost + peer_interface._pr_cost = self._pr_cost + RNS.Transport.add_interface(peer_interface) def incoming_connection(self, handler): @@ -839,6 +843,9 @@ class I2PInterface(Interface): spawned_interface.ec_pr_freq = self.ec_pr_freq spawned_interface.ic_pr_burst_freq_new = self.ic_pr_burst_freq_new spawned_interface.ic_pr_burst_freq = self.ic_pr_burst_freq + + spawned_interface._destination_cost = self._destination_cost + spawned_interface._pr_cost = self._pr_cost spawned_interface.parent_interface = self spawned_interface.online = True diff --git a/RNS/Interfaces/Interface.py b/RNS/Interfaces/Interface.py index 137f92cd..722bbf94 100755 --- a/RNS/Interfaces/Interface.py +++ b/RNS/Interfaces/Interface.py @@ -174,6 +174,9 @@ class Interface: self._dp_ec_zero_ticks = 0 self._dp_ec_last_drain = time.time() + self._destination_cost = None + self._pr_cost = None + self.reports_phy_stats = False self.r_stat_rssi = None self.r_stat_snr = None @@ -343,6 +346,16 @@ class Interface: self.hop_limiter_hits += 1 return None + @property + def destination_cost(self): + if not self._destination_cost == None: return self._destination_cost + else: return RNS.Transport.DESTINATION_COST + + @property + def pr_cost(self): + if not self._pr_cost == None: return self._pr_cost + else: return RNS.Transport.PR_COST + @property def ic_burst_count(self): return None diff --git a/RNS/Interfaces/TCPInterface.py b/RNS/Interfaces/TCPInterface.py index 12d25c86..d4cfae87 100644 --- a/RNS/Interfaces/TCPInterface.py +++ b/RNS/Interfaces/TCPInterface.py @@ -605,6 +605,9 @@ class TCPServerInterface(Interface): spawned_interface.ic_pr_burst_freq_new = self.ic_pr_burst_freq_new spawned_interface.ic_pr_burst_freq = self.ic_pr_burst_freq + spawned_interface._destination_cost = self._destination_cost + spawned_interface._pr_cost = self._pr_cost + spawned_interface.target_ip = handler.client_address[0] spawned_interface.target_port = str(handler.client_address[1]) spawned_interface.parent_interface = self diff --git a/RNS/Interfaces/WeaveInterface.py b/RNS/Interfaces/WeaveInterface.py index 33d00afb..c3cdb9cb 100644 --- a/RNS/Interfaces/WeaveInterface.py +++ b/RNS/Interfaces/WeaveInterface.py @@ -933,6 +933,9 @@ class WeaveInterface(Interface): spawned_interface.ic_new_time = self.ic_new_time spawned_interface.ic_burst_penalty = self.ic_burst_penalty spawned_interface.ic_held_release_interval = self.ic_held_release_interval + + spawned_interface._destination_cost = self._destination_cost + spawned_interface._pr_cost = self._pr_cost spawned_interface.parent_interface = self spawned_interface.bitrate = self.bitrate diff --git a/RNS/Reticulum.py b/RNS/Reticulum.py index 486e97e3..c3595727 100755 --- a/RNS/Reticulum.py +++ b/RNS/Reticulum.py @@ -933,6 +933,16 @@ class Reticulum: gravity = self._default_gravity() if "gravity" in c: gravity = c.as_int("gravity") + destination_cost = None + if "destination_cost" in c: + v = c.as_float("destination_cost") + destination_cost = max(0, min(v, RNS.Transport.STAMP_COST_CAP)) + + pr_cost = None + if "pr_cost" in c: + v = c.as_float("pr_cost") + pr_cost = max(0, min(v, RNS.Transport.STAMP_COST_CAP)) + ifac_size = None if "ifac_size" in c: if c.as_int("ifac_size") >= Reticulum.IFAC_MIN_SIZE*8: ifac_size = c.as_int("ifac_size")//8 @@ -1117,6 +1127,9 @@ class Reticulum: interface.announce_rate_penalty = announce_rate_penalty interface.ingress_control = ingress_control + interface._destination_cost = destination_cost + interface._pr_cost = pr_cost + if egress_control != None: interface.egress_control = egress_control if ic_max_held_announces != None: interface.ic_max_held_announces = ic_max_held_announces if ic_burst_hold != None: interface.ic_burst_hold = ic_burst_hold diff --git a/RNS/Transport.py b/RNS/Transport.py index 842a6e11..74b696f0 100755 --- a/RNS/Transport.py +++ b/RNS/Transport.py @@ -2072,15 +2072,16 @@ class Transport: tag_valid = True if not tag_valid: - RNS.log("Ignoring duplicate path request for "+RNS.prettyhexrep(destination_hash)+" with tag "+RNS.prettyhexrep(unique_tag), RNS.LOG_EXTREME) if RNS.sl(RNS.LOG_EXTREME) else None + RNS.log(f"Ignoring duplicate path request for {RNS.prettyhexrep(destination_hash)} with tag {RNS.prettyhexrep(unique_tag)}", RNS.LOG_EXTREME) if RNS.sl(RNS.LOG_EXTREME) else None return if Transport.STAMPS_ENABLED: stamp_ok = False if ext_payload != None: if ext_payload[0] == RNS.Identity.EXTPAYLOADMARKER: + required_value = packet.receiving_interface.pr_cost stamp = ext_payload[Transport.PR_EXT_HEADERLENGTH:] - stamp_ok = Transport.validate_pr_stamp(stamp, destination_hash, tag_bytes) + stamp_ok = Transport.validate_pr_stamp(stamp, destination_hash, tag_bytes, required_value=required_value) # TODO: Remove debug block ################################################# dval = None