From: Tomek Mrugalski Date: Tue, 17 Feb 2015 14:07:30 +0000 (+0100) Subject: [3711] increaseAddress() moved from AllocEngine to IOAddress X-Git-Tag: trac3723_base~18^2~9 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=a1c67bd19462b53797c06dab91db5a5c40dc1b5a;p=thirdparty%2Fkea.git [3711] increaseAddress() moved from AllocEngine to IOAddress --- diff --git a/src/lib/asiolink/io_address.cc b/src/lib/asiolink/io_address.cc index e7379fa581..b3937ce36f 100644 --- a/src/lib/asiolink/io_address.cc +++ b/src/lib/asiolink/io_address.cc @@ -170,6 +170,34 @@ IOAddress::subtract(const IOAddress& a, const IOAddress& b) { } } +IOAddress +IOAddress::increaseAddress(const IOAddress& addr) { + // Get a buffer holding an address. + const std::vector& vec = addr.toBytes(); + // Get the address length. + const int len = vec.size(); + + // Since the same array will be used to hold the IPv4 and IPv6 + // address we have to make sure that the size of the array + // we allocate will work for both types of address. + BOOST_STATIC_ASSERT(V4ADDRESS_LEN <= V6ADDRESS_LEN); + uint8_t packed[V6ADDRESS_LEN]; + + // Copy the address. It can be either V4 or V6. + std::memcpy(packed, &vec[0], len); + + // Start increasing the least significant byte + for (int i = len - 1; i >= 0; --i) { + ++packed[i]; + // if we haven't overflowed (0xff -> 0x0), than we are done + if (packed[i] != 0) { + break; + } + } + + return (IOAddress::fromBytes(addr.getFamily(), packed)); +} + } // namespace asiolink } // namespace isc diff --git a/src/lib/asiolink/io_address.h b/src/lib/asiolink/io_address.h index 259b85d7f7..8f0a4b44e0 100644 --- a/src/lib/asiolink/io_address.h +++ b/src/lib/asiolink/io_address.h @@ -235,6 +235,16 @@ public: /// @return IOAddress object that represents the difference static IOAddress subtract(const IOAddress& a, const IOAddress& b); + /// @brief Returns an address increased by one + /// + /// This method works for both IPv4 and IPv6 addresses. For example, + /// increase 192.0.2.255 will become 192.0.3.0. + /// + /// @param addr address to be increased + /// @return address increased by one + static IOAddress + increaseAddress(const IOAddress& addr); + /// \brief Converts IPv4 address to uint32_t /// /// Will throw BadValue exception if that is not IPv4 diff --git a/src/lib/asiolink/tests/io_address_unittest.cc b/src/lib/asiolink/tests/io_address_unittest.cc index d5e286c423..c94d0d5887 100644 --- a/src/lib/asiolink/tests/io_address_unittest.cc +++ b/src/lib/asiolink/tests/io_address_unittest.cc @@ -264,3 +264,22 @@ TEST(IOAddressTest, subtract) { EXPECT_THROW(IOAddress::subtract(addr1, addr6), isc::BadValue); EXPECT_THROW(IOAddress::subtract(addr6, addr1), isc::BadValue); } + +// Test checks whether an address can be increased. +TEST(IOAddressTest, increaseAddr) { + IOAddress addr1("192.0.2.12"); + IOAddress any4("0.0.0.0"); + IOAddress bcast("255.255.255.255"); + IOAddress addr6("2001:db8::ffff:ffff:ffff:ffff"); + IOAddress addr11("::1"); + IOAddress any6("::"); + IOAddress the_last_one("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"); + + EXPECT_EQ("192.0.2.13", IOAddress::increaseAddress(addr1).toText()); + EXPECT_EQ("0.0.0.1", IOAddress::increaseAddress(any4).toText()); + EXPECT_EQ("0.0.0.0", IOAddress::increaseAddress(bcast).toText()); + EXPECT_EQ("2001:db8:0:1::", IOAddress::increaseAddress(addr6).toText()); + EXPECT_EQ("::2", IOAddress::increaseAddress(addr11).toText()); + EXPECT_EQ("::1", IOAddress::increaseAddress(any6).toText()); + EXPECT_EQ("::", IOAddress::increaseAddress(the_last_one).toText()); +} diff --git a/src/lib/dhcpsrv/alloc_engine.cc b/src/lib/dhcpsrv/alloc_engine.cc index 4f5dcd07d4..22016c5100 100644 --- a/src/lib/dhcpsrv/alloc_engine.cc +++ b/src/lib/dhcpsrv/alloc_engine.cc @@ -66,34 +66,6 @@ AllocEngine::IterativeAllocator::IterativeAllocator(Lease::Type lease_type) :Allocator(lease_type) { } -isc::asiolink::IOAddress -AllocEngine::IterativeAllocator::increaseAddress(const isc::asiolink::IOAddress& addr) { - // Get a buffer holding an address. - const std::vector& vec = addr.toBytes(); - // Get the address length. - const int len = vec.size(); - - // Since the same array will be used to hold the IPv4 and IPv6 - // address we have to make sure that the size of the array - // we allocate will work for both types of address. - BOOST_STATIC_ASSERT(V4ADDRESS_LEN <= V6ADDRESS_LEN); - uint8_t packed[V6ADDRESS_LEN]; - - // Copy the address. It can be either V4 or V6. - std::memcpy(packed, &vec[0], len); - - // Start increasing the least significant byte - for (int i = len - 1; i >= 0; --i) { - ++packed[i]; - // if we haven't overflowed (0xff -> 0x0), than we are done - if (packed[i] != 0) { - break; - } - } - - return (IOAddress::fromBytes(addr.getFamily(), packed)); -} - isc::asiolink::IOAddress AllocEngine::IterativeAllocator::increasePrefix(const isc::asiolink::IOAddress& prefix, const uint8_t prefix_len) { @@ -193,7 +165,7 @@ AllocEngine::IterativeAllocator::pickAddress(const SubnetPtr& subnet, IOAddress next("::"); if (!prefix) { - next = increaseAddress(last); // basically addr++ + next = IOAddress::increaseAddress(last); // basically addr++ } else { Pool6Ptr pool6 = boost::dynamic_pointer_cast(*it); if (!pool6) { diff --git a/src/lib/dhcpsrv/alloc_engine.h b/src/lib/dhcpsrv/alloc_engine.h index 418fe8cb70..50e68dd306 100644 --- a/src/lib/dhcpsrv/alloc_engine.h +++ b/src/lib/dhcpsrv/alloc_engine.h @@ -137,16 +137,6 @@ protected: const isc::asiolink::IOAddress& hint); protected: - /// @brief Returns an address increased by one - /// - /// This method works for both IPv4 and IPv6 addresses. For example, - /// increase 192.0.2.255 will become 192.0.3.0. - /// - /// @param addr address to be increased - /// @return address increased by one - static isc::asiolink::IOAddress - increaseAddress(const isc::asiolink::IOAddress& addr); - /// @brief Returns the next prefix /// /// This method works for IPv6 addresses only. It increases