]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#3478] Checkpoint: handle acceptor
authorFrancis Dupont <fdupont@isc.org>
Wed, 31 Jul 2024 11:53:01 +0000 (13:53 +0200)
committerFrancis Dupont <fdupont@isc.org>
Wed, 21 Aug 2024 07:58:57 +0000 (09:58 +0200)
src/lib/http/Makefile.am
src/lib/http/connection.cc
src/lib/http/connection.h
src/lib/http/listener.cc
src/lib/http/listener.h
src/lib/http/listener_impl.cc
src/lib/http/listener_impl.h
src/lib/http/tests/Makefile.am
src/lib/http/tests/request_parser_unittests.cc

index 94c55695815edced47aae1f39bd80bcf60b898c7..e71bde1d7cafadf00e50d1f1162f7b9fab61410b 100644 (file)
@@ -49,7 +49,8 @@ libkea_http_la_CPPFLAGS = $(AM_CPPFLAGS)
 libkea_http_la_LDFLAGS  = $(AM_LDFLAGS)
 libkea_http_la_LDFLAGS += -no-undefined -version-info 81:0:0
 
-libkea_http_la_LIBADD  = $(top_builddir)/src/lib/hooks/libkea-hooks.la
+libkea_http_la_LIBADD  = $(top_builddir)/src/lib/dhcp/libkea-dhcp++.la
+libkea_http_la_LIBADD += $(top_builddir)/src/lib/hooks/libkea-hooks.la
 libkea_http_la_LIBADD += $(top_builddir)/src/lib/cc/libkea-cc.la
 libkea_http_la_LIBADD += $(top_builddir)/src/lib/asiolink/libkea-asiolink.la
 libkea_http_la_LIBADD += $(top_builddir)/src/lib/log/libkea-log.la
index 86cc3749cd73835967794acba1930a580131dd82..487566c1988b4c2498c735f6fe178a86fde5629b 100644 (file)
@@ -79,7 +79,8 @@ HttpConnection::HttpConnection(const asiolink::IOServicePtr& io_service,
       acceptor_(acceptor),
       connection_pool_(connection_pool),
       response_creator_(response_creator),
-      acceptor_callback_(callback) {
+      acceptor_callback_(callback),
+      use_external_(false) {
     if (!tls_context) {
         tcp_socket_.reset(new asiolink::TCPSocket<SocketCallback>(io_service));
     } else {
@@ -92,6 +93,11 @@ HttpConnection::~HttpConnection() {
     close();
 }
 
+void
+HttpConnection::addExternalSockets(bool use_external) {
+    use_external_ = use_external;
+}
+
 void
 HttpConnection::recordParameters(const HttpRequestPtr& request) const {
     if (!request) {
index f9e0d43e2710629282788b4adf76bd0f0aa191a5..cea77244512a2ff17d31409c675e23344f932071 100644 (file)
@@ -255,6 +255,14 @@ public:
     /// Closes current connection.
     virtual ~HttpConnection();
 
+    /// @brief Use external sockets flag.
+    ///
+    /// Add sockets as external sockets of the interface manager
+    /// so available I/O on them makes a waiting select to return.
+    ///
+    /// @param use_external True add external sockets (default false).
+    void addExternalSockets(bool use_external = false);
+
     /// @brief Asynchronously accepts new connection.
     ///
     /// When the connection is established successfully, the timeout timer is
@@ -424,6 +432,9 @@ protected:
 
     /// @brief External TCP acceptor callback.
     HttpAcceptorCallback acceptor_callback_;
+
+    /// @brief Use external sockets flag.
+    bool use_external_;
 };
 
 } // end of namespace isc::http
index 544a4b46e7443d5204155309b21c29f1dd1820a7..18f81f529cc9748d053805a3ba47100d133cd7c8 100644 (file)
@@ -47,6 +47,11 @@ HttpListener::getNative() const {
     return (impl_->getNative());
 }
 
+void
+HttpListener::addExternalSockets(bool use_external) {
+    impl_->addExternalSockets(use_external);
+}
+
 void
 HttpListener::start() {
     impl_->start();
index c05a60d1dbc9c66e4f4616a67ee46b4f89675d8a..3d513019c8d1555656658f45052551195d7d25e0 100644 (file)
@@ -118,6 +118,14 @@ public:
     /// @brief file descriptor of the underlying acceptor socket.
     int getNative() const;
 
+    /// @brief Use external sockets flag.
+    ///
+    /// Add sockets as external sockets of the interface manager
+    /// so available I/O on them makes a waiting select to return.
+    ///
+    /// @param use_external True add external sockets (default false).
+    void addExternalSockets(bool use_external = false);
+
     /// @brief Starts accepting new connections.
     ///
     /// This method starts accepting and handling new HTTP connections on
index 771b6e9469517994c20744f13e7d40b720893044..cd68e0901120fcb79fb535eea8ecd6d140683437 100644 (file)
@@ -6,11 +6,13 @@
 
 #include <config.h>
 #include <asiolink/asio_wrapper.h>
+#include <dhcp/iface_mgr.h>
 #include <http/connection_pool.h>
 #include <http/listener.h>
 #include <http/listener_impl.h>
 
 using namespace isc::asiolink;
+using namespace isc::dhcp;
 namespace ph = std::placeholders;
 
 namespace isc {
@@ -26,7 +28,8 @@ HttpListenerImpl::HttpListenerImpl(const IOServicePtr& io_service,
     : io_service_(io_service), tls_context_(tls_context), acceptor_(),
       endpoint_(), connections_(),
       creator_factory_(creator_factory),
-      request_timeout_(request_timeout), idle_timeout_(idle_timeout) {
+      request_timeout_(request_timeout), idle_timeout_(idle_timeout),
+      use_external_(false) {
     // Create the TCP or TLS acceptor.
     if (!tls_context) {
         acceptor_.reset(new HttpAcceptor(io_service));
@@ -72,10 +75,18 @@ HttpListenerImpl::getNative() const {
     return (acceptor_ ? acceptor_->getNative() : -1);
 }
 
+void
+HttpListenerImpl::addExternalSockets(bool use_external) {
+    use_external_ = use_external;
+}
+
 void
 HttpListenerImpl::start() {
     try {
         acceptor_->open(*endpoint_);
+        if (use_external_) {
+            IfaceMgr::instance().addExternalSocket(acceptor_->getNative(), 0);
+        }
         acceptor_->setOption(HttpAcceptor::ReuseAddress(true));
         acceptor_->bind(*endpoint_);
         acceptor_->listen();
@@ -92,6 +103,9 @@ HttpListenerImpl::start() {
 void
 HttpListenerImpl::stop() {
     connections_.stopAll();
+    if (use_external_) {
+        IfaceMgr::instance().deleteExternalSocket(acceptor_->getNative());
+    }
     acceptor_->close();
 }
 
@@ -105,6 +119,10 @@ HttpListenerImpl::accept() {
         std::bind(&HttpListenerImpl::acceptHandler, this, ph::_1);
     HttpConnectionPtr conn = createConnection(response_creator,
                                               acceptor_callback);
+    // Transmit the use external sockets flag.
+    if (use_external_) {
+        conn->addExternalSockets(true);
+    }
     // Add this new connection to the pool.
     connections_.start(conn);
 }
index 1608c945537d1ce0657f23612064fe5776feb803..246ead4e274d6002083934e0ebb76c182fc5ecdf 100644 (file)
@@ -62,6 +62,14 @@ public:
     /// @brief file descriptor of the underlying acceptor socket.
     int getNative() const;
 
+    /// @brief Use external sockets flag.
+    ///
+    /// Add sockets as external sockets of the interface manager
+    /// so available I/O on them makes a waiting select to return.
+    ///
+    /// @param use_external True add external sockets.
+    void addExternalSockets(bool use_external);
+
     /// @brief Starts accepting new connections.
     ///
     /// This method starts accepting and handling new HTTP connections on
@@ -130,6 +138,9 @@ protected:
     /// @brief Timeout after which idle persistent connection is closed by
     /// the server.
     long idle_timeout_;
+
+    /// @brief Use external sockets flag.
+    bool use_external_;
 };
 
 
index fcd3398739403c6798091ca196bb5e08bd2e72c8..92f1bb7c5e4acdb52a707f32b00ccebf4b1f07b6 100644 (file)
@@ -60,6 +60,7 @@ libhttp_unittests_CXXFLAGS = $(AM_CXXFLAGS)
 libhttp_unittests_LDFLAGS  = $(AM_LDFLAGS) $(CRYPTO_LDFLAGS) $(GTEST_LDFLAGS)
 
 libhttp_unittests_LDADD  = $(top_builddir)/src/lib/http/libkea-http.la
+libhttp_unittests_LDADD += $(top_builddir)/src/lib/dhcp/libkea-dhcp++.la
 libhttp_unittests_LDADD += $(top_builddir)/src/lib/hooks/libkea-hooks.la
 libhttp_unittests_LDADD += $(top_builddir)/src/lib/testutils/libkea-testutils.la
 libhttp_unittests_LDADD += $(top_builddir)/src/lib/cc/libkea-cc.la
index 0756711219348498d66b2aad01ec007a42838ea7..8f1701d091bb356acecf9214a2d0c1902c19b00d 100644 (file)
@@ -91,7 +91,7 @@ TEST_F(HttpRequestParserTest, postHttpRequestWithJson) {
     // Simulate receiving HTTP request in chunks.
     for (size_t i = 0; i < http_req.size(); i += http_req.size() / 10) {
         bool done = false;
-        // Get the size of the data chunk. 
+        // Get the size of the data chunk.
         size_t chunk = http_req.size() / 10;
         // When we're near the end of the data stream, the chunk length may
         // vary.