Kea 1.5.0
subnet_select_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 <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
45 if (!user_registry) {
46 LOG_ERROR(user_chk_logger, USER_CHK_SUBNET4_SELECT_REGISTRY_NULL);
47 return (1);
48 }
49
50 try {
51 // Get subnet collection. If it's empty just bail nothing to do.
52 const isc::dhcp::Subnet4Collection *subnets = NULL;
53 handle.getArgument("subnet4collection", subnets);
54 if (subnets->empty()) {
55 return (0);
56 }
57
58 // Get registered_user pointer.
59 UserPtr registered_user;
60 handle.getContext(registered_user_label, registered_user);
61
62 if (registered_user) {
63 // User is in the registry, so leave the pre-selected subnet alone.
64 Subnet4Ptr subnet;
65 handle.getArgument("subnet4", subnet);
66 } else {
67 // User is not in the registry, so assign them to the last subnet
68 // in the collection. By convention we are assuming this is the
69 // restricted subnet.
70 Subnet4Ptr subnet = subnets->back();
71 handle.setArgument("subnet4", subnet);
72 }
73 } catch (const std::exception& ex) {
74 LOG_ERROR(user_chk_logger, USER_CHK_SUBNET4_SELECT_ERROR)
75 .arg(ex.what());
76 return (1);
77 }
78
79 return (0);
80}
81
99 if (!user_registry) {
100 LOG_ERROR(user_chk_logger, USER_CHK_SUBNET6_SELECT_REGISTRY_NULL);
101 return (1);
102 }
103
104 try {
105 // Get subnet collection. If it's empty just bail nothing to do.
106 const isc::dhcp::Subnet6Collection *subnets = NULL;
107 handle.getArgument("subnet6collection", subnets);
108 if (subnets->empty()) {
109 return (0);
110 }
111
112 // Get registered_user pointer.
113 UserPtr registered_user;
114 handle.getContext(registered_user_label, registered_user);
115
116 if (registered_user) {
117 // User is in the registry, so leave the pre-selected subnet alone.
118 Subnet6Ptr subnet;
119 handle.getArgument("subnet6", subnet);
120 } else {
121 // User is not in the registry, so assign them to the last subnet
122 // in the collection. By convention we are assuming this is the
123 // restricted subnet.
124 Subnet6Ptr subnet = subnets->back();
125 handle.setArgument("subnet6", subnet);
126 }
127 } catch (const std::exception& ex) {
128 LOG_ERROR(user_chk_logger, USER_CHK_SUBNET6_SELECT_ERROR)
129 .arg(ex.what());
130 return (1);
131 }
132
133 return (0);
134}
135
136}
Per-packet callout handle.
void getContext(const std::string &name, T &value) const
Get context.
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::multi_index_container< Subnet6Ptr, boost::multi_index::indexed_by< boost::multi_index::random_access< boost::multi_index::tag< SubnetRandomAccessIndexTag > >, 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 > > > > Subnet6Collection
A collection of Subnet6 objects.
Definition: subnet.h:843
boost::shared_ptr< Subnet4 > Subnet4Ptr
A pointer to a Subnet4 object.
Definition: subnet.h:464
boost::shared_ptr< Subnet6 > Subnet6Ptr
A pointer to a Subnet6 object.
Definition: subnet.h:629
boost::multi_index_container< Subnet4Ptr, boost::multi_index::indexed_by< boost::multi_index::random_access< boost::multi_index::tag< SubnetRandomAccessIndexTag > >, 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 > > > > Subnet4Collection
A collection of Subnet4 objects.
Definition: subnet.h:798
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.