Skip to content

Commit 7d64ea4

Browse files
committed
net: only assume all local addresses if listening on any
If `-bind=` is provided then we would bind only to a particular address and should not add all the other addresses of the machine to the list of local addresses. Fixes bitcoin/bitcoin#20184 (case 4.)
1 parent 0cfc0cd commit 7d64ea4

File tree

4 files changed

+93
-2
lines changed

4 files changed

+93
-2
lines changed

src/init.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1668,8 +1668,6 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
16681668
LogPrintf("nBestHeight = %d\n", chain_active_height);
16691669
if (node.peerman) node.peerman->SetBestHeight(chain_active_height);
16701670

1671-
Discover();
1672-
16731671
// Map ports with UPnP or NAT-PMP.
16741672
StartMapPort(args.GetBoolArg("-upnp", DEFAULT_UPNP), gArgs.GetBoolArg("-natpmp", DEFAULT_NATPMP));
16751673

@@ -1762,6 +1760,12 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
17621760
StartTorControl(onion_service_target);
17631761
}
17641762

1763+
if (connOptions.bind_on_any) {
1764+
// Only add all IP addresses of the machine if we would be listening on
1765+
// any address - 0.0.0.0 (IPv4) and :: (IPv6).
1766+
Discover();
1767+
}
1768+
17651769
for (const auto& net : args.GetArgs("-whitelist")) {
17661770
NetWhitelistPermissions subnet;
17671771
bilingual_str error;

src/net.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,15 @@ enum class ConnectionType {
183183

184184
/** Convert ConnectionType enum to a string value */
185185
std::string ConnectionTypeAsString(ConnectionType conn_type);
186+
187+
/**
188+
* Look up IP addresses from all interfaces on the machine and add them to the
189+
* list of local addresses to self-advertise.
190+
* The loopback interface is skipped and only the first address from each
191+
* interface is used.
192+
*/
186193
void Discover();
194+
187195
uint16_t GetListenPort();
188196

189197
enum
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#!/usr/bin/env python3
2+
# Copyright (c) 2020-2021 The Bitcoin Core developers
3+
# Distributed under the MIT software license, see the accompanying
4+
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
5+
"""
6+
Test that -discover does not add all interfaces' addresses if we listen on only some of them
7+
"""
8+
9+
from test_framework.test_framework import BitcoinTestFramework, SkipTest
10+
from test_framework.util import assert_equal
11+
12+
# We need to bind to a routable address for this test to exercise the relevant code
13+
# and also must have another routable address on another interface which must not
14+
# be named "lo" or "lo0".
15+
# To set these routable addresses on the machine, use:
16+
# Linux:
17+
# ifconfig lo:0 1.1.1.1/32 up && ifconfig lo:1 2.2.2.2/32 up # to set up
18+
# ifconfig lo:0 down && ifconfig lo:1 down # to remove it, after the test
19+
# FreeBSD:
20+
# ifconfig em0 1.1.1.1/32 alias && ifconfig wlan0 2.2.2.2/32 alias # to set up
21+
# ifconfig em0 1.1.1.1 -alias && ifconfig wlan0 2.2.2.2 -alias # to remove it, after the test
22+
ADDR1 = '1.1.1.1'
23+
ADDR2 = '2.2.2.2'
24+
25+
BIND_PORT = 31001
26+
27+
class BindPortDiscoverTest(BitcoinTestFramework):
28+
def set_test_params(self):
29+
# Avoid any -bind= on the command line. Force the framework to avoid adding -bind=127.0.0.1.
30+
self.setup_clean_chain = True
31+
self.bind_to_localhost_only = False
32+
self.extra_args = [
33+
['-discover', f'-port={BIND_PORT}'], # bind on any
34+
['-discover', f'-bind={ADDR1}:{BIND_PORT}'],
35+
]
36+
self.num_nodes = len(self.extra_args)
37+
38+
def add_options(self, parser):
39+
parser.add_argument(
40+
"--ihave1111and2222", action='store_true', dest="ihave1111and2222",
41+
help=f"Run the test, assuming {ADDR1} and {ADDR2} are configured on the machine",
42+
default=False)
43+
44+
def skip_test_if_missing_module(self):
45+
if not self.options.ihave1111and2222:
46+
raise SkipTest(
47+
f"To run this test make sure that {ADDR1} and {ADDR2} (routable addresses) are "
48+
"assigned to the interfaces on this machine and rerun with --ihave1111and2222")
49+
50+
def run_test(self):
51+
self.log.info(
52+
"Test that if -bind= is not passed then all addresses are "
53+
"added to localaddresses")
54+
found_addr1 = False
55+
found_addr2 = False
56+
for local in self.nodes[0].getnetworkinfo()['localaddresses']:
57+
if local['address'] == ADDR1:
58+
found_addr1 = True
59+
assert_equal(local['port'], BIND_PORT)
60+
if local['address'] == ADDR2:
61+
found_addr2 = True
62+
assert_equal(local['port'], BIND_PORT)
63+
assert found_addr1
64+
assert found_addr2
65+
66+
self.log.info(
67+
"Test that if -bind= is passed then only that address is "
68+
"added to localaddresses")
69+
found_addr1 = False
70+
for local in self.nodes[1].getnetworkinfo()['localaddresses']:
71+
if local['address'] == ADDR1:
72+
found_addr1 = True
73+
assert_equal(local['port'], BIND_PORT)
74+
assert local['address'] != ADDR2
75+
assert found_addr1
76+
77+
if __name__ == '__main__':
78+
BindPortDiscoverTest().main()

test/functional/test_runner.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@
292292
'feature_loadblock.py',
293293
'p2p_dos_header_tree.py',
294294
'p2p_add_connections.py',
295+
'feature_bind_port_discover.py',
295296
'p2p_unrequested_blocks.py',
296297
'p2p_blockfilters.py',
297298
'p2p_message_capture.py',

0 commit comments

Comments
 (0)