This repository has been archived on 2024-12-01. You can view files and clone it, but cannot push or open issues or pull requests.
PyBitmessage-2024-12-01/src/helper_generic.py

103 lines
3.0 KiB
Python
Raw Normal View History

"""
Helper Generic perform generic oprations for threading.
Also perform some conversion operations.
"""
2018-04-06 15:03:30 +02:00
import socket
import sys
2016-03-23 23:26:57 +01:00
from binascii import hexlify, unhexlify
from multiprocessing import current_process
2018-04-06 14:20:55 +02:00
import threading
import traceback
import shared
2018-04-09 06:38:48 +02:00
import state
from debug import logger
import queues
import shutdown
2018-04-06 14:20:55 +02:00
def powQueueSize():
curWorkerQueue = queues.workerQueue.qsize()
2018-04-06 14:20:55 +02:00
for thread in threading.enumerate():
try:
if thread.name == "singleWorker":
curWorkerQueue += thread.busy
except Exception as err:
logger.info("Thread error %s", err)
return curWorkerQueue
2018-04-06 14:20:55 +02:00
def convertIntToString(n):
a = __builtins__.hex(n)
if a[-1:] == 'L':
a = a[:-1]
if (len(a) % 2) == 0:
2016-03-23 23:26:57 +01:00
return unhexlify(a[2:])
else:
2016-03-23 23:26:57 +01:00
return unhexlify('0' + a[2:])
2018-04-06 14:20:55 +02:00
def convertStringToInt(s):
2016-03-23 23:26:57 +01:00
return int(hexlify(s), 16)
2018-04-06 14:20:55 +02:00
def allThreadTraceback(frame):
2018-04-06 14:20:55 +02:00
id2name = dict([(th.ident, th.name) for th in threading.enumerate()])
code = []
for threadId, stack in sys._current_frames().items():
2018-04-06 14:20:55 +02:00
code.append("\n# Thread: %s(%d)" % (
id2name.get(threadId, ""), threadId))
for filename, lineno, name, line in traceback.extract_stack(stack):
2018-04-06 14:20:55 +02:00
code.append('File: "%s", line %d, in %s' % (
filename, lineno, name))
if line:
code.append(" %s" % (line.strip()))
print "\n".join(code)
2018-04-06 14:20:55 +02:00
def signal_handler(signal, frame):
2018-04-06 14:20:55 +02:00
logger.error("Got signal %i in %s/%s", signal,
current_process().name,
threading.current_thread().name)
if "PoolWorker" in current_process().name:
raise SystemExit
2018-04-06 14:20:55 +02:00
if threading.current_thread().name not in (
"PyBitmessage", "MainThread"):
return
logger.error("Got signal %i", signal)
2018-04-09 06:38:48 +02:00
if shared.thisapp.daemon or not state.enableGUI: # FIXME redundant?
shutdown.doCleanShutdown()
else:
allThreadTraceback(frame)
print 'Unfortunately you cannot use Ctrl+C when running the UI because the UI captures the signal.'
2018-04-06 14:20:55 +02:00
def isHostInPrivateIPRange(host):
2018-04-06 14:20:55 +02:00
if ":" in host: # IPv6
hostAddr = socket.inet_pton(socket.AF_INET6, host)
if hostAddr == ('\x00' * 15) + '\x01':
return False
if hostAddr[0] == '\xFE' and (ord(hostAddr[1]) & 0xc0) == 0x80:
return False
if (ord(hostAddr[0]) & 0xfe) == 0xfc:
return False
elif ".onion" not in host:
if host[:3] == '10.':
return True
if host[:4] == '172.':
if host[6] == '.':
if int(host[4:6]) >= 16 and int(host[4:6]) <= 31:
return True
if host[:8] == '192.168.':
return True
# Multicast
if host[:3] >= 224 and host[:3] <= 239 and host[4] == '.':
return True
return False
2018-04-06 14:20:55 +02:00
def addDataPadding(data, desiredMsgLength=12, paddingChar='\x00'):
2014-05-02 18:47:50 +02:00
return data + paddingChar * (desiredMsgLength - len(data))