2019-11-27 06:47:04 +01:00
|
|
|
"""
|
|
|
|
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):
|
2020-01-06 12:44:47 +01:00
|
|
|
peerList = [peerList]
|
2019-11-27 06:47:04 +01:00
|
|
|
if not peerList:
|
|
|
|
return b''
|
|
|
|
retval = b''
|
|
|
|
for i in range(0, len(peerList), MAX_ADDR_COUNT):
|
2020-01-06 12:44:47 +01:00
|
|
|
payload = addresses.encodeVarint(len(peerList[i:i + MAX_ADDR_COUNT]))
|
2019-11-27 06:47:04 +01:00
|
|
|
for stream, peer, timestamp in peerList[i:i + MAX_ADDR_COUNT]:
|
2020-01-06 12:44:47 +01:00
|
|
|
# 64-bit time
|
|
|
|
payload += struct.pack('>Q', timestamp)
|
2019-11-27 06:47:04 +01:00
|
|
|
payload += struct.pack('>I', stream)
|
2020-01-06 12:44:47 +01:00
|
|
|
# service bit flags offered by this node
|
|
|
|
payload += struct.pack('>q', 1)
|
2019-11-27 06:47:04 +01:00
|
|
|
payload += encodeHost(peer.host)
|
2020-01-06 12:44:47 +01:00
|
|
|
# remote port
|
|
|
|
payload += struct.pack('>H', peer.port)
|
2019-11-27 06:47:04 +01:00
|
|
|
retval += CreatePacket('addr', payload)
|
|
|
|
return retval
|