Kea 2.2.0
duid_config_parser.cc
Go to the documentation of this file.
1// Copyright (C) 2015,2017 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/foreach.hpp>
17#include <boost/lexical_cast.hpp>
18#include <limits>
19#include <string>
20
21using namespace isc::data;
22
23namespace isc {
24namespace dhcp {
25
26void
28 isc::data::ConstElementPtr duid_configuration) {
29 if (!cfg) {
30 // Sanity check
31 isc_throw(DhcpConfigError, "Must provide valid pointer to cfg when parsing duid");
32 }
33
34 std::string param;
35 try {
36 param = "type";
37 std::string duid_type = getString(duid_configuration, "type");
38 // Map DUID type represented as text into numeric value.
40 if (duid_type == "LLT") {
41 numeric_type = DUID::DUID_LLT;
42 } else if (duid_type == "EN") {
43 numeric_type = DUID::DUID_EN;
44 } else if (duid_type == "LL") {
45 numeric_type = DUID::DUID_LL;
46 } else {
47 isc_throw(BadValue, "unsupported DUID type '"
48 << duid_type << "'. Expected: LLT, EN or LL");
49 }
50
51 cfg->setType(static_cast<DUID::DUIDType>(numeric_type));
52
53 param = "identifier";
54 if (duid_configuration->contains(param)) {
55 cfg->setIdentifier(getString(duid_configuration, param));
56 }
57
58 param = "htype";
59 if (duid_configuration->contains(param)) {
60 cfg->setHType(getUint16(duid_configuration, param));
61 }
62
63 param = "time";
64 if (duid_configuration->contains(param)) {
65 cfg->setTime(getUint32(duid_configuration, param));
66 }
67
68 param = "enterprise-id";
69 if (duid_configuration->contains(param)) {
70 cfg->setEnterpriseId(getUint32(duid_configuration, param));
71 }
72
73 param = "persist";
74 if (duid_configuration->contains(param)) {
75 cfg->setPersist(getBoolean(duid_configuration, param));
76 }
77
78 param = "user-context";
79 ConstElementPtr user_context = duid_configuration->get("user-context");
80 if (user_context) {
81 cfg->setContext(user_context);
82 }
83 } catch (const DhcpConfigError&) {
84 throw;
85 } catch (const std::exception& ex) {
86 // Append position.
87 isc_throw(DhcpConfigError, ex.what() << " ("
88 << getPosition(param, duid_configuration) << ")");
89 }
90
92}
93
94} // end of namespace isc::dhcp
95} // end of namespace isc
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
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:38
@ DUID_LL
link-layer, see RFC3315, section 11.4
Definition: duid.h:42
@ DUID_LLT
link-layer + time, see RFC3315, section 11.2
Definition: duid.h:40
@ DUID_UNKNOWN
invalid/unknown type
Definition: duid.h:39
@ DUID_EN
enterprise-id, see RFC3315, section 11.3
Definition: duid.h:41
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:27
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.