duid.cc 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 <vector>
  15. #include <exceptions/exceptions.h>
  16. #include <stdint.h>
  17. #include <util/io_utilities.h>
  18. #include <dhcp/duid.h>
  19. namespace isc {
  20. namespace dhcp {
  21. DUID::DUID(const std::vector<uint8_t>& duid) {
  22. if (duid.size() > MAX_DUID_LEN) {
  23. isc_throw(OutOfRange, "DUID too large");
  24. } else {
  25. duid_ = duid;
  26. }
  27. }
  28. DUID::DUID(const uint8_t * data, size_t len) {
  29. if (len > MAX_DUID_LEN) {
  30. isc_throw(OutOfRange, "DUID too large");
  31. }
  32. duid_ = std::vector<uint8_t>(data, data + len);
  33. }
  34. const std::vector<uint8_t> DUID::getDuid() const {
  35. return (duid_);
  36. }
  37. DUID::DUIDType DUID::getType() const {
  38. if (duid_.size() < 2) {
  39. return (DUID_UNKNOWN);
  40. }
  41. uint16_t type = (duid_[0] << 8) + duid_[1];
  42. if (type < DUID_MAX) {
  43. return (static_cast<DUID::DUIDType>(type));
  44. } else {
  45. return (DUID_UNKNOWN);
  46. }
  47. }
  48. bool DUID::operator == (const DUID& other) const {
  49. return (this->duid_ == other.duid_);
  50. }
  51. bool DUID::operator != (const DUID& other) const {
  52. return (this->duid_ != other.duid_);
  53. }
  54. /// constructor based on vector<uint8_t>
  55. ClientId::ClientId(const std::vector<uint8_t>& clientid)
  56. :DUID(clientid) {
  57. }
  58. /// constructor based on C-style data
  59. ClientId::ClientId(const uint8_t *clientid, size_t len)
  60. :DUID(clientid, len) {
  61. }
  62. /// @brief returns a copy of client-id data
  63. const std::vector<uint8_t> ClientId::getClientId() const {
  64. return (duid_);
  65. }
  66. // compares two client-ids
  67. bool ClientId::operator == (const ClientId& other) const {
  68. return (this->duid_ == other.duid_);
  69. }
  70. // compares two client-ids
  71. bool ClientId::operator != (const ClientId& other) const {
  72. return (this->duid_ != other.duid_);
  73. }
  74. }; // end of isc::dhcp namespace
  75. }; // end of isc namespace