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