From 32aaaf20234bea3cf217513493337374a8ddfa07 Mon Sep 17 00:00:00 2001 From: Jonathan Warren Date: Fri, 21 Jun 2013 15:44:28 -0400 Subject: [PATCH] Fix bugs in githup pull request #238 --- src/bitmessagemain.py | 79 ++++++++++++++++++++++++++++++++--- src/class_addressGenerator.py | 1 + src/class_singleListener.py | 76 --------------------------------- src/class_sqlThread.py | 1 + src/helper_bootstrap.py | 1 + 5 files changed, 76 insertions(+), 82 deletions(-) delete mode 100644 src/class_singleListener.py diff --git a/src/bitmessagemain.py b/src/bitmessagemain.py index 4383b0ac..52e89846 100644 --- a/src/bitmessagemain.py +++ b/src/bitmessagemain.py @@ -19,7 +19,6 @@ useVeryEasyProofOfWorkForTesting = False # If you set this to True while on the encryptedBroadcastSwitchoverTime = 1369735200 import sys -import ConfigParser import Queue from addresses import * import shared @@ -33,13 +32,11 @@ import pickle import random import sqlite3 from time import strftime, localtime, gmtime -import shutil # used for moving the messages.dat file import string import socks import highlevelcrypto from pyelliptic.openssl import OpenSSL -import ctypes -from pyelliptic import arithmetic +#import ctypes import signal # Used to capture a Ctrl-C keypress so that Bitmessage can shutdown gracefully. # The next 3 are used for the API from SimpleXMLRPCServer import * @@ -49,7 +46,6 @@ import singleton import proofofwork # Classes -from class_singleListener import * from class_sqlThread import * from class_singleCleaner import * from class_addressGenerator import * @@ -223,10 +219,81 @@ class outgoingSynSender(threading.Thread): time.sleep(0.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(threading.Thread): + + def __init__(self): + threading.Thread.__init__(self) + + def run(self): + # We don't want to accept incoming connections if the user is using a + # SOCKS proxy. If they eventually select proxy 'none' then this will + # start listening for connections. + while shared.config.get('bitmessagesettings', 'socksproxytype')[0:5] == 'SOCKS': + time.sleep(300) + + shared.printLock.acquire() + print 'Listening for incoming connections.' + shared.printLock.release() + HOST = '' # Symbolic name meaning all available interfaces + PORT = shared.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) + + while True: + # 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() + + # 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 'We are already connected to', HOST + '. Ignoring connection.' + 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() + + rd = receiveDataThread() + rd.daemon = True # close the main program even if there are threads left + rd.setup( + a, HOST, PORT, -1, objectsOfWhichThisRemoteNodeIsAlreadyAware) + rd.start() + + shared.printLock.acquire() + print self, 'connected to', HOST, 'during INCOMING request.' + shared.printLock.release() + # This thread is created either by the synSenderThread(for outgoing # connections) or the singleListenerThread(for incoming connectiosn). - class receiveDataThread(threading.Thread): def __init__(self): diff --git a/src/class_addressGenerator.py b/src/class_addressGenerator.py index 3059bc90..e22fdd51 100644 --- a/src/class_addressGenerator.py +++ b/src/class_addressGenerator.py @@ -6,6 +6,7 @@ from pyelliptic.openssl import OpenSSL import ctypes import hashlib from addresses import * +from pyelliptic import arithmetic class addressGenerator(threading.Thread): diff --git a/src/class_singleListener.py b/src/class_singleListener.py deleted file mode 100644 index 9e982a24..00000000 --- a/src/class_singleListener.py +++ /dev/null @@ -1,76 +0,0 @@ -import threading -import shared -import socket - -# 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(threading.Thread): - - def __init__(self): - threading.Thread.__init__(self) - - def run(self): - # We don't want to accept incoming connections if the user is using a - # SOCKS proxy. If they eventually select proxy 'none' then this will - # start listening for connections. - while shared.config.get('bitmessagesettings', 'socksproxytype')[0:5] == 'SOCKS': - time.sleep(300) - - shared.printLock.acquire() - print 'Listening for incoming connections.' - shared.printLock.release() - HOST = '' # Symbolic name meaning all available interfaces - PORT = shared.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) - - while True: - # 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() - - # 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 'We are already connected to', HOST + '. Ignoring connection.' - 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() - - rd = receiveDataThread() - rd.daemon = True # close the main program even if there are threads left - rd.setup( - a, HOST, PORT, -1, objectsOfWhichThisRemoteNodeIsAlreadyAware) - rd.start() - - shared.printLock.acquire() - print self, 'connected to', HOST, 'during INCOMING request.' - shared.printLock.release() diff --git a/src/class_sqlThread.py b/src/class_sqlThread.py index e483284f..210a78e3 100644 --- a/src/class_sqlThread.py +++ b/src/class_sqlThread.py @@ -2,6 +2,7 @@ import threading import shared import sqlite3 import time +import shutil # used for moving the messages.dat file # 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 diff --git a/src/helper_bootstrap.py b/src/helper_bootstrap.py index 2dcd285c..296dda6b 100644 --- a/src/helper_bootstrap.py +++ b/src/helper_bootstrap.py @@ -2,6 +2,7 @@ import shared import socket import defaultKnownNodes import pickle +import time def knownNodes(): try: