]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[4320] Global set of allocated addresses in the ClientContext6.
authorMarcin Siodelski <marcin@isc.org>
Mon, 6 Jun 2016 13:04:26 +0000 (15:04 +0200)
committerMarcin Siodelski <marcin@isc.org>
Mon, 6 Jun 2016 13:12:29 +0000 (15:12 +0200)
src/lib/dhcpsrv/alloc_engine.cc
src/lib/dhcpsrv/alloc_engine.h
src/lib/dhcpsrv/tests/alloc_engine6_unittest.cc
src/lib/dhcpsrv/tests/alloc_engine_utils.cc
src/lib/dhcpsrv/tests/alloc_engine_utils.h

index 2c508f3a6550b38588b3fb429863bd406b182794..aaae39f8dfd325ba01128d505ba1584537e9b15e 100644 (file)
@@ -349,7 +349,8 @@ AllocEngine::ClientContext6::ClientContext6(const Subnet6Ptr& subnet,
     : query_(query), fake_allocation_(fake_allocation), subnet_(subnet),
       duid_(duid), hwaddr_(), host_identifiers_(), host_(),
       fwd_dns_update_(fwd_dns), rev_dns_update_(rev_dns),
-      hostname_(hostname), callout_handle_(callout_handle), ias_() {
+      hostname_(hostname), callout_handle_(callout_handle),
+      allocated_resources_(), ias_() {
 
     // Initialize host identifiers.
     if (duid) {
@@ -358,8 +359,8 @@ AllocEngine::ClientContext6::ClientContext6(const Subnet6Ptr& subnet,
 }
 
 AllocEngine::ClientContext6::IAContext::IAContext()
-    : iaid_(0), type_(Lease::TYPE_NA), hints_(), allocated_resources_(),
-      old_leases_(), changed_leases_(), ia_rsp_() {
+    : iaid_(0), type_(Lease::TYPE_NA), hints_(), old_leases_(),
+      changed_leases_(), ia_rsp_() {
 }
 
 void
@@ -371,9 +372,17 @@ IAContext::addHint(const asiolink::IOAddress& prefix,
 
 void
 AllocEngine::ClientContext6::
-IAContext::addAllocatedResource(const asiolink::IOAddress& prefix,
-                                const uint8_t prefix_len) {
-    allocated_resources_.push_back(std::make_pair(prefix, prefix_len));
+addAllocatedResource(const asiolink::IOAddress& prefix,
+                     const uint8_t prefix_len) {
+    static_cast<void>(allocated_resources_.insert(std::make_pair(prefix,
+                                                                 prefix_len)));
+}
+
+bool
+AllocEngine::ClientContext6::
+isAllocated(const asiolink::IOAddress& prefix, const uint8_t prefix_len) const {
+    return (static_cast<bool>
+            (allocated_resources_.count(std::make_pair(prefix, prefix_len))));
 }
 
 
@@ -534,8 +543,7 @@ AllocEngine::allocateLeases6(ClientContext6& ctx) {
             // IA context so as they are available when we process subsequent
             // IAs.
             BOOST_FOREACH(Lease6Ptr lease, leases) {
-                ctx.currentIA().addAllocatedResource(lease->addr_,
-                                                     lease->prefixlen_);
+                ctx.addAllocatedResource(lease->addr_, lease->prefixlen_);
             }
             return (leases);
         }
@@ -1212,8 +1220,7 @@ AllocEngine::renewLeases6(ClientContext6& ctx) {
             // IA context so as they are available when we process subsequent
             // IAs.
             BOOST_FOREACH(Lease6Ptr lease, leases) {
-                ctx.currentIA().addAllocatedResource(lease->addr_,
-                                                     lease->prefixlen_);
+                ctx.addAllocatedResource(lease->addr_, lease->prefixlen_);
             }
         }
 
index 01bc568efab7c89d468034c3f4cd5a4daa8543ed..434a2cf0029d2af6876abb14f985cc3893d1ff97 100644 (file)
@@ -254,6 +254,9 @@ public:
     /// @brief Container for client's hints.
     typedef std::vector<ResourceType> HintContainer;
 
+    /// @brief Container holding allocated prefixes or addresses.
+    typedef std::set<ResourceType> ResourceContainer;
+
     /// @brief A tuple holding host identifier type and value.
     typedef std::pair<Host::IdentifierType, std::vector<uint8_t> > IdentifierPair;
 
@@ -336,6 +339,9 @@ public:
         /// @brief Callout handle associated with the client's message.
         hooks::CalloutHandlePtr callout_handle_;
 
+        /// @brief Holds addresses and prefixes allocated for all IAs.
+        ResourceContainer allocated_resources_;
+
         //@}
 
         /// @brief Parameters pertaining to individual IAs.
@@ -354,9 +360,6 @@ public:
             /// allows more than one address or prefix for each IA container.
             HintContainer hints_;
 
-            /// @brief Holds addresses or prefixes allocated for this IA.
-            HintContainer allocated_resources_;
-
             /// @brief A pointer to any old leases that the client had before
             /// update but are no longer valid after the update/allocation.
             ///
@@ -389,19 +392,25 @@ public:
             /// @param prefix_len Prefix length. Default is 128 for addresses.
             void addHint(const asiolink::IOAddress& prefix,
                          const uint8_t prefix_len = 128);
-
-            /// @brief Convenience method adding allocated prefix or address.
-            ///
-            /// @param prefix Prefix or address.
-            /// @param prefix_len Prefix length. Default is 128 for addresses.
-            void addAllocatedResource(const asiolink::IOAddress& prefix,
-                                      const uint8_t prefix_len = 128);
-
         };
 
         /// @brief Container holding IA specific contexts.
         std::vector<IAContext> ias_;
 
+        /// @brief Convenience method adding allocated prefix or address.
+        ///
+        /// @param prefix Prefix or address.
+        /// @param prefix_len Prefix length. Default is 128 for addresses.
+        void addAllocatedResource(const asiolink::IOAddress& prefix,
+                                  const uint8_t prefix_len = 128);
+
+        /// @brief Checks if specified address or prefix was allocated.
+        ///
+        /// @param prefix Prefix or address.
+        /// @param prefix_len Prefix length. Default is 128 for addresses.
+        bool isAllocated(const asiolink::IOAddress& prefix,
+                         const uint8_t prefix_len = 128) const;
+
         /// @brief Conveniece function adding host identifier into
         /// @ref host_identifiers_ list.
         ///
index 638658b1119b327a28dd034b52d94dd30062a2b3..7cd707a4dc7e5bb80d21db4dabcc4df9523e9eea 100644 (file)
@@ -32,15 +32,15 @@ TEST(ClientContext6Test, addHint) {
 }
 
 // Test convenience method adding allocated prefixes and addresses to
-// IA context.
+// a context.
 TEST(ClientContext6Test, addAllocatedResource) {
    AllocEngine::ClientContext6 ctx;
-   ctx.currentIA().addAllocatedResource(IOAddress("2001:db8:1::1"));
-   ctx.currentIA().addAllocatedResource(IOAddress("3000:1::"), 64);
+   ctx.addAllocatedResource(IOAddress("2001:db8:1::1"));
+   ctx.addAllocatedResource(IOAddress("3000:1::"), 64);
 
-   ASSERT_EQ(2, ctx.currentIA().allocated_resources_.size());
-   EXPECT_EQ("2001:db8:1::1", ctx.currentIA().allocated_resources_[0].first.toText());
-   EXPECT_EQ("3000:1::", ctx.currentIA().allocated_resources_[1].first.toText());
+   ASSERT_EQ(2, ctx.allocated_resources_.size());
+   EXPECT_TRUE(ctx.isAllocated(IOAddress("2001:db8:1::1")));
+   EXPECT_TRUE(ctx.isAllocated(IOAddress("3000:1::"), 64));
 }
 
 // This test checks if the v6 Allocation Engine can be instantiated, parses
index 9eaafe9df64689602d30ca77d1ba3c43528d9966..8f86f6a5e978c2ef8818811f0e692ffa6ddf296c 100644 (file)
@@ -25,7 +25,6 @@
 #include <boost/scoped_ptr.hpp>
 
 #include <iostream>
-#include <iterator>
 #include <sstream>
 #include <algorithm>
 #include <set>
@@ -227,7 +226,9 @@ AllocEngine6Test::allocateTest(AllocEngine& engine, const Pool6Ptr& pool,
         // Do all checks on the lease
         checkLease6(*it, type, expected_len, in_pool, in_pool);
 
-        checkAllocatedResources(*it, ctx, std::distance(leases.begin(), it));
+        // Check that context has been updated with allocated addresses or
+        // prefixes.
+        checkAllocatedResources(*it, ctx);
 
         // Check that the lease is indeed in LeaseMgr
         Lease6Ptr from_mgr = LeaseMgrFactory::instance().getLease6(type,
@@ -342,7 +343,7 @@ AllocEngine6Test::renewTest(AllocEngine& engine, const Pool6Ptr& pool,
 
         // Check that context has been updated with allocated addresses or
         // prefixes.
-        checkAllocatedResources(*it, ctx, std::distance(leases.begin(), it));
+        checkAllocatedResources(*it, ctx);
 
         // Check that the lease is indeed in LeaseMgr
         Lease6Ptr from_mgr = LeaseMgrFactory::instance().getLease6(type,
index 0fcb39348e2c1f9498a5e15d68e1847e945bf8c8..b2473fe7e93b21aef7559474a59eba57e8b113ce 100644 (file)
@@ -190,20 +190,15 @@ public:
         /// @todo: check cltt
     }
 
-    /// @brief Checks if context has been updated with allocated addresses
-    /// or prefixes.
+    /// @brief Checks if specified address or prefix has been recorded as
+    /// allocated to the client.
     ///
     /// @param lease Allocated lease.
     /// @param ctx Context structure in which this function should check if
     /// leased address is stored as allocated resource.
-    /// @param lease_index Index of the lease within IA.
     void checkAllocatedResources(const Lease6Ptr& lease,
-                                 AllocEngine::ClientContext6& ctx,
-                                 const size_t lease_index) {
-        ASSERT_GE(ctx.currentIA().allocated_resources_.size(), lease_index + 1);
-        EXPECT_EQ(lease->addr_, ctx.currentIA().allocated_resources_[lease_index].first);
-        EXPECT_EQ(static_cast<int>(lease->prefixlen_),
-                  static_cast<int>(ctx.currentIA().allocated_resources_[lease_index].second));
+                                 AllocEngine::ClientContext6& ctx) {
+        EXPECT_TRUE(ctx.isAllocated(lease->addr_, lease->prefixlen_));
     }
 
     /// @brief Checks if specified address is increased properly