Kea: pkt_receive_co.cc Source File

| Kea 3.0.3-git

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
40 int pkt4_receive(CalloutHandle& handle) {
41 CalloutHandle::CalloutNextStep status = handle.getStatus();
42 if (status == CalloutHandle::NEXT_STEP_DROP ||
43 status == CalloutHandle::NEXT_STEP_SKIP) {
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
98 int pkt6_receive(CalloutHandle& handle) {
99 CalloutHandle::CalloutNextStep status = handle.getStatus();
100 if (status == CalloutHandle::NEXT_STEP_DROP ||
101 status == CalloutHandle::NEXT_STEP_SKIP) {
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"
CalloutHandle::CalloutNextStep
CalloutNextStep
Specifies allowed next steps.
Definition callout_handle.h:86
CalloutHandle::NEXT_STEP_DROP
@ NEXT_STEP_DROP
drop the packet
Definition callout_handle.h:89
CalloutHandle::NEXT_STEP_SKIP
@ NEXT_STEP_SKIP
skip the next processing step
Definition callout_handle.h:88
isc::dhcp::DUID
Holds DUID (DHCPv6 Unique Identifier).
Definition duid.h:142
isc::hooks::CalloutHandle
Per-packet callout handle.
Definition callout_handle.h:78
isc::hooks::CalloutHandle::setContext
void setContext(const std::string &name, T value)
Set context.
Definition callout_handle.h:253
isc::hooks::CalloutHandle::getStatus
CalloutNextStep getStatus() const
Returns the next processing step.
Definition callout_handle.h:241
isc::hooks::CalloutHandle::getArgument
void getArgument(const std::string &name, T &value) const
Get argument.
Definition callout_handle.h:164
dhcp6.h
D6O_CLIENTID
@ D6O_CLIENTID
Definition dhcp6.h:21
hooks.h
isc::dhcp
Definition dhcp4/client_handler.cc:20
isc::dhcp::Pkt4Ptr
boost::shared_ptr< Pkt4 > Pkt4Ptr
A pointer to Pkt4 object.
Definition pkt4.h:555
isc::dhcp::DuidPtr
boost::shared_ptr< DUID > DuidPtr
Definition duid.h:136
isc::dhcp::HWAddrPtr
boost::shared_ptr< HWAddr > HWAddrPtr
Shared pointer to a hardware address structure.
Definition hwaddr.h:154
isc::dhcp::Pkt6Ptr
boost::shared_ptr< Pkt6 > Pkt6Ptr
A pointer to Pkt6 packet.
Definition pkt6.h:31
isc::dhcp::OptionPtr
boost::shared_ptr< Option > OptionPtr
Definition option.h:37
isc::hooks
Definition callout_handle.cc:21
user_chk
Defines the logger used by the user check hooks library.
Definition user.cc:19
user_chk::UserPtr
boost::shared_ptr< User > UserPtr
Defines a smart pointer to a User.
Definition user.h:241
pkt4.h
pkt6.h
pkt4_receive
int pkt4_receive(CalloutHandle &handle)
This callout is called at the "pkt4_receive" hook.
Definition pkt_receive_co.cc:40
pkt6_receive
int pkt6_receive(CalloutHandle &handle)
This callout is called at the "pkt6_receive" hook.
Definition pkt_receive_co.cc:98
query_user_id_label
const char * query_user_id_label
Text label of user id in the inbound query in callout context.
Definition user_chk/load_unload.cc:38
user_registry
UserRegistryPtr user_registry
Pointer to the registry instance.
Definition user_chk/load_unload.cc:24
registered_user_label
const char * registered_user_label
Text label of registered user pointer in callout context.
Definition user_chk/load_unload.cc:41
user_chk.h