subnet_select_co.cc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #include <hooks/hooks.h>
  2. #include <dhcp/pkt4.h>
  3. #include <dhcp/dhcp6.h>
  4. #include <dhcp/pkt6.h>
  5. #include <dhcpsrv/subnet.h>
  6. #include <user_registry.h>
  7. #include <fstream>
  8. #include <string>
  9. using namespace isc::dhcp;
  10. using namespace isc::hooks;
  11. using namespace std;
  12. extern UserRegistryPtr user_registry;
  13. extern std::fstream user_chk_output;
  14. extern const char* registry_fname;
  15. extern const char* user_chk_output_fname;
  16. extern "C" {
  17. void generate_output_record(const std::string& id_type_str,
  18. const std::string& id_val_str,
  19. const std::string& subnet_str,
  20. const bool& registered)
  21. {
  22. user_chk_output << "id_type=" << id_type_str << std::endl
  23. << "client=" << id_val_str << std::endl
  24. << "subnet=" << subnet_str << std::endl
  25. << "registered=" << (registered ? "yes" : "no")
  26. << std::endl;
  27. flush(user_chk_output);
  28. }
  29. // This callout is called at the "subnet4_select" hook.
  30. int subnet4_select(CalloutHandle& handle) {
  31. if (!user_registry) {
  32. std::cout << "UserCheckHook: UserRegistry is null!" << std::endl;
  33. return (1);
  34. }
  35. try {
  36. // Refresh the registry.
  37. user_registry->refresh();
  38. // Get the HWAddress as the user identifier.
  39. Pkt4Ptr query;
  40. handle.getArgument("query4", query);
  41. HWAddrPtr hwaddr = query->getHWAddr();
  42. // Look for the user.
  43. UserPtr registered_user = user_registry->findUser(*hwaddr);
  44. if (registered_user) {
  45. // User is in the registry, so leave the pre-selected
  46. // subnet alone.
  47. Subnet4Ptr subnet;
  48. handle.getArgument("subnet4", subnet);
  49. generate_output_record("mac", hwaddr->toText(), subnet->toText(),
  50. true);
  51. } else {
  52. // User is not in the registry, so assign them to
  53. // the last subnet in the collection. By convention
  54. // we are assuming this is the restricted subnet.
  55. const isc::dhcp::Subnet4Collection *subnets = NULL;
  56. handle.getArgument("subnet4collection", subnets);
  57. Subnet4Ptr subnet = subnets->back();
  58. handle.setArgument("subnet4", subnet);
  59. generate_output_record("mac", hwaddr->toText(), subnet->toText(),
  60. false);
  61. }
  62. } catch (const std::exception& ex) {
  63. std::cout << "UserCheckHook: Exception in subnet4_select callout:"
  64. << ex.what() << std::endl;
  65. return (1);
  66. }
  67. return (0);
  68. }
  69. // This callout is called at the "subnet6_select" hook.
  70. int subnet6_select(CalloutHandle& handle) {
  71. if (!user_registry) {
  72. std::cout << "UserCheckHook: UserRegistry is null!" << std::endl;
  73. return (1);
  74. }
  75. try {
  76. // Refresh the registry.
  77. user_registry->refresh();
  78. // Get the HWAddress as the user identifier.
  79. Pkt6Ptr query;
  80. handle.getArgument("query6", query);
  81. DuidPtr duid;
  82. OptionPtr opt_duid = query->getOption(D6O_CLIENTID);
  83. if (!opt_duid) {
  84. std::cout << "DHCP6 query is missing DUID" << std::endl;
  85. return (1);
  86. }
  87. duid = DuidPtr(new DUID(opt_duid->getData()));
  88. // Look for the user.
  89. UserPtr registered_user = user_registry->findUser(*duid);
  90. if (registered_user) {
  91. // User is in the registry, so leave the pre-selected
  92. // subnet alone.
  93. Subnet6Ptr subnet;
  94. handle.getArgument("subnet6", subnet);
  95. generate_output_record("duid", duid->toText(), subnet->toText(),
  96. true);
  97. } else {
  98. // User is not in the registry, so assign them to
  99. // the last subnet in the collection. By convention
  100. // we are assuming this is the restricted subnet.
  101. const isc::dhcp::Subnet6Collection *subnets = NULL;
  102. handle.getArgument("subnet6collection", subnets);
  103. Subnet6Ptr subnet = subnets->back();
  104. handle.setArgument("subnet6", subnet);
  105. generate_output_record("duid", duid->toText(), subnet->toText(),
  106. false);
  107. }
  108. } catch (const std::exception& ex) {
  109. std::cout << "UserCheckHook: Exception in subnet6_select callout:" << ex.what() << std::endl;
  110. return (1);
  111. }
  112. return (0);
  113. }
  114. }