duid.cc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. // Copyright (C) 2012-2015 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <dhcp/duid.h>
  15. #include <exceptions/exceptions.h>
  16. #include <util/encode/hex.h>
  17. #include <util/io_utilities.h>
  18. #include <boost/algorithm/string/classification.hpp>
  19. #include <boost/algorithm/string/constants.hpp>
  20. #include <boost/algorithm/string/split.hpp>
  21. #include <iomanip>
  22. #include <cctype>
  23. #include <sstream>
  24. #include <vector>
  25. #include <stdint.h>
  26. namespace isc {
  27. namespace dhcp {
  28. DUID::DUID(const std::vector<uint8_t>& duid) {
  29. if (duid.size() > MAX_DUID_LEN) {
  30. isc_throw(isc::BadValue, "DUID too large");
  31. }
  32. if (duid.empty()) {
  33. isc_throw(isc::BadValue, "Empty DUIDs are not allowed");
  34. }
  35. duid_ = duid;
  36. }
  37. DUID::DUID(const uint8_t* data, size_t len) {
  38. if (len > MAX_DUID_LEN) {
  39. isc_throw(isc::BadValue, "DUID too large");
  40. }
  41. if (len == 0) {
  42. isc_throw(isc::BadValue, "Empty DUIDs/Client-ids not allowed");
  43. }
  44. duid_ = std::vector<uint8_t>(data, data + len);
  45. }
  46. std::vector<uint8_t>
  47. DUID::decode(const std::string& text) {
  48. /// @todo optimize stream operations here.
  49. std::vector<std::string> split_text;
  50. boost::split(split_text, text, boost::is_any_of(":"),
  51. boost::algorithm::token_compress_off);
  52. std::ostringstream s;
  53. for (size_t i = 0; i < split_text.size(); ++i) {
  54. // Check that only hexadecimal digits are used.
  55. size_t ch_index = 0;
  56. while (ch_index < split_text[i].length()) {
  57. if (!isxdigit(split_text[i][ch_index])) {
  58. isc_throw(isc::BadValue, "invalid value '"
  59. << split_text[i][ch_index] << "' in"
  60. << " DUID '" << text << "'");
  61. }
  62. ++ch_index;
  63. }
  64. if (split_text.size() > 1) {
  65. // If there are multiple tokens and the current one is empty, it
  66. // means that two consecutive colons were specified. This is not
  67. // allowed for client identifier.
  68. if (split_text[i].empty()) {
  69. isc_throw(isc::BadValue, "invalid identifier '"
  70. << text << "': tokens must be"
  71. " separated with a single colon");
  72. } else if (split_text[i].size() > 2) {
  73. isc_throw(isc::BadValue, "invalid identifier '"
  74. << text << "'");
  75. }
  76. }
  77. if (split_text[i].size() % 2) {
  78. s << "0";
  79. }
  80. s << split_text[i];
  81. }
  82. std::vector<uint8_t> binary;
  83. try {
  84. util::encode::decodeHex(s.str(), binary);
  85. } catch (const Exception& ex) {
  86. isc_throw(isc::BadValue, "failed to create identifier from text '"
  87. << text << "': " << ex.what());
  88. }
  89. return (binary);
  90. }
  91. const std::vector<uint8_t>& DUID::getDuid() const {
  92. return (duid_);
  93. }
  94. DUID::DUIDType DUID::getType() const {
  95. if (duid_.size() < 2) {
  96. return (DUID_UNKNOWN);
  97. }
  98. uint16_t type = (duid_[0] << 8) + duid_[1];
  99. if (type < DUID_MAX) {
  100. return (static_cast<DUID::DUIDType>(type));
  101. } else {
  102. return (DUID_UNKNOWN);
  103. }
  104. }
  105. DUID
  106. DUID::fromText(const std::string& text) {
  107. std::vector<uint8_t> binary = decode(text);
  108. return DUID(binary);
  109. }
  110. std::string DUID::toText() const {
  111. std::stringstream tmp;
  112. tmp << std::hex;
  113. bool delim = false;
  114. for (std::vector<uint8_t>::const_iterator it = duid_.begin();
  115. it != duid_.end(); ++it) {
  116. if (delim) {
  117. tmp << ":";
  118. }
  119. tmp << std::setw(2) << std::setfill('0') << static_cast<unsigned int>(*it);
  120. delim = true;
  121. }
  122. return (tmp.str());
  123. }
  124. bool DUID::operator==(const DUID& other) const {
  125. return (this->duid_ == other.duid_);
  126. }
  127. bool DUID::operator!=(const DUID& other) const {
  128. return (this->duid_ != other.duid_);
  129. }
  130. // Constructor based on vector<uint8_t>
  131. ClientId::ClientId(const std::vector<uint8_t>& clientid)
  132. : DUID(clientid) {
  133. if (clientid.size() < MIN_CLIENT_ID_LEN) {
  134. isc_throw(isc::BadValue, "client-id is too short (" << clientid.size()
  135. << "), at least 2 is required");
  136. }
  137. }
  138. // Constructor based on C-style data
  139. ClientId::ClientId(const uint8_t *clientid, size_t len)
  140. : DUID(clientid, len) {
  141. if (len < MIN_CLIENT_ID_LEN) {
  142. isc_throw(isc::BadValue, "client-id is too short (" << len
  143. << "), at least 2 is required");
  144. }
  145. }
  146. // Returns a copy of client-id data
  147. const std::vector<uint8_t>& ClientId::getClientId() const {
  148. return (duid_);
  149. }
  150. // Returns the Client ID in text form
  151. std::string ClientId::toText() const {
  152. // As DUID is a private base class of ClientId, we can't access
  153. // its public toText() method through inheritance: instead we
  154. // need the interface of a ClientId::toText() that calls the
  155. // equivalent method in the base class.
  156. return (DUID::toText());
  157. }
  158. ClientIdPtr
  159. ClientId::fromText(const std::string& text) {
  160. std::vector<uint8_t> binary = decode(text);
  161. return (ClientIdPtr(new ClientId(binary)));
  162. }
  163. // Compares two client-ids
  164. bool ClientId::operator==(const ClientId& other) const {
  165. return (this->duid_ == other.duid_);
  166. }
  167. // Compares two client-ids
  168. bool ClientId::operator!=(const ClientId& other) const {
  169. return (this->duid_ != other.duid_);
  170. }
  171. }; // end of isc::dhcp namespace
  172. }; // end of isc namespace