rdata_reader.h 9.2 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 DATASRC_MEMORY_RDATA_READER_H
  15. #define DATASRC_MEMORY_RDATA_READER_H 1
  16. #include "rdata_field.h"
  17. #include <boost/function.hpp>
  18. #include <dns/labelsequence.h>
  19. #include <dns/name.h>
  20. namespace isc {
  21. // Some forward declarations
  22. namespace dns{
  23. class RRClass;
  24. class RRType;
  25. }
  26. namespace datasrc {
  27. namespace memory {
  28. /// \brief Class to read serialized rdata
  29. ///
  30. /// This class allows you to read the data encoded by RDataEncoder.
  31. /// It is rather low-level -- it provides sequence of data fields.
  32. /// Each field is either opaque data, passed as a pointer and length,
  33. /// or a name, in the form of dns::LabelSequence (which is always
  34. /// absolute) and attributes.
  35. ///
  36. /// Conceptually, these fields correspond to consecutive regions in
  37. /// wire-format representation of the RDATA, varying the type of above
  38. /// two cases depending on whether the region corresponds to a domain
  39. /// name or other data. For example, for an MX RDATA the field
  40. /// sequence will be
  41. /// - 2 bytes of opaque data (which corresponds to the MX preference)
  42. /// - a domain name (which corresponds to the MX name)
  43. /// If the encoded data contain multiple MX RDATAs, the same type of
  44. /// sequence continues for the number of RDATAs. Note that the opaque
  45. /// data field does not always correspond to a specific RDATA field
  46. /// as is the 2-byte preference field of MX. For example, the field
  47. /// sequence for an SOA RDATA in terms of `RdataEncoder` will be:
  48. /// - a domain name (which corresponds to the SOA MNAME)
  49. /// - a domain name (which corresponds to the SOA RNAME)
  50. /// - 20 bytes of opaque data (for the rest of fields)
  51. ///
  52. /// So, if you want to construct a general purpose dns::Rdata object
  53. /// from the field sequence, you'll need to build the complete
  54. /// wire-format data, and then construct a dns::Rdata object from it.
  55. ///
  56. /// To use it, contstruct it with the data you got from RDataEncoder,
  57. /// provide it with callbacks and then iterate through the data.
  58. /// The callbacks are called with the data fields contained in the
  59. /// data.
  60. ///
  61. /// \code
  62. /// void handleName(const dns::LabelSequence& labels, unsigned int flags) {
  63. /// ...
  64. /// }
  65. /// void handleData(const uint8_t* data, size_t size) {
  66. /// ...
  67. /// }
  68. ///
  69. /// RdataReader reader(RRClass::IN(), RRType::AAAA(), size, data,
  70. /// &handleName, &handleData);
  71. /// reader.iterate();
  72. /// \endcode
  73. ///
  74. /// \note It is caller's responsibility to pass valid data here. This means
  75. /// the data returned by RdataEncoder and the corresponding class and type.
  76. /// If this is not the case, all the kinds of pointer hell might get loose.
  77. class RdataReader {
  78. public:
  79. /// \brief Function called on each name encountered in the data.
  80. typedef boost::function<void(const dns::LabelSequence&,
  81. RdataNameAttributes)> NameAction;
  82. /// \brief Function called on each data field in the data.
  83. typedef boost::function<void(const uint8_t*, size_t)> DataAction;
  84. /// \brief Constructor
  85. ///
  86. /// This constructs the reader on top of some serialized data.
  87. /// It does not copy the data, you have to make sure the data
  88. /// is valid for the whole life of this object and that they
  89. /// don't change.
  90. ///
  91. /// \param rrclass The class the encoded rdata belongs to.
  92. /// \param rrtype The type of the encode rdata.
  93. /// \param data The actual data.
  94. /// \param rdata_count The number of Rdata encoded in the data.
  95. /// \param sig_count The number of RRSig rdata bundled with the data.
  96. /// \param name_action The callback to be called on each encountered name.
  97. /// \param data_action The callback to be called on each data chunk.
  98. RdataReader(const dns::RRClass& rrclass, const dns::RRType& rrtype,
  99. const uint8_t* data, size_t rdata_count, size_t sig_count,
  100. const NameAction& name_action, const DataAction& data_action);
  101. /// \brief Result of next() and nextSig()
  102. ///
  103. /// This specifies if there's any boundary in the data at the
  104. /// place where the corresponding call to next() or nextSig()
  105. /// finished.
  106. enum Boundary {
  107. NO_BOUNDARY, ///< It is in the middle of Rdata
  108. RDATA_BOUNDARY, ///< At the end of single Rdata
  109. RRSET_BOUNDARY ///< At the end of the RRset (past the end)
  110. };
  111. /// \brief Step to next data field.
  112. ///
  113. /// Iterate over the next field and call appropriate hook (name_action
  114. /// or data_action, depending on the type) as passed to the constructor.
  115. ///
  116. /// \return It returns NO_BOUNDARY if the next call to next() will process
  117. /// data of the same rdata as this one. RDATA_BOUNDARY is returned when
  118. /// this field is the last of the current rdata. If there are no more
  119. /// data to process, no hook is called and RRSET_BOUNDARY is returned.
  120. /// Therefore, at the end of the whole data, once it processes the last
  121. /// field and returns RDATA_BOUNDARY and then it returns RRSET_BOUNDARY
  122. /// on the next call.
  123. Boundary next();
  124. /// \brief Call next() until the end.
  125. ///
  126. /// This is just convenience method to iterate through all the data.
  127. /// It calls next until it reaches the end (it does not revind before,
  128. /// therefore if you already called next() yourself, it does not start
  129. /// at the beginning).
  130. void iterate() {
  131. while (next() != RRSET_BOUNDARY) { }
  132. }
  133. /// \brief Call next() until the end of current rdata.
  134. ///
  135. /// This is a convenience method to iterate until the end of current
  136. /// rdata. Notice this may cause more than one field being processed,
  137. /// as some rrtypes are more complex.
  138. ///
  139. /// \return If there was Rdata to iterate through.
  140. bool iterateRdata() {
  141. while (true) {
  142. switch(next()) {
  143. case NO_BOUNDARY: break;
  144. case RDATA_BOUNDARY: return (true);
  145. case RRSET_BOUNDARY: return (false);
  146. }
  147. }
  148. }
  149. /// \brief Step to next field of RRSig data.
  150. ///
  151. /// This is almost the same as next(), but it iterates through the
  152. /// associated RRSig data, not the data for the given RRType.
  153. Boundary nextSig();
  154. /// \brief Iterate through all RRSig data.
  155. ///
  156. /// This is almost the same as iterate(), but it iterates through the
  157. /// RRSig data instead.
  158. void iterateAllSigs() {
  159. while (nextSig() != RRSET_BOUNDARY) { }
  160. }
  161. /// \brief Iterate through the current RRSig Rdata.
  162. ///
  163. /// This is almote the same as iterateRdata, except it is for single
  164. /// signature Rdata.
  165. ///
  166. /// In practice, this should process one DATA field.
  167. bool iterateSingleSig() {
  168. while (true) {
  169. switch(nextSig()) {
  170. case NO_BOUNDARY: break;
  171. case RDATA_BOUNDARY: return (true);
  172. case RRSET_BOUNDARY: return (false);
  173. }
  174. }
  175. }
  176. /// \brief Rewind the iterator to the beginnig of data.
  177. ///
  178. /// The following next() and nextSig() will start iterating from the
  179. /// beginning again.
  180. void rewind();
  181. /// \brief Returns the size of associated data.
  182. ///
  183. /// This should be the same as the return value of
  184. /// RdataEncoder::getStorageLength() for the same set of data.
  185. /// The intended use of this method is to tell the caller the size of
  186. /// data that were possibly dynamically allocated so that the caller can
  187. /// use it for deallocation.
  188. ///
  189. /// This method only uses the parameters given at the construction of the
  190. /// object, and does not rely on or modify other mutable states.
  191. /// In practice, when the caller wants to call this method, that would be
  192. /// the only purpose of that RdataReader object (although it doesn't have
  193. /// to be so).
  194. size_t getSize() const;
  195. private:
  196. const NameAction name_action_;
  197. const DataAction data_action_;
  198. const RdataEncodeSpec& spec_;
  199. // Total number of var-length fields, count of signatures
  200. const size_t var_count_total_, sig_count_, spec_count_;
  201. // Pointer to the beginning of length fields
  202. const uint16_t* const lengths_;
  203. // Pointer to the beginning of the data (after the lengths)
  204. const uint8_t* const data_;
  205. // Pointer to the first data signature
  206. // Will be computed during the normal RR iteration
  207. const uint8_t* sigs_;
  208. // The positions in data.
  209. size_t data_pos_, spec_pos_, length_pos_;
  210. size_t sig_pos_, sig_data_pos_;
  211. Boundary nextInternal(const NameAction& name_action,
  212. const DataAction& data_action);
  213. };
  214. }
  215. }
  216. }
  217. #endif