Kea 3.1.1
host_cache_parsers.cc
Go to the documentation of this file.
1// Copyright (C) 2020-2025 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
7#include <config.h>
8
12#include <boost/pointer_cast.hpp>
13#include <boost/lexical_cast.hpp>
14
15using namespace isc::asiolink;
16using namespace isc::data;
17using namespace isc::dhcp;
18using namespace std;
19
20namespace isc {
21namespace host_cache {
22
25 { "maximum", Element::integer, "0" }
26};
27
29void
31 ElementPtr mutable_cfg;
32 if (config) {
33 mutable_cfg = boost::const_pointer_cast<Element>(config);
34 } else {
35 mutable_cfg = Element::createMap();
36 }
37
38 if (!mutable_cfg || (mutable_cfg->getType() != Element::map)) {
39 isc_throw(ConfigError, "Host Cache config is not a map");
40 }
41
42 try {
43 setDefaults(mutable_cfg, HOST_CACHE_DEFAULTS);
44
45 int64_t maximum = getInteger(mutable_cfg, "maximum");
46 if (maximum < 0) {
47 isc_throw(ConfigError, "Host Cache maximum parameter is negative");
48 }
49 if (maximum > MAXIMUM) {
50 isc_throw(ConfigError, "Host Cache maximum parameter is too large "
51 << "(" << maximum << " > " << MAXIMUM << ")");
52 }
53 hcref.setMaximum(static_cast<size_t>(maximum));
54 } catch (const ConfigError&) {
55 throw;
56 } catch (const exception& ex) {
57 isc_throw(ConfigError, ex.what());
58 }
59}
60
63 try {
64 // Scan critical parameters
65 string identifier;
66 string identifier_name;
67 string hostname;
68 ConstElementPtr user_context;
69 auto& map = entry->mapValue();
70 for (auto const& element : map) {
71 if ((element.first == "hw-address") ||
72 (element.first == "duid") ||
73 (element.first == "circuit-id") ||
74 (element.first == "client-id") ||
75 (element.first == "flex-id")) {
76 if (!identifier_name.empty()) {
77 isc_throw(ConfigError, "the '" << element.first
78 << "' and '" << identifier_name
79 << "' are mutually exclusive");
80 }
81 identifier = element.second->stringValue();
82 identifier_name = element.first;
83 continue;
84 }
85
86 if (element.first == "hostname") {
87 hostname = element.second->stringValue();
88 continue;
89 }
90
91 if (element.first == "user-context") {
92 user_context = element.second;
93 continue;
94 }
95 }
96
97 // Host identifier is a must.
98 if (identifier_name.empty()) {
99 isc_throw(ConfigError, "no identifier");
100 }
101
102 // Create a host object from the basic parameters we already parsed.
103 HostPtr host;
104 host.reset(new Host(identifier, identifier_name,
105 SubnetID(SUBNET_ID_UNUSED),
106 SubnetID(SUBNET_ID_UNUSED),
107 IOAddress("0.0.0.0"), hostname));
108 // Add user context
109 if (user_context) {
110 host->setContext(user_context);
111 }
112
113 // Scan other parameters
114 for (auto const& element : map) {
115 if ((element.first == "hw-address") ||
116 (element.first == "duid") ||
117 (element.first == "circuit-id") ||
118 (element.first == "client-id") ||
119 (element.first == "flex-id") ||
120 (element.first == "hostname") ||
121 (element.first == "user-context")) {
122 continue;
123 }
124
125 if (element.first == "subnet-id4") {
126 host->setIPv4SubnetID(element.second->intValue());
127 continue;
128 }
129
130 if (element.first == "subnet-id6") {
131 host->setIPv6SubnetID(element.second->intValue());
132 continue;
133 }
134
135 if (element.first == "option-data4") {
136 CfgOptionPtr cfg_option = host->getCfgOption4();
137 OptionDataListParser parser(AF_INET);
138 parser.parse(cfg_option, element.second);
139 continue;
140 }
141
142 if (element.first == "option-data6") {
143 CfgOptionPtr cfg_option = host->getCfgOption6();
144 OptionDataListParser parser(AF_INET6);
145 parser.parse(cfg_option, element.second);
146 continue;
147 }
148
149 if (element.first == "ip-address") {
150 host->setIPv4Reservation(IOAddress(element.second->stringValue()));
151 continue;
152 }
153
154 if (element.first == "next-server") {
155 host->setNextServer(IOAddress(element.second->stringValue()));
156 continue;
157 }
158
159 if (element.first == "server-hostname") {
160 host->setServerHostname(element.second->stringValue());
161 continue;
162 }
163
164 if (element.first == "boot-file-name") {
165 host->setBootFileName(element.second->stringValue());
166 continue;
167 }
168
169 if (element.first == "client-classes4") {
170 auto const& classes = element.second->listValue();
171 for (auto const& cclass : classes) {
172 host->addClientClass4(cclass->stringValue());
173 }
174 continue;
175 }
176
177 if (element.first == "client-classes6") {
178 auto const& classes = element.second->listValue();
179 for (auto const& cclass : classes) {
180 host->addClientClass6(cclass->stringValue());
181 }
182 continue;
183 }
184
185 if (element.first == "ip-addresses") {
186 auto const& addresses = element.second->listValue();
187 for (auto const& addr : addresses) {
188 string address = addr->stringValue();
189 host->addReservation(IPv6Resrv(IPv6Resrv::TYPE_NA,
190 IOAddress(address),
191 128));
192 }
193 continue;
194 }
195
196 if (element.first == "prefixes") {
197 auto& prefixes = element.second->listValue();
198 for (auto const& pref : prefixes) {
199 string prefix = pref->stringValue();
200 uint8_t len = 128;
201 size_t pos = prefix.find('/');
202 if (pos == string::npos) {
203 isc_throw(ConfigError, "bad prefix (no /)");
204 } else if (pos > prefix.length() - 1) {
205 isc_throw(ConfigError, "bad prefix (end /)");
206 }
207 try {
208 string preflen = prefix.substr(pos + 1);
209 len = boost::lexical_cast<unsigned>(preflen);
210 } catch (const boost::bad_lexical_cast&) {
211 isc_throw(ConfigError, "bad prefix length");
212 }
213 prefix.erase(pos);
214
215 host->addReservation(IPv6Resrv(IPv6Resrv::TYPE_PD,
216 IOAddress(prefix),
217 len));
218 }
219 continue;
220 }
221
222 if (element.first == "host-id") {
223 int64_t hid = element.second->intValue();
224 host->setHostId(static_cast<uint64_t>(hid));
225 continue;
226 }
227
228 if (element.first == "negative") {
229 host->setNegative(element.second->boolValue());
230 continue;
231 }
232
233 isc_throw(ConfigError, "unknown parameter: " << element.first);
234 }
235 return (host);
236
237 } catch (const ConfigError&) {
238 throw;
239 } catch (const exception& ex) {
240 isc_throw(ConfigError, ex.what());
241 }
242}
243
244} // end of namespace isc::host_cache
245} // end of namespace isc
@ map
Definition data.h:147
@ integer
Definition data.h:140
static ElementPtr createMap(const Position &pos=ZERO_POSITION())
Creates an empty MapElement type ElementPtr.
Definition data.cc:304
An exception that is thrown if an error occurs while configuring any server.
static int64_t getInteger(isc::data::ConstElementPtr scope, const std::string &name)
Returns an integer parameter from a scope.
static size_t setDefaults(isc::data::ElementPtr scope, const SimpleDefaults &default_values)
Sets the default values.
Represents a device with IPv4 and/or IPv6 reservations.
Definition host.h:327
IPv6 reservation for a host.
Definition host.h:163
Parser for option data values within a subnet.
void parse(const CfgOptionPtr &cfg, isc::data::ConstElementPtr option_data_list, bool encapsulate=true)
Parses a list of options, instantiates them and stores in cfg.
static const int64_t MAXIMUM
Absolute maximum number of elements.
static const data::SimpleDefaults HOST_CACHE_DEFAULTS
Defaults for Host Cache configuration.
void parse(HostCache &hcref, const data::ConstElementPtr &config)
Parses Host Cache configuration.
dhcp::HostPtr parse(const data::ConstElementPtr &entry)
Parses Host Cache entry.
Host Cache implementation.
Definition host_cache.h:35
virtual void setMaximum(size_t maximum)
Set maximum number of cached hosts (0 means unbound).
Definition host_cache.cc:47
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
boost::shared_ptr< const Element > ConstElementPtr
Definition data.h:29
std::vector< SimpleDefault > SimpleDefaults
This specifies all default values in a given scope (e.g. a subnet).
boost::shared_ptr< Element > ElementPtr
Definition data.h:28
boost::shared_ptr< CfgOption > CfgOptionPtr
Non-const pointer.
Definition cfg_option.h:973
boost::shared_ptr< Host > HostPtr
Pointer to the Host object.
Definition host.h:837
uint32_t SubnetID
Defines unique IPv4 or IPv6 subnet identifier.
Definition subnet_id.h:25
Defines the logger used by the top-level component of kea-lfc.