Browse Source

[3186] Implemented initial callouts for subnet_select

Added initial callout functions and ability to load the user_chk hooks library.
Thomas Markwalder 11 years ago
parent
commit
c95421cd2f

+ 1 - 0
src/hooks/dhcp/user_chk/Makefile.am

@@ -32,6 +32,7 @@ CLEANFILES = *.gcno *.gcda
 lib_LTLIBRARIES = libdhcp_user_chk.la
 libdhcp_user_chk_la_SOURCES  =
 libdhcp_user_chk_la_SOURCES += load_unload.cc
+libdhcp_user_chk_la_SOURCES += subnet_select_co.cc
 libdhcp_user_chk_la_SOURCES += user.cc user.h
 libdhcp_user_chk_la_SOURCES += user_data_source.cc user_data_source.h
 libdhcp_user_chk_la_SOURCES += user_file.cc user_file.h

+ 18 - 2
src/hooks/dhcp/user_chk/load_unload.cc

@@ -14,18 +14,34 @@
 // load_unload.cc
 
 #include <hooks/hooks.h>
+#include <user_registry.h>
+#include <user_file.h>
 
 using namespace isc::hooks;
 
+UserRegistryPtr user_registry;
+
 extern "C" {
 
 int load(LibraryHandle&) {
-    // @todo instantiate registry here
+    // @todo what about exception handling
+
+    // Instantiate the registry.
+    user_registry.reset(new UserRegistry());
+
+    // Create the data source.
+    UserDataSourcePtr user_file(new UserFile("/tmp/user_registry.txt"));
+
+    // Set the registry's data source
+    user_registry->setSource(user_file);
+
+    // Do an initial load of the registry.
+    user_registry->refresh();
     return (0);
 }
 
 int unload() {
-    // @todo destruct registry here
+    user_registry.reset();
     return (0);
 }
 

+ 94 - 0
src/hooks/dhcp/user_chk/subnet_select_co.cc

@@ -0,0 +1,94 @@
+#include <hooks/hooks.h>
+#include <dhcp/pkt4.h>
+#include <dhcp/dhcp6.h>
+#include <dhcp/pkt6.h>
+#include <user_registry.h>
+
+extern UserRegistryPtr user_registry;
+
+#include <string>
+
+using namespace isc::dhcp;
+using namespace isc::hooks;
+using namespace std;
+
+extern "C" {
+
+// This callout is called at the "subnet4_select" hook.
+int subnet4_select(CalloutHandle& handle) {
+    if (!user_registry) {
+        std::cout << "UserRegistry is null!" << std::endl;
+    }
+
+    try {
+        // Refresh the registry.
+        user_registry->refresh();
+
+        // Get the HWAddress as the user identifier.
+        Pkt4Ptr query;
+        handle.getArgument("query4", query);
+        HWAddrPtr hwaddr = query->getHWAddr();
+
+        // Look for the user.
+        UserPtr registered_user = user_registry->findUser(*hwaddr);
+        if (registered_user) {
+            //@todo give them an unrestricted subnet
+            std::cout << "DHCP4 User is registered! :" 
+                      << registered_user->getUserId() << std::endl;
+        } else {
+            //@todo give them a restricted subnet
+           std::cout << "DHCP4 User is NOT registered! :" 
+                     << hwaddr->toText() << std::endl;
+        }
+    } catch (const std::exception& ex) {
+        std::cout << "Exception in subnet4_select callout:" << ex.what() 
+                  << std::endl;
+            
+    }
+
+    return (0);
+}
+
+// This callout is called at the "subnet6_select" hook.
+int subnet6_select(CalloutHandle& handle) {
+    if (!user_registry) {
+        std::cout << "UserRegistry is null!" << std::endl;
+    }
+
+    try {
+        // Refresh the registry.
+        user_registry->refresh();
+
+        // Get the HWAddress as the user identifier.
+        Pkt6Ptr query;
+        handle.getArgument("query6", query);
+
+        DuidPtr duid;
+        OptionPtr opt_duid = query->getOption(D6O_CLIENTID);
+        if (opt_duid) {
+            duid = DuidPtr(new DUID(opt_duid->getData()));
+        } else {
+            std::cout << "DHCP6 query is missing DUID" << std::endl;
+        }
+
+        // Look for the user.
+        UserPtr registered_user = user_registry->findUser(*duid);
+        if (registered_user) {
+            //@todo give them an unrestricted subnet
+            std::cout << "DHCP6 User is registered! :" 
+                      << registered_user->getUserId() << std::endl;
+        } else {
+            //@todo give them a restricted subnet
+           std::cout << "DHCP6 User is NOT registered! :" 
+                     << duid->toText() << std::endl;
+        }
+    } catch (const std::exception& ex) {
+        std::cout << "Exception in subnet6_select callout:" << ex.what() 
+                  << std::endl;
+            
+    }
+
+    return (0);
+}
+
+}

+ 3 - 0
src/hooks/dhcp/user_chk/tests/Makefile.am

@@ -30,6 +30,9 @@ if HAVE_GTEST
 TESTS += libdhcp_user_chk_unittests
 
 libdhcp_user_chk_unittests_SOURCES  = 
+libdhcp_user_chk_unittests_SOURCES += ../load_unload.cc
+libdhcp_user_chk_unittests_SOURCES += ../subnet_select_co.cc
+libdhcp_user_chk_unittests_SOURCES += ../version.cc
 libdhcp_user_chk_unittests_SOURCES += ../user.cc ../user.h
 libdhcp_user_chk_unittests_SOURCES += ../user_data_source.cc ../user_data_source.h
 libdhcp_user_chk_unittests_SOURCES += ../user_file.cc ../user_file.h

+ 1 - 2
src/hooks/dhcp/user_chk/tests/user_registry_unittests.cc

@@ -172,8 +172,7 @@ TEST(UserRegistry, userFileTest) {
 
     // Set the registry's data source and refresh the registry.
     ASSERT_NO_THROW(reg->setSource(user_file));
-    //ASSERT_NO_THROW(reg->refresh());
-    (reg->refresh());
+    ASSERT_NO_THROW(reg->refresh());
 
     // Verify we can find all the expected users.
     UserPtr found_user;