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
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

View File

@ -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

View File

@ -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'

View File

@ -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

View File

@ -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