From c9d1c938ffed3f0a24372e413643c3ff93a0f978 Mon Sep 17 00:00:00 2001 From: Mark Qvist Date: Tue, 28 Apr 2020 20:50:57 +0200 Subject: [PATCH] Implemented progress on resource initiator side. Made MDUs more obvious. --- Examples/Filetransfer.py | 2 +- RNS/Identity.py | 7 +++++-- RNS/Link.py | 9 ++++++--- RNS/Packet.py | 11 +++++++++-- RNS/Resource.py | 14 +++++++++++--- RNS/Reticulum.py | 4 +--- RNS/__init__.py | 2 +- 7 files changed, 34 insertions(+), 15 deletions(-) diff --git a/Examples/Filetransfer.py b/Examples/Filetransfer.py index da20488..1647f34 100644 --- a/Examples/Filetransfer.py +++ b/Examples/Filetransfer.py @@ -89,7 +89,7 @@ def client_connected(link): data = umsgpack.packb(list_files()) # Check the size of the packed data - if len(data) <= RNS.Reticulum.LINK_MDU: + if len(data) <= RNS.Link.MDU: # If it fits in one packet, we will just # send it as a single packet over the link. list_packet = RNS.Packet(link, data) diff --git a/RNS/Identity.py b/RNS/Identity.py index 4709481..a3983ad 100644 --- a/RNS/Identity.py +++ b/RNS/Identity.py @@ -23,6 +23,9 @@ class Identity: HASHLENGTH = 256 # In bits SIGLENGTH = KEYSIZE + ENCRYPT_CHUNKSIZE = (KEYSIZE-PADDINGSIZE)//8 + DECRYPT_CHUNKSIZE = KEYSIZE//8 + TRUNCATED_HASHLENGTH = 80 # In bits # Storage @@ -222,7 +225,7 @@ class Identity: def encrypt(self, plaintext): if self.pub != None: - chunksize = (Identity.KEYSIZE-Identity.PADDINGSIZE)//8 + chunksize = Identity.ENCRYPT_CHUNKSIZE chunks = int(math.ceil(len(plaintext)/(float(chunksize)))) ciphertext = b""; @@ -249,7 +252,7 @@ class Identity: if self.prv != None: plaintext = None try: - chunksize = (Identity.KEYSIZE)//8 + chunksize = Identity.DECRYPT_CHUNKSIZE chunks = int(math.ceil(len(ciphertext)/(float(chunksize)))) plaintext = b""; diff --git a/RNS/Link.py b/RNS/Link.py index 6eb2496..af92b15 100644 --- a/RNS/Link.py +++ b/RNS/Link.py @@ -8,6 +8,7 @@ from time import sleep from .vendor import umsgpack as umsgpack import threading import base64 +import math import time import RNS @@ -25,6 +26,8 @@ class Link: CURVE = ec.SECP256R1() ECPUBSIZE = 91 BLOCKSIZE = 16 + AES_HMAC_OVERHEAD = 58 + MDU = math.floor((RNS.Reticulum.MDU-AES_HMAC_OVERHEAD)/BLOCKSIZE)*BLOCKSIZE - 1 # TODO: This should not be hardcoded, # but calculated from something like @@ -44,8 +47,8 @@ class Link: DESTINATION_CLOSED = 0x03 ACCEPT_NONE = 0x00 - ACCEPT_APP = 0x01 - ACCEPT_ALL = 0x02 + ACCEPT_APP = 0x01 + ACCEPT_ALL = 0x02 resource_strategies = [ACCEPT_NONE, ACCEPT_APP, ACCEPT_ALL] @staticmethod @@ -69,7 +72,7 @@ class Link: #if self.owner.callbacks.link_established != None: # self.owner.callbacks.link_established(link) - RNS.log("Incoming link request "+str(link)+" accepted, waiting for RTT packet", RNS.LOG_VERBOSE) + RNS.log("Incoming link request "+str(link)+" accepted", RNS.LOG_VERBOSE) return link except Exception as e: diff --git a/RNS/Packet.py b/RNS/Packet.py index cac13cd..c6cfad2 100755 --- a/RNS/Packet.py +++ b/RNS/Packet.py @@ -1,4 +1,5 @@ import struct +import math import time import RNS @@ -40,11 +41,17 @@ class Packet: # This is used to calculate allowable # payload sizes - HEADER_MAXSIZE = RNS.Reticulum.HEADER_MAXSIZE + HEADER_MAXSIZE = 23 + MDU = RNS.Reticulum.MDU + + # With an MTU of 500, the maximum RSA-encrypted + # amount of data we can send in a single packet + # is given by the below calculation; 258 bytes. + RSA_MDU = math.floor(MDU/RNS.Identity.DECRYPT_CHUNKSIZE)*RNS.Identity.ENCRYPT_CHUNKSIZE + PLAIN_MDU = MDU # TODO: This should be calculated # more intelligently - # Default packet timeout TIMEOUT = 60 diff --git a/RNS/Resource.py b/RNS/Resource.py index b767256..22c50b7 100644 --- a/RNS/Resource.py +++ b/RNS/Resource.py @@ -11,7 +11,7 @@ class Resource: WINDOW_MAX = 7 WINDOW = 4 MAPHASH_LEN = 4 - SDU = RNS.Reticulum.MTU - RNS.Packet.HEADER_MAXSIZE + SDU = RNS.Packet.MDU RANDOM_HASH_SIZE = 4 # TODO: Should be allocated more @@ -89,6 +89,7 @@ class Resource: self.hmu_retry_ok = False self.watchdog_lock = False self.__watchdog_job_id = 0 + self.__progress_callback = progress_callback self.rtt = None if data != None: @@ -96,7 +97,8 @@ class Resource: while not hashmap_ok: self.initiator = True self.callback = callback - self.progress_callback = progress_callback + # TODO: Remove + #self.progress_callback = progress_callback self.random_hash = RNS.Identity.getRandomHash()[:Resource.RANDOM_HASH_SIZE] self.uncompressed_data = data self.compressed_data = bz2.compress(self.uncompressed_data) @@ -478,6 +480,9 @@ class Resource: if self.sent_parts == len(self.parts): self.status = Resource.AWAITING_PROOF + if self.__progress_callback != None: + self.__progress_callback(self) + def cancel(self): if self.status < Resource.COMPLETE: self.status = Resource.FAILED @@ -497,7 +502,10 @@ class Resource: self.__progress_callback = callback def progress(self): - progress = self.received_count / float(self.total_parts) + if self.initiator: + progress = self.sent_parts / len(self.parts) + else: + progress = self.received_count / float(self.total_parts) return progress def __str__(self): diff --git a/RNS/Reticulum.py b/RNS/Reticulum.py index b971c24..c829f85 100755 --- a/RNS/Reticulum.py +++ b/RNS/Reticulum.py @@ -14,10 +14,8 @@ import RNS class Reticulum: MTU = 500 HEADER_MAXSIZE = 23 - - PAD_AES_HMAC = 64 MDU = MTU - HEADER_MAXSIZE - LINK_MDU = MDU - PAD_AES_HMAC + router = None config = None diff --git a/RNS/__init__.py b/RNS/__init__.py index 3cbcd52..033e20d 100755 --- a/RNS/__init__.py +++ b/RNS/__init__.py @@ -77,7 +77,7 @@ def hexrep(data, delimit=True): delimiter = ":" if not delimit: delimiter = "" - hexrep = delimiter.join("{:02x}".format(ord(c)) for c in data) + hexrep = delimiter.join("{:02x}".format(c) for c in data) return hexrep def prettyhexrep(data):