rdata_serialization.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  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_ENCODER_H
  15. #define DATASRC_MEMORY_RDATA_ENCODER_H 1
  16. #include <exceptions/exceptions.h>
  17. #include <dns/labelsequence.h>
  18. #include <dns/rdata.h>
  19. #include <dns/rrclass.h>
  20. #include <dns/rrtype.h>
  21. #include <boost/function.hpp>
  22. #include <boost/noncopyable.hpp>
  23. /// \file rdata_serialization.h
  24. ///
  25. /// This file defines a set of interfaces (classes, types, constants) to
  26. /// manipulate a given set of RDATA of the same type (normally associated with
  27. /// an RRset) that may be accompanied with RRSIGs in a memory efficient way.
  28. ///
  29. /// The entire set of RDATA is stored in a packed form in a contiguous
  30. /// memory region. It's opaque data, without containing non trivial
  31. /// data structures, so it can be located anywhere in the memory or even
  32. /// dumped to a file.
  33. ///
  34. /// Two main classes are provided: one is
  35. /// \c isc::datasrc::memory::RdataEncoder, which allows
  36. /// the application to create encoded data for a set of RDATA;
  37. /// the isc::datasrc::memory::RdataReader provides an interface to iterate
  38. /// over encoded set of RDATA for purposes such as data lookups or rendering
  39. /// the data into the wire format to create a DNS message.
  40. ///
  41. /// The actual encoding detail is private information to the implementation,
  42. /// and the application shouldn't assume anything about that except that
  43. /// each RDATA is considered to consist of one or more generic fields,
  44. /// and each field is typed as either opaque data or a domain name.
  45. /// A domain name field has additional attributes
  46. /// (see \c isc::datasrc::memory::RdataNameAttributes)
  47. /// so the application can change how the name should be handled in terms
  48. /// of the DNS protocol (e.g., whether it's subject to name compression).
  49. ///
  50. /// The following are the current implementation of internal encoding, shown
  51. /// only for reference. Applications must not assume this particular form
  52. /// for the encoded data; in fact, it can change in a future version of the
  53. /// implementation.
  54. /// \verbatim
  55. // The encoded data begin with a series of 16-bit length fields (values are
  56. // stored in the host byte order). The sequence may be empty.
  57. // uint16_t n1_1: size of 1st variable len field (if any) of 1st RDATA
  58. // uint16_t n1_2: size of 2nd variable len field of 1st RDATA
  59. // ...
  60. // uint16_t nN_M: size of last (Mth) variable len field of last (Nth) RDATA
  61. // uint16_t ns1: size of 1st RRSIG (if any) data
  62. // ...
  63. // uint16_t nsL: size of last (Lth) RRSIG data
  64. // A sequence of packed data fields follows:
  65. // uint8_t[]: data field value, length specified by nI_J (in case it's
  66. // variable-length) or by the per type field spec (in case it's
  67. // fixed-length).
  68. // or
  69. // opaque data, LabelSequence::getSerializedLength() bytes: data for a name
  70. // uint8_t[ns1]: 1st RRSIG data
  71. // ...
  72. // uint8_t[nsL]: last RRSIG data
  73. // \endverbatim
  74. ///
  75. /// As described above, this implementation treats RRSIGs as opaque data
  76. /// that don't contain any domain names. Technically, it has a "signer"
  77. /// domain name field in the sense of RFC4034. In practice, however, this
  78. /// field is essentially mere data; it's not subject to name compression,
  79. /// and since it's very likely to be a subdomain of (or equal to) the
  80. /// owner name of the corresponding RR (or, if used in a DNS message,
  81. /// some domain name that already appears before this field), so it won't
  82. /// be a target of name compression either. By treating the entire RRSIG
  83. /// as single-field data we can make the implementation simpler, and probably
  84. /// make it faster in rendering it into a DNS message.
  85. namespace isc {
  86. namespace datasrc {
  87. namespace memory {
  88. /// \brief General error in RDATA encoding.
  89. ///
  90. /// This is thrown when \c RdataEncoder encounters a rare, unsupported
  91. /// situation.
  92. class RdataEncodingError : public Exception {
  93. public:
  94. RdataEncodingError(const char* file, size_t line, const char* what) :
  95. Exception(file, line, what) {}
  96. };
  97. /// \brief RDATA encoder.
  98. ///
  99. /// This class provides interfaces to encode a set of RDATA of a specific
  100. /// RR class and type, possibly with their RRSIG RDATAs, in a memory-efficient
  101. /// format. In many cases these sets of RDATA come from a specific (signed
  102. /// or unsigned) RRset.
  103. ///
  104. /// It is expected for a single \c RdataEncoder object to be used multiple
  105. /// times for different sets of RDATA, such as in loading an entire zone
  106. /// into memory. Each encoding session begins with the \c start() method,
  107. /// which sets the context for the specific RR class and type to be encoded.
  108. /// Any number of calls to \c addRdata() or \c addSIGRdata() follow, each
  109. /// of which updates the internal state of the encoder with the encoding
  110. /// information for the given RDATA or RRSIG RDATA, respectively.
  111. /// The \c addRdata() is expected to be called with an
  112. /// \c isc::dns::rdata::Rdata object
  113. /// of the specified class and type, and \c addRdata() checks the consistency
  114. /// for the purpose of encoding (but it's not completely type safe; for
  115. /// example, it wouldn't distinguish TXT RDATA and HINFO RDATA.
  116. /// Likewise, an \c isc::dns::rdata::Rdata given to \c addSIGRdata() is
  117. /// expected to be of RRSIG, but the method does not check the assumption).
  118. ///
  119. /// After passing the complete set of RDATA and their RRSIG, the application
  120. /// is expected to call \c getStorageLength() to know the size of storage
  121. /// that is sufficient to store all encoded data. Normally the application
  122. /// would allocate a memory region of that size, and then call \c encode()
  123. /// with the prepared region. The \c encode() method dumps encoded data
  124. /// to the given memory region.
  125. ///
  126. /// The caller can reuse the \c RdataEncoder object for another set of RDATA
  127. /// by repeating the session from \c start().
  128. class RdataEncoder : boost::noncopyable {
  129. public:
  130. /// \brief Default constructor.
  131. RdataEncoder();
  132. /// \brief The destrcutor.
  133. ~RdataEncoder();
  134. /// \brief Start the encoding session.
  135. ///
  136. /// It re-initializes the internal encoder state for a new encoding
  137. /// session. The \c rrclass and \c rrtype parameters specify the
  138. /// type of RDATA to be encoded in the new session. Note that if the
  139. /// set of RDATA is signed, \c rrtype always specifies the "signed" type;
  140. /// it must not be RRSIG.
  141. ///
  142. /// \throw BadValue RRSIG is specified for rrtype.
  143. ///
  144. /// \param rrclass The RR class of RDATA to be encoded in the session.
  145. /// \param rrtype The RR type of RDATA to be encoded in the session.
  146. void start(dns::RRClass rrclass, dns::RRType rrtype);
  147. /// \brief Start the encoding session in the merge mode.
  148. ///
  149. /// This method is similar to the other version, but begins with a copy
  150. /// of previously encoded data and merges Rdata and RRSIGs into it
  151. /// that will be given via subsequent calls to \c addRdata() and
  152. /// \c addSIGRdata(). \c old_data, \c old_rdata_count, and
  153. /// \c old_sig_count correspond to parameters given to the
  154. /// \c RdataReader constructor, and must have valid values for encoded
  155. /// data by this class for the same \c rrclass and \c rrtype.
  156. /// It's the caller's responsibility to ensure this condition; if it's
  157. /// not met, the behavior will be undefined.
  158. ///
  159. /// The caller must also ensure that previously encoded data (pointed
  160. /// to by \c old_data) will be valid and intact throughout the encoding
  161. /// session started by this method. The resulting encoded data (by
  162. /// \c encode()) won't refer to the previous data, so once encoding the
  163. /// merged data is completed (and unless this encoding session continues
  164. /// for another attempt of encoding, which is unlikely), the caller can
  165. /// modify or destroy the old data.
  166. ///
  167. /// \param rrclass The RR class of RDATA to be encoded in the session.
  168. /// \param rrtype The RR type of RDATA to be encoded in the session.
  169. /// \param old_data Point to previously encoded data for the same RR
  170. /// class and type.
  171. /// \param old_rdata_count The number of RDATAs stored in \c old_data.
  172. /// \param old_sig_count The number of RRSIGs stored in \c old_data.
  173. void start(dns::RRClass rrclass, dns::RRType rrtype,
  174. const void* old_data, size_t old_rdata_count,
  175. size_t old_sig_count);
  176. /// \brief Add an RDATA for encoding.
  177. ///
  178. /// This method updates internal state of the \c RdataEncoder() with the
  179. /// given RDATA so it will be part of the encoded data in a subsequent
  180. /// call to \c encode().
  181. ///
  182. /// The given \c rdata must be of the RR class and type specified at
  183. /// the prior call to \c start(). This method checks the assumption
  184. /// to some extent, but the check is not complete; this is generally
  185. /// the responsibility of the caller.
  186. ///
  187. /// This method checks if the given RDATA is a duplicate of already
  188. /// added one (including ones encoded in the old data if the session
  189. /// began with the merge mode). If it's a duplicate this method ignores
  190. /// the given RDATA and returns false; otherwise it returns true.
  191. /// The check is based on the comparison in the "canonical form" as
  192. /// described in RFC4034 Section 6.2. In particular, domain name fields
  193. /// of the RDATA are generally compared in case-insensitive manner.
  194. ///
  195. /// The caller can destroy \c rdata after this call is completed.
  196. ///
  197. /// \note This implementation does not support RDATA (or any subfield of
  198. /// it) whose size exceeds 65535 bytes (max uint16_t value). Such RDATA
  199. /// may not necessarily be considered invalid in terms of protocol
  200. /// specification, but in practice it's mostly useless because the
  201. /// corresponding RR won't fit in any valid DNS message.
  202. ///
  203. /// As long as the \c rdata is of the correct type and its size is normal,
  204. /// this method should normally be exception free. If it throws, however,
  205. /// it doesn't always provide the strong exception guarantee. In general,
  206. /// the caller needs to either destroy the encoder object or restart a
  207. /// new session from \c start() should this method throws an exception.
  208. ///
  209. /// \throw InvalidOperation called before start().
  210. /// \throw std::bad_cast The given Rdata is of different RR type.
  211. /// \throw RdataEncodingError A very unusual case, such as over 64KB RDATA.
  212. /// \throw std::bad_alloc Internal memory allocation failure.
  213. ///
  214. /// \param rdata An RDATA to be encoded in the session.
  215. /// \return true if the given RDATA was added to encode; false if
  216. /// it's a duplicate and ignored.
  217. bool addRdata(const dns::rdata::Rdata& rdata);
  218. /// \brief Add an RRSIG RDATA for encoding.
  219. ///
  220. /// This method updates internal state of the \c RdataEncoder() with the
  221. /// given RDATA, which is assumed to be of type RRSIG that covers the
  222. /// type specified at the time of \c start() for the encoding session.
  223. /// The corresponding data for the RRSIG RDATA will be encoded in a
  224. /// subsequent call to \c encode().
  225. ///
  226. /// The passed \c sig_rdata is expected to be of type RRSIG and cover
  227. /// the RR type specified at the call to \c start() to this encoding
  228. /// session. But this method does not check if it is the case at all;
  229. /// it could even accept any type of RDATA as opaque data. It's caller's
  230. /// responsibility to ensure the assumption.
  231. ///
  232. /// This method checks if the given RRSIG RDATA is a duplicate of already
  233. /// added one (including ones encoded in the old data if the session
  234. /// began with the merge mode). If it's a duplicate this method ignores
  235. /// the given RRSIG and returns false; otherwise it returns true.
  236. /// The check is based on the comparison in the "canonical form" as
  237. /// described in RFC4034 Section 6.2.
  238. ///
  239. /// The caller can destroy \c rdata after this call is completed.
  240. ///
  241. /// \note Like addRdata(), this implementation does not support
  242. /// RRSIG RDATA whose size (in the form of wire format) exceeds 65535
  243. /// bytes.
  244. ///
  245. /// The same note about exception safety as \c addRdata() applies.
  246. ///
  247. /// \throw InvalidOperation called before start().
  248. /// \throw RdataEncodingError A very unusual case, such as over 64KB RDATA.
  249. /// \throw std::bad_alloc Internal memory allocation failure.
  250. ///
  251. /// \param sig_rdata An RDATA to be encoded in the session. Supposed to
  252. /// be of type RRSIG.
  253. /// \return true if the given RRSIG RDATA was added to encode; false if
  254. /// it's a duplicate and ignored.
  255. bool addSIGRdata(const dns::rdata::Rdata& sig_rdata);
  256. /// \brief Return the length of space for encoding for the session.
  257. ///
  258. /// It returns the size of the encoded data that would be generated for
  259. /// the set of RDATA (and RRSIGs) in the encoder at the call of this
  260. /// method. It's ensured that a buffer of that size can be safely passed
  261. /// to \c encode() unless there's no other "add" method is called by then.
  262. ///
  263. /// As long as this method is called after start(), it never throws.
  264. ///
  265. /// \throw InvalidOperation called before start().
  266. ///
  267. /// \return The expected size of the encoded data at the time of the call.
  268. size_t getStorageLength() const;
  269. /// \brief Encode RDATAs of the session to a buffer.
  270. ///
  271. /// This method dumps encoded data for the stored set of RDATA and
  272. /// their RRSIGs to a given buffer. The buffer must have a size
  273. /// at least as large as the return value of a prior call to
  274. /// \c getStorageLength() (it may be larger than that).
  275. ///
  276. /// The given buffer must be aligned at the natural boundary for
  277. /// 16-bit integers. The method doesn't check this condition; it's
  278. /// caller's responsibility to ensure that. Note: the alignment
  279. /// requirement may change in a future version of this implementation.
  280. ///
  281. /// As long as this method is called after start() and the buffer is
  282. /// valid with a sufficient size, this method never throws.
  283. ///
  284. /// \throw InvalidOperation called before start().
  285. /// \throw BadValue buffer is NULL or it's too short for the encoded data.
  286. ///
  287. /// \param buf A pointer to the buffer to which encoded data are to be
  288. /// dumped.
  289. /// \param buf_len The size of the buffer in bytes.
  290. void encode(void* buf, size_t buf_len) const;
  291. private:
  292. struct RdataEncoderImpl;
  293. RdataEncoderImpl* impl_;
  294. };
  295. /// \brief Attributes of domain name fields of encoded RDATA.
  296. ///
  297. /// The enum values define special traits of the name that can affect how
  298. /// it should be handled in rendering or query processing.
  299. enum RdataNameAttributes {
  300. NAMEATTR_NONE = 0, ///< No special attributes
  301. NAMEATTR_COMPRESSIBLE = 1, ///< Name should be compressed when rendered
  302. NAMEATTR_ADDITIONAL = (NAMEATTR_COMPRESSIBLE << 1) ///< Name requires
  303. ///< Additional section
  304. ///< handling
  305. };
  306. // forward declaration, defined in a private implementation file.
  307. struct RdataEncodeSpec;
  308. /// \brief Class to read serialized rdata
  309. ///
  310. /// This class allows you to read the data encoded by RdataEncoder.
  311. /// It is rather low-level -- it provides sequence of data fields.
  312. /// Each field is either opaque data, passed as a pointer and length,
  313. /// or a name, in the form of dns::LabelSequence (which is always
  314. /// absolute) and attributes.
  315. ///
  316. /// Conceptually, these fields correspond to consecutive regions in
  317. /// wire-format representation of the RDATA, varying the type of above
  318. /// two cases depending on whether the region corresponds to a domain
  319. /// name or other data. For example, for an MX RDATA the field
  320. /// sequence will be
  321. /// - 2 bytes of opaque data (which corresponds to the MX preference)
  322. /// - a domain name (which corresponds to the MX name)
  323. ///
  324. /// If the encoded data contain multiple MX RDATAs, the same type of
  325. /// sequence continues for the number of RDATAs. Note that the opaque
  326. /// data field does not always corresponds to a specific RDATA field
  327. /// as is the 2-byte preference field of MX. For example, the field
  328. /// sequence for an SOA RDATA in terms of RdataEncoder will be:
  329. /// - a domain name (which corresponds to the SOA MNAME)
  330. /// - a domain name (which corresponds to the SOA RNAME)
  331. /// - 20 bytes of opaque data (for the rest of fields)
  332. ///
  333. /// So, if you want to construct a general purpose dns::Rdata object
  334. /// from the field sequence, you'll need to build the complete
  335. /// wire-format data, and then construct a dns::Rdata object from it.
  336. ///
  337. /// To use it, contstruct it with the data you got from RDataEncoder,
  338. /// provide it with callbacks and then iterate through the data.
  339. /// The callbacks are called with the data fields contained in the
  340. /// data.
  341. ///
  342. /// \code
  343. /// void handleName(const dns::LabelSequence& labels, unsigned int flags) {
  344. /// ...
  345. /// }
  346. /// void handleData(const void* data, size_t size) {
  347. /// ...
  348. /// }
  349. ///
  350. /// RdataReader reader(RRClass::IN(), RRType::AAAA(), size, data,
  351. /// rdata_count, sig_count, &handleName, &handleData);
  352. /// reader.iterate();
  353. /// \endcode
  354. ///
  355. /// If you need to do the iteration per RDATA basis rather than per data field
  356. /// basis, you can use \c iterateRdata() as follows:
  357. ///
  358. /// \code
  359. /// for (size_t i = 0; i < rdata_count; ++i)
  360. /// // maybe do something related to this RDATA
  361. /// reader.iterateRdata(); // specified actions called for this RDATA
  362. /// // maybe do some other thing related to this RDATA
  363. /// }
  364. /// if (reader.iterateRdata()) {
  365. /// isc_throw(Unexpected, "Inconsistent data");
  366. /// }
  367. /// \endcode
  368. ///
  369. /// The check after the loop is primarily for consistency
  370. /// validation, but it would also help a possible subsequent call
  371. /// to \c iterateAllSigs() if you also want to iterate over RRSIGs;
  372. /// the final call to \c iterateRdata() updates the internal state of the
  373. /// reader object so \c iterateAllSigs() can find the RRSIG data more
  374. /// efficiently. \c iterateAllSigs() will work correctly even with out
  375. /// this small optimization, but checking the consistency is a good practice
  376. /// anyway, and the optimization is an additional bonus.
  377. ///
  378. /// \note It is caller's responsibility to pass valid data here. This means
  379. /// the data returned by RdataEncoder and the corresponding class and type.
  380. /// If this is not the case, all the kinds of pointer hell might get loose.
  381. class RdataReader {
  382. public:
  383. /// \brief Function called on each name encountered in the data.
  384. typedef boost::function<void(const dns::LabelSequence&,
  385. RdataNameAttributes)> NameAction;
  386. /// \brief Function called on each data field in the data.
  387. typedef boost::function<void(const void*, size_t)> DataAction;
  388. /// \brief An NameAction that does intentionally nothing.
  389. ///
  390. /// This static method can be used as the name action parameter to
  391. /// construct \c RdataReader when the caller does not have to anything
  392. /// for name fields.
  393. static void emptyNameAction(const dns::LabelSequence&,
  394. RdataNameAttributes);
  395. /// \brief An DataAction that does intentionally nothing.
  396. ///
  397. /// This static method can be used as the data action parameter to
  398. /// construct \c RdataReader when the caller does not have to anything
  399. /// for opaque data fields.
  400. static void emptyDataAction(const void*, size_t);
  401. /// \brief Constructor
  402. ///
  403. /// This constructs the reader on top of some serialized data.
  404. /// It does not copy the data, you have to make sure the data
  405. /// is valid for the whole life of this object and that they
  406. /// don't change.
  407. ///
  408. /// \param rrclass The class the encoded rdata belongs to.
  409. /// \param rrtype The type of the encode rdata.
  410. /// \param data The actual data.
  411. /// \param rdata_count The number of Rdata encoded in the data.
  412. /// \param sig_count The number of RRSig rdata bundled with the data.
  413. /// \param name_action The callback to be called on each encountered name.
  414. /// \param data_action The callback to be called on each data chunk.
  415. RdataReader(const dns::RRClass& rrclass, const dns::RRType& rrtype,
  416. const void* data, size_t rdata_count, size_t sig_count,
  417. const NameAction& name_action, const DataAction& data_action);
  418. /// \brief Result of next() and nextSig()
  419. ///
  420. /// This specifies if there's any boundary in the data at the
  421. /// place where the corresponding call to next() or nextSig()
  422. /// finished.
  423. enum Boundary {
  424. NO_BOUNDARY, ///< It is in the middle of Rdata
  425. RDATA_BOUNDARY, ///< At the end of single Rdata
  426. RRSET_BOUNDARY ///< At the end of the RRset (past the end)
  427. };
  428. /// \brief Step to next data field.
  429. ///
  430. /// Iterate over the next field and call appropriate hook (name_action
  431. /// or data_action, depending on the type) as passed to the constructor.
  432. ///
  433. /// \return It returns NO_BOUNDARY if the next call to next() will process
  434. /// data of the same rdata as this one. RDATA_BOUNDARY is returned when
  435. /// this field is the last of the current rdata. If there are no more
  436. /// data to process, no hook is called and RRSET_BOUNDARY is returned.
  437. /// Therefore, at the end of the whole data, once it processes the last
  438. /// field and returns RDATA_BOUNDARY and then it returns RRSET_BOUNDARY
  439. /// on the next call.
  440. Boundary next();
  441. /// \brief Call next() until the end.
  442. ///
  443. /// This is just convenience method to iterate through all the data.
  444. /// It calls next until it reaches the end (it does not rewind beforehand,
  445. /// therefore if you already called next() yourself, it does not start
  446. /// at the beginning).
  447. void iterate() {
  448. while (nextInternal(name_action_, data_action_) != RRSET_BOUNDARY) {}
  449. }
  450. /// \brief Call next() until the end of current rdata.
  451. ///
  452. /// This is a convenience method to iterate until the end of current
  453. /// rdata. Notice this may cause more than one field being processed,
  454. /// as some rrtypes are more complex.
  455. ///
  456. /// \return If there was Rdata to iterate through.
  457. bool iterateRdata() {
  458. while (true) {
  459. switch (nextInternal(name_action_, data_action_)) {
  460. case NO_BOUNDARY: break;
  461. case RDATA_BOUNDARY: return (true);
  462. case RRSET_BOUNDARY: return (false);
  463. }
  464. }
  465. }
  466. /// \brief Step to next field of RRSig data.
  467. ///
  468. /// This is almost the same as next(), but it iterates through the
  469. /// associated RRSig data, not the data for the given RRType.
  470. Boundary nextSig();
  471. /// \brief Iterate through all RRSig data.
  472. ///
  473. /// This is almost the same as iterate(), but it iterates through the
  474. /// RRSig data instead.
  475. void iterateAllSigs() {
  476. while (nextSig() != RRSET_BOUNDARY) {}
  477. }
  478. /// \brief Iterate through the current RRSig Rdata.
  479. ///
  480. /// This is almote the same as iterateRdata, except it is for single
  481. /// signature Rdata.
  482. ///
  483. /// In practice, this should process one DATA field.
  484. bool iterateSingleSig() {
  485. while (true) {
  486. switch (nextSig()) {
  487. case NO_BOUNDARY:
  488. isc_throw(isc::Unexpected, "NO_BOUNDARY inside an RRSig. "
  489. "Data corruption? Bug inside RdataReader?");
  490. case RDATA_BOUNDARY: return (true);
  491. case RRSET_BOUNDARY: return (false);
  492. }
  493. }
  494. }
  495. /// \brief Rewind the iterator to the beginnig of data.
  496. ///
  497. /// The following next() and nextSig() will start iterating from the
  498. /// beginning again.
  499. void rewind();
  500. /// \brief Returns the size of associated data.
  501. ///
  502. /// This should be the same as the return value of
  503. /// RdataEncoder::getStorageLength() for the same set of data.
  504. /// The intended use of this method is to tell the caller the size of
  505. /// data that were possibly dynamically allocated so that the caller can
  506. /// use it for deallocation.
  507. ///
  508. /// This method only uses the parameters given at the construction of the
  509. /// object, and does not rely on or modify other mutable states.
  510. /// In practice, when the caller wants to call this method, that would be
  511. /// the only purpose of that RdataReader object (although it doesn't have
  512. /// to be so).
  513. size_t getSize() const;
  514. private:
  515. const NameAction name_action_;
  516. const DataAction data_action_;
  517. const RdataEncodeSpec& spec_;
  518. // Total number of var-length fields, count of signatures
  519. const size_t var_count_total_, sig_count_, spec_count_;
  520. // Pointer to the beginning of length fields
  521. const uint16_t* const lengths_;
  522. // Pointer to the beginning of the data (after the lengths)
  523. const uint8_t* const data_;
  524. // Pointer to the first data signature
  525. // Will be computed during the normal RR iteration
  526. const uint8_t* sigs_;
  527. // The positions in data.
  528. size_t data_pos_, spec_pos_, length_pos_;
  529. size_t sig_pos_, sig_data_pos_;
  530. Boundary nextInternal(const NameAction& name_action,
  531. const DataAction& data_action);
  532. };
  533. } // namespace memory
  534. } // namespace datasrc
  535. } // namespace isc
  536. #endif // DATASRC_MEMORY_RDATA_ENCODER_H
  537. // Local Variables:
  538. // mode: c++
  539. // End: