Kea  2.3.9
duid.h
Go to the documentation of this file.
1 // Copyright (C) 2012-2023 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 #ifndef DUID_H
8 #define DUID_H
9 
10 #include <asiolink/io_address.h>
11 #include <util/strutil.h>
12 #include <boost/shared_ptr.hpp>
13 #include <vector>
14 #include <stdint.h>
15 #include <unistd.h>
16 
17 namespace isc {
18 namespace dhcp {
19 
22 protected:
26  virtual ~IdentifierBaseType() = 0;
27 };
28 
30 typedef boost::shared_ptr<IdentifierBaseType> IdentifierBaseTypePtr;
31 
32 template<size_t min_size, size_t max_size>
34 public:
35 
39  IdentifierType(const std::vector<uint8_t>& data) {
40  if (data.size() < min_size) {
41  isc_throw(isc::BadValue, "identifier is too short (" << data.size()
42  << "), at least "<< min_size << " is required");
43  }
44  if (data.size() > max_size) {
45  isc_throw(isc::BadValue, "identifier is too large (" << data.size()
46  << "), at most " << max_size << " is required");
47  }
48  data_ = data;
49  }
50 
55  IdentifierType(const uint8_t* data, size_t len) {
56  if (len < min_size) {
57  isc_throw(isc::BadValue, "identifier is too short (" << len
58  << "), at least "<< min_size << " is required");
59  }
60  if (len > max_size) {
61  isc_throw(isc::BadValue, "identifier is too large (" << len
62  << "), at most " << max_size << " is required");
63  }
64  data_ = std::vector<uint8_t>(data, data + len);
65  }
66 
70  static constexpr size_t getMinSize() {
71  return (min_size);
72  }
73 
77  static constexpr size_t getMaxSize() {
78  return (max_size);
79  }
80 
84  std::string toText() const {
85  std::stringstream tmp;
86  tmp << std::hex;
87  bool delim = false;
88  for (auto const data : data_) {
89  if (delim) {
90  tmp << ":";
91  }
92  tmp << std::setw(2) << std::setfill('0') << static_cast<unsigned int>(data);
93  delim = true;
94  }
95  return (tmp.str());
96  }
97 
104  static std::vector<uint8_t> fromText(const std::string& text) {
105  std::vector<uint8_t> binary;
107  return (binary);
108  }
109 
113  bool operator==(const IdentifierType& other) const {
114  return (data_ == other.data_);
115  }
116 
120  bool operator!=(const IdentifierType& other) const {
121  return (data_ != other.data_);
122  }
123 
124 protected:
125 
127  std::vector<uint8_t> data_;
128 };
129 
131 class DUID;
132 typedef boost::shared_ptr<DUID> DuidPtr;
133 
138 class DUID : public IdentifierType<3, 130> {
139 public:
140 
145  static constexpr size_t MIN_DUID_LEN = IdentifierType::getMinSize();
146 
151  static constexpr size_t MAX_DUID_LEN = IdentifierType::getMaxSize();
152 
154  typedef enum {
156  DUID_LLT = 1,
157  DUID_EN = 2,
158  DUID_LL = 3,
159  DUID_UUID = 4,
160  DUID_MAX
162 
166  DUID(const std::vector<uint8_t>& data);
167 
172  DUID(const uint8_t* data, size_t len);
173 
182  const std::vector<uint8_t>& getDuid() const;
183 
191  //
193  static const DUID& EMPTY();
194 
196  DUIDType getType() const;
197 
206  static DUID fromText(const std::string& text);
207 };
208 
210 class ClientId;
212 typedef boost::shared_ptr<ClientId> ClientIdPtr;
213 
218 class ClientId : public IdentifierType<2, 255> {
219 public:
220 
225  static constexpr size_t MIN_CLIENT_ID_LEN = IdentifierType::getMinSize();
226 
231  static constexpr size_t MAX_CLIENT_ID_LEN = IdentifierType::getMaxSize();
232 
236  ClientId(const std::vector<uint8_t>& data);
237 
242  ClientId(const uint8_t* data, size_t len);
243 
252  const std::vector<uint8_t>& getClientId() const;
253 
264  static ClientIdPtr fromText(const std::string& text);
265 };
266 
267 } // namespace dhcp
268 } // namespace isc
269 
270 #endif /* DUID_H */
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
Holds Client identifier or client IPv4 address.
Definition: duid.h:218
static constexpr size_t MAX_CLIENT_ID_LEN
Maximum size of a client ID.
Definition: duid.h:231
const std::vector< uint8_t > & getClientId() const
Returns reference to the client-id data.
Definition: duid.cc:70
ClientId(const std::vector< uint8_t > &data)
Constructor based on vector<uint8_t>
Definition: duid.cc:64
static ClientIdPtr fromText(const std::string &text)
Create client identifier from the textual format.
Definition: duid.cc:74
static constexpr size_t MIN_CLIENT_ID_LEN
Minimum size of a client ID.
Definition: duid.h:225
Holds DUID (DHCPv6 Unique Identifier)
Definition: duid.h:138
static constexpr size_t MIN_DUID_LEN
minimum duid size
Definition: duid.h:145
static DUID fromText(const std::string &text)
Create DUID from the textual format.
Definition: duid.cc:51
static const DUID & EMPTY()
Defines the constant "empty" DUID.
Definition: duid.cc:56
static constexpr size_t MAX_DUID_LEN
maximum duid size
Definition: duid.h:151
DUIDType
specifies DUID type
Definition: duid.h:154
@ DUID_LL
link-layer, see RFC3315, section 11.4
Definition: duid.h:158
@ DUID_LLT
link-layer + time, see RFC3315, section 11.2
Definition: duid.h:156
@ DUID_MAX
not a real type, just maximum defined value + 1
Definition: duid.h:160
@ DUID_UNKNOWN
invalid/unknown type
Definition: duid.h:155
@ DUID_EN
enterprise-id, see RFC3315, section 11.3
Definition: duid.h:157
@ DUID_UUID
UUID, see RFC3315, section 11.5.
Definition: duid.h:159
const std::vector< uint8_t > & getDuid() const
Returns a const reference to the actual DUID value.
Definition: duid.cc:34
DUIDType getType() const
Returns the DUID type.
Definition: duid.cc:38
DUID(const std::vector< uint8_t > &data)
Constructor from vector.
Definition: duid.cc:28
Base type used to define a common smart pointer for all derived types.
Definition: duid.h:21
virtual ~IdentifierBaseType()=0
Pure virtual destructor.
Definition: duid.cc:22
std::vector< uint8_t > data_
The actual content of the Identifier.
Definition: duid.h:127
IdentifierType(const uint8_t *data, size_t len)
Constructor from array and array size.
Definition: duid.h:55
bool operator==(const IdentifierType &other) const
Compares two identifiers for equality.
Definition: duid.h:113
IdentifierType(const std::vector< uint8_t > &data)
Constructor from vector.
Definition: duid.h:39
bool operator!=(const IdentifierType &other) const
Compares two identifiers for inequality.
Definition: duid.h:120
static constexpr size_t getMaxSize()
Return the maximum size of the acceptable data.
Definition: duid.h:77
std::string toText() const
Returns textual representation of the identifier (e.g.
Definition: duid.h:84
static std::vector< uint8_t > fromText(const std::string &text)
This static function parses an Identifier specified in the textual format.
Definition: duid.h:104
static constexpr size_t getMinSize()
Return the minimum size of the acceptable data.
Definition: duid.h:70
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
boost::shared_ptr< DUID > DuidPtr
Definition: duid.h:131
boost::shared_ptr< IdentifierBaseType > IdentifierBaseTypePtr
Shared pointer to a IdentifierType.
Definition: duid.h:30
boost::shared_ptr< ClientId > ClientIdPtr
Shared pointer to a Client ID.
Definition: duid.h:210
void decodeFormattedHexString(const std::string &hex_string, std::vector< uint8_t > &binary)
Converts a formatted string of hexadecimal digits into a vector.
Definition: strutil.cc:273
Defines the logger used by the top-level component of kea-lfc.