PyBitmessage/bitmessagemain.py

3474 lines
197 KiB
Python

# Copyright (c) 2012 Jonathan Warren
# Copyright (c) 2012 The Bitmessage developers
# Distributed under the MIT/X11 software license. See the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
#Right now, PyBitmessage only support connecting to stream 1. It doesn't yet contain logic to expand into further streams.
softwareVersion = '0.1.0'
verbose = 2
maximumAgeOfAnObjectThatIAmWillingToAccept = 216000 #Equals two days and 12 hours.
lengthOfTimeToLeaveObjectsInInventory = 237600 #Equals two days and 18 hours. This should be longer than maximumAgeOfAnObjectThatIAmWillingToAccept so that we don't process messages twice.
maximumAgeOfObjectsThatIAdvertiseToOthers = 216000 #Equals two days and 12 hours
maximumAgeOfNodesThatIAdvertiseToOthers = 10800 #Equals three hours
import sys
try:
from PyQt4.QtCore import *
from PyQt4.QtGui import *
except Exception, err:
print 'Bitmessage requires PyQt. You can download it from http://www.riverbankcomputing.com/software/pyqt/download or by searching Google for \'PyQt Download\' (without quotes).'
print 'Error message:', err
sys.exit()
import ConfigParser
from bitmessageui import *
from newaddressdialog import *
from newsubscriptiondialog import *
from settings import *
from about import *
from help import *
from iconglossary import *
from addresses import *
import Queue
from defaultKnownNodes import *
import time
import socket
import threading
import rsa
from rsa.bigfile import *
import hashlib
from struct import *
import pickle
import random
import sqlite3
import threading #used for the locks, not for the threads
import cStringIO
from email.parser import Parser
from time import strftime, localtime
import os
#For each stream to which we connect, one outgoingSynSender thread will exist and will create 8 connections with peers.
class outgoingSynSender(QThread):
def __init__(self, parent = None):
QThread.__init__(self, parent)
self.selfInitiatedConnectionList = [] #This is a list of current connections (the thread pointers at least)
self.alreadyAttemptedConnectionsList = [] #This is a list of nodes to which we have already attempted a connection
def setup(self,streamNumber):
self.streamNumber = streamNumber
def run(self):
time.sleep(1)
resetTime = int(time.time()) #used below to clear out the alreadyAttemptedConnectionsList periodically so that we will retry connecting to hosts to which we have already tried to connect.
while True:
#time.sleep(999999)#I'm using this to prevent connections for testing.
if len(self.selfInitiatedConnectionList) < 8: #maximum number of outgoing connections = 8
random.seed()
HOST, = random.sample(knownNodes[self.streamNumber], 1)
while HOST in self.alreadyAttemptedConnectionsList or HOST in connectedHostsList:
#print 'choosing new sample'
random.seed()
HOST, = random.sample(knownNodes[self.streamNumber], 1)
time.sleep(1)
#Clear out the alreadyAttemptedConnectionsList every half hour so that this program will again attempt a connection to any nodes, even ones it has already tried.
if (int(time.time()) - resetTime) > 1800:
self.alreadyAttemptedConnectionsList = []
resetTime = int(time.time())
self.alreadyAttemptedConnectionsList.append(HOST)
PORT, timeNodeLastSeen = knownNodes[self.streamNumber][HOST]
print 'Trying an outgoing connection to', HOST, ':', PORT
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
sock.connect((HOST, PORT))
rd = receiveDataThread()
self.emit(SIGNAL("passObjectThrough(PyQt_PyObject)"),rd)
rd.setup(sock,HOST,PORT,self.streamNumber,self.selfInitiatedConnectionList)
rd.start()
print self, 'connected to', HOST, 'during outgoing attempt.'
sd = sendDataThread()
sd.setup(sock,HOST,PORT,self.streamNumber)
sd.start()
sd.sendVersionMessage()
except Exception, err:
print 'Could NOT connect to', HOST, 'during outgoing attempt.', err
PORT, timeLastSeen = knownNodes[self.streamNumber][HOST]
if (int(time.time())-timeLastSeen) > 172800: # for nodes older than 48 hours old, delete from the knownNodes data-structure.
if len(knownNodes[self.streamNumber]) > 1000: #as long as we have more than 1000 hosts in our list
del knownNodes[self.streamNumber][HOST]
print 'deleting ', HOST, 'from knownNodes because it is more than 48 hours old and we could not connect to it.'
time.sleep(1)
#Only one singleListener thread will ever exist. It creates the receiveDataThread and sendDataThread for each incoming connection. Note that it cannot set the stream number because it is not known yet- the other node will have to tell us its stream number in a version message. If we don't care about their stream, we will close the connection (within the recversion function of the recieveData thread)
class singleListener(QThread):
def __init__(self, parent = None):
QThread.__init__(self, parent)
def run(self):
print 'bitmessage listener running'
HOST = '' # Symbolic name meaning all available interfaces
PORT = config.getint('bitmessagesettings', 'port')
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#This option apparently avoids the TIME_WAIT state so that we can rebind faster
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind((HOST, PORT))
sock.listen(2)
self.incomingConnectionList = []
while True:
#for i in range(0,1): #uncomment this line and comment the line above this to accept only one connection.
rd = receiveDataThread()
a,(HOST,PORT) = sock.accept()
while HOST in connectedHostsList:
print 'incoming connection is from a host in connectedHostsList (we are already connected to it). Ignoring it.'
a.close()
a,(HOST,PORT) = sock.accept()
self.emit(SIGNAL("passObjectThrough(PyQt_PyObject)"),rd)
rd.setup(a,HOST,PORT,-1,self.incomingConnectionList)
print self, 'connected to', HOST,'during INCOMING request.'
rd.start()
sd = sendDataThread()
sd.setup(a,HOST,PORT,-1)
sd.start()
#This thread is created either by the synSenderThread(for outgoing connections) or the singleListenerThread(for incoming connectiosn).
class receiveDataThread(QThread):
def __init__(self, parent = None):
QThread.__init__(self, parent)
self.data = ''
self.verackSent = False
self.verackReceived = False
def setup(self,sock,HOST,port,streamNumber,selfInitiatedConnectionList):
self.sock = sock
self.HOST = HOST
self.PORT = port
self.sock.settimeout(600) #We'll send out a pong every 5 minutes to make sure the connection stays alive if there has been no other traffic to send lately.
self.streamNumber = streamNumber
self.selfInitiatedConnectionList = selfInitiatedConnectionList
self.selfInitiatedConnectionList.append(self)
self.payloadLength = 0
self.receivedgetbiginv = False
self.objectsThatWeHaveYetToGet = {}
connectedHostsList[self.HOST] = 0
self.connectionIsOrWasFullyEstablished = False #set to true after the remote node and I accept each other's version messages. This is needed to allow the user interface to accurately reflect the current number of connections.
if self.streamNumber == -1: #This was an incoming connection. Send out a version message if we accept the other node's version message.
self.initiatedConnection = False
else:
self.initiatedConnection = True
def run(self):
while True:
try:
self.data = self.data + self.sock.recv(65536)
except socket.timeout:
printLock.acquire()
print 'Timeout occurred waiting for data. Closing thread.'
printLock.release()
break
except Exception, err:
printLock.acquire()
print 'Error receiving data. Closing receiveData thread.'
printLock.release()
break
#print 'Received', repr(self.data)
if self.data == "":
printLock.acquire()
print 'Socket timeout.'
printLock.release()
break
else:
self.processData()
try:
self.sock.close()
except Exception, err:
print 'Within receiveDataThread run(), self.sock.close() failed.', err
try:
self.selfInitiatedConnectionList.remove(self)
print 'removed self from ConnectionList'
except:
pass
broadcastToSendDataQueues((self.streamNumber, 'shutdown', self.HOST))
if self.connectionIsOrWasFullyEstablished: #We don't want to decrement the number of connections and show the result if we never incremented it in the first place (which we only do if the connection is fully established- meaning that both nodes accepted each other's version packets.)
connectionsCountLock.acquire()
connectionsCount[self.streamNumber] -= 1
self.emit(SIGNAL("updateNetworkStatusTab(PyQt_PyObject,PyQt_PyObject)"),self.streamNumber,connectionsCount[self.streamNumber])
connectionsCountLock.release()
del connectedHostsList[self.HOST]
def processData(self):
global verbose
if verbose >= 2:
printLock.acquire()
print 'self.data is currently ', repr(self.data)
printLock.release()
if self.data == "":
pass
elif self.data[0:4] != '\xe9\xbe\xb4\xd9':
self.data = ""
if verbose >= 2:
#printLock.acquire()
sys.stderr.write('The magic bytes were not correct. This may indicate a problem with the program code, or a transmission error occurred.\n')
#printLock.release()
else: #if there is no reason to discard the data we have so far,
if len(self.data) < 20: #if so little of the data has arrived that we can't even unpack the payload length
pass
else:
self.payloadLength, = unpack('>L',self.data[16:20])
if len(self.data) >= self.payloadLength: #check if the whole message has arrived yet. If it has,...
if self.data[20:24] == hashlib.sha512(self.data[24:self.payloadLength+24]).digest()[0:4]:#test the checksum in the message. If it is correct...
#print 'message checksum is correct'
#The time we've last seen this node is obviously right now since we just received valid data from it. So update the knownNodes list so that other peers can be made aware of its existance.
if self.initiatedConnection: #The remote port is only something we should share with others if it is the remote node's incoming port (rather than some random operating-system-assigned outgoing port).
knownNodes[self.streamNumber][self.HOST] = (self.PORT,int(time.time()))
remoteCommand = self.data[4:16]
if verbose >= 2:
print 'remoteCommand ', remoteCommand, 'from', self.HOST
if remoteCommand == 'version\x00\x00\x00\x00\x00':
self.recversion()
elif remoteCommand == 'verack\x00\x00\x00\x00\x00\x00':
self.recverack()
elif remoteCommand == 'addr\x00\x00\x00\x00\x00\x00\x00\x00' and self.connectionIsOrWasFullyEstablished:
self.recaddr()
elif remoteCommand == 'getpubkey\x00\x00\x00' and self.connectionIsOrWasFullyEstablished:
self.recgetpubkey()
elif remoteCommand == 'pubkey\x00\x00\x00\x00\x00\x00' and self.connectionIsOrWasFullyEstablished:
self.recpubkey()
elif remoteCommand == 'inv\x00\x00\x00\x00\x00\x00\x00\x00\x00' and self.connectionIsOrWasFullyEstablished:
self.recinv()
elif remoteCommand == 'getdata\x00\x00\x00\x00\x00' and self.connectionIsOrWasFullyEstablished:
self.recgetdata()
elif remoteCommand == 'getbiginv\x00\x00\x00' and self.connectionIsOrWasFullyEstablished:
self.sendBigInv()
elif remoteCommand == 'msg\x00\x00\x00\x00\x00\x00\x00\x00\x00' and self.connectionIsOrWasFullyEstablished:
self.recmsg()
elif remoteCommand == 'broadcast\x00\x00\x00' and self.connectionIsOrWasFullyEstablished:
self.recbroadcast()
elif remoteCommand == 'getaddr\x00\x00\x00\x00\x00' and self.connectionIsOrWasFullyEstablished:
self.sendaddr()
elif remoteCommand == 'ping\x00\x00\x00\x00\x00\x00\x00\x00' and self.connectionIsOrWasFullyEstablished:
self.sendpong()
elif remoteCommand == 'pong\x00\x00\x00\x00\x00\x00\x00\x00' and self.connectionIsOrWasFullyEstablished:
pass
elif remoteCommand == 'alert\x00\x00\x00\x00\x00\x00\x00' and self.connectionIsOrWasFullyEstablished:
pass
self.data = self.data[self.payloadLength+24:]#take this message out and then process the next message
if self.data == '':
while len(self.objectsThatWeHaveYetToGet) > 0:
random.seed()
objectHash, = random.sample(self.objectsThatWeHaveYetToGet, 1)
if objectHash in inventory:
print 'Inventory (in memory) already has object that we received in an inv message.'
del self.objectsThatWeHaveYetToGet[objectHash]
elif isInSqlInventory(objectHash):
print 'Inventory (SQL on disk) already has object that we received in an inv message.'
del self.objectsThatWeHaveYetToGet[objectHash]
else:
print 'processData function making request for object:', repr(objectHash)
self.sendgetdata(objectHash)
del self.objectsThatWeHaveYetToGet[objectHash] #It is possible that the remote node doesn't respond with the object. In that case, we'll very likely get it from someone else anyway.
break
print 'within processData, length of objectsThatWeHaveYetToGet is now', len(self.objectsThatWeHaveYetToGet)
self.processData()
#if self.versionSent == 0:
# self.sendversion()
'''if (self.versionRec == 1) and (self.getaddrthreadalreadycreated == 0):
while (threading.activeCount() > maxGlobalThreads):#If there too many open threads, just wait. This includes full and half-open.
time.sleep(0.1)
printLock.acquire()
print 'Starting a new getaddrSender thread'
printLock.release()
bar = getaddrSender((self.sock,self.addr))
bar.daemon = True
self.getaddrthreadalreadycreated = 1 #we're about to start it
while True: #My goal is to try bar.start() until it succeedes
while (threading.activeCount() > maxGlobalThreads):#If there too many open threads, just wait. This includes full and half-open.
time.sleep(0.1)
try:
bar.start()
except:
maxGlobalThreads = maxGlobalThreads - 20
sys.stderr.write('Problem! Your OS did not allow the starting of another thread. Reducing the number of threads to %s and trying again.\n' % maxGlobalThreads)
continue #..the while loop again right now
break '''
else:
print 'Checksum incorrect. Clearing messages and waiting for new data.'
self.data = ''
def sendpong(self):
print 'Sending pong'
self.sock.sendall('\xE9\xBE\xB4\xD9\x70\x6F\x6E\x67\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x82\x6D\xf0\x68')
def recverack(self):
print 'verack received'
self.verackReceived = True
if self.verackSent == True:
#We have thus both sent and received a verack.
self.connectionFullyEstablished()
def connectionFullyEstablished(self):
self.connectionIsOrWasFullyEstablished = True
if not self.initiatedConnection:
self.emit(SIGNAL("setStatusIcon(PyQt_PyObject)"),'green')
#Update the 'Network Status' tab
connectionsCountLock.acquire()
connectionsCount[self.streamNumber] += 1
self.emit(SIGNAL("updateNetworkStatusTab(PyQt_PyObject,PyQt_PyObject)"),self.streamNumber,connectionsCount[self.streamNumber])
connectionsCountLock.release()
self.sendaddr()
remoteNodeIncomingPort, remoteNodeSeenTime = knownNodes[self.streamNumber][self.HOST]
print 'within connectionFullyEstablished, self.HOST and remoteNodeIncomingPort:', self.HOST, remoteNodeIncomingPort
print 'broadcasting addr from within connectionFullyEstablished function.'
self.broadcastaddr([(int(time.time()), self.streamNumber, 1, self.HOST, remoteNodeIncomingPort)])
if connectionsCount[self.streamNumber] > 150:
print 'We are connected to too many people. Closing connection.'
self.sock.close()
return
self.sendBigInv()
def sendBigInv(self): #I used capitals in for this function name because there is no such Bitmessage command as 'biginv'.
if self.receivedgetbiginv:
print 'We have already sent a big inv message to this peer. Ignoring request.'
return
else:
self.receivedgetbiginv = True
sqlLock.acquire()
#Select all hashes which are younger than two days old and in this stream.
t = (int(time.time())-172800,self.streamNumber)
sqlSubmitQueue.put('''SELECT hash FROM inventory WHERE receivedtime>? and streamnumber=?''')
sqlSubmitQueue.put(t)
queryreturn = sqlReturnQueue.get()
sqlLock.release()
print 'sendBigInv pulled', len(queryreturn), 'items from SQL inventory.'
bigInvList = {}
for row in queryreturn:
hash, = row
bigInvList[hash] = 0
#print 'bigInvList:', bigInvList
for hash, storedValue in inventory.items():
objectType, streamNumber, payload, receivedTime = storedValue
if streamNumber == self.streamNumber and receivedTime > int(time.time())-172800:
bigInvList[hash] = 0
numberOfObjectsInInvMessage = 0
payload = ''
for hash, storedValue in bigInvList.items():
payload += hash
numberOfObjectsInInvMessage += 1
if numberOfObjectsInInvMessage >= 50000: #We can only send a max of 50000 items per inv message but we may have more objects to advertise. They must be split up into multiple inv messages.
sendinvMessageToJustThisOnePeer(numberOfObjectsInInvMessage,payload,)
payload = ''
numberOfObjectsInInvMessage = 0
if numberOfObjectsInInvMessage > 0:
self.sendinvMessageToJustThisOnePeer(numberOfObjectsInInvMessage,payload,)
#Notice that there is also a broadcastinv function for broadcasting invs to everyone in our stream.
def sendinvMessageToJustThisOnePeer(self,numberOfObjects,payload,destination):
payload = encodeVarint(numberOfObjects) + payload
headerData = '\xe9\xbe\xb4\xd9' #magic bits, slighly different from Bitcoin's magic bits.
headerData = headerData + 'inv\x00\x00\x00\x00\x00\x00\x00\x00\x00'
headerData = headerData + pack('>L',len(payload))
headerData = headerData + hashlib.sha512(payload).digest()[:4]
self.sock.send(headerData + payload)
#We have received a broadcast message
def recbroadcast(self):
#First we must check to make sure the proof of work is sufficient.
POW, = unpack('>Q',hashlib.sha512(self.data[24:24+self.payloadLength]).digest()[4:12])
if POW > 2**64 / ((self.payloadLength+payloadLengthExtraBytes) * averageProofOfWorkNonceTrialsPerByte):
print 'The proof of work in this broadcast message is insufficient. Ignoring message.'
return
embeddedTime, = unpack('>I',self.data[32:36])
if embeddedTime > (int(time.time())+10800): #prevent funny business
print 'The embedded time in this broadcast message is more than three hours in the future. That doesn\'t make sense. Ignoring message.'
return
if embeddedTime < (int(time.time())-maximumAgeOfAnObjectThatIAmWillingToAccept):
print 'The embedded time in this broadcast message is too old. Ignoring message.'
return
if self.payloadLength < 66:
print 'The payload length of this broadcast packet is unreasonably low. Someone is probably trying funny business. Ignoring message.'
return
inventoryHash = calculateInventoryHash(self.data[24:self.payloadLength+24])
if inventoryHash in inventory:
print 'We have already received this broadcast object. Ignoring.'
return
elif isInSqlInventory(inventoryHash):
print 'We have already received this broadcast object (it is stored on disk in the SQL inventory). Ignoring it.'
return
#It is valid so far. Let's let our peers know about it.
objectType = 'broadcast'
inventory[inventoryHash] = (objectType, self.streamNumber, self.data[24:self.payloadLength+24], embeddedTime)
self.broadcastinv(inventoryHash)
self.emit(SIGNAL("incrementNumberOfBroadcastsProcessed()"))
readPosition = 36
broadcastVersion, broadcastVersionLength = decodeVarint(self.data[readPosition:readPosition+9])
if broadcastVersion <> 1:
#Cannot decode incoming broadcast versions higher than 1. Assuming the sender isn\' being silly, you should upgrade Bitmessage because this message shall be ignored.
return
readPosition += broadcastVersionLength
sendersAddressVersion, sendersAddressVersionLength = decodeVarint(self.data[readPosition:readPosition+9])
if sendersAddressVersion <> 1:
#Cannot decode senderAddressVersion higher than 1. Assuming the sender isn\' being silly, you should upgrade Bitmessage because this message shall be ignored.
return
readPosition += sendersAddressVersionLength
sendersStream, sendersStreamLength = decodeVarint(self.data[readPosition:readPosition+9])
if sendersStream <= 0:
return
readPosition += sendersStreamLength
sendersHash = self.data[readPosition:readPosition+20]
if sendersHash not in broadcastSendersForWhichImWatching:
return
#At this point, this message claims to be from sendersHash and we are interested in it. We still have to hash the public key to make sure it is truly the key that matches the hash, and also check the signiture.
readPosition += 20
nLength, nLengthLength = decodeVarint(self.data[readPosition:readPosition+9])
if nLength < 1:
return
readPosition += nLengthLength
nString = self.data[readPosition:readPosition+nLength]
readPosition += nLength
eLength, eLengthLength = decodeVarint(self.data[readPosition:readPosition+9])
if eLength < 1:
return
readPosition += eLengthLength
eString = self.data[readPosition:readPosition+eLength]
#We are now ready to hash the public key and verify that its hash matches the hash claimed in the message
readPosition += eLength
sha = hashlib.new('sha512')
sha.update(nString+eString)
ripe = hashlib.new('ripemd160')
ripe.update(sha.digest())
if ripe.digest() != sendersHash:
#The sender of this message lied.
return
readPositionAtBeginningOfMessageEncodingType = readPosition
messageEncodingType, messageEncodingTypeLength = decodeVarint(self.data[readPosition:readPosition+9])
if messageEncodingType == 0:
return
readPosition += messageEncodingTypeLength
messageLength, messageLengthLength = decodeVarint(self.data[readPosition:readPosition+9])
readPosition += messageLengthLength
message = self.data[readPosition:readPosition+messageLength]
readPosition += messageLength
signature = self.data[readPosition:readPosition+nLength]
print 'signature', repr(signature)
sendersPubkey = rsa.PublicKey(convertStringToInt(nString),convertStringToInt(eString))
#print 'senders Pubkey', sendersPubkey
try:
rsa.verify(self.data[readPositionAtBeginningOfMessageEncodingType:readPositionAtBeginningOfMessageEncodingType+messageEncodingTypeLength+messageLengthLength+messageLength],signature,sendersPubkey)
print 'verify passed'
except Exception, err:
print 'verify failed', err
return
#verify passed
fromAddress = encodeAddress(sendersAddressVersion,sendersStream,ripe.digest())
print 'fromAddress:', fromAddress
if messageEncodingType == 2:
headers = Parser().parsestr(message)
subject = headers['subject']
body = headers['body']
elif messageEncodingType == 1:
body = message
subject = ''
elif messageEncodingType == 0:
print 'messageEncodingType == 0. Doing nothing with the message.'
else:
body = 'Unknown encoding type.\n\n' + repr(message)
subject = ''
toAddress = '[Broadcast subscribers]'
if messageEncodingType <> 0:
sqlLock.acquire()
t = (inventoryHash,toAddress,fromAddress,subject,int(time.time()),body,'inbox')
sqlSubmitQueue.put('''INSERT INTO inbox VALUES (,?,?,?,?,?,?)''')
sqlSubmitQueue.put(t)
sqlReturnQueue.get()
sqlLock.release()
self.emit(SIGNAL("displayNewMessage(PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject)"),inventoryHasy,toAddress,fromAddress,subject,body)
#We have received a msg message.
def recmsg(self):
#First we must check to make sure the proof of work is sufficient.
POW, = unpack('>Q',hashlib.sha512(self.data[24:24+self.payloadLength]).digest()[4:12])
print 'POW:', POW
initialDecryptionSuccessful = False
if POW > 2**64 / ((self.payloadLength+payloadLengthExtraBytes) * averageProofOfWorkNonceTrialsPerByte):
print 'Proof of work in msg message insufficient.'
return
readPosition = 32
embeddedTime, = unpack('>I',self.data[readPosition:readPosition+4])
if embeddedTime > int(time.time())+10800:
print 'The time in the msg message is too new. Ignoring it. Time:', embeddedTime
return
if embeddedTime < int(time.time())-maximumAgeOfAnObjectThatIAmWillingToAccept:
print 'The time in the msg message is too old. Ignoring it. Time:', embeddedTime
return
readPosition += 4
inventoryHash = calculateInventoryHash(self.data[24:self.payloadLength+24])
if inventoryHash in inventory:
print 'We have already received this msg message. Ignoring.'
return
elif isInSqlInventory(inventoryHash):
print 'We have already received this msg message (it is stored on disk in the SQL inventory). Ignoring it.'
return
streamNumberAsClaimedByMsg, streamNumberAsClaimedByMsgLength = decodeVarint(self.data[readPosition:readPosition+9])
if streamNumberAsClaimedByMsg != self.streamNumber:
print 'The stream number encoded in this msg (' + streamNumberAsClaimedByMsg + ') message does not match the stream number on which it was received. Ignoring it.'
return
readPosition += streamNumberAsClaimedByMsgLength
#This msg message is valid. Let's let our peers know about it.
objectType = 'msg'
inventory[inventoryHash] = (objectType, self.streamNumber, self.data[24:self.payloadLength+24], embeddedTime)
self.broadcastinv(inventoryHash)
self.emit(SIGNAL("incrementNumberOfMessagesProcessed()"))
#Let's check whether this is a message acknowledgement bound for us.
if self.data[readPosition:24+self.payloadLength] in ackdataForWhichImWatching:
printLock.acquire()
print 'This msg is an acknowledgement bound for me.'
printLock.release()
del ackdataForWhichImWatching[self.data[readPosition:24+self.payloadLength]]
t = ('ackreceived',self.data[readPosition:24+self.payloadLength])
sqlLock.acquire()
sqlSubmitQueue.put('UPDATE sent SET status=? WHERE ackdata=?')
sqlSubmitQueue.put(t)
sqlReturnQueue.get()
sqlLock.release()
self.emit(SIGNAL("updateSentItemStatusByAckdata(PyQt_PyObject,PyQt_PyObject)"),self.data[readPosition:24+self.payloadLength],'Acknowledgement of the message received just now.')
flushInventory() #so that we won't accidentially receive this message twice if the user restarts Bitmessage soon.
return
else:
printLock.acquire()
print 'This was NOT an acknowledgement bound for me. Msg potential ack data:', repr(self.data[readPosition:24+self.payloadLength])
print 'ackdataForWhichImWatching', ackdataForWhichImWatching
printLock.release()
#This is not an acknowledgement bound for me. See if it is a message bound for me by trying to decrypt it with my private keys.
infile = cStringIO.StringIO(self.data[readPosition:self.payloadLength+24])
outfile = cStringIO.StringIO()
#print 'len(myAddressHashes.items()):', len(myAddressHashes.items())
for key, value in myAddressHashes.items():
try:
decrypt_bigfile(infile, outfile, value)
#The initial decryption passed though there is a small chance that the message isn't actually for me. We'll need to check that the 20 zeros are present.
#print 'initial decryption successful using key', repr(key)
initialDecryptionSuccessful = True
printLock.acquire()
print 'Initial decryption passed'
printLock.release()
break
except Exception, err:
infile.seek(0)
#print 'Exception:', err
#print 'outfile len is:', len(outfile.getvalue()),'data is:', repr(outfile.getvalue())
#print 'Initial decryption failed using key', value
#decryption failed for this key. The message is for someone else (or for a different key of mine).
if initialDecryptionSuccessful and outfile.getvalue()[:20] == '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00': #this run of 0s allows the true message receiver to identify his message
#This is clearly a message bound for me.
flushInventory() #so that we won't accidentially receive this message twice if the user restarts Bitmessage soon.
outfile.seek(0)
data = outfile.getvalue()
readPosition = 20 #To start reading past the 20 zero bytes
messageVersion, messageVersionLength = decodeVarint(data[readPosition:readPosition+10])
readPosition += messageVersionLength
if messageVersion == 1:
bitfieldBehavior = data[readPosition:readPosition+4]
readPosition += 4
sendersAddressVersionNumber, sendersAddressVersionNumberLength = decodeVarint(data[readPosition:readPosition+10])
if sendersAddressVersionNumber == 1:
readPosition += sendersAddressVersionNumberLength
sendersStreamNumber, sendersStreamNumberLength = decodeVarint(data[readPosition:readPosition+10])
readPosition += sendersStreamNumberLength
sendersNLength, sendersNLengthLength = decodeVarint(data[readPosition:readPosition+10])
readPosition += sendersNLengthLength
sendersN = data[readPosition:readPosition+sendersNLength]
readPosition += sendersNLength
sendersELength, sendersELengthLength = decodeVarint(data[readPosition:readPosition+10])
readPosition += sendersELengthLength
sendersE = data[readPosition:readPosition+sendersELength]
readPosition += sendersELength
endOfThePublicKeyPosition = readPosition
messageEncodingType, messageEncodingTypeLength = decodeVarint(data[readPosition:readPosition+10])
readPosition += messageEncodingTypeLength
print 'Message Encoding Type:', messageEncodingType
messageLength, messageLengthLength = decodeVarint(data[readPosition:readPosition+10])
print 'message length:', messageLength
readPosition += messageLengthLength
message = data[readPosition:readPosition+messageLength]
#print 'First 150 characters of message:', repr(message[:150])
readPosition += messageLength
ackLength, ackLengthLength = decodeVarint(data[readPosition:readPosition+10])
#print 'ackLength:', ackLength
readPosition += ackLengthLength
ackData = data[readPosition:readPosition+ackLength]
readPosition += ackLength
payloadSigniture = data[readPosition:readPosition+sendersNLength] #We're using the length of the sender's n because it should match the signiture size.
sendersPubkey = rsa.PublicKey(convertStringToInt(sendersN),convertStringToInt(sendersE))
print 'senders Pubkey', sendersPubkey
verifyPassed = False
try:
rsa.verify(data[:-len(payloadSigniture)],payloadSigniture, sendersPubkey)
print 'verify passed'
verifyPassed = True
except Exception, err:
print 'verify failed', err
if verifyPassed:
#Let's calculate the fromAddress.
sha = hashlib.new('sha512')
sha.update(sendersN+sendersE)
ripe = hashlib.new('ripemd160')
ripe.update(sha.digest())
#We have reached the end of the public key. Let's store it in case we want to reply to this person.
#We don't have the correct nonce in order to send out a pubkey message so we'll just fill it with 1's. We won't be able to send this pubkey to others (without doing the proof of work ourselves, which this program is programmed to not do.)
t = (ripe.digest(),False,'\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF'+data[20+messageVersionLength:endOfThePublicKeyPosition],int(time.time())+2419200) #after one month we may remove this pub key from our database.
sqlLock.acquire()
sqlSubmitQueue.put('''INSERT INTO pubkeys VALUES (?,?,?,?)''')
sqlSubmitQueue.put(t)
sqlReturnQueue.get()
sqlLock.release()
blockMessage = False #Set to True if the user shouldn't see the message according to black or white lists.
fromAddress = encodeAddress(sendersAddressVersionNumber,sendersStreamNumber,ripe.digest())
if config.get('bitmessagesettings', 'blackwhitelist') == 'black':
t = (fromAddress,)
sqlLock.acquire()
sqlSubmitQueue.put('''SELECT label, enabled FROM blacklist where address=?''')
sqlSubmitQueue.put(t)
queryreturn = sqlReturnQueue.get()
sqlLock.release()
for row in queryreturn:
label, enabled = row
if enabled:
print 'Message ignored because address is in blacklist.'
blockMessage = True
else: #We're using a whitelist
t = (fromAddress,)
sqlLock.acquire()
sqlSubmitQueue.put('''SELECT label, enabled FROM whitelist where address=?''')
sqlSubmitQueue.put(t)
queryreturn = sqlReturnQueue.get()
sqlLock.release()
if queryreturn == []:
print 'Message ignored because address not in whitelist.'
blockMessage = True
for row in queryreturn: #It could be in the whitelist but disabled. Let's check.
label, enabled = row
if not enabled:
print 'Message ignored because address in whitelist but not enabled.'
blockMessage = True
if not blockMessage:
print 'fromAddress:', fromAddress
print 'First 150 characters of message:', repr(message[:150])
#Now I would like to find the destination address (my address). Right now all I have is the ripe hash. I realize that I
#could have a data structure devoted to this task, or maintain an indexed table in the sql database, but I would prefer to
#minimize the number of data structures this program uses, and searching linearly through a short list for each message
#received doesn't take very long anyway.
configSections = config.sections()
for addressInKeysFile in configSections:
if addressInKeysFile <> 'bitmessagesettings':
status,addressVersionNumber,streamNumber,hash = decodeAddress(addressInKeysFile)
if hash == key:
toAddress = addressInKeysFile
toLabel = config.get(addressInKeysFile, 'label')
if toLabel == '':
toLabel = addressInKeysFile
if messageEncodingType == 2:
headers = Parser().parsestr(message)
subject = headers['subject']
body = headers['body']
elif messageEncodingType == 1:
body = message
subject = ''
elif messageEncodingType == 0:
print 'messageEncodingType == 0. Doing nothing with the message. They probably just sent it so that we would store their public key or send their ack data for them.'
else:
body = 'Unknown encoding type.\n\n' + repr(message)
subject = ''
if messageEncodingType <> 0:
sqlLock.acquire()
t = (inventoryHash,toAddress,fromAddress,subject,int(time.time()),body,'inbox')
sqlSubmitQueue.put('''INSERT INTO inbox VALUES (?,?,?,?,?,?,?)''')
sqlSubmitQueue.put(t)
sqlReturnQueue.get()
sqlLock.release()
self.emit(SIGNAL("displayNewMessage(PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject)"),inventoryHash,toAddress,fromAddress,subject,body)
#Now let's send the acknowledgement
POW, = unpack('>Q',hashlib.sha512(hashlib.sha512(ackData[24:]).digest()).digest()[4:12])
if POW <= 2**64 / ((len(ackData[24:])+payloadLengthExtraBytes) * averageProofOfWorkNonceTrialsPerByte):
print 'The POW is strong enough that this ackdataPayload will be accepted by the Bitmessage network.'
#Currently PyBitmessage only supports sending a message with the acknowledgement in the form of a msg message. But future versions, and other clients, could send any object and this software will relay them. This can be used to relay identifying information, like your public key, through another Bitmessage host in case you believe that your Internet connection is being individually watched. You may pick a random address, hope its owner is online, and send a message with encoding type 0 so that they ignore the message but send your acknowledgement data over the network. If you send and receive many messages, it would also be clever to take someone else's acknowledgement data and use it for your own. Assuming that your message is delivered successfully, both will be acknowledged simultaneously (though if it is not delivered successfully, you will be in a pickle.)
#print 'self.data before:', repr(self.data)
self.data = self.data[:self.payloadLength+24] + ackData + self.data[self.payloadLength+24:]
#print 'self.data after:', repr(self.data)
'''if ackData[4:16] == 'msg\x00\x00\x00\x00\x00\x00\x00\x00\x00':
inventoryHash = calculateInventoryHash(ackData[24:])
#objectType = 'msg'
#inventory[inventoryHash] = (objectType, self.streamNumber, ackData[24:], embeddedTime) #We should probably be storing the embeddedTime of the ackData, not the embeddedTime of the original incoming msg message, but this is probably close enough.
#print 'sending the inv for the msg which is actually an acknowledgement (within sendmsg function)'
#self.broadcastinv(inventoryHash)
self.data[:payloadLength+24] + ackData + self.data[payloadLength+24:]
elif ackData[4:16] == 'getpubkey\x00\x00\x00':
#objectType = 'getpubkey'
#inventory[inventoryHash] = (objectType, self.streamNumber, ackData[24:], embeddedTime) #We should probably be storing the embeddedTime of the ackData, not the embeddedTime of the original incoming msg message, but this is probably close enough.
#print 'sending the inv for the getpubkey which is actually an acknowledgement (within sendmsg function)'
self.data[:payloadLength+24] + ackData + self.data[payloadLength+24:]
elif ackData[4:16] == 'pubkey\x00\x00\x00\x00\x00\x00':
#objectType = 'pubkey'
#inventory[inventoryHash] = (objectType, self.streamNumber, ackData[24:], embeddedTime) #We should probably be storing the embeddedTime of the ackData, not the embeddedTime of the original incoming msg message, but this is probably close enough.
#print 'sending the inv for a pubkey which is actually an acknowledgement (within sendmsg function)'
self.data[:payloadLength+24] + ackData + self.data[payloadLength+24:]
elif ackData[4:16] == 'broadcast\x00\x00\x00':
#objectType = 'broadcast'
#inventory[inventoryHash] = (objectType, self.streamNumber, ackData[24:], embeddedTime) #We should probably be storing the embeddedTime of the ackData, not the embeddedTime of the original incoming msg message, but this is probably close enough.
#print 'sending the inv for a broadcast which is actually an acknowledgement (within sendmsg function)'
self.data[:payloadLength+24] + ackData + self.data[payloadLength+24:]'''
else:
print 'ACK POW not strong enough to be accepted by the Bitmessage network.'
else:
print 'This program cannot decode messages from addresses with versions higher than 1. Ignoring.'
statusbar = 'This program cannot decode messages from addresses with versions higher than 1. Ignoring it.'
self.emit(SIGNAL("updateStatusBar(PyQt_PyObject)"),statusbar)
else:
print 'Error: Cannot decode incoming msg versions higher than 1. Assuming the sender isn\' being silly, you should upgrade Bitmessage because this message shall be ignored.'
statusbar = 'Error: Cannot decode incoming msg versions higher than 1. Assuming the sender isn\' being silly, you should upgrade Bitmessage because this message shall be ignored.'
self.emit(SIGNAL("updateStatusBar(PyQt_PyObject)"),statusbar)
else:
printLock.acquire()
print 'Decryption unsuccessful.'
printLock.release()
infile.close()
outfile.close()
#We have received a pubkey
def recpubkey(self):
inventoryHash = calculateInventoryHash(self.data[24:self.payloadLength+24])
if inventoryHash in inventory:
print 'We have already received this pubkey. Ignoring it.'
return
#We must check to make sure the proof of work is sufficient.
POW, = unpack('>Q',hashlib.sha512(hashlib.sha512(self.data[24:24+self.payloadLength]).digest()).digest()[4:12])
print 'POW:', POW
if POW > 2**64 / ((self.payloadLength+payloadLengthExtraBytes) * averageProofOfWorkNonceTrialsPerByte):
printLock.acquire()
print 'The Proof of Work in the pubkey message is insufficient. Ignoring it.'
print 'pubkey payload length:', len(self.data[24:self.payloadLength+24])
print repr(self.data[24:self.payloadLength+24])
printLock.release()
return
objectType = 'pubkey'
inventory[inventoryHash] = (objectType, self.streamNumber, self.data[24:self.payloadLength+24], int(time.time()))
self.broadcastinv(inventoryHash)
self.emit(SIGNAL("incrementNumberOfPubkeysProcessed()"))
readPosition = 24 #for the message header
readPosition += 8 #for the nonce
bitfieldBehaviors = self.data[readPosition:readPosition+4]
readPosition += 4 #for the bitfield of behaviors and features
addressVersion, varintLength = decodeVarint(self.data[readPosition:readPosition+10])
readPosition += varintLength
streamNumber, varintLength = decodeVarint(self.data[readPosition:readPosition+10])
readPosition += varintLength
#ripe = self.data[readPosition:readPosition+20]
#readPosition += 20 #for the ripe hash
nLength, varintLength = decodeVarint(self.data[readPosition:readPosition+10])
readPosition += varintLength
nString = self.data[readPosition:readPosition+nLength]
readPosition += nLength
eLength, varintLength = decodeVarint(self.data[readPosition:readPosition+10])
readPosition += varintLength
eString = self.data[readPosition:readPosition+eLength]
readPosition += eLength
sha = hashlib.new('sha512')
sha.update(nString+eString)
ripeHasher = hashlib.new('ripemd160')
ripeHasher.update(sha.digest())
ripe = ripeHasher.digest()
print 'within recpubkey, addressVersion', addressVersion
print 'streamNumber', streamNumber
print 'ripe', ripe
print 'n=', convertStringToInt(nString)
print 'e=', convertStringToInt(eString)
t = (ripe,True,self.data[24:24+self.payloadLength],int(time.time())+604800) #after one week we may remove this pub key from our database.
sqlLock.acquire()
sqlSubmitQueue.put('''INSERT INTO pubkeys VALUES (?,?,?,?)''')
sqlSubmitQueue.put(t)
sqlReturnQueue.get()
sqlLock.release()
print 'added foreign pubkey into our database'
workerQueue.put(('newpubkey',(addressVersion,streamNumber,ripe)))
#We have received a getpubkey message
def recgetpubkey(self):
inventoryHash = calculateInventoryHash(self.data[24:self.payloadLength+24])
if inventoryHash in inventory:
print 'We have already received this getpubkey request. Ignoring it.'
elif isInSqlInventory(inventoryHash):
print 'We have already received this getpubkey request (it is stored on disk in the SQL inventory). Ignoring it.'
else:
objectType = 'pubkeyrequest'
inventory[inventoryHash] = (objectType, self.streamNumber, self.data[24:self.payloadLength+24], int(time.time()))
#First we must check to make sure the proof of work is sufficient.
POW, = unpack('>Q',hashlib.sha512(hashlib.sha512(self.data[24:24+self.payloadLength]).digest()).digest()[4:12])
print 'POW:', POW
if POW > 2**64 / ((self.payloadLength+payloadLengthExtraBytes) * averageProofOfWorkNonceTrialsPerByte):
print 'POW value in getpubkey message is insufficient. Ignoring it.'
return
#Now let us make sure that the getpubkey request isn't too old or with a fake (future) time.
embeddedTime, = unpack('>I',self.data[32:36])
if embeddedTime > int(time.time())+10800:
print 'The time in this getpubkey message is too new. Ignoring it. Time:', embeddedTime
return
if embeddedTime < int(time.time())-maximumAgeOfAnObjectThatIAmWillingToAccept:
print 'The time in this getpubkey message is too old. Ignoring it. Time:', embeddedTime
return
addressVersionNumber, addressVersionLength = decodeVarint(self.data[36:42])
if addressVersionNumber > 1:
print 'The addressVersionNumber of the pubkey is too high. Can\'t understand. Ignoring it.'
return
streamNumber, streamNumberLength = decodeVarint(self.data[36+addressVersionLength:42+addressVersionLength])
if streamNumber <> self.streamNumber:
print 'The streamNumber', streamNumber, 'doesn\'t match our stream number:', self.streamNumber
return
if self.data[36+addressVersionLength+streamNumberLength:56+addressVersionLength+streamNumberLength] in myAddressHashes:
print 'Found getpubkey requested hash in my list of hashes.'
#check to see whether we have already calculated the nonce and transmitted this key before
sqlLock.acquire()#released at the bottom of this payload generation section
t = (self.data[36+addressVersionLength+streamNumberLength:56+addressVersionLength+streamNumberLength],) #this prevents SQL injection
sqlSubmitQueue.put('SELECT * FROM pubkeys WHERE hash=?')
sqlSubmitQueue.put(t)
queryreturn = sqlReturnQueue.get()
#print 'queryreturn', queryreturn
if queryreturn == []:
print 'pubkey request is for me but the pubkey is not in our database of pubkeys. Making it.'
payload = '\x00\x00\x00\x01' #bitfield of features supported by me (see the wiki).
payload += self.data[36:36+addressVersionLength+streamNumberLength]
#print int(config.get(encodeAddress(addressVersionNumber,streamNumber,self.data[36+addressVersionLength+streamNumberLength:56+addressVersionLength+streamNumberLength]), 'n'))
nString = convertIntToString(int(config.get(encodeAddress(addressVersionNumber,streamNumber,self.data[36+addressVersionLength+streamNumberLength:56+addressVersionLength+streamNumberLength]), 'n')))
eString = convertIntToString(config.getint(encodeAddress(addressVersionNumber,streamNumber,self.data[36+addressVersionLength+streamNumberLength:56+addressVersionLength+streamNumberLength]), 'e'))
payload += encodeVarint(len(nString))
payload += nString
payload += encodeVarint(len(eString))
payload += eString
nonce = 0
trialValue = 99999999999999999999
target = 2**64 / ((len(payload)+payloadLengthExtraBytes+8) * averageProofOfWorkNonceTrialsPerByte) #The 108 added to the payload length is 8 for the size of the nonce + 50 as an extra penalty simply for having this seperate message exist.
print '(For pubkey message) Doing proof of work...'
while trialValue > target:
nonce += 1
trialValue, = unpack('>Q',hashlib.sha512(hashlib.sha512(pack('>Q',nonce) + payload).digest()).digest()[4:12])
print '(For pubkey message) Found proof of work', trialValue, 'Nonce:', nonce
payload = pack('>Q',nonce) + payload
t = (self.data[36+addressVersionLength+streamNumberLength:56+addressVersionLength+streamNumberLength],True,payload,int(time.time())+1209600) #after two weeks (1,209,600 seconds), we may remove our own pub key from our database. It will be regenerated and put back in the database if it is requested.
sqlSubmitQueue.put('''INSERT INTO pubkeys VALUES (?,?,?,?)''')
sqlSubmitQueue.put(t)
queryreturn = sqlReturnQueue.get()
#Now that we have the key either from getting it earlier or making it and storing it ourselves...
t = (self.data[36+addressVersionLength+streamNumberLength:56+addressVersionLength+streamNumberLength],) #this prevents SQL injection
sqlSubmitQueue.put('SELECT * FROM pubkeys WHERE hash=?')
sqlSubmitQueue.put(t)
queryreturn = sqlReturnQueue.get()
for row in queryreturn:
hash, havecorrectnonce, payload, timeLastRequested = row
if timeLastRequested < int(time.time())+604800: #if the last time anyone asked about this hash was this week, extend the time.
t = (int(time.time())+604800,hash)
sqlSubmitQueue.put('''UPDATE pubkeys set time=? WHERE hash=?''')
sqlSubmitQueue.put(t)
queryreturn = sqlReturnQueue.get()
sqlLock.release()
inventoryHash = calculateInventoryHash(payload)
objectType = 'pubkey'
inventory[inventoryHash] = (objectType, self.streamNumber, payload, int(time.time()))
self.broadcastinv(inventoryHash)
else:
print 'Hash in getpubkey is not mine.'
#..but lets see if we have it stored from when it came in from someone else.
t = (self.data[36+addressVersionLength+streamNumberLength:56+addressVersionLength+streamNumberLength],) #this prevents SQL injection
sqlLock.acquire()
sqlSubmitQueue.put('''SELECT hash, time FROM pubkeys WHERE hash=? AND havecorrectnonce='True' ''')
sqlSubmitQueue.put(t)
queryreturn = sqlReturnQueue.get()
sqlLock.release()
print 'queryreturn', queryreturn
if queryreturn <> []:
print 'we have the public key. sending it.'
#We have it. Let's send it.
for row in queryreturn:
hash, timeLastRequested = row
if timeLastRequested < int(time.time())+604800: #if the last time anyone asked about this hash was this week, extend the time.
t = (int(time.time())+604800,hash)
sqlSubmitQueue.put('''UPDATE pubkeys set time=? WHERE hash=?''')
sqlSubmitQueue.put(t)
queryreturn = sqlReturnQueue.get()
inventoryHash = calculateInventoryHash(self.data[24:self.payloadLength+24])
objectType = 'pubkey'
inventory[inventoryHash] = (objectType, self.streamNumber, self.data[24:self.payloadLength+24], int(time.time()))
self.broadcastinv(inventoryHash)
else:
#We don't have it. We'll need to forward the getpubkey request to our peers.
print 'We don\' have the public key. Forwarding getpubkey message to peers.'
broadcastToSendDataQueues((self.streamNumber,'send',self.data[:self.payloadLength+24]))
#We have received an inv message
def recinv(self):
numberOfItemsInInv, lengthOfVarint = decodeVarint(self.data[24:34])
if numberOfItemsInInv == 1: #we'll just request this data from the person who advertised the object.
for i in range(numberOfItemsInInv):
if self.data[24+lengthOfVarint+(32*i):56+lengthOfVarint+(32*i)] in inventory:
print 'Inventory (in memory) has inventory item already.'
elif isInSqlInventory(self.data[24+lengthOfVarint+(32*i):56+lengthOfVarint+(32*i)]):
print 'Inventory (SQL on disk) has inventory item already.'
else:
self.sendgetdata(self.data[24+lengthOfVarint+(32*i):56+lengthOfVarint+(32*i)])
else:
print 'inv message lists', numberOfItemsInInv, 'objects.'
for i in range(numberOfItemsInInv): #upon finishing dealing with an incoming message, the receiveDataThread will request a random object from the peer. This way if we get multiple inv messages from multiple peers which list mostly the same objects, we will make getdata requests for different random objects from the various peers.
#print 'Adding object to self.objectsThatWeHaveYetToGet.'
self.objectsThatWeHaveYetToGet[self.data[24+lengthOfVarint+(32*i):56+lengthOfVarint+(32*i)]] = 0
print 'length of objectsThatWeHaveYetToGet', len(self.objectsThatWeHaveYetToGet)
#Send a getdata message to our peer to request the object with the given hash
def sendgetdata(self,hash):
print 'sending getdata with hash', repr(hash)
payload = '\x01' + hash
headerData = '\xe9\xbe\xb4\xd9' #magic bits, slighly different from Bitcoin's magic bits.
headerData = headerData + 'getdata\x00\x00\x00\x00\x00'
headerData = headerData + pack('>L',len(payload)) #payload length. Note that we add an extra 8 for the nonce.
headerData = headerData + hashlib.sha512(payload).digest()[:4]
self.sock.send(headerData + payload)
#We have received a getdata request from our peer
def recgetdata(self):
value, lengthOfVarint = decodeVarint(self.data[24:34])
#print 'Number of items in getdata request:', value
try:
for i in range(value):
hash = self.data[24+lengthOfVarint+(i*32):56+lengthOfVarint+(i*32)]
print 'getdata request for item:', repr(hash), 'length', len(hash)
#print 'inventory is', inventory
if hash in inventory:
objectType, streamNumber, payload, receivedTime = inventory[hash]
self.sendData(objectType,payload)
else:
t = (hash,)
sqlLock.acquire()
sqlSubmitQueue.put('''select objecttype, payload from inventory where hash=?''')
sqlSubmitQueue.put(t)
queryreturn = sqlReturnQueue.get()
sqlLock.release()
if queryreturn <> []:
for row in queryreturn:
objectType, payload = row
print 'Sending data out of inventory that came from sql.' #todo: remove line
self.sendData(objectType,payload)
else:
print 'Someone asked for an object with a getdata which is not in either our memory inventory or our SQL inventory. That shouldn\'t have happened.'
except:
pass #someone is probably trying to cause a program error by, for example, making a request for 10 items but only including the hashes for 5.
#Our peer has requested (in a getdata message) that we send an object.
def sendData(self,objectType,payload):
if objectType == 'pubkey':
print 'sending pubkey'
headerData = '\xe9\xbe\xb4\xd9' #magic bits, slighly different from Bitcoin's magic bits.
headerData = headerData + 'pubkey\x00\x00\x00\x00\x00\x00'
headerData = headerData + pack('>L',len(payload)) #payload length. Note that we add an extra 8 for the nonce.
headerData = headerData + hashlib.sha512(payload).digest()[:4]
self.sock.send(headerData + payload)
elif objectType == 'pubkeyrequest':
print 'sending pubkeyrequest'
headerData = '\xe9\xbe\xb4\xd9' #magic bits, slighly different from Bitcoin's magic bits.
headerData = headerData + 'getpubkey\x00\x00\x00'
headerData = headerData + pack('>L',len(payload)) #payload length. Note that we add an extra 8 for the nonce.
headerData = headerData + hashlib.sha512(payload).digest()[:4]
self.sock.send(headerData + payload)
elif objectType == 'msg':
print 'sending msg'
headerData = '\xe9\xbe\xb4\xd9' #magic bits, slighly different from Bitcoin's magic bits.
headerData = headerData + 'msg\x00\x00\x00\x00\x00\x00\x00\x00\x00'
headerData = headerData + pack('>L',len(payload)) #payload length. Note that we add an extra 8 for the nonce.
headerData = headerData + hashlib.sha512(payload).digest()[:4]
self.sock.send(headerData + payload)
elif objectType == 'broadcast':
print 'sending broadcast'
headerData = '\xe9\xbe\xb4\xd9' #magic bits, slighly different from Bitcoin's magic bits.
headerData = headerData + 'broadcast\x00\x00\x00'
headerData = headerData + pack('>L',len(payload)) #payload length. Note that we add an extra 8 for the nonce.
headerData = headerData + hashlib.sha512(payload).digest()[:4]
self.sock.send(headerData + payload)
elif objectType == 'getpubkey':
print 'sending getpubkey'
headerData = '\xe9\xbe\xb4\xd9' #magic bits, slighly different from Bitcoin's magic bits.
headerData = headerData + 'getpubkey\x00\x00\x00' #version command
headerData = headerData + pack('>L',len(payload)) #payload length
headerData = headerData + hashlib.sha512(payload).digest()[0:4]
self.sock.send(headerData + payload)
else:
sys.stderr.write('Error: sendData has been asked to send a strange objectType: %s\n' % str(objectType))
#Send an inv message with just one hash to all of our peers
def broadcastinv(self,hash):
print 'sending inv'
#payload = '\x01' + pack('>H',objectType) + hash
payload = '\x01' + hash
headerData = '\xe9\xbe\xb4\xd9' #magic bits, slighly different from Bitcoin's magic bits.
headerData = headerData + 'inv\x00\x00\x00\x00\x00\x00\x00\x00\x00'
headerData = headerData + pack('>L',len(payload))
headerData = headerData + hashlib.sha512(payload).digest()[:4]
#self.sock.send(headerData + payload)
broadcastToSendDataQueues((self.streamNumber, 'send', headerData + payload))
print 'broadcasting inv with hash:', repr(hash)
#We have received an addr message.
def recaddr(self):
listOfAddressDetailsToBroadcastToPeers = []
numberOfAddressesIncluded = 0
numberOfAddressesIncluded, lengthOfNumberOfAddresses = decodeVarint(self.data[24:29])
if verbose >= 1:
print 'addr message contains', numberOfAddressesIncluded, 'addresses.'
#print 'lengthOfNumberOfAddresses', lengthOfNumberOfAddresses
if numberOfAddressesIncluded > 1000:
return
needToWriteKnownNodesToDisk = False
for i in range(0,numberOfAddressesIncluded):
try:
if self.data[40+lengthOfNumberOfAddresses+(34*i):52+lengthOfNumberOfAddresses+(34*i)] != '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF':
printLock.acquire()
print 'Skipping IPv6 address.', repr(self.data[40+lengthOfNumberOfAddresses+(34*i):56+lengthOfNumberOfAddresses+(34*i)])
printLock.release()
continue
#print repr(self.data[6+lengthOfNumberOfAddresses+(34*i):18+lengthOfNumberOfAddresses+(34*i)])
except Exception, err:
if verbose >= 2:
printLock.acquire()
sys.stderr.write('ERROR TRYING TO UNPACK recaddr (to test for an IPv6 address). Message: %s\n' % str(err))
printLock.release()
break #giving up on unpacking any more. We should still be connected however.
try:
recaddrStream, = unpack('>I',self.data[28+lengthOfNumberOfAddresses+(34*i):32+lengthOfNumberOfAddresses+(34*i)])
except Exception, err:
if verbose >= 2:
printLock.acquire()
sys.stderr.write('ERROR TRYING TO UNPACK recaddr (recaddrStream). Message: %s\n' % str(err))
printLock.release()
break #giving up on unpacking any more. We should still be connected however.
try:
recaddrServices, = unpack('>Q',self.data[32+lengthOfNumberOfAddresses+(34*i):40+lengthOfNumberOfAddresses+(34*i)])
except Exception, err:
if verbose >= 2:
printLock.acquire()
sys.stderr.write('ERROR TRYING TO UNPACK recaddr (recaddrServices). Message: %s\n' % str(err))
printLock.release()
break #giving up on unpacking any more. We should still be connected however.
try:
recaddrPort, = unpack('>H',self.data[56+lengthOfNumberOfAddresses+(34*i):58+lengthOfNumberOfAddresses+(34*i)])
except Exception, err:
if verbose >= 2:
printLock.acquire()
sys.stderr.write('ERROR TRYING TO UNPACK recaddr (recaddrPort). Message: %s\n' % str(err))
printLock.release()
break #giving up on unpacking any more. We should still be connected however.
#print 'Within recaddr(): IP', recaddrIP, ', Port', recaddrPort, ', i', i
hostFromAddrMessage = socket.inet_ntoa(self.data[52+lengthOfNumberOfAddresses+(34*i):56+lengthOfNumberOfAddresses+(34*i)])
print 'hostFromAddrMessage', hostFromAddrMessage
timeSomeoneElseReceivedMessageFromThisNode, = unpack('>I',self.data[24+lengthOfNumberOfAddresses+(34*i):28+lengthOfNumberOfAddresses+(34*i)]) #This is the 'time' value in the received addr message.
if hostFromAddrMessage not in knownNodes[recaddrStream]:
if len(knownNodes[recaddrStream]) < 20000 and timeSomeoneElseReceivedMessageFromThisNode > (int(time.time())-10800) and timeSomeoneElseReceivedMessageFromThisNode < (int(time.time()) + 10800): #If we have more than 20000 nodes in our list already then just forget about it. Also, make sure that the time that someone else received a message from this node is within three hours from now.
knownNodes[recaddrStream][hostFromAddrMessage] = (recaddrPort, timeSomeoneElseReceivedMessageFromThisNode)
print 'added new node', hostFromAddrMessage, 'to knownNodes.'
needToWriteKnownNodesToDisk = True
hostDetails = (timeSomeoneElseReceivedMessageFromThisNode, recaddrStream, recaddrServices, hostFromAddrMessage, recaddrPort)
listOfAddressDetailsToBroadcastToPeers.append(hostDetails)
else:
PORT, timeLastReceivedMessageFromThisNode = knownNodes[recaddrStream][hostFromAddrMessage]#PORT in this case is either the port we used to connect to the remote node, or the port that was specified by someone else in a past addr message.
if (timeLastReceivedMessageFromThisNode < timeSomeoneElseReceivedMessageFromThisNode) and (timeSomeoneElseReceivedMessageFromThisNode < int(time.time())):
knownNodes[recaddrStream][hostFromAddrMessage] = (PORT, timeSomeoneElseReceivedMessageFromThisNode)
if PORT != recaddrPort:
sys.stderr.write('Strange occurance: The port specified in an addr message (%s) does not match the port (%s) that this program (or some other peer) used to connect to it (%s). Perhaps they changed their port or are using a strange NAT configuration.\n' % (str(recaddrPort), str(PORT), str(hostFromAddrMessage)))
if needToWriteKnownNodesToDisk: #Runs if any nodes were new to us. Also, share those nodes with our peers.
output = open(appdata + 'knownnodes.dat', 'wb')
pickle.dump(knownNodes, output)
output.close()
self.broadcastaddr(listOfAddressDetailsToBroadcastToPeers)
print 'knownNodes currently:', knownNodes
#Function runs when we want to broadcast an addr message to all of our peers. Runs when we learn of nodes that we didn't previously know about and want to share them with our peers.
def broadcastaddr(self,listOfAddressDetailsToBroadcastToPeers):
numberOfAddressesInAddrMessage = len(listOfAddressDetailsToBroadcastToPeers)
payload = ''
for hostDetails in listOfAddressDetailsToBroadcastToPeers:
timeLastReceivedMessageFromThisNode, streamNumber, services, host, port = hostDetails
payload += pack('>I',timeLastReceivedMessageFromThisNode)
payload += pack('>I',streamNumber)
payload += pack('>q',services) #service bit flags offered by this node
payload += '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF' + socket.inet_aton(host)
payload += pack('>H',port)#remote port
payload = encodeVarint(numberOfAddressesInAddrMessage) + payload
datatosend = '\xE9\xBE\xB4\xD9addr\x00\x00\x00\x00\x00\x00\x00\x00'
datatosend = datatosend + pack('>L',len(payload)) #payload length
datatosend = datatosend + hashlib.sha512(payload).digest()[0:4]
datatosend = datatosend + payload
if verbose >= 2:
printLock.acquire()
print 'Broadcasting addr with # of entries:', numberOfAddressesInAddrMessage
printLock.release()
broadcastToSendDataQueues((self.streamNumber, 'send', datatosend))
#Send a big addr message to our peer
def sendaddr(self):
addrsInMyStream = {}
addrsInChildStreamLeft = {}
addrsInChildStreamRight = {}
print 'knownNodes', knownNodes
#We are going to share a maximum number of 1000 addrs with our peer. 500 from this stream, 250 from the left child stream, and 250 from the right child stream.
if len(knownNodes[self.streamNumber]) > 0:
for i in range(500):
random.seed()
HOST, = random.sample(knownNodes[self.streamNumber], 1)
addrsInMyStream[HOST] = knownNodes[self.streamNumber][HOST]
if len(knownNodes[self.streamNumber*2]) > 0:
for i in range(250):
random.seed()
HOST, = random.sample(knownNodes[self.streamNumber*2], 1)
addrsInChildStreamLeft[HOST] = knownNodes[self.streamNumber*2][HOST]
if len(knownNodes[(self.streamNumber*2)+1]) > 0:
for i in range(250):
random.seed()
HOST, = random.sample(knownNodes[(self.streamNumber*2)+1], 1)
addrsInChildStreamRight[HOST] = knownNodes[(self.streamNumber*2)+1][HOST]
numberOfAddressesInAddrMessage = 0
payload = ''
print 'addrsInMyStream.items()', addrsInMyStream.items()
for HOST, value in addrsInMyStream.items():
PORT, timeLastReceivedMessageFromThisNode = value
if timeLastReceivedMessageFromThisNode > (int(time.time())- maximumAgeOfNodesThatIAdvertiseToOthers): #If it is younger than 3 hours old..
numberOfAddressesInAddrMessage += 1
payload += pack('>I',timeLastReceivedMessageFromThisNode)
payload += pack('>I',self.streamNumber)
payload += pack('>q',1) #service bit flags offered by this node
payload += '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF' + socket.inet_aton(HOST)
payload += pack('>H',PORT)#remote port
for HOST, value in addrsInChildStreamLeft.items():
PORT, timeLastReceivedMessageFromThisNode = value
if timeLastReceivedMessageFromThisNode > (int(time.time())- maximumAgeOfNodesThatIAdvertiseToOthers): #If it is younger than 3 hours old..
numberOfAddressesInAddrMessage += 1
payload += pack('>I',timeLastReceivedMessageFromThisNode)
payload += pack('>I',self.streamNumber*2)
payload += pack('>q',1) #service bit flags offered by this node
payload += '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF' + socket.inet_aton(HOST)
payload += pack('>H',PORT)#remote port
for HOST, value in addrsInChildStreamRight.items():
PORT, timeLastReceivedMessageFromThisNode = value
if timeLastReceivedMessageFromThisNode > (int(time.time())- maximumAgeOfNodesThatIAdvertiseToOthers): #If it is younger than 3 hours old..
numberOfAddressesInAddrMessage += 1
payload += pack('>I',timeLastReceivedMessageFromThisNode)
payload += pack('>I',(self.streamNumber*2)+1)
payload += pack('>q',1) #service bit flags offered by this node
payload += '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF' + socket.inet_aton(HOST)
payload += pack('>H',PORT)#remote port
payload = encodeVarint(numberOfAddressesInAddrMessage) + payload
datatosend = '\xE9\xBE\xB4\xD9addr\x00\x00\x00\x00\x00\x00\x00\x00'
datatosend = datatosend + pack('>L',len(payload)) #payload length
datatosend = datatosend + hashlib.sha512(payload).digest()[0:4]
datatosend = datatosend + payload
if verbose >= 2:
printLock.acquire()
print 'Sending addr with # of entries:', numberOfAddressesInAddrMessage
printLock.release()
self.sock.send(datatosend)
#We have received a version message
def recversion(self):
if self.payloadLength < 83:
self.data = ''
return
else:
self.remoteProtocolVersion, = unpack('>L',self.data[24:28])
#print 'remoteProtocolVersion', self.remoteProtocolVersion
self.myExternalIP = socket.inet_ntoa(self.data[64:68])
#print 'myExternalIP', self.myExternalIP
self.remoteNodeIncomingPort, = unpack('>H',self.data[94:96])
#print 'remoteNodeIncomingPort', self.remoteNodeIncomingPort
#print 'self.data[96:104]', repr(self.data[96:104])
#print 'eightBytesOfRandomDataUsedToDetectConnectionsToSelf', repr(eightBytesOfRandomDataUsedToDetectConnectionsToSelf)
useragentLength, lengthOfUseragentVarint = decodeVarint(self.data[104:108])
readPosition = 104 + lengthOfUseragentVarint + useragentLength
#Note that PyBitmessage curreutnly currentl supports a single stream per connection.
numberOfStreamsInVersionMessage, lengthOfNumberOfStreamsInVersionMessage = decodeVarint(self.data[readPosition:])
readPosition += lengthOfNumberOfStreamsInVersionMessage
self.streamNumber, lengthOfRemoteStreamNumber = decodeVarint(self.data[readPosition:])
print 'Remote node stream number:', self.streamNumber
#If this was an incoming connection, then the sendData thread doesn't know the stream. We have to set it.
if not self.initiatedConnection:
broadcastToSendDataQueues((0,'setStreamNumber',(self.HOST,self.streamNumber)))
if self.streamNumber != 1:
self.sock.close()
printLock.acquire()
print 'Closed connection to', self.HOST, 'because they are interested in stream', self.steamNumber,'.'
printLock.release()
self.data = ''
return
if self.data[96:104] == eightBytesOfRandomDataUsedToDetectConnectionsToSelf:
self.sock.close()
printLock.acquire()
print 'Closing connection to myself: ', self.HOST
printLock.release()
self.data = ''
return
knownNodes[self.streamNumber][self.HOST] = (self.remoteNodeIncomingPort, int(time.time()))
output = open(appdata + 'knownnodes.dat', 'wb')
pickle.dump(knownNodes, output)
output.close()
#I've commented out this code because it should be up to the newer node to decide whether their protocol version is incompatiable with the remote node's version.
'''if self.remoteProtocolVersion > 1:
print 'The remote node''s protocol version is too new for this program to understand. Disconnecting. It is:', self.remoteProtocolVersion
self.sock.close()
self.selfInitiatedConnectionList.remove(self)
else:'''
self.sendverack()
if self.initiatedConnection == False:
self.sendversion()
'''#Now that we have a friendly connection with this peer, we should advertise it to our other peers
payload = '\x01'
payload += pack('>I',int(time.time()))
payload += pack('>I',self.streamNumber)
payload += pack('>q',1) #service bit flags offered by this node
payload += '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF' + socket.inet_aton(self.HOST)
payload += pack('>H',self.remoteNodeIncomingPort)#remote port
datatosend = '\xE9\xBE\xB4\xD9addr\x00\x00\x00\x00\x00\x00\x00\x00'
datatosend += pack('>L',len(payload)) #payload length
datatosend += hashlib.sha512(payload).digest()[0:4]
datatosend += payload
broadcastToSendDataQueues((self.streamNumber, 'send', datatosend))'''
#Sends a version message
def sendversion(self):
global softwareVersion
payload = ''
payload += pack('>L',1) #protocol version.
payload += pack('>q',1) #bitflags of the services I offer.
payload += pack('>q',int(time.time()))
payload += pack('>q',1) #boolservices of remote connection. How can I even know this for sure? This is probably ignored by the remote host.
payload += '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF' + socket.inet_aton(self.HOST)
payload += pack('>H',self.PORT)#remote IPv6 and port
payload += pack('>q',1) #bitflags of the services I offer.
payload += '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF' + pack('>L',2130706433) # = 127.0.0.1. This will be ignored by the remote host. The actual remote connected IP will be used.
payload += pack('>H',config.getint('bitmessagesettings', 'port'))#my external IPv6 and port
random.seed()
payload += eightBytesOfRandomDataUsedToDetectConnectionsToSelf
userAgent = '/PyBitmessage:' + softwareVersion + '/' #Length of userAgent must be less than 253.
payload += pack('>B',len(userAgent)) #user agent string length. If the user agent is more than 252 bytes long, this code isn't going to work.
payload += userAgent
payload += encodeVarint(1) #The number of streams about which I care. PyBitmessage currently only supports 1.
payload += encodeVarint(self.streamNumber)
datatosend = '\xe9\xbe\xb4\xd9' #magic bits, slighly different from Bitcoin's magic bits.
datatosend = datatosend + 'version\x00\x00\x00\x00\x00' #version command
datatosend = datatosend + pack('>L',len(payload)) #payload length
datatosend = datatosend + hashlib.sha512(payload).digest()[0:4]
datatosend = datatosend + payload
printLock.acquire()
print 'Sending version packet: ', repr(datatosend)
printLock.release()
self.sock.send(datatosend)
#self.versionSent = 1
#Sends a verack message
def sendverack(self):
print 'Sending verack'
self.sock.sendall('\xE9\xBE\xB4\xD9\x76\x65\x72\x61\x63\x6B\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x82\x6D\xf0\x68')
self.verackSent = True
if self.verackReceived == True:
self.connectionFullyEstablished()
#Every connection to a peer has a sendDataThread (and also a receiveDataThread).
class sendDataThread(QThread):
def __init__(self, parent = None):
QThread.__init__(self, parent)
self.mailbox = Queue.Queue()
sendDataQueues.append(self.mailbox)
self.data = ''
def setup(self,sock,HOST,PORT,streamNumber):
self.sock = sock
self.HOST = HOST
self.PORT = PORT
self.streamNumber = streamNumber
self.lastTimeISentData = int(time.time()) #If this value increases beyond five minutes ago, we'll send a pong message to keep the connection alive.
printLock.acquire()
print 'The streamNumber of this sendDataThread at setup() is', self.streamNumber, self
printLock.release()
def sendVersionMessage(self):
#Note that there is another copy of this version-sending code in the receiveData class which would need to be changed if you make changes here.
global softwareVersion
payload = ''
payload += pack('>L',1) #protocol version.
payload += pack('>q',1) #bitflags of the services I offer.
payload += pack('>q',int(time.time()))
payload += pack('>q',1) #boolservices of remote connection. How can I even know this for sure? This is probably ignored by the remote host.
payload += '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF' + socket.inet_aton(self.HOST)
payload += pack('>H',self.PORT)#remote IPv6 and port
payload += pack('>q',1) #bitflags of the services I offer.
payload += '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF' + pack('>L',2130706433) # = 127.0.0.1. This will be ignored by the remote host. The actual remote connected IP will be used.
payload += pack('>H',config.getint('bitmessagesettings', 'port'))#my external IPv6 and port
random.seed()
payload += eightBytesOfRandomDataUsedToDetectConnectionsToSelf
userAgent = '/PyBitmessage:' + softwareVersion + '/' #Length of userAgent must be less than 253.
payload += pack('>B',len(userAgent)) #user agent string length. If the user agent is more than 252 bytes long, this code isn't going to work.
payload += userAgent
payload += encodeVarint(1) #The number of streams about which I care. PyBitmessage currently only supports 1 per connection.
payload += encodeVarint(self.streamNumber)
datatosend = '\xe9\xbe\xb4\xd9' #magic bits, slighly different from Bitcoin's magic bits.
datatosend = datatosend + 'version\x00\x00\x00\x00\x00' #version command
datatosend = datatosend + pack('>L',len(payload)) #payload length
datatosend = datatosend + hashlib.sha512(payload).digest()[0:4]
datatosend = datatosend + payload
printLock.acquire()
print 'Sending version packet: ', repr(datatosend)
printLock.release()
self.sock.send(datatosend)
self.versionSent = 1
def run(self):
message = ''
while True:
deststream,command,data = self.mailbox.get()
printLock.acquire()
print 'within a sendDataThread, destream:', deststream,', Command:', command, self
printLock.release()
if deststream == self.streamNumber or deststream == 0:
if command == 'shutdown':
if data == self.HOST or data == 'all':
printLock.acquire()
print 'sendDataThread thread (associated with', self.HOST,')', self, 'shutting down now.'
self.sock.close()
sendDataQueues.remove(self.mailbox)
print 'len of sendDataQueues', len(sendDataQueues)
printLock.release()
break
elif command == 'setStreamNumber':
hostInMessage, specifiedStreamNumber = data
if hostInMessage == self.HOST:
printLock.acquire()
print 'setting the stream number in the sendData thread to', specifiedStreamNumber, self
printLock.release()
self.streamNumber = specifiedStreamNumber
elif command == 'send':
try:
#To prevent some network analysis, 'leak' the data out to our peer after waiting a sort random amount of time.
random.seed()
#time.sleep(random.randrange(0, 5)) todo: uncomment this line.
self.sock.sendall(data)
self.lastTimeISentData = int(time.time())
except:
print 'self.sock.sendall failed'
self.sock.close()
sendDataQueues.remove(self.mailbox)
print 'sendDataThread thread', self, 'ending now'
break
elif command == 'pong':
if self.lastTimeISentData < (int(time.time()) - 298):
#Send out a pong message to keep the connection alive.
print 'Sending pong to', self.HOST, 'to keep connection alive.'
try:
self.sock.sendall('\xE9\xBE\xB4\xD9\x70\x6F\x6E\x67\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x82\x6D\xf0\x68')
self.lastTimeISentData = int(time.time())
except:
print 'self.sock.send pong failed'
self.sock.close()
sendDataQueues.remove(self.mailbox)
print 'sendDataThread thread', self, 'ending now'
break
#Wen you want to command a sendDataThread to do something, like shutdown or send some data, this function puts your data into the queues for each of the sendDataThreads. The sendDataThreads are responsible for putting their queue into (and out of) the sendDataQueues list.
def broadcastToSendDataQueues(data):
#print 'running broadcastToSendDataQueues'
for q in sendDataQueues:
q.put((data))
def flushInventory():
#Note that the singleCleanerThread clears out the inventory dictionary from time to time, although it only clears things that have been in the dictionary for a long time. This clears the inventory dictionary Now.
sqlLock.acquire()
for hash, storedValue in inventory.items():
objectType, streamNumber, payload, receivedTime = storedValue
t = (hash,objectType,streamNumber,payload,receivedTime)
sqlSubmitQueue.put('''INSERT INTO inventory VALUES (?,?,?,?,?)''')
sqlSubmitQueue.put(t)
sqlReturnQueue.get()
del inventory[hash]
sqlLock.release()
def isInSqlInventory(hash):
t = (hash,)
sqlLock.acquire()
sqlSubmitQueue.put('''select hash from inventory where hash=?''')
sqlSubmitQueue.put(t)
queryreturn = sqlReturnQueue.get()
sqlLock.release()
if queryreturn == []:
return False
else:
return True
def convertIntToString(n):
a = __builtins__.hex(n)
if a[-1:] == 'L':
a = a[:-1]
if (len(a) % 2) == 0:
return a[2:].decode('hex')
else:
return ('0'+a[2:]).decode('hex')
def convertStringToInt(s):
return int(s.encode('hex'), 16)
#This thread exists because SQLITE3 is so un-threadsafe that we must submit queries to it and it puts results back in a different queue. They won't let us just use locks.
class sqlThread(QThread):
def __init__(self, parent = None):
QThread.__init__(self, parent)
def run(self):
self.conn = sqlite3.connect(appdata + 'messages.dat' )
self.conn.text_factory = str
self.cur = self.conn.cursor()
try:
self.cur.execute( '''CREATE TABLE inbox (msgid blob, toaddress text, fromaddress text, subject text, received text, message text, folder text, UNIQUE(msgid) ON CONFLICT REPLACE)''' )
self.cur.execute( '''CREATE TABLE sent (msgid blob, toaddress text, toripe blob, fromaddress text, subject text, message text, ackdata blob, lastactiontime integer, status text, pubkeyretrynumber integer, msgretrynumber integer, folder text)''' )
self.cur.execute( '''CREATE TABLE subscriptions (label text, address text, enabled bool)''' )
self.cur.execute( '''CREATE TABLE addressbook (label text, address text)''' )
self.cur.execute( '''CREATE TABLE blacklist (label text, address text, enabled bool)''' )
self.cur.execute( '''CREATE TABLE whitelist (label text, address text, enabled bool)''' )
self.cur.execute( '''CREATE TABLE pubkeys (hash blob, havecorrectnonce bool, transmitdata blob, time blob, UNIQUE(hash, havecorrectnonce, transmitdata) ON CONFLICT REPLACE)''' )
self.cur.execute( '''CREATE TABLE inventory (hash blob, objecttype text, streamnumber int, payload blob, receivedtime integer, UNIQUE(hash) ON CONFLICT REPLACE)''' )
self.cur.execute( '''CREATE TABLE knownnodes (timelastseen int, stream int, services blob, host blob, port blob, UNIQUE(host) ON CONFLICT REPLACE)''' ) #This table isn't used in the program yet but I have a feeling that we'll need it.
self.conn.commit()
print 'Created messages database file'
except Exception, err:
if str(err) == 'table inbox already exists':
print 'Database file already exists.'
else:
sys.stderr.write('ERROR trying to create database file (message.dat). Error message: %s\n' % str(err))
sys.exit()
try:
testpayload = '\x00\x00'
t = ('1234','True',testpayload,'12345678')
self.cur.execute( '''INSERT INTO pubkeys VALUES(?,?,?,?)''',t)
self.conn.commit()
self.cur.execute('''SELECT transmitdata FROM pubkeys WHERE hash='1234' ''')
queryreturn = self.cur.fetchall()
for row in queryreturn:
transmitdata, = row
self.cur.execute('''DELETE FROM pubkeys WHERE hash='1234' ''')
self.conn.commit()
if transmitdata == '':
sys.stderr.write('Problem: The version of SQLite you have cannot store Null values. Please download and install the latest revision of your version of Python (for example, the latest Python 2.7 revision) and try again. Exiting.\n')
sys.exit()
except Exception, err:
print err
while True:
item = sqlSubmitQueue.get()
parameters = sqlSubmitQueue.get()
#print 'item', item
#print 'parameters', parameters
self.cur.execute(item, parameters)
sqlReturnQueue.put(self.cur.fetchall())
sqlSubmitQueue.task_done()
self.conn.commit()
'''The singleCleaner class is a timer-driven thread that cleans data structures to free memory, resends messages when a remote node doesn't respond, and sends pong messages to keep connections alive if the network isn't busy.
It cleans these data structures in memory:
inventory (moves data to the on-disk sql database)
It cleans these tables on the disk:
inventory (clears data more than 2 days and 12 hours old)
pubkeys (clears data older than the date specified in the table. This is because we won't want to hold pubkeys that show up randomly as long as those that we have actually have used.)
It resends messages when there has been no response:
resends getpubkey messages in two days (then 4 days, then 8 days, etc...)
resends msg messages in two days (then 4 days, then 8 days, etc...)
'''
class singleCleaner(QThread):
def __init__(self, parent = None):
QThread.__init__(self, parent)
def run(self):
timeWeLastClearedInventoryAndPubkeysTables = 0
while True:
time.sleep(300)
sqlLock.acquire()
for hash, storedValue in inventory.items():
objectType, streamNumber, payload, receivedTime = storedValue
if int(time.time())- 3600 > receivedTime:
t = (hash,objectType,streamNumber,payload,receivedTime)
sqlSubmitQueue.put('''INSERT INTO inventory VALUES (?,?,?,?,?)''')
sqlSubmitQueue.put(t)
sqlReturnQueue.get()
del inventory[hash]
sqlLock.release()
broadcastToSendDataQueues((0, 'pong', 'no data')) #commands the sendData threads to send out a pong message if they haven't sent anything else in the last five minutes. The socket timeout-time is 10 minutes.
if timeWeLastClearedInventoryAndPubkeysTables < int(time.time()) - 7380:
timeWeLastClearedInventoryAndPubkeysTables = int(time.time())
#inventory (moves data from the inventory data structure to the on-disk sql database)
sqlLock.acquire()
#inventory (clears data more than 2 days and 12 hours old)
t = (int(time.time())-lengthOfTimeToLeaveObjectsInInventory,)
sqlSubmitQueue.put('''DELETE FROM inventory WHERE receivedtime<?''')
sqlSubmitQueue.put(t)
sqlReturnQueue.get()
#pubkeys (clears data older than the date specified in the table. This is because we won't want to hold pubkeys that show up randomly as long as those that we have actually have used (unless someone can come up with a decent attack based on this behavior.))
t = (int(time.time()),)
sqlSubmitQueue.put('''DELETE FROM pubkeys WHERE time<?''')
sqlSubmitQueue.put(t)
sqlReturnQueue.get()
t = ()
sqlSubmitQueue.put('''select toaddress, toripe, fromaddress, subject, message, ackdata, lastactiontime, status, pubkeyretrynumber, msgretrynumber FROM sent WHERE (status='findingpubkey' OR status='sentmessage') ''')
sqlSubmitQueue.put(t)
queryreturn = sqlReturnQueue.get()
for row in queryreturn:
toaddress, toripe, fromaddress, subject, message, ackdata, lastactiontime, status, pubkeyretrynumber, msgretrynumber = row
if status == 'findingpubkey':
if int(time.time()) - lastactiontime > (maximumAgeOfAnObjectThatIAmWillingToAccept * (2 ** (pubkeyretrynumber))):
print 'It has been a long time and we haven\'t heard a response to our getpubkey request. Sending again.'
try:
del neededPubkeys[toripe]
except:
pass
workerQueue.put(('sendmessage',toaddress))
t = (int(time.time()),pubkeyretrynumber+1,toripe)
sqlSubmitQueue.put('''UPDATE sent SET lastactiontime=?, pubkeyretrynumber=? WHERE toripe=?''')
sqlSubmitQueue.put(t)
sqlReturnQueue.get()
#self.emit(SIGNAL("updateSentItemStatusByHash(PyQt_PyObject,PyQt_PyObject)"),toripe,'Public key requested again. ' + strftime(config.get('bitmessagesettings', 'timeformat'),localtime(int(time.time()))))
else:# status == sentmessage
if int(time.time()) - lastactiontime > (maximumAgeOfAnObjectThatIAmWillingToAccept * (2 ** (msgretrynumber))):
print 'It has been a long time and we haven\'t heard an acknowledgement to our msg. Sending again.'
t = (int(time.time()),msgretrynumber+1,'findingpubkey',ackdata)
sqlSubmitQueue.put('''UPDATE sent SET lastactiontime=?, msgretrynumber=?, status=? WHERE ackdata=?''')
sqlSubmitQueue.put(t)
sqlReturnQueue.get()
#self.emit(SIGNAL("updateSentItemStatusByAckdata(PyQt_PyObject,PyQt_PyObject)"),ackdata,'Message sent again because the acknowledgement was never received. ' + strftime(config.get('bitmessagesettings', 'timeformat'),localtime(int(time.time()))))
workerQueue.put(('sendmessage',toaddress))
sqlLock.release()
#This thread, of which there is only one, does the heavy lifting: calculating POWs.
class singleWorker(QThread):
def __init__(self, parent = None):
QThread.__init__(self, parent)
def run(self):
sqlLock.acquire()
sqlSubmitQueue.put('SELECT toripe FROM sent WHERE status=?')
sqlSubmitQueue.put(('findingpubkey',))
queryreturn = sqlReturnQueue.get()
sqlLock.release()
for row in queryreturn:
toripe, = row
neededPubkeys[toripe] = 0
self.sendBroadcast() #just in case there are any proof of work tasks for Broadcasts that have yet to be sent.
#Now let us see if there are any proofs of work for msg messages that we have yet to complete..
sqlLock.acquire()
t = ('doingpow',)
sqlSubmitQueue.put('SELECT toripe FROM sent WHERE status=?')
sqlSubmitQueue.put(t)
queryreturn = sqlReturnQueue.get()
sqlLock.release()
for row in queryreturn:
toripe, = row
self.sendMsg(toripe)
while True:
command, data = workerQueue.get()
#statusbar = 'The singleWorker thread is working on work.'
#self.emit(SIGNAL("updateStatusBar(PyQt_PyObject)"),statusbar)
if command == 'sendmessage':
toAddress = data
toStatus,toAddressVersionNumber,toStreamNumber,toRipe = decodeAddress(toAddress)
#print 'message type', type(message)
#print repr(message.toUtf8())
#print str(message.toUtf8())
sqlLock.acquire()
sqlSubmitQueue.put('SELECT * FROM pubkeys WHERE hash=?')
sqlSubmitQueue.put((toRipe,))
queryreturn = sqlReturnQueue.get()
sqlLock.release()
#print 'queryreturn', queryreturn
if queryreturn == []:
#We'll need to request the pub key because we don't have it.
if not toRipe in neededPubkeys:
neededPubkeys[toRipe] = 0
print 'requesting pubkey:', repr(toRipe)
self.requestPubKey(toAddressVersionNumber,toStreamNumber,toRipe)
else:
print 'We have already requested this pubkey (the ripe hash is in neededPubkeys). We will re-request again soon.'
self.emit(SIGNAL("updateSentItemStatusByHash(PyQt_PyObject,PyQt_PyObject)"),toRipe,'Public key was requested earlier. Receiver must be offline. Will retry.')
else:
print 'We already have the necessary public key.'
self.sendMsg(toRipe)
elif command == 'sendbroadcast':
print 'Within WorkerThread, processing sendbroadcast command.'
fromAddress,subject,message = data
self.sendBroadcast()
elif command == 'newpubkey':
toAddressVersion,toStreamNumber,toRipe = data
if toRipe in neededPubkeys:
print 'We have been awaiting the arrival of this pubkey.'
del neededPubkeys[toRipe]
self.sendMsg(toRipe)
else:
print 'We don\'t need this pub key. We didn\'t ask for it. Pubkey hash:', repr(toRipe)
workerQueue.task_done()
def sendBroadcast(self):
sqlLock.acquire()
t = ('broadcastpending',)
sqlSubmitQueue.put('SELECT fromaddress, subject, message, ackdata FROM sent WHERE status=?')
sqlSubmitQueue.put(t)
queryreturn = sqlReturnQueue.get()
sqlLock.release()
for row in queryreturn:
#print 'within sendMsg, row is:', row
#msgid, toaddress, toripe, fromaddress, subject, message, ackdata, lastactiontime, status = row
fromaddress, subject, body, ackdata = row
messageToTransmit = '\x02'
messageToTransmit += encodeVarint(len('Subject:' + subject + '\n' + 'Body:' + body)) #Type 2 is simple UTF-8 message encoding.
messageToTransmit += 'Subject:' + subject + '\n' + 'Body:' + body
#We need the all the integers for our private key in order to sign our message, and we need our public key to send with the message.
n = config.getint(fromaddress, 'n')
e = config.getint(fromaddress, 'e')
d = config.getint(fromaddress, 'd')
p = config.getint(fromaddress, 'p')
q = config.getint(fromaddress, 'q')
nString = convertIntToString(n)
eString = convertIntToString(e)
myPubkey = rsa.PublicKey(n,e)
myPrivatekey = rsa.PrivateKey(n,e,d,p,q)
status,addressVersionNumber,streamNumber,ripe = decodeAddress(fromaddress)
#The payload of the broadcast message starts with a POW, but that will be added later.
payload = pack('>I',(int(time.time())))
payload += encodeVarint(1) #broadcast version
payload += encodeVarint(addressVersionNumber)
payload += encodeVarint(streamNumber)
payload += ripe
payload += encodeVarint(len(nString))
payload += nString
payload += encodeVarint(len(eString))
payload += eString
payload += messageToTransmit
signature = rsa.sign(messageToTransmit,myPrivatekey,'SHA-512')
print 'signature', repr(signature)
payload += signature
print 'nString', repr(nString)
print 'eString', repr(eString)
nonce = 0
trialValue = 99999999999999999999
target = 2**64 / ((len(payload)+payloadLengthExtraBytes+8) * averageProofOfWorkNonceTrialsPerByte)
print '(For broadcast message) Doing proof of work...'
while trialValue > target:
nonce += 1
trialValue, = unpack('>Q',hashlib.sha512(hashlib.sha512(pack('>Q',nonce) + payload).digest()).digest()[4:12])
print '(For broadcast message) Found proof of work', trialValue, 'Nonce:', nonce
payload = pack('>Q',nonce) + payload
inventoryHash = calculateInventoryHash(payload)
objectType = 'broadcast'
inventory[inventoryHash] = (objectType, streamNumber, payload, int(time.time()))
print 'sending inv (within sendBroadcast function)'
payload = '\x01' + inventoryHash
headerData = '\xe9\xbe\xb4\xd9' #magic bits, slighly different from Bitcoin's magic bits.
headerData = headerData + 'inv\x00\x00\x00\x00\x00\x00\x00\x00\x00'
headerData = headerData + pack('>L',len(payload)) #payload length. Note that we add an extra 8 for the nonce.
headerData = headerData + hashlib.sha512(payload).digest()[:4]
broadcastToSendDataQueues((streamNumber, 'send', headerData + payload))
self.emit(SIGNAL("updateSentItemStatusByAckdata(PyQt_PyObject,PyQt_PyObject)"),ackdata,'Broadcast sent at '+strftime(config.get('bitmessagesettings', 'timeformat'),localtime(int(time.time()))))
#Update the status of the message in the 'sent' table to have a 'broadcastsent' status
sqlLock.acquire()
t = ('broadcastsent',int(time.time()),fromaddress, subject, body,'broadcastpending')
sqlSubmitQueue.put('UPDATE sent SET status=?, lastactiontime=? WHERE fromaddress=? AND subject=? AND message=? AND status=?')
sqlSubmitQueue.put(t)
queryreturn = sqlReturnQueue.get()
sqlLock.release()
def sendMsg(self,toRipe):
sqlLock.acquire()
t = ('doingpow','findingpubkey',toRipe)
sqlSubmitQueue.put('UPDATE sent SET status=? WHERE status=? AND toripe=?')
sqlSubmitQueue.put(t)
queryreturn = sqlReturnQueue.get()
t = ('doingpow',toRipe)
sqlSubmitQueue.put('SELECT toaddress, fromaddress, subject, message, ackdata FROM sent WHERE status=? AND toripe=?')
sqlSubmitQueue.put(t)
queryreturn = sqlReturnQueue.get()
sqlLock.release()
for row in queryreturn:
toaddress, fromaddress, subject, message, ackdata = row
status,addressVersionNumber,toStreamNumber,hash = decodeAddress(toaddress)
#if hash == toRipe:
self.emit(SIGNAL("updateSentItemStatusByAckdata(PyQt_PyObject,PyQt_PyObject)"),ackdata,'Doing work necessary to send the message.')
printLock.acquire()
print 'Found the necessary message that needs to be sent with this pubkey.'
print 'First 150 characters of message:', message[:150]
printLock.release()
payload = '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' #this run of nulls allows the true message receiver to identify his message
payload += '\x01' #Message version.
payload += '\x00\x00\x00\x01'
fromStatus,fromAddressVersionNumber,fromStreamNumber,fromHash = decodeAddress(fromaddress)
payload += encodeVarint(fromAddressVersionNumber)
payload += encodeVarint(fromStreamNumber)
sendersN = convertIntToString(config.getint(fromaddress, 'n'))
payload += encodeVarint(len(sendersN))
payload += sendersN
sendersE = convertIntToString(config.getint(fromaddress, 'e'))
payload += encodeVarint(len(sendersE))
payload += sendersE
payload += '\x02' #Type 2 is simple UTF-8 message encoding.
messageToTransmit = 'Subject:' + subject + '\n' + 'Body:' + message
payload += encodeVarint(len(messageToTransmit))
payload += messageToTransmit
#Later, if anyone impliments clients that don't send the ack_data, then we should probably check here to make sure that the receiver will make use of this ack_data and not attach it if not.
fullAckPayload = self.generateFullAckMessage(ackdata,fromStreamNumber)
payload += encodeVarint(len(fullAckPayload))
payload += fullAckPayload
sendersPrivKey = rsa.PrivateKey(config.getint(fromaddress, 'n'),config.getint(fromaddress, 'e'),config.getint(fromaddress, 'd'),config.getint(fromaddress, 'p'),config.getint(fromaddress, 'q'))
payload += rsa.sign(payload,sendersPrivKey,'SHA-512')
sqlLock.acquire()
sqlSubmitQueue.put('SELECT * FROM pubkeys WHERE hash=?')
sqlSubmitQueue.put((toRipe,))
queryreturn = sqlReturnQueue.get()
sqlLock.release()
for row in queryreturn:
hash, havecorrectnonce, pubkeyPayload, timeLastRequested = row
readPosition = 8 #to bypass the nonce
bitfieldBehaviors = pubkeyPayload[8:12]
readPosition += 4 #to bypass the bitfield of behaviors
addressVersion, addressVersionLength = decodeVarint(pubkeyPayload[readPosition:readPosition+10])
readPosition += addressVersionLength
streamNumber, streamNumberLength = decodeVarint(pubkeyPayload[readPosition:readPosition+10])
readPosition += streamNumberLength
nLength, nLengthLength = decodeVarint(pubkeyPayload[readPosition:readPosition+10])
readPosition += nLengthLength
n = convertStringToInt(pubkeyPayload[readPosition:readPosition+nLength])
readPosition += nLength
eLength, eLengthLength = decodeVarint(pubkeyPayload[readPosition:readPosition+10])
readPosition += eLengthLength
e = convertStringToInt(pubkeyPayload[readPosition:readPosition+eLength])
receiversPubkey = rsa.PublicKey(n,e)
infile = cStringIO.StringIO(payload)
outfile = cStringIO.StringIO()
#print 'Encrypting using public key:', receiversPubkey
encrypt_bigfile(infile,outfile,receiversPubkey)
encrypted = outfile.getvalue()
infile.close()
outfile.close()
nonce = 0
trialValue = 99999999999999999999
embeddedTime = pack('>I',(int(time.time())))
encodedStreamNumber = encodeVarint(toStreamNumber)
#We are now dropping the unencrypted data in payload since it has already been encrypted and replacing it with the encrypted payload that we will send out.
payload = embeddedTime + encodedStreamNumber + encrypted
target = 2**64 / ((len(payload)+payloadLengthExtraBytes+8) * averageProofOfWorkNonceTrialsPerByte)
print '(For msg message) Doing proof of work. Target:', target
powStartTime = time.time()
while trialValue > target:
nonce += 1
trialValue, = unpack('>Q',hashlib.sha512(hashlib.sha512(pack('>Q',nonce) + payload).digest()).digest()[4:12])
print '(For msg message) Found proof of work', trialValue, 'Nonce:', nonce
print 'POW took', int(time.time()-powStartTime), 'seconds.', nonce/(time.time()-powStartTime), 'nonce trials per second.'
payload = pack('>Q',nonce) + payload
inventoryHash = calculateInventoryHash(payload)
objectType = 'msg'
inventory[inventoryHash] = (objectType, toStreamNumber, payload, int(time.time()))
self.emit(SIGNAL("updateSentItemStatusByAckdata(PyQt_PyObject,PyQt_PyObject)"),ackdata,'Message sent. Waiting on acknowledgement. Sent on ' + strftime(config.get('bitmessagesettings', 'timeformat'),localtime(int(time.time()))))
print 'sending inv (within sendmsg function)'
payload = '\x01' + inventoryHash
headerData = '\xe9\xbe\xb4\xd9' #magic bits, slighly different from Bitcoin's magic bits.
headerData = headerData + 'inv\x00\x00\x00\x00\x00\x00\x00\x00\x00'
headerData = headerData + pack('>L',len(payload)) #payload length. Note that we add an extra 8 for the nonce.
headerData = headerData + hashlib.sha512(payload).digest()[:4]
broadcastToSendDataQueues((toStreamNumber, 'send', headerData + payload))
#Update the status of the message in the 'sent' table to have a 'sent' status
sqlLock.acquire()
t = ('sentmessage',toaddress, fromaddress, subject, message,'doingpow')
sqlSubmitQueue.put('UPDATE sent SET status=? WHERE toaddress=? AND fromaddress=? AND subject=? AND message=? AND status=?')
sqlSubmitQueue.put(t)
queryreturn = sqlReturnQueue.get()
#Update the time in the pubkey sql database so that we'll hold the foreign pubkey for quite a while longer
t = (int(time.time())+31449600,toRipe) #Hold the pubkey for an entire year from today
sqlSubmitQueue.put('UPDATE pubkeys SET time=? WHERE hash=?')
sqlSubmitQueue.put(t)
queryreturn = sqlReturnQueue.get()
sqlLock.release()
def requestPubKey(self,addressVersionNumber,streamNumber,ripe):
payload = pack('>I',int(time.time()))
payload += encodeVarint(addressVersionNumber)
payload += encodeVarint(streamNumber)
payload += ripe
nonce = 0
trialValue = 99999999999999999999
#print 'trial value', trialValue
statusbar = 'Doing the computations necessary to request the recipient''s public key. (Doing the proof-of-work.)'
self.emit(SIGNAL("updateStatusBar(PyQt_PyObject)"),statusbar)
self.emit(SIGNAL("updateSentItemStatusByHash(PyQt_PyObject,PyQt_PyObject)"),ripe,'Doing work necessary to request public key.')
print 'Doing proof-of-work necessary to send getpubkey message.'
target = 2**64 / ((len(payload)+payloadLengthExtraBytes+8) * averageProofOfWorkNonceTrialsPerByte)
while trialValue > target:
nonce += 1
trialValue, = unpack('>Q',hashlib.sha512(hashlib.sha512(pack('>Q',nonce) + payload).digest()).digest()[4:12])
print 'Found proof of work', trialValue, 'Nonce:', nonce
payload = pack('>Q',nonce) + payload
inventoryHash = calculateInventoryHash(payload)
objectType = 'getpubkey'
inventory[inventoryHash] = (objectType, streamNumber, payload, int(time.time()))
print 'sending inv (for the getpubkey message)'
#payload = '\x01' + pack('>H',objectType) + hash
payload = '\x01' + inventoryHash
headerData = '\xe9\xbe\xb4\xd9' #magic bits, slighly different from Bitcoin's magic bits.
headerData = headerData + 'inv\x00\x00\x00\x00\x00\x00\x00\x00\x00'
headerData = headerData + pack('>L',len(payload))
headerData = headerData + hashlib.sha512(payload).digest()[:4]
broadcastToSendDataQueues((streamNumber, 'send', headerData + payload))
self.emit(SIGNAL("updateStatusBar(PyQt_PyObject)"),'Broacasting the public key request. The recipient''s software must be on. This program will auto-retry if they are offline.')
self.emit(SIGNAL("updateSentItemStatusByHash(PyQt_PyObject,PyQt_PyObject)"),ripe,'Sending public key request. Waiting for reply. Requested at ' + strftime(config.get('bitmessagesettings', 'timeformat'),localtime(int(time.time()))))
broadcastToSendDataQueues((streamNumber, 'send', headerData + payload))
def generateFullAckMessage(self,ackdata,toStreamNumber):
nonce = 0
trialValue = 99999999999999999999
embeddedTime = pack('>I',(int(time.time())))
encodedStreamNumber = encodeVarint(toStreamNumber)
payload = embeddedTime + encodedStreamNumber + ackdata
target = 2**64 / ((len(payload)+payloadLengthExtraBytes+8) * averageProofOfWorkNonceTrialsPerByte)
print '(For ack message) Doing proof of work...'
while trialValue > target:
nonce += 1
trialValue, = unpack('>Q',hashlib.sha512(hashlib.sha512(pack('>Q',nonce) + payload).digest()).digest()[4:12])
print '(For ack message) Found proof of work', trialValue, 'Nonce:', nonce
payload = pack('>Q',nonce) + payload
headerData = '\xe9\xbe\xb4\xd9' #magic bits, slighly different from Bitcoin's magic bits.
headerData = headerData + 'msg\x00\x00\x00\x00\x00\x00\x00\x00\x00'
headerData = headerData + pack('>L',len(payload))
headerData = headerData + hashlib.sha512(payload).digest()[:4]
return headerData + payload
class addressGenerator(QThread):
def __init__(self, parent = None):
QThread.__init__(self, parent)
def setup(self,streamNumber,label):
self.streamNumber = streamNumber
self.label = label
def run(self):
statusbar = 'Generating new ' + str(config.getint('bitmessagesettings', 'bitstrength')) + ' bit RSA key. This takes a minute on average. If you want to generate multiple addresses now, you can; they will queue.'
self.emit(SIGNAL("updateStatusBar(PyQt_PyObject)"),statusbar)
(pubkey, privkey) = rsa.newkeys(config.getint('bitmessagesettings', 'bitstrength'))
print privkey['n']
print privkey['e']
print privkey['d']
print privkey['p']
print privkey['q']
sha = hashlib.new('sha512')
#sha.update(str(pubkey.n)+str(pubkey.e))
sha.update(convertIntToString(pubkey.n)+convertIntToString(pubkey.e))
ripe = hashlib.new('ripemd160')
ripe.update(sha.digest())
address = encodeAddress(1,self.streamNumber,ripe.digest())
self.emit(SIGNAL("updateStatusBar(PyQt_PyObject)"),'Finished generating address. Writing to keys.dat')
config.add_section(address)
config.set(address,'label',self.label)
config.set(address,'enabled','true')
config.set(address,'n',str(privkey['n']))
config.set(address,'e',str(privkey['e']))
config.set(address,'d',str(privkey['d']))
config.set(address,'p',str(privkey['p']))
config.set(address,'q',str(privkey['q']))
with open(appdata + 'keys.dat', 'wb') as configfile:
config.write(configfile)
self.emit(SIGNAL("updateStatusBar(PyQt_PyObject)"),'Done generating address')
self.emit(SIGNAL("writeNewAddressToTable(PyQt_PyObject,PyQt_PyObject,PyQt_PyObject)"),self.label,address,str(self.streamNumber))
'''class streamThread(QThread):
def __init__(self, parent = None):
QThread.__init__(self, parent)
self.mailbox = Queue.Queue()
streamQueues.append(self.mailbox)
self.sendDataQueues = []
def __del__(self):
self.wait()
def broadcastTosendDataQueues(self,data):
for q in self.sendDataQueues:
q.put(data)
def setup(self,streamNumber):
self.streamNumber = streamNumber
def run(self):
#self.listOfSendDataThreads = []
for i in range(1,2):
x = sendDataThread()
#self.listOfSendDataThreads.append(x)
x.setup(i,self.sendDataQueues)
#x.daemon = False
x.start()
#print 'length of listOfSendDataThreads', len(self.listOfSendDataThreads)
while True:
print self.streamNumber
data = self.mailbox.get()
if data == 'shutdown':
self.broadcastTosendDataQueues('shutdown')
print 'Stream thread', self, 'shutting down now'
print 'len of streamQueues', len(streamQueues)
while len(self.sendDataQueues) > 0 :
pass
streamQueues.remove(self.mailbox)
return
print self, 'received a message:', data
returnedthing = str(self)+"dololly"
self.emit(SIGNAL("updatebox(PyQt_PyObject)"),returnedthing)'''
#time.sleep(1)
#while True:
# print 'test'
# time.sleep(1)
class iconGlossaryDialog(QtGui.QDialog):
def __init__(self,parent):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_iconGlossaryDialog()
self.ui.setupUi(self)
self.parent = parent
self.ui.labelPortNumber.setText('You are using TCP port ' + str(config.getint('bitmessagesettings', 'port')) + '. (This can be changed in the settings).')
class helpDialog(QtGui.QDialog):
def __init__(self,parent):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_helpDialog()
self.ui.setupUi(self)
self.parent = parent
self.ui.labelHelpURI.setOpenExternalLinks(True)
class aboutDialog(QtGui.QDialog):
def __init__(self,parent):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_aboutDialog()
self.ui.setupUi(self)
self.parent = parent
self.ui.labelVersion.setText('version ' + softwareVersion)
class settingsDialog(QtGui.QDialog):
def __init__(self,parent):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_settingsDialog()
self.ui.setupUi(self)
self.parent = parent
self.ui.checkBoxStartOnLogon.setChecked(config.getboolean('bitmessagesettings', 'startonlogon'))
self.ui.checkBoxMinimizeToTray.setChecked(config.getboolean('bitmessagesettings', 'minimizetotray'))
self.ui.lineEditTCPPort.setText(str(config.get('bitmessagesettings', 'port')))
self.ui.checkBoxShowTrayNotifications.setChecked(config.getboolean('bitmessagesettings', 'showtraynotifications'))
self.ui.checkBoxStartInTray.setChecked(config.getboolean('bitmessagesettings', 'startintray'))
if 'darwin' in sys.platform:
self.ui.checkBoxStartOnLogon.setDisabled(True)
self.ui.labelSettingsNote.setText('Some options have been disabled because they haven\'t yet been implimented for your operating system.')
elif 'linux' in sys.platform:
pass
class NewSubscriptionDialog(QtGui.QDialog):
def __init__(self,parent):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_NewSubscriptionDialog() #Jonathan changed this line
self.ui.setupUi(self) #Jonathan left this line alone
self.parent = parent
QtCore.QObject.connect(self.ui.lineEditSubscriptionAddress, QtCore.SIGNAL("textChanged(QString)"), self.subscriptionAddressChanged)
def subscriptionAddressChanged(self,QString):
status,a,b,c = decodeAddress(str(QString))
if status == 'missingbm':
self.ui.labelSubscriptionAddressCheck.setText('The address should start with ''BM-''')
elif status == 'checksumfailed':
self.ui.labelSubscriptionAddressCheck.setText('The address is not typed or copied correctly (the checksum failed).')
elif status == 'versiontoohigh':
self.ui.labelSubscriptionAddressCheck.setText('The version number of this address is higher than this software can support. Please upgrade Bitmessage.')
elif status == 'invalidcharacters':
self.ui.labelSubscriptionAddressCheck.setText('The address contains invalid characters.')
elif status == 'success':
self.ui.labelSubscriptionAddressCheck.setText('Address is valid.')
class NewAddressDialog(QtGui.QDialog):
def __init__(self, parent):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_NewAddressDialog() #Jonathan changed this line
self.ui.setupUi(self) #Jonathan left this line alone
self.parent = parent
row = 1
while self.parent.ui.tableWidgetYourIdentities.item(row-1,1):
self.ui.radioButtonExisting.click()
#print self.parent.ui.tableWidgetYourIdentities.item(row-1,1).text()
self.ui.comboBoxExisting.addItem(self.parent.ui.tableWidgetYourIdentities.item(row-1,1).text())
row += 1
class MyForm(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_MainWindow() #Jonathan changed this line
self.ui.setupUi(self) #Jonathan left this line alone
if 'win' in sys.platform:
#Auto-startup for Windows
RUN_PATH = "HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Run"
self.settings = QSettings(RUN_PATH, QSettings.NativeFormat)
self.settings.remove("PyBitmessage") #In case the user moves the program and the registry entry is no longer valid, this will delete the old registry entry.
if config.getboolean('bitmessagesettings', 'startonlogon'):
self.settings.setValue("PyBitmessage",sys.argv[0])
elif 'darwin' in sys.platform:
#startup for mac
pass
elif 'linux' in sys.platform:
#startup for linux
pass
self.trayIcon = QtGui.QSystemTrayIcon(self)
self.trayIcon.setIcon( QtGui.QIcon(':/newPrefix/images/inbox.png') )
traySignal = "activated(QSystemTrayIcon::ActivationReason)"
QtCore.QObject.connect(self.trayIcon, QtCore.SIGNAL(traySignal), self.__icon_activated)
menu = QtGui.QMenu()
self.exitAction = menu.addAction("Exit", self.close)
self.trayIcon.setContextMenu(menu)
#FILE MENU and other buttons
QtCore.QObject.connect(self.ui.actionExit, QtCore.SIGNAL("triggered()"), self.close)
QtCore.QObject.connect(self.ui.actionManageKeys, QtCore.SIGNAL("triggered()"), self.click_actionManageKeys)
QtCore.QObject.connect(self.ui.pushButtonNewAddress, QtCore.SIGNAL("clicked()"), self.click_NewAddressDialog)
QtCore.QObject.connect(self.ui.comboBoxSendFrom, QtCore.SIGNAL("activated(int)"),self.redrawLabelFrom)
QtCore.QObject.connect(self.ui.pushButtonAddAddressBook, QtCore.SIGNAL("clicked()"), self.click_pushButtonAddAddressBook)
QtCore.QObject.connect(self.ui.pushButtonAddSubscription, QtCore.SIGNAL("clicked()"), self.click_pushButtonAddSubscription)
QtCore.QObject.connect(self.ui.pushButtonAddBlacklist, QtCore.SIGNAL("clicked()"), self.click_pushButtonAddBlacklist)
QtCore.QObject.connect(self.ui.pushButtonSend, QtCore.SIGNAL("clicked()"), self.click_pushButtonSend)
QtCore.QObject.connect(self.ui.pushButtonLoadFromAddressBook, QtCore.SIGNAL("clicked()"), self.click_pushButtonLoadFromAddressBook)
QtCore.QObject.connect(self.ui.radioButtonBlacklist, QtCore.SIGNAL("clicked()"), self.click_radioButtonBlacklist)
QtCore.QObject.connect(self.ui.radioButtonWhitelist, QtCore.SIGNAL("clicked()"), self.click_radioButtonWhitelist)
QtCore.QObject.connect(self.ui.pushButtonStatusIcon, QtCore.SIGNAL("clicked()"), self.click_pushButtonStatusIcon)
QtCore.QObject.connect(self.ui.actionSettings, QtCore.SIGNAL("triggered()"), self.click_actionSettings)
QtCore.QObject.connect(self.ui.actionAbout, QtCore.SIGNAL("triggered()"), self.click_actionAbout)
QtCore.QObject.connect(self.ui