mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-02-09 02:59:31 +08:00
test: extend FreeBSD ephemeral port range fix to P2P listeners
The previous commit added set_ephemeral_port_range() to avoid port conflicts on FreeBSD by requesting ports from the high ephemeral range (49152-65535) instead of the default range which overlaps with the test framework's static port range. That fix was applied to the SOCKS5 server but not to P2P listeners created via NetworkThread.create_listen_server(). This commit extends the fix to cover P2P listeners as well. When port=0 is requested (dynamic allocation), we now: 1. Manually create a socket with the appropriate address family 2. Call set_ephemeral_port_range() to configure the port range 3. Bind and listen on the socket 4. Pass the pre-configured socket to asyncio's create_server() This ensures that dynamically allocated ports for P2P listeners also come from the high range on FreeBSD, avoiding conflicts with the test framework's static port assignments. Co-Authored-By: Vasil Dimov <vd@FreeBSD.org>
This commit is contained in:
@@ -22,9 +22,11 @@ P2PTxInvStore: A p2p interface class that inherits from P2PDataStore, and keeps
|
||||
|
||||
import asyncio
|
||||
from collections import defaultdict
|
||||
import ipaddress
|
||||
from io import BytesIO
|
||||
import logging
|
||||
import platform
|
||||
import socket
|
||||
import struct
|
||||
import sys
|
||||
import threading
|
||||
@@ -76,6 +78,9 @@ from test_framework.messages import (
|
||||
MAGIC_BYTES,
|
||||
sha256,
|
||||
)
|
||||
from test_framework.netutil import (
|
||||
set_ephemeral_port_range,
|
||||
)
|
||||
from test_framework.util import (
|
||||
assert_not_equal,
|
||||
MAX_NODES,
|
||||
@@ -793,8 +798,22 @@ class NetworkThread(threading.Thread):
|
||||
# connections, we can accomplish this by providing different
|
||||
# `proto` functions
|
||||
|
||||
listener = await cls.network_event_loop.create_server(peer_protocol, addr, port)
|
||||
port = listener.sockets[0].getsockname()[1]
|
||||
if port == 0:
|
||||
# Manually create the socket in order to set the range to be
|
||||
# used for the port before the bind() call.
|
||||
if ipaddress.ip_address(addr).version == 4:
|
||||
address_family = socket.AF_INET
|
||||
else:
|
||||
address_family = socket.AF_INET6
|
||||
s = socket.socket(address_family)
|
||||
set_ephemeral_port_range(s)
|
||||
s.bind((addr, 0))
|
||||
s.listen()
|
||||
listener = await cls.network_event_loop.create_server(peer_protocol, sock=s)
|
||||
port = listener.sockets[0].getsockname()[1]
|
||||
else:
|
||||
listener = await cls.network_event_loop.create_server(peer_protocol, addr, port)
|
||||
|
||||
logger.debug("Listening server on %s:%d should be started" % (addr, port))
|
||||
cls.listeners[(addr, port)] = listener
|
||||
|
||||
|
||||
Reference in New Issue
Block a user