Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/network/addrthread.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
"""
from six.moves import queue


# magic imports!
import state
from helper_random import randomshuffle
from network.assemble import assemble_addr
from protocol import assembleAddrMessage
from queues import addrQueue # FIXME: init with queue
from network.connectionpool import BMConnectionPool
from queues import addrQueue

from threads import StoppableThread


Expand Down Expand Up @@ -41,7 +42,7 @@ def run(self):
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)):
Expand Down
6 changes: 4 additions & 2 deletions src/network/announcethread.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
"""
import time

# magic imports!
import state
from bmconfigparser import BMConfigParser
from network.assemble import assemble_addr
from protocol import assembleAddrMessage
from network.connectionpool import BMConnectionPool

from node import Peer
from threads import StoppableThread

Expand Down Expand Up @@ -40,4 +42,4 @@ def announceSelf():
BMConfigParser().safeGetInt(
'bitmessagesettings', 'port')),
time.time())
connection.append_write_buf(assemble_addr([addr]))
connection.append_write_buf(assembleAddrMessage([addr]))
4 changes: 2 additions & 2 deletions src/network/bmproto.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
BMObjectInvalidError, BMObjectUnwantedStreamError
)
from network.constants import (
ADDRESS_ALIVE, MAX_MESSAGE_SIZE, MAX_OBJECT_COUNT,
ADDRESS_ALIVE, MAX_MESSAGE_SIZE,
MAX_OBJECT_PAYLOAD_SIZE, MAX_TIME_OFFSET
)
from network.dandelion import Dandelion
Expand Down Expand Up @@ -350,7 +350,7 @@ def _command_inv(self, dandelion=False):
"""
items = self.decode_payload_content("l32s")

if len(items) > MAX_OBJECT_COUNT:
if len(items) > protocol.MAX_OBJECT_COUNT:
logger.error(
'Too many items in %sinv message!', 'd' if dandelion else '')
raise BMProtoExcessiveDataError()
Expand Down
2 changes: 0 additions & 2 deletions src/network/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@

#: 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
Expand Down
6 changes: 2 additions & 4 deletions src/network/tcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@
from helper_random import randomBytes
from inventory import Inventory
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
from network.objectracker import ObjectTracker
from network.socks4a import Socks4aConnection
Expand Down Expand Up @@ -205,7 +203,7 @@ def sendAddr(self):
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):
"""
Expand Down Expand Up @@ -244,7 +242,7 @@ def sendChunk():
# Remove -1 below when sufficient time has passed for users to
# upgrade to versions of PyBitmessage that accept inv with 50,000
# items
if objectCount >= MAX_OBJECT_COUNT - 1:
if objectCount >= protocol.MAX_OBJECT_COUNT - 1:
sendChunk()
payload = b''
objectCount = 0
Expand Down
26 changes: 26 additions & 0 deletions src/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from debug import logger
from fallback import RIPEMD160Hash
from helper_sql import sqlExecute
from network.node import Peer
from version import softwareVersion

# Service flags
Expand Down Expand Up @@ -55,6 +56,9 @@
OBJECT_I2P = 0x493250
OBJECT_ADDR = 0x61646472

#: protocol specification says max 1000 addresses in one addr command
MAX_ADDR_COUNT = 1000

eightBytesOfRandomDataUsedToDetectConnectionsToSelf = pack(
'>Q', random.randrange(1, 18446744073709551615))

Expand Down Expand Up @@ -295,6 +299,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
):
Expand Down