Skip to content

Commit c1de112

Browse files
committed
net: execute Discover() when bind=0.0.0.0 or :: is set
1 parent 0bb8a01 commit c1de112

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

src/init.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1902,6 +1902,10 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
19021902

19031903
const uint16_t default_bind_port_onion = default_bind_port + 1;
19041904

1905+
// If the user did not specify -bind= or -whitebind= then we bind
1906+
// on any address - 0.0.0.0 (IPv4) and :: (IPv6).
1907+
connOptions.bind_on_any = args.GetArgs("-bind").empty() && args.GetArgs("-whitebind").empty();
1908+
19051909
const auto BadPortWarning = [](const char* prefix, uint16_t port) {
19061910
return strprintf(_("%s request to listen on port %u. This port is considered \"bad\" and "
19071911
"thus it is unlikely that any peer will connect to it. See "
@@ -1916,6 +1920,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
19161920
if (index == std::string::npos) {
19171921
bind_addr = Lookup(bind_arg, default_bind_port, /*fAllowLookup=*/false);
19181922
if (bind_addr.has_value()) {
1923+
connOptions.bind_on_any |= bind_addr.value().IsBindAny();
19191924
connOptions.vBinds.push_back(bind_addr.value());
19201925
if (IsBadPort(bind_addr.value().GetPort())) {
19211926
InitWarning(BadPortWarning("-bind", bind_addr.value().GetPort()));
@@ -1928,6 +1933,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
19281933
const std::string truncated_bind_arg = bind_arg.substr(0, index);
19291934
bind_addr = Lookup(truncated_bind_arg, default_bind_port_onion, false);
19301935
if (bind_addr.has_value()) {
1936+
connOptions.bind_on_any |= bind_addr.value().IsBindAny();
19311937
connOptions.onion_binds.push_back(bind_addr.value());
19321938
continue;
19331939
}
@@ -1943,10 +1949,6 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
19431949
connOptions.vWhiteBinds.push_back(whitebind);
19441950
}
19451951

1946-
// If the user did not specify -bind= or -whitebind= then we bind
1947-
// on any address - 0.0.0.0 (IPv4) and :: (IPv6).
1948-
connOptions.bind_on_any = args.GetArgs("-bind").empty() && args.GetArgs("-whitebind").empty();
1949-
19501952
// Emit a warning if a bad port is given to -port= but only if -bind and -whitebind are not
19511953
// given, because if they are, then -port= is ignored.
19521954
if (connOptions.bind_on_any && args.IsArgSet("-port")) {
@@ -1963,7 +1965,6 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
19631965
onion_service_target = connOptions.vBinds.front();
19641966
} else {
19651967
onion_service_target = DefaultOnionServiceTarget(default_bind_port_onion);
1966-
connOptions.onion_binds.push_back(onion_service_target);
19671968
}
19681969

19691970
if (args.GetBoolArg("-listenonion", DEFAULT_LISTEN_ONION)) {

src/net.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,10 @@ uint16_t GetListenPort()
146146
// If -bind= is provided with ":port" part, use that (first one if multiple are provided).
147147
for (const std::string& bind_arg : gArgs.GetArgs("-bind")) {
148148
constexpr uint16_t dummy_port = 0;
149+
// maybe bind_arg has "=onion"
150+
const std::string truncated_bind_arg = bind_arg.substr(0, bind_arg.rfind('='));
149151

150-
const std::optional<CService> bind_addr{Lookup(bind_arg, dummy_port, /*fAllowLookup=*/false)};
152+
const std::optional<CService> bind_addr{Lookup(truncated_bind_arg, dummy_port, /*fAllowLookup=*/false)};
151153
if (bind_addr.has_value() && bind_addr->GetPort() != dummy_port) return bind_addr->GetPort();
152154
}
153155

@@ -3273,7 +3275,7 @@ bool CConnman::InitBinds(const Options& options)
32733275
return false;
32743276
}
32753277
}
3276-
if (options.bind_on_any) {
3278+
if (options.bind_on_any && options.vBinds.empty() && options.onion_binds.empty()) {
32773279
// Don't consider errors to bind on IPv6 "::" fatal because the host OS
32783280
// may not have IPv6 support and the user did not explicitly ask us to
32793281
// bind on that.
@@ -3286,6 +3288,14 @@ bool CConnman::InitBinds(const Options& options)
32863288
if (!Bind(ipv4_any, BF_REPORT_ERROR, NetPermissionFlags::None)) {
32873289
return false;
32883290
}
3291+
3292+
struct in_addr onion_service_target;
3293+
onion_service_target.s_addr = htonl(INADDR_LOOPBACK);
3294+
const uint16_t onion_port = GetListenPort() + 1;
3295+
const CService onion_addr = {onion_service_target, onion_port}; // 127.0.0.1
3296+
if (!Bind(onion_addr, BF_REPORT_ERROR | BF_DONT_ADVERTISE, NetPermissionFlags::None)) {
3297+
return false;
3298+
}
32893299
}
32903300
return true;
32913301
}

0 commit comments

Comments
 (0)