Kea 2.6.0
pkt_receive_co.cc
Go to the documentation of this file.
1// Copyright (C) 2013-2020 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
16using namespace isc::dhcp;
17using namespace isc::hooks;
18using namespace user_chk;
19using 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.
24extern "C" {
25
42 if (status == CalloutHandle::NEXT_STEP_DROP ||
44 return (0);
45 }
46
47 if (!user_registry) {
48 std::cout << "DHCP UserCheckHook : pkt4_receive UserRegistry is null"
49 << std::endl;
50 return (1);
51 }
52
53 try {
54 // Refresh the registry.
55 user_registry->refresh();
56
57 // Get the HWAddress to use as the user identifier.
58 Pkt4Ptr query;
59 handle.getArgument("query4", query);
60 HWAddrPtr hwaddr = query->getHWAddr();
61
62 // Store the id we search with so it is available down the road.
63 handle.setContext(query_user_id_label, hwaddr);
64
65 // Look for the user in the registry.
66 UserPtr registered_user = user_registry->findUser(*hwaddr);
67
68 // Store user regardless. Empty user pointer means non-found. It is
69 // cheaper to fetch it and test it, than to use an exception throw.
70 handle.setContext(registered_user_label, registered_user);
71 std::cout << "DHCP UserCheckHook : pkt4_receive user : "
72 << hwaddr->toText() << " is "
73 << (registered_user ? " registered" : " not registered")
74 << std::endl;
75 } catch (const std::exception& ex) {
76 std::cout << "DHCP UserCheckHook : pkt4_receive unexpected error: "
77 << ex.what() << std::endl;
78 return (1);
79 }
80
81 return (0);
82}
83
100 if (status == CalloutHandle::NEXT_STEP_DROP ||
102 return (0);
103 }
104
105 if (!user_registry) {
106 std::cout << "DHCP UserCheckHook : pkt6_receive UserRegistry is null"
107 << std::endl;
108 return (1);
109 }
110
111 try {
112 // Refresh the registry.
113 user_registry->refresh();
114
115 // Fetch the inbound packet.
116 Pkt6Ptr query;
117 handle.getArgument("query6", query);
118
119 // Get the DUID to use as the user identifier.
120 OptionPtr opt_duid = query->getOption(D6O_CLIENTID);
121 if (!opt_duid) {
122 std::cout << "DHCP6 query is missing DUID" << std::endl;
123 return (1);
124 }
125 DuidPtr duid = DuidPtr(new DUID(opt_duid->getData()));
126
127 // Store the id we search with so it is available down the road.
128 handle.setContext(query_user_id_label, duid);
129
130 // Look for the user in the registry.
131 UserPtr registered_user = user_registry->findUser(*duid);
132
133 // Store user regardless. Empty user pointer means non-found. It is
134 // cheaper to fetch it and test it, than to use an exception throw.
135 handle.setContext(registered_user_label, registered_user);
136 std::cout << "DHCP UserCheckHook : pkt6_receive user : "
137 << duid->toText() << " is "
138 << (registered_user ? " registered" : " not registered")
139 << std::endl;
140 } catch (const std::exception& ex) {
141 std::cout << "DHCP UserCheckHook : pkt6_receive unexpected error: "
142 << ex.what() << std::endl;
143 return (1);
144 }
145
146 return (0);
147}
148
149} // end extern "C"
Holds DUID (DHCPv6 Unique Identifier)
Definition: duid.h:142
Per-packet callout handle.
CalloutNextStep
Specifies allowed next steps.
@ NEXT_STEP_DROP
drop the packet
@ NEXT_STEP_SKIP
skip the next processing step
void setContext(const std::string &name, T value)
Set context.
CalloutNextStep getStatus() const
Returns the next processing step.
void getArgument(const std::string &name, T &value) const
Get argument.
@ D6O_CLIENTID
Definition: dhcp6.h:21
const char * query_user_id_label
Text label of user id in the inbound query in callout context.
Definition: load_unload.cc:38
UserRegistryPtr user_registry
Pointer to the registry instance.
Definition: load_unload.cc:24
const char * registered_user_label
Text label of registered user pointer in callout context.
Definition: load_unload.cc:41
boost::shared_ptr< Pkt4 > Pkt4Ptr
A pointer to Pkt4 object.
Definition: pkt4.h:555
boost::shared_ptr< DUID > DuidPtr
Definition: duid.h:136
boost::shared_ptr< HWAddr > HWAddrPtr
Shared pointer to a hardware address structure.
Definition: hwaddr.h:154
boost::shared_ptr< Pkt6 > Pkt6Ptr
A pointer to Pkt6 packet.
Definition: pkt6.h:31
boost::shared_ptr< Option > OptionPtr
Definition: option.h:37
Defines the logger used by the user check hooks library.
Definition: user.cc:19
boost::shared_ptr< User > UserPtr
Defines a smart pointer to a User.
Definition: user.h:241
int pkt4_receive(CalloutHandle &handle)
This callout is called at the "pkt4_receive" hook.
int pkt6_receive(CalloutHandle &handle)
This callout is called at the "pkt6_receive" hook.