From 1da91f41b3fab95b7cee8ba8ea8644f189892f3e Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Tue, 7 Feb 2023 21:41:32 -0800 Subject: [PATCH] ircd::net: Elide repeated getsockname()/getpeername() w/ cached sockaddrs. --- include/ircd/net/socket.h | 4 +--- ircd/net.cc | 23 +++++++---------------- ircd/net_listener.cc | 15 ++++++++++----- 3 files changed, 18 insertions(+), 24 deletions(-) diff --git a/include/ircd/net/socket.h b/include/ircd/net/socket.h index 0a8ffc8bc..ab3743e9b 100644 --- a/include/ircd/net/socket.h +++ b/include/ircd/net/socket.h @@ -62,6 +62,7 @@ ircd::net::socket uint64_t id {++count}; ip::tcp::socket sd; asio::ssl::stream ssl; + endpoint local, remote; stat in, out; deadline_timer timer; uint64_t timer_sem[2] {0}; // handler, sender @@ -85,9 +86,6 @@ ircd::net::socket operator const SSL &() const; operator SSL &(); - endpoint remote() const; // getpeername(); throws if not conn - endpoint local() const; // getsockname(); throws if not conn/bound - // Timer for this socket void set_timeout(const milliseconds &, ec_handler); void set_timeout(const milliseconds &); diff --git a/ircd/net.cc b/ircd/net.cc index 3a1d5f50f..53e0b3ffa 100644 --- a/ircd/net.cc +++ b/ircd/net.cc @@ -204,7 +204,7 @@ noexcept try if(!opened(socket)) return {}; - const auto &ep(socket.remote()); + const auto &ep(socket.remote); return make_ipport(ep); } catch(...) @@ -219,7 +219,7 @@ noexcept try if(!opened(socket)) return {}; - const auto &ep(socket.local()); + const auto &ep(socket.local); return make_ipport(ep); } catch(...) @@ -1490,6 +1490,7 @@ ircd::net::socket::connect(const endpoint &ep, std::bind(&socket::handle_connect, this, weak_from(*this), opts, std::move(callback), ph::_1) }; + this->remote = ep; set_timeout(opts.connect_timeout); sd.async_connect(ep, ios::handle(desc_connect, std::move(connect_handler))); } @@ -2284,6 +2285,10 @@ noexcept try using std::errc; const life_guard s{wp}; + + if(likely(sd.is_open())) + this->local = sd.local_endpoint(); + char ecbuf[64], epbuf[128]; log::debug { @@ -2732,20 +2737,6 @@ ircd::net::socket::set_timeout(const milliseconds &t, timer.async_wait(ios::handle(desc_timeout, std::move(handler))); } -boost::asio::ip::tcp::endpoint -ircd::net::socket::local() -const -{ - return sd.local_endpoint(); -} - -boost::asio::ip::tcp::endpoint -ircd::net::socket::remote() -const -{ - return sd.remote_endpoint(); -} - ircd::net::socket::operator SSL &() { diff --git a/ircd/net_listener.cc b/ircd/net_listener.cc index e8a399419..24c670852 100644 --- a/ircd/net_listener.cc +++ b/ircd/net_listener.cc @@ -519,6 +519,7 @@ try std::bind(&acceptor::accept, this, ph::_1, sock) }; + sock->local = ep; ip::tcp::socket &sd(*sock); a.async_accept(sd, ios::handle(accept_desc, std::move(handler))); ++accepting; @@ -545,6 +546,15 @@ noexcept try assert(bool(sock)); assert(accepting > 0); assert(accepting == 1); // for now + + if(likely(sock->sd.is_open())) + sock->remote = sock->sd.remote_endpoint(); + + const auto remote + { + remote_ipport(*sock) + }; + char ecbuf[64]; log::debug { @@ -563,11 +573,6 @@ noexcept try return; } - const auto &remote - { - remote_ipport(*sock) - }; - if(unlikely(!check_handshake_limit(*sock, remote))) { allow(*this);