This commit is contained in:
Vasil Dimov 2024-04-29 04:35:17 +02:00 committed by GitHub
commit 2244d9d6ce
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 46 additions and 15 deletions

View File

@ -1882,6 +1882,8 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
CService onion_service_target;
if (!connOptions.onion_binds.empty()) {
onion_service_target = connOptions.onion_binds.front();
} else if (!connOptions.vBinds.empty()) {
onion_service_target = connOptions.vBinds.front();
} else {
onion_service_target = DefaultOnionServiceTarget();
connOptions.onion_binds.push_back(onion_service_target);

View File

@ -3166,24 +3166,36 @@ bool CConnman::Bind(const CService& addr_, unsigned int flags, NetPermissionFlag
bool CConnman::InitBinds(const Options& options)
{
bool fBound = false;
for (const auto& addrBind : options.vBinds) {
fBound |= Bind(addrBind, BF_REPORT_ERROR, NetPermissionFlags::None);
if (!Bind(addrBind, BF_REPORT_ERROR, NetPermissionFlags::None)) {
return false;
}
}
for (const auto& addrBind : options.vWhiteBinds) {
fBound |= Bind(addrBind.m_service, BF_REPORT_ERROR, addrBind.m_flags);
if (!Bind(addrBind.m_service, BF_REPORT_ERROR, addrBind.m_flags)) {
return false;
}
}
for (const auto& addr_bind : options.onion_binds) {
fBound |= Bind(addr_bind, BF_DONT_ADVERTISE, NetPermissionFlags::None);
if (!Bind(addr_bind, BF_REPORT_ERROR | BF_DONT_ADVERTISE, NetPermissionFlags::None)) {
return false;
}
}
if (options.bind_on_any) {
// Don't consider errors to bind on IPv6 "::" fatal because the host OS
// may not have IPv6 support and the user did not explicitly ask us to
// bind on that.
const CService ipv6_any{in6_addr(IN6ADDR_ANY_INIT), GetListenPort()}; // ::
Bind(ipv6_any, BF_NONE, NetPermissionFlags::None);
struct in_addr inaddr_any;
inaddr_any.s_addr = htonl(INADDR_ANY);
struct in6_addr inaddr6_any = IN6ADDR_ANY_INIT;
fBound |= Bind(CService(inaddr6_any, GetListenPort()), BF_NONE, NetPermissionFlags::None);
fBound |= Bind(CService(inaddr_any, GetListenPort()), !fBound ? BF_REPORT_ERROR : BF_NONE, NetPermissionFlags::None);
const CService ipv4_any{inaddr_any, GetListenPort()}; // 0.0.0.0
if (!Bind(ipv4_any, BF_REPORT_ERROR, NetPermissionFlags::None)) {
return false;
}
}
return fBound;
return true;
}
bool CConnman::Start(CScheduler& scheduler, const Options& connOptions)

View File

@ -21,19 +21,23 @@ from test_framework.util import (
)
# From chainparamsbase.cpp:CreateBaseChainParams().
REGTEST_TOR_TARGET_PORT = 18445
class BindExtraTest(BitcoinTestFramework):
def set_test_params(self):
self.setup_clean_chain = True
# Avoid any -bind= on the command line. Force the framework to avoid
# adding -bind=127.0.0.1.
self.bind_to_localhost_only = False
self.num_nodes = 2
self.num_nodes = 4
def skip_test_if_missing_module(self):
# Due to OS-specific network stats queries, we only run on Linux.
self.skip_if_platform_not_linux()
def setup_network(self):
any_ipv4 = addr_to_hex('0.0.0.0')
loopback_ipv4 = addr_to_hex("127.0.0.1")
# Start custom ports by reusing unused p2p ports
@ -60,14 +64,29 @@ class BindExtraTest(BitcoinTestFramework):
)
port += 2
# Node2, no -bind, expected to bind on any + tor target.
self.expected.append(
[
["-listen=1"],
[(any_ipv4, p2p_port(2)), (loopback_ipv4, REGTEST_TOR_TARGET_PORT)]
]
)
# Node3, no -bind=...=onion, thus no extra port for Tor target.
self.expected.append(
[
['-bind=127.0.0.1:{}'.format(port)],
[(loopback_ipv4, port)]
],
)
port += 1
self.extra_args = list(map(lambda e: e[0], self.expected))
self.add_nodes(self.num_nodes, self.extra_args)
# Don't start the nodes, as some of them would collide trying to bind on the same port.
self.setup_nodes()
def run_test(self):
for i in range(len(self.expected)):
self.log.info(f"Starting node {i} with {self.expected[i][0]}")
self.start_node(i)
self.log.info(f"Checking listening ports of node {i} with {self.expected[i][0]}")
pid = self.nodes[i].process.pid
binds = set(get_bind_addrs(pid))
# Remove IPv6 addresses because on some CI environments "::1" is not configured
@ -79,8 +98,6 @@ class BindExtraTest(BitcoinTestFramework):
# Remove RPC ports. They are not relevant for this test.
binds = set(filter(lambda e: e[1] != rpc_port(i), binds))
assert_equal(binds, set(self.expected[i][1]))
self.stop_node(i)
self.log.info(f"Stopped node {i}")
if __name__ == '__main__':
BindExtraTest().main()