Merge pull request #160 from Atheros1/master

Prevent incoming connection flooding from crashing singleListener thread
This commit is contained in:
Jonathan Warren 2013-05-21 10:02:38 -07:00
commit 2a7c6c2805
1 changed files with 17 additions and 19 deletions

View File

@ -206,14 +206,23 @@ class singleListener(threading.Thread):
#We don't want to accept incoming connections if the user is using a SOCKS proxy. If the user eventually select proxy 'none' then this will start listening for connections.
while shared.config.get('bitmessagesettings', 'socksproxytype')[0:5] == 'SOCKS':
time.sleep(10)
while len(shared.connectedHostsList) > 220:
shared.printLock.acquire()
print 'We are connected to too many people. Not accepting further incoming connections for ten seconds.'
shared.printLock.release()
time.sleep(10)
a,(HOST,PORT) = sock.accept()
#Users are finding that if they run more than one node in the same network (thus with the same public IP), they can not connect with the second node. This is because this section of code won't accept the connection from the same IP. This problem will go away when the Bitmessage network grows beyond being tiny but in the mean time I'll comment out this code section.
"""while HOST in shared.connectedHostsList:
print 'incoming connection is from a host in shared.connectedHostsList (we are already connected to it). Ignoring it.'
a.close()
a,(HOST,PORT) = sock.accept()"""
objectsOfWhichThisRemoteNodeIsAlreadyAware = {}
#The following code will, unfortunately, block an incoming connection if someone else on the same LAN is already connected because the two computers will share the same external IP. This is here to prevent connection flooding.
while HOST in shared.connectedHostsList:
shared.printLock.acquire()
print 'incoming connection is from a host in shared.connectedHostsList (we are already connected to it). Ignoring it.'
shared.printLock.release()
a.close()
a,(HOST,PORT) = sock.accept()
objectsOfWhichThisRemoteNodeIsAlreadyAware = {}
a.settimeout(20)
sd = sendDataThread()
sd.setup(a,HOST,PORT,-1,objectsOfWhichThisRemoteNodeIsAlreadyAware)
sd.start()
@ -239,7 +248,6 @@ class receiveDataThread(threading.Thread):
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.payloadLength = 0 #This is the protocol payload length thus it doesn't include the 24 byte message header
self.objectsThatWeHaveYetToCheckAndSeeWhetherWeAlreadyHave = {}
@ -281,11 +289,6 @@ class receiveDataThread(threading.Thread):
"""try:
#self.sock.shutdown(socket.SHUT_RDWR)
self.sock.close()
except Exception, err:
print 'Within receiveDataThread run(), self.sock.shutdown or .close() failed.', err"""
try:
del selfInitiatedConnections[self.streamNumber][self]
@ -327,7 +330,7 @@ class receiveDataThread(threading.Thread):
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).
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).
shared.knownNodesLock.acquire()
shared.knownNodes[self.streamNumber][self.HOST] = (self.PORT,int(time.time()))
shared.knownNodesLock.release()
@ -431,6 +434,7 @@ class receiveDataThread(threading.Thread):
if not self.initiatedConnection:
#self.emit(SIGNAL("setStatusIcon(PyQt_PyObject)"),'green')
shared.UISignalQueue.put(('setStatusIcon','green'))
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.
shared.UISignalQueue.put(('updateNetworkStatusTab','no data'))
remoteNodeIncomingPort, remoteNodeSeenTime = shared.knownNodes[self.streamNumber][self.HOST]
shared.printLock.acquire()
@ -445,8 +449,6 @@ class receiveDataThread(threading.Thread):
shared.printLock.acquire()
print 'We are connected to too many people. Closing connection.'
shared.printLock.release()
#self.sock.shutdown(socket.SHUT_RDWR)
#self.sock.close()
shared.broadcastToSendDataQueues((0, 'shutdown', self.HOST))
return
self.sendBigInv()
@ -1932,8 +1934,6 @@ class receiveDataThread(threading.Thread):
print 'Remote node useragent:', useragent, ' stream number:', self.streamNumber
shared.printLock.release()
if self.streamNumber != 1:
#self.sock.shutdown(socket.SHUT_RDWR)
#self.sock.close()
shared.broadcastToSendDataQueues((0, 'shutdown', self.HOST))
shared.printLock.acquire()
print 'Closed connection to', self.HOST, 'because they are interested in stream', self.streamNumber,'.'
@ -1944,8 +1944,6 @@ class receiveDataThread(threading.Thread):
if not self.initiatedConnection:
shared.broadcastToSendDataQueues((0,'setStreamNumber',(self.HOST,self.streamNumber)))
if data[72:80] == eightBytesOfRandomDataUsedToDetectConnectionsToSelf:
#self.sock.shutdown(socket.SHUT_RDWR)
#self.sock.close()
shared.broadcastToSendDataQueues((0, 'shutdown', self.HOST))
shared.printLock.acquire()
print 'Closing connection to myself: ', self.HOST