From 8f35b7865186d3577dcdcc066ac29a4392807ec8 Mon Sep 17 00:00:00 2001 From: Arthur Britto Date: Thu, 20 Dec 2012 23:19:20 -0800 Subject: [PATCH] Exit with code 3 if network port is in use. --- src/cpp/ripple/Application.cpp | 48 +++++++++++++++++++++++++++++++--- src/cpp/ripple/RPCDoor.cpp | 17 +++++++----- src/cpp/ripple/WSDoor.cpp | 7 +++-- 3 files changed, 57 insertions(+), 15 deletions(-) diff --git a/src/cpp/ripple/Application.cpp b/src/cpp/ripple/Application.cpp index 475f41d262..5abf6bbc10 100644 --- a/src/cpp/ripple/Application.cpp +++ b/src/cpp/ripple/Application.cpp @@ -158,7 +158,17 @@ void Application::run() // if (!theConfig.RUN_STANDALONE && !theConfig.PEER_IP.empty() && theConfig.PEER_PORT) { - mPeerDoor = new PeerDoor(mIOService); + try + { + mPeerDoor = new PeerDoor(mIOService); + } + catch (const std::exception& e) + { + // Must run as directed or exit. + cLog(lsFATAL) << boost::str(boost::format("Can not open peer service: %s") % e.what()); + + exit(3); + } } else { @@ -170,7 +180,17 @@ void Application::run() // if (!theConfig.RPC_IP.empty() && theConfig.RPC_PORT) { - mRPCDoor = new RPCDoor(mIOService); + try + { + mRPCDoor = new RPCDoor(mIOService); + } + catch (const std::exception& e) + { + // Must run as directed or exit. + cLog(lsFATAL) << boost::str(boost::format("Can not open RPC service: %s") % e.what()); + + exit(3); + } } else { @@ -182,7 +202,17 @@ void Application::run() // if (!theConfig.WEBSOCKET_IP.empty() && theConfig.WEBSOCKET_PORT) { - mWSPrivateDoor = WSDoor::createWSDoor(theConfig.WEBSOCKET_IP, theConfig.WEBSOCKET_PORT, false); + try + { + mWSPrivateDoor = WSDoor::createWSDoor(theConfig.WEBSOCKET_IP, theConfig.WEBSOCKET_PORT, false); + } + catch (const std::exception& e) + { + // Must run as directed or exit. + cLog(lsFATAL) << boost::str(boost::format("Can not open private websocket service: %s") % e.what()); + + exit(3); + } } else { @@ -194,7 +224,17 @@ void Application::run() // if (!theConfig.WEBSOCKET_PUBLIC_IP.empty() && theConfig.WEBSOCKET_PUBLIC_PORT) { - mWSPublicDoor = WSDoor::createWSDoor(theConfig.WEBSOCKET_PUBLIC_IP, theConfig.WEBSOCKET_PUBLIC_PORT, true); + try + { + mWSPublicDoor = WSDoor::createWSDoor(theConfig.WEBSOCKET_PUBLIC_IP, theConfig.WEBSOCKET_PUBLIC_PORT, true); + } + catch (const std::exception& e) + { + // Must run as directed or exit. + cLog(lsFATAL) << boost::str(boost::format("Can not open public websocket service: %s") % e.what()); + + exit(3); + } } else { diff --git a/src/cpp/ripple/RPCDoor.cpp b/src/cpp/ripple/RPCDoor.cpp index 9d98a24624..40c1e0f676 100644 --- a/src/cpp/ripple/RPCDoor.cpp +++ b/src/cpp/ripple/RPCDoor.cpp @@ -5,18 +5,20 @@ #include #include +SETUP_LOG(); + using namespace std; using namespace boost::asio::ip; RPCDoor::RPCDoor(boost::asio::io_service& io_service) : mAcceptor(io_service, tcp::endpoint(address::from_string(theConfig.RPC_IP), theConfig.RPC_PORT)) { - Log(lsINFO) << "RPC port: " << theConfig.RPC_IP << " " << theConfig.RPC_PORT << " allow remote: " << theConfig.RPC_ALLOW_REMOTE; + cLog(lsINFO) << "RPC port: " << theConfig.RPC_IP << " " << theConfig.RPC_PORT << " allow remote: " << theConfig.RPC_ALLOW_REMOTE; startListening(); } RPCDoor::~RPCDoor() { - Log(lsINFO) << "RPC port: " << theConfig.RPC_IP << " " << theConfig.RPC_PORT << " allow remote: " << theConfig.RPC_ALLOW_REMOTE; + cLog(lsINFO) << "RPC port: " << theConfig.RPC_IP << " " << theConfig.RPC_PORT << " allow remote: " << theConfig.RPC_ALLOW_REMOTE; } void RPCDoor::startListening() @@ -31,25 +33,26 @@ void RPCDoor::startListening() bool RPCDoor::isClientAllowed(const std::string& ip) { - if(theConfig.RPC_ALLOW_REMOTE) return(true); - if(ip=="127.0.0.1") return(true); + if (theConfig.RPC_ALLOW_REMOTE) return(true); + if (ip=="127.0.0.1") return(true); + return(false); } void RPCDoor::handleConnect(RPCServer::pointer new_connection, const boost::system::error_code& error) { - if(!error) + if (!error) { // Restrict callers by IP - if(!isClientAllowed(new_connection->getSocket().remote_endpoint().address().to_string())) + if (!isClientAllowed(new_connection->getSocket().remote_endpoint().address().to_string())) { return; } new_connection->connected(); } - else Log(lsINFO) << "RPCDoor::handleConnect Error: " << error; + else cLog(lsINFO) << "RPCDoor::handleConnect Error: " << error; startListening(); } diff --git a/src/cpp/ripple/WSDoor.cpp b/src/cpp/ripple/WSDoor.cpp index a992d667c4..d75c30cc59 100644 --- a/src/cpp/ripple/WSDoor.cpp +++ b/src/cpp/ripple/WSDoor.cpp @@ -58,7 +58,7 @@ void WSDoor::startListening() SSL_CTX_set_tmp_dh_callback(mCtx->native_handle(), handleTmpDh); - if(theConfig.WEBSOCKET_SECURE) + if (theConfig.WEBSOCKET_SECURE) { // Construct a single handler for all requests. websocketpp::server_tls::handler::ptr handler(new WSServerHandler(mCtx, mPublic)); @@ -94,7 +94,8 @@ void WSDoor::startListening() } delete mSEndpoint; - }else + } + else { // Construct a single handler for all requests. websocketpp::server::handler::ptr handler(new WSServerHandler(mCtx, mPublic)); @@ -131,7 +132,6 @@ void WSDoor::startListening() delete mEndpoint; } - } WSDoor* WSDoor::createWSDoor(const std::string& strIp, const int iPort, bool bPublic) @@ -163,5 +163,4 @@ void WSDoor::stop() } } - // vim:ts=4