Improved link error handling. Fixes #387.

This commit is contained in:
Mark Qvist 2023-10-31 11:44:12 +01:00
parent 3d645ae2f4
commit a4143cfe6d

View File

@ -392,6 +392,7 @@ class Link:
try: try:
measured_rtt = time.time() - self.request_time measured_rtt = time.time() - self.request_time
plaintext = self.decrypt(packet.data) plaintext = self.decrypt(packet.data)
if plaintext != None:
rtt = umsgpack.unpackb(plaintext) rtt = umsgpack.unpackb(plaintext)
self.rtt = max(measured_rtt, rtt) self.rtt = max(measured_rtt, rtt)
self.status = Link.ACTIVE self.status = Link.ACTIVE
@ -748,6 +749,7 @@ class Link:
should_query = False should_query = False
if packet.context == RNS.Packet.NONE: if packet.context == RNS.Packet.NONE:
plaintext = self.decrypt(packet.data) plaintext = self.decrypt(packet.data)
if plaintext != None:
if self.callbacks.packet != None: if self.callbacks.packet != None:
thread = threading.Thread(target=self.callbacks.packet, args=(plaintext, packet)) thread = threading.Thread(target=self.callbacks.packet, args=(plaintext, packet))
thread.daemon = True thread.daemon = True
@ -770,7 +772,7 @@ class Link:
elif packet.context == RNS.Packet.LINKIDENTIFY: elif packet.context == RNS.Packet.LINKIDENTIFY:
plaintext = self.decrypt(packet.data) plaintext = self.decrypt(packet.data)
if plaintext != None:
if not self.initiator and len(plaintext) == RNS.Identity.KEYSIZE//8 + RNS.Identity.SIGLENGTH//8: if not self.initiator and len(plaintext) == RNS.Identity.KEYSIZE//8 + RNS.Identity.SIGLENGTH//8:
public_key = plaintext[:RNS.Identity.KEYSIZE//8] public_key = plaintext[:RNS.Identity.KEYSIZE//8]
signed_data = self.link_id+public_key signed_data = self.link_id+public_key
@ -792,6 +794,7 @@ class Link:
try: try:
request_id = packet.getTruncatedHash() request_id = packet.getTruncatedHash()
packed_request = self.decrypt(packet.data) packed_request = self.decrypt(packet.data)
if packed_request != None:
unpacked_request = umsgpack.unpackb(packed_request) unpacked_request = umsgpack.unpackb(packed_request)
self.handle_request(request_id, unpacked_request) self.handle_request(request_id, unpacked_request)
self.__update_phy_stats(packet, query_shared=True) self.__update_phy_stats(packet, query_shared=True)
@ -801,6 +804,7 @@ class Link:
elif packet.context == RNS.Packet.RESPONSE: elif packet.context == RNS.Packet.RESPONSE:
try: try:
packed_response = self.decrypt(packet.data) packed_response = self.decrypt(packet.data)
if packed_response != None:
unpacked_response = umsgpack.unpackb(packed_response) unpacked_response = umsgpack.unpackb(packed_response)
request_id = unpacked_response[0] request_id = unpacked_response[0]
response_data = unpacked_response[1] response_data = unpacked_response[1]
@ -821,6 +825,7 @@ class Link:
elif packet.context == RNS.Packet.RESOURCE_ADV: elif packet.context == RNS.Packet.RESOURCE_ADV:
packet.plaintext = self.decrypt(packet.data) packet.plaintext = self.decrypt(packet.data)
if packet.plaintext != None:
self.__update_phy_stats(packet, query_shared=True) self.__update_phy_stats(packet, query_shared=True)
if RNS.ResourceAdvertisement.is_request(packet): if RNS.ResourceAdvertisement.is_request(packet):
@ -849,6 +854,7 @@ class Link:
elif packet.context == RNS.Packet.RESOURCE_REQ: elif packet.context == RNS.Packet.RESOURCE_REQ:
plaintext = self.decrypt(packet.data) plaintext = self.decrypt(packet.data)
if plaintext != None:
self.__update_phy_stats(packet, query_shared=True) self.__update_phy_stats(packet, query_shared=True)
if ord(plaintext[:1]) == RNS.Resource.HASHMAP_IS_EXHAUSTED: if ord(plaintext[:1]) == RNS.Resource.HASHMAP_IS_EXHAUSTED:
resource_hash = plaintext[1+RNS.Resource.MAPHASH_LEN:RNS.Identity.HASHLENGTH//8+1+RNS.Resource.MAPHASH_LEN] resource_hash = plaintext[1+RNS.Resource.MAPHASH_LEN:RNS.Identity.HASHLENGTH//8+1+RNS.Resource.MAPHASH_LEN]
@ -865,6 +871,7 @@ class Link:
elif packet.context == RNS.Packet.RESOURCE_HMU: elif packet.context == RNS.Packet.RESOURCE_HMU:
plaintext = self.decrypt(packet.data) plaintext = self.decrypt(packet.data)
if plaintext != None:
self.__update_phy_stats(packet, query_shared=True) self.__update_phy_stats(packet, query_shared=True)
resource_hash = plaintext[:RNS.Identity.HASHLENGTH//8] resource_hash = plaintext[:RNS.Identity.HASHLENGTH//8]
for resource in self.incoming_resources: for resource in self.incoming_resources:
@ -873,6 +880,7 @@ class Link:
elif packet.context == RNS.Packet.RESOURCE_ICL: elif packet.context == RNS.Packet.RESOURCE_ICL:
plaintext = self.decrypt(packet.data) plaintext = self.decrypt(packet.data)
if plaintext != None:
self.__update_phy_stats(packet) self.__update_phy_stats(packet)
resource_hash = plaintext[:RNS.Identity.HASHLENGTH//8] resource_hash = plaintext[:RNS.Identity.HASHLENGTH//8]
for resource in self.incoming_resources: for resource in self.incoming_resources:
@ -909,11 +917,13 @@ class Link:
# else: # else:
# packet.prove() # packet.prove()
# plaintext = self.decrypt(packet.data) # plaintext = self.decrypt(packet.data)
# if plaintext != None:
# self._channel._receive(plaintext) # self._channel._receive(plaintext)
############################################ ############################################
packet.prove() packet.prove()
plaintext = self.decrypt(packet.data) plaintext = self.decrypt(packet.data)
if plaintext != None:
self.__update_phy_stats(packet) self.__update_phy_stats(packet)
self._channel._receive(plaintext) self._channel._receive(plaintext)
@ -953,6 +963,7 @@ class Link:
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)
return None
def sign(self, message): def sign(self, message):