Skip to content

Commit 45750f6

Browse files
committed
Merge bitcoin/bitcoin#22729: Make it possible to disable Tor binds and abort startup on bind failure
bca346a net: require P2P binds to succeed (Vasil Dimov) af55253 net: report an error if unable to bind on the Tor listening addr:port (Vasil Dimov) 9a7e5f4 net: don't extra bind for Tor if binds are restricted (Vasil Dimov) Pull request description: Make it possible to disable the Tor binding on `127.0.0.1:8334` and stop startup if any P2P bind fails instead of "if all P2P binds fail". Fixes bitcoin/bitcoin#22726 Fixes bitcoin/bitcoin#22727 ACKs for top commit: achow101: ACK bca346a cbergqvist: ACK bca346a pinheadmz: ACK bca346a Tree-SHA512: fabef89a957191eea4f3e3b6109d2b8389f27ecc74440a920b0c10f31fff00a85bcfd1eb3c91826c7169c618f4de8a8d0a260e2caf40fd854f07ea9a980d8603
2 parents 16b4f75 + bca346a commit 45750f6

File tree

6 files changed

+59
-21
lines changed

6 files changed

+59
-21
lines changed

src/init.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1881,6 +1881,8 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
18811881
CService onion_service_target;
18821882
if (!connOptions.onion_binds.empty()) {
18831883
onion_service_target = connOptions.onion_binds.front();
1884+
} else if (!connOptions.vBinds.empty()) {
1885+
onion_service_target = connOptions.vBinds.front();
18841886
} else {
18851887
onion_service_target = DefaultOnionServiceTarget();
18861888
connOptions.onion_binds.push_back(onion_service_target);

src/net.cpp

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3202,24 +3202,36 @@ bool CConnman::Bind(const CService& addr_, unsigned int flags, NetPermissionFlag
32023202

32033203
bool CConnman::InitBinds(const Options& options)
32043204
{
3205-
bool fBound = false;
32063205
for (const auto& addrBind : options.vBinds) {
3207-
fBound |= Bind(addrBind, BF_REPORT_ERROR, NetPermissionFlags::None);
3206+
if (!Bind(addrBind, BF_REPORT_ERROR, NetPermissionFlags::None)) {
3207+
return false;
3208+
}
32083209
}
32093210
for (const auto& addrBind : options.vWhiteBinds) {
3210-
fBound |= Bind(addrBind.m_service, BF_REPORT_ERROR, addrBind.m_flags);
3211+
if (!Bind(addrBind.m_service, BF_REPORT_ERROR, addrBind.m_flags)) {
3212+
return false;
3213+
}
32113214
}
32123215
for (const auto& addr_bind : options.onion_binds) {
3213-
fBound |= Bind(addr_bind, BF_DONT_ADVERTISE, NetPermissionFlags::None);
3216+
if (!Bind(addr_bind, BF_REPORT_ERROR | BF_DONT_ADVERTISE, NetPermissionFlags::None)) {
3217+
return false;
3218+
}
32143219
}
32153220
if (options.bind_on_any) {
3221+
// Don't consider errors to bind on IPv6 "::" fatal because the host OS
3222+
// may not have IPv6 support and the user did not explicitly ask us to
3223+
// bind on that.
3224+
const CService ipv6_any{in6_addr(IN6ADDR_ANY_INIT), GetListenPort()}; // ::
3225+
Bind(ipv6_any, BF_NONE, NetPermissionFlags::None);
3226+
32163227
struct in_addr inaddr_any;
32173228
inaddr_any.s_addr = htonl(INADDR_ANY);
3218-
struct in6_addr inaddr6_any = IN6ADDR_ANY_INIT;
3219-
fBound |= Bind(CService(inaddr6_any, GetListenPort()), BF_NONE, NetPermissionFlags::None);
3220-
fBound |= Bind(CService(inaddr_any, GetListenPort()), !fBound ? BF_REPORT_ERROR : BF_NONE, NetPermissionFlags::None);
3229+
const CService ipv4_any{inaddr_any, GetListenPort()}; // 0.0.0.0
3230+
if (!Bind(ipv4_any, BF_REPORT_ERROR, NetPermissionFlags::None)) {
3231+
return false;
3232+
}
32213233
}
3222-
return fBound;
3234+
return true;
32233235
}
32243236

32253237
bool CConnman::Start(CScheduler& scheduler, const Options& connOptions)

test/functional/feature_bind_extra.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def set_test_params(self):
2727
# Avoid any -bind= on the command line. Force the framework to avoid
2828
# adding -bind=127.0.0.1.
2929
self.bind_to_localhost_only = False
30-
self.num_nodes = 2
30+
self.num_nodes = 3
3131

3232
def skip_test_if_missing_module(self):
3333
# Due to OS-specific network stats queries, we only run on Linux.
@@ -60,14 +60,21 @@ def setup_network(self):
6060
)
6161
port += 2
6262

63+
# Node2, no -bind=...=onion, thus no extra port for Tor target.
64+
self.expected.append(
65+
[
66+
[f"-bind=127.0.0.1:{port}"],
67+
[(loopback_ipv4, port)]
68+
],
69+
)
70+
port += 1
71+
6372
self.extra_args = list(map(lambda e: e[0], self.expected))
64-
self.add_nodes(self.num_nodes, self.extra_args)
65-
# Don't start the nodes, as some of them would collide trying to bind on the same port.
73+
self.setup_nodes()
6674

6775
def run_test(self):
68-
for i in range(len(self.expected)):
69-
self.log.info(f"Starting node {i} with {self.expected[i][0]}")
70-
self.start_node(i)
76+
for i, (args, expected_services) in enumerate(self.expected):
77+
self.log.info(f"Checking listening ports of node {i} with {args}")
7178
pid = self.nodes[i].process.pid
7279
binds = set(get_bind_addrs(pid))
7380
# Remove IPv6 addresses because on some CI environments "::1" is not configured
@@ -78,9 +85,7 @@ def run_test(self):
7885
binds = set(filter(lambda e: len(e[0]) != ipv6_addr_len_bytes, binds))
7986
# Remove RPC ports. They are not relevant for this test.
8087
binds = set(filter(lambda e: e[1] != rpc_port(i), binds))
81-
assert_equal(binds, set(self.expected[i][1]))
82-
self.stop_node(i)
83-
self.log.info(f"Stopped node {i}")
88+
assert_equal(binds, set(expected_services))
8489

8590
if __name__ == '__main__':
8691
BindExtraTest().main()

test/functional/test-shell.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ can be called after the TestShell is shut down.
169169

170170
| Test parameter key | Default Value | Description |
171171
|---|---|---|
172-
| `bind_to_localhost_only` | `True` | Binds bitcoind RPC services to `127.0.0.1` if set to `True`.|
172+
| `bind_to_localhost_only` | `True` | Binds bitcoind P2P services to `127.0.0.1` if set to `True`.|
173173
| `cachedir` | `"/path/to/bitcoin/test/cache"` | Sets the bitcoind datadir directory. |
174174
| `chain` | `"regtest"` | Sets the chain-type for the underlying test bitcoind processes. |
175175
| `configfile` | `"/path/to/bitcoin/test/config.ini"` | Sets the location of the test framework config file. |

test/functional/test_framework/test_node.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
rpc_url,
4040
wait_until_helper_internal,
4141
p2p_port,
42+
tor_port,
4243
)
4344

4445
BITCOIND_PROC_WAIT_TIMEOUT = 60
@@ -88,8 +89,11 @@ def __init__(self, i, datadir_path, *, chain, rpchost, timewait, timeout_factor,
8889
self.coverage_dir = coverage_dir
8990
self.cwd = cwd
9091
self.descriptors = descriptors
92+
self.has_explicit_bind = False
9193
if extra_conf is not None:
9294
append_config(self.datadir_path, extra_conf)
95+
# Remember if there is bind=... in the config file.
96+
self.has_explicit_bind = any(e.startswith("bind=") for e in extra_conf)
9397
# Most callers will just need to add extra args to the standard list below.
9498
# For those callers that need more flexibility, they can just set the args property directly.
9599
# Note that common args are set in the config file (see initialize_datadir)
@@ -210,6 +214,17 @@ def start(self, extra_args=None, *, cwd=None, stdout=None, stderr=None, env=None
210214
if extra_args is None:
211215
extra_args = self.extra_args
212216

217+
# If listening and no -bind is given, then bitcoind would bind P2P ports on
218+
# 0.0.0.0:P and 127.0.0.1:18445 (for incoming Tor connections), where P is
219+
# a unique port chosen by the test framework and configured as port=P in
220+
# bitcoin.conf. To avoid collisions on 127.0.0.1:18445, change it to
221+
# 127.0.0.1:tor_port().
222+
will_listen = all(e != "-nolisten" and e != "-listen=0" for e in extra_args)
223+
has_explicit_bind = self.has_explicit_bind or any(e.startswith("-bind=") for e in extra_args)
224+
if will_listen and not has_explicit_bind:
225+
extra_args.append(f"-bind=0.0.0.0:{p2p_port(self.index)}")
226+
extra_args.append(f"-bind=127.0.0.1:{tor_port(self.index)}=onion")
227+
213228
self.use_v2transport = "-v2transport=1" in extra_args or (self.default_to_v2 and "-v2transport=0" not in extra_args)
214229

215230
# Add a new stdout and stderr file each time bitcoind is started

test/functional/test_framework/util.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -316,9 +316,9 @@ def sha256sum_file(filename):
316316

317317
# The maximum number of nodes a single test can spawn
318318
MAX_NODES = 12
319-
# Don't assign rpc or p2p ports lower than this
319+
# Don't assign p2p, rpc or tor ports lower than this
320320
PORT_MIN = int(os.getenv('TEST_RUNNER_PORT_MIN', default=11000))
321-
# The number of ports to "reserve" for p2p and rpc, each
321+
# The number of ports to "reserve" for p2p, rpc and tor, each
322322
PORT_RANGE = 5000
323323

324324

@@ -358,7 +358,11 @@ def p2p_port(n):
358358

359359

360360
def rpc_port(n):
361-
return PORT_MIN + PORT_RANGE + n + (MAX_NODES * PortSeed.n) % (PORT_RANGE - 1 - MAX_NODES)
361+
return p2p_port(n) + PORT_RANGE
362+
363+
364+
def tor_port(n):
365+
return p2p_port(n) + PORT_RANGE * 2
362366

363367

364368
def rpc_url(datadir, i, chain, rpchost):

0 commit comments

Comments
 (0)