]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#2689] Addressed TCP library
authorFrancis Dupont <fdupont@isc.org>
Thu, 22 Dec 2022 16:21:38 +0000 (17:21 +0100)
committerFrancis Dupont <fdupont@isc.org>
Thu, 22 Dec 2022 16:34:24 +0000 (17:34 +0100)
src/lib/tcp/tcp_connection.cc
src/lib/tcp/tcp_connection.h

index fba602ffc30d5e776a8ed94a281305c6700faa85..f910aff5da58cc6fe173cce93dea6d2c85dc2b35 100644 (file)
@@ -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)");
index 0c8f24294e7cf626e8df75eb439d9cf41ebccd89..8b71d077f83779cdba6d76e83381d0e5241fdc79 100644 (file)
@@ -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.