]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[5374] Updated asiolink
authorFrancis Dupont <fdupont@isc.org>
Thu, 29 Mar 2018 13:41:28 +0000 (15:41 +0200)
committerFrancis Dupont <fdupont@isc.org>
Thu, 29 Mar 2018 13:41:28 +0000 (15:41 +0200)
src/lib/asiolink/io_acceptor.h
src/lib/asiolink/io_address.h
src/lib/asiolink/io_asio_socket.h
src/lib/asiolink/io_service.h
src/lib/asiolink/tcp_socket.h
src/lib/asiolink/tests/io_service_unittest.cc
src/lib/asiolink/tests/run_unittests.cc
src/lib/asiolink/udp_socket.h
src/lib/asiolink/unix_domain_socket.cc

index c493d3427e1ff0d8fd3974606cdbb3f5422e8ab0..913a3280b262c7035252abb6b85d3b5e3f1b6e2e 100644 (file)
@@ -47,7 +47,11 @@ public:
 
     /// @brief Returns file descriptor of the underlying socket.
     virtual int getNative() const {
+#if BOOST_VERSION < 106600
         return (acceptor_->native());
+#else
+        return (acceptor_->native_handle());
+#endif
     }
 
     /// @brief Opens acceptor socket given the endpoint.
index 5747cb9291ec5b07f23e963933c0f996af3ca263..42f09d7a9f7d3e7d12463973183660cc071107ad 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2010-2017 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2010-2018 Internet Systems Consortium, Inc. ("ISC")
 //
 // This Source Code Form is subject to the terms of the Mozilla Public
 // License, v. 2.0. If a copy of the MPL was not distributed with this
 namespace isc {
 namespace asiolink {
 
-    /// Defines length of IPv6 address.
-    const static size_t V6ADDRESS_LEN = 16;
+    /// Defines length of IPv6 address (in binary format).
+    static constexpr size_t V6ADDRESS_LEN = 16;
 
-    /// Defines length of IPv4 address.
-    const static size_t V4ADDRESS_LEN = 4;
+    /// Defines length of IPv4 address (in binary format).
+    static constexpr size_t V4ADDRESS_LEN = 4;
+
+    /// @brief Maximum size of an IPv4 address represented as a text string. 12
+    ///     digits plus 3 full stops (dots).
+    static constexpr size_t V4ADDRESS_TEXT_MAX_LEN = 15u;
+
+    /// @brief Maximum size of an IPv6 address represented as a text string. 32
+    ///     hexadecimal characters written in 8 groups of four, plus 7 colon
+    ///     separators.
+    static constexpr size_t V6ADDRESS_TEXT_MAX_LEN = 39u;
 
 /// \brief The \c IOAddress class represents an IP addresses (version
 /// agnostic)
@@ -136,7 +145,7 @@ public:
 
     /// \brief Creates an address from over wire data.
     ///
-    /// \param family AF_NET for IPv4 or AF_NET6 for IPv6.
+    /// \param family AF_INET for IPv4 or AF_INET6 for IPv6.
     /// \param data pointer to first char of data
     ///
     /// \return Created IOAddress object
index cdb4074db27f275888aee9b7b409c6805870ecf1..49f5041db47fccc0ce2f795c2b5953af0406d299 100644 (file)
 #include <string>
 
 #include <exceptions/exceptions.h>
-#include <coroutine.h>
 
 #include <util/buffer.h>
 
 #include <asiolink/io_error.h>
 #include <asiolink/io_socket.h>
 
+#include <boost/asio/coroutine.hpp>
+
 namespace isc {
 namespace asiolink {
 
index e9e402d1148e4fcd8e912234463eb6b2efd60a15..e0832b2c0498a52a8843fbefa0f61f379d8019b7 100644 (file)
 
 namespace boost {
 namespace asio {
+#if BOOST_VERSION < 106600
     class io_service;
+#else
+    class io_context;
+    typedef io_context io_service;
+#endif
 }
 }
 
index adf74d1f0f2fc12c3acb3b34fd11d23ce666d149..f1297e06300fa6568c427c62e036083bf2c9ff2e 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2011-2017 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2011-2018 Internet Systems Consortium, Inc. ("ISC")
 //
 // This Source Code Form is subject to the terms of the Mozilla Public
 // License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -75,7 +75,11 @@ public:
 
     /// \brief Return file descriptor of underlying socket
     virtual int getNative() const {
+#if BOOST_VERSION < 106600
         return (socket_.native());
+#else
+        return (socket_.native_handle());
+#endif
     }
 
     /// \brief Return protocol of socket
@@ -90,6 +94,48 @@ public:
         return (false);
     }
 
+    /// \brief Checks if the connection is usable.
+    ///
+    /// The connection is usable if the socket is open and the peer has not
+    /// closed its connection.
+    ///
+    /// \return true if the connection is usable.
+    bool isUsable() const {
+        // If the socket is open it doesn't mean that it is still usable. The connection
+        // could have been closed on the other end. We have to check if we can still
+        // use this socket.
+        if (socket_.is_open()) {
+            // Remember the current non blocking setting.
+            const bool non_blocking_orig = socket_.non_blocking();
+            // Set the socket to non blocking mode. We're going to test if the socket
+            // returns would_block status on the attempt to read from it.
+            socket_.non_blocking(true);
+
+            boost::system::error_code ec;
+            char data[2];
+
+            // Use receive with message peek flag to avoid removing the data awaiting
+            // to be read.
+            socket_.receive(boost::asio::buffer(data, sizeof(data)),
+                            boost::asio::socket_base::message_peek,
+                            ec);
+
+            // Revert the original non_blocking flag on the socket.
+            socket_.non_blocking(non_blocking_orig);
+
+            // If the connection is alive we'd typically get would_block status code.
+            // If there are any data that haven't been read we may also get success
+            // status. We're guessing that try_again may also be returned by some
+            // implementations in some situations. Any other error code indicates a
+            // problem with the connection so we assume that the connection has been
+            // closed.
+            return (!ec || (ec.value() == boost::asio::error::try_again) ||
+                    (ec.value() == boost::asio::error::would_block));
+        }
+
+        return (false);
+    }
+
     /// \brief Open Socket
     ///
     /// Opens the TCP socket.  This is an asynchronous operation, completion of
@@ -227,7 +273,11 @@ TCPSocket<C>::~TCPSocket()
 
 template <typename C> void
 TCPSocket<C>::open(const IOEndpoint* endpoint, C& callback) {
-
+    // If socket is open on this end but has been closed by the peer,
+    // we need to reconnect.
+    if (socket_.is_open() && !isUsable()) {
+        close();
+    }
     // Ignore opens on already-open socket.  Don't throw a failure because
     // of uncertainties as to what precedes whan when using asynchronous I/O.
     // At also allows us a treat a passed-in socket as a self-managed socket.
index 70887c3d243bb1722b3ec17751570ceb75f72ae5..b1c33956be4fe48212db98b9bdde23bb4e7a4f0a 100644 (file)
@@ -4,6 +4,8 @@
 // License, v. 2.0. If a copy of the MPL was not distributed with this
 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
+#include <config.h>
+
 #include <asiolink/io_service.h>
 
 #include <gtest/gtest.h>
index 7044863677f15b351528e7521c21f6c3b0b42498..395dcd1d6e20f250cdcf13e71f81a16fccc9ce0b 100644 (file)
@@ -4,6 +4,8 @@
 // License, v. 2.0. If a copy of the MPL was not distributed with this
 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
+#include <config.h>
+
 #include <gtest/gtest.h>
 #include <util/unittests/run_all.h>
 #include <log/logger_manager.h>
index 07ba44743e1808a9150149b1d1dd6f81cec807c7..5b040cfe1b2dc023c2a639fb4621f01b244cdf79 100644 (file)
@@ -61,7 +61,11 @@ public:
 
     /// \brief Return file descriptor of underlying socket
     virtual int getNative() const {
+#if BOOST_VERSION < 106600
         return (socket_.native());
+#else
+        return (socket_.native_handle());
+#endif
     }
 
     /// \brief Return protocol of socket
index f17ec2e8f73f5b4452ab65c94892c86a56505be7..d1ad9ec30ca81570fccc31c4678b07cc68163f44 100644 (file)
@@ -287,7 +287,11 @@ UnixDomainSocket::UnixDomainSocket(IOService& io_service)
 
 int
 UnixDomainSocket::getNative() const {
+#if BOOST_VERSION < 106600
     return (impl_->socket_.native());
+#else
+    return (impl_->socket_.native_handle());
+#endif
 }
 
 int