Commit a1c67bd1 authored by Tomek Mrugalski's avatar Tomek Mrugalski 🛰
Browse files

[3711] increaseAddress() moved from AllocEngine to IOAddress

parent e6b7f3c4
......@@ -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<uint8_t>& 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
......@@ -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
......
......@@ -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());
}
......@@ -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<uint8_t>& 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<Pool6>(*it);
if (!pool6) {
......
......@@ -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
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment