From: Marcin Siodelski Date: Fri, 17 Feb 2023 07:14:26 +0000 (+0100) Subject: [#2764] Hashing operator for IOAddress X-Git-Tag: Kea-2.3.6~92 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=78cc08bd034bcd1b949c823a816ac8c12e2cd045;p=thirdparty%2Fkea.git [#2764] Hashing operator for IOAddress To store the addresses in the unordered containers. --- diff --git a/src/lib/asiolink/io_address.cc b/src/lib/asiolink/io_address.cc index 7f7134d534..b17ec63acf 100644 --- a/src/lib/asiolink/io_address.cc +++ b/src/lib/asiolink/io_address.cc @@ -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) { diff --git a/src/lib/asiolink/io_address.h b/src/lib/asiolink/io_address.h index fb1d67b3ea..d08753b5cf 100644 --- a/src/lib/asiolink/io_address.h +++ b/src/lib/asiolink/io_address.h @@ -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 /// diff --git a/src/lib/asiolink/tests/io_address_unittest.cc b/src/lib/asiolink/tests/io_address_unittest.cc index 79888d29a6..b1139402df 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-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 @@ -15,9 +15,38 @@ #include #include #include +#include using namespace isc::asiolink; +TEST(IOAddressHashTest, hashIPv4) { + IOAddress::Hash hash; + std::unordered_set 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 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());