Browse Source

[2984] skip flag in pkt6_send now skips pack() operation.

Tomek Mrugalski 11 years ago
parent
commit
7d4a538829
2 changed files with 47 additions and 34 deletions
  1. 38 32
      src/bin/dhcp6/dhcp6_srv.cc
  2. 9 2
      src/bin/dhcp6/tests/hook_unittest.cc

+ 38 - 32
src/bin/dhcp6/dhcp6_srv.cc

@@ -331,6 +331,9 @@ bool Dhcpv6Srv::run() {
             rsp->setIndex(query->getIndex());
             rsp->setIface(query->getIface());
 
+            // specifies if server should do the packing
+            bool skip_pack = false;
+
             // Execute all callouts registered for packet6_send
             if (HooksManager::getHooksManager().calloutsPresent(Hooks.hook_index_pkt6_send_)) {
                 CalloutHandlePtr callout_handle = getCalloutHandle(query);
@@ -349,7 +352,7 @@ bool Dhcpv6Srv::run() {
                 // stage means "drop response".
                 if (callout_handle->getSkip()) {
                     LOG_DEBUG(dhcp6_logger, DBG_DHCP6_HOOKS, DHCP6_HOOK_PACKET_SEND_SKIP);
-                    continue;
+                    skip_pack = true;
                 }
             }
 
@@ -357,41 +360,44 @@ bool Dhcpv6Srv::run() {
                       DHCP6_RESPONSE_DATA)
                 .arg(static_cast<int>(rsp->getType())).arg(rsp->toText());
 
-            if (rsp->pack()) {
-                try {
-
-                    // Let's execute all callouts registered for buffer6_send
-                    if (HooksManager::getHooksManager().calloutsPresent(Hooks.hook_index_buffer6_send_)) {
-                        CalloutHandlePtr callout_handle = getCalloutHandle(query);
-
-                        // Delete previously set arguments
-                        callout_handle->deleteAllArguments();
-
-                        // Pass incoming packet as argument
-                        callout_handle->setArgument("response6", rsp);
-
-                        // Call callouts
-                        HooksManager::callCallouts(Hooks.hook_index_buffer6_send_, *callout_handle);
-
-                        // Callouts decided to skip the next processing step. The next
-                        // processing step would to parse the packet, so skip at this
-                        // stage means drop.
-                        if (callout_handle->getSkip()) {
-                            LOG_DEBUG(dhcp6_logger, DBG_DHCP6_HOOKS, DHCP6_HOOK_BUFFER_SEND_SKIP);
-                            continue;
-                        }
+            if (!skip_pack) {
+                if (!rsp->pack()) {
+                    LOG_ERROR(dhcp6_logger, DHCP6_PACK_FAIL);
+                    continue;
+                }
+            }
 
-                        callout_handle->getArgument("response6", rsp);
+            try {
+
+                // Let's execute all callouts registered for buffer6_send
+                if (HooksManager::getHooksManager().calloutsPresent(Hooks.hook_index_buffer6_send_)) {
+                    CalloutHandlePtr callout_handle = getCalloutHandle(query);
+
+                    // Delete previously set arguments
+                    callout_handle->deleteAllArguments();
+
+                    // Pass incoming packet as argument
+                    callout_handle->setArgument("response6", rsp);
+                    
+                    // Call callouts
+                    HooksManager::callCallouts(Hooks.hook_index_buffer6_send_, *callout_handle);
+                    
+                    // Callouts decided to skip the next processing step. The next
+                    // processing step would to parse the packet, so skip at this
+                    // stage means drop.
+                    if (callout_handle->getSkip()) {
+                        LOG_DEBUG(dhcp6_logger, DBG_DHCP6_HOOKS, DHCP6_HOOK_BUFFER_SEND_SKIP);
+                        continue;
                     }
-
-                    sendPacket(rsp);
-                } catch (const std::exception& e) {
-                    LOG_ERROR(dhcp6_logger, DHCP6_PACKET_SEND_FAIL).arg(e.what());
+                    
+                    callout_handle->getArgument("response6", rsp);
                 }
-            } else {
-                LOG_ERROR(dhcp6_logger, DHCP6_PACK_FAIL);
+                
+                sendPacket(rsp);
+            } catch (const std::exception& e) {
+                LOG_ERROR(dhcp6_logger, DHCP6_PACKET_SEND_FAIL).arg(e.what());
             }
-        }
+        } 
     }
 
     return (true);

+ 9 - 2
src/bin/dhcp6/tests/hook_unittest.cc

@@ -910,8 +910,15 @@ TEST_F(HooksDhcpv6SrvTest, skip_pkt6_send) {
     // In particular, it should call registered pkt6_receive callback.
     srv_->run();
 
-    // check that the server dropped the packet and did not produce any response
-    ASSERT_EQ(0, srv_->fake_sent_.size());
+    // check that the server send the packet
+    ASSERT_EQ(1, srv_->fake_sent_.size());
+
+    // but the sent packet should have 0 length (we told the server to
+    // skip pack(), but did not do packing outselves)
+    Pkt6Ptr sent = srv_->fake_sent_.front();
+
+    // The actual size of sent packet should be 0
+    EXPECT_EQ(0, sent->getBuffer().getLength());
 }
 
 // This test checks if subnet6_select callout is triggered and reports