Kea  1.9.9-git
duid.cc
Go to the documentation of this file.
1 // Copyright (C) 2012-2019 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.h>
10 #include <exceptions/exceptions.h>
11 #include <util/io_utilities.h>
12 #include <util/strutil.h>
13 #include <iomanip>
14 #include <cctype>
15 #include <sstream>
16 #include <vector>
17 
18 #include <stdint.h>
19 
20 namespace isc {
21 namespace dhcp {
22 
23 DUID::DUID(const std::vector<uint8_t>& duid) {
24  if (duid.size() > MAX_DUID_LEN) {
25  isc_throw(isc::BadValue, "DUID size is " << duid.size()
26  << " bytes, exceeds maximum of " << MAX_DUID_LEN);
27  }
28  if (duid.empty()) {
29  isc_throw(isc::BadValue, "Empty DUIDs are not allowed");
30  }
31  duid_ = duid;
32 }
33 
34 DUID::DUID(const uint8_t* data, size_t len) {
35  if (len > MAX_DUID_LEN) {
36  isc_throw(isc::BadValue, "DUID size is " << len
37  << " bytes, exceeds maximum of " << MAX_DUID_LEN);
38  }
39  if (len == 0) {
40  isc_throw(isc::BadValue, "Empty DUIDs/Client-ids not allowed");
41  }
42 
43  duid_ = std::vector<uint8_t>(data, data + len);
44 }
45 
46 const std::vector<uint8_t>& DUID::getDuid() const {
47  return (duid_);
48 }
50  if (duid_.size() < 2) {
51  return (DUID_UNKNOWN);
52  }
53  uint16_t type = (duid_[0] << 8) + duid_[1];
54  if (type < DUID_MAX) {
55  return (static_cast<DUID::DUIDType>(type));
56  } else {
57  return (DUID_UNKNOWN);
58  }
59 }
60 
61 DUID
62 DUID::fromText(const std::string& text) {
63  std::vector<uint8_t> binary;
65  return (DUID(binary));
66 }
67 
68 const DUID&
70  static std::vector<uint8_t> empty_duid(1,0);
71  static DUID empty(empty_duid);
72  return (empty);
73 }
74 
75 std::string DUID::toText() const {
76  std::stringstream tmp;
77  tmp << std::hex;
78  bool delim = false;
79  for (std::vector<uint8_t>::const_iterator it = duid_.begin();
80  it != duid_.end(); ++it) {
81  if (delim) {
82  tmp << ":";
83  }
84  tmp << std::setw(2) << std::setfill('0') << static_cast<unsigned int>(*it);
85  delim = true;
86  }
87  return (tmp.str());
88 }
89 
90 bool DUID::operator==(const DUID& other) const {
91  return (this->duid_ == other.duid_);
92 }
93 
94 bool DUID::operator!=(const DUID& other) const {
95  return (this->duid_ != other.duid_);
96 }
97 
98 // Constructor based on vector<uint8_t>
99 ClientId::ClientId(const std::vector<uint8_t>& clientid)
100  : DUID(clientid) {
101  if (clientid.size() < MIN_CLIENT_ID_LEN) {
102  isc_throw(isc::BadValue, "client-id is too short (" << clientid.size()
103  << "), at least 2 is required");
104  }
105 }
106 
107 // Constructor based on C-style data
108 ClientId::ClientId(const uint8_t *clientid, size_t len)
109  : DUID(clientid, len) {
110  if (len < MIN_CLIENT_ID_LEN) {
111  isc_throw(isc::BadValue, "client-id is too short (" << len
112  << "), at least 2 is required");
113  }
114 }
115 
116 // Returns a copy of client-id data
117 const std::vector<uint8_t>& ClientId::getClientId() const {
118  return (duid_);
119 }
120 
121 // Returns the Client ID in text form
122 std::string ClientId::toText() const {
123 
124  // As DUID is a private base class of ClientId, we can't access
125  // its public toText() method through inheritance: instead we
126  // need the interface of a ClientId::toText() that calls the
127  // equivalent method in the base class.
128  return (DUID::toText());
129 }
130 
132 ClientId::fromText(const std::string& text) {
133  std::vector<uint8_t> binary;
135  return (ClientIdPtr(new ClientId(binary)));
136 }
137 
138 // Compares two client-ids
139 bool ClientId::operator==(const ClientId& other) const {
140  return (this->duid_ == other.duid_);
141 }
142 
143 // Compares two client-ids
144 bool ClientId::operator!=(const ClientId& other) const {
145  return (this->duid_ != other.duid_);
146 }
147 
148 }; // end of isc::dhcp namespace
149 }; // end of isc namespace
const std::vector< uint8_t > & getDuid() const
Returns a const reference to the actual DUID value.
Definition: duid.cc:46
static ClientIdPtr fromText(const std::string &text)
Create client identifier from the textual format.
Definition: duid.cc:132
std::string toText() const
Returns textual representation of a DUID (e.g. 00:01:02:03:ff)
Definition: duid.cc:122
bool operator==(const DUID &other) const
Compares two DUIDs for equality.
Definition: duid.cc:90
static const DUID & EMPTY()
Defines the constant "empty" DUID.
Definition: duid.cc:69
static DUID fromText(const std::string &text)
Create DUID from the textual format.
Definition: duid.cc:62
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
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...
DUIDType
specifies DUID type
Definition: duid.h:38
DUIDType getType() const
Returns the DUID type.
Definition: duid.cc:49
const std::vector< uint8_t > & getClientId() const
Returns reference to the client-id data.
Definition: duid.cc:117
std::vector< uint8_t > duid_
The actual content of the DUID.
Definition: duid.h:99
not a real type, just maximum defined value + 1
Definition: duid.h:44
boost::shared_ptr< ClientId > ClientIdPtr
Shared pointer to a Client ID.
Definition: duid.h:103
bool operator!=(const ClientId &other) const
Compares two client-ids for inequality.
Definition: duid.cc:144
std::string toText() const
Returns textual representation of a DUID (e.g. 00:01:02:03:ff)
Definition: duid.cc:75
static const size_t MIN_CLIENT_ID_LEN
Minimum size of a client ID.
Definition: duid.h:118
Defines the logger used by the top-level component of kea-dhcp-ddns.
ClientId(const std::vector< uint8_t > &clientid)
Constructor based on vector
Definition: duid.cc:99
static const size_t MAX_DUID_LEN
maximum duid size As defined in RFC 8415, section 11.1
Definition: duid.h:31
Holds Client identifier or client IPv4 address.
Definition: duid.h:111
bool operator!=(const DUID &other) const
Compares two DUIDs for inequality.
Definition: duid.cc:94
DUID(const std::vector< uint8_t > &duid)
Constructor from vector.
Definition: duid.cc:23
bool operator==(const ClientId &other) const
Compares two client-ids for equality.
Definition: duid.cc:139
invalid/unknown type
Definition: duid.h:39