Kea 2.5.8
subnet_select_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 <dhcpsrv/subnet.h>
15#include <user_chk.h>
16#include <user_chk_log.h>
17
18using namespace isc::dhcp;
19using namespace isc::hooks;
20using namespace user_chk;
21using namespace std;
22
23// Functions accessed by the hooks framework use C linkage to avoid the name
24// mangling that accompanies use of the C++ compiler as well as to avoid
25// issues related to namespaces.
26extern "C" {
27
46 if (status == CalloutHandle::NEXT_STEP_DROP ||
48 return (0);
49 }
50
51 if (!user_registry) {
53 return (1);
54 }
55
56 try {
57 // Get subnet collection. If it's empty just bail nothing to do.
58 const isc::dhcp::Subnet4Collection *subnets = NULL;
59 handle.getArgument("subnet4collection", subnets);
60 if (subnets->empty()) {
61 return (0);
62 }
63
64 // Get registered_user pointer.
65 UserPtr registered_user;
66 handle.getContext(registered_user_label, registered_user);
67
68 if (registered_user) {
69 // User is in the registry, so leave the pre-selected subnet alone.
70 Subnet4Ptr subnet;
71 handle.getArgument("subnet4", subnet);
72 } else {
73 // User is not in the registry, so assign them to the last subnet
74 // in the collection. By convention we are assuming this is the
75 // restricted subnet.
76 Subnet4Ptr subnet = *subnets->rbegin();
77 handle.setArgument("subnet4", subnet);
78 }
79 } catch (const std::exception& ex) {
81 .arg(ex.what());
82 return (1);
83 }
84
85 return (0);
86}
87
106 if (status == CalloutHandle::NEXT_STEP_DROP ||
108 return (0);
109 }
110
111 if (!user_registry) {
113 return (1);
114 }
115
116 try {
117 // Get subnet collection. If it's empty just bail nothing to do.
118 const isc::dhcp::Subnet6Collection *subnets = NULL;
119 handle.getArgument("subnet6collection", subnets);
120 if (subnets->empty()) {
121 return (0);
122 }
123
124 // Get registered_user pointer.
125 UserPtr registered_user;
126 handle.getContext(registered_user_label, registered_user);
127
128 if (registered_user) {
129 // User is in the registry, so leave the pre-selected subnet alone.
130 Subnet6Ptr subnet;
131 handle.getArgument("subnet6", subnet);
132 } else {
133 // User is not in the registry, so assign them to the last subnet
134 // in the collection. By convention we are assuming this is the
135 // restricted subnet.
136 Subnet6Ptr subnet = *subnets->rbegin();
137 handle.setArgument("subnet6", subnet);
138 }
139 } catch (const std::exception& ex) {
141 .arg(ex.what());
142 return (1);
143 }
144
145 return (0);
146}
147
148}
Per-packet callout handle.
void getContext(const std::string &name, T &value) const
Get context.
CalloutNextStep
Specifies allowed next steps.
@ NEXT_STEP_DROP
drop the packet
@ NEXT_STEP_SKIP
skip the next processing step
CalloutNextStep getStatus() const
Returns the next processing step.
void getArgument(const std::string &name, T &value) const
Get argument.
void setArgument(const std::string &name, T value)
Set argument.
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
#define LOG_ERROR(LOGGER, MESSAGE)
Macro to conveniently test error output and log it.
Definition: macros.h:32
boost::shared_ptr< Subnet4 > Subnet4Ptr
A pointer to a Subnet4 object.
Definition: subnet.h:498
boost::shared_ptr< Subnet6 > Subnet6Ptr
A pointer to a Subnet6 object.
Definition: subnet.h:663
boost::multi_index_container< Subnet6Ptr, boost::multi_index::indexed_by< boost::multi_index::ordered_unique< boost::multi_index::tag< SubnetSubnetIdIndexTag >, boost::multi_index::const_mem_fun< Subnet, SubnetID, &Subnet::getID > >, boost::multi_index::ordered_unique< boost::multi_index::tag< SubnetPrefixIndexTag >, boost::multi_index::const_mem_fun< Subnet, std::string, &Subnet::toText > >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< SubnetModificationTimeIndexTag >, boost::multi_index::const_mem_fun< data::BaseStampedElement, boost::posix_time::ptime, &data::BaseStampedElement::getModificationTime > > > > Subnet6Collection
A collection of Subnet6 objects.
Definition: subnet.h:974
boost::multi_index_container< Subnet4Ptr, boost::multi_index::indexed_by< boost::multi_index::ordered_unique< boost::multi_index::tag< SubnetSubnetIdIndexTag >, boost::multi_index::const_mem_fun< Subnet, SubnetID, &Subnet::getID > >, boost::multi_index::ordered_unique< boost::multi_index::tag< SubnetPrefixIndexTag >, boost::multi_index::const_mem_fun< Subnet, std::string, &Subnet::toText > >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< SubnetServerIdIndexTag >, boost::multi_index::const_mem_fun< Network4, asiolink::IOAddress, &Network4::getServerId > >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< SubnetModificationTimeIndexTag >, boost::multi_index::const_mem_fun< data::BaseStampedElement, boost::posix_time::ptime, &data::BaseStampedElement::getModificationTime > > > > Subnet4Collection
A collection of Subnet4 objects.
Definition: subnet.h:903
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
isc::log::Logger user_chk_logger("user_chk")
User Check Logger.
Definition: user_chk_log.h:21
int subnet4_select(CalloutHandle &handle)
This callout is called at the "subnet4_select" hook.
int subnet6_select(CalloutHandle &handle)
This callout is called at the "subnet6_select" hook.
const isc::log::MessageID USER_CHK_SUBNET4_SELECT_ERROR
const isc::log::MessageID USER_CHK_SUBNET6_SELECT_ERROR
const isc::log::MessageID USER_CHK_SUBNET6_SELECT_REGISTRY_NULL
const isc::log::MessageID USER_CHK_SUBNET4_SELECT_REGISTRY_NULL