flake8 and style correction for network.udp

This commit is contained in:
Dmitri Bogomolov 2018-10-19 11:50:46 +03:00
parent 625e83f4b4
commit 1c914d8cd8
Signed by untrusted user: g1itch
GPG Key ID: 720A756F18DEED13

View File

@ -1,17 +1,13 @@
import time import time
import Queue
import socket import socket
from debug import logger
from network.advanceddispatcher import AdvancedDispatcher
from network.bmproto import BMProtoError, BMProtoInsufficientDataError, BMProto
from network.bmobject import BMObject, BMObjectInsufficientPOWError, BMObjectInvalidDataError, BMObjectExpiredError, BMObjectInvalidError, BMObjectAlreadyHaveError
import network.asyncore_pollchoose as asyncore
from network.objectracker import ObjectTracker
from queues import objectProcessorQueue, UISignalQueue, receiveDataQueue
import state import state
import protocol import protocol
from bmproto import BMProto
from debug import logger
from objectracker import ObjectTracker
from queues import receiveDataQueue
class UDPSocket(BMProto): class UDPSocket(BMProto):
port = 8444 port = 8444
@ -29,23 +25,18 @@ class UDPSocket(BMProto):
if sock is None: if sock is None:
if host is None: if host is None:
host = '' host = ''
if ":" in host: self.create_socket(
self.create_socket(socket.AF_INET6, socket.SOCK_DGRAM) socket.AF_INET6 if ":" in host else socket.AF_INET,
else: socket.SOCK_DGRAM
self.create_socket(socket.AF_INET, socket.SOCK_DGRAM) )
self.set_socket_reuse() self.set_socket_reuse()
logger.info("Binding UDP socket to %s:%i", host, UDPSocket.port) logger.info("Binding UDP socket to %s:%i", host, self.port)
self.socket.bind((host, UDPSocket.port)) self.socket.bind((host, self.port))
#BINDTODEVICE is only available on linux and requires root
#try:
#print "binding to %s" % (host)
#self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_BINDTODEVICE, host)
#except AttributeError:
else: else:
self.socket = sock self.socket = sock
self.set_socket_reuse() self.set_socket_reuse()
self.listening = state.Peer(self.socket.getsockname()[0], self.socket.getsockname()[1]) self.listening = state.Peer(*self.socket.getsockname())
self.destination = state.Peer(self.socket.getsockname()[0], self.socket.getsockname()[1]) self.destination = state.Peer(*self.socket.getsockname())
ObjectTracker.__init__(self) ObjectTracker.__init__(self)
self.connecting = False self.connecting = False
self.connected = True self.connected = True
@ -60,48 +51,44 @@ class UDPSocket(BMProto):
except AttributeError: except AttributeError:
pass pass
def state_bm_command(self):
return BMProto.state_bm_command(self)
# disable most commands before doing research / testing # disable most commands before doing research / testing
# only addr (peer discovery), error and object are implemented # only addr (peer discovery), error and object are implemented
def bm_command_error(self):
return BMProto.bm_command_error(self)
def bm_command_getdata(self): def bm_command_getdata(self):
# return BMProto.bm_command_getdata(self)
return True return True
# return BMProto.bm_command_getdata(self)
def bm_command_inv(self): def bm_command_inv(self):
# return BMProto.bm_command_inv(self)
return True return True
# return BMProto.bm_command_inv(self)
def bm_command_object(self):
return BMProto.bm_command_object(self)
def bm_command_addr(self): def bm_command_addr(self):
# BMProto.bm_command_object(self)
addresses = self._decode_addr() addresses = self._decode_addr()
# only allow peer discovery from private IPs in order to avoid attacks from random IPs on the internet # only allow peer discovery from private IPs in order to avoid
# attacks from random IPs on the internet
if not self.local: if not self.local:
return True return True
remoteport = False remoteport = False
for i in addresses: for seenTime, stream, services, ip, port in addresses:
seenTime, stream, services, ip, port = i
decodedIP = protocol.checkIPAddress(str(ip)) decodedIP = protocol.checkIPAddress(str(ip))
if stream not in state.streamsInWhichIAmParticipating: if stream not in state.streamsInWhichIAmParticipating:
continue continue
if seenTime < time.time() - BMProto.maxTimeOffset or seenTime > time.time() + BMProto.maxTimeOffset: if (seenTime < time.time() - self.maxTimeOffset or
seenTime > time.time() + self.maxTimeOffset):
continue continue
if decodedIP is False: if decodedIP is False:
# if the address isn't local, interpret it as the hosts' own announcement # if the address isn't local, interpret it as
# the host's own announcement
remoteport = port remoteport = port
if remoteport is False: if remoteport is False:
return True return True
logger.debug("received peer discovery from %s:%i (port %i):", self.destination.host, self.destination.port, remoteport) logger.debug(
"received peer discovery from %s:%i (port %i):",
self.destination.host, self.destination.port, remoteport)
if self.local: if self.local:
state.discoveredPeers[state.Peer(self.destination.host, remoteport)] = time.time() state.discoveredPeers[
state.Peer(self.destination.host, remoteport)
] = time.time()
return True return True
def bm_command_portcheck(self): def bm_command_portcheck(self):
@ -126,51 +113,32 @@ class UDPSocket(BMProto):
return self.write_buf return self.write_buf
def readable(self): def readable(self):
return len(self.read_buf) < AdvancedDispatcher._buf_len return len(self.read_buf) < self._buf_len
def handle_read(self): def handle_read(self):
try: try:
(recdata, addr) = self.socket.recvfrom(AdvancedDispatcher._buf_len) (recdata, addr) = self.socket.recvfrom(self._buf_len)
except socket.error as e: except socket.error as e:
logger.error("socket error: %s", str(e)) logger.error("socket error: %s", e)
return return
self.destination = state.Peer(addr[0], addr[1]) self.destination = state.Peer(*addr)
encodedAddr = protocol.encodeHost(addr[0]) encodedAddr = protocol.encodeHost(addr[0])
if protocol.checkIPAddress(encodedAddr, True): if protocol.checkIPAddress(encodedAddr, True):
self.local = True self.local = True
else: else:
self.local = False self.local = False
# overwrite the old buffer to avoid mixing data and so that self.local works correctly # overwrite the old buffer to avoid mixing data and so that
# self.local works correctly
self.read_buf[0:] = recdata self.read_buf[0:] = recdata
self.bm_proto_reset() self.bm_proto_reset()
receiveDataQueue.put(self.listening) receiveDataQueue.put(self.listening)
def handle_write(self): def handle_write(self):
try: try:
retval = self.socket.sendto(self.write_buf, ('<broadcast>', UDPSocket.port)) retval = self.socket.sendto(
self.write_buf, ('<broadcast>', self.port))
except socket.error as e: except socket.error as e:
logger.error("socket error on sendato: %s", str(e)) logger.error("socket error on sendato: %s", e)
retval = 0 retval = 0
self.slice_write_buf(retval) self.slice_write_buf(retval)
if __name__ == "__main__":
# initial fill
for host in (("127.0.0.1", 8448),):
direct = BMConnection(host)
while len(asyncore.socket_map) > 0:
print "loop, state = %s" % (direct.state)
asyncore.loop(timeout=10, count=1)
continue
proxy = Socks5BMConnection(host)
while len(asyncore.socket_map) > 0:
# print "loop, state = %s" % (proxy.state)
asyncore.loop(timeout=10, count=1)
proxy = Socks4aBMConnection(host)
while len(asyncore.socket_map) > 0:
# print "loop, state = %s" % (proxy.state)
asyncore.loop(timeout=10, count=1)