diff --git a/src/httpserver.cpp b/src/httpserver.cpp index c15199d2350..76aefc5ab75 100644 --- a/src/httpserver.cpp +++ b/src/httpserver.cpp @@ -362,8 +362,7 @@ static void ThreadHTTP(struct event_base* base) LogDebug(BCLog::HTTP, "Exited http event loop\n"); } -/** Bind HTTP server to specified addresses */ -static bool HTTPBindAddresses(struct evhttp* http) +static std::vector> GetBindAddresses() { uint16_t http_port{static_cast(gArgs.GetIntArg("-rpcport", BaseParams().RPCPort()))}; std::vector> endpoints; @@ -388,33 +387,12 @@ static bool HTTPBindAddresses(struct evhttp* http) std::string host; if (!SplitHostPort(strRPCBind, port, host)) { LogError("%s\n", InvalidPortErrMsg("-rpcbind", strRPCBind).original); - return false; + return {}; // empty } endpoints.emplace_back(host, port); } } - - // Bind addresses - for (std::vector >::iterator i = endpoints.begin(); i != endpoints.end(); ++i) { - LogPrintf("Binding RPC on address %s port %i\n", i->first, i->second); - evhttp_bound_socket *bind_handle = evhttp_bind_socket_with_handle(http, i->first.empty() ? nullptr : i->first.c_str(), i->second); - if (bind_handle) { - const std::optional addr{LookupHost(i->first, false)}; - if (i->first.empty() || (addr.has_value() && addr->IsBindAny())) { - LogPrintf("WARNING: the RPC server is not safe to expose to untrusted networks such as the public internet\n"); - } - // Set the no-delay option (disable Nagle's algorithm) on the TCP socket. - evutil_socket_t fd = evhttp_bound_socket_get_fd(bind_handle); - int one = 1; - if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (sockopt_arg_type)&one, sizeof(one)) == SOCKET_ERROR) { - LogInfo("WARNING: Unable to set TCP_NODELAY on RPC server socket, continuing anyway\n"); - } - boundSockets.push_back(bind_handle); - } else { - LogPrintf("Binding RPC on address %s port %i failed.\n", i->first, i->second); - } - } - return !boundSockets.empty(); + return endpoints; } /** Simple wrapper to set thread name and run work queue */ @@ -446,6 +424,32 @@ static void libevent_log_cb(int severity, const char *msg) } namespace http_libevent { +/** Bind HTTP server to specified addresses */ +static bool HTTPBindAddresses(struct evhttp* http) +{ + std::vector> endpoints{GetBindAddresses()}; + for (std::vector >::iterator i = endpoints.begin(); i != endpoints.end(); ++i) { + LogPrintf("Binding RPC on address %s port %i\n", i->first, i->second); + evhttp_bound_socket *bind_handle = evhttp_bind_socket_with_handle(http, i->first.empty() ? nullptr : i->first.c_str(), i->second); + if (bind_handle) { + const std::optional addr{LookupHost(i->first, false)}; + if (i->first.empty() || (addr.has_value() && addr->IsBindAny())) { + LogPrintf("WARNING: the RPC server is not safe to expose to untrusted networks such as the public internet\n"); + } + // Set the no-delay option (disable Nagle's algorithm) on the TCP socket. + evutil_socket_t fd = evhttp_bound_socket_get_fd(bind_handle); + int one = 1; + if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (sockopt_arg_type)&one, sizeof(one)) == SOCKET_ERROR) { + LogInfo("WARNING: Unable to set TCP_NODELAY on RPC server socket, continuing anyway\n"); + } + boundSockets.push_back(bind_handle); + } else { + LogPrintf("Binding RPC on address %s port %i failed.\n", i->first, i->second); + } + } + return !boundSockets.empty(); +} + bool InitHTTPServer(const util::SignalInterrupt& interrupt) { if (!InitHTTPAllowList())