2019-09-12 11:54:18 +02:00
|
|
|
"""
|
2019-12-19 12:24:53 +01:00
|
|
|
UDP protocol handler
|
2019-09-12 11:54:18 +02:00
|
|
|
"""
|
2019-08-06 13:04:33 +02:00
|
|
|
import logging
|
2017-05-27 19:09:21 +02:00
|
|
|
import socket
|
2019-12-19 12:24:53 +01:00
|
|
|
import time
|
2017-05-27 19:09:21 +02:00
|
|
|
|
|
|
|
import protocol
|
2019-12-19 12:24:53 +01:00
|
|
|
import state
|
2018-10-19 10:50:46 +02:00
|
|
|
from bmproto import BMProto
|
2019-11-03 16:11:52 +01:00
|
|
|
from node import Peer
|
2018-10-19 10:50:46 +02:00
|
|
|
from objectracker import ObjectTracker
|
|
|
|
from queues import receiveDataQueue
|
|
|
|
|
2019-08-06 13:04:33 +02:00
|
|
|
logger = logging.getLogger('default')
|
2017-05-27 19:09:21 +02:00
|
|
|
|
2019-08-06 13:04:33 +02:00
|
|
|
|
|
|
|
class UDPSocket(BMProto): # pylint: disable=too-many-instance-attributes
|
2019-09-12 11:54:18 +02:00
|
|
|
"""Bitmessage protocol over UDP (class)"""
|
2017-05-27 19:09:21 +02:00
|
|
|
port = 8444
|
|
|
|
announceInterval = 60
|
|
|
|
|
2017-08-09 17:34:47 +02:00
|
|
|
def __init__(self, host=None, sock=None, announcing=False):
|
2019-08-06 13:04:33 +02:00
|
|
|
# pylint: disable=bad-super-call
|
|
|
|
super(BMProto, self).__init__(sock=sock)
|
2017-05-27 19:09:21 +02:00
|
|
|
self.verackReceived = True
|
|
|
|
self.verackSent = True
|
2019-09-12 11:54:18 +02:00
|
|
|
# .. todo:: sort out streams
|
2017-05-27 19:09:21 +02:00
|
|
|
self.streams = [1]
|
|
|
|
self.fullyEstablished = True
|
|
|
|
self.connectedAt = 0
|
|
|
|
self.skipUntil = 0
|
|
|
|
if sock is None:
|
|
|
|
if host is None:
|
|
|
|
host = ''
|
2018-10-19 10:50:46 +02:00
|
|
|
self.create_socket(
|
|
|
|
socket.AF_INET6 if ":" in host else socket.AF_INET,
|
|
|
|
socket.SOCK_DGRAM
|
|
|
|
)
|
2017-08-05 10:14:15 +02:00
|
|
|
self.set_socket_reuse()
|
2018-10-19 10:50:46 +02:00
|
|
|
logger.info("Binding UDP socket to %s:%i", host, self.port)
|
|
|
|
self.socket.bind((host, self.port))
|
2017-05-27 19:09:21 +02:00
|
|
|
else:
|
|
|
|
self.socket = sock
|
2017-08-05 10:14:15 +02:00
|
|
|
self.set_socket_reuse()
|
2019-11-03 16:11:52 +01:00
|
|
|
self.listening = Peer(*self.socket.getsockname())
|
|
|
|
self.destination = Peer(*self.socket.getsockname())
|
2017-05-27 19:09:21 +02:00
|
|
|
ObjectTracker.__init__(self)
|
|
|
|
self.connecting = False
|
|
|
|
self.connected = True
|
2017-08-09 17:34:47 +02:00
|
|
|
self.announcing = announcing
|
2017-05-31 10:17:36 +02:00
|
|
|
self.set_state("bm_header", expectBytes=protocol.Header.size)
|
2017-05-27 19:09:21 +02:00
|
|
|
|
2017-08-05 10:14:15 +02:00
|
|
|
def set_socket_reuse(self):
|
2019-09-12 11:54:18 +02:00
|
|
|
"""Set socket reuse option"""
|
2017-08-05 10:14:15 +02:00
|
|
|
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
|
|
|
|
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
|
|
|
try:
|
|
|
|
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
|
2017-05-27 19:09:21 +02:00
|
|
|
# disable most commands before doing research / testing
|
|
|
|
# only addr (peer discovery), error and object are implemented
|
|
|
|
|
|
|
|
def bm_command_getdata(self):
|
2018-10-19 10:50:46 +02:00
|
|
|
# return BMProto.bm_command_getdata(self)
|
2017-05-27 19:09:21 +02:00
|
|
|
return True
|
|
|
|
|
|
|
|
def bm_command_inv(self):
|
2018-10-19 10:50:46 +02:00
|
|
|
# return BMProto.bm_command_inv(self)
|
2017-05-27 19:09:21 +02:00
|
|
|
return True
|
|
|
|
|
|
|
|
def bm_command_addr(self):
|
|
|
|
addresses = self._decode_addr()
|
2018-10-19 10:50:46 +02:00
|
|
|
# only allow peer discovery from private IPs in order to avoid
|
|
|
|
# attacks from random IPs on the internet
|
2017-05-27 19:09:21 +02:00
|
|
|
if not self.local:
|
2017-07-06 19:45:36 +02:00
|
|
|
return True
|
2017-05-27 19:09:21 +02:00
|
|
|
remoteport = False
|
2019-12-19 12:24:53 +01:00
|
|
|
for seenTime, stream, _, ip, port in addresses:
|
2017-10-20 01:07:30 +02:00
|
|
|
decodedIP = protocol.checkIPAddress(str(ip))
|
2017-05-27 19:09:21 +02:00
|
|
|
if stream not in state.streamsInWhichIAmParticipating:
|
|
|
|
continue
|
2019-08-06 13:04:33 +02:00
|
|
|
if (seenTime < time.time() - self.maxTimeOffset
|
|
|
|
or seenTime > time.time() + self.maxTimeOffset):
|
2017-05-27 19:09:21 +02:00
|
|
|
continue
|
|
|
|
if decodedIP is False:
|
2018-10-19 10:50:46 +02:00
|
|
|
# if the address isn't local, interpret it as
|
|
|
|
# the host's own announcement
|
2017-05-27 19:09:21 +02:00
|
|
|
remoteport = port
|
|
|
|
if remoteport is False:
|
2017-07-06 19:45:36 +02:00
|
|
|
return True
|
2018-10-19 10:50:46 +02:00
|
|
|
logger.debug(
|
|
|
|
"received peer discovery from %s:%i (port %i):",
|
|
|
|
self.destination.host, self.destination.port, remoteport)
|
2017-05-27 19:09:21 +02:00
|
|
|
if self.local:
|
2019-12-19 12:24:53 +01:00
|
|
|
state.discoveredPeers[Peer(self.destination.host, remoteport)] = \
|
|
|
|
time.time()
|
2017-05-27 19:09:21 +02:00
|
|
|
return True
|
|
|
|
|
|
|
|
def bm_command_portcheck(self):
|
|
|
|
return True
|
|
|
|
|
|
|
|
def bm_command_ping(self):
|
|
|
|
return True
|
|
|
|
|
|
|
|
def bm_command_pong(self):
|
|
|
|
return True
|
|
|
|
|
|
|
|
def bm_command_verack(self):
|
|
|
|
return True
|
|
|
|
|
|
|
|
def bm_command_version(self):
|
|
|
|
return True
|
|
|
|
|
2017-05-27 21:52:56 +02:00
|
|
|
def handle_connect(self):
|
2017-05-27 19:09:21 +02:00
|
|
|
return
|
|
|
|
|
|
|
|
def writable(self):
|
2017-07-06 19:45:36 +02:00
|
|
|
return self.write_buf
|
2017-05-27 19:09:21 +02:00
|
|
|
|
|
|
|
def readable(self):
|
2018-10-19 10:50:46 +02:00
|
|
|
return len(self.read_buf) < self._buf_len
|
2017-05-27 19:09:21 +02:00
|
|
|
|
|
|
|
def handle_read(self):
|
|
|
|
try:
|
2018-10-19 10:50:46 +02:00
|
|
|
(recdata, addr) = self.socket.recvfrom(self._buf_len)
|
2017-05-27 19:09:21 +02:00
|
|
|
except socket.error as e:
|
2018-10-19 10:50:46 +02:00
|
|
|
logger.error("socket error: %s", e)
|
2017-05-27 19:09:21 +02:00
|
|
|
return
|
|
|
|
|
2019-11-03 16:11:52 +01:00
|
|
|
self.destination = Peer(*addr)
|
2017-05-27 20:43:27 +02:00
|
|
|
encodedAddr = protocol.encodeHost(addr[0])
|
2019-09-10 11:21:02 +02:00
|
|
|
self.local = bool(protocol.checkIPAddress(encodedAddr, True))
|
2018-10-19 10:50:46 +02:00
|
|
|
# overwrite the old buffer to avoid mixing data and so that
|
|
|
|
# self.local works correctly
|
2017-10-16 08:07:32 +02:00
|
|
|
self.read_buf[0:] = recdata
|
2017-05-27 20:43:27 +02:00
|
|
|
self.bm_proto_reset()
|
2017-08-06 23:05:54 +02:00
|
|
|
receiveDataQueue.put(self.listening)
|
2017-05-27 19:09:21 +02:00
|
|
|
|
|
|
|
def handle_write(self):
|
|
|
|
try:
|
2018-10-19 10:50:46 +02:00
|
|
|
retval = self.socket.sendto(
|
|
|
|
self.write_buf, ('<broadcast>', self.port))
|
2017-05-27 19:09:21 +02:00
|
|
|
except socket.error as e:
|
2019-12-14 11:53:51 +01:00
|
|
|
logger.error("socket error on sendto: %s", e)
|
2020-09-11 13:12:58 +02:00
|
|
|
retval = len(self.write_buf)
|
2017-07-06 19:45:36 +02:00
|
|
|
self.slice_write_buf(retval)
|