diff --git a/src/network/addrthread.py b/src/network/addrthread.py index 8b46750f..dfd0738f 100644 --- a/src/network/addrthread.py +++ b/src/network/addrthread.py @@ -6,7 +6,8 @@ from six.moves import queue import state from helper_random import randomshuffle -from network.assemble import assemble_addr +from protocol import assembleAddrMessage + from network.connectionpool import BMConnectionPool from queues import addrQueue from threads import StoppableThread @@ -41,7 +42,7 @@ class AddrThread(StoppableThread): continue filtered.append((stream, peer, seen)) if filtered: - i.append_write_buf(assemble_addr(filtered)) + i.append_write_buf(assembleAddrMessage(filtered)) addrQueue.iterate() for i in range(len(chunk)): diff --git a/src/network/announcethread.py b/src/network/announcethread.py index 6e18e661..cc82051a 100644 --- a/src/network/announcethread.py +++ b/src/network/announcethread.py @@ -5,7 +5,8 @@ import time import state from bmconfigparser import config -from network.assemble import assemble_addr +from protocol import assembleAddrMessage + from network.connectionpool import BMConnectionPool from node import Peer from threads import StoppableThread @@ -40,4 +41,4 @@ class AnnounceThread(StoppableThread): config.safeGetInt( 'bitmessagesettings', 'port')), time.time()) - connection.append_write_buf(assemble_addr([addr])) + connection.append_write_buf(assembleAddrMessage([addr])) diff --git a/src/network/assemble.py b/src/network/assemble.py deleted file mode 100644 index 32fad3e4..00000000 --- a/src/network/assemble.py +++ /dev/null @@ -1,31 +0,0 @@ -""" -Create bitmessage protocol command packets -""" -import struct - -import addresses -from network.constants import MAX_ADDR_COUNT -from network.node import Peer -from protocol import CreatePacket, encodeHost - - -def assemble_addr(peerList): - """Create address command""" - if isinstance(peerList, Peer): - peerList = [peerList] - if not peerList: - return b'' - retval = b'' - for i in range(0, len(peerList), MAX_ADDR_COUNT): - payload = addresses.encodeVarint(len(peerList[i:i + MAX_ADDR_COUNT])) - for stream, peer, timestamp in peerList[i:i + MAX_ADDR_COUNT]: - # 64-bit time - payload += struct.pack('>Q', timestamp) - payload += struct.pack('>I', stream) - # service bit flags offered by this node - payload += struct.pack('>q', 1) - payload += encodeHost(peer.host) - # remote port - payload += struct.pack('>H', peer.port) - retval += CreatePacket('addr', payload) - return retval diff --git a/src/network/constants.py b/src/network/constants.py index f8f4120f..a4a2fcc5 100644 --- a/src/network/constants.py +++ b/src/network/constants.py @@ -5,8 +5,6 @@ Network protocol constants #: address is online if online less than this many seconds ago ADDRESS_ALIVE = 10800 -#: protocol specification says max 1000 addresses in one addr command -MAX_ADDR_COUNT = 1000 #: ~1.6 MB which is the maximum possible size of an inv message. MAX_MESSAGE_SIZE = 1600100 #: 2**18 = 256kB is the maximum size of an object payload diff --git a/src/network/tcp.py b/src/network/tcp.py index b18080c9..c68adf46 100644 --- a/src/network/tcp.py +++ b/src/network/tcp.py @@ -2,7 +2,7 @@ TCP protocol handler """ # pylint: disable=too-many-ancestors -import l10n + import logging import math import random @@ -10,17 +10,20 @@ import socket import time import addresses -import asyncore_pollchoose as asyncore -import connectionpool import helper_random -import knownnodes +import l10n import protocol import state from bmconfigparser import config from helper_random import randomBytes from inventory import Inventory +from queues import invQueue, receiveDataQueue, UISignalQueue +from tr import _translate + +import asyncore_pollchoose as asyncore +import connectionpool +import knownnodes from network.advanceddispatcher import AdvancedDispatcher -from network.assemble import assemble_addr from network.bmproto import BMProto from network.constants import MAX_OBJECT_COUNT from network.dandelion import Dandelion @@ -29,8 +32,7 @@ from network.socks4a import Socks4aConnection from network.socks5 import Socks5Connection from network.tls import TLSDispatcher from node import Peer -from queues import invQueue, receiveDataQueue, UISignalQueue -from tr import _translate + logger = logging.getLogger('default') @@ -205,7 +207,7 @@ class TCPConnection(BMProto, TLSDispatcher): for peer, params in addrs[substream]: templist.append((substream, peer, params["lastseen"])) if templist: - self.append_write_buf(assemble_addr(templist)) + self.append_write_buf(protocol.assembleAddrMessage(templist)) def sendBigInv(self): """ diff --git a/src/protocol.py b/src/protocol.py index e49b45b0..1b62b78f 100644 --- a/src/protocol.py +++ b/src/protocol.py @@ -22,8 +22,13 @@ from bmconfigparser import config from debug import logger from fallback import RIPEMD160Hash from helper_sql import sqlExecute +from network.node import Peer from version import softwareVersion + +#: protocol specification says max 1000 addresses in one addr command +MAX_ADDR_COUNT = 1000 + # Service flags #: This is a normal network node NODE_NETWORK = 1 @@ -300,6 +305,28 @@ def CreatePacket(command, payload=b''): return bytes(b) +def assembleAddrMessage(peerList): + """Create address command""" + if isinstance(peerList, Peer): + peerList = [peerList] + if not peerList: + return b'' + retval = b'' + for i in range(0, len(peerList), MAX_ADDR_COUNT): + payload = encodeVarint(len(peerList[i:i + MAX_ADDR_COUNT])) + for stream, peer, timestamp in peerList[i:i + MAX_ADDR_COUNT]: + # 64-bit time + payload += pack('>Q', timestamp) + payload += pack('>I', stream) + # service bit flags offered by this node + payload += pack('>q', 1) + payload += encodeHost(peer.host) + # remote port + payload += pack('>H', peer.port) + retval += CreatePacket('addr', payload) + return retval + + def assembleVersionMessage( remoteHost, remotePort, participatingStreams, server=False, nodeid=None ):