Implemented app_data recall from announces, better destination registration handling and link inactivity querying.

This commit is contained in:
Mark Qvist 2021-05-13 16:41:23 +02:00
parent 54206d9101
commit 51ab2d3488
4 changed files with 28 additions and 8 deletions

View File

@ -43,12 +43,24 @@ class Identity:
identity_data = Identity.known_destinations[destination_hash] identity_data = Identity.known_destinations[destination_hash]
identity = Identity(public_only=True) identity = Identity(public_only=True)
identity.loadPublicKey(identity_data[2]) identity.loadPublicKey(identity_data[2])
identity.app_data = identity_data[3]
RNS.log("Found "+RNS.prettyhexrep(destination_hash)+" in known destinations", RNS.LOG_EXTREME) RNS.log("Found "+RNS.prettyhexrep(destination_hash)+" in known destinations", RNS.LOG_EXTREME)
return identity return identity
else: else:
RNS.log("Could not find "+RNS.prettyhexrep(destination_hash)+" in known destinations", RNS.LOG_EXTREME) RNS.log("Could not find "+RNS.prettyhexrep(destination_hash)+" in known destinations", RNS.LOG_EXTREME)
return None return None
@staticmethod
def recall_app_data(destination_hash):
RNS.log("Searching for app_data for "+RNS.prettyhexrep(destination_hash)+"...", RNS.LOG_EXTREME)
if destination_hash in Identity.known_destinations:
app_data = Identity.known_destinations[destination_hash][3]
RNS.log("Found "+RNS.prettyhexrep(destination_hash)+" app_data in known destinations", RNS.LOG_EXTREME)
return app_data
else:
RNS.log("Could not find "+RNS.prettyhexrep(destination_hash)+" app_data in known destinations", RNS.LOG_EXTREME)
return None
@staticmethod @staticmethod
def saveKnownDestinations(): def saveKnownDestinations():
RNS.log("Saving known destinations to storage...", RNS.LOG_VERBOSE) RNS.log("Saving known destinations to storage...", RNS.LOG_VERBOSE)
@ -79,10 +91,6 @@ class Identity:
@staticmethod @staticmethod
def truncatedHash(data): def truncatedHash(data):
# TODO: Remove
# digest = hashes.Hash(hashes.SHA256(), backend=default_backend())
# digest.update(data)
return Identity.fullHash(data)[:(Identity.TRUNCATED_HASHLENGTH//8)] return Identity.fullHash(data)[:(Identity.TRUNCATED_HASHLENGTH//8)]
@staticmethod @staticmethod
@ -103,11 +111,14 @@ class Identity:
signed_data = destination_hash+public_key+random_hash+app_data signed_data = destination_hash+public_key+random_hash+app_data
if not len(packet.data) > Identity.DERKEYSIZE//8+20+Identity.KEYSIZE//8:
app_data = None
announced_identity = Identity(public_only=True) announced_identity = Identity(public_only=True)
announced_identity.loadPublicKey(public_key) announced_identity.loadPublicKey(public_key)
if announced_identity.pub != None and announced_identity.validate(signature, signed_data): if announced_identity.pub != None and announced_identity.validate(signature, signed_data):
RNS.Identity.remember(packet.getHash(), destination_hash, public_key) RNS.Identity.remember(packet.getHash(), destination_hash, public_key, app_data)
RNS.log("Stored valid announce from "+RNS.prettyhexrep(destination_hash), RNS.LOG_DEBUG) RNS.log("Stored valid announce from "+RNS.prettyhexrep(destination_hash), RNS.LOG_DEBUG)
del announced_identity del announced_identity
return True return True

View File

@ -238,6 +238,9 @@ class Link:
def getContext(self): def getContext(self):
return None return None
def inactive_for(self):
return min(time.time() - self.last_inbound, time.time() - self.last_outbound)
def teardown(self): def teardown(self):
if self.status != Link.PENDING and self.status != Link.CLOSED: if self.status != Link.PENDING and self.status != Link.CLOSED:
teardown_packet = RNS.Packet(self, self.link_id, context=RNS.Packet.LINKCLOSE) teardown_packet = RNS.Packet(self, self.link_id, context=RNS.Packet.LINKCLOSE)
@ -331,6 +334,7 @@ class Link:
if sleep_time == None or sleep_time < 0: if sleep_time == None or sleep_time < 0:
RNS.log("Timing error! Tearing down link "+str(self)+" now.", RNS.LOG_ERROR) RNS.log("Timing error! Tearing down link "+str(self)+" now.", RNS.LOG_ERROR)
self.teardown() self.teardown()
sleep_time = 0.1
sleep(sleep_time) sleep(sleep_time)
@ -454,7 +458,10 @@ class Link:
return plaintext return plaintext
except Exception as e: except Exception as e:
RNS.log("Decryption failed on link "+str(self)+". The contained exception was: "+str(e), RNS.LOG_ERROR) RNS.log("Decryption failed on link "+str(self)+". The contained exception was: "+str(e), RNS.LOG_ERROR)
traceback.print_exc() RNS.log(traceback.format_exc(), RNS.LOG_ERROR)
# TODO: Do we really need to do this? Or can we recover somehow?
self.teardown()
def sign(self, message): def sign(self, message):
return self.prv.sign(message, ec.ECDSA(hashes.SHA256())) return self.prv.sign(message, ec.ECDSA(hashes.SHA256()))

View File

@ -111,8 +111,6 @@ class Resource:
return resource return resource
except Exception as e: except Exception as e:
RNS.log("Could not decode resource advertisement, dropping resource", RNS.LOG_DEBUG) RNS.log("Could not decode resource advertisement, dropping resource", RNS.LOG_DEBUG)
# TODO: Remove
raise e
return None return None
# Create a resource for transmission to a remote destination # Create a resource for transmission to a remote destination

View File

@ -446,6 +446,7 @@ class Transport:
@staticmethod @staticmethod
def inbound(raw, interface=None): def inbound(raw, interface=None):
while (Transport.jobs_running): while (Transport.jobs_running):
# TODO: Decrease this for performance
sleep(0.1) sleep(0.1)
Transport.jobs_locked = True Transport.jobs_locked = True
@ -849,6 +850,9 @@ class Transport:
def registerDestination(destination): def registerDestination(destination):
destination.MTU = RNS.Reticulum.MTU destination.MTU = RNS.Reticulum.MTU
if destination.direction == RNS.Destination.IN: if destination.direction == RNS.Destination.IN:
for registered_destination in Transport.destinations:
if destination.hash == registered_destination.hash:
raise KeyError("Attempt to register an already registered destination.")
Transport.destinations.append(destination) Transport.destinations.append(destination)
@staticmethod @staticmethod