Skip to content

Commit 385f5a4

Browse files
mzumsandevasild
andcommitted
p2p: Don't query DNS seeds when both IPv4 and IPv6 are unreachable
This happens, for example, if the user specified -onlynet=onion or -onlynet=i2p. DNS seeds only resolve to IPv4 / IPv6 addresses, making their answers useless to us, since we don't want to make connections to these. If, within the DNS seed thread, we'd instead do fallback AddrFetch connections to one of the clearnet addresses the DNS seed resolves to, we might get usable addresses from other networks if lucky, but would be violating our -onlynet user preference in doing so. Therefore, in this case it is better to rely on fixed seeds for networks we want to connect to. Co-authored-by: Vasil Dimov <[email protected]>
1 parent 91f0a7f commit 385f5a4

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

src/init.cpp

+17
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,16 @@ void InitParameterInteraction(ArgsManager& args)
719719
if (args.SoftSetBoolArg("-whitelistrelay", true))
720720
LogPrintf("%s: parameter interaction: -whitelistforcerelay=1 -> setting -whitelistrelay=1\n", __func__);
721721
}
722+
if (args.IsArgSet("-onlynet")) {
723+
const auto onlynets = args.GetArgs("-onlynet");
724+
bool clearnet_reachable = std::any_of(onlynets.begin(), onlynets.end(), [](const auto& net) {
725+
const auto n = ParseNetwork(net);
726+
return n == NET_IPV4 || n == NET_IPV6;
727+
});
728+
if (!clearnet_reachable && args.SoftSetBoolArg("-dnsseed", false)) {
729+
LogPrintf("%s: parameter interaction: -onlynet excludes IPv4 and IPv6 -> setting -dnsseed=0\n", __func__);
730+
}
731+
}
722732
}
723733

724734
/**
@@ -1322,6 +1332,13 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
13221332
// 2.1. -onlynet is not given or
13231333
// 2.2. -onlynet=cjdns is given
13241334

1335+
// Requesting DNS seeds entails connecting to IPv4/IPv6, which -onlynet options may prohibit:
1336+
// If -dnsseed=1 is explicitly specified, abort. If it's left unspecified by the user, we skip
1337+
// the DNS seeds by adjusting -dnsseed in InitParameterInteraction.
1338+
if (args.GetBoolArg("-dnsseed") == true && !IsReachable(NET_IPV4) && !IsReachable(NET_IPV6)) {
1339+
return InitError(strprintf(_("Incompatible options: -dnsseed=1 was explicitly specified, but -onlynet forbids connections to IPv4/IPv6")));
1340+
};
1341+
13251342
// Check for host lookup allowed before parsing any network related parameters
13261343
fNameLookup = args.GetBoolArg("-dns", DEFAULT_NAME_LOOKUP);
13271344

src/net.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1630,7 +1630,7 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
16301630
LOCK2(m_addr_fetches_mutex, m_added_nodes_mutex);
16311631
if (m_addr_fetches.empty() && m_added_nodes.empty()) {
16321632
add_fixed_seeds_now = true;
1633-
LogPrintf("Adding fixed seeds as -dnsseed=0, -addnode is not provided and all -seednode(s) attempted\n");
1633+
LogPrintf("Adding fixed seeds as -dnsseed=0 (or IPv4/IPv6 connections are disabled via -onlynet), -addnode is not provided and all -seednode(s) attempted\n");
16341634
}
16351635
}
16361636

test/functional/feature_config_args.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -186,11 +186,12 @@ def test_seed_peers(self):
186186
with self.nodes[0].assert_debug_log(expected_msgs=[
187187
"Loaded 0 addresses from peers.dat",
188188
"DNS seeding disabled",
189-
"Adding fixed seeds as -dnsseed=0, -addnode is not provided and all -seednode(s) attempted\n",
189+
"Adding fixed seeds as -dnsseed=0 (or IPv4/IPv6 connections are disabled via -onlynet), -addnode is not provided and all -seednode(s) attempted\n",
190190
]):
191191
self.start_node(0, extra_args=['-dnsseed=0', '-fixedseeds=1'])
192192
assert time.time() - start < 60
193193
self.stop_node(0)
194+
self.nodes[0].assert_start_raises_init_error(['-dnsseed=1', '-onlynet=i2p', '-i2psam=127.0.0.1:7656'], "Error: Incompatible options: -dnsseed=1 was explicitly specified, but -onlynet forbids connections to IPv4/IPv6")
194195

195196
# No peers.dat exists and dns seeds are disabled.
196197
# We expect the node will not add fixed seeds when explicitly disabled.

0 commit comments

Comments
 (0)