Skip to content

Commit 37095c7

Browse files
committed
Merge bitcoin#25678: p2p: skip querying dns seeds if -onlynet disables IPv4 and IPv6
385f5a4 p2p: Don't query DNS seeds when both IPv4 and IPv6 are unreachable (Martin Zumsande) 91f0a7f p2p: add only reachable addresses to addrman (Martin Zumsande) Pull request description: Currently, `-onlynet` does not work well in connection with initial peer discovery, because DNS seeds only resolve to IPv6 and IPv4 adresses: With `-onlynet=i2p`, we would load clearnet addresses from DNS seeds into addrman, be content our addrman isn't empty so we don't try to query hardcoded seeds (although these exist for i2p!), and never attempt to make an automatic outbound connection. With `-onlynet=onion` and `-proxy` set, we wouldn't load addresses via DNS, but will make AddrFetch connections (through a tor exit node) to a random clearnet peer the DNS seed resolves to (see bitcoin#6808 (comment)), thus breaching the `-onlynet` preference of the user - this has been reported in the two issues listed below. This PR proposes two changes: 1.) Don't load addresses that are unreachable (so that we wouldn't connect to them) into addrman. This is already the case for addresses received via p2p addr messages, this PR implements the same for addresses received from DNS seeds and fixed seeds. This means that in the case of `-onlynet=onion`, we wouldn't load fixed seed IPv4 addresses into addrman, only the onion ones. 2.) Skip trying the DNS seeds if neither IPv4 nor IPv6 are reachable and move directly to adding the hardcoded seeds from networks we can connect to. This is done by soft-setting `-dnsseed` to 0 in this case, unless `-dnsseed=1` was explicitly specified, in which case we abort with an `InitError`. Fixes bitcoin#6808 Fixes bitcoin#12344 ACKs for top commit: naumenkogs: utACK 385f5a4 vasild: ACK 385f5a4 Tree-SHA512: 33a8c29faccb2d9b937b017dba4ef72c10e05e458ccf258f1aed3893bcc37c2e984ec8de998d2ecfa54282abbf44a132e97d98bbcc24a0dcf1871566016a9b91
2 parents fc44d17 + 385f5a4 commit 37095c7

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

src/init.cpp

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

728738
/**
@@ -1281,6 +1291,13 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
12811291
// 2.1. -onlynet is not given or
12821292
// 2.2. -onlynet=cjdns is given
12831293

1294+
// Requesting DNS seeds entails connecting to IPv4/IPv6, which -onlynet options may prohibit:
1295+
// If -dnsseed=1 is explicitly specified, abort. If it's left unspecified by the user, we skip
1296+
// the DNS seeds by adjusting -dnsseed in InitParameterInteraction.
1297+
if (args.GetBoolArg("-dnsseed") == true && !IsReachable(NET_IPV4) && !IsReachable(NET_IPV6)) {
1298+
return InitError(strprintf(_("Incompatible options: -dnsseed=1 was explicitly specified, but -onlynet forbids connections to IPv4/IPv6")));
1299+
};
1300+
12841301
// Check for host lookup allowed before parsing any network related parameters
12851302
fNameLookup = args.GetBoolArg("-dns", DEFAULT_NAME_LOOKUP);
12861303

src/net.cpp

+7-2
Original file line numberDiff line numberDiff line change
@@ -1642,15 +1642,20 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
16421642
LOCK2(m_addr_fetches_mutex, m_added_nodes_mutex);
16431643
if (m_addr_fetches.empty() && m_added_nodes.empty()) {
16441644
add_fixed_seeds_now = true;
1645-
LogPrintf("Adding fixed seeds as -dnsseed=0, -addnode is not provided and all -seednode(s) attempted\n");
1645+
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");
16461646
}
16471647
}
16481648

16491649
if (add_fixed_seeds_now) {
1650+
std::vector<CAddress> seed_addrs{ConvertSeeds(Params().FixedSeeds())};
1651+
seed_addrs.erase(std::remove_if(seed_addrs.begin(), seed_addrs.end(),
1652+
[](const CAddress& addr) { return !IsReachable(addr); }),
1653+
seed_addrs.end());
16501654
CNetAddr local;
16511655
local.SetInternal("fixedseeds");
1652-
addrman.Add(ConvertSeeds(Params().FixedSeeds()), local);
1656+
addrman.Add(seed_addrs, local);
16531657
add_fixed_seeds = false;
1658+
LogPrintf("Added %d fixed seeds from reachable networks.\n", seed_addrs.size());
16541659
}
16551660
}
16561661

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)