Kea  1.5.0
pkt_receive_co.cc
Go to the documentation of this file.
1 // Copyright (C) 2013-2015 Internet Systems Consortium, Inc. ("ISC")
2 //
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 
8 
9 #include <config.h>
10 #include <hooks/hooks.h>
11 #include <dhcp/pkt4.h>
12 #include <dhcp/dhcp6.h>
13 #include <dhcp/pkt6.h>
14 #include <user_chk.h>
15 
16 using namespace isc::dhcp;
17 using namespace isc::hooks;
18 using namespace user_chk;
19 using namespace std;
20 
21 // Functions accessed by the hooks framework use C linkage to avoid the name
22 // mangling that accompanies use of the C++ compiler as well as to avoid
23 // issues related to namespaces.
24 extern "C" {
25 
41  if (!user_registry) {
42  std::cout << "DHCP UserCheckHook : pkt4_receive UserRegistry is null"
43  << std::endl;
44  return (1);
45  }
46 
47  try {
48  // Refresh the registry.
49  user_registry->refresh();
50 
51  // Get the HWAddress to use as the user identifier.
52  Pkt4Ptr query;
53  handle.getArgument("query4", query);
54  HWAddrPtr hwaddr = query->getHWAddr();
55 
56  // Store the id we search with so it is available down the road.
57  handle.setContext(query_user_id_label, hwaddr);
58 
59  // Look for the user in the registry.
60  UserPtr registered_user = user_registry->findUser(*hwaddr);
61 
62  // Store user regardless. Empty user pointer means non-found. It is
63  // cheaper to fetch it and test it, than to use an exception throw.
64  handle.setContext(registered_user_label, registered_user);
65  std::cout << "DHCP UserCheckHook : pkt4_receive user : "
66  << hwaddr->toText() << " is "
67  << (registered_user ? " registered" : " not registered")
68  << std::endl;
69  } catch (const std::exception& ex) {
70  std::cout << "DHCP UserCheckHook : pkt4_receive unexpected error: "
71  << ex.what() << std::endl;
72  return (1);
73  }
74 
75  return (0);
76 }
77 
93  if (!user_registry) {
94  std::cout << "DHCP UserCheckHook : pkt6_receive UserRegistry is null"
95  << std::endl;
96  return (1);
97  }
98 
99  try {
100  // Refresh the registry.
101  user_registry->refresh();
102 
103  // Fetch the inbound packet.
104  Pkt6Ptr query;
105  handle.getArgument("query6", query);
106 
107  // Get the DUID to use as the user identifier.
108  OptionPtr opt_duid = query->getOption(D6O_CLIENTID);
109  if (!opt_duid) {
110  std::cout << "DHCP6 query is missing DUID" << std::endl;
111  return (1);
112  }
113  DuidPtr duid = DuidPtr(new DUID(opt_duid->getData()));
114 
115  // Store the id we search with so it is available down the road.
116  handle.setContext(query_user_id_label, duid);
117 
118  // Look for the user in the registry.
119  UserPtr registered_user = user_registry->findUser(*duid);
120 
121  // Store user regardless. Empty user pointer means non-found. It is
122  // cheaper to fetch it and test it, than to use an exception throw.
123  handle.setContext(registered_user_label, registered_user);
124  std::cout << "DHCP UserCheckHook : pkt6_receive user : "
125  << duid->toText() << " is "
126  << (registered_user ? " registered" : " not registered")
127  << std::endl;
128  } catch (const std::exception& ex) {
129  std::cout << "DHCP UserCheckHook : pkt6_receive unexpected error: "
130  << ex.what() << std::endl;
131  return (1);
132  }
133 
134  return (0);
135 }
136 
137 } // end extern "C"
boost::shared_ptr< DUID > DuidPtr
Definition: duid.h:20
const char * query_user_id_label
Text label of user id in the inbound query in callout context.
Definition: load_unload.cc:38
int pkt6_receive(CalloutHandle &handle)
This callout is called at the "pkt6_receive" hook.
boost::shared_ptr< HWAddr > HWAddrPtr
Shared pointer to a hardware address structure.
Definition: hwaddr.h:154
boost::shared_ptr< Option > OptionPtr
Definition: option.h:37
boost::shared_ptr< User > UserPtr
Defines a smart pointer to a User.
Definition: user.h:241
const char * registered_user_label
Text label of registered user pointer in callout context.
Definition: load_unload.cc:41
Holds DUID (DHCPv6 Unique Identifier)
Definition: duid.h:27
Defines the logger used by the user check hooks library.
Definition: user.cc:19
boost::shared_ptr< Pkt6 > Pkt6Ptr
A pointer to Pkt6 packet.
Definition: pkt6.h:28
Per-packet callout handle.
boost::shared_ptr< Pkt4 > Pkt4Ptr
A pointer to Pkt4 object.
Definition: pkt4.h:546
void getArgument(const std::string &name, T &value) const
Get argument.
void setContext(const std::string &name, T value)
Set context.
UserRegistryPtr user_registry
Pointer to the registry instance.
Definition: load_unload.cc:24
int pkt4_receive(CalloutHandle &handle)
This callout is called at the "pkt4_receive" hook.