Kea 2.5.8
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
21namespace isc {
22namespace dhcp {
23
26protected:
30 virtual ~IdentifierBaseType() = 0;
31};
32
34typedef boost::shared_ptr<IdentifierBaseType> IdentifierBaseTypePtr;
35
36template<size_t min_size, size_t max_size>
38public:
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;
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
128protected:
129
131 std::vector<uint8_t> data_;
132};
133
135class DUID;
136typedef boost::shared_ptr<DUID> DuidPtr;
137
142class DUID : public IdentifierType<3, 130> {
143public:
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 {
164 DUID_MAX
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
214class ClientId;
216typedef boost::shared_ptr<ClientId> ClientIdPtr;
217
222class ClientId : public IdentifierType<2, 255> {
223public:
224
230
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 */
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:222
static constexpr size_t MAX_CLIENT_ID_LEN
Maximum size of a client ID.
Definition: duid.h:235
const std::vector< uint8_t > & getClientId() const
Returns reference to the client-id data.
Definition: duid.cc:69
static ClientIdPtr fromText(const std::string &text)
Create client identifier from the textual format.
Definition: duid.cc:73
static constexpr size_t MIN_CLIENT_ID_LEN
Minimum size of a client ID.
Definition: duid.h:229
Holds DUID (DHCPv6 Unique Identifier)
Definition: duid.h:142
static constexpr size_t MIN_DUID_LEN
minimum duid size
Definition: duid.h:149
static DUID fromText(const std::string &text)
Create DUID from the textual format.
Definition: duid.cc:50
static const DUID & EMPTY()
Defines the constant "empty" DUID.
Definition: duid.cc:55
static constexpr size_t MAX_DUID_LEN
maximum duid size
Definition: duid.h:155
DUIDType
specifies DUID type
Definition: duid.h:158
@ 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_MAX
not a real type, just maximum defined value + 1
Definition: duid.h:164
@ DUID_UNKNOWN
invalid/unknown type
Definition: duid.h:159
@ DUID_EN
enterprise-id, see RFC3315, section 11.3
Definition: duid.h:161
@ DUID_UUID
UUID, see RFC3315, section 11.5.
Definition: duid.h:163
const std::vector< uint8_t > & getDuid() const
Returns a const reference to the actual DUID value.
Definition: duid.cc:33
DUIDType getType() const
Returns the DUID type.
Definition: duid.cc:37
Base type used to define a common smart pointer for all derived types.
Definition: duid.h:25
virtual ~IdentifierBaseType()=0
Pure virtual destructor.
Definition: duid.cc:21
std::vector< uint8_t > data_
The actual content of the Identifier.
Definition: duid.h:131
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
IdentifierType(const uint8_t *data, size_t len)
Constructor from array and array size.
Definition: duid.h:59
bool operator==(const IdentifierType &other) const
Compares two identifiers for equality.
Definition: duid.h:117
IdentifierType(const std::vector< uint8_t > &data)
Constructor from vector.
Definition: duid.h:43
bool operator!=(const IdentifierType &other) const
Compares two identifiers for inequality.
Definition: duid.h:124
static constexpr size_t getMaxSize()
Return the maximum size of the acceptable data.
Definition: duid.h:81
std::string toText() const
Returns textual representation of the identifier (e.g.
Definition: duid.h:88
static constexpr size_t getMinSize()
Return the minimum size of the acceptable data.
Definition: duid.h:74
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
boost::shared_ptr< DUID > DuidPtr
Definition: duid.h:136
boost::shared_ptr< IdentifierBaseType > IdentifierBaseTypePtr
Shared pointer to a IdentifierType.
Definition: duid.h:34
boost::shared_ptr< ClientId > ClientIdPtr
Shared pointer to a Client ID.
Definition: duid.h:216
void decodeFormattedHexString(const string &hex_string, vector< uint8_t > &binary)
Converts a formatted string of hexadecimal digits into a vector.
Definition: str.cc:212
Defines the logger used by the top-level component of kea-lfc.