From ed8a874e740a95f78eba3f4a133b83794490dea7 Mon Sep 17 00:00:00 2001 From: Marcin Siodelski Date: Fri, 14 Dec 2012 19:46:24 +0100 Subject: [2549] Changes as a result of the code review. --- src/lib/dhcpsrv/addr_utilities.cc | 8 +++++++- src/lib/dhcpsrv/alloc_engine.cc | 23 ++++++++++++----------- 2 files changed, 19 insertions(+), 12 deletions(-) (limited to 'src/lib/dhcpsrv') diff --git a/src/lib/dhcpsrv/addr_utilities.cc b/src/lib/dhcpsrv/addr_utilities.cc index 24a0633f4a..98fbeb93d0 100644 --- a/src/lib/dhcpsrv/addr_utilities.cc +++ b/src/lib/dhcpsrv/addr_utilities.cc @@ -53,8 +53,11 @@ isc::asiolink::IOAddress firstAddrInPrefix6(const isc::asiolink::IOAddress& pref } // First we copy the whole address as 16 bytes. + // We don't check that it is a valid IPv6 address and thus has + // the required length because it is already checked by + // the calling function. uint8_t packed[V6ADDRESS_LEN]; - memcpy(packed, &prefix.toBytes()[0], 16); + memcpy(packed, &prefix.toBytes()[0], V6ADDRESS_LEN); // If the length is divisible by 8, it is simple. We just zero out the host // part. Otherwise we need to handle the byte that has to be partially @@ -95,6 +98,9 @@ isc::asiolink::IOAddress firstAddrInPrefix4(const isc::asiolink::IOAddress& pref isc_throw(isc::BadValue, "Too large netmask. 0..32 is allowed in IPv4"); } + // We don't check that it is a valid IPv4 address and thus has + // a required length of 4 bytes because it has been already + // checked by the calling function. uint32_t addr = prefix; return (IOAddress(addr & (~bitMask4[len]))); } diff --git a/src/lib/dhcpsrv/alloc_engine.cc b/src/lib/dhcpsrv/alloc_engine.cc index 3d57a2f5a6..452343cb2e 100644 --- a/src/lib/dhcpsrv/alloc_engine.cc +++ b/src/lib/dhcpsrv/alloc_engine.cc @@ -30,20 +30,21 @@ AllocEngine::IterativeAllocator::IterativeAllocator() 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]; - int len; - // First we copy the whole address as 16 bytes. - if (addr.isV4()) { - // IPv4 - std::memcpy(packed, &addr.toBytes()[0], 4); - len = 4; - } else { - // IPv6 - std::memcpy(packed, &addr.toBytes()[0], 16); - len = 16; - } + // Copy the address. It can be either V4 or V6. + std::memcpy(packed, &vec[0], len); + // Increase the address. for (int i = len - 1; i >= 0; --i) { ++packed[i]; if (packed[i] != 0) { -- cgit v1.2.3