From: Francis Dupont Date: Thu, 22 Dec 2022 16:21:38 +0000 (+0100) Subject: [#2689] Addressed TCP library X-Git-Tag: Kea-2.3.4~82 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=5cffdd7a9cf6f96f795af23491fa5b2e9203d4c2;p=thirdparty%2Fkea.git [#2689] Addressed TCP library --- diff --git a/src/lib/tcp/tcp_connection.cc b/src/lib/tcp/tcp_connection.cc index fba602ffc3..f910aff5da 100644 --- a/src/lib/tcp/tcp_connection.cc +++ b/src/lib/tcp/tcp_connection.cc @@ -300,18 +300,28 @@ TcpConnection::acceptorCallback(const boost::system::error_code& ec) { acceptor_callback_(ec); if (!ec) { - if (connection_filter_) { - // In theory, we should not get here with an unopened socket - // but just in case, we'll check for NO_ENDPOINT. - auto endpoint = getRemoteEndpoint(); - if (endpoint == NO_ENDPOINT() || !connection_filter_(endpoint)) { - LOG_DEBUG(tcp_logger, isc::log::DBGLVL_TRACE_DETAIL, - TCP_CONNECTION_REJECTED_BY_FILTER) - .arg(getRemoteEndpointAddressAsText()); - stopThisConnection(); - TcpConnectionPool::rejected_counter_ += 1; - return; + try { + if (tcp_socket_ && tcp_socket_->getASIOSocket().is_open()) { + remote_endpoint_ = + tcp_socket_->getASIOSocket().remote_endpoint(); + } else if (tls_socket_ && tls_socket_->getASIOSocket().is_open()) { + remote_endpoint_ = + tls_socket_->getASIOSocket().remote_endpoint(); } + } catch (...) { + // Let's it to fail later. + } + + // In theory, we should not get here with an unopened socket + // but just in case, we'll check for NO_ENDPOINT. + if ((remote_endpoint_ == NO_ENDPOINT()) || + (connection_filter_ && !connection_filter_(remote_endpoint_))) { + LOG_DEBUG(tcp_logger, isc::log::DBGLVL_TRACE_DETAIL, + TCP_CONNECTION_REJECTED_BY_FILTER) + .arg(getRemoteEndpointAddressAsText()); + TcpConnectionPool::rejected_counter_ += 1; + stopThisConnection(); + return; } if (!tls_context_) { @@ -495,30 +505,10 @@ TcpConnection::idleTimeoutCallback() { stopThisConnection(); } -const boost::asio::ip::tcp::endpoint -TcpConnection::getRemoteEndpoint() const { - try { - if (tcp_socket_) { - if (tcp_socket_->getASIOSocket().is_open()) { - return (tcp_socket_->getASIOSocket().remote_endpoint()); - } - } else if (tls_socket_) { - if (tls_socket_->getASIOSocket().is_open()) { - return (tls_socket_->getASIOSocket().remote_endpoint()); - } - } - } catch (...) { - } - - return (NO_ENDPOINT()); -} - std::string TcpConnection::getRemoteEndpointAddressAsText() const { - - auto endpoint = getRemoteEndpoint(); - if (endpoint != NO_ENDPOINT()) { - return (endpoint.address().to_string()); + if (remote_endpoint_ != NO_ENDPOINT()) { + return (remote_endpoint_.address().to_string()); } return ("(unknown address)"); diff --git a/src/lib/tcp/tcp_connection.h b/src/lib/tcp/tcp_connection.h index 0c8f24294e..8b71d077f8 100644 --- a/src/lib/tcp/tcp_connection.h +++ b/src/lib/tcp/tcp_connection.h @@ -315,7 +315,9 @@ public: /// /// @return A reference to the endpoint if the socket is open, otherwise /// NO_ENDPOINT. - const boost::asio::ip::tcp::endpoint getRemoteEndpoint() const; + const boost::asio::ip::tcp::endpoint getRemoteEndpoint() const { + return (remote_endpoint_); + } protected: @@ -453,6 +455,9 @@ protected: /// @brief Buffer for a single socket read. WireData input_buf_; + + /// @brief Remote endpoint. + boost::asio::ip::tcp::endpoint remote_endpoint_; }; /// @brief Pointer to the @ref TcpConnection.