Kea 1.5.0
cfg_duid.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
9#include <dhcp/duid_factory.h>
10#include <dhcpsrv/cfg_duid.h>
11#include <util/encode/hex.h>
12#include <util/strutil.h>
13#include <iostream>
14#include <string>
15#include <string.h>
16
17using namespace isc;
18using namespace isc::data;
19using namespace isc::util::encode;
20using namespace isc::util::str;
21
22namespace isc {
23namespace dhcp {
24
26 : type_(DUID::DUID_LLT), identifier_(), htype_(0), time_(0),
27 enterprise_id_(0), persist_(true) {
28}
29
30void
31CfgDUID::setIdentifier(const std::string& identifier_as_hex) {
32 // Remove whitespaces.
33 const std::string identifier = trim(identifier_as_hex);
34 // Temporary result of parsing.
35 std::vector<uint8_t> binary;
36 if (!identifier.empty()) {
37 try {
38 // Decode identifier specified as a string of hexadecimal digits.
39 decodeHex(identifier, binary);
40 // All went ok, so let's replace identifier with a new value.
41 identifier_.swap(binary);
42 } catch (const std::exception& ex) {
43 isc_throw(BadValue, "identifier specified in the DUID"
44 " configuration '" << identifier
45 << "' is not a valid string of hexadecimal digits");
46 }
47
48 } else {
49 // If specified identifier is empty, clear our internal identifier.
50 identifier_.clear();
51 }
52}
53
55CfgDUID::create(const std::string& duid_file_path) const {
56 // Use DUID factory to create a DUID instance.
57 DUIDFactory factory(persist() ? duid_file_path : "");
58
59 switch (getType()) {
60 case DUID::DUID_LLT:
61 factory.createLLT(getHType(), getTime(), getIdentifier());
62 break;
63 case DUID::DUID_EN:
65 break;
66 case DUID::DUID_LL:
67 factory.createLL(getHType(), getIdentifier());
68 break;
69 default:
70 // This should actually never happen.
71 isc_throw(Unexpected, "invalid DUID type used " << getType()
72 << " to create a new DUID");
73 }
74
75 // Return generated DUID.
76 return (factory.get());
77}
78
82 // Set user context
83 contextToElement(result);
84 // The type item is required
85 std::string duid_type = "LLT";
86 switch (type_) {
87 case DUID::DUID_LLT:
88 break;
89 case DUID::DUID_EN:
90 duid_type = "EN";
91 break;
92 case DUID::DUID_LL:
93 duid_type = "LL";
94 break;
95 default:
96 isc_throw(ToElementError, "invalid DUID type: " << getType());
97 break;
98 }
99 result->set("type", Element::create(duid_type));
100 // Set the identifier
101 result->set("identifier",
103 // Set the hardware type
104 result->set("htype", Element::create(htype_));
105 // Set the time
106 result->set("time", Element::create(static_cast<long long>(time_)));
107 // Set the enterprise id
108 result->set("enterprise-id",
109 Element::create(static_cast<long long>(enterprise_id_)));
110 // Set the persistence flag
111 result->set("persist", Element::create(persist_));
112 return (result);
113}
114
115}
116}
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
Cannot unparse error.
A generic exception that is thrown when an unexpected error condition occurs.
static ElementPtr create(const Position &pos=ZERO_POSITION())
Definition: data.cc:223
static ElementPtr createMap(const Position &pos=ZERO_POSITION())
Creates an empty MapElement type ElementPtr.
Definition: data.cc:268
std::vector< uint8_t > getIdentifier() const
Returns identifier.
Definition: cfg_duid.h:55
CfgDUID()
Constructor.
Definition: cfg_duid.cc:25
DUID::DUIDType getType() const
Returns DUID type.
Definition: cfg_duid.h:37
void setIdentifier(const std::string &identifier_as_hex)
Sets new identifier as hex string.
Definition: cfg_duid.cc:31
uint32_t getEnterpriseId() const
Returns enterprise id for the DUID-EN.
Definition: cfg_duid.h:86
DuidPtr create(const std::string &duid_file_path) const
Creates instance of a DUID from the current configuration.
Definition: cfg_duid.cc:55
uint32_t getTime() const
Returns time for the DUID-LLT.
Definition: cfg_duid.h:76
virtual isc::data::ElementPtr toElement() const
Unparse a configuration object.
Definition: cfg_duid.cc:80
uint16_t getHType() const
Returns hardware type for DUID-LLT and DUID-LL.
Definition: cfg_duid.h:66
bool persist() const
Checks if server identifier should be stored on disk.
Definition: cfg_duid.h:101
Factory for generating DUIDs (DHCP Unique Identifiers).
Definition: duid_factory.h:63
void createLLT(const uint16_t htype, const uint32_t time_in, const std::vector< uint8_t > &ll_identifier)
Generates DUID-LLT.
Definition: duid_factory.cc:54
void createEN(const uint32_t enterprise_id, const std::vector< uint8_t > &identifier)
Generates DUID-EN.
DuidPtr get()
Returns current DUID.
void createLL(const uint16_t htype, const std::vector< uint8_t > &ll_identifier)
Generates DUID-LL.
Holds DUID (DHCPv6 Unique Identifier)
Definition: duid.h:27
@ 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_EN
enterprise-id, see RFC3315, section 11.3
Definition: duid.h:41
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
boost::shared_ptr< Element > ElementPtr
Definition: data.h:22
boost::shared_ptr< DUID > DuidPtr
Definition: duid.h:21
string encodeHex(const vector< uint8_t > &binary)
Encode binary data in the base16 ('hex') format.
Definition: base_n.cc:461
void decodeHex(const string &input, vector< uint8_t > &result)
Decode a text encoded in the base16 ('hex') format into the original data.
Definition: base_n.cc:466
string trim(const string &instring)
Trim Leading and Trailing Spaces.
Definition: strutil.cc:53
Defines the logger used by the top-level component of kea-dhcp-ddns.
void contextToElement(data::ElementPtr map) const
Merge unparse a user_context object.
Definition: user_context.cc:15