| Kea 3.0.2-git

duid.h
Go to the documentation of this file.
1// Copyright (C) 2012-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#ifndef DUID_H
8#define DUID_H
9
10#include <asiolink/io_address.h>
11#include <util/str.h>
12
13#include <cstdint>
14#include <iomanip>
15#include <vector>
16
17#include <boost/shared_ptr.hpp>
18
19#include <unistd.h>
20
21 namespace isc {
22 namespace dhcp {
23
25 class IdentifierBaseType {
26 protected:
30 virtual ~IdentifierBaseType() = 0;
31};
32
34 typedef boost::shared_ptr<IdentifierBaseType> IdentifierBaseTypePtr;
35
36 template<size_t min_size, size_t max_size>
37 class IdentifierType : public IdentifierBaseType {
38 public:
39
43 IdentifierType(const std::vector<uint8_t>& data) {
44 if (data.size() < min_size) {
45 isc_throw(isc::BadValue, "identifier is too short (" << data.size()
46 << "), at least "<< min_size << " is required");
47 }
48 if (data.size() > max_size) {
49 isc_throw(isc::BadValue, "identifier is too large (" << data.size()
50 << "), at most " << max_size << " is required");
51 }
52 data_ = data;
53 }
54
59 IdentifierType(const uint8_t* data, size_t len) {
60 if (len < min_size) {
61 isc_throw(isc::BadValue, "identifier is too short (" << len
62 << "), at least "<< min_size << " is required");
63 }
64 if (len > max_size) {
65 isc_throw(isc::BadValue, "identifier is too large (" << len
66 << "), at most " << max_size << " is required");
67 }
68 data_ = std::vector<uint8_t>(data, data + len);
69 }
70
74 static constexpr size_t getMinSize() {
75 return (min_size);
76 }
77
81 static constexpr size_t getMaxSize() {
82 return (max_size);
83 }
84
88 std::string toText() const {
89 std::stringstream tmp;
90 tmp << std::hex;
91 bool delim = false;
92 for (auto const& data : data_) {
93 if (delim) {
94 tmp << ":";
95 }
96 tmp << std::setw(2) << std::setfill('0') << static_cast<unsigned int>(data);
97 delim = true;
98 }
99 return (tmp.str());
100 }
101
108 static std::vector<uint8_t> fromText(const std::string& text) {
109 std::vector<uint8_t> binary;
110 util::str::decodeFormattedHexString(text, binary);
111 return (binary);
112 }
113
117 bool operator==(const IdentifierType& other) const {
118 return (data_ == other.data_);
119 }
120
124 bool operator!=(const IdentifierType& other) const {
125 return (data_ != other.data_);
126 }
127
128 protected:
129
131 std::vector<uint8_t> data_;
132};
133
135 class DUID;
136 typedef boost::shared_ptr<DUID> DuidPtr;
137
142 class DUID : public IdentifierType<3, 130> {
143 public:
144
149 static constexpr size_t MIN_DUID_LEN = IdentifierType::getMinSize();
150
155 static constexpr size_t MAX_DUID_LEN = IdentifierType::getMaxSize();
156
158 typedef enum {
159 DUID_UNKNOWN = 0,
160 DUID_LLT = 1,
161 DUID_EN = 2,
162 DUID_LL = 3,
163 DUID_UUID = 4,
164 DUID_MAX
165 } DUIDType;
166
170 DUID(const std::vector<uint8_t>& data);
171
176 DUID(const uint8_t* data, size_t len);
177
186 const std::vector<uint8_t>& getDuid() const;
187
195 //
197 static const DUID& EMPTY();
198
200 DUIDType getType() const;
201
210 static DUID fromText(const std::string& text);
211};
212
214 class ClientId;
216 typedef boost::shared_ptr<ClientId> ClientIdPtr;
217
222 class ClientId : public IdentifierType<2, 255> {
223 public:
224
229 static constexpr size_t MIN_CLIENT_ID_LEN = IdentifierType::getMinSize();
230
235 static constexpr size_t MAX_CLIENT_ID_LEN = IdentifierType::getMaxSize();
236
240 ClientId(const std::vector<uint8_t>& data);
241
246 ClientId(const uint8_t* data, size_t len);
247
256 const std::vector<uint8_t>& getClientId() const;
257
268 static ClientIdPtr fromText(const std::string& text);
269};
270
271} // namespace dhcp
272} // namespace isc
273
274#endif /* DUID_H */
DUID::DUID
DUID(const std::vector< uint8_t > &data)
Constructor from vector.
Definition duid.cc:27
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::dhcp::ClientId::MAX_CLIENT_ID_LEN
static constexpr size_t MAX_CLIENT_ID_LEN
Maximum size of a client ID.
Definition duid.h:235
isc::dhcp::ClientId::getClientId
const std::vector< uint8_t > & getClientId() const
Returns reference to the client-id data.
Definition duid.cc:69
isc::dhcp::ClientId::ClientId
ClientId(const std::vector< uint8_t > &data)
Constructor based on vector<uint8_t>.
Definition duid.cc:63
isc::dhcp::ClientId::fromText
static ClientIdPtr fromText(const std::string &text)
Create client identifier from the textual format.
Definition duid.cc:73
isc::dhcp::ClientId::MIN_CLIENT_ID_LEN
static constexpr size_t MIN_CLIENT_ID_LEN
Minimum size of a client ID.
Definition duid.h:229
isc::dhcp::DUID
Holds DUID (DHCPv6 Unique Identifier).
Definition duid.h:142
isc::dhcp::DUID::MIN_DUID_LEN
static constexpr size_t MIN_DUID_LEN
minimum duid size
Definition duid.h:149
isc::dhcp::DUID::fromText
static DUID fromText(const std::string &text)
Create DUID from the textual format.
Definition duid.cc:50
isc::dhcp::DUID::EMPTY
static const DUID & EMPTY()
Defines the constant "empty" DUID.
Definition duid.cc:55
isc::dhcp::DUID::MAX_DUID_LEN
static constexpr size_t MAX_DUID_LEN
maximum duid size
Definition duid.h:155
isc::dhcp::DUID::DUIDType
DUIDType
specifies DUID type
Definition duid.h:158
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_MAX
@ DUID_MAX
not a real type, just maximum defined value + 1
Definition duid.h:164
isc::dhcp::DUID::DUID_UNKNOWN
@ DUID_UNKNOWN
invalid/unknown type
Definition duid.h:159
isc::dhcp::DUID::DUID_EN
@ DUID_EN
enterprise-id, see RFC3315, section 11.3
Definition duid.h:161
isc::dhcp::DUID::DUID_UUID
@ DUID_UUID
UUID, see RFC3315, section 11.5.
Definition duid.h:163
isc::dhcp::DUID::getDuid
const std::vector< uint8_t > & getDuid() const
Returns a const reference to the actual DUID value.
Definition duid.cc:33
isc::dhcp::DUID::getType
DUIDType getType() const
Returns the DUID type.
Definition duid.cc:37
isc::dhcp::DUID::DUID
DUID(const std::vector< uint8_t > &data)
Constructor from vector.
Definition duid.cc:27
isc::dhcp::IdentifierBaseType
Base type used to define a common smart pointer for all derived types.
Definition duid.h:25
isc::dhcp::IdentifierBaseType::~IdentifierBaseType
virtual ~IdentifierBaseType()=0
Pure virtual destructor.
Definition duid.cc:21
isc::dhcp::IdentifierType::data_
std::vector< uint8_t > data_
The actual content of the Identifier.
Definition duid.h:131
isc::dhcp::IdentifierType::fromText
static std::vector< uint8_t > fromText(const std::string &text)
This static function parses an Identifier specified in the textual format.
Definition duid.h:108
isc::dhcp::IdentifierType::IdentifierType
IdentifierType(const uint8_t *data, size_t len)
Constructor from array and array size.
Definition duid.h:59
isc::dhcp::IdentifierType::operator==
bool operator==(const IdentifierType &other) const
Compares two identifiers for equality.
Definition duid.h:117
isc::dhcp::IdentifierType::IdentifierType
IdentifierType(const std::vector< uint8_t > &data)
Constructor from vector.
Definition duid.h:43
isc::dhcp::IdentifierType::operator!=
bool operator!=(const IdentifierType &other) const
Compares two identifiers for inequality.
Definition duid.h:124
isc::dhcp::IdentifierType::getMaxSize
static constexpr size_t getMaxSize()
Return the maximum size of the acceptable data.
Definition duid.h:81
isc::dhcp::IdentifierType::toText
std::string toText() const
Returns textual representation of the identifier (e.g.
Definition duid.h:88
isc::dhcp::IdentifierType::getMinSize
static constexpr size_t getMinSize()
Return the minimum size of the acceptable data.
Definition duid.h:74
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition exceptions/exceptions.h:210
io_address.h
isc::data
Definition base_stamped_element.cc:12
isc::dhcp
Definition dhcp4/client_handler.cc:20
isc::dhcp::DuidPtr
boost::shared_ptr< DUID > DuidPtr
Definition duid.h:136
isc::dhcp::IdentifierBaseTypePtr
boost::shared_ptr< IdentifierBaseType > IdentifierBaseTypePtr
Shared pointer to a IdentifierType.
Definition duid.h:34
isc::dhcp::ClientIdPtr
boost::shared_ptr< ClientId > ClientIdPtr
Shared pointer to a Client ID.
Definition duid.h:216
isc::util::str::decodeFormattedHexString
void decodeFormattedHexString(const string &hex_string, vector< uint8_t > &binary)
Converts a formatted string of hexadecimal digits into a vector.
Definition str.cc:212
isc
Defines the logger used by the top-level component of kea-lfc.
Definition agent_parser.cc:148
str.h