From: Francis Dupont Date: Fri, 19 Jun 2020 22:28:07 +0000 (+0200) Subject: [#1283] Added (and used) IOAddress hash X-Git-Tag: Kea-1.7.10~33 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d30af02ceaebcbea2dcd41fbcd9e98a6fd518f78;p=thirdparty%2Fkea.git [#1283] Added (and used) IOAddress hash --- diff --git a/src/lib/asiolink/io_address.cc b/src/lib/asiolink/io_address.cc index dae8074066..a9eb587c33 100644 --- a/src/lib/asiolink/io_address.cc +++ b/src/lib/asiolink/io_address.cc @@ -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 #include +#include #include // for some IPC/network system calls #include @@ -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 > hasher; + return (hasher(address.toBytes())); +} } // namespace asiolink } // namespace isc diff --git a/src/lib/asiolink/io_address.h b/src/lib/asiolink/io_address.h index bb2ce692d8..91e415dd92 100644 --- a/src/lib/asiolink/io_address.h +++ b/src/lib/asiolink/io_address.h @@ -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 diff --git a/src/lib/asiolink/tests/io_address_unittest.cc b/src/lib/asiolink/tests/io_address_unittest.cc index ad6faec87d..503b0249ca 100644 --- a/src/lib/asiolink/tests/io_address_unittest.cc +++ b/src/lib/asiolink/tests/io_address_unittest.cc @@ -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); +} diff --git a/src/lib/dhcpsrv/shared_network.h b/src/lib/dhcpsrv/shared_network.h index d12bb62339..cc9d712e79 100644 --- a/src/lib/dhcpsrv/shared_network.h +++ b/src/lib/dhcpsrv/shared_network.h @@ -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, boost::multi_index::const_mem_fun diff --git a/src/lib/dhcpsrv/subnet.h b/src/lib/dhcpsrv/subnet.h index e2ac1b3648..77ce70d098 100644 --- a/src/lib/dhcpsrv/subnet.h +++ b/src/lib/dhcpsrv/subnet.h @@ -16,8 +16,9 @@ #include #include #include -#include +#include #include +#include #include #include #include @@ -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, boost::multi_index::const_mem_fun