From ccfbe8da4777889e8a3c93865adb78ced8de0b8e Mon Sep 17 00:00:00 2001 From: bmng-dev Date: Thu, 22 May 2014 13:08:30 +0000 Subject: [PATCH] Refactor handling of packet headers Refactored ackDataHasAVaildHeader: -shared.Header is used as necessary -avoided slicing wherever possible -remove trailing null characters when comparing command strings -don't calculate the checksum of a large payload --- src/class_objectProcessor.py | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/src/class_objectProcessor.py b/src/class_objectProcessor.py index 20ef31ff..717db9c6 100644 --- a/src/class_objectProcessor.py +++ b/src/class_objectProcessor.py @@ -1149,25 +1149,28 @@ class objectProcessor(threading.Thread): shared.workerQueue.put(('sendmessage', '')) def ackDataHasAVaildHeader(self, ackData): - if len(ackData) < 24: + if len(ackData) < shared.Header.size: logger.info('The length of ackData is unreasonably short. Not sending ackData.') return False - if ackData[0:4] != '\xe9\xbe\xb4\xd9': + + magic,command,payload_length,checksum = shared.Header.unpack(ackData[:shared.Header.size]) + if magic != 0xE9BEB4D9: logger.info('Ackdata magic bytes were wrong. Not sending ackData.') return False - ackDataPayloadLength, = unpack('>L', ackData[16:20]) - if len(ackData) - 24 != ackDataPayloadLength: + payload = ackData[shared.Header.size:] + if len(payload) != payload_length: logger.info('ackData payload length doesn\'t match the payload length specified in the header. Not sending ackdata.') return False - if ackData[20:24] != hashlib.sha512(ackData[24:]).digest()[0:4]: # test the checksum in the message. + if payload_length > 180000000: # If the size of the message is greater than 180MB, ignore it. + return False + if checksum != hashlib.sha512(payload).digest()[0:4]: # test the checksum in the message. logger.info('ackdata checksum wrong. Not sending ackdata.') return False - if ackDataPayloadLength > 180000000: # If the size of the message is greater than 180MB, ignore it. - return False - if (ackData[4:16] != 'getpubkey\x00\x00\x00' and - ackData[4:16] != 'pubkey\x00\x00\x00\x00\x00\x00' and - ackData[4:16] != 'msg\x00\x00\x00\x00\x00\x00\x00\x00\x00' and - ackData[4:16] != 'broadcast\x00\x00\x00'): + command = command.rstrip('\x00') + if (command != 'getpubkey' and + command != 'pubkey' and + command != 'msg' and + command != 'broadcast'): return False return True @@ -1210,4 +1213,4 @@ class objectProcessor(threading.Thread): # Throw away any extra lines (headers) after the subject. if subject: subject = subject.splitlines()[0] - return subject, body \ No newline at end of file + return subject, body