Cache the result of remote_endpoint at connect/accept time and use it

This commit is contained in:
Nik Bougalis
2014-03-05 12:57:15 -08:00
parent e8002a7a62
commit 9c465487ae
6 changed files with 29 additions and 45 deletions

View File

@@ -60,9 +60,10 @@ public:
mAcceptor.set_option (boost::asio::ip::tcp::acceptor::reuse_address (true)); mAcceptor.set_option (boost::asio::ip::tcp::acceptor::reuse_address (true));
mAcceptor.async_accept (new_connection->getRawSocket (), mAcceptor.async_accept (new_connection->getRawSocket (),
boost::bind (&RPCDoorImp::handleConnect, this, new_connection, new_connection->getRemoteEndpoint (),
boost::asio::placeholders::error)); boost::bind (&RPCDoorImp::handleConnect, this,
new_connection, boost::asio::placeholders::error));
} }
//-------------------------------------------------------------------------- //--------------------------------------------------------------------------
@@ -82,25 +83,19 @@ public:
//-------------------------------------------------------------------------- //--------------------------------------------------------------------------
void handleConnect (RPCServerImp::pointer new_connection, boost::system::error_code const& error) void handleConnect (RPCServerImp::pointer new_connection,
boost::system::error_code const& error)
{ {
bool delay = false; bool delay = false;
if (!error) if (!error)
{ {
// Restrict callers by IP // Restrict callers by IP
// VFALCO NOTE Prevent exceptions from being thrown at all. std::string client_ip (
try new_connection->getRemoteEndpoint ().address ().to_string ());
if (! isClientAllowed (client_ip))
{ {
if (! isClientAllowed (new_connection->getRemoteAddressText ()))
{
startListening ();
return;
}
}
catch (...)
{
// client may have disconnected
startListening (); startListening ();
return; return;
} }

View File

@@ -55,15 +55,9 @@ public:
*/ */
virtual void connected () = 0; virtual void connected () = 0;
// VFALCO TODO Remove this since it exposes boost // VFALCO TODO Remove these since they expose boost
virtual boost::asio::ip::tcp::socket& getRawSocket () = 0; virtual boost::asio::ip::tcp::socket& getRawSocket () = 0;
virtual boost::asio::ip::tcp::socket::endpoint_type& getRemoteEndpoint () = 0;
/** Retrieve the remote address as a string.
@return A std::string representing the remote address.
*/
// VFALCO TODO Replace the return type with a dedicated class.
virtual std::string getRemoteAddressText () = 0;
}; };
#endif #endif

View File

@@ -219,8 +219,9 @@ public:
{ {
WriteLog (lsTRACE, RPCServer) << "handleRequest " << request; WriteLog (lsTRACE, RPCServer) << "handleRequest " << request;
return m_handler.processRequest (request, beast::IPAddressConversion::from_asio ( return m_handler.processRequest (request,
mSocket.PlainSocket ().remote_endpoint().address())); beast::IPAddressConversion::from_asio (
m_remote_endpoint.address()));
} }
//-------------------------------------------------------------------------- //--------------------------------------------------------------------------
@@ -237,15 +238,9 @@ public:
return mSocket.PlainSocket (); return mSocket.PlainSocket ();
} }
//-------------------------------------------------------------------------- boost::asio::ip::tcp::socket::endpoint_type& getRemoteEndpoint ()
std::string getRemoteAddressText ()
{ {
std::string address; return m_remote_endpoint;
address = mSocket.PlainSocket ().remote_endpoint ().address ().to_string ();
return address;
} }
private: private:
@@ -253,6 +248,7 @@ private:
boost::asio::io_service::strand mStrand; boost::asio::io_service::strand mStrand;
AutoSocket mSocket; AutoSocket mSocket;
AutoSocket::endpoint_type m_remote_endpoint;
boost::asio::streambuf mLineBuffer; boost::asio::streambuf mLineBuffer;
Blob mQueryVec; Blob mQueryVec;

View File

@@ -168,6 +168,7 @@ public:
/** New incoming peer from the specified socket */ /** New incoming peer from the specified socket */
PeerImp ( PeerImp (
boost::shared_ptr <NativeSocketType> const& socket, boost::shared_ptr <NativeSocketType> const& socket,
IP::Endpoint remoteAddress,
Peers& peers, Peers& peers,
Resource::Manager& resourceManager, Resource::Manager& resourceManager,
PeerFinder::Manager& peerFinder, PeerFinder::Manager& peerFinder,
@@ -177,6 +178,7 @@ public:
: m_shared_socket (socket) : m_shared_socket (socket)
, m_journal (LogPartition::getJournal <Peer> ()) , m_journal (LogPartition::getJournal <Peer> ())
, m_shortId (0) , m_shortId (0)
, m_remoteAddress (remoteAddress)
, m_resourceManager (resourceManager) , m_resourceManager (resourceManager)
, m_peerFinder (peerFinder) , m_peerFinder (peerFinder)
, m_peers (peers) , m_peers (peers)
@@ -202,6 +204,7 @@ public:
from inside constructors. from inside constructors.
*/ */
PeerImp ( PeerImp (
IP::Endpoint remoteAddress,
boost::asio::io_service& io_service, boost::asio::io_service& io_service,
Peers& peers, Peers& peers,
Resource::Manager& resourceManager, Resource::Manager& resourceManager,
@@ -211,6 +214,7 @@ public:
MultiSocket::Flag flags) MultiSocket::Flag flags)
: m_journal (LogPartition::getJournal <Peer> ()) : m_journal (LogPartition::getJournal <Peer> ())
, m_shortId (0) , m_shortId (0)
, m_remoteAddress (remoteAddress)
, m_resourceManager (resourceManager) , m_resourceManager (resourceManager)
, m_peerFinder (peerFinder) , m_peerFinder (peerFinder)
, m_peers (peers) , m_peers (peers)
@@ -274,10 +278,8 @@ public:
object and begins the process of connection establishment instead object and begins the process of connection establishment instead
of requiring the caller to construct a Peer and call connect. of requiring the caller to construct a Peer and call connect.
*/ */
void connect (IP::Endpoint const& address) void connect ()
{ {
m_remoteAddress = address;
m_journal.info << "Connecting to " << m_remoteAddress; m_journal.info << "Connecting to " << m_remoteAddress;
boost::system::error_code err; boost::system::error_code err;
@@ -295,7 +297,7 @@ public:
} }
getNativeSocket ().async_connect ( getNativeSocket ().async_connect (
IPAddressConversion::to_asio_endpoint (address), IPAddressConversion::to_asio_endpoint (m_remoteAddress),
m_strand.wrap (boost::bind (&PeerImp::onConnect, m_strand.wrap (boost::bind (&PeerImp::onConnect,
shared_from_this (), boost::asio::placeholders::error))); shared_from_this (), boost::asio::placeholders::error)));
} }
@@ -425,8 +427,6 @@ public:
*/ */
void accept () void accept ()
{ {
m_remoteAddress = m_socket->remote_endpoint();
m_journal.info << "Accepted " << m_remoteAddress; m_journal.info << "Accepted " << m_remoteAddress;
m_socket->set_verify_mode (boost::asio::ssl::verify_none); m_socket->set_verify_mode (boost::asio::ssl::verify_none);
@@ -776,8 +776,6 @@ private:
return; return;
} }
m_remoteAddress = m_socket->remote_endpoint();
if (m_inbound) if (m_inbound)
m_usage = m_resourceManager.newInboundEndpoint (m_remoteAddress); m_usage = m_resourceManager.newInboundEndpoint (m_remoteAddress);
else else

View File

@@ -206,7 +206,7 @@ public:
flags = flags.with (MultiSocket::Flag::proxy); flags = flags.with (MultiSocket::Flag::proxy);
PeerImp::ptr const peer (boost::make_shared <PeerImp> ( PeerImp::ptr const peer (boost::make_shared <PeerImp> (
socket, *this, m_resourceManager, *m_peerFinder, socket, remote_endpoint, *this, m_resourceManager, *m_peerFinder,
slot, m_ssl_context, flags)); slot, m_ssl_context, flags));
{ {
@@ -244,8 +244,8 @@ public:
MultiSocket::Flag::client_role | MultiSocket::Flag::ssl); MultiSocket::Flag::client_role | MultiSocket::Flag::ssl);
PeerImp::ptr const peer (boost::make_shared <PeerImp> ( PeerImp::ptr const peer (boost::make_shared <PeerImp> (
m_io_service, *this, m_resourceManager, *m_peerFinder, remote_endpoint, m_io_service, *this, m_resourceManager,
slot, m_ssl_context, flags)); *m_peerFinder, slot, m_ssl_context, flags));
{ {
std::lock_guard <decltype(m_mutex)> lock (m_mutex); std::lock_guard <decltype(m_mutex)> lock (m_mutex);
@@ -258,7 +258,7 @@ public:
// This has to happen while holding the lock, // This has to happen while holding the lock,
// otherwise the socket might not be canceled during a stop. // otherwise the socket might not be canceled during a stop.
peer->connect (remote_endpoint); peer->connect ();
} }
} }

View File

@@ -29,6 +29,7 @@ class AutoSocket
{ {
public: public:
typedef boost::asio::ssl::stream<boost::asio::ip::tcp::socket> ssl_socket; typedef boost::asio::ssl::stream<boost::asio::ip::tcp::socket> ssl_socket;
typedef boost::asio::ip::tcp::socket::endpoint_type endpoint_type;
typedef boost::shared_ptr<ssl_socket> socket_ptr; typedef boost::shared_ptr<ssl_socket> socket_ptr;
typedef ssl_socket::next_layer_type plain_socket; typedef ssl_socket::next_layer_type plain_socket;
typedef ssl_socket::lowest_layer_type lowest_layer_type; typedef ssl_socket::lowest_layer_type lowest_layer_type;