labelsequence.h 11 KB

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