Kea 2.7.6
duid_config_parser.cc
Go to the documentation of this file.
1// Copyright (C) 2015-2024 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#include <cc/data.h>
9#include <dhcp/duid.h>
10#include <dhcpsrv/cfg_duid.h>
11#include <dhcpsrv/cfgmgr.h>
12#include <dhcpsrv/dhcpsrv_log.h>
16#include <boost/lexical_cast.hpp>
17
18#include <string>
19
20using namespace isc::data;
21
22namespace isc {
23namespace dhcp {
24
25void
27 isc::data::ConstElementPtr duid_configuration) {
28 if (!cfg) {
29 // Sanity check
30 isc_throw(DhcpConfigError, "Must provide valid pointer to cfg when parsing duid");
31 }
32
33 std::string param;
34 try {
35 param = "type";
36 std::string duid_type = getString(duid_configuration, "type");
37 // Map DUID type represented as text into numeric value.
39 if (duid_type == "LLT") {
40 numeric_type = DUID::DUID_LLT;
41 } else if (duid_type == "EN") {
42 numeric_type = DUID::DUID_EN;
43 } else if (duid_type == "LL") {
44 numeric_type = DUID::DUID_LL;
45 } else {
46 isc_throw(BadValue, "unsupported DUID type '"
47 << duid_type << "'. Expected: LLT, EN or LL");
48 }
49
50 cfg->setType(static_cast<DUID::DUIDType>(numeric_type));
51
52 param = "identifier";
53 if (duid_configuration->contains(param)) {
54 cfg->setIdentifier(getString(duid_configuration, param));
55 }
56
57 param = "htype";
58 if (duid_configuration->contains(param)) {
59 cfg->setHType(getUint16(duid_configuration, param));
60 }
61
62 param = "time";
63 if (duid_configuration->contains(param)) {
64 cfg->setTime(getUint32(duid_configuration, param));
65 }
66
67 param = "enterprise-id";
68 if (duid_configuration->contains(param)) {
69 cfg->setEnterpriseId(getUint32(duid_configuration, param));
70 }
71
72 param = "persist";
73 if (duid_configuration->contains(param)) {
74 cfg->setPersist(getBoolean(duid_configuration, param));
75 }
76
77 param = "user-context";
78 ConstElementPtr user_context = duid_configuration->get("user-context");
79 if (user_context) {
80 cfg->setContext(user_context);
81 }
82 } catch (const DhcpConfigError&) {
83 throw;
84 } catch (const std::exception& ex) {
85 // Append position.
86 isc_throw(DhcpConfigError, ex.what() << " ("
87 << getPosition(param, duid_configuration) << ")");
88 }
89
91}
92
93} // end of namespace isc::dhcp
94} // end of namespace isc
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
static const data::Element::Position & getPosition(const std::string &name, const data::ConstElementPtr parent)
Utility method that returns position of an element.
static std::string getString(isc::data::ConstElementPtr scope, const std::string &name)
Returns a string parameter from a scope.
uint32_t getUint32(isc::data::ConstElementPtr scope, const std::string &name)
Returns a value converted to uint32_t.
static bool getBoolean(isc::data::ConstElementPtr scope, const std::string &name)
Returns a boolean parameter from a scope.
uint16_t getUint16(isc::data::ConstElementPtr scope, const std::string &name)
Returns a value converted to uint16_t.
void parse(const CfgDUIDPtr &cfg, isc::data::ConstElementPtr duid_configuration)
Parses DUID configuration.
DUIDType
specifies DUID type
Definition duid.h:158
@ DUID_LL
link-layer, see RFC3315, section 11.4
Definition duid.h:162
@ DUID_LLT
link-layer + time, see RFC3315, section 11.2
Definition duid.h:160
@ DUID_UNKNOWN
invalid/unknown type
Definition duid.h:159
@ DUID_EN
enterprise-id, see RFC3315, section 11.3
Definition duid.h:161
To be removed. Please use ConfigError instead.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
#define LOG_WARN(LOGGER, MESSAGE)
Macro to conveniently test warn output and log it.
Definition macros.h:26
boost::shared_ptr< const Element > ConstElementPtr
Definition data.h:29
isc::log::Logger dhcpsrv_logger("dhcpsrv")
DHCP server library Logger.
Definition dhcpsrv_log.h:56
boost::shared_ptr< CfgDUID > CfgDUIDPtr
Pointer to the Non-const object.
Definition cfg_duid.h:161
const isc::log::MessageID DHCPSRV_CFGMGR_CONFIGURE_SERVERID
Defines the logger used by the top-level component of kea-lfc.