Browse Source

[4320] Global set of allocated addresses in the ClientContext6.

Marcin Siodelski 9 years ago
parent
commit
17e2afb7f3

+ 17 - 10
src/lib/dhcpsrv/alloc_engine.cc

@@ -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_);
             }
         }
 

+ 20 - 11
src/lib/dhcpsrv/alloc_engine.h

@@ -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.
         ///

+ 6 - 6
src/lib/dhcpsrv/tests/alloc_engine6_unittest.cc

@@ -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

+ 4 - 3
src/lib/dhcpsrv/tests/alloc_engine_utils.cc

@@ -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,

+ 4 - 9
src/lib/dhcpsrv/tests/alloc_engine_utils.h

@@ -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