]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#2764] Hashing operator for IOAddress
authorMarcin Siodelski <marcin@isc.org>
Fri, 17 Feb 2023 07:14:26 +0000 (08:14 +0100)
committerMarcin Siodelski <msiodelski@gmail.com>
Tue, 14 Mar 2023 18:23:31 +0000 (19:23 +0100)
To store the addresses in the unordered containers.

src/lib/asiolink/io_address.cc
src/lib/asiolink/io_address.h
src/lib/asiolink/tests/io_address_unittest.cc

index 7f7134d534a169a472a5d6b4219505943deeea4d..b17ec63acfa70658f088fc9e46d2ed202cfcb31d 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2010-2020 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2010-2023 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
@@ -28,6 +28,11 @@ using namespace std;
 namespace isc {
 namespace asiolink {
 
+size_t
+IOAddress::Hash::operator()(const IOAddress &io_address) const {
+    return (hash_value(io_address));
+}
+
 // XXX: we cannot simply construct the address in the initialization list,
 // because we'd like to throw our own exception on failure.
 IOAddress::IOAddress(const std::string& address_str) {
index fb1d67b3ea4d462dcec472b3085f4cd16a56ee34..d08753b5cfab795cbf0c7cf22db9dbc6c573eb85 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2010-2020 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2010-2023 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
@@ -44,6 +44,17 @@ namespace asiolink {
 /// This class is a wrapper for the ASIO \c ip::address class.
 class IOAddress {
 public:
+
+    /// \brief An \c IOAddress hash enabling the use in the unordered
+    /// STL containers.
+    struct Hash {
+        /// \brief A hashing operator.
+        ///
+        /// \param io_address an address to be hashed.
+        /// \return a hashing result.
+        size_t operator()(const IOAddress &io_address) const;
+    };
+
     ///
     /// \name Constructors and Destructor
     ///
index 79888d29a6437b6f672c642d09ef095360c18dd6..b1139402df2c9340f6a1d555e48bb2e00ff0461f 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2011-2020 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2011-2023 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
 #include <cstring>
 #include <vector>
 #include <sstream>
+#include <unordered_set>
 
 using namespace isc::asiolink;
 
+TEST(IOAddressHashTest, hashIPv4) {
+    IOAddress::Hash hash;
+    std::unordered_set<size_t> results;
+    for (uint32_t i = 0; i < 10; i++) {
+        IOAddress address(i);
+        auto result = hash(address);
+        results.insert(result);
+    }
+    // Make sure that the hashing function generated a unique hash for
+    // each address.
+    EXPECT_EQ(10, results.size());
+}
+
+TEST(IOAddressHashTest, hashIPv6) {
+    IOAddress::Hash hash;
+    std::unordered_set<size_t> results;
+    for (auto i = 0; i < 10; i++) {
+        std::ostringstream s;
+        s << "2001:db8:" << i << "::ffff";
+        IOAddress address(s.str());
+        auto result = hash(address);
+        results.insert(result);
+    }
+    // Make sure that the hashing function generated a unique hash for
+    // each address.
+    EXPECT_EQ(10, results.size());
+}
+
 TEST(IOAddressTest, fromText) {
     IOAddress io_address_v4("192.0.2.1");
     EXPECT_EQ("192.0.2.1", io_address_v4.toText());