diff --git a/src/class_objectProcessor.py b/src/class_objectProcessor.py index 246dfdea..2e1f5891 100644 --- a/src/class_objectProcessor.py +++ b/src/class_objectProcessor.py @@ -1033,7 +1033,7 @@ class objectProcessor(threading.Thread): magic, command, payloadLength, checksum = protocol.Header.unpack( ackData[:protocol.Header.size]) - if magic != 0xE9BEB4D9: + if magic != protocol.magic: logger.info('Ackdata magic bytes were wrong. Not sending ackData.') return False payload = ackData[protocol.Header.size:] diff --git a/src/network/bmproto.py b/src/network/bmproto.py index c4f8462a..e23dfe8d 100644 --- a/src/network/bmproto.py +++ b/src/network/bmproto.py @@ -30,10 +30,6 @@ from network.bmobject import ( from network.dandelion import Dandelion from network.proxy import ProxyError -from constants import ( - ADDRESS_ALIVE, MAX_MESSAGE_SIZE, MAX_OBJECT_COUNT, - MAX_OBJECT_PAYLOAD_SIZE, MAX_TIME_OFFSET -) from node import Node, Peer from objectracker import ObjectTracker, missingObjects @@ -90,7 +86,7 @@ class BMProto(AdvancedDispatcher, ObjectTracker): self.magic, self.command, self.payloadLength, self.checksum = \ protocol.Header.unpack(self.read_buf[:protocol.Header.size]) self.command = self.command.rstrip('\x00') - if self.magic != 0xE9BEB4D9: + if self.magic != protocol.magic: # skip 1 byte in order to sync self.set_state("bm_header", length=1) self.bm_proto_reset() @@ -99,7 +95,7 @@ class BMProto(AdvancedDispatcher, ObjectTracker): self.close_reason = "Bad magic" self.set_state("close") return False - if self.payloadLength > MAX_MESSAGE_SIZE: + if self.payloadLength > protocol.MAX_MESSAGE_SIZE: self.invalid = True self.set_state( "bm_command", @@ -351,7 +347,7 @@ class BMProto(AdvancedDispatcher, ObjectTracker): """ 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() @@ -387,7 +383,7 @@ class BMProto(AdvancedDispatcher, ObjectTracker): self.payload, self.payloadOffset) payload_len = len(self.payload) - self.payloadOffset - if payload_len > MAX_OBJECT_PAYLOAD_SIZE: + if payload_len > protocol.MAX_OBJECT_PAYLOAD_SIZE: logger.info( 'The payload length of this object is too large' ' (%d bytes). Ignoring it.', payload_len) @@ -460,7 +456,7 @@ class BMProto(AdvancedDispatcher, ObjectTracker): decodedIP = protocol.checkIPAddress(ip) if ( decodedIP and time.time() - seenTime > 0 - and seenTime > time.time() - ADDRESS_ALIVE + and seenTime > time.time() - protocol.ADDRESS_ALIVE and port > 0 ): peer = Peer(decodedIP, port) @@ -573,7 +569,7 @@ class BMProto(AdvancedDispatcher, ObjectTracker): 'Closing connection to old protocol version %s, node: %s', self.remoteProtocolVersion, self.destination) return False - if self.timeOffset > MAX_TIME_OFFSET: + if self.timeOffset > protocol.MAX_TIME_OFFSET: self.append_write_buf(protocol.assembleErrorMessage( errorText="Your time is too far in the future" " compared to mine. Closing connection.", fatal=2)) @@ -583,7 +579,7 @@ class BMProto(AdvancedDispatcher, ObjectTracker): self.destination, self.timeOffset) BMProto.timeOffsetWrongCount += 1 return False - elif self.timeOffset < -MAX_TIME_OFFSET: + elif self.timeOffset < -protocol.MAX_TIME_OFFSET: self.append_write_buf(protocol.assembleErrorMessage( errorText="Your time is too far in the past compared to mine." " Closing connection.", fatal=2)) diff --git a/src/network/constants.py b/src/network/constants.py deleted file mode 100644 index a4a2fcc5..00000000 --- a/src/network/constants.py +++ /dev/null @@ -1,15 +0,0 @@ -""" -Network protocol constants -""" - - -#: address is online if online less than this many seconds ago -ADDRESS_ALIVE = 10800 -#: ~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 -MAX_OBJECT_PAYLOAD_SIZE = 2**18 -#: protocol specification says max 50000 objects in one inv command -MAX_OBJECT_COUNT = 50000 -#: maximum time offset -MAX_TIME_OFFSET = 3600 diff --git a/src/network/tcp.py b/src/network/tcp.py index ae473281..0bfde3bb 100644 --- a/src/network/tcp.py +++ b/src/network/tcp.py @@ -24,7 +24,6 @@ from tr import _translate import asyncore_pollchoose as asyncore import connectionpool import knownnodes -from constants import MAX_OBJECT_COUNT from network.advanceddispatcher import AdvancedDispatcher from network.bmproto import BMProto from network.dandelion import Dandelion @@ -247,7 +246,7 @@ class TCPConnection(BMProto, TLSDispatcher): # 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 diff --git a/src/network/udp.py b/src/network/udp.py index 3f999332..1a9891ec 100644 --- a/src/network/udp.py +++ b/src/network/udp.py @@ -5,13 +5,15 @@ import logging import socket import time +# magic imports! import protocol import state +from queues import receiveDataQueue + from bmproto import BMProto -from constants import MAX_TIME_OFFSET from node import Peer from objectracker import ObjectTracker -from queues import receiveDataQueue + logger = logging.getLogger('default') @@ -81,8 +83,8 @@ class UDPSocket(BMProto): # pylint: disable=too-many-instance-attributes decodedIP = protocol.checkIPAddress(str(ip)) if stream not in state.streamsInWhichIAmParticipating: continue - if (seenTime < time.time() - MAX_TIME_OFFSET - or seenTime > time.time() + MAX_TIME_OFFSET): + if (seenTime < time.time() - protocol.MAX_TIME_OFFSET + or seenTime > time.time() + protocol.MAX_TIME_OFFSET): continue if decodedIP is False: # if the address isn't local, interpret it as diff --git a/src/protocol.py b/src/protocol.py index caecb183..f5aa2750 100644 --- a/src/protocol.py +++ b/src/protocol.py @@ -25,9 +25,20 @@ from helper_sql import sqlExecute from network.node import Peer from version import softwareVersion - +# Network constants +magic = 0xE9BEB4D9 #: protocol specification says max 1000 addresses in one addr command MAX_ADDR_COUNT = 1000 +#: address is online if online less than this many seconds ago +ADDRESS_ALIVE = 10800 +#: ~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 +MAX_OBJECT_PAYLOAD_SIZE = 2**18 +#: protocol specification says max 50000 objects in one inv command +MAX_OBJECT_COUNT = 50000 +#: maximum time offset +MAX_TIME_OFFSET = 3600 # Service flags #: This is a normal network node @@ -300,7 +311,7 @@ def CreatePacket(command, payload=b''): checksum = hashlib.sha512(payload).digest()[0:4] b = bytearray(Header.size + payload_length) - Header.pack_into(b, 0, 0xE9BEB4D9, command, payload_length, checksum) + Header.pack_into(b, 0, magic, command, payload_length, checksum) b[Header.size:] = payload return bytes(b) diff --git a/src/tests/samples.py b/src/tests/samples.py index 7a9870a9..9237d19d 100644 --- a/src/tests/samples.py +++ b/src/tests/samples.py @@ -3,8 +3,6 @@ from binascii import unhexlify -magic = 0xE9BEB4D9 - # 500 identical peers: # 1626611891, 1, 1, 127.0.0.1, 8444 sample_addr_data = unhexlify( diff --git a/src/tests/test_packets.py b/src/tests/test_packets.py index 115f1edc..f030912a 100644 --- a/src/tests/test_packets.py +++ b/src/tests/test_packets.py @@ -5,7 +5,7 @@ from struct import pack from pybitmessage import addresses, protocol -from .samples import magic, sample_addr_data +from .samples import sample_addr_data from .test_protocol import TestSocketInet @@ -45,7 +45,7 @@ class TestSerialize(TestSocketInet): def test_packet(self): """Check the packet created by protocol.CreatePacket()""" - head = unhexlify(b'%x' % magic) + head = unhexlify(b'%x' % protocol.magic) self.assertEqual( protocol.CreatePacket(b'ping')[:len(head)], head)