Merge bitcoin/bitcoin#34346: test: use IP_PORTRANGE_HIGH on FreeBSD for dynamic port allocation
2845f10a2btest: extend FreeBSD ephemeral port range fix to P2P listeners (node)34bed0ed8ctest: use IP_PORTRANGE_HIGH on FreeBSD for dynamic port allocation (woltx) Pull request description: Reopening #34336. I’ve now tested it on FreeBSD and confirmed it works. On FreeBSD, the default ephemeral port range (10000-65535) overlaps with the test framework's static port range (11000-26000), possibly causing intermittent "address already in use" failures when tests use dynamic port allocation (`port=0`). This PR adds a helper that sets `IP_PORTRANGE_HIGH` via `setsockopt()` before binding, requesting ports from 49152-65535 instead, which avoids the overlap, as suggested in https://github.com/bitcoin/bitcoin/issues/34331#issuecomment-3767161843 by @maflcko . From FreeBSD's [sys/netinet/in.h](https://cgit.freebsd.org/src/tree/sys/netinet/in.h): ```c #define IP_PORTRANGE 19 #define IP_PORTRANGE_HIGH 1 #define IPPORT_EPHEMERALFIRST 10000 /* default range start */ #define IPPORT_HIFIRSTAUTO 49152 /* high range start */ ``` See also: FreeBSD https://man.freebsd.org/cgi/man.cgi?query=ip&sektion=4 man page. Fixes #34331 ACKs for top commit: vasild: ACK2845f10a2bhebasto: ACK2845f10a2b, I have reviewed the code and it looks OK. Tree-SHA512: ce501ce3e8a4023e07bad572df2b85d6829becf133813e4529aebba83e4eba59fa8b48e9d2197ebbb226adaf3054fad720775a787244d6b38c0078ee086102f4
This commit is contained in:
3 files changed
+46
-3
No files matched your search
@@ -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