duid.cc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright (C) 2012 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/io_utilities.h>
  17. #include <iomanip>
  18. #include <sstream>
  19. #include <vector>
  20. #include <stdint.h>
  21. namespace isc {
  22. namespace dhcp {
  23. DUID::DUID(const std::vector<uint8_t>& duid) {
  24. if (duid.size() > MAX_DUID_LEN) {
  25. isc_throw(OutOfRange, "DUID too large");
  26. } else {
  27. duid_ = duid;
  28. }
  29. }
  30. DUID::DUID(const uint8_t* data, size_t len) {
  31. if (len > MAX_DUID_LEN) {
  32. isc_throw(OutOfRange, "DUID too large");
  33. }
  34. duid_ = std::vector<uint8_t>(data, data + len);
  35. }
  36. const std::vector<uint8_t> DUID::getDuid() const {
  37. return (duid_);
  38. }
  39. DUID::DUIDType DUID::getType() const {
  40. if (duid_.size() < 2) {
  41. return (DUID_UNKNOWN);
  42. }
  43. uint16_t type = (duid_[0] << 8) + duid_[1];
  44. if (type < DUID_MAX) {
  45. return (static_cast<DUID::DUIDType>(type));
  46. } else {
  47. return (DUID_UNKNOWN);
  48. }
  49. }
  50. std::string DUID::toText() const {
  51. std::stringstream tmp;
  52. tmp << std::hex;
  53. bool delim = false;
  54. for (std::vector<uint8_t>::const_iterator it = duid_.begin();
  55. it != duid_.end(); ++it) {
  56. if (delim) {
  57. tmp << ":";
  58. }
  59. tmp << std::setw(2) << std::setfill('0') << static_cast<unsigned int>(*it);
  60. delim = true;
  61. }
  62. return (tmp.str());
  63. }
  64. bool DUID::operator==(const DUID& other) const {
  65. return (this->duid_ == other.duid_);
  66. }
  67. bool DUID::operator!=(const DUID& other) const {
  68. return (this->duid_ != other.duid_);
  69. }
  70. // Constructor based on vector<uint8_t>
  71. ClientId::ClientId(const std::vector<uint8_t>& clientid)
  72. : DUID(clientid) {
  73. }
  74. // Constructor based on C-style data
  75. ClientId::ClientId(const uint8_t *clientid, size_t len)
  76. : DUID(clientid, len) {
  77. }
  78. // Returns a copy of client-id data
  79. const std::vector<uint8_t> ClientId::getClientId() const {
  80. return (duid_);
  81. }
  82. // Compares two client-ids
  83. bool ClientId::operator==(const ClientId& other) const {
  84. return (this->duid_ == other.duid_);
  85. }
  86. // Compares two client-ids
  87. bool ClientId::operator!=(const ClientId& other) const {
  88. return (this->duid_ != other.duid_);
  89. }
  90. }; // end of isc::dhcp namespace
  91. }; // end of isc namespace