Worked on network issue and stopped the udp connection aand required the select's epoll condition and instead used select pollar method

This commit is contained in:
jai.s 2019-11-30 13:18:15 +05:30
parent 0e593b66ad
commit ded1defb1f
No known key found for this signature in database
GPG Key ID: 360CFA25EFC67D12
9 changed files with 96 additions and 53 deletions

View File

@ -102,8 +102,7 @@ def _strerror(err):
return os.strerror(err) return os.strerror(err)
except (ValueError, OverflowError, NameError): except (ValueError, OverflowError, NameError):
if err in errorcode: if err in errorcode:
return errorcode[err] ret18 ("Unknown error {}".format(err))
return "Unknown error %s" % err
class ExitNow(Exception): class ExitNow(Exception):
@ -247,25 +246,24 @@ def select_poller(timeout=0.0, map=None):
if map is None: if map is None:
map = socket_map map = socket_map
if map: if map:
r = [] rd = []
w = [] wt = []
e = [] ex = []
for fd, obj in list(map.items()): for fd, obj in map.items():
is_r = obj.readable() is_r = obj.readable()
is_w = obj.writable() is_w = obj.writable()
if is_r: if is_r:
r.append(fd) rd.append(fd)
# accepting sockets should not be writable # accepting sockets should not be writable
if is_w and not obj.accepting: if is_w and not obj.accepting:
w.append(fd) wt.append(fd)
if is_r or is_w: if is_r or is_w:
e.append(fd) ex.append(fd)
if [] == r == w == e: if [] == rd == wt == ex:
time.sleep(timeout) time.sleep(timeout)
return return
try: try:
r, w, e = select.select(r, w, e, timeout) rd, wt, ex = select.select(rd, wt, ex, timeout)
except KeyboardInterrupt: except KeyboardInterrupt:
return return
except socket.error as err: except socket.error as err:
@ -275,19 +273,19 @@ def select_poller(timeout=0.0, map=None):
if err.args[0] in (WSAENOTSOCK, ): if err.args[0] in (WSAENOTSOCK, ):
return return
for fd in helper_random.randomsample(r, len(r)): for fd in helper_random.randomsample(rd, len(rd)):
obj = map.get(fd) obj = map.get(fd)
if obj is None: if obj is None:
continue continue
read(obj) read(obj)
for fd in helper_random.randomsample(w, len(w)): for fd in helper_random.randomsample(wt, len(wt)):
obj = map.get(fd) obj = map.get(fd)
if obj is None: if obj is None:
continue continue
write(obj) write(obj)
for fd in e: for fd in ex:
obj = map.get(fd) obj = map.get(fd)
if obj is None: if obj is None:
continue continue
@ -491,18 +489,18 @@ def loop(timeout=30.0, use_poll=False, map=None, count=None, poller=None):
# argument which should no longer be used in favor of # argument which should no longer be used in favor of
# "poller" # "poller"
if poller is None: # if poller is None:
if use_poll: # if use_poll:
poller = poll_poller # poller = poll_poller
elif hasattr(select, 'epoll'): # elif hasattr(select, 'epoll'):
poller = epoll_poller # poller = epoll_poller
elif hasattr(select, 'kqueue'): # elif hasattr(select, 'kqueue'):
poller = kqueue_poller # poller = kqueue_poller
elif hasattr(select, 'poll'): # elif hasattr(select, 'poll'):
poller = poll_poller # poller = poll_poller
elif hasattr(select, 'select'): # elif hasattr(select, 'select'):
# poller = select_poller
poller = select_poller poller = select_poller
if timeout == 0: if timeout == 0:
deadline = 0 deadline = 0
else: else:
@ -662,6 +660,9 @@ class dispatcher:
def readable(self): def readable(self):
"""Predicate to indicate download throttle status""" """Predicate to indicate download throttle status"""
print('-------------------------------------')
print('---------------asyncore--------------')
print('-------------------------------------')
if maxDownloadRate > 0: if maxDownloadRate > 0:
return downloadBucket > dispatcher.minTx return downloadBucket > dispatcher.minTx
return True return True
@ -788,7 +789,7 @@ class dispatcher:
def log_info(self, message, log_type='info'): def log_info(self, message, log_type='info'):
"""Conditionally print a message""" """Conditionally print a message"""
if log_type not in self.ignore_log_types: if log_type not in self.ignore_log_types:
print ('{}: {}'.format((log_type, message))) print ('{}: {}'.format(log_type, message))
def handle_read_event(self): def handle_read_event(self):
"""Handle a read event""" """Handle a read event"""

View File

@ -30,6 +30,19 @@ from network.objectracker import missingObjects, ObjectTracker
from queues import objectProcessorQueue, portCheckerQueue, invQueue, addrQueue from queues import objectProcessorQueue, portCheckerQueue, invQueue, addrQueue
from network.randomtrackingdict import RandomTrackingDict from network.randomtrackingdict import RandomTrackingDict
global addr_count
addr_count = 0
global addr_verack
addr_verack = 0
global addr_version
addr_version = 0
# global addr_count
# addr_count = 0
count = 0
class BMProtoError(ProxyError): class BMProtoError(ProxyError):
"""A Bitmessage Protocol Base Error""" """A Bitmessage Protocol Base Error"""
@ -89,6 +102,24 @@ class BMProto(AdvancedDispatcher, ObjectTracker):
protocol.Header.unpack(self.read_buf[:protocol.Header.size]) protocol.Header.unpack(self.read_buf[:protocol.Header.size])
#its shoule be in string #its shoule be in string
self.command = self.command.rstrip('\x00'.encode('utf-8')) self.command = self.command.rstrip('\x00'.encode('utf-8'))
global count,addr_version,addr_count,addr_verack
count+=1
if self.command == 'verack'.encode():
addr_verack+=1
print('the addr_verack count are -{}'.format(addr_verack))
if self.command == 'version'.encode():
addr_version+=1
print('the addr_version count are -{}'.format(addr_version))
if self.command == 'addr'.encode():
addr_count+=1
print('the addr_count count are -{}'.format(addr_count))
# print('The count of the excaution are -{}'.format(count))
# print('-----------count---------------{}'.format(count))
# print('------self command-----------{}'.format(self.command))
# print('----------self---------------{}'.format(self))
if self.magic != 0xE9BEB4D9: if self.magic != 0xE9BEB4D9:
# skip 1 byte in order to sync # skip 1 byte in order to sync
#in the advancedispatched and length commend's #in the advancedispatched and length commend's
@ -158,7 +189,7 @@ class BMProto(AdvancedDispatcher, ObjectTracker):
self.set_state("close") self.set_state("close")
return False return False
if retval: if retval:
print('if retval is true and inside the if ') # print('if retval is true and inside the if ')
self.set_state("bm_header", length=self.payloadLength) self.set_state("bm_header", length=self.payloadLength)
self.bm_proto_reset() self.bm_proto_reset()
# else assume the command requires a different state to follow # else assume the command requires a different state to follow
@ -437,8 +468,8 @@ class BMProto(AdvancedDispatcher, ObjectTracker):
return self.decode_payload_content("LQIQ16sH") return self.decode_payload_content("LQIQ16sH")
def bm_command_addr(self): def bm_command_addr(self):
print('+++++++++++++++++++++++++++\ # print('+++++++++++++++++++++++++++\
bm_command_addr bm_command_addr bm_command_addr ++++++++++++++++') # bm_command_addr bm_command_addr bm_command_addr ++++++++++++++++')
"""Incoming addresses, process them""" """Incoming addresses, process them"""
addresses = self._decode_addr() # pylint: disable=redefined-outer-name addresses = self._decode_addr() # pylint: disable=redefined-outer-name
for i in addresses: for i in addresses:
@ -506,7 +537,7 @@ class BMProto(AdvancedDispatcher, ObjectTracker):
length=self.payloadLength, expectBytes=0) length=self.payloadLength, expectBytes=0)
return False return False
def bm_command_version(self): def bm_command_version(self):
print('inside the bmproto ') # print('inside the bmproto ')
""" """
Incoming version. Incoming version.
Parse and log, remember important things, like streams, bitfields, etc. Parse and log, remember important things, like streams, bitfields, etc.
@ -543,12 +574,12 @@ class BMProto(AdvancedDispatcher, ObjectTracker):
self.isSSL = True self.isSSL = True
if not self.verackReceived: if not self.verackReceived:
return True return True
print('inside the bmproto line') # print('inside the bmproto line')
print('before the value of state are :-{}'.format(self.state)) # print('before the value of state are :-{}'.format(self.state))
self.set_state( self.set_state(
"tls_init" if self.isSSL else "connection_fully_established", "tls_init" if self.isSSL else "connection_fully_established",
length=self.payloadLength, expectBytes=0) length=self.payloadLength, expectBytes=0)
print('After the value of state are :-{}'.format(self.state)) # print('After the value of state are :-{}'.format(self.state))
return False return False
def peerValidityChecks(self): # pylint: disable=too-many-return-statements def peerValidityChecks(self): # pylint: disable=too-many-return-statements
@ -591,9 +622,9 @@ class BMProto(AdvancedDispatcher, ObjectTracker):
return False return False
if self.destination in connectionpool.BMConnectionPool().inboundConnections: if self.destination in connectionpool.BMConnectionPool().inboundConnections:
try: try:
print('+++++++++++++++++++++++++++') # print('+++++++++++++++++++++++++++')
print('self destination host -{}'.format(self.destination.host)) # print('self destination host -{}'.format(self.destination.host))
print('++++++++++++++++++++++++++++++') # print('++++++++++++++++++++++++++++++')
if not protocol.checkSocksIP(self.destination.host): if not protocol.checkSocksIP(self.destination.host):
self.append_write_buf(protocol.assembleErrorMessage( self.append_write_buf(protocol.assembleErrorMessage(
errorText="Too many connections from your IP." errorText="Too many connections from your IP."

View File

@ -11,7 +11,7 @@ from queues import Queue, portCheckerQueue
def getDiscoveredPeer(): def getDiscoveredPeer():
try: try:
peer = random.choice(state.discoveredPeers.keys()) peer = random.choice([key for key in state.discoveredPeers.keys()])
except (IndexError, KeyError): except (IndexError, KeyError):
raise ValueError raise ValueError
try: try:
@ -24,6 +24,7 @@ def getDiscoveredPeer():
def chooseConnection(stream): def chooseConnection(stream):
haveOnion = BMConfigParser().safeGet( haveOnion = BMConfigParser().safeGet(
"bitmessagesettings", "socksproxytype")[0:5] == 'SOCKS' "bitmessagesettings", "socksproxytype")[0:5] == 'SOCKS'
if state.trustedPeer: if state.trustedPeer:
return state.trustedPeer return state.trustedPeer
try: try:
@ -37,14 +38,14 @@ def chooseConnection(stream):
# discovered peers are already filtered by allowed streams # discovered peers are already filtered by allowed streams
return getDiscoveredPeer() return getDiscoveredPeer()
for _ in range(50): for _ in range(50):
peer = random.choice(list(knownnodes.knownNodes[stream].keys())) peer = random.choice([key for key in knownnodes.knownNodes[stream].keys()])
try: try:
peer_info = knownnodes.knownNodes[stream][peer] peer_info = knownnodes.knownNodes[stream][peer]
if peer_info.get('self'): if peer_info.get('self'):
continue continue
rating = peer_info["rating"] rating = peer_info["rating"]
except TypeError: except TypeError:
logger.warning('Error in %s', peer) logger.warning('Error in {}'.format(peer))
rating = 0 rating = 0
if haveOnion: if haveOnion:
# onion addresses have a higher priority when SOCKS # onion addresses have a higher priority when SOCKS

View File

@ -147,6 +147,7 @@ class BMConnectionPool(object):
port = int(BMConfigParser().safeGet("bitmessagesettings", "port")) port = int(BMConfigParser().safeGet("bitmessagesettings", "port"))
# correct port even if it changed # correct port even if it changed
ls = TCPServer(host=bind, port=port) ls = TCPServer(host=bind, port=port)
print('inside the startListening method')
self.listeningSockets[ls.destination] = ls self.listeningSockets[ls.destination] = ls
def startUDPSocket(self, bind=None): def startUDPSocket(self, bind=None):
@ -219,6 +220,7 @@ class BMConnectionPool(object):
self.startBootstrappers() self.startBootstrappers()
knownnodes.knownNodesActual = True knownnodes.knownNodesActual = True
if not self.bootstrapped: if not self.bootstrapped:
self.bootstrapped = True self.bootstrapped = True
Proxy.proxy = ( Proxy.proxy = (
BMConfigParser().safeGet( BMConfigParser().safeGet(
@ -295,6 +297,7 @@ class BMConnectionPool(object):
): ):
# FIXME: rating will be increased after next connection # FIXME: rating will be increased after next connection
i.handle_close() i.handle_close()
if acceptConnections: if acceptConnections:
if not self.listeningSockets: if not self.listeningSockets:
if BMConfigParser().safeGet('network', 'bind') == '': if BMConfigParser().safeGet('network', 'bind') == '':
@ -306,7 +309,7 @@ class BMConnectionPool(object):
).split(): ).split():
self.startListening(bind) self.startListening(bind)
logger.info('Listening for incoming connections.') logger.info('Listening for incoming connections.')
if not self.udpSockets: if False:
# self.udpSockets :- {'0.0.0.0': <network.udp.UDPSocket connected at 0x7f95cce7d7b8>} # self.udpSockets :- {'0.0.0.0': <network.udp.UDPSocket connected at 0x7f95cce7d7b8>}
if BMConfigParser().safeGet('network', 'bind') == '': if BMConfigParser().safeGet('network', 'bind') == '':
self.startUDPSocket() self.startUDPSocket()

View File

@ -32,7 +32,7 @@ def connectedHostsList():
retval.append(i) retval.append(i)
except AttributeError: except AttributeError:
pass pass
print('#################### retval -{}'.format(retval)) # print('#################### retval -{}'.format(retval))
return retval return retval

View File

@ -367,8 +367,10 @@ class TCPServer(AdvancedDispatcher):
self.create_socket(socket.AF_INET, socket.SOCK_STREAM) self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self.set_reuse_addr() self.set_reuse_addr()
for attempt in range(50): for attempt in range(50):
print('inside the attempt of line 371')
try: try:
if attempt > 0: if attempt > 0:
print('inside the if condition attempt in 373')
port = random.randint(32767, 65535) port = random.randint(32767, 65535)
self.bind((host, port)) self.bind((host, port))
except socket.error as e: except socket.error as e:
@ -376,6 +378,7 @@ class TCPServer(AdvancedDispatcher):
continue continue
else: else:
if attempt > 0: if attempt > 0:
print('inside the if condition attempt in 381')
BMConfigParser().set( BMConfigParser().set(
'bitmessagesettings', 'port', str(port)) 'bitmessagesettings', 'port', str(port))
BMConfigParser().save() BMConfigParser().save()

View File

@ -67,7 +67,7 @@ class TLSDispatcher(AdvancedDispatcher): # pylint: disable=too-many-instanc
self.isSSL = False self.isSSL = False
def state_tls_init(self): def state_tls_init(self):
print() # print()
"""Prepare sockets for TLS handshake""" """Prepare sockets for TLS handshake"""
# pylint: disable=attribute-defined-outside-init # pylint: disable=attribute-defined-outside-init
self.isSSL = True self.isSSL = True
@ -95,16 +95,16 @@ class TLSDispatcher(AdvancedDispatcher): # pylint: disable=too-many-instanc
ciphers=self.ciphers, do_handshake_on_connect=False) ciphers=self.ciphers, do_handshake_on_connect=False)
self.sslSocket.setblocking(0) self.sslSocket.setblocking(0)
self.want_read = self.want_write = True self.want_read = self.want_write = True
print('before tls file python 98 state are :- {}'.format(self.state)) # print('before tls file python 98 state are :- {}'.format(self.state))
self.set_state("tls_handshake") self.set_state("tls_handshake")
print('after tls file python 100 state are :- {}'.format(self.state)) # print('after tls file python 100 state are :- {}'.format(self.state))
return False return False
# if hasattr(self.socket, "context"): # if hasattr(self.socket, "context"):
# self.socket.context.set_ecdh_curve("secp256k1") # self.socket.context.set_ecdh_curve("secp256k1")
@staticmethod @staticmethod
def state_tls_handshake(): def state_tls_handshake():
print("tls's state_tls_handshake method in line 107") # print("tls's state_tls_handshake method in line 107")
"""Do nothing while TLS handshake is pending, as during this phase we need to react to callbacks instead""" """Do nothing while TLS handshake is pending, as during this phase we need to react to callbacks instead"""
return False return False
@ -182,7 +182,7 @@ class TLSDispatcher(AdvancedDispatcher): # pylint: disable=too-many-instanc
return return
def tls_handshake(self): def tls_handshake(self):
print('inside the tls_handshake') # print('inside the tls_handshake')
"""Perform TLS handshake and handle its stages""" """Perform TLS handshake and handle its stages"""
# wait for flush # wait for flush
if self.write_buf: if self.write_buf:

View File

@ -72,11 +72,14 @@ class UDPSocket(BMProto): # pylint: disable=too-many-instance-attribut
addresses = self._decode_addr() addresses = self._decode_addr()
# only allow peer discovery from private IPs in order to avoid # only allow peer discovery from private IPs in order to avoid
# attacks from random IPs on the internet # attacks from random IPs on the internet
if not self.local: # if not self.local:
return True # return True
self.local = True
remoteport = False remoteport = False
for seenTime, stream, services, ip, port in addresses: for seenTime, stream, services, ip, port in addresses:
decodedIP = protocol.checkIPAddress(str(ip)) # decodedIP = bool(protocol.checkIPAddress(ip))
decodedIP = False
if stream not in state.streamsInWhichIAmParticipating: if stream not in state.streamsInWhichIAmParticipating:
continue continue
if (seenTime < time.time() - self.maxTimeOffset or seenTime > time.time() + self.maxTimeOffset): if (seenTime < time.time() - self.maxTimeOffset or seenTime > time.time() + self.maxTimeOffset):
@ -88,7 +91,7 @@ class UDPSocket(BMProto): # pylint: disable=too-many-instance-attribut
if remoteport is False: if remoteport is False:
return True return True
logger.debug( logger.debug(
"received peer discovery from %s:%i (port %i):", "received peer discovery from {}:{} (port {}):",
self.destination.host, self.destination.port, remoteport) self.destination.host, self.destination.port, remoteport)
if self.local: if self.local:
state.discoveredPeers[ state.discoveredPeers[

View File

@ -178,6 +178,7 @@ def haveSSL(server=False):
python < 2.7.9's ssl library does not support ECDSA server due to python < 2.7.9's ssl library does not support ECDSA server due to
missing initialisation of available curves, but client works ok missing initialisation of available curves, but client works ok
""" """
return False
if not server: if not server:
return True return True
elif sys.version_info >= (2, 7, 9): elif sys.version_info >= (2, 7, 9):