-// 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
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) {
-// 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
/// 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
///
-// 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());