From 914dc01222c54206096cf5c066c834b27e61add0 Mon Sep 17 00:00:00 2001 From: Giel van Schijndel Date: Wed, 10 Aug 2011 13:53:13 +0200 Subject: [PATCH 1/9] Use asynchronous I/O to handle RPC requests This allows more flexibility in the RPC code, e.g. making it easier to handle multiple simultaneous connections later on. Currently asynchronous I/O is only used to listen for and accept incoming connections. Asynchronous reading/writing is more involved. Signed-off-by: Giel van Schijndel --- src/bitcoinrpc.cpp | 120 +++++++++++++++++++++++++++++++-------------- 1 file changed, 82 insertions(+), 38 deletions(-) diff --git a/src/bitcoinrpc.cpp b/src/bitcoinrpc.cpp index 8f4fb93a5a..efcb39d70d 100644 --- a/src/bitcoinrpc.cpp +++ b/src/bitcoinrpc.cpp @@ -14,6 +14,7 @@ #undef printf #include +#include #include #include #include @@ -21,6 +22,7 @@ #include #include #include +#include typedef boost::asio::ssl::stream SSLStream; #define printf OutputDebugStringF @@ -2641,6 +2643,75 @@ void ThreadRPCServer(void* parg) printf("ThreadRPCServer exited\n"); } +// Forward declaration required for RPCListen +static void RPCAcceptHandler(boost::shared_ptr acceptor, + ssl::context& context, + bool fUseSSL, + AcceptedConnection* conn, + const boost::system::error_code& error); + +/** + * Sets up I/O resources to accept and handle a new connection. + */ +static void RPCListen(boost::shared_ptr acceptor, + ssl::context& context, + const bool fUseSSL) +{ + + // Accept connection + AcceptedConnection* conn = new AcceptedConnection(acceptor->get_io_service(), context, fUseSSL); + + acceptor->async_accept( + conn->sslStream.lowest_layer(), + conn->peer, + boost::bind(&RPCAcceptHandler, + acceptor, + boost::ref(context), + fUseSSL, + conn, + boost::asio::placeholders::error)); +} + +/** + * Accept and handle incoming connection. + */ +static void RPCAcceptHandler(boost::shared_ptr acceptor, + ssl::context& context, + const bool fUseSSL, + AcceptedConnection* conn, + const boost::system::error_code& error) +{ + vnThreadsRunning[THREAD_RPCLISTENER]++; + + // Immediately start accepting new connections + RPCListen(acceptor, context, fUseSSL); + + // TODO: Actually handle errors + if (error) + { + delete conn; + } + + // Restrict callers by IP. It is important to + // do this before starting client thread, to filter out + // certain DoS and misbehaving clients. + else if (!ClientAllowed(conn->peer.address().to_string())) + { + // Only send a 403 if we're not using SSL to prevent a DoS during the SSL handshake. + if (!fUseSSL) + conn->stream << HTTPReply(403, "", false) << std::flush; + delete conn; + } + + // start HTTP client thread + else if (!CreateThread(ThreadRPCServer3, conn)) { + printf("Failed to create RPC server client thread\n"); + delete conn; + } + + vnThreadsRunning[THREAD_RPCLISTENER]--; +} + void ThreadRPCServer2(void* parg) { printf("ThreadRPCServer started\n"); @@ -2670,18 +2741,18 @@ void ThreadRPCServer2(void* parg) return; } - bool fUseSSL = GetBoolArg("-rpcssl"); + const bool fUseSSL = GetBoolArg("-rpcssl"); asio::ip::address bindAddress = mapArgs.count("-rpcallowip") ? asio::ip::address_v4::any() : asio::ip::address_v4::loopback(); asio::io_service io_service; ip::tcp::endpoint endpoint(bindAddress, GetArg("-rpcport", 8332)); - ip::tcp::acceptor acceptor(io_service); + boost::shared_ptr acceptor(new ip::tcp::acceptor(io_service)); try { - acceptor.open(endpoint.protocol()); - acceptor.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true)); - acceptor.bind(endpoint); - acceptor.listen(socket_base::max_connections); + acceptor->open(endpoint.protocol()); + acceptor->set_option(boost::asio::ip::tcp::acceptor::reuse_address(true)); + acceptor->bind(endpoint); + acceptor->listen(socket_base::max_connections); } catch(boost::system::system_error &e) { @@ -2710,39 +2781,12 @@ void ThreadRPCServer2(void* parg) SSL_CTX_set_cipher_list(context.impl(), strCiphers.c_str()); } - loop - { - // Accept connection - AcceptedConnection *conn = - new AcceptedConnection(io_service, context, fUseSSL); - - vnThreadsRunning[THREAD_RPCLISTENER]--; - acceptor.accept(conn->sslStream.lowest_layer(), conn->peer); - vnThreadsRunning[THREAD_RPCLISTENER]++; - - if (fShutdown) - { - delete conn; - return; - } - - // Restrict callers by IP. It is important to - // do this before starting client thread, to filter out - // certain DoS and misbehaving clients. - if (!ClientAllowed(conn->peer.address().to_string())) - { - // Only send a 403 if we're not using SSL to prevent a DoS during the SSL handshake. - if (!fUseSSL) - conn->stream << HTTPReply(403, "", false) << std::flush; - delete conn; - } + RPCListen(acceptor, context, fUseSSL); - // start HTTP client thread - else if (!CreateThread(ThreadRPCServer3, conn)) { - printf("Failed to create RPC server client thread\n"); - delete conn; - } - } + vnThreadsRunning[THREAD_RPCLISTENER]--; + while (!fShutdown) + io_service.run_one(); + vnThreadsRunning[THREAD_RPCLISTENER]++; } void ThreadRPCServer3(void* parg) From c1ecab818c3d26e49bb68111c31bd8bd68956e1e Mon Sep 17 00:00:00 2001 From: Giel van Schijndel Date: Wed, 10 Aug 2011 14:17:02 +0200 Subject: [PATCH 2/9] Add dual IPv4/IPv6 stack support to the RPC server The RPC server now listens for, and handles, incoming connections on both IPv4 as well as IPv6. If available (and usable) it uses a dual IPv4/IPv6 socket on systems that support it (e.g. Linux and BSDs) and falls back to separate IPv4/IPv6 sockets on systems that don't (e.g. Windows). Signed-off-by: Giel van Schijndel --- src/bitcoinrpc.cpp | 63 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 44 insertions(+), 19 deletions(-) diff --git a/src/bitcoinrpc.cpp b/src/bitcoinrpc.cpp index efcb39d70d..293c3793de 100644 --- a/src/bitcoinrpc.cpp +++ b/src/bitcoinrpc.cpp @@ -14,6 +14,7 @@ #undef printf #include +#include #include #include #include @@ -2742,26 +2743,8 @@ void ThreadRPCServer2(void* parg) } const bool fUseSSL = GetBoolArg("-rpcssl"); - asio::ip::address bindAddress = mapArgs.count("-rpcallowip") ? asio::ip::address_v4::any() : asio::ip::address_v4::loopback(); asio::io_service io_service; - ip::tcp::endpoint endpoint(bindAddress, GetArg("-rpcport", 8332)); - boost::shared_ptr acceptor(new ip::tcp::acceptor(io_service)); - try - { - acceptor->open(endpoint.protocol()); - acceptor->set_option(boost::asio::ip::tcp::acceptor::reuse_address(true)); - acceptor->bind(endpoint); - acceptor->listen(socket_base::max_connections); - } - catch(boost::system::system_error &e) - { - uiInterface.ThreadSafeMessageBox(strprintf(_("An error occured while setting up the RPC port %i for listening: %s"), endpoint.port(), e.what()), - _("Error"), CClientUIInterface::OK | CClientUIInterface::MODAL); - uiInterface.QueueShutdown(); - return; - } - ssl::context context(io_service, ssl::context::sslv23); if (fUseSSL) { @@ -2781,7 +2764,49 @@ void ThreadRPCServer2(void* parg) SSL_CTX_set_cipher_list(context.impl(), strCiphers.c_str()); } - RPCListen(acceptor, context, fUseSSL); + // Try a dual IPv6/IPv4 socket, falling back to separate IPv4 and IPv6 sockets + const bool loopback = !mapArgs.count("-rpcallowip"); + asio::ip::address bindAddress = loopback ? asio::ip::address_v6::loopback() : asio::ip::address_v6::any(); + ip::tcp::endpoint endpoint(bindAddress, GetArg("-rpcport", 8332)); + + boost::shared_ptr acceptor, acceptor4; + try + { + acceptor.reset(new ip::tcp::acceptor(io_service)); + acceptor->open(endpoint.protocol()); + acceptor->set_option(boost::asio::ip::tcp::acceptor::reuse_address(true)); + + // Try making the socket dual IPv6/IPv4 (if listening on the "any" address) + boost::system::error_code v6_only_error; + acceptor->set_option(boost::asio::ip::v6_only(loopback), v6_only_error); + + acceptor->bind(endpoint); + acceptor->listen(socket_base::max_connections); + + RPCListen(acceptor, context, fUseSSL); + + // If dual IPv6/IPv4 failed (or we're opening loopback interfaces only), open IPv4 separately + if (loopback || v6_only_error) + { + bindAddress = loopback ? asio::ip::address_v4::loopback() : asio::ip::address_v4::any(); + endpoint.address(bindAddress); + + acceptor4.reset(new ip::tcp::acceptor(io_service)); + acceptor4->open(endpoint.protocol()); + acceptor4->set_option(boost::asio::ip::tcp::acceptor::reuse_address(true)); + acceptor4->bind(endpoint); + acceptor4->listen(socket_base::max_connections); + + RPCListen(acceptor4, context, fUseSSL); + } + } + catch(boost::system::system_error &e) + { + uiInterface.ThreadSafeMessageBox(strprintf(_("An error occured while setting up the RPC port %i for listening: %s"), endpoint.port(), e.what()), + _("Error"), CClientUIInterface::OK | CClientUIInterface::MODAL); + uiInterface.QueueShutdown(); + return; + } vnThreadsRunning[THREAD_RPCLISTENER]--; while (!fShutdown) From 43b6dafa6ed4abee14828333596f9c611d40e213 Mon Sep 17 00:00:00 2001 From: Giel van Schijndel Date: Wed, 10 Aug 2011 14:21:43 +0200 Subject: [PATCH 3/9] Allow clients on the IPv6 loopback as well Signed-off-by: Giel van Schijndel --- src/bitcoinrpc.cpp | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/bitcoinrpc.cpp b/src/bitcoinrpc.cpp index 293c3793de..08425b40ef 100644 --- a/src/bitcoinrpc.cpp +++ b/src/bitcoinrpc.cpp @@ -2548,10 +2548,19 @@ void ErrorReply(std::ostream& stream, const Object& objError, const Value& id) stream << HTTPReply(nStatus, strReply, false) << std::flush; } -bool ClientAllowed(const string& strAddress) +bool ClientAllowed(const boost::asio::ip::address& address) { - if (strAddress == asio::ip::address_v4::loopback().to_string()) + // Make sure that IPv4-compatible and IPv4-mapped IPv6 addresses are treated as IPv4 addresses + if (address.is_v6() + && (address.to_v6().is_v4_compatible() + || address.to_v6().is_v4_mapped())) + return ClientAllowed(address.to_v6().to_v4()); + + if (address == asio::ip::address_v4::loopback() + || address == asio::ip::address_v6::loopback()) return true; + + const string strAddress = address.to_string(); const vector& vAllow = mapMultiArgs["-rpcallowip"]; BOOST_FOREACH(string strAllow, vAllow) if (WildcardMatch(strAddress, strAllow)) @@ -2696,7 +2705,7 @@ static void RPCAcceptHandler(boost::shared_ptr acceptor, // Restrict callers by IP. It is important to // do this before starting client thread, to filter out // certain DoS and misbehaving clients. - else if (!ClientAllowed(conn->peer.address().to_string())) + else if (!ClientAllowed(conn->peer.address())) { // Only send a 403 if we're not using SSL to prevent a DoS during the SSL handshake. if (!fUseSSL) From a0780ba08ac456f8bcfb69e288e0a064b79b5423 Mon Sep 17 00:00:00 2001 From: Giel van Schijndel Date: Wed, 10 Aug 2011 15:07:46 +0200 Subject: [PATCH 4/9] Generalise RPC connection handling code to allow more listening sockets Using this modification it should be relatively easy to, at a later time, listen on multiple addresses (even Unix domain sockets should be possible). Signed-off-by: Giel van Schijndel --- src/bitcoinrpc.cpp | 140 +++++++++++++++++++++++++++++++-------------- 1 file changed, 97 insertions(+), 43 deletions(-) diff --git a/src/bitcoinrpc.cpp b/src/bitcoinrpc.cpp index 08425b40ef..8a278070cf 100644 --- a/src/bitcoinrpc.cpp +++ b/src/bitcoinrpc.cpp @@ -17,6 +17,7 @@ #include #include #include +#include #include #include #include @@ -24,7 +25,7 @@ #include #include #include -typedef boost::asio::ssl::stream SSLStream; +#include #define printf OutputDebugStringF // MinGW 3.4.5 gets "fatal error: had to relocate PCH" if the json headers are @@ -2571,9 +2572,10 @@ bool ClientAllowed(const boost::asio::ip::address& address) // // IOStream device that speaks SSL but can also speak non-SSL // +template class SSLIOStreamDevice : public iostreams::device { public: - SSLIOStreamDevice(SSLStream &streamIn, bool fUseSSLIn) : stream(streamIn) + SSLIOStreamDevice(asio::ssl::stream &streamIn, bool fUseSSLIn) : stream(streamIn) { fUseSSL = fUseSSLIn; fNeedHandshake = fUseSSLIn; @@ -2617,21 +2619,54 @@ public: private: bool fNeedHandshake; bool fUseSSL; - SSLStream& stream; + asio::ssl::stream& stream; }; class AcceptedConnection { - public: - SSLStream sslStream; - SSLIOStreamDevice d; - iostreams::stream stream; +public: + virtual ~AcceptedConnection() {} + + virtual std::iostream& stream() = 0; + virtual std::string peer_address_to_string() const = 0; + virtual void close() = 0; +}; - ip::tcp::endpoint peer; +template +class AcceptedConnectionImpl : public AcceptedConnection +{ +public: + AcceptedConnectionImpl( + asio::io_service& io_service, + ssl::context &context, + bool fUseSSL) : + sslStream(io_service, context), + _d(sslStream, fUseSSL), + _stream(_d) + { + } + + virtual std::iostream& stream() + { + return _stream; + } + + virtual std::string peer_address_to_string() const + { + return peer.address().to_string(); + } - AcceptedConnection(asio::io_service &io_service, ssl::context &context, - bool fUseSSL) : sslStream(io_service, context), d(sslStream, fUseSSL), - stream(d) { ; } + virtual void close() + { + _stream.close(); + } + + typename Protocol::endpoint peer; + asio::ssl::stream sslStream; + +private: + SSLIOStreamDevice _d; + iostreams::stream< SSLIOStreamDevice > _stream; }; void ThreadRPCServer(void* parg) @@ -2654,7 +2689,8 @@ void ThreadRPCServer(void* parg) } // Forward declaration required for RPCListen -static void RPCAcceptHandler(boost::shared_ptr acceptor, +template +static void RPCAcceptHandler(boost::shared_ptr< basic_socket_acceptor > acceptor, ssl::context& context, bool fUseSSL, AcceptedConnection* conn, @@ -2663,18 +2699,19 @@ static void RPCAcceptHandler(boost::shared_ptr acceptor, /** * Sets up I/O resources to accept and handle a new connection. */ -static void RPCListen(boost::shared_ptr acceptor, +template +static void RPCListen(boost::shared_ptr< basic_socket_acceptor > acceptor, ssl::context& context, const bool fUseSSL) { // Accept connection - AcceptedConnection* conn = new AcceptedConnection(acceptor->get_io_service(), context, fUseSSL); + AcceptedConnectionImpl* conn = new AcceptedConnectionImpl(acceptor->get_io_service(), context, fUseSSL); acceptor->async_accept( conn->sslStream.lowest_layer(), conn->peer, - boost::bind(&RPCAcceptHandler, + boost::bind(&RPCAcceptHandler, acceptor, boost::ref(context), fUseSSL, @@ -2685,7 +2722,8 @@ static void RPCListen(boost::shared_ptr acceptor, /** * Accept and handle incoming connection. */ -static void RPCAcceptHandler(boost::shared_ptr acceptor, +template +static void RPCAcceptHandler(boost::shared_ptr< basic_socket_acceptor > acceptor, ssl::context& context, const bool fUseSSL, AcceptedConnection* conn, @@ -2696,6 +2734,8 @@ static void RPCAcceptHandler(boost::shared_ptr acceptor, // Immediately start accepting new connections RPCListen(acceptor, context, fUseSSL); + AcceptedConnectionImpl* tcp_conn = dynamic_cast< AcceptedConnectionImpl* >(conn); + // TODO: Actually handle errors if (error) { @@ -2705,11 +2745,12 @@ static void RPCAcceptHandler(boost::shared_ptr acceptor, // Restrict callers by IP. It is important to // do this before starting client thread, to filter out // certain DoS and misbehaving clients. - else if (!ClientAllowed(conn->peer.address())) + else if (tcp_conn + && !ClientAllowed(tcp_conn->peer.address())) { // Only send a 403 if we're not using SSL to prevent a DoS during the SSL handshake. if (!fUseSSL) - conn->stream << HTTPReply(403, "", false) << std::flush; + conn->stream() << HTTPReply(403, "", false) << std::flush; delete conn; } @@ -2778,21 +2819,21 @@ void ThreadRPCServer2(void* parg) asio::ip::address bindAddress = loopback ? asio::ip::address_v6::loopback() : asio::ip::address_v6::any(); ip::tcp::endpoint endpoint(bindAddress, GetArg("-rpcport", 8332)); - boost::shared_ptr acceptor, acceptor4; + std::list< boost::shared_ptr > acceptors; try { - acceptor.reset(new ip::tcp::acceptor(io_service)); - acceptor->open(endpoint.protocol()); - acceptor->set_option(boost::asio::ip::tcp::acceptor::reuse_address(true)); + acceptors.push_back(boost::shared_ptr(new ip::tcp::acceptor(io_service))); + acceptors.back()->open(endpoint.protocol()); + acceptors.back()->set_option(boost::asio::ip::tcp::acceptor::reuse_address(true)); // Try making the socket dual IPv6/IPv4 (if listening on the "any" address) boost::system::error_code v6_only_error; - acceptor->set_option(boost::asio::ip::v6_only(loopback), v6_only_error); + acceptors.back()->set_option(boost::asio::ip::v6_only(loopback), v6_only_error); - acceptor->bind(endpoint); - acceptor->listen(socket_base::max_connections); + acceptors.back()->bind(endpoint); + acceptors.back()->listen(socket_base::max_connections); - RPCListen(acceptor, context, fUseSSL); + RPCListen(acceptors.back(), context, fUseSSL); // If dual IPv6/IPv4 failed (or we're opening loopback interfaces only), open IPv4 separately if (loopback || v6_only_error) @@ -2800,13 +2841,13 @@ void ThreadRPCServer2(void* parg) bindAddress = loopback ? asio::ip::address_v4::loopback() : asio::ip::address_v4::any(); endpoint.address(bindAddress); - acceptor4.reset(new ip::tcp::acceptor(io_service)); - acceptor4->open(endpoint.protocol()); - acceptor4->set_option(boost::asio::ip::tcp::acceptor::reuse_address(true)); - acceptor4->bind(endpoint); - acceptor4->listen(socket_base::max_connections); + acceptors.push_back(boost::shared_ptr(new ip::tcp::acceptor(io_service))); + acceptors.back()->open(endpoint.protocol()); + acceptors.back()->set_option(boost::asio::ip::tcp::acceptor::reuse_address(true)); + acceptors.back()->bind(endpoint); + acceptors.back()->listen(socket_base::max_connections); - RPCListen(acceptor4, context, fUseSSL); + RPCListen(acceptors.back(), context, fUseSSL); } } catch(boost::system::system_error &e) @@ -2821,6 +2862,19 @@ void ThreadRPCServer2(void* parg) while (!fShutdown) io_service.run_one(); vnThreadsRunning[THREAD_RPCLISTENER]++; + + // Terminate all outstanding accept-requests + BOOST_FOREACH(boost::shared_ptr& acceptor, acceptors) + { + acceptor->cancel(); + acceptor->close(); + } + acceptors.clear(); + + // Handle any actions that are still in progress. + vnThreadsRunning[THREAD_RPCLISTENER]--; + io_service.run(); + vnThreadsRunning[THREAD_RPCLISTENER]++; } void ThreadRPCServer3(void* parg) @@ -2833,7 +2887,7 @@ void ThreadRPCServer3(void* parg) loop { if (fShutdown || !fRun) { - conn->stream.close(); + conn->close(); delete conn; --vnThreadsRunning[THREAD_RPCHANDLER]; return; @@ -2841,24 +2895,24 @@ void ThreadRPCServer3(void* parg) map mapHeaders; string strRequest; - ReadHTTP(conn->stream, mapHeaders, strRequest); + ReadHTTP(conn->stream(), mapHeaders, strRequest); // Check authorization if (mapHeaders.count("authorization") == 0) { - conn->stream << HTTPReply(401, "", false) << std::flush; + conn->stream() << HTTPReply(401, "", false) << std::flush; break; } if (!HTTPAuthorized(mapHeaders)) { - printf("ThreadRPCServer incorrect password attempt from %s\n", conn->peer.address().to_string().c_str()); + printf("ThreadRPCServer incorrect password attempt from %s\n", conn->peer_address_to_string().c_str()); /* Deter brute-forcing short passwords. If this results in a DOS the user really shouldn't have their RPC port exposed.*/ if (mapArgs["-rpcpassword"].size() < 20) Sleep(250); - conn->stream << HTTPReply(401, "", false) << std::flush; + conn->stream() << HTTPReply(401, "", false) << std::flush; break; } if (mapHeaders["connection"] == "close") @@ -2900,16 +2954,16 @@ void ThreadRPCServer3(void* parg) // Send reply string strReply = JSONRPCReply(result, Value::null, id); - conn->stream << HTTPReply(200, strReply, fRun) << std::flush; + conn->stream() << HTTPReply(200, strReply, fRun) << std::flush; } catch (Object& objError) { - ErrorReply(conn->stream, objError, id); + ErrorReply(conn->stream(), objError, id); break; } catch (std::exception& e) { - ErrorReply(conn->stream, JSONRPCError(-32700, e.what()), id); + ErrorReply(conn->stream(), JSONRPCError(-32700, e.what()), id); break; } } @@ -2961,9 +3015,9 @@ Object CallRPC(const string& strMethod, const Array& params) asio::io_service io_service; ssl::context context(io_service, ssl::context::sslv23); context.set_options(ssl::context::no_sslv2); - SSLStream sslStream(io_service, context); - SSLIOStreamDevice d(sslStream, fUseSSL); - iostreams::stream stream(d); + asio::ssl::stream sslStream(io_service, context); + SSLIOStreamDevice d(sslStream, fUseSSL); + iostreams::stream< SSLIOStreamDevice > stream(d); if (!d.connect(GetArg("-rpcconnect", "127.0.0.1"), GetArg("-rpcport", "8332"))) throw runtime_error("couldn't connect to server"); From 7cc2ceae09d7b36f9054e8f57c1fa7ba87e21171 Mon Sep 17 00:00:00 2001 From: Giel van Schijndel Date: Sun, 20 May 2012 17:46:44 +0200 Subject: [PATCH 5/9] Allow all addresses on the loopback subnet (127.0.0.0/8) not just 127.0.0.1 Signed-off-by: Giel van Schijndel --- src/bitcoinrpc.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/bitcoinrpc.cpp b/src/bitcoinrpc.cpp index 8a278070cf..bd7cb3ef44 100644 --- a/src/bitcoinrpc.cpp +++ b/src/bitcoinrpc.cpp @@ -2558,7 +2558,10 @@ bool ClientAllowed(const boost::asio::ip::address& address) return ClientAllowed(address.to_v6().to_v4()); if (address == asio::ip::address_v4::loopback() - || address == asio::ip::address_v6::loopback()) + || address == asio::ip::address_v6::loopback() + || (address.is_v4() + // Chech whether IPv4 addresses match 127.0.0.0/8 (loopback subnet) + && (address.to_v4().to_ulong() & 0xff000000) == 0x7f000000)) return true; const string strAddress = address.to_string(); From fbf9df2ea32528c71b58160283b4eb2c52e30ccb Mon Sep 17 00:00:00 2001 From: Giel van Schijndel Date: Sun, 20 May 2012 20:27:53 +0200 Subject: [PATCH 6/9] Use the QueueShutdown signal to stop accepting new RPC connections Signed-off-by: Giel van Schijndel --- src/bitcoinrpc.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/bitcoinrpc.cpp b/src/bitcoinrpc.cpp index bd7cb3ef44..9e785a3e37 100644 --- a/src/bitcoinrpc.cpp +++ b/src/bitcoinrpc.cpp @@ -2798,6 +2798,12 @@ void ThreadRPCServer2(void* parg) const bool fUseSSL = GetBoolArg("-rpcssl"); asio::io_service io_service; + + // Make sure that we'll get stopped when the application shuts down + boost::signals2::scoped_connection rpc_listen_thread_stop( + uiInterface.QueueShutdown.connect(boost::bind( + &asio::io_service::stop, &io_service))); + ssl::context context(io_service, ssl::context::sslv23); if (fUseSSL) { @@ -2862,8 +2868,7 @@ void ThreadRPCServer2(void* parg) } vnThreadsRunning[THREAD_RPCLISTENER]--; - while (!fShutdown) - io_service.run_one(); + io_service.run(); vnThreadsRunning[THREAD_RPCLISTENER]++; // Terminate all outstanding accept-requests @@ -2873,11 +2878,6 @@ void ThreadRPCServer2(void* parg) acceptor->close(); } acceptors.clear(); - - // Handle any actions that are still in progress. - vnThreadsRunning[THREAD_RPCLISTENER]--; - io_service.run(); - vnThreadsRunning[THREAD_RPCLISTENER]++; } void ThreadRPCServer3(void* parg) From 896899e0d66e25f6549a92749d237c8a87b12f08 Mon Sep 17 00:00:00 2001 From: Giel van Schijndel Date: Sun, 17 Jun 2012 16:21:09 +0200 Subject: [PATCH 7/9] *Always* send a shutdown signal to enable custom shutdown actions NOTE: This is required to be sure that we can properly shut down the RPC thread. Signed-off-by: Giel van Schijndel --- src/bitcoinrpc.cpp | 3 +-- src/init.cpp | 11 ++++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/bitcoinrpc.cpp b/src/bitcoinrpc.cpp index 01142ab7cd..a35d33e14e 100644 --- a/src/bitcoinrpc.cpp +++ b/src/bitcoinrpc.cpp @@ -2884,8 +2884,7 @@ void ThreadRPCServer2(void* parg) } vnThreadsRunning[THREAD_RPCLISTENER]--; - while (!fShutdown) - io_service.run_one(); + io_service.run(); vnThreadsRunning[THREAD_RPCLISTENER]++; // Terminate all outstanding accept-requests diff --git a/src/init.cpp b/src/init.cpp index 08b594f56c..4e0947a16a 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -9,6 +9,7 @@ #include "init.h" #include "util.h" #include "ui_interface.h" +#include #include #include #include @@ -40,13 +41,8 @@ void ExitTimeout(void* parg) void StartShutdown() { -#ifdef QT_GUI // ensure we leave the Qt main loop for a clean GUI exit (Shutdown() is called in bitcoin.cpp afterwards) uiInterface.QueueShutdown(); -#else - // Without UI, Shutdown() can simply be started in a new thread - CreateThread(Shutdown, NULL); -#endif } void Shutdown(void* parg) @@ -154,6 +150,11 @@ bool AppInit(int argc, char* argv[]) exit(ret); } + // Create the shutdown thread when receiving a shutdown signal + boost::signals2::scoped_connection do_stop( + uiInterface.QueueShutdown.connect(boost::bind( + &CreateThread, &Shutdown, static_cast(0), false))); + fRet = AppInit2(); } catch (std::exception& e) { From ad25804febcd8fe7d03241e420a8dd5f297aa56b Mon Sep 17 00:00:00 2001 From: Giel van Schijndel Date: Sun, 24 Jun 2012 13:20:17 +0200 Subject: [PATCH 8/9] Cancel outstanding listen ops for RPC when shutting down Use Boost's signal2 slot tracking mechanism to cancel any (still open) listening sockets when receiving a shutdown signal. Signed-off-by: Giel van Schijndel --- src/bitcoinrpc.cpp | 57 +++++++++++++++++++++------------------------- 1 file changed, 26 insertions(+), 31 deletions(-) diff --git a/src/bitcoinrpc.cpp b/src/bitcoinrpc.cpp index 25ff6c1e5f..8d78b8b00a 100644 --- a/src/bitcoinrpc.cpp +++ b/src/bitcoinrpc.cpp @@ -2741,7 +2741,6 @@ static void RPCListen(boost::shared_ptr< basic_socket_acceptor* conn = new AcceptedConnectionImpl(acceptor->get_io_service(), context, fUseSSL); @@ -2768,8 +2767,10 @@ static void RPCAcceptHandler(boost::shared_ptr< basic_socket_acceptoris_open()) + RPCListen(acceptor, context, fUseSSL); AcceptedConnectionImpl* tcp_conn = dynamic_cast< AcceptedConnectionImpl* >(conn); @@ -2833,11 +2834,6 @@ void ThreadRPCServer2(void* parg) asio::io_service io_service; - // Make sure that we'll get stopped when the application shuts down - boost::signals2::scoped_connection rpc_listen_thread_stop( - uiInterface.QueueShutdown.connect(boost::bind( - &asio::io_service::stop, &io_service))); - ssl::context context(io_service, ssl::context::sslv23); if (fUseSSL) { @@ -2862,21 +2858,24 @@ void ThreadRPCServer2(void* parg) asio::ip::address bindAddress = loopback ? asio::ip::address_v6::loopback() : asio::ip::address_v6::any(); ip::tcp::endpoint endpoint(bindAddress, GetArg("-rpcport", 8332)); - std::list< boost::shared_ptr > acceptors; try { - acceptors.push_back(boost::shared_ptr(new ip::tcp::acceptor(io_service))); - acceptors.back()->open(endpoint.protocol()); - acceptors.back()->set_option(boost::asio::ip::tcp::acceptor::reuse_address(true)); + boost::shared_ptr acceptor(new ip::tcp::acceptor(io_service)); + acceptor->open(endpoint.protocol()); + acceptor->set_option(boost::asio::ip::tcp::acceptor::reuse_address(true)); // Try making the socket dual IPv6/IPv4 (if listening on the "any" address) boost::system::error_code v6_only_error; - acceptors.back()->set_option(boost::asio::ip::v6_only(loopback), v6_only_error); + acceptor->set_option(boost::asio::ip::v6_only(loopback), v6_only_error); - acceptors.back()->bind(endpoint); - acceptors.back()->listen(socket_base::max_connections); + acceptor->bind(endpoint); + acceptor->listen(socket_base::max_connections); - RPCListen(acceptors.back(), context, fUseSSL); + RPCListen(acceptor, context, fUseSSL); + // Cancel outstanding listen-requests for this acceptor when shutting down + uiInterface.QueueShutdown.connect(signals2::slot( + static_cast(&ip::tcp::acceptor::close), acceptor.get()) + .track(acceptor)); // If dual IPv6/IPv4 failed (or we're opening loopback interfaces only), open IPv4 separately if (loopback || v6_only_error) @@ -2884,13 +2883,17 @@ void ThreadRPCServer2(void* parg) bindAddress = loopback ? asio::ip::address_v4::loopback() : asio::ip::address_v4::any(); endpoint.address(bindAddress); - acceptors.push_back(boost::shared_ptr(new ip::tcp::acceptor(io_service))); - acceptors.back()->open(endpoint.protocol()); - acceptors.back()->set_option(boost::asio::ip::tcp::acceptor::reuse_address(true)); - acceptors.back()->bind(endpoint); - acceptors.back()->listen(socket_base::max_connections); - - RPCListen(acceptors.back(), context, fUseSSL); + acceptor.reset(new ip::tcp::acceptor(io_service)); + acceptor->open(endpoint.protocol()); + acceptor->set_option(boost::asio::ip::tcp::acceptor::reuse_address(true)); + acceptor->bind(endpoint); + acceptor->listen(socket_base::max_connections); + + RPCListen(acceptor, context, fUseSSL); + // Cancel outstanding listen-requests for this acceptor when shutting down + uiInterface.QueueShutdown.connect(signals2::slot( + static_cast(&ip::tcp::acceptor::close), acceptor.get()) + .track(acceptor)); } } catch(boost::system::system_error &e) @@ -2904,14 +2907,6 @@ void ThreadRPCServer2(void* parg) vnThreadsRunning[THREAD_RPCLISTENER]--; io_service.run(); vnThreadsRunning[THREAD_RPCLISTENER]++; - - // Terminate all outstanding accept-requests - BOOST_FOREACH(boost::shared_ptr& acceptor, acceptors) - { - acceptor->cancel(); - acceptor->close(); - } - acceptors.clear(); } void ThreadRPCServer3(void* parg) From 5b1462211031934437afef7da1382766c3570770 Mon Sep 17 00:00:00 2001 From: Giel van Schijndel Date: Sun, 24 Jun 2012 14:16:30 +0200 Subject: [PATCH 9/9] On Windows link with `mswsock`, it being required (indirectly) by RPC code Signed-off-by: Giel van Schijndel --- bitcoin-qt.pro | 2 +- src/makefile.linux-mingw | 2 +- src/makefile.mingw | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/bitcoin-qt.pro b/bitcoin-qt.pro index f1ebec6bcc..430214eaff 100644 --- a/bitcoin-qt.pro +++ b/bitcoin-qt.pro @@ -306,7 +306,7 @@ isEmpty(BOOST_INCLUDE_PATH) { macx:BOOST_INCLUDE_PATH = /opt/local/include } -windows:LIBS += -lws2_32 -lshlwapi +windows:LIBS += -lws2_32 -lshlwapi -lmswsock windows:DEFINES += WIN32 windows:RC_FILE = src/qt/res/bitcoin-qt.rc diff --git a/src/makefile.linux-mingw b/src/makefile.linux-mingw index 51f49bb3cf..cd8e97080c 100644 --- a/src/makefile.linux-mingw +++ b/src/makefile.linux-mingw @@ -39,7 +39,7 @@ ifdef USE_UPNP DEFS += -DSTATICLIB -DUSE_UPNP=$(USE_UPNP) endif -LIBS += -l mingwthrd -l kernel32 -l user32 -l gdi32 -l comdlg32 -l winspool -l winmm -l shell32 -l comctl32 -l ole32 -l oleaut32 -l uuid -l rpcrt4 -l advapi32 -l ws2_32 -l shlwapi +LIBS += -l mingwthrd -l kernel32 -l user32 -l gdi32 -l comdlg32 -l winspool -l winmm -l shell32 -l comctl32 -l ole32 -l oleaut32 -l uuid -l rpcrt4 -l advapi32 -l ws2_32 -l mswsock -l shlwapi # TODO: make the mingw builds smarter about dependencies, like the linux/osx builds are HEADERS = $(wildcard *.h) diff --git a/src/makefile.mingw b/src/makefile.mingw index 577c77b7d4..919be007b6 100644 --- a/src/makefile.mingw +++ b/src/makefile.mingw @@ -36,7 +36,7 @@ ifdef USE_UPNP DEFS += -DSTATICLIB -DUSE_UPNP=$(USE_UPNP) endif -LIBS += -l kernel32 -l user32 -l gdi32 -l comdlg32 -l winspool -l winmm -l shell32 -l comctl32 -l ole32 -l oleaut32 -l uuid -l rpcrt4 -l advapi32 -l ws2_32 -l shlwapi +LIBS += -l kernel32 -l user32 -l gdi32 -l comdlg32 -l winspool -l winmm -l shell32 -l comctl32 -l ole32 -l oleaut32 -l uuid -l rpcrt4 -l advapi32 -l ws2_32 -l mswsock -l shlwapi # TODO: make the mingw builds smarter about dependencies, like the linux/osx builds are HEADERS = $(wildcard *.h)