+1, i think it makes sense to fail-fast
went ahead and extended your patch a little by making InitHTTPServer() fail fast (as well) when a required endpoint fails to bind. this removes the need for bind_success, required_failure, and the failure-handling helper. we just handle the failure where it happens and return immediately.
i think this will make the control flow simpler and easier to follow, with less state to keep track of. plus, it also avoids attempting further binds when we already know startup cannot succeed
<details><summary><i>extended</i> patch</summary>
diff --git a/src/httpserver.cpp b/src/httpserver.cpp
index 10f1cb85f8..88f87e9ac9 100644
--- a/src/httpserver.cpp
+++ b/src/httpserver.cpp
@@ -207,8 +207,7 @@ static void RejectRequest(std::unique_ptr<HTTPRequest> hreq)
}
struct BindAddress {
- std::string address;
- uint16_t port;
+ CService addr;
bool required{true};
};
@@ -232,8 +231,10 @@ static std::vector<BindAddress> GetBindAddresses()
// We allow the default IPv6 bind to fail, but IPv4 is required even
// with the default port as bitcoin-cli will default to handing RPC
// credentials in plaintext to whatever process is running on that port.
- endpoints.push_back({.address = "::1", .port = http_port, .required = false});
- endpoints.push_back({.address = "127.0.0.1", .port = http_port, .required = true});
+ // Numeric literals always parse, InitHTTPAllowList relies on the same.
+ // Never fall back to CService{}, which is [::]:0 and binds every interface.
+ endpoints.push_back({.addr = Lookup("127.0.0.1", http_port, /*fAllowLookup=*/false).value(), .required = true});
+ endpoints.push_back({.addr = Lookup("::1", http_port, /*fAllowLookup=*/false).value(), .required = false});
if (!gArgs.GetArgs("-rpcallowip").empty()) {
LogWarning("Option -rpcallowip was specified without -rpcbind; this doesn't usually make sense");
}
@@ -248,7 +249,15 @@ static std::vector<BindAddress> GetBindAddresses()
LogError("%s\n", InvalidPortErrMsg("-rpcbind", strRPCBind).original);
return {}; // empty
}
- endpoints.emplace_back(host, port);
+ // Don't allow hostname lookups when binding to local interfaces.
+ const std::optional<CService> addr{Lookup(host, port, /*fAllowLookup=*/false)};
+ if (!addr) {
+ LogError("Could not bind RPC on address \"%s\" port %i: Address lookup failed "
+ "(only numeric addresses are allowed).",
+ host, port);
+ return {}; // empty
+ }
+ endpoints.push_back({.addr = *addr, .required = true});
}
}
return endpoints;
@@ -1350,58 +1359,38 @@ bool InitHTTPServer()
// Bind HTTP server to specified addresses
std::vector<BindAddress> endpoints{GetBindAddresses()};
+ if (endpoints.empty()) {
+ LogError("Unable to bind any endpoint for RPC server");
+ return false;
+ }
+
std::set<CService> endpoints_seen;
- bool bind_success{false};
- bool required_failure{false};
- auto on_failure = [&required_failure](bool required, std::string_view msg) {
- if (required) {
- LogError("%s", msg);
- required_failure = true;
- } else {
- LogWarning("%s", msg);
+ for (const auto& [addr, required] : endpoints) {
+ // Addresses are numeric, so collisions detected by endpoints_seen are
+ // probably just repeated instances - skip without logging why.
+ if (!endpoints_seen.insert(addr).second) continue;
+ LogInfo("Binding RPC on address %s", addr.ToStringAddrPort());
+ if (addr.IsBindAny()) {
+ LogWarning("The RPC server is not safe to expose to untrusted networks such as the public internet");
}
- };
- for (const auto& [address_string, port, required] : endpoints) {
- // Don't allow hostname lookups when binding to local interfaces. This
- // means numeric collisions on available interfaces detected by endpoints_seen
- // are probably just repeated instances - skip without logging why.
- const std::optional<CService> addr{Lookup(address_string, port, /*fAllowLookup=*/false)};
- if (addr) {
- if (!endpoints_seen.insert(*addr).second) continue;
- LogInfo("Binding RPC on address %s port %i", address_string, port);
- if (addr->IsBindAny()) {
- LogWarning("The RPC server is not safe to expose to untrusted networks such as the public internet");
- }
- auto result{g_http_server->BindAndStartListening(addr.value())};
- if (!result) {
- on_failure(required,
- strprintf("Binding RPC on %s address %s failed: %s",
+ auto result{g_http_server->BindAndStartListening(addr)};
+ if (!result) {
+ const auto msg{strprintf("Binding RPC on %s address %s failed: %s",
required ? "required" : "optional",
- addr->ToStringAddrPort(), result.error()));
- } else {
- bind_success = true;
+ addr.ToStringAddrPort(), result.error())};
+ if (required) {
+ LogError("%s", msg);
+ g_http_server->StopListening();
+ if (BindDefaultLoopbackAddresses()) {
+ LogError("Unable to bind default required IPv4 endpoint. Ensure the "
+ "port is free and retry, or specify endpoints via -rpcbind.");
+ } else {
+ LogError("Unable to bind all specified endpoints for RPC server.");
+ }
+ return false;
}
- } else {
- on_failure(required,
- strprintf("Could not bind RPC on %s address \"%s\" port %i: Address lookup failed "
- "(only numeric addresses are allowed).",
- required ? "required" : "optional",
- address_string, port));
- }
- }
-
- if (!bind_success) {
- LogError("Unable to bind any endpoint for RPC server");
- return false;
- } else if (required_failure) {
- g_http_server->StopListening();
- if (BindDefaultLoopbackAddresses()) {
- LogError("Unable to bind default required IPv4 endpoint. Ensure the "
- "port is free and retry, or specify endpoints via -rpcbind.");
- } else {
- LogError("Unable to bind all specified endpoints for RPC server.");
+ LogWarning("%s", msg);
}
- return false;
}
LogDebug(BCLog::HTTP, "Initialized HTTP server");
</details>
WDYT ?