duid.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. // Copyright (C) 2012-2014 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. #ifndef DUID_H
  15. #define DUID_H
  16. #include <asiolink/io_address.h>
  17. #include <vector>
  18. #include <stdint.h>
  19. #include <unistd.h>
  20. namespace isc {
  21. namespace dhcp {
  22. /// @brief Holds DUID (DHCPv6 Unique Identifier)
  23. ///
  24. /// This class holds DUID, that is used in client-id, server-id and
  25. /// several other options. It is used to identify DHCPv6 entity.
  26. class DUID {
  27. public:
  28. /// @brief maximum duid size
  29. /// As defined in RFC3315, section 9.1
  30. static const size_t MAX_DUID_LEN = 128;
  31. /// @brief minimum duid size
  32. /// There's no explicit minimal DUID size specified in RFC3315,
  33. /// so let's use absolute minimum
  34. static const size_t MIN_DUID_LEN = 1;
  35. /// @brief specifies DUID type
  36. typedef enum {
  37. DUID_UNKNOWN = 0, ///< invalid/unknown type
  38. DUID_LLT = 1, ///< link-layer + time, see RFC3315, section 9.2
  39. DUID_EN = 2, ///< enterprise-id, see RFC3315, section 9.3
  40. DUID_LL = 3, ///< link-layer, see RFC3315, section 9.4
  41. DUID_UUID = 4, ///< UUID, see RFC6355
  42. DUID_MAX ///< not a real type, just maximum defined value + 1
  43. } DUIDType;
  44. /// @brief Constructor from vector
  45. DUID(const std::vector<uint8_t>& duid);
  46. /// @brief Constructor from array and array size
  47. DUID(const uint8_t* duid, size_t len);
  48. /// @brief Returns a const reference to the actual DUID value
  49. ///
  50. /// @warning Since this function returns a reference to the vector (not a
  51. /// copy) the returned object must be used with caution because it remains
  52. /// valid only for the time period when the object which returned it is
  53. /// valid.
  54. ///
  55. /// @return A reference to a vector holding a DUID.
  56. const std::vector<uint8_t>& getDuid() const;
  57. /// @brief Returns the DUID type
  58. DUIDType getType() const;
  59. /// @brief Create DUID from the textual format.
  60. ///
  61. /// This static function parses a DUID specified in the textual format.
  62. /// Internally it uses @c DUID::decode to parse the DUID.
  63. ///
  64. /// @param text DUID in the hexadecimal format with digits representing
  65. /// individual bytes separated by colons.
  66. ///
  67. /// @throw isc::BadValue if parsing the DUID failed.
  68. static DUID fromText(const std::string& text);
  69. /// @brief Returns textual representation of a DUID (e.g. 00:01:02:03:ff)
  70. std::string toText() const;
  71. /// @brief Compares two DUIDs for equality
  72. bool operator==(const DUID& other) const;
  73. /// @brief Compares two DUIDs for inequality
  74. bool operator!=(const DUID& other) const;
  75. protected:
  76. /// @brief Decodes the textual format of the DUID.
  77. ///
  78. /// The format being parsed should match the DUID representation returned
  79. /// by the @c DUID::toText method, i.e. the pairs of hexadecimal digits
  80. /// representing bytes of DUID must be separated by colons. Usually the
  81. /// single byte is represented by two hexadecimal digits. However, this
  82. /// function allows one digit per byte. In this case, a zero is prepended
  83. /// before the conversion. For example, a DUID 0:1:2:3:4:5 equals to
  84. /// 00:01:02:03:04:05.
  85. ///
  86. /// @param text DUID in the hexadecimal format with digits representing
  87. /// individual bytes separated by colons.
  88. ///
  89. /// @throw isc::BadValue if parsing the DUID failed.
  90. static std::vector<uint8_t> decode(const std::string& text);
  91. /// The actual content of the DUID
  92. std::vector<uint8_t> duid_;
  93. };
  94. /// @brief Shared pointer to a DUID
  95. typedef boost::shared_ptr<DUID> DuidPtr;
  96. /// @brief Forward declaration to the @c ClientId class.
  97. class ClientId;
  98. /// @brief Shared pointer to a Client ID.
  99. typedef boost::shared_ptr<ClientId> ClientIdPtr;
  100. /// @brief Holds Client identifier or client IPv4 address
  101. ///
  102. /// This class is intended to be a generic IPv4 client identifier. It can hold
  103. /// a client-id
  104. class ClientId : public DUID {
  105. public:
  106. /// @brief Minimum size of a client ID
  107. ///
  108. /// Excerpt from RFC2132, section 9.14.
  109. /// The code for this option is 61, and its minimum length is 2.
  110. static const size_t MIN_CLIENT_ID_LEN = 2;
  111. /// @brief Maximum size of a client ID
  112. ///
  113. /// This is the same as the maximum size of the underlying DUID.
  114. ///
  115. /// @note RFC 2131 does not specify an upper length of a client ID, the
  116. /// value chosen here just being that of the underlying DUID. For
  117. /// some backend database, there may be a possible (minor)
  118. /// performance enhancement if this were smaller.
  119. static const size_t MAX_CLIENT_ID_LEN = DUID::MAX_DUID_LEN;
  120. /// @brief Constructor based on vector<uint8_t>
  121. ClientId(const std::vector<uint8_t>& clientid);
  122. /// @brief Constructor based on array and array size
  123. ClientId(const uint8_t* clientid, size_t len);
  124. /// @brief Returns reference to the client-id data.
  125. ///
  126. /// @warning Since this function returns a reference to the vector (not a
  127. /// copy) the returned object must be used with caution because it remains
  128. /// valid only for the time period when the object which returned it is
  129. /// valid.
  130. ///
  131. /// @return A reference to a vector holding a client identifier.
  132. const std::vector<uint8_t>& getClientId() const;
  133. /// @brief Returns textual representation of a DUID (e.g. 00:01:02:03:ff)
  134. std::string toText() const;
  135. /// @brief Create client identifier from the textual format.
  136. ///
  137. /// This static function creates the instance of the @c ClientId from the
  138. /// textual format. Internally it calls @c DUID::fromText. The format of
  139. /// the input must match the format of the DUID in @c DUID::fromText.
  140. ///
  141. /// @param text Client identifier in the textual format.
  142. ///
  143. /// @return Pointer to the instance of the @c ClientId.
  144. /// @throw isc::BadValue if parsing the client identifier failed.
  145. /// @throw isc::OutOfRange if the client identifier is truncated.
  146. static ClientIdPtr fromText(const std::string& text);
  147. /// @brief Compares two client-ids for equality
  148. bool operator==(const ClientId& other) const;
  149. /// @brief Compares two client-ids for inequality
  150. bool operator!=(const ClientId& other) const;
  151. };
  152. }; // end of isc::dhcp namespace
  153. }; // end of isc namespace
  154. #endif /* DUID_H */