From 53a3d1635a3f315ec99b1a4276a91617a2e8a06c Mon Sep 17 00:00:00 2001 From: antius Date: Fri, 2 May 2014 16:46:36 +0200 Subject: [PATCH 1/2] Make addDataPadding method to avoid clutter. --- src/class_objectProcessor.py | 17 ++++++------ src/class_receiveDataThread.py | 48 +++++++++++++++++----------------- src/class_sendDataThread.py | 7 ++--- src/class_singleWorker.py | 3 ++- src/helper_generic.py | 8 ++++++ 5 files changed, 47 insertions(+), 36 deletions(-) diff --git a/src/class_objectProcessor.py b/src/class_objectProcessor.py index 20ef31ff..2b215a60 100644 --- a/src/class_objectProcessor.py +++ b/src/class_objectProcessor.py @@ -12,6 +12,7 @@ from pyelliptic.openssl import OpenSSL import highlevelcrypto from addresses import * import helper_generic +from helper_generic import addDataPadding import helper_bitcoin import helper_inbox import helper_sent @@ -659,13 +660,13 @@ class objectProcessor(threading.Thread): shared.workerQueue.put(('sendbroadcast', '')) if self.ackDataHasAVaildHeader(ackData): - if ackData[4:16] == 'getpubkey\x00\x00\x00': + if ackData[4:16] == addDataPadding('getpubkey'): shared.checkAndSharegetpubkeyWithPeers(ackData[24:]) - elif ackData[4:16] == 'pubkey\x00\x00\x00\x00\x00\x00': + elif ackData[4:16] == addDataPadding('pubkey'): shared.checkAndSharePubkeyWithPeers(ackData[24:]) - elif ackData[4:16] == 'msg\x00\x00\x00\x00\x00\x00\x00\x00\x00': + elif ackData[4:16] == addDataPadding('msg'): shared.checkAndShareMsgWithPeers(ackData[24:]) - elif ackData[4:16] == 'broadcast\x00\x00\x00': + elif ackData[4:16] == addDataPadding('broadcast'): shared.checkAndShareBroadcastWithPeers(ackData[24:]) # Display timing data @@ -1164,10 +1165,10 @@ class objectProcessor(threading.Thread): 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'): + if (ackData[4:16] != addDataPadding('getpubkey') and + ackData[4:16] != addDataPadding('pubkey') and + ackData[4:16] != addDataPadding('msg') and + ackData[4:16] != addDataPadding('broadcast')): return False return True diff --git a/src/class_receiveDataThread.py b/src/class_receiveDataThread.py index e78a7ae2..51ce9fe9 100644 --- a/src/class_receiveDataThread.py +++ b/src/class_receiveDataThread.py @@ -14,7 +14,7 @@ import sys #import highlevelcrypto from addresses import * -import helper_generic +from helper_generic import addDataPadding, isHostInPrivateIPRange #import helper_bitcoin #import helper_inbox #import helper_sent @@ -147,29 +147,29 @@ class receiveDataThread(threading.Thread): with shared.printLock: print 'remoteCommand', repr(remoteCommand.replace('\x00', '')), ' from', self.peer - if remoteCommand == 'version\x00\x00\x00\x00\x00' and not self.connectionIsOrWasFullyEstablished: + if remoteCommand == addDataPadding('version') and not self.connectionIsOrWasFullyEstablished: self.recversion(self.data[24:self.payloadLength + 24]) - elif remoteCommand == 'verack\x00\x00\x00\x00\x00\x00' and not self.connectionIsOrWasFullyEstablished: + elif remoteCommand == addDataPadding('verack') and not self.connectionIsOrWasFullyEstablished: self.recverack() - elif remoteCommand == 'addr\x00\x00\x00\x00\x00\x00\x00\x00' and self.connectionIsOrWasFullyEstablished: + elif remoteCommand == addDataPadding('addr') and self.connectionIsOrWasFullyEstablished: self.recaddr(self.data[24:self.payloadLength + 24]) - elif remoteCommand == 'getpubkey\x00\x00\x00' and self.connectionIsOrWasFullyEstablished: + elif remoteCommand == addDataPadding('getpubkey') and self.connectionIsOrWasFullyEstablished: shared.checkAndSharegetpubkeyWithPeers(self.data[24:self.payloadLength + 24]) - elif remoteCommand == 'pubkey\x00\x00\x00\x00\x00\x00' and self.connectionIsOrWasFullyEstablished: + elif remoteCommand == addDataPadding('pubkey') and self.connectionIsOrWasFullyEstablished: self.recpubkey(self.data[24:self.payloadLength + 24]) - elif remoteCommand == 'inv\x00\x00\x00\x00\x00\x00\x00\x00\x00' and self.connectionIsOrWasFullyEstablished: + elif remoteCommand == addDataPadding('inv') and self.connectionIsOrWasFullyEstablished: self.recinv(self.data[24:self.payloadLength + 24]) - elif remoteCommand == 'getdata\x00\x00\x00\x00\x00' and self.connectionIsOrWasFullyEstablished: + elif remoteCommand == addDataPadding('getdata') and self.connectionIsOrWasFullyEstablished: self.recgetdata(self.data[24:self.payloadLength + 24]) - elif remoteCommand == 'msg\x00\x00\x00\x00\x00\x00\x00\x00\x00' and self.connectionIsOrWasFullyEstablished: + elif remoteCommand == addDataPadding('msg') and self.connectionIsOrWasFullyEstablished: self.recmsg(self.data[24:self.payloadLength + 24]) - elif remoteCommand == 'broadcast\x00\x00\x00' and self.connectionIsOrWasFullyEstablished: + elif remoteCommand == addDataPadding('broadcast') and self.connectionIsOrWasFullyEstablished: self.recbroadcast(self.data[24:self.payloadLength + 24]) - elif remoteCommand == 'ping\x00\x00\x00\x00\x00\x00\x00\x00' and self.connectionIsOrWasFullyEstablished: + elif remoteCommand == addDataPadding('ping') and self.connectionIsOrWasFullyEstablished: self.sendpong() - elif remoteCommand == 'pong\x00\x00\x00\x00\x00\x00\x00\x00' and self.connectionIsOrWasFullyEstablished: + elif remoteCommand == addDataPadding('pong') and self.connectionIsOrWasFullyEstablished: pass - elif remoteCommand == 'alert\x00\x00\x00\x00\x00\x00\x00' and self.connectionIsOrWasFullyEstablished: + elif remoteCommand == addDataPadding('alert') and self.connectionIsOrWasFullyEstablished: pass self.data = self.data[ @@ -226,7 +226,7 @@ class receiveDataThread(threading.Thread): def sendpong(self): print 'Sending pong' - self.sendDataThreadQueue.put((0, 'sendRawData', '\xE9\xBE\xB4\xD9\x70\x6F\x6E\x67\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xcf\x83\xe1\x35')) + self.sendDataThreadQueue.put((0, 'sendRawData', '\xE9\xBE\xB4\xD9\x70\x6F\x6E\x67' + addDataPadding('') + '\xcf\x83\xe1\x35')) def recverack(self): @@ -312,7 +312,7 @@ class receiveDataThread(threading.Thread): def sendinvMessageToJustThisOnePeer(self, numberOfObjects, payload): payload = encodeVarint(numberOfObjects) + payload headerData = '\xe9\xbe\xb4\xd9' # magic bits, slighly different from Bitcoin's magic bits. - headerData += 'inv\x00\x00\x00\x00\x00\x00\x00\x00\x00' + headerData += addDataPadding('inv') headerData += pack('>L', len(payload)) headerData += hashlib.sha512(payload).digest()[:4] with shared.printLock: @@ -458,7 +458,7 @@ class receiveDataThread(threading.Thread): payload = '\x01' + hash headerData = '\xe9\xbe\xb4\xd9' # magic bits, slighly different from Bitcoin's magic bits. - headerData += 'getdata\x00\x00\x00\x00\x00' + headerData += addDataPadding('getdata') headerData += pack('>L', len( payload)) # payload length. Note that we add an extra 8 for the nonce. headerData += hashlib.sha512(payload).digest()[:4] @@ -504,22 +504,22 @@ class receiveDataThread(threading.Thread): with shared.printLock: print 'sending pubkey' - headerData += 'pubkey\x00\x00\x00\x00\x00\x00' + headerData += addDataPadding('pubkey') elif objectType == 'getpubkey' or objectType == 'pubkeyrequest': with shared.printLock: print 'sending getpubkey' - headerData += 'getpubkey\x00\x00\x00' + headerData += addDataPadding('getpubkey') elif objectType == 'msg': with shared.printLock: print 'sending msg' - headerData += 'msg\x00\x00\x00\x00\x00\x00\x00\x00\x00' + headerData += addDataPadding('msg') elif objectType == 'broadcast': with shared.printLock: print 'sending broadcast' - headerData += 'broadcast\x00\x00\x00' + headerData += addDataPadding('broadcast') else: sys.stderr.write( 'Error: sendData has been asked to send a strange objectType: %s\n' % str(objectType)) @@ -686,7 +686,7 @@ class receiveDataThread(threading.Thread): if len(shared.knownNodes[self.streamNumber]) > 0: for i in range(500): peer, = random.sample(shared.knownNodes[self.streamNumber], 1) - if helper_generic.isHostInPrivateIPRange(peer.host): + if isHostInPrivateIPRange(peer.host): continue addrsInMyStream[peer] = shared.knownNodes[ self.streamNumber][peer] @@ -694,7 +694,7 @@ class receiveDataThread(threading.Thread): for i in range(250): peer, = random.sample(shared.knownNodes[ self.streamNumber * 2], 1) - if helper_generic.isHostInPrivateIPRange(peer.host): + if isHostInPrivateIPRange(peer.host): continue addrsInChildStreamLeft[peer] = shared.knownNodes[ self.streamNumber * 2][peer] @@ -702,7 +702,7 @@ class receiveDataThread(threading.Thread): for i in range(250): peer, = random.sample(shared.knownNodes[ (self.streamNumber * 2) + 1], 1) - if helper_generic.isHostInPrivateIPRange(peer.host): + if isHostInPrivateIPRange(peer.host): continue addrsInChildStreamRight[peer] = shared.knownNodes[ (self.streamNumber * 2) + 1][peer] @@ -745,7 +745,7 @@ class receiveDataThread(threading.Thread): payload += pack('>H', PORT) # remote port payload = encodeVarint(numberOfAddressesInAddrMessage) + payload - datatosend = '\xE9\xBE\xB4\xD9addr\x00\x00\x00\x00\x00\x00\x00\x00' + datatosend = '\xE9\xBE\xB4\xD9' + addDataPadding('addr') datatosend = datatosend + pack('>L', len(payload)) # payload length datatosend = datatosend + hashlib.sha512(payload).digest()[0:4] datatosend = datatosend + payload diff --git a/src/class_sendDataThread.py b/src/class_sendDataThread.py index 5aabaeb5..d7aec29f 100644 --- a/src/class_sendDataThread.py +++ b/src/class_sendDataThread.py @@ -8,6 +8,7 @@ import random import sys import socket +from helper_generic import addDataPadding from class_objectHashHolder import * from addresses import * @@ -108,7 +109,7 @@ class sendDataThread(threading.Thread): payload += pack('>H', port) payload = encodeVarint(numberOfAddressesInAddrMessage) + payload - datatosend = '\xE9\xBE\xB4\xD9addr\x00\x00\x00\x00\x00\x00\x00\x00' + datatosend = '\xE9\xBE\xB4\xD9' + addDataPadding('addr') datatosend = datatosend + pack('>L', len(payload)) # payload length datatosend = datatosend + hashlib.sha512(payload).digest()[0:4] datatosend = datatosend + payload @@ -132,7 +133,7 @@ class sendDataThread(threading.Thread): if payload != '': payload = encodeVarint(len(payload)/32) + payload headerData = '\xe9\xbe\xb4\xd9' # magic bits, slighly different from Bitcoin's magic bits. - headerData += 'inv\x00\x00\x00\x00\x00\x00\x00\x00\x00' + headerData += addDataPadding('inv') headerData += pack('>L', len(payload)) headerData += hashlib.sha512(payload).digest()[:4] try: @@ -150,7 +151,7 @@ class sendDataThread(threading.Thread): try: self.sock.sendall( - '\xE9\xBE\xB4\xD9\x70\x6F\x6E\x67\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xcf\x83\xe1\x35') + '\xE9\xBE\xB4\xD9\x70\x6F\x6E\x67' + addDataPadding('') + '\xcf\x83\xe1\x35') self.lastTimeISentData = int(time.time()) except: print 'send pong failed' diff --git a/src/class_singleWorker.py b/src/class_singleWorker.py index 4b43c22f..362d1d8f 100644 --- a/src/class_singleWorker.py +++ b/src/class_singleWorker.py @@ -13,6 +13,7 @@ import tr from debug import logger from helper_sql import * import helper_inbox +from helper_generic import addDataPadding # This thread, of which there is only one, does the heavy lifting: # calculating POWs. @@ -950,7 +951,7 @@ class singleWorker(threading.Thread): payload = pack('>Q', nonce) + payload headerData = '\xe9\xbe\xb4\xd9' # magic bits, slighly different from Bitcoin's magic bits. - headerData += 'msg\x00\x00\x00\x00\x00\x00\x00\x00\x00' + headerData += addDataPadding('msg') headerData += pack('>L', len(payload)) headerData += hashlib.sha512(payload).digest()[:4] return headerData + payload diff --git a/src/helper_generic.py b/src/helper_generic.py index e79a5a43..2ea088cc 100644 --- a/src/helper_generic.py +++ b/src/helper_generic.py @@ -32,3 +32,11 @@ def isHostInPrivateIPRange(host): if host[:8] == '192.168.': return True return False + +def addDataPadding(data, desiredMsgLength = 12): + if len(data) >= desiredMsgLength: + return data + padded = data + while len(padded) < desiredMsgLength: + padded += '\x00' + return padded -- 2.45.1 From 41ba9216267b1fd7c87c4fffc153b29742d9a916 Mon Sep 17 00:00:00 2001 From: antius Date: Fri, 2 May 2014 18:47:50 +0200 Subject: [PATCH 2/2] Simplify and optimize --- src/helper_generic.py | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/helper_generic.py b/src/helper_generic.py index 2ea088cc..f3380b26 100644 --- a/src/helper_generic.py +++ b/src/helper_generic.py @@ -33,10 +33,5 @@ def isHostInPrivateIPRange(host): return True return False -def addDataPadding(data, desiredMsgLength = 12): - if len(data) >= desiredMsgLength: - return data - padded = data - while len(padded) < desiredMsgLength: - padded += '\x00' - return padded +def addDataPadding(data, desiredMsgLength = 12, paddingChar = '\x00'): + return data + paddingChar * (desiredMsgLength - len(data)) -- 2.45.1