]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#1283] Added (and used) IOAddress hash
authorFrancis Dupont <fdupont@isc.org>
Fri, 19 Jun 2020 22:28:07 +0000 (00:28 +0200)
committerFrancis Dupont <fdupont@isc.org>
Thu, 23 Jul 2020 19:07:51 +0000 (21:07 +0200)
src/lib/asiolink/io_address.cc
src/lib/asiolink/io_address.h
src/lib/asiolink/tests/io_address_unittest.cc
src/lib/dhcpsrv/shared_network.h
src/lib/dhcpsrv/subnet.h

index dae8074066655b75ed10e8016e75475e067c91d6..a9eb587c337cbd6475a50422bcd500ffd6fd7555 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2010-2016 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2010-2020 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
@@ -11,6 +11,7 @@
 #include <exceptions/exceptions.h>
 
 #include <boost/static_assert.hpp>
+#include <boost/container_hash/hash.hpp>
 
 #include <unistd.h>             // for some IPC/network system calls
 #include <stdint.h>
@@ -172,6 +173,11 @@ IOAddress::increase(const IOAddress& addr) {
     return (IOAddress::fromBytes(addr.getFamily(), &packed[0]));
 }
 
+std::size_t
+hash_value(const IOAddress& address) {
+    boost::hash<std::vector<uint8_t> > hasher;
+    return (hasher(address.toBytes()));
+}
 
 } // namespace asiolink
 } // namespace isc
index bb2ce692d83c8118ab8a9bfb4fb06c787251c0fd..91e415dd9293a53534e79af1683fc618ee76554c 100644 (file)
@@ -302,6 +302,14 @@ private:
 std::ostream&
 operator<<(std::ostream& os, const IOAddress& address);
 
+/// \brief Hash the IOAddress.
+///
+/// This method allows boost multi-index hashed indexes on IOAddresses.
+///
+/// \param address A \c IOAddress to hash.
+/// \return The hash of the IOAddress.
+std::size_t hash_value(const IOAddress& address);
+
 } // namespace asiolink
 } // namespace isc
 #endif // IO_ADDRESS_H
index ad6faec87d4a9910686a1ddba41b1be73755dc52..503b0249ca5a6c04db80a943afeda00ecbc44271 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2011-2016 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2011-2020 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
@@ -339,3 +339,14 @@ TEST(IOAddressTest, increaseAddr) {
     EXPECT_EQ(IOAddress("::1"), IOAddress::increase(any6));
     EXPECT_EQ(IOAddress("::"), IOAddress::increase(the_last_one));
 }
+
+// Test verifies the hash function is sane.
+TEST(IOAddressTest, hash) {
+    size_t hash1 = hash_value(IOAddress("192.0.2.1"));
+    size_t hash2 = hash_value(IOAddress("192.0.2.2"));
+    EXPECT_NE(hash1, hash2);
+
+    hash1 = hash_value(IOAddress("2001:db8::12"));
+    hash2 = hash_value(IOAddress("2001:db8::1234"));
+    EXPECT_NE(hash1, hash2);
+}
index d12bb623392acf2ee4ba96fd6bffc98e9d35f298..cc9d712e795cdafc4d9cd4ab683d84a033ccd978 100644 (file)
@@ -243,7 +243,7 @@ typedef boost::multi_index_container<
         >,
         // Fourth index allows for access by server identifier specified for the
         // network.
-        boost::multi_index::ordered_non_unique<
+        boost::multi_index::hashed_non_unique<
             boost::multi_index::tag<SharedNetworkServerIdIndexTag>,
             boost::multi_index::const_mem_fun<Network4, asiolink::IOAddress,
                                               &Network4::getServerId>
index e2ac1b364842257cb20973f49f0f1099169085d3..77ce70d0988b0394a791c73204df047f41893d75 100644 (file)
@@ -16,8 +16,9 @@
 #include <dhcpsrv/pool.h>
 #include <dhcpsrv/subnet_id.h>
 #include <dhcpsrv/triplet.h>
-#include <boost/multi_index/mem_fun.hpp>
+#include <boost/multi_index/hashed_index.hpp>
 #include <boost/multi_index/indexed_by.hpp>
+#include <boost/multi_index/mem_fun.hpp>
 #include <boost/multi_index/ordered_index.hpp>
 #include <boost/multi_index/random_access_index.hpp>
 #include <boost/multi_index_container.hpp>
@@ -861,7 +862,7 @@ typedef boost::multi_index_container<
         >,
 
         // Fourth index allows for searching using an output from getServerId.
-        boost::multi_index::ordered_non_unique<
+        boost::multi_index::hashed_non_unique<
             boost::multi_index::tag<SubnetServerIdIndexTag>,
             boost::multi_index::const_mem_fun<Network4, asiolink::IOAddress,
                                               &Network4::getServerId>