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>
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
20namespace isc {
21namespace dhcp {
22
23DUID::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
33DUID::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
44const 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
60DUID
61DUID::fromText(const std::string& text) {
62 std::vector<uint8_t> binary;
64 return (DUID(binary));
65}
66
67const DUID&
69 static std::vector<uint8_t> empty_duid(1,0);
70 static DUID empty(empty_duid);
71 return (empty);
72}
73
74std::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
89bool DUID::operator==(const DUID& other) const {
90 return (this->duid_ == other.duid_);
91}
92
93bool DUID::operator!=(const DUID& other) const {
94 return (this->duid_ != other.duid_);
95}
96
97// Constructor based on vector<uint8_t>
98ClientId::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
107ClientId::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
116const std::vector<uint8_t>& ClientId::getClientId() const {
117 return (duid_);
118}
119
120// Returns the Client ID in text form
121std::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
131ClientId::fromText(const std::string& text) {
132 std::vector<uint8_t> binary;
134 return (ClientIdPtr(new ClientId(binary)));
135}
136
137// Compares two client-ids
138bool ClientId::operator==(const ClientId& other) const {
139 return (this->duid_ == other.duid_);
140}
141
142// Compares two client-ids
143bool 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
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:111
const std::vector< uint8_t > & getClientId() const
Returns reference to the client-id data.
Definition: duid.cc:116
bool operator==(const ClientId &other) const
Compares two client-ids for equality.
Definition: duid.cc:138
static ClientIdPtr fromText(const std::string &text)
Create client identifier from the textual format.
Definition: duid.cc:131
bool operator!=(const ClientId &other) const
Compares two client-ids for inequality.
Definition: duid.cc:143
std::string toText() const
Returns textual representation of a DUID (e.g. 00:01:02:03:ff)
Definition: duid.cc:121
ClientId(const std::vector< uint8_t > &clientid)
Constructor based on vector<uint8_t>
Definition: duid.cc:98
static const size_t MIN_CLIENT_ID_LEN
Minimum size of a client ID.
Definition: duid.h:118
Holds DUID (DHCPv6 Unique Identifier)
Definition: duid.h:27
bool operator!=(const DUID &other) const
Compares two DUIDs for inequality.
Definition: duid.cc:93
bool operator==(const DUID &other) const
Compares two DUIDs for equality.
Definition: duid.cc:89
std::string toText() const
Returns textual representation of a DUID (e.g. 00:01:02:03:ff)
Definition: duid.cc:74
static DUID fromText(const std::string &text)
Create DUID from the textual format.
Definition: duid.cc:61
DUID(const std::vector< uint8_t > &duid)
Constructor from vector.
Definition: duid.cc:23
static const DUID & EMPTY()
Defines the constant "empty" DUID.
Definition: duid.cc:68
static const size_t MAX_DUID_LEN
maximum duid size As defined in RFC 8415, section 11.1
Definition: duid.h:31
std::vector< uint8_t > duid_
The actual content of the DUID.
Definition: duid.h:99
DUIDType
specifies DUID type
Definition: duid.h:38
@ DUID_MAX
not a real type, just maximum defined value + 1
Definition: duid.h:44
@ DUID_UNKNOWN
invalid/unknown type
Definition: duid.h:39
const std::vector< uint8_t > & getDuid() const
Returns a const reference to the actual DUID value.
Definition: duid.cc:44
DUIDType getType() const
Returns the DUID type.
Definition: duid.cc:48
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
boost::shared_ptr< ClientId > ClientIdPtr
Shared pointer to a Client ID.
Definition: duid.h:105
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
Defines the logger used by the top-level component of kea-dhcp-ddns.