Kea  1.9.9-git
cfg_duid.cc
Go to the documentation of this file.
1 // Copyright (C) 2015-2020 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 
17 using namespace isc;
18 using namespace isc::data;
19 using namespace isc::util::encode;
20 using namespace isc::util::str;
21 
22 namespace isc {
23 namespace dhcp {
24 
25 CfgDUID::CfgDUID()
26  : type_(DUID::DUID_LLT), identifier_(), htype_(0), time_(0),
27  enterprise_id_(0), persist_(true) {
28 }
29 
30 void
31 CfgDUID::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
55 CfgDUID::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 
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 }
boost::shared_ptr< DUID > DuidPtr
Definition: duid.h:20
uint32_t getTime() const
Returns time for the DUID-LLT.
Definition: cfg_duid.h:76
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:53
link-layer + time, see RFC3315, section 11.2
Definition: duid.h:40
Cannot unparse error.
boost::shared_ptr< Element > ElementPtr
Definition: data.h:20
bool persist() const
Checks if server identifier should be stored on disk.
Definition: cfg_duid.h:101
uint16_t getHType() const
Returns hardware type for DUID-LLT and DUID-LL.
Definition: cfg_duid.h:66
DuidPtr get()
Returns current DUID.
void contextToElement(data::ElementPtr map) const
Merge unparse a user_context object.
Definition: user_context.cc:15
Holds DUID (DHCPv6 Unique Identifier)
Definition: duid.h:27
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
void createLL(const uint16_t htype, const std::vector< uint8_t > &ll_identifier)
Generates DUID-LL.
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:474
virtual isc::data::ElementPtr toElement() const
Unparse a configuration object.
Definition: cfg_duid.cc:86
A generic exception that is thrown when an unexpected error condition occurs.
DUID::DUIDType getType() const
Returns DUID type.
Definition: cfg_duid.h:37
link-layer, see RFC3315, section 11.4
Definition: duid.h:42
Defines the logger used by the top-level component of kea-dhcp-ddns.
string encodeHex(const vector< uint8_t > &binary)
Encode binary data in the base16 ('hex') format.
Definition: base_n.cc:469
std::vector< uint8_t > getIdentifier() const
Returns identifier.
Definition: cfg_duid.h:55
uint32_t getEnterpriseId() const
Returns enterprise id for the DUID-EN.
Definition: cfg_duid.h:86
void createEN(const uint32_t enterprise_id, const std::vector< uint8_t > &identifier)
Generates DUID-EN.
string trim(const string &instring)
Trim Leading and Trailing Spaces.
Definition: strutil.cc:53
enterprise-id, see RFC3315, section 11.3
Definition: duid.h:41
Factory for generating DUIDs (DHCP Unique Identifiers).
Definition: duid_factory.h:63
DuidPtr create(const std::string &duid_file_path)
Creates instance of a DUID from the current configuration.
Definition: cfg_duid.cc:55
void setIdentifier(const std::string &identifier_as_hex)
Sets new identifier as hex string.
Definition: cfg_duid.cc:31