rdata_encoder.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. #include <vector>
  24. namespace isc {
  25. namespace datasrc {
  26. namespace memory {
  27. /// \brief General error in RDATA encoding.
  28. ///
  29. /// This is thrown when \c RdataEncoder encounters a rare, unsupported
  30. /// situation. a method is called for a name or RRset which
  31. /// is not in or below the zone.
  32. class RdataEncodingError : public Exception {
  33. public:
  34. RdataEncodingError(const char* file, size_t line, const char* what) :
  35. Exception(file, line, what) {}
  36. };
  37. /// \brief Attributes of domain name fields of encoded RDATA.
  38. ///
  39. /// The enum values define special traits of the name that can affect how
  40. /// it should be handled in rendering or query processing.
  41. enum RdataNameAttributes {
  42. NAMEATTR_COMPRESSIBLE = 1, ///< Name should be compressed when rendered
  43. NAMEATTR_ADDITIONAL = (NAMEATTR_COMPRESSIBLE << 1) ///< Name requires
  44. ///< Additional section
  45. ///< handling
  46. };
  47. /// \brief TBD
  48. ///
  49. /// Encoding, FYI:
  50. /// uint16_t n1_1: size of 1st variable len field (if any) of 1st RDATA
  51. /// uint16_t n1_2: size of 2nd variable len field of 1st RDATA
  52. /// ...
  53. /// uint16_t nN_M: size of last (Mth) variable len field of last (Nth) RDATA
  54. /// A sequence of packed data fields follow:
  55. /// uint8_t[]: data field value, length specified by nI_J (in case it's
  56. /// variable) or by the field spec (in case it's fixed-length).
  57. /// or
  58. /// opaque data, LabelSequence::getSerializedLength() bytes: data for a name
  59. /// (a possible 1-byte padding)
  60. /// uint16_t ns1: size of 1st RRSIG data
  61. /// ...
  62. /// uint16_t nsL: size of last (Lth) RRSIG data
  63. /// uint8_t[ns1]: 1st RRSIG data
  64. /// ...
  65. /// uint8_t[nsL]: last RRSIG data
  66. class RdataEncoder : boost::noncopyable {
  67. public:
  68. /// \brief Default constructor.
  69. RdataEncoder();
  70. /// \brief The destrcutor.
  71. ~RdataEncoder();
  72. /// \brief TBD
  73. ///
  74. /// \throw BadValue RRSIG is specified as rrtype.
  75. void start(dns::RRClass rrclass, dns::RRType rrtype);
  76. /// \brief TBD
  77. ///
  78. /// This implementation does not support RDATA (or any subfield of it)
  79. /// whose size exceeds 65535 bytes (max uint16_t value). Such RDATA
  80. /// may not necessarily considered invalid in terms of protocol
  81. /// specification, but in practice it's mostly useless because the
  82. /// corresponding RR won't fit in any valid DNS message.
  83. ///
  84. /// \throw InvalidOperation called before start().
  85. /// \throw BadValue inconsistent data found.
  86. /// \throw RdataEncodingError A very unusual case, such as over 64KB RDATA.
  87. /// \throw std::bad_alloc Internal memory allocation failure.
  88. void addRdata(const dns::rdata::Rdata& rdata);
  89. /// \brief TBD
  90. ///
  91. /// \throw InvalidOperation called before start().
  92. size_t getStorageLength() const;
  93. /// \brief TBD
  94. ///
  95. /// \throw InvalidOperation called before start().
  96. /// \throw BadValue buffer is NULL or it's too short for the encoded data.
  97. void encode(void* buf, size_t buf_len) const;
  98. private:
  99. struct RdataEncoderImpl;
  100. RdataEncoderImpl* impl_;
  101. };
  102. // We use the following quick-hack version of encoder and "foreach"
  103. // operator until we implement the complete versions. The plan is to
  104. // update the test cases that use these functions with the complete
  105. // functions/classes, and then remove the entire namespace.
  106. namespace testing {
  107. // Callbacks used in foreachRdataField.
  108. typedef boost::function<void(const dns::LabelSequence&,
  109. RdataNameAttributes)> NameCallback;
  110. typedef boost::function<void(const uint8_t*, size_t)> DataCallback;
  111. // Iterate over each field (in terms of the internal encoding) of each
  112. // RDATA stored in encoded_data, and call the given callback for each
  113. // data (for domain name fields, name_callback will be called; for
  114. // normal data fields data_callback will be called). rdata_count is
  115. // the number of RDATAs. If the encoded data contain variable-length
  116. // data fields, varlen_list should store a sequence of their lengths,
  117. // in the of the appearance.
  118. void foreachRdataField(dns::RRClass rrclass, dns::RRType rrtype,
  119. size_t rdata_count,
  120. const std::vector<uint8_t>& encoded_data,
  121. const std::vector<uint16_t>& varlen_list,
  122. NameCallback name_callback, DataCallback data_callback);
  123. }
  124. } // namespace memory
  125. } // namespace datasrc
  126. } // namespace isc
  127. #endif // DATASRC_MEMORY_RDATA_ENCODER_H
  128. // Local Variables:
  129. // mode: c++
  130. // End: