2016-01-22 11:17:10 +01:00
doTimingAttackMitigation = False
2013-06-24 23:29:04 +02:00
2016-03-18 16:39:29 +01:00
import base64
2016-10-23 10:12:49 +02:00
import datetime
2015-11-13 12:32:10 +01:00
import errno
2016-02-13 12:54:23 +01:00
import math
2013-06-22 00:29:04 +02:00
import time
import threading
import shared
2013-06-22 01:49:50 +02:00
import hashlib
2015-11-13 12:32:10 +01:00
import os
2017-01-16 19:36:58 +01:00
import Queue
2015-11-13 12:32:10 +01:00
import select
2013-06-22 01:49:50 +02:00
import socket
import random
2015-11-13 12:32:10 +01:00
import ssl
2013-06-22 01:49:50 +02:00
from struct import unpack , pack
import sys
2014-08-27 09:14:32 +02:00
import traceback
2016-03-23 23:26:57 +01:00
from binascii import hexlify
2013-11-20 07:29:37 +01:00
#import string
#from subprocess import call # used when the API must execute an outside program
#from pyelliptic.openssl import OpenSSL
2013-06-22 01:49:50 +02:00
2013-11-20 07:29:37 +01:00
#import highlevelcrypto
2013-06-22 01:49:50 +02:00
from addresses import *
2017-02-22 09:34:54 +01:00
from bmconfigparser import BMConfigParser
2016-02-13 12:54:23 +01:00
from class_objectHashHolder import objectHashHolder
2014-05-02 16:46:36 +02:00
from helper_generic import addDataPadding , isHostInPrivateIPRange
2015-03-09 07:35:32 +01:00
from helper_sql import sqlQuery
2017-02-08 13:41:56 +01:00
import knownnodes
2013-09-04 04:45:45 +02:00
from debug import logger
2017-01-11 17:00:00 +01:00
import paths
2017-01-11 14:27:19 +01:00
import protocol
2017-01-19 19:48:12 +01:00
from inventory import Inventory , PendingDownload , PendingUpload
2017-02-08 13:41:56 +01:00
import queues
2017-01-11 17:00:00 +01:00
import state
2017-01-14 23:21:00 +01:00
import throttle
2016-10-23 10:12:49 +02:00
import tr
2017-01-11 14:27:19 +01:00
from version import softwareVersion
2013-06-22 00:29:04 +02:00
# This thread is created either by the synSenderThread(for outgoing
2013-11-20 07:29:37 +01:00
# connections) or the singleListenerThread(for incoming connections).
2013-06-22 00:29:04 +02:00
class receiveDataThread ( threading . Thread ) :
def __init__ ( self ) :
2015-11-18 16:22:17 +01:00
threading . Thread . __init__ ( self , name = " receiveData " )
2013-06-22 00:29:04 +02:00
self . data = ' '
self . verackSent = False
self . verackReceived = False
def setup (
self ,
sock ,
HOST ,
port ,
streamNumber ,
2013-12-30 04:36:23 +01:00
someObjectsOfWhichThisRemoteNodeIsAlreadyAware ,
selfInitiatedConnections ,
2016-02-18 00:53:13 +01:00
sendDataThreadQueue ,
objectHashHolderInstance ) :
2013-12-30 04:36:23 +01:00
2013-06-22 00:29:04 +02:00
self . sock = sock
2017-01-12 06:58:35 +01:00
self . peer = state . Peer ( HOST , port )
2016-01-26 11:54:11 +01:00
self . name = " receiveData- " + self . peer . host . replace ( " : " , " . " ) # ":" log parser field separator
2017-02-06 17:47:05 +01:00
self . streamNumber = state . streamsInWhichIAmParticipating
self . remoteStreams = [ ]
2013-06-22 01:49:50 +02:00
self . selfInitiatedConnections = selfInitiatedConnections
2013-12-30 04:36:23 +01:00
self . sendDataThreadQueue = sendDataThreadQueue # used to send commands and data to the sendDataThread
2017-01-11 14:27:19 +01:00
self . hostIdent = self . peer . port if " .onion " in BMConfigParser ( ) . get ( ' bitmessagesettings ' , ' onionhostname ' ) and protocol . checkSocksIP ( self . peer . host ) else self . peer . host
2013-06-22 00:29:04 +02:00
shared . connectedHostsList [
2016-11-16 19:36:50 +01:00
self . hostIdent ] = 0 # The very fact that this receiveData thread exists shows that we are connected to the remote host. Let's add it to this list so that an outgoingSynSender thread doesn't try to connect to it.
2013-06-22 00:29:04 +02:00
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.
2015-11-13 12:32:10 +01:00
self . services = 0
2017-02-06 17:47:05 +01:00
if streamNumber == - 1 : # This was an incoming connection. Send out a version message if we accept the other node's version message.
2013-06-22 00:29:04 +02:00
self . initiatedConnection = False
else :
self . initiatedConnection = True
2013-06-22 01:49:50 +02:00
self . selfInitiatedConnections [ streamNumber ] [ self ] = 0
2013-06-24 21:51:01 +02:00
self . someObjectsOfWhichThisRemoteNodeIsAlreadyAware = someObjectsOfWhichThisRemoteNodeIsAlreadyAware
2016-02-18 00:53:13 +01:00
self . objectHashHolderInstance = objectHashHolderInstance
2016-02-13 12:54:23 +01:00
self . startTime = time . time ( )
2013-06-22 00:29:04 +02:00
def run ( self ) :
2015-11-18 16:22:17 +01:00
logger . debug ( ' receiveDataThread starting. ID ' + str ( id ( self ) ) + ' . The size of the shared.connectedHostsList is now ' + str ( len ( shared . connectedHostsList ) ) )
2013-06-29 19:29:35 +02:00
2017-02-02 15:52:32 +01:00
while state . shutdown == 0 :
2013-07-02 17:43:54 +02:00
dataLen = len ( self . data )
2013-06-22 00:29:04 +02:00
try :
2017-02-07 13:00:24 +01:00
isSSL = False
2017-01-11 14:27:19 +01:00
if ( ( self . services & protocol . NODE_SSL == protocol . NODE_SSL ) and
2015-11-22 16:43:53 +01:00
self . connectionIsOrWasFullyEstablished and
2017-01-11 14:27:19 +01:00
protocol . haveSSL ( not self . initiatedConnection ) ) :
2017-02-07 13:00:24 +01:00
isSSL = True
2017-01-15 15:08:03 +01:00
dataRecv = self . sslSock . recv ( throttle . ReceiveThrottle ( ) . chunkSize )
2015-11-13 12:32:10 +01:00
else :
2017-01-15 15:08:03 +01:00
dataRecv = self . sock . recv ( throttle . ReceiveThrottle ( ) . chunkSize )
2014-07-07 22:30:23 +02:00
self . data + = dataRecv
2017-01-14 23:21:00 +01:00
throttle . ReceiveThrottle ( ) . wait ( len ( dataRecv ) )
2013-06-22 00:29:04 +02:00
except socket . timeout :
2017-02-07 16:06:24 +01:00
if self . connectionIsOrWasFullyEstablished :
self . sendping ( " Still around! " )
continue
logger . error ( " Timeout during protocol initialisation " )
2013-06-22 00:29:04 +02:00
break
2017-02-07 13:00:24 +01:00
except ssl . SSLError as err :
if err . errno == ssl . SSL_ERROR_WANT_READ :
select . select ( [ self . sslSock ] , [ ] , [ ] , 10 )
logger . debug ( ' sock.recv retriable SSL error ' )
continue
2017-02-07 16:06:24 +01:00
if err . errno is None and ' timed out ' in str ( err ) :
if self . connectionIsOrWasFullyEstablished :
self . sendping ( " Still around! " )
continue
logger . error ( ' SSL error: %i / %s ' , err . errno if err . errno else 0 , str ( err ) )
2017-02-07 13:00:24 +01:00
break
2017-01-11 14:27:19 +01:00
except socket . error as err :
2017-02-14 01:38:58 +01:00
if err . errno in ( errno . EAGAIN , errno . EWOULDBLOCK ) or \
( sys . platform . startswith ( ' win ' ) and \
2017-02-17 21:14:39 +01:00
err . errno == errno . WSAEWOULDBLOCK ) :
2017-02-07 13:00:24 +01:00
select . select ( [ self . sslSock if isSSL else self . sock ] , [ ] , [ ] , 10 )
logger . debug ( ' sock.recv retriable error ' )
2017-02-06 19:41:25 +01:00
continue
2015-11-18 16:22:17 +01:00
logger . error ( ' sock.recv error. Closing receiveData thread ( ' + str ( self . peer ) + ' , Thread ID: ' + str ( id ( self ) ) + ' ). ' + str ( err . errno ) + " / " + str ( err ) )
2013-06-22 00:29:04 +02:00
break
# print 'Received', repr(self.data)
2013-07-05 22:56:49 +02:00
if len ( self . data ) == dataLen : # If self.sock.recv returned no data:
2015-11-18 16:22:17 +01:00
logger . debug ( ' Connection to ' + str ( self . peer ) + ' closed. Closing receiveData thread. (ID: ' + str ( id ( self ) ) + ' ) ' )
2013-06-22 00:29:04 +02:00
break
else :
self . processData ( )
try :
2017-02-06 17:47:05 +01:00
for stream in self . remoteStreams :
try :
del self . selfInitiatedConnections [ stream ] [ self ]
except KeyError :
pass
2015-11-18 16:22:17 +01:00
logger . debug ( ' removed self (a receiveDataThread) from selfInitiatedConnections ' )
2013-06-22 00:29:04 +02:00
except :
pass
2014-08-06 21:54:59 +02:00
self . sendDataThreadQueue . put ( ( 0 , ' shutdown ' , ' no data ' ) ) # commands the corresponding sendDataThread to shut itself down.
2013-06-22 00:29:04 +02:00
try :
2016-11-16 19:36:50 +01:00
del shared . connectedHostsList [ self . hostIdent ]
2013-06-22 00:29:04 +02:00
except Exception as err :
2016-11-16 19:36:50 +01:00
logger . error ( ' Could not delete ' + str ( self . hostIdent ) + ' from shared.connectedHostsList. ' + str ( err ) )
2013-06-29 19:29:35 +02:00
2017-01-19 19:48:12 +01:00
PendingDownload ( ) . threadEnd ( )
2017-02-08 13:41:56 +01:00
queues . UISignalQueue . put ( ( ' updateNetworkStatusTab ' , ' no data ' ) )
2016-10-23 10:12:49 +02:00
self . checkTimeOffsetNotification ( )
2015-11-18 16:22:17 +01:00
logger . debug ( ' receiveDataThread ending. ID ' + str ( id ( self ) ) + ' . The size of the shared.connectedHostsList is now ' + str ( len ( shared . connectedHostsList ) ) )
2013-06-29 19:29:35 +02:00
2016-02-13 12:54:23 +01:00
def antiIntersectionDelay ( self , initial = False ) :
# estimated time for a small object to propagate across the whole network
2017-02-08 13:41:56 +01:00
delay = math . ceil ( math . log ( max ( len ( knownnodes . knownNodes [ x ] ) for x in knownnodes . knownNodes ) + 2 , 20 ) ) * ( 0.2 + objectHashHolder . size / 2 )
2017-02-06 17:47:05 +01:00
# take the stream with maximum amount of nodes
2016-02-13 12:54:23 +01:00
# +2 is to avoid problems with log(0) and log(1)
# 20 is avg connected nodes count
# 0.2 is avg message transmission time
now = time . time ( )
if initial and now - delay < self . startTime :
2016-02-18 00:53:13 +01:00
logger . debug ( " Initial sleeping for %.2f s " , delay - ( now - self . startTime ) )
2016-02-13 12:54:23 +01:00
time . sleep ( delay - ( now - self . startTime ) )
elif not initial :
2016-02-18 00:53:13 +01:00
logger . debug ( " Sleeping due to missing object for %.2f s " , delay )
2016-02-13 12:54:23 +01:00
time . sleep ( delay )
2013-06-22 00:29:04 +02:00
2016-10-23 10:12:49 +02:00
def checkTimeOffsetNotification ( self ) :
if shared . timeOffsetWrongCount > = 4 and not self . connectionIsOrWasFullyEstablished :
2017-02-08 13:41:56 +01:00
queues . UISignalQueue . put ( ( ' updateStatusBar ' , tr . _translate ( " MainWindow " , " The time on your computer, % 1, may be wrong. Please verify your settings. " ) . arg ( datetime . datetime . now ( ) . strftime ( " % H: % M: % S " ) ) ) )
2016-10-23 10:12:49 +02:00
2013-06-22 00:29:04 +02:00
def processData ( self ) :
2017-01-11 14:27:19 +01:00
if len ( self . data ) < protocol . Header . size : # if so little of the data has arrived that we can't even read the checksum then wait for more data.
2013-06-22 00:29:04 +02:00
return
2014-08-06 21:54:59 +02:00
2017-01-11 14:27:19 +01:00
magic , command , payloadLength , checksum = protocol . Header . unpack ( self . data [ : protocol . Header . size ] )
2014-05-22 17:57:48 +02:00
if magic != 0xE9BEB4D9 :
2013-06-22 00:29:04 +02:00
self . data = " "
return
2014-09-10 22:47:51 +02:00
if payloadLength > 1600100 : # ~1.6 MB which is the maximum possible size of an inv message.
2014-05-22 17:57:48 +02:00
logger . info ( ' The incoming message, which we have not yet download, is too large. Ignoring it. (unfortunately there is no way to tell the other node to stop sending it except to disconnect.) Message size: %s ' % payloadLength )
2017-01-11 14:27:19 +01:00
self . data = self . data [ payloadLength + protocol . Header . size : ]
2014-08-06 21:54:59 +02:00
del magic , command , payloadLength , checksum # we don't need these anymore and better to clean them now before the recursive call rather than after
2014-02-05 08:45:10 +01:00
self . processData ( )
return
2017-01-11 14:27:19 +01:00
if len ( self . data ) < payloadLength + protocol . Header . size : # check if the whole message has arrived yet.
2013-06-22 00:29:04 +02:00
return
2017-01-11 14:27:19 +01:00
payload = self . data [ protocol . Header . size : payloadLength + protocol . Header . size ]
2014-07-15 00:01:56 +02:00
if checksum != hashlib . sha512 ( payload ) . digest ( ) [ 0 : 4 ] : # test the checksum in the message.
2015-11-18 16:22:17 +01:00
logger . error ( ' Checksum incorrect. Clearing this message. ' )
2017-01-11 14:27:19 +01:00
self . data = self . data [ payloadLength + protocol . Header . size : ]
2014-08-06 21:54:59 +02:00
del magic , command , payloadLength , checksum , payload # better to clean up before the recursive call
2013-06-22 00:29:04 +02:00
self . processData ( )
return
2014-08-06 21:54:59 +02:00
2013-06-22 00:29:04 +02:00
# 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 and self . connectionIsOrWasFullyEstablished : # 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).
2017-02-08 13:41:56 +01:00
with knownnodes . knownNodesLock :
2017-02-06 17:47:05 +01:00
for stream in self . streamNumber :
2017-02-08 13:41:56 +01:00
knownnodes . knownNodes [ stream ] [ self . peer ] = int ( time . time ( ) )
2014-02-05 08:45:10 +01:00
2014-05-22 17:57:48 +02:00
#Strip the nulls
command = command . rstrip ( ' \x00 ' )
2015-11-18 16:22:17 +01:00
logger . debug ( ' remoteCommand ' + repr ( command ) + ' from ' + str ( self . peer ) )
2014-05-22 17:57:48 +02:00
2014-08-27 09:14:32 +02:00
try :
#TODO: Use a dispatcher here
2014-09-10 22:47:51 +02:00
if command == ' error ' :
self . recerror ( payload )
elif not self . connectionIsOrWasFullyEstablished :
2014-08-27 09:14:32 +02:00
if command == ' version ' :
self . recversion ( payload )
elif command == ' verack ' :
self . recverack ( )
else :
if command == ' addr ' :
self . recaddr ( payload )
elif command == ' inv ' :
self . recinv ( payload )
elif command == ' getdata ' :
self . recgetdata ( payload )
elif command == ' object ' :
self . recobject ( payload )
elif command == ' ping ' :
self . sendpong ( payload )
2017-02-07 16:06:24 +01:00
elif command == ' pong ' :
pass
else :
logger . info ( " Unknown command %s , ignoring " , command )
2014-08-27 09:14:32 +02:00
except varintDecodeError as e :
logger . debug ( " There was a problem with a varint while processing a message from the wire. Some details: %s " % e )
except Exception as e :
logger . critical ( " Critical error in a receiveDataThread: \n %s " % traceback . format_exc ( ) )
2014-08-06 21:54:59 +02:00
del payload
2017-01-11 14:27:19 +01:00
self . data = self . data [ payloadLength + protocol . Header . size : ] # take this message out and then process the next message
2013-06-22 00:29:04 +02:00
2014-08-06 21:54:59 +02:00
if self . data == ' ' : # if there are no more messages
2017-01-16 19:36:58 +01:00
try :
2017-01-19 19:48:12 +01:00
self . sendgetdata ( PendingDownload ( ) . pull ( 100 ) )
2017-01-16 23:37:03 +01:00
except Queue . Full :
2017-01-16 19:36:58 +01:00
pass
2013-06-22 00:29:04 +02:00
self . processData ( )
2017-01-11 18:13:00 +01:00
def sendpong ( self , payload ) :
2015-11-18 16:22:17 +01:00
logger . debug ( ' Sending pong ' )
2017-01-11 18:13:00 +01:00
self . sendDataThreadQueue . put ( ( 0 , ' sendRawData ' , protocol . CreatePacket ( ' pong ' , payload ) ) )
2013-06-29 19:29:35 +02:00
2017-02-07 16:06:24 +01:00
def sendping ( self , payload ) :
logger . debug ( ' Sending ping ' )
self . sendDataThreadQueue . put ( ( 0 , ' sendRawData ' , protocol . CreatePacket ( ' ping ' , payload ) ) )
2013-06-22 00:29:04 +02:00
def recverack ( self ) :
2015-11-18 16:22:17 +01:00
logger . debug ( ' verack received ' )
2013-06-22 00:29:04 +02:00
self . verackReceived = True
if self . verackSent :
# We have thus both sent and received a verack.
self . connectionFullyEstablished ( )
2017-02-07 19:38:52 +01:00
def sslHandshake ( self ) :
2015-11-13 12:32:10 +01:00
self . sslSock = self . sock
2017-01-11 14:27:19 +01:00
if ( ( self . services & protocol . NODE_SSL == protocol . NODE_SSL ) and
protocol . haveSSL ( not self . initiatedConnection ) ) :
2015-11-22 22:44:58 +01:00
logger . debug ( " Initialising TLS " )
2017-01-11 20:47:27 +01:00
if sys . version_info > = ( 2 , 7 , 9 ) :
2017-01-14 13:22:46 +01:00
context = ssl . SSLContext ( protocol . sslProtocolVersion )
2017-01-14 17:50:49 +01:00
context . set_ciphers ( protocol . sslProtocolCiphers )
2017-01-11 20:47:27 +01:00
context . set_ecdh_curve ( " secp256k1 " )
context . check_hostname = False
context . verify_mode = ssl . CERT_NONE
# also exclude TLSv1 and TLSv1.1 in the future
2017-01-14 13:22:46 +01:00
context . options = ssl . OP_ALL | ssl . OP_NO_SSLv2 | ssl . OP_NO_SSLv3 | ssl . OP_SINGLE_ECDH_USE | ssl . OP_CIPHER_SERVER_PREFERENCE
2017-01-11 20:47:27 +01:00
self . sslSock = context . wrap_socket ( self . sock , server_side = not self . initiatedConnection , do_handshake_on_connect = False )
else :
2017-01-14 17:50:49 +01:00
self . sslSock = ssl . wrap_socket ( self . sock , keyfile = os . path . join ( paths . codePath ( ) , ' sslkeys ' , ' key.pem ' ) , certfile = os . path . join ( paths . codePath ( ) , ' sslkeys ' , ' cert.pem ' ) , server_side = not self . initiatedConnection , ssl_version = protocol . sslProtocolVersion , do_handshake_on_connect = False , ciphers = protocol . sslProtocolCiphers )
2017-01-14 13:22:46 +01:00
self . sendDataThreadQueue . join ( )
2015-11-13 12:32:10 +01:00
while True :
try :
self . sslSock . do_handshake ( )
2017-01-07 23:42:07 +01:00
logger . debug ( " TLS handshake success " )
2015-11-13 12:32:10 +01:00
break
2017-01-19 19:52:54 +01:00
except ssl . SSLError as e :
2017-02-03 10:05:35 +01:00
if sys . hexversion > = 0x02070900 :
if isinstance ( e , ssl . SSLWantReadError ) :
logger . debug ( " Waiting for SSL socket handhake read " )
select . select ( [ self . sslSock ] , [ ] , [ ] , 10 )
continue
elif isinstance ( e , ssl . SSLWantWriteError ) :
logger . debug ( " Waiting for SSL socket handhake write " )
select . select ( [ ] , [ self . sslSock ] , [ ] , 10 )
continue
else :
if e . args [ 0 ] == ssl . SSL_ERROR_WANT_READ :
logger . debug ( " Waiting for SSL socket handhake read " )
select . select ( [ self . sslSock ] , [ ] , [ ] , 10 )
continue
elif e . args [ 0 ] == ssl . SSL_ERROR_WANT_WRITE :
logger . debug ( " Waiting for SSL socket handhake write " )
select . select ( [ ] , [ self . sslSock ] , [ ] , 10 )
continue
2017-01-19 19:52:54 +01:00
logger . error ( " SSL socket handhake failed: %s , shutting down connection " , str ( e ) )
self . sendDataThreadQueue . put ( ( 0 , ' shutdown ' , ' tls handshake fail %s ' % ( str ( e ) ) ) )
2017-02-08 20:49:14 +01:00
return False
2017-02-03 10:05:35 +01:00
except Exception :
2017-01-14 13:22:46 +01:00
logger . error ( " SSL socket handhake failed, shutting down connection " , exc_info = True )
2017-01-07 23:42:07 +01:00
self . sendDataThreadQueue . put ( ( 0 , ' shutdown ' , ' tls handshake fail ' ) )
2017-02-08 20:49:14 +01:00
return False
2017-02-07 16:42:02 +01:00
# SSL in the background should be blocking, otherwise the error handling is difficult
self . sslSock . settimeout ( None )
2017-02-08 20:49:14 +01:00
return True
# no SSL
return True
2017-02-07 19:38:52 +01:00
def peerValidityChecks ( self ) :
if self . remoteProtocolVersion < 3 :
2017-02-07 20:09:11 +01:00
self . sendDataThreadQueue . put ( ( 0 , ' sendRawData ' , protocol . assembleErrorMessage (
fatal = 2 , errorText = " Your is using an old protocol. Closing connection. " ) ) )
2017-02-07 19:38:52 +01:00
logger . debug ( ' Closing connection to old protocol version ' + str ( self . remoteProtocolVersion ) + ' node: ' + str ( self . peer ) )
return False
if self . timeOffset > 3600 :
2017-02-07 20:09:11 +01:00
self . sendDataThreadQueue . put ( ( 0 , ' sendRawData ' , protocol . assembleErrorMessage (
fatal = 2 , errorText = " Your time is too far in the future compared to mine. Closing connection. " ) ) )
logger . info ( " %s ' s time is too far in the future ( %s seconds). Closing connection to it. " , self . peer , self . timeOffset )
2017-02-07 19:38:52 +01:00
shared . timeOffsetWrongCount + = 1
time . sleep ( 2 )
return False
elif self . timeOffset < - 3600 :
2017-02-07 20:09:11 +01:00
self . sendDataThreadQueue . put ( ( 0 , ' sendRawData ' , protocol . assembleErrorMessage (
fatal = 2 , errorText = " Your time is too far in the past compared to mine. Closing connection. " ) ) )
logger . info ( " %s ' s time is too far in the past (timeOffset %s seconds). Closing connection to it. " , self . peer , self . timeOffset )
2017-02-07 19:38:52 +01:00
shared . timeOffsetWrongCount + = 1
return False
else :
shared . timeOffsetWrongCount = 0
if len ( self . streamNumber ) == 0 :
2017-02-07 20:09:11 +01:00
self . sendDataThreadQueue . put ( ( 0 , ' sendRawData ' , protocol . assembleErrorMessage (
fatal = 2 , errorText = " We don ' t have shared stream interests. Closing connection. " ) ) )
2017-02-07 19:38:52 +01:00
logger . debug ( ' Closed connection to ' + str ( self . peer ) + ' because there is no overlapping interest in streams. ' )
return False
return True
def connectionFullyEstablished ( self ) :
if self . connectionIsOrWasFullyEstablished :
# there is no reason to run this function a second time
return
2017-02-08 20:49:14 +01:00
if not self . sslHandshake ( ) :
return
2017-02-07 19:38:52 +01:00
if self . peerValidityChecks ( ) == False :
time . sleep ( 2 )
self . sendDataThreadQueue . put ( ( 0 , ' shutdown ' , ' no data ' ) )
self . checkTimeOffsetNotification ( )
return
self . connectionIsOrWasFullyEstablished = True
shared . timeOffsetWrongCount = 0
2014-04-30 21:39:25 +02:00
# Command the corresponding sendDataThread to set its own connectionIsOrWasFullyEstablished variable to True also
2015-11-22 22:44:58 +01:00
self . sendDataThreadQueue . put ( ( 0 , ' connectionIsOrWasFullyEstablished ' , ( self . services , self . sslSock ) ) )
2015-11-13 12:32:10 +01:00
2013-06-22 00:29:04 +02:00
if not self . initiatedConnection :
2013-08-25 01:40:48 +02:00
shared . clientHasReceivedIncomingConnections = True
2017-02-08 13:41:56 +01:00
queues . UISignalQueue . put ( ( ' setStatusIcon ' , ' green ' ) )
2013-06-22 00:29:04 +02:00
self . sock . settimeout (
2017-02-07 16:06:24 +01:00
600 ) # We'll send out a ping every 5 minutes to make sure the connection stays alive if there has been no other traffic to send lately.
2017-02-08 13:41:56 +01:00
queues . UISignalQueue . put ( ( ' updateNetworkStatusTab ' , ' no data ' ) )
2015-11-18 16:22:17 +01:00
logger . debug ( ' Connection fully established with ' + str ( self . peer ) + " \n " + \
' The size of the connectedHostsList is now ' + str ( len ( shared . connectedHostsList ) ) + " \n " + \
2017-01-11 17:00:00 +01:00
' The length of sendDataQueues is now: ' + str ( len ( state . sendDataQueues ) ) + " \n " + \
2015-11-18 16:22:17 +01:00
' broadcasting addr from within connectionFullyEstablished function. ' )
2013-06-29 19:29:35 +02:00
2017-01-12 19:18:56 +01:00
if self . initiatedConnection :
state . networkProtocolAvailability [ protocol . networkType ( self . peer . host ) ] = True
2017-01-19 19:48:12 +01:00
# we need to send our own objects to this node
PendingUpload ( ) . add ( )
2013-11-20 07:29:37 +01:00
# Let all of our peers know about this new node.
2017-02-06 17:47:05 +01:00
for stream in self . remoteStreams :
dataToSend = ( int ( time . time ( ) ) , stream , self . services , self . peer . host , self . remoteNodeIncomingPort )
protocol . broadcastToSendDataQueues ( (
stream , ' advertisepeer ' , dataToSend ) )
2013-09-10 01:26:32 +02:00
2013-06-22 00:29:04 +02:00
self . sendaddr ( ) # This is one large addr message to this one peer.
if not self . initiatedConnection and len ( shared . connectedHostsList ) > 200 :
2015-11-18 16:22:17 +01:00
logger . info ( ' We are connected to too many people. Closing connection. ' )
2017-02-07 19:38:52 +01:00
self . sendDataThreadQueue . put ( ( 0 , ' sendRawData ' , protocol . assembleErrorMessage ( fatal = 2 , errorText = " Server full, please try again later. " ) ) )
2014-08-06 21:54:59 +02:00
self . sendDataThreadQueue . put ( ( 0 , ' shutdown ' , ' no data ' ) )
2013-06-22 00:29:04 +02:00
return
self . sendBigInv ( )
def sendBigInv ( self ) :
2014-08-27 09:14:32 +02:00
# Select all hashes for objects in this stream.
2013-06-22 00:29:04 +02:00
bigInvList = { }
2017-02-06 17:47:05 +01:00
for stream in self . streamNumber :
for hash in Inventory ( ) . unexpired_hashes_by_stream ( stream ) :
if hash not in self . someObjectsOfWhichThisRemoteNodeIsAlreadyAware and not self . objectHashHolderInstance . hasHash ( hash ) :
bigInvList [ hash ] = 0
2013-06-22 00:29:04 +02:00
numberOfObjectsInInvMessage = 0
payload = ' '
# Now let us start appending all of these hashes together. They will be
# sent out in a big inv message to our new peer.
for hash , storedValue in bigInvList . items ( ) :
payload + = hash
numberOfObjectsInInvMessage + = 1
2014-05-22 17:57:48 +02:00
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.
2013-06-22 00:29:04 +02:00
self . sendinvMessageToJustThisOnePeer (
numberOfObjectsInInvMessage , payload )
payload = ' '
numberOfObjectsInInvMessage = 0
if numberOfObjectsInInvMessage > 0 :
self . sendinvMessageToJustThisOnePeer (
numberOfObjectsInInvMessage , payload )
2014-04-30 21:39:25 +02:00
# Used to send a big inv message when the connection with a node is
# first fully established. Notice that there is also a broadcastinv
# function for broadcasting invs to everyone in our stream.
2013-06-22 00:29:04 +02:00
def sendinvMessageToJustThisOnePeer ( self , numberOfObjects , payload ) :
payload = encodeVarint ( numberOfObjects ) + payload
2015-11-18 16:22:17 +01:00
logger . debug ( ' Sending huge inv message with ' + str ( numberOfObjects ) + ' objects to just this one peer ' )
2017-01-11 14:27:19 +01:00
self . sendDataThreadQueue . put ( ( 0 , ' sendRawData ' , protocol . CreatePacket ( ' inv ' , payload ) ) )
2013-06-29 19:29:35 +02:00
2014-02-06 14:16:07 +01:00
def _sleepForTimingAttackMitigation ( self , sleepTime ) :
# We don't need to do the timing attack mitigation if we are
# only connected to the trusted peer because we can trust the
# peer not to attack
2017-01-12 06:58:35 +01:00
if sleepTime > 0 and doTimingAttackMitigation and state . trustedPeer == None :
2015-11-18 16:22:17 +01:00
logger . debug ( ' Timing attack mitigation: Sleeping for ' + str ( sleepTime ) + ' seconds. ' )
2014-02-06 14:16:07 +01:00
time . sleep ( sleepTime )
2014-09-10 22:47:51 +02:00
def recerror ( self , data ) :
"""
The remote node has been polite enough to send you an error message .
"""
fatalStatus , readPosition = decodeVarint ( data [ : 10 ] )
banTime , banTimeLength = decodeVarint ( data [ readPosition : readPosition + 10 ] )
readPosition + = banTimeLength
inventoryVectorLength , inventoryVectorLengthLength = decodeVarint ( data [ readPosition : readPosition + 10 ] )
if inventoryVectorLength > 100 :
return
readPosition + = inventoryVectorLengthLength
inventoryVector = data [ readPosition : readPosition + inventoryVectorLength ]
readPosition + = inventoryVectorLength
errorTextLength , errorTextLengthLength = decodeVarint ( data [ readPosition : readPosition + 10 ] )
if errorTextLength > 1000 :
return
readPosition + = errorTextLengthLength
errorText = data [ readPosition : readPosition + errorTextLength ]
if fatalStatus == 0 :
fatalHumanFriendly = ' Warning '
elif fatalStatus == 1 :
fatalHumanFriendly = ' Error '
elif fatalStatus == 2 :
fatalHumanFriendly = ' Fatal '
message = ' %s message received from %s : %s . ' % ( fatalHumanFriendly , self . peer , errorText )
if inventoryVector :
2016-03-23 23:26:57 +01:00
message + = " This concerns object %s " % hexlify ( inventoryVector )
2014-09-10 22:47:51 +02:00
if banTime > 0 :
message + = " Remote node says that the ban time is %s " % banTime
logger . error ( message )
2013-06-22 00:29:04 +02:00
2013-11-20 07:29:37 +01:00
2014-08-27 09:14:32 +02:00
def recobject ( self , data ) :
2013-06-22 00:29:04 +02:00
self . messageProcessingStartTime = time . time ( )
2014-08-27 09:14:32 +02:00
lengthOfTimeWeShouldUseToProcessThisMessage = shared . checkAndShareObjectWithPeers ( data )
2013-11-20 07:29:37 +01:00
"""
2014-08-27 09:14:32 +02:00
Sleeping will help guarantee that we can process messages faster than a
remote node can send them . If we fall behind , the attacker could observe
that we are are slowing down the rate at which we request objects from the
2013-11-20 07:29:37 +01:00
network which would indicate that we own a particular address ( whichever
one to which they are sending all of their attack messages ) . Note
that if an attacker connects to a target with many connections , this
mitigation mechanism might not be sufficient .
"""
2014-08-27 09:14:32 +02:00
sleepTime = lengthOfTimeWeShouldUseToProcessThisMessage - ( time . time ( ) - self . messageProcessingStartTime )
2014-02-06 14:16:07 +01:00
self . _sleepForTimingAttackMitigation ( sleepTime )
2014-08-27 09:14:32 +02:00
2013-06-22 00:29:04 +02:00
# We have received an inv message
def recinv ( self , data ) :
numberOfItemsInInv , lengthOfVarint = decodeVarint ( data [ : 10 ] )
if numberOfItemsInInv > 50000 :
sys . stderr . write ( ' Too many items in inv message! ' )
return
if len ( data ) < lengthOfVarint + ( numberOfItemsInInv * 32 ) :
2015-11-18 16:22:17 +01:00
logger . info ( ' inv message doesn \' t contain enough data. Ignoring. ' )
2013-06-22 00:29:04 +02:00
return
2017-01-15 19:21:24 +01:00
startTime = time . time ( )
advertisedSet = set ( )
for i in range ( numberOfItemsInInv ) :
advertisedSet . add ( data [ lengthOfVarint + ( 32 * i ) : 32 + lengthOfVarint + ( 32 * i ) ] )
2017-02-06 17:47:05 +01:00
objectsNewToMe = advertisedSet
for stream in self . streamNumber :
objectsNewToMe - = Inventory ( ) . hashes_by_stream ( stream )
2017-01-15 19:21:24 +01:00
logger . info ( ' inv message lists %s objects. Of those %s are new to me. It took %s seconds to figure that out. ' , numberOfItemsInInv , len ( objectsNewToMe ) , time . time ( ) - startTime )
for item in objectsNewToMe :
2017-01-19 19:48:12 +01:00
PendingDownload ( ) . add ( item )
2017-01-15 19:21:24 +01:00
self . someObjectsOfWhichThisRemoteNodeIsAlreadyAware [ item ] = 0 # helps us keep from sending inv messages to peers that already know about the objects listed therein
2013-06-22 00:29:04 +02:00
# Send a getdata message to our peer to request the object with the given
# hash
2017-01-16 19:36:58 +01:00
def sendgetdata ( self , hashes ) :
2017-01-16 23:37:25 +01:00
if len ( hashes ) == 0 :
return
2017-01-16 19:36:58 +01:00
logger . debug ( ' sending getdata to retrieve %i objects ' , len ( hashes ) )
payload = encodeVarint ( len ( hashes ) ) + ' ' . join ( hashes )
self . sendDataThreadQueue . put ( ( 0 , ' sendRawData ' , protocol . CreatePacket ( ' getdata ' , payload ) ) , False )
2013-06-29 19:29:35 +02:00
2013-06-22 00:29:04 +02:00
# We have received a getdata request from our peer
def recgetdata ( self , data ) :
numberOfRequestedInventoryItems , lengthOfVarint = decodeVarint (
data [ : 10 ] )
if len ( data ) < lengthOfVarint + ( 32 * numberOfRequestedInventoryItems ) :
2015-11-18 16:22:17 +01:00
logger . debug ( ' getdata message does not contain enough data. Ignoring. ' )
2013-06-22 00:29:04 +02:00
return
2016-02-18 00:53:13 +01:00
self . antiIntersectionDelay ( True ) # only handle getdata requests if we have been connected long enough
2013-06-22 00:29:04 +02:00
for i in xrange ( numberOfRequestedInventoryItems ) :
hash = data [ lengthOfVarint + (
i * 32 ) : 32 + lengthOfVarint + ( i * 32 ) ]
2016-03-23 23:26:57 +01:00
logger . debug ( ' received getdata request for item: ' + hexlify ( hash ) )
2013-06-29 19:29:35 +02:00
2016-02-18 00:53:13 +01:00
if self . objectHashHolderInstance . hasHash ( hash ) :
self . antiIntersectionDelay ( )
2013-06-22 00:29:04 +02:00
else :
2017-01-10 21:15:35 +01:00
if hash in Inventory ( ) :
2017-01-19 19:48:12 +01:00
self . sendObject ( hash , Inventory ( ) [ hash ] . payload )
2013-06-22 00:29:04 +02:00
else :
2016-02-13 12:54:23 +01:00
self . antiIntersectionDelay ( )
2015-01-28 20:45:29 +01:00
logger . warning ( ' %s asked for an object with a getdata which is not in either our memory inventory or our SQL inventory. We probably cleaned it out after advertising it but before they got around to asking for it. ' % ( self . peer , ) )
2013-06-22 00:29:04 +02:00
# Our peer has requested (in a getdata message) that we send an object.
2017-01-19 19:48:12 +01:00
def sendObject ( self , hash , payload ) :
2015-11-18 16:22:17 +01:00
logger . debug ( ' sending an object. ' )
2017-01-19 19:48:12 +01:00
self . sendDataThreadQueue . put ( ( 0 , ' sendRawData ' , ( hash , protocol . CreatePacket ( ' object ' , payload ) ) ) )
2013-06-29 19:29:35 +02:00
2016-01-26 12:04:06 +01:00
def _checkIPAddress ( self , host ) :
if host [ 0 : 12 ] == ' \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \xFF \xFF ' :
hostStandardFormat = socket . inet_ntop ( socket . AF_INET , host [ 12 : ] )
return self . _checkIPv4Address ( host [ 12 : ] , hostStandardFormat )
2016-03-18 16:39:29 +01:00
elif host [ 0 : 6 ] == ' \xfd \x87 \xd8 \x7e \xeb \x43 ' :
# Onion, based on BMD/bitcoind
hostStandardFormat = base64 . b32encode ( host [ 6 : ] ) . lower ( ) + " .onion "
return hostStandardFormat
2016-01-26 12:04:06 +01:00
else :
hostStandardFormat = socket . inet_ntop ( socket . AF_INET6 , host )
if hostStandardFormat == " " :
# This can happen on Windows systems which are not 64-bit compatible
# so let us drop the IPv6 address.
return False
return self . _checkIPv6Address ( host , hostStandardFormat )
2014-02-16 17:21:20 +01:00
2015-02-20 23:33:17 +01:00
def _checkIPv4Address ( self , host , hostStandardFormat ) :
2015-11-12 17:36:12 +01:00
if host [ 0 ] == ' \x7F ' : # 127/8
2015-11-18 16:22:17 +01:00
logger . debug ( ' Ignoring IP address in loopback range: ' + hostStandardFormat )
2014-02-16 17:21:20 +01:00
return False
2015-11-12 17:36:12 +01:00
if host [ 0 ] == ' \x0A ' : # 10/8
2015-11-18 16:22:17 +01:00
logger . debug ( ' Ignoring IP address in private range: ' + hostStandardFormat )
2014-02-16 17:21:20 +01:00
return False
2015-11-12 17:36:12 +01:00
if host [ 0 : 2 ] == ' \xC0 \xA8 ' : # 192.168/16
2015-11-18 16:22:17 +01:00
logger . debug ( ' Ignoring IP address in private range: ' + hostStandardFormat )
2015-11-12 17:36:12 +01:00
return False
if host [ 0 : 2 ] > = ' \xAC \x10 ' and host [ 0 : 2 ] < ' \xAC \x20 ' : # 172.16/12
2015-11-18 16:22:17 +01:00
logger . debug ( ' Ignoring IP address in private range: ' + hostStandardFormat )
2014-02-16 17:21:20 +01:00
return False
2016-01-26 12:04:06 +01:00
return hostStandardFormat
2014-02-16 17:21:20 +01:00
2015-02-20 23:33:17 +01:00
def _checkIPv6Address ( self , host , hostStandardFormat ) :
2014-02-16 17:21:20 +01:00
if host == ( ' \x00 ' * 15 ) + ' \x01 ' :
2015-11-18 16:22:17 +01:00
logger . debug ( ' Ignoring loopback address: ' + hostStandardFormat )
2014-02-16 17:21:20 +01:00
return False
if host [ 0 ] == ' \xFE ' and ( ord ( host [ 1 ] ) & 0xc0 ) == 0x80 :
2015-11-18 16:22:17 +01:00
logger . debug ( ' Ignoring local address: ' + hostStandardFormat )
2014-02-16 17:21:20 +01:00
return False
if ( ord ( host [ 0 ] ) & 0xfe ) == 0xfc :
2015-11-18 16:22:17 +01:00
logger . debug ( ' Ignoring unique local address: ' + hostStandardFormat )
2014-02-16 17:21:20 +01:00
return False
2016-01-26 12:04:06 +01:00
return hostStandardFormat
2014-02-16 17:21:20 +01:00
2013-06-22 00:29:04 +02:00
# We have received an addr message.
def recaddr ( self , data ) :
numberOfAddressesIncluded , lengthOfNumberOfAddresses = decodeVarint (
data [ : 10 ] )
2013-06-24 21:51:01 +02:00
if shared . verbose > = 1 :
2015-11-18 16:22:17 +01:00
logger . debug ( ' addr message contains ' + str ( numberOfAddressesIncluded ) + ' IP addresses. ' )
2013-06-29 19:29:35 +02:00
2013-09-10 01:26:32 +02:00
if numberOfAddressesIncluded > 1000 or numberOfAddressesIncluded == 0 :
return
if len ( data ) != lengthOfNumberOfAddresses + ( 38 * numberOfAddressesIncluded ) :
2015-11-18 16:22:17 +01:00
logger . debug ( ' addr message does not contain the correct amount of data. Ignoring. ' )
2013-09-10 01:26:32 +02:00
return
2013-06-22 00:29:04 +02:00
2013-09-10 01:26:32 +02:00
for i in range ( 0 , numberOfAddressesIncluded ) :
2015-02-20 23:33:17 +01:00
fullHost = data [ 20 + lengthOfNumberOfAddresses + ( 38 * i ) : 36 + lengthOfNumberOfAddresses + ( 38 * i ) ]
recaddrStream , = unpack ( ' >I ' , data [ 8 + lengthOfNumberOfAddresses + (
38 * i ) : 12 + lengthOfNumberOfAddresses + ( 38 * i ) ] )
2013-09-10 01:26:32 +02:00
if recaddrStream == 0 :
continue
2017-02-06 17:47:05 +01:00
if recaddrStream not in self . streamNumber and ( recaddrStream / 2 ) not in self . streamNumber : # if the embedded stream number and its parent are not in my streams then ignore it. Someone might be trying funny business.
2013-09-10 01:26:32 +02:00
continue
2015-02-20 23:33:17 +01:00
recaddrServices , = unpack ( ' >Q ' , data [ 12 + lengthOfNumberOfAddresses + (
38 * i ) : 20 + lengthOfNumberOfAddresses + ( 38 * i ) ] )
recaddrPort , = unpack ( ' >H ' , data [ 36 + lengthOfNumberOfAddresses + (
38 * i ) : 38 + lengthOfNumberOfAddresses + ( 38 * i ) ] )
2016-01-26 12:04:06 +01:00
hostStandardFormat = self . _checkIPAddress ( fullHost )
if hostStandardFormat is False :
continue
2016-02-20 11:14:42 +01:00
if recaddrPort == 0 :
continue
2013-09-10 01:26:32 +02:00
timeSomeoneElseReceivedMessageFromThisNode , = unpack ( ' >Q ' , data [ lengthOfNumberOfAddresses + (
38 * i ) : 8 + lengthOfNumberOfAddresses + ( 38 * i ) ] ) # This is the 'time' value in the received addr message. 64-bit.
2017-02-08 13:41:56 +01:00
if recaddrStream not in knownnodes . knownNodes : # knownNodes is a dictionary of dictionaries with one outer dictionary for each stream. If the outer stream dictionary doesn't exist yet then we must make it.
with knownnodes . knownNodesLock :
knownnodes . knownNodes [ recaddrStream ] = { }
2017-01-12 06:58:35 +01:00
peerFromAddrMessage = state . Peer ( hostStandardFormat , recaddrPort )
2017-02-08 13:41:56 +01:00
if peerFromAddrMessage not in knownnodes . knownNodes [ recaddrStream ] :
if len ( knownnodes . 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 adding more. Also, make sure that the time that someone else received a message from this node is within three hours from now.
with knownnodes . knownNodesLock :
knownnodes . knownNodes [ recaddrStream ] [ peerFromAddrMessage ] = timeSomeoneElseReceivedMessageFromThisNode
2015-11-18 16:22:17 +01:00
logger . debug ( ' added new node ' + str ( peerFromAddrMessage ) + ' to knownNodes in stream ' + str ( recaddrStream ) )
2013-09-10 01:26:32 +02:00
shared . needToWriteKnownNodesToDisk = True
hostDetails = (
timeSomeoneElseReceivedMessageFromThisNode ,
2015-02-20 23:33:17 +01:00
recaddrStream , recaddrServices , hostStandardFormat , recaddrPort )
2017-01-11 17:00:00 +01:00
protocol . broadcastToSendDataQueues ( (
2017-02-06 19:34:38 +01:00
recaddrStream , ' advertisepeer ' , hostDetails ) )
2013-09-10 01:26:32 +02:00
else :
2017-02-08 13:41:56 +01:00
timeLastReceivedMessageFromThisNode = knownnodes . knownNodes [ recaddrStream ] [
2015-02-20 23:33:17 +01:00
peerFromAddrMessage ]
if ( timeLastReceivedMessageFromThisNode < timeSomeoneElseReceivedMessageFromThisNode ) and ( timeSomeoneElseReceivedMessageFromThisNode < int ( time . time ( ) ) + 900 ) : # 900 seconds for wiggle-room in case other nodes' clocks aren't quite right.
2017-02-08 13:41:56 +01:00
with knownnodes . knownNodesLock :
knownnodes . knownNodes [ recaddrStream ] [ peerFromAddrMessage ] = timeSomeoneElseReceivedMessageFromThisNode
2015-02-20 23:33:17 +01:00
2017-02-06 17:47:05 +01:00
for stream in self . streamNumber :
2017-02-08 13:41:56 +01:00
logger . debug ( ' knownNodes currently has %i nodes for stream %i ' , len ( knownnodes . knownNodes [ stream ] ) , stream )
2013-06-29 19:29:35 +02:00
2013-06-22 00:29:04 +02:00
2014-04-30 21:39:25 +02:00
# Send a huge addr message to our peer. This is only used
# when we fully establish a connection with a
# peer (with the full exchange of version and verack
# messages).
2013-06-22 00:29:04 +02:00
def sendaddr ( self ) :
2017-02-06 17:47:05 +01:00
# We are going to share a maximum number of 1000 addrs (per overlapping
# stream) with our peer. 500 from overlapping streams, 250 from the
# left child stream, and 250 from the right child stream.
for stream in self . streamNumber :
addrsInMyStream = { }
addrsInChildStreamLeft = { }
addrsInChildStreamRight = { }
2017-02-08 13:41:56 +01:00
with knownnodes . knownNodesLock :
if len ( knownnodes . knownNodes [ stream ] ) > 0 :
2017-02-22 15:09:36 +01:00
filtered = { k : v for k , v in knownnodes . knownNodes [ stream ] . items ( )
if v > ( int ( time . time ( ) ) - shared . maximumAgeOfNodesThatIAdvertiseToOthers ) }
2017-02-20 22:32:49 +01:00
elemCount = len ( filtered )
if elemCount > 500 :
elemCount = 500
# only if more recent than 3 hours
addrsInMyStream = random . sample ( filtered . items ( ) , elemCount )
2017-02-06 17:47:05 +01:00
# sent 250 only if the remote isn't interested in it
2017-02-08 13:41:56 +01:00
if len ( knownnodes . knownNodes [ stream * 2 ] ) > 0 and stream not in self . streamNumber :
2017-02-22 15:09:36 +01:00
filtered = { k : v for k , v in knownnodes . knownNodes [ stream * 2 ] . items ( )
if v > ( int ( time . time ( ) ) - shared . maximumAgeOfNodesThatIAdvertiseToOthers ) }
2017-02-20 22:32:49 +01:00
elemCount = len ( filtered )
if elemCount > 250 :
elemCount = 250
2017-02-22 15:09:36 +01:00
addrsInChildStreamLeft = random . sample ( filtered . items ( ) , elemCount )
2017-02-08 13:41:56 +01:00
if len ( knownnodes . knownNodes [ ( stream * 2 ) + 1 ] ) > 0 and stream not in self . streamNumber :
2017-02-22 15:09:36 +01:00
filtered = { k : v for k , v in knownnodes . knownNodes [ stream * 2 + 1 ] . items ( )
if v > ( int ( time . time ( ) ) - shared . maximumAgeOfNodesThatIAdvertiseToOthers ) }
2017-02-20 22:32:49 +01:00
elemCount = len ( filtered )
if elemCount > 250 :
elemCount = 250
2017-02-22 15:09:36 +01:00
addrsInChildStreamRight = random . sample ( filtered . items ( ) , elemCount )
2017-02-06 17:47:05 +01:00
numberOfAddressesInAddrMessage = 0
payload = ' '
2017-02-20 22:32:49 +01:00
for ( HOST , PORT ) , timeLastReceivedMessageFromThisNode in addrsInMyStream :
numberOfAddressesInAddrMessage + = 1
payload + = pack (
' >Q ' , timeLastReceivedMessageFromThisNode ) # 64-bit time
payload + = pack ( ' >I ' , stream )
payload + = pack (
' >q ' , 1 ) # service bit flags offered by this node
payload + = protocol . encodeHost ( HOST )
payload + = pack ( ' >H ' , PORT ) # remote port
for ( HOST , PORT ) , timeLastReceivedMessageFromThisNode in addrsInChildStreamLeft :
numberOfAddressesInAddrMessage + = 1
payload + = pack (
' >Q ' , timeLastReceivedMessageFromThisNode ) # 64-bit time
payload + = pack ( ' >I ' , stream * 2 )
payload + = pack (
' >q ' , 1 ) # service bit flags offered by this node
payload + = protocol . encodeHost ( HOST )
payload + = pack ( ' >H ' , PORT ) # remote port
for ( HOST , PORT ) , timeLastReceivedMessageFromThisNode in addrsInChildStreamRight :
numberOfAddressesInAddrMessage + = 1
payload + = pack (
' >Q ' , timeLastReceivedMessageFromThisNode ) # 64-bit time
payload + = pack ( ' >I ' , ( stream * 2 ) + 1 )
payload + = pack (
' >q ' , 1 ) # service bit flags offered by this node
payload + = protocol . encodeHost ( HOST )
payload + = pack ( ' >H ' , PORT ) # remote port
2017-02-06 17:47:05 +01:00
payload = encodeVarint ( numberOfAddressesInAddrMessage ) + payload
self . sendDataThreadQueue . put ( ( 0 , ' sendRawData ' , protocol . CreatePacket ( ' addr ' , payload ) ) )
2013-06-29 19:29:35 +02:00
2013-06-22 00:29:04 +02:00
# We have received a version message
def recversion ( self , data ) :
if len ( data ) < 83 :
# This version message is unreasonably short. Forget it.
return
2014-04-30 21:39:25 +02:00
if self . verackSent :
"""
We must have already processed the remote node ' s version message.
There might be a time in the future when we Do want to process
a new version message , like if the remote node wants to update
the streams in which they are interested . But for now we ' ll
ignore this version message
"""
return
2017-02-07 19:38:52 +01:00
2014-04-30 21:39:25 +02:00
self . remoteProtocolVersion , = unpack ( ' >L ' , data [ : 4 ] )
2015-11-13 12:32:10 +01:00
self . services , = unpack ( ' >q ' , data [ 4 : 12 ] )
2017-02-07 19:38:52 +01:00
2014-09-10 22:47:51 +02:00
timestamp , = unpack ( ' >Q ' , data [ 12 : 20 ] )
2017-02-07 19:38:52 +01:00
self . timeOffset = timestamp - int ( time . time ( ) )
2016-10-23 10:12:49 +02:00
2014-04-30 21:39:25 +02:00
self . myExternalIP = socket . inet_ntoa ( data [ 40 : 44 ] )
# print 'myExternalIP', self.myExternalIP
self . remoteNodeIncomingPort , = unpack ( ' >H ' , data [ 70 : 72 ] )
# print 'remoteNodeIncomingPort', self.remoteNodeIncomingPort
useragentLength , lengthOfUseragentVarint = decodeVarint (
data [ 80 : 84 ] )
readPosition = 80 + lengthOfUseragentVarint
2017-02-07 19:38:52 +01:00
self . userAgent = data [ readPosition : readPosition + useragentLength ]
2015-10-19 22:33:18 +02:00
# version check
2016-03-26 18:42:22 +01:00
try :
2017-02-07 20:09:11 +01:00
userAgentName , userAgentVersion = self . userAgent [ 1 : - 1 ] . split ( " : " , 2 )
2016-03-26 18:42:22 +01:00
except :
2017-02-07 19:38:52 +01:00
userAgentName = self . userAgent
2016-03-26 18:42:22 +01:00
userAgentVersion = " 0.0.0 "
2015-10-19 22:33:18 +02:00
if userAgentName == " PyBitmessage " :
2017-01-11 14:27:19 +01:00
myVersion = [ int ( n ) for n in softwareVersion . split ( " . " ) ]
2016-03-26 18:42:22 +01:00
try :
remoteVersion = [ int ( n ) for n in userAgentVersion . split ( " . " ) ]
except :
remoteVersion = 0
2015-10-19 22:33:18 +02:00
# remote is newer, but do not cross between stable and unstable
2016-03-26 18:42:22 +01:00
try :
if cmp ( remoteVersion , myVersion ) > 0 and \
( myVersion [ 1 ] % 2 == remoteVersion [ 1 ] % 2 ) :
2017-02-08 13:41:56 +01:00
queues . UISignalQueue . put ( ( ' newVersionAvailable ' , remoteVersion ) )
2016-03-26 18:42:22 +01:00
except :
pass
2015-10-19 22:33:18 +02:00
2014-04-30 21:39:25 +02:00
readPosition + = useragentLength
numberOfStreamsInVersionMessage , lengthOfNumberOfStreamsInVersionMessage = decodeVarint (
data [ readPosition : ] )
readPosition + = lengthOfNumberOfStreamsInVersionMessage
2017-02-06 17:47:05 +01:00
self . remoteStreams = [ ]
for i in range ( numberOfStreamsInVersionMessage ) :
newStreamNumber , lengthOfRemoteStreamNumber = decodeVarint ( data [ readPosition : ] )
readPosition + = lengthOfRemoteStreamNumber
self . remoteStreams . append ( newStreamNumber )
2017-02-07 20:09:11 +01:00
logger . debug ( ' Remote node useragent: %s , streams: ( %s ), time offset: %i s. ' ,
self . userAgent , ' , ' . join ( str ( x ) for x in self . remoteStreams ) , self . timeOffset )
2017-02-06 17:47:05 +01:00
# find shared streams
self . streamNumber = sorted ( set ( state . streamsInWhichIAmParticipating ) . intersection ( self . remoteStreams ) )
2013-06-29 19:29:35 +02:00
2014-04-30 21:39:25 +02:00
shared . connectedHostsList [
2016-11-16 19:36:50 +01:00
self . hostIdent ] = 1 # We use this data structure to not only keep track of what hosts we are connected to so that we don't try to connect to them again, but also to list the connections count on the Network Status tab.
2017-02-06 17:47:05 +01:00
self . sendDataThreadQueue . put ( ( 0 , ' setStreamNumber ' , self . remoteStreams ) )
2017-01-11 14:27:19 +01:00
if data [ 72 : 80 ] == protocol . eightBytesOfRandomDataUsedToDetectConnectionsToSelf :
2014-08-06 21:54:59 +02:00
self . sendDataThreadQueue . put ( ( 0 , ' shutdown ' , ' no data ' ) )
2015-11-18 16:22:17 +01:00
logger . debug ( ' Closing connection to myself: ' + str ( self . peer ) )
2014-04-30 21:39:25 +02:00
return
# The other peer's protocol version is of interest to the sendDataThread but we learn of it
# in this version message. Let us inform the sendDataThread.
self . sendDataThreadQueue . put ( ( 0 , ' setRemoteProtocolVersion ' , self . remoteProtocolVersion ) )
2013-06-22 00:29:04 +02:00
2016-01-26 12:04:06 +01:00
if not isHostInPrivateIPRange ( self . peer . host ) :
2017-02-08 13:41:56 +01:00
with knownnodes . knownNodesLock :
2017-02-06 17:47:05 +01:00
for stream in self . remoteStreams :
2017-02-08 13:41:56 +01:00
knownnodes . knownNodes [ stream ] [ state . Peer ( self . peer . host , self . remoteNodeIncomingPort ) ] = int ( time . time ( ) )
2017-02-06 17:47:05 +01:00
if not self . initiatedConnection :
2017-02-08 13:41:56 +01:00
knownnodes . knownNodes [ stream ] [ state . Peer ( self . peer . host , self . remoteNodeIncomingPort ) ] - = 162000 # penalise inbound, 2 days minus 3 hours
2017-02-06 17:47:05 +01:00
shared . needToWriteKnownNodesToDisk = True
2013-06-22 00:29:04 +02:00
2014-04-30 21:39:25 +02:00
self . sendverack ( )
if self . initiatedConnection == False :
self . sendversion ( )
2013-06-22 00:29:04 +02:00
# Sends a version message
def sendversion ( self ) :
2015-11-18 16:22:17 +01:00
logger . debug ( ' Sending version message ' )
2017-01-11 14:27:19 +01:00
self . sendDataThreadQueue . put ( ( 0 , ' sendRawData ' , protocol . assembleVersionMessage (
2017-02-06 17:47:05 +01:00
self . peer . host , self . peer . port , state . streamsInWhichIAmParticipating , not self . initiatedConnection ) ) )
2013-06-29 19:29:35 +02:00
2013-06-22 00:29:04 +02:00
# Sends a verack message
def sendverack ( self ) :
2015-11-18 16:22:17 +01:00
logger . debug ( ' Sending verack ' )
2017-01-11 14:27:19 +01:00
self . sendDataThreadQueue . put ( ( 0 , ' sendRawData ' , protocol . CreatePacket ( ' verack ' ) ) )
2013-06-22 00:29:04 +02:00
self . verackSent = True
if self . verackReceived :
self . connectionFullyEstablished ( )