278 lines
11 KiB
Raw Normal View History

#!/usr/bin/env python2.7
2012-11-19 19:45:05 +00:00
# Copyright (c) 2012 Jonathan Warren
# Copyright (c) 2012 The Bitmessage developers
# Distributed under the MIT/X11 software license. See the accompanying
# file COPYING or
# Right now, PyBitmessage only support connecting to stream 1. It doesn't
# yet contain logic to expand into further streams.
2012-11-19 19:45:05 +00:00
# The software version variable is now held in
import sys
#Version check
#Older versions of Python don't support the print function while Python 3 doesn't
#like the print statement, so we use sys.stdout for the version check. After this
#check we can then use the print function in the remainder of this file. Currently
#in order to use logging, a lot of unnecessary code needs to be executed which could
#potentially render this version check useless. So logging won't be used here until
#there is a more efficient way to configure logging
if sys.hexversion >= 0x3000000:
msg = "PyBitmessage does not support Python 3. Python 2.7.3 or later is required. Your version: %s" % sys.version
if sys.hexversion < 0x20703F0:
msg = "You should use Python 2.7.3 or greater (but not Python 3). Your version: %s" % sys.version
import signal # Used to capture a Ctrl-C keypress so that Bitmessage can shutdown gracefully.
# The next 3 are used for the API
import singleton
2013-06-25 20:26:12 +00:00
import os
import socket
import ctypes
from struct import pack
2013-05-01 20:06:55 +00:00
from SimpleXMLRPCServer import SimpleXMLRPCServer
2014-01-20 20:25:02 +00:00
from api import MySimpleXMLRPCRequestHandler
from helper_startup import isOurOperatingSystemLimitedToHavingVeryFewHalfOpenConnections
import shared
from helper_sql import sqlQuery
import threading
# Classes
#from helper_sql import *
#from class_sqlThread import *
from class_sqlThread import sqlThread
from class_singleCleaner import singleCleaner
#from class_singleWorker import *
from class_objectProcessor import objectProcessor
from class_outgoingSynSender import outgoingSynSender
from class_singleListener import singleListener
from class_singleWorker import singleWorker
#from class_addressGenerator import *
from class_addressGenerator import addressGenerator
2013-08-25 20:23:28 +00:00
from debug import logger
# Helper Functions
import helper_bootstrap
import helper_generic
from subprocess import call
import time
2013-05-01 20:06:55 +00:00
def connectToStream(streamNumber):
shared.streamsInWhichIAmParticipating[streamNumber] = 'no data'
2013-05-01 20:06:55 +00:00
selfInitiatedConnections[streamNumber] = {}
2013-09-06 22:55:12 +00:00
shared.inventorySets[streamNumber] = set()
queryData = sqlQuery('''SELECT hash FROM inventory WHERE streamnumber=?''', streamNumber)
for row in queryData:
2014-01-20 20:25:02 +00:00
if isOurOperatingSystemLimitedToHavingVeryFewHalfOpenConnections():
# Some XP and Vista systems can only have 10 outgoing connections at a time.
maximumNumberOfHalfOpenConnections = 9
2014-01-20 20:25:02 +00:00
maximumNumberOfHalfOpenConnections = 64
for i in range(maximumNumberOfHalfOpenConnections):
2013-05-01 20:06:55 +00:00
a = outgoingSynSender()
a.setup(streamNumber, selfInitiatedConnections)
2013-05-01 20:06:55 +00:00
def _fixWinsock():
if not ('win32' in sys.platform) and not ('win64' in sys.platform):
# Python 2 on Windows doesn't define a wrapper for
# socket.inet_ntop but we can make one ourselves using ctypes
if not hasattr(socket, 'inet_ntop'):
addressToString = ctypes.windll.ws2_32.WSAAddressToStringA
def inet_ntop(family, host):
if family == socket.AF_INET:
if len(host) != 4:
raise ValueError("invalid IPv4 host")
host = pack("hH4s8s", socket.AF_INET, 0, host, "\0" * 8)
elif family == socket.AF_INET6:
if len(host) != 16:
raise ValueError("invalid IPv6 host")
host = pack("hHL16sL", socket.AF_INET6, 0, 0, host, 0)
raise ValueError("invalid address family")
buf = "\0" * 64
lengthBuf = pack("I", len(buf))
addressToString(host, len(host), None, buf, lengthBuf)
return buf[0:buf.index("\0")]
socket.inet_ntop = inet_ntop
# Same for inet_pton
if not hasattr(socket, 'inet_pton'):
stringToAddress = ctypes.windll.ws2_32.WSAStringToAddressA
def inet_pton(family, host):
buf = "\0" * 28
lengthBuf = pack("I", len(buf))
if stringToAddress(str(host),
lengthBuf) != 0:
raise socket.error("illegal IP address passed to inet_pton")
if family == socket.AF_INET:
return buf[4:8]
elif family == socket.AF_INET6:
return buf[8:24]
raise ValueError("invalid address family")
socket.inet_pton = inet_pton
# These sockopts are needed on for IPv6 support
if not hasattr(socket, 'IPPROTO_IPV6'):
socket.IPPROTO_IPV6 = 41
if not hasattr(socket, 'IPV6_V6ONLY'):
socket.IPV6_V6ONLY = 27
# This thread, of which there is only one, runs the API.
2013-05-01 20:06:55 +00:00
class singleAPI(threading.Thread):
2013-05-01 20:06:55 +00:00
def __init__(self):
2013-03-19 17:32:37 +00:00
def run(self):
se = SimpleXMLRPCServer((shared.config.get('bitmessagesettings', 'apiinterface'), shared.config.getint(
'bitmessagesettings', 'apiport')), MySimpleXMLRPCRequestHandler, True, True)
2013-03-19 17:32:37 +00:00
# This is a list of current connections (the thread pointers at least)
selfInitiatedConnections = {}
if shared.useVeryEasyProofOfWorkForTesting:
shared.networkDefaultProofOfWorkNonceTrialsPerByte = int(
shared.networkDefaultProofOfWorkNonceTrialsPerByte / 16)
shared.networkDefaultPayloadLengthExtraBytes = int(
shared.networkDefaultPayloadLengthExtraBytes / 7000)
2013-01-18 22:38:09 +00:00
class Main:
def start(self, daemon=False):
2013-09-05 00:14:25 +00:00
shared.daemon = daemon
# is the application already running? If yes then exit.
thisapp = singleton.singleinstance()
# get curses flag
curses = False
if '-c' in sys.argv:
curses = True
signal.signal(signal.SIGINT, helper_generic.signal_handler)
# signal.signal(signal.SIGINT, signal.SIG_DFL)
# Start the address generation thread
addressGeneratorThread = addressGenerator()
addressGeneratorThread.daemon = True # close the main program even if there are threads left
# Start the thread that calculates POWs
singleWorkerThread = singleWorker()
singleWorkerThread.daemon = True # close the main program even if there are threads left
# Start the SQL thread
sqlLookup = sqlThread()
sqlLookup.daemon = False # DON'T close the main program even if there are threads left. The closeEvent should command this thread to exit gracefully.
# Start the thread that calculates POWs
objectProcessorThread = objectProcessor()
objectProcessorThread.daemon = False # DON'T close the main program even the thread remains. This thread checks the shutdown variable after processing each object.
# Start the cleanerThread
singleCleanerThread = singleCleaner()
singleCleanerThread.daemon = True # close the main program even if there are threads left
if shared.safeConfigGetBoolean('bitmessagesettings', 'apienabled'):
apiNotifyPath = shared.config.get(
'bitmessagesettings', 'apinotifypath')
apiNotifyPath = ''
if apiNotifyPath != '':
with shared.printLock:
print('Trying to call', apiNotifyPath)
2013-05-02 15:53:54 +00:00
call([apiNotifyPath, "startingUp"])
singleAPIThread = singleAPI()
singleAPIThread.daemon = True # close the main program even if there are threads left
2013-05-01 20:06:55 +00:00
singleListenerThread = singleListener()
singleListenerThread.daemon = True # close the main program even if there are threads left
2013-05-01 20:06:55 +00:00
if daemon == False and shared.safeConfigGetBoolean('bitmessagesettings', 'daemon') == False:
if curses == False:
from PyQt4 import QtCore, QtGui
except Exception as err:
print('PyBitmessage requires PyQt unless you want to run it as a daemon and interact with it using the API. You can download PyQt from or by searching Google for \'PyQt Download\'. If you want to run in daemon mode, see')
print('Error message:', err)
print('You can also run PyBitmessage with the new curses interface by providing \'-c\' as a commandline argument.')
import bitmessageqt
print('Running with curses')
import bitmessagecurses
shared.config.remove_option('bitmessagesettings', 'dontconnect')
2013-05-02 20:55:13 +00:00
if daemon:
with shared.printLock:
print('Running as a daemon. The main program should exit this thread.')
with shared.printLock:
print('Running as a daemon. You can use Ctrl+C to exit.')
while True:
2013-09-28 12:09:15 +00:00
def stop(self):
with shared.printLock:
print('Stopping Bitmessage Deamon.')
2013-09-28 12:09:15 +00:00
#TODO: nice function but no one is using this
def getApiAddress(self):
if not shared.safeConfigGetBoolean('bitmessagesettings', 'apienabled'):
return None
address = shared.config.get('bitmessagesettings', 'apiinterface')
port = shared.config.getint('bitmessagesettings', 'apiport')
return {'address':address,'port':port}
2013-09-28 12:09:15 +00:00
2013-08-05 20:29:06 +00:00
if __name__ == "__main__":
mainprogram = Main()
2013-09-28 12:09:15 +00:00
# So far, the creation of and management of the Bitmessage protocol and this
# client is a one-man operation. Bitcoin tips are quite appreciated.
# 1H5XaDA6fYENLbknwZyjiYXYPQaFjjLX2u