Skip to content

Commit

Permalink
net: only add all addresses if listening on any
Browse files Browse the repository at this point in the history
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#20184 (case 4.)
  • Loading branch information
vasild committed Oct 22, 2020
1 parent 3fe113e commit 1ae0d40
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1918,8 +1918,6 @@ bool AppInitMain(const util::Ref& context, NodeContext& node, interfaces::BlockA
}
LogPrintf("nBestHeight = %d\n", chain_active_height);

Discover();

// Map ports with UPnP
if (args.GetBoolArg("-upnp", DEFAULT_UPNP)) {
StartMapPort();
Expand Down Expand Up @@ -1988,6 +1986,12 @@ bool AppInitMain(const util::Ref& context, NodeContext& node, interfaces::BlockA
connOptions.vWhiteBinds.push_back(whitebind);
}

if (connOptions.vBinds.empty() && connOptions.vWhiteBinds.empty()) {
// Only add all IP addresses on the machine if we would be listening on
// any address (0.0.0.0, aka *). See CConnman::InitBinds().
Discover();
}

for (const auto& net : args.GetArgs("-whitelist")) {
NetWhitelistPermissions subnet;
bilingual_str error;
Expand Down
8 changes: 8 additions & 0 deletions src/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,15 @@ class CConnman
friend struct CConnmanTest;
friend struct ConnmanTestMsg;
};

/**
* Lookup IP addresses from all interfaces on the machine and add them to the
* list of local addresses for self-advertise.
* The loopback interface is skipped and only the first address from each
* interface is used.
*/
void Discover();

void StartMapPort();
void InterruptMapPort();
void StopMapPort();
Expand Down
75 changes: 75 additions & 0 deletions test/functional/feature_bind_port_discover.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#!/usr/bin/env python3
# Copyright (c) 2020 The Bitcoin Core developers
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
"""
Test that -discover does not add all intefaces' addresses if we listen just on some
"""

from test_framework.test_framework import BitcoinTestFramework, SkipTest
from test_framework.util import assert_equal

# We need to bind to a routable address for this test to exercise the relevant code
# and also must have another routable address on another interface which must not
# be named "lo" or "lo0".
# To set a routable address on the machine use:
# Linux:
# ifconfig lo:0 1.1.1.1/32 up && ifconfig lo:1 2.2.2.2/32 up # to set up
# ifconfig lo:0 down && ifconfig lo:1 down # to remove it, after the test
# FreeBSD:
# ifconfig em0 1.1.1.1/32 alias && ifconfig wlan0 2.2.2.2/32 alias # to set up
# ifconfig em0 1.1.1.1 -alias && ifconfig wlan0 2.2.2.2 -alias # to remove it, after the test
ADDR1 = '1.1.1.1'
ADDR2 = '2.2.2.2'

BIND_PORT = 20001;

class BindPortDiscoverTest(BitcoinTestFramework):
def set_test_params(self):
# Avoid any -bind= on the command line. Force the framework to avoid adding -bind=127.0.0.1.
self.setup_clean_chain = True
self.bind_to_localhost_only = False
self.num_nodes = 2

def add_options(self, parser):
parser.add_argument(
"--ihave1111and2222", action='store_true', dest="ihave1111and2222",
help="Run the test, assuming {} and {} are configured on the machine".format(ADDR1, ADDR2),
default=False)

def setup_nodes(self):
if not self.options.ihave1111and2222:
raise SkipTest(
"To run this test make sure that {} and {} (a routable addresses) are assigned "
"to the interfaces on this machine and rerun with --ihave1111and2222".format(ADDR))
self.add_nodes(self.num_nodes, extra_args=[
['-discover', '-port={}'.format(BIND_PORT)], # bind on any
['-discover', '-bind={}:{}'.format(ADDR1, BIND_PORT)],
])
self.start_nodes()

def run_test(self):
# Check that if no -bind= is given, then all addresses are added to localaddresses.
found_addr1 = False
found_addr2 = False
for local in self.nodes[0].getnetworkinfo()['localaddresses']:
if local['address'] == ADDR1:
found_addr1 = True
assert_equal(local['port'], BIND_PORT)
if local['address'] == ADDR2:
found_addr2 = True
assert_equal(local['port'], BIND_PORT)
assert(found_addr1)
assert(found_addr2)

# Check that if -bind= is given, just that address is added to localaddresses.
found_addr1 = False
for local in self.nodes[1].getnetworkinfo()['localaddresses']:
if local['address'] == ADDR1:
found_addr1 = True
assert_equal(local['port'], BIND_PORT)
assert(local['address'] != ADDR2)
assert(found_addr1)

if __name__ == '__main__':
BindPortDiscoverTest().main()
1 change: 1 addition & 0 deletions test/functional/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@
'feature_filelock.py',
'feature_loadblock.py',
'p2p_dos_header_tree.py',
'feature_bind_port_discover.py',
'p2p_unrequested_blocks.py',
'p2p_blockfilters.py',
'feature_includeconf.py',
Expand Down

0 comments on commit 1ae0d40

Please sign in to comment.