labelsequence.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. #ifndef __LABELSEQUENCE_H
  15. #define __LABELSEQUENCE_H 1
  16. #include <dns/name.h>
  17. #include <util/buffer.h>
  18. namespace isc {
  19. namespace dns {
  20. /// \brief Light-weight Accessor to Name object
  21. ///
  22. /// The purpose of this class is to easily match Names and parts of Names,
  23. /// without needing to copy the underlying data on each label strip.
  24. ///
  25. /// It can only work on existing Name objects, and the Name object MUST
  26. /// remain in scope during the entire lifetime of its associated
  27. /// LabelSequence(s).
  28. ///
  29. /// Upon creation of a LabelSequence, it records the offsets of the
  30. /// labels in the wireformat data of the Name. When stripLeft() or
  31. /// stripRight() is called on the LabelSequence, no changes in the
  32. /// Name's data occur, but the internal pointers of the
  33. /// LabelSequence are modified.
  34. ///
  35. /// LabelSequences can be compared to other LabelSequences, and their
  36. /// data can be requested (which then points to part of the original
  37. /// data of the associated Name object).
  38. ///
  39. class LabelSequence {
  40. // Name calls the private toText(bool) method of LabelSequence.
  41. friend std::string Name::toText(bool) const;
  42. public:
  43. /// \brief Constructs a LabelSequence for the given name
  44. ///
  45. /// \note The associated Name MUST remain in scope during the lifetime
  46. /// of this LabelSequence, since getData() refers to data from the
  47. /// Name object (the only data the LabelSequence stores are pointers
  48. /// to the labels in the Name object).
  49. ///
  50. /// \param name The Name to construct a LabelSequence for
  51. LabelSequence(const Name& name): name_(name),
  52. first_label_(0),
  53. last_label_(name.getLabelCount())
  54. {}
  55. /// \brief Return the wire-format data for this LabelSequence
  56. ///
  57. /// The data, is returned as a pointer to the original wireformat
  58. /// data of the original Name object, and the given len value is
  59. /// set to the number of octets that match this labelsequence.
  60. ///
  61. /// \note The data pointed to is only valid if the original Name
  62. /// object is still in scope
  63. ///
  64. /// \param len Pointer to a size_t where the length of the data
  65. /// will be stored (in number of octets)
  66. /// \return Pointer to the wire-format data of this label sequence
  67. const uint8_t* getData(size_t* len) const;
  68. /// \brief Return the length of the wire-format data of this LabelSequence
  69. ///
  70. /// This method returns the number of octets for the data that would
  71. /// be returned by the \c getData() method.
  72. ///
  73. /// Note that the return value of this method is always positive.
  74. /// Note also that if the return value of this method is 1, it means the
  75. /// sequence consists of the null label, i.e., a single "dot", and vice
  76. /// versa.
  77. ///
  78. /// \note The data pointed to is only valid if the original Name
  79. /// object is still in scope
  80. ///
  81. /// \return The length of the data of the label sequence in octets.
  82. size_t getDataLength() const;
  83. /// \brief Compares two label sequences for equality.
  84. ///
  85. /// Performs a (optionally case-insensitive) comparison between this
  86. /// LabelSequence and another LabelSequence for equality.
  87. ///
  88. /// \param other The LabelSequence to compare with
  89. /// \param case_sensitive If true, comparison is case-insensitive
  90. /// \return true if The label sequences consist are the same length,
  91. /// and contain the same data.
  92. bool equals(const LabelSequence& other, bool case_sensitive = false) const;
  93. /// \brief Compares two label sequences.
  94. ///
  95. /// Performs a (optionally case-insensitive) comparison between this
  96. /// LabelSequence and another LabelSequence.
  97. ///
  98. /// \param other The LabelSequence to compare with
  99. /// \param case_sensitive If true, comparison is case-insensitive
  100. /// \return a <code>NameComparisonResult</code> object representing the
  101. /// comparison result.
  102. NameComparisonResult compare(const LabelSequence& other,
  103. bool case_sensitive = false) const;
  104. /// \brief Remove labels from the front of this LabelSequence
  105. ///
  106. /// \note No actual memory is changed, this operation merely updates the
  107. /// internal pointers based on the offsets in the Name object.
  108. ///
  109. /// \exception OutOfRange if i is greater than or equal to the number
  110. /// of labels currently pointed to by this LabelSequence
  111. ///
  112. /// \param i The number of labels to remove.
  113. void stripLeft(size_t i);
  114. /// \brief Remove labels from the end of this LabelSequence
  115. ///
  116. /// \note No actual memory is changed, this operation merely updates the
  117. /// internal pointers based on the offsets in the Name object.
  118. ///
  119. /// \exception OutOfRange if i is greater than or equal to the number
  120. /// of labels currently pointed to by this LabelSequence
  121. ///
  122. /// \param i The number of labels to remove.
  123. void stripRight(size_t i);
  124. /// \brief Returns the current number of labels for this LabelSequence
  125. ///
  126. /// \return The number of labels
  127. size_t getLabelCount() const { return (last_label_ - first_label_); }
  128. /// \brief Convert the LabelSequence to a string.
  129. ///
  130. /// This method returns a <code>std::string</code> object representing the
  131. /// LabelSequence as a string. The returned string ends with a dot
  132. /// '.' if the label sequence is absolute.
  133. ///
  134. /// This function assumes the underlying name is in proper
  135. /// uncompressed wire format. If it finds an unexpected label
  136. /// character including compression pointer, an exception of class
  137. /// \c BadLabelType will be thrown. In addition, if resource
  138. /// allocation for the result string fails, a corresponding standard
  139. /// exception will be thrown.
  140. //
  141. /// \return a string representation of the <code>LabelSequence</code>.
  142. std::string toText() const;
  143. private:
  144. /// \brief Convert the LabelSequence to a string.
  145. ///
  146. /// This method is a version of the zero-argument toText() method,
  147. /// that accepts a <code>omit_final_dot</code> argument. The
  148. /// returned string ends with a dot '.' if
  149. /// <code>omit_final_dot</code> is <code>false</code>.
  150. ///
  151. /// This method is used as a helper for <code>Name::toText()</code>
  152. /// only.
  153. ///
  154. /// \param omit_final_dot whether to omit the trailing dot in the output.
  155. /// \return a string representation of the <code>LabelSequence</code>.
  156. std::string toText(bool omit_final_dot) const;
  157. public:
  158. /// \brief Calculate a simple hash for the label sequence.
  159. ///
  160. /// This method calculates a hash value for the label sequence as binary
  161. /// data. If \c case_sensitive is false, it ignores the case stored in
  162. /// the labels; specifically, it normalizes the labels by converting all
  163. /// upper case characters to lower case ones and calculates the hash value
  164. /// for the result.
  165. ///
  166. /// This method is intended to provide a lightweight way to store a
  167. /// relatively small number of label sequences in a hash table.
  168. /// For this reason it only takes into account data up to 16 octets
  169. /// (16 was derived from BIND 9's implementation). Also, the function does
  170. /// not provide any unpredictability; a specific sequence will always have
  171. /// the same hash value. It should therefore not be used in the context
  172. /// where an untrusted third party can mount a denial of service attack by
  173. /// forcing the application to create a very large number of label
  174. /// sequences that have the same hash value and expected to be stored in
  175. /// a hash table.
  176. ///
  177. /// \exception None
  178. ///
  179. /// \param case_sensitive
  180. /// \return A hash value for this label sequence.
  181. size_t getHash(bool case_sensitive) const;
  182. /// \brief Checks whether the label sequence is absolute
  183. ///
  184. /// \return true if the last label is the root label
  185. bool isAbsolute() const;
  186. private:
  187. const Name& name_;
  188. size_t first_label_;
  189. size_t last_label_;
  190. };
  191. ///
  192. /// \brief Insert the label sequence as a string into stream.
  193. ///
  194. /// This method convert the \c label_sequence into a string and inserts
  195. /// it into the output stream \c os.
  196. ///
  197. /// This function overloads the global operator<< to behave as described in
  198. /// ostream::operator<< but applied to \c LabelSequence objects.
  199. ///
  200. /// \param os A \c std::ostream object on which the insertion operation is
  201. /// performed.
  202. /// \param name The \c LabelSequence object output by the operation.
  203. /// \return A reference to the same \c std::ostream object referenced by
  204. /// parameter \c os after the insertion operation.
  205. std::ostream&
  206. operator<<(std::ostream& os, const LabelSequence& label_sequence);
  207. } // end namespace dns
  208. } // end namespace isc
  209. #endif