Kea 2.7.0
cfg_duid.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
9#include <dhcp/duid_factory.h>
10#include <dhcpsrv/cfg_duid.h>
11#include <util/encode/encode.h>
12#include <util/str.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) {
56 // Forget the current DUID.
57 current_duid_.reset();
58
59 // Use DUID factory to create a DUID instance.
60 DUIDFactory factory(persist() ? duid_file_path : "");
61
62 switch (getType()) {
63 case DUID::DUID_LLT:
64 factory.createLLT(getHType(), getTime(), getIdentifier());
65 break;
66 case DUID::DUID_EN:
68 break;
69 case DUID::DUID_LL:
70 factory.createLL(getHType(), getIdentifier());
71 break;
72 default:
73 // This should actually never happen.
74 isc_throw(Unexpected, "invalid DUID type used " << getType()
75 << " to create a new DUID");
76 }
77
78 // Save the newly created DUID.
79 current_duid_ = factory.get();
80
81 // Return generated DUID.
82 return (current_duid_);
83}
84
88 // Set user context
89 contextToElement(result);
90 // The type item is required
91 std::string duid_type = "LLT";
92 switch (type_) {
93 case DUID::DUID_LLT:
94 break;
95 case DUID::DUID_EN:
96 duid_type = "EN";
97 break;
98 case DUID::DUID_LL:
99 duid_type = "LL";
100 break;
101 default:
102 isc_throw(ToElementError, "invalid DUID type: " << getType());
103 break;
104 }
105 result->set("type", Element::create(duid_type));
106 // Set the identifier
107 result->set("identifier",
109 // Set the hardware type
110 result->set("htype", Element::create(htype_));
111 // Set the time
112 result->set("time", Element::create(static_cast<long long>(time_)));
113 // Set the enterprise id
114 result->set("enterprise-id",
115 Element::create(static_cast<long long>(enterprise_id_)));
116 // Set the persistence flag
117 result->set("persist", Element::create(persist_));
118 return (result);
119}
120
121}
122}
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:249
static ElementPtr createMap(const Position &pos=ZERO_POSITION())
Creates an empty MapElement type ElementPtr.
Definition data.cc:304
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
DuidPtr create(const std::string &duid_file_path)
Creates instance of a DUID from the current configuration.
Definition cfg_duid.cc:55
uint32_t getEnterpriseId() const
Returns enterprise id for the DUID-EN.
Definition cfg_duid.h:86
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:86
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).
void createLLT(const uint16_t htype, const uint32_t time_in, const std::vector< uint8_t > &ll_identifier)
Generates DUID-LLT.
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:142
@ 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_EN
enterprise-id, see RFC3315, section 11.3
Definition duid.h:161
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
boost::shared_ptr< Element > ElementPtr
Definition data.h:28
boost::shared_ptr< DUID > DuidPtr
Definition duid.h:136
void decodeHex(const string &encoded_str, vector< uint8_t > &output)
Decode a base16 encoded string into binary data.
Definition encode.cc:367
string encodeHex(const vector< uint8_t > &binary)
Encode binary data in the base16 format.
Definition encode.cc:361
string trim(const string &input)
Trim leading and trailing spaces.
Definition str.cc:32
Defines the logger used by the top-level component of kea-lfc.
void contextToElement(data::ElementPtr map) const
Merge unparse a user_context object.