Make addDataPadding method to avoid clutter. #668

Merged
ghost merged 2 commits from master into master 2014-07-14 20:12:07 +02:00
5 changed files with 47 additions and 36 deletions
Showing only changes of commit 53a3d1635a - Show all commits

View File

@ -12,6 +12,7 @@ from pyelliptic.openssl import OpenSSL
import highlevelcrypto import highlevelcrypto
from addresses import * from addresses import *
import helper_generic import helper_generic
from helper_generic import addDataPadding
import helper_bitcoin import helper_bitcoin
import helper_inbox import helper_inbox
import helper_sent import helper_sent
@ -659,13 +660,13 @@ class objectProcessor(threading.Thread):
shared.workerQueue.put(('sendbroadcast', '')) shared.workerQueue.put(('sendbroadcast', ''))
if self.ackDataHasAVaildHeader(ackData): if self.ackDataHasAVaildHeader(ackData):
if ackData[4:16] == 'getpubkey\x00\x00\x00': if ackData[4:16] == addDataPadding('getpubkey'):
shared.checkAndSharegetpubkeyWithPeers(ackData[24:]) 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:]) 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:]) shared.checkAndShareMsgWithPeers(ackData[24:])
elif ackData[4:16] == 'broadcast\x00\x00\x00': elif ackData[4:16] == addDataPadding('broadcast'):
shared.checkAndShareBroadcastWithPeers(ackData[24:]) shared.checkAndShareBroadcastWithPeers(ackData[24:])
# Display timing data # Display timing data
@ -1164,10 +1165,10 @@ class objectProcessor(threading.Thread):
return False return False
if ackDataPayloadLength > 180000000: # If the size of the message is greater than 180MB, ignore it. if ackDataPayloadLength > 180000000: # If the size of the message is greater than 180MB, ignore it.
return False return False
if (ackData[4:16] != 'getpubkey\x00\x00\x00' and if (ackData[4:16] != addDataPadding('getpubkey') and
ackData[4:16] != 'pubkey\x00\x00\x00\x00\x00\x00' and ackData[4:16] != addDataPadding('pubkey') and
ackData[4:16] != 'msg\x00\x00\x00\x00\x00\x00\x00\x00\x00' and ackData[4:16] != addDataPadding('msg') and
ackData[4:16] != 'broadcast\x00\x00\x00'): ackData[4:16] != addDataPadding('broadcast')):
return False return False
return True return True

View File

@ -14,7 +14,7 @@ import sys
#import highlevelcrypto #import highlevelcrypto
from addresses import * from addresses import *
import helper_generic from helper_generic import addDataPadding, isHostInPrivateIPRange
#import helper_bitcoin #import helper_bitcoin
#import helper_inbox #import helper_inbox
#import helper_sent #import helper_sent
@ -147,29 +147,29 @@ class receiveDataThread(threading.Thread):
with shared.printLock: with shared.printLock:
print 'remoteCommand', repr(remoteCommand.replace('\x00', '')), ' from', self.peer 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]) 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() 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]) 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]) 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]) 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]) 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]) 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]) 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]) 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() self.sendpong()
elif remoteCommand == 'pong\x00\x00\x00\x00\x00\x00\x00\x00' and self.connectionIsOrWasFullyEstablished: elif remoteCommand == addDataPadding('pong') and self.connectionIsOrWasFullyEstablished:
pass pass
elif remoteCommand == 'alert\x00\x00\x00\x00\x00\x00\x00' and self.connectionIsOrWasFullyEstablished: elif remoteCommand == addDataPadding('alert') and self.connectionIsOrWasFullyEstablished:
pass pass
self.data = self.data[ self.data = self.data[
@ -226,7 +226,7 @@ class receiveDataThread(threading.Thread):
def sendpong(self): def sendpong(self):
print 'Sending pong' 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): def recverack(self):
@ -312,7 +312,7 @@ class receiveDataThread(threading.Thread):
def sendinvMessageToJustThisOnePeer(self, numberOfObjects, payload): def sendinvMessageToJustThisOnePeer(self, numberOfObjects, payload):
payload = encodeVarint(numberOfObjects) + payload payload = encodeVarint(numberOfObjects) + payload
headerData = '\xe9\xbe\xb4\xd9' # magic bits, slighly different from Bitcoin's magic bits. 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 += pack('>L', len(payload))
headerData += hashlib.sha512(payload).digest()[:4] headerData += hashlib.sha512(payload).digest()[:4]
with shared.printLock: with shared.printLock:
@ -458,7 +458,7 @@ class receiveDataThread(threading.Thread):
payload = '\x01' + hash payload = '\x01' + hash
headerData = '\xe9\xbe\xb4\xd9' # magic bits, slighly different from Bitcoin's magic bits. 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( headerData += pack('>L', len(
payload)) # payload length. Note that we add an extra 8 for the nonce. payload)) # payload length. Note that we add an extra 8 for the nonce.
headerData += hashlib.sha512(payload).digest()[:4] headerData += hashlib.sha512(payload).digest()[:4]
@ -504,22 +504,22 @@ class receiveDataThread(threading.Thread):
with shared.printLock: with shared.printLock:
print 'sending pubkey' print 'sending pubkey'
headerData += 'pubkey\x00\x00\x00\x00\x00\x00' headerData += addDataPadding('pubkey')
elif objectType == 'getpubkey' or objectType == 'pubkeyrequest': elif objectType == 'getpubkey' or objectType == 'pubkeyrequest':
with shared.printLock: with shared.printLock:
print 'sending getpubkey' print 'sending getpubkey'
headerData += 'getpubkey\x00\x00\x00' headerData += addDataPadding('getpubkey')
elif objectType == 'msg': elif objectType == 'msg':
with shared.printLock: with shared.printLock:
print 'sending msg' print 'sending msg'
headerData += 'msg\x00\x00\x00\x00\x00\x00\x00\x00\x00' headerData += addDataPadding('msg')
elif objectType == 'broadcast': elif objectType == 'broadcast':
with shared.printLock: with shared.printLock:
print 'sending broadcast' print 'sending broadcast'
headerData += 'broadcast\x00\x00\x00' headerData += addDataPadding('broadcast')
else: else:
sys.stderr.write( sys.stderr.write(
'Error: sendData has been asked to send a strange objectType: %s\n' % str(objectType)) '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: if len(shared.knownNodes[self.streamNumber]) > 0:
for i in range(500): for i in range(500):
peer, = random.sample(shared.knownNodes[self.streamNumber], 1) peer, = random.sample(shared.knownNodes[self.streamNumber], 1)
if helper_generic.isHostInPrivateIPRange(peer.host): if isHostInPrivateIPRange(peer.host):
continue continue
addrsInMyStream[peer] = shared.knownNodes[ addrsInMyStream[peer] = shared.knownNodes[
self.streamNumber][peer] self.streamNumber][peer]
@ -694,7 +694,7 @@ class receiveDataThread(threading.Thread):
for i in range(250): for i in range(250):
peer, = random.sample(shared.knownNodes[ peer, = random.sample(shared.knownNodes[
self.streamNumber * 2], 1) self.streamNumber * 2], 1)
if helper_generic.isHostInPrivateIPRange(peer.host): if isHostInPrivateIPRange(peer.host):
continue continue
addrsInChildStreamLeft[peer] = shared.knownNodes[ addrsInChildStreamLeft[peer] = shared.knownNodes[
self.streamNumber * 2][peer] self.streamNumber * 2][peer]
@ -702,7 +702,7 @@ class receiveDataThread(threading.Thread):
for i in range(250): for i in range(250):
peer, = random.sample(shared.knownNodes[ peer, = random.sample(shared.knownNodes[
(self.streamNumber * 2) + 1], 1) (self.streamNumber * 2) + 1], 1)
if helper_generic.isHostInPrivateIPRange(peer.host): if isHostInPrivateIPRange(peer.host):
continue continue
addrsInChildStreamRight[peer] = shared.knownNodes[ addrsInChildStreamRight[peer] = shared.knownNodes[
(self.streamNumber * 2) + 1][peer] (self.streamNumber * 2) + 1][peer]
@ -745,7 +745,7 @@ class receiveDataThread(threading.Thread):
payload += pack('>H', PORT) # remote port payload += pack('>H', PORT) # remote port
payload = encodeVarint(numberOfAddressesInAddrMessage) + payload 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 + pack('>L', len(payload)) # payload length
datatosend = datatosend + hashlib.sha512(payload).digest()[0:4] datatosend = datatosend + hashlib.sha512(payload).digest()[0:4]
datatosend = datatosend + payload datatosend = datatosend + payload

View File

@ -8,6 +8,7 @@ import random
import sys import sys
import socket import socket
from helper_generic import addDataPadding
from class_objectHashHolder import * from class_objectHashHolder import *
from addresses import * from addresses import *
@ -108,7 +109,7 @@ class sendDataThread(threading.Thread):
payload += pack('>H', port) payload += pack('>H', port)
payload = encodeVarint(numberOfAddressesInAddrMessage) + payload 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 + pack('>L', len(payload)) # payload length
datatosend = datatosend + hashlib.sha512(payload).digest()[0:4] datatosend = datatosend + hashlib.sha512(payload).digest()[0:4]
datatosend = datatosend + payload datatosend = datatosend + payload
@ -132,7 +133,7 @@ class sendDataThread(threading.Thread):
if payload != '': if payload != '':
payload = encodeVarint(len(payload)/32) + payload payload = encodeVarint(len(payload)/32) + payload
headerData = '\xe9\xbe\xb4\xd9' # magic bits, slighly different from Bitcoin's magic bits. 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 += pack('>L', len(payload))
headerData += hashlib.sha512(payload).digest()[:4] headerData += hashlib.sha512(payload).digest()[:4]
try: try:
@ -150,7 +151,7 @@ class sendDataThread(threading.Thread):
try: try:
self.sock.sendall( 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()) self.lastTimeISentData = int(time.time())
except: except:
print 'send pong failed' print 'send pong failed'

View File

@ -13,6 +13,7 @@ import tr
from debug import logger from debug import logger
from helper_sql import * from helper_sql import *
import helper_inbox import helper_inbox
from helper_generic import addDataPadding
# This thread, of which there is only one, does the heavy lifting: # This thread, of which there is only one, does the heavy lifting:
# calculating POWs. # calculating POWs.
@ -950,7 +951,7 @@ class singleWorker(threading.Thread):
payload = pack('>Q', nonce) + payload payload = pack('>Q', nonce) + payload
headerData = '\xe9\xbe\xb4\xd9' # magic bits, slighly different from Bitcoin's magic bits. 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 += pack('>L', len(payload))
headerData += hashlib.sha512(payload).digest()[:4] headerData += hashlib.sha512(payload).digest()[:4]
return headerData + payload return headerData + payload

View File

@ -32,3 +32,11 @@ def isHostInPrivateIPRange(host):
if host[:8] == '192.168.': if host[:8] == '192.168.':
return True return True
return False return False
def addDataPadding(data, desiredMsgLength = 12):
if len(data) >= desiredMsgLength:
return data
padded = data
while len(padded) < desiredMsgLength:
padded += '\x00'
return padded