Kea: cfg_duid.cc Source File

| Kea 3.2.0-git

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
25CfgDUID::CfgDUID()
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
54 DuidPtr
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:
67 factory.createEN(getEnterpriseId(), getIdentifier());
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
85 ElementPtr
86CfgDUID::toElement() const {
87 ElementPtr result = Element::createMap();
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",
108 Element::create(util::encode::encodeHex(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}
cfg_duid.h
isc::BadValue
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
Definition exceptions/exceptions.h:132
isc::ToElementError
Cannot unparse error.
Definition cfg_to_element.h:19
isc::Unexpected
A generic exception that is thrown when an unexpected error condition occurs.
Definition exceptions/exceptions.h:153
isc::data::Element::create
static ElementPtr create(const Position &pos=ZERO_POSITION())
Create a NullElement.
Definition data.cc:299
isc::data::Element::createMap
static ElementPtr createMap(const Position &pos=ZERO_POSITION())
Creates an empty MapElement type ElementPtr.
Definition data.cc:354
isc::dhcp::CfgDUID::getIdentifier
std::vector< uint8_t > getIdentifier() const
Returns identifier.
Definition cfg_duid.h:55
isc::dhcp::CfgDUID::CfgDUID
CfgDUID()
Constructor.
Definition cfg_duid.cc:25
isc::dhcp::CfgDUID::getType
DUID::DUIDType getType() const
Returns DUID type.
Definition cfg_duid.h:37
isc::dhcp::CfgDUID::setIdentifier
void setIdentifier(const std::string &identifier_as_hex)
Sets new identifier as hex string.
Definition cfg_duid.cc:31
isc::dhcp::CfgDUID::create
DuidPtr create(const std::string &duid_file_path)
Creates instance of a DUID from the current configuration.
Definition cfg_duid.cc:55
isc::dhcp::CfgDUID::getEnterpriseId
uint32_t getEnterpriseId() const
Returns enterprise id for the DUID-EN.
Definition cfg_duid.h:86
isc::dhcp::CfgDUID::getTime
uint32_t getTime() const
Returns time for the DUID-LLT.
Definition cfg_duid.h:76
isc::dhcp::CfgDUID::toElement
virtual isc::data::ElementPtr toElement() const
Unparse a configuration object.
Definition cfg_duid.cc:86
isc::dhcp::CfgDUID::getHType
uint16_t getHType() const
Returns hardware type for DUID-LLT and DUID-LL.
Definition cfg_duid.h:66
isc::dhcp::CfgDUID::persist
bool persist() const
Checks if server identifier should be stored on disk.
Definition cfg_duid.h:101
isc::dhcp::DUIDFactory
Factory for generating DUIDs (DHCP Unique Identifiers).
Definition duid_factory.h:63
isc::dhcp::DUID
Holds DUID (DHCPv6 Unique Identifier).
Definition duid.h:142
isc::dhcp::DUID::DUID_LL
@ DUID_LL
link-layer, see RFC3315, section 11.4
Definition duid.h:162
isc::dhcp::DUID::DUID_LLT
@ DUID_LLT
link-layer + time, see RFC3315, section 11.2
Definition duid.h:160
isc::dhcp::DUID::DUID_EN
@ DUID_EN
enterprise-id, see RFC3315, section 11.3
Definition duid.h:161
duid_factory.h
encode.h
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition exceptions/exceptions.h:210
isc::data
Definition base_stamped_element.cc:12
isc::data::ElementPtr
boost::shared_ptr< Element > ElementPtr
Definition data.h:29
isc::dhcp
Definition dhcp4/client_handler.cc:20
isc::dhcp::DuidPtr
boost::shared_ptr< DUID > DuidPtr
Definition duid.h:136
isc::util::encode
Definition encode.cc:24
isc::util::encode::decodeHex
void decodeHex(const string &encoded_str, vector< uint8_t > &output)
Decode a base16 encoded string into binary data.
Definition encode.cc:367
isc::util::encode::encodeHex
string encodeHex(const vector< uint8_t > &binary)
Encode binary data in the base16 format.
Definition encode.cc:361
isc::util::str
Definition str.cc:29
isc::util::str::trim
string trim(const string &input)
Trim leading and trailing spaces.
Definition str.cc:32
isc
Defines the logger used by the top-level component of kea-lfc.
Definition check_exists_add.cc:18
str.h
isc::data::UserContext::contextToElement
void contextToElement(data::ElementPtr map) const
Merge unparse a user_context object.
Definition user_context.cc:15