treenode_rrset.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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_TREENODE_RRSET_H
  15. #define DATASRC_MEMORY_TREENODE_RRSET_H 1
  16. #include <util/buffer.h>
  17. #include <dns/messagerenderer.h>
  18. #include <dns/name.h>
  19. #include <dns/rrclass.h>
  20. #include <dns/rrtype.h>
  21. #include <dns/rrttl.h>
  22. #include <dns/rdata.h>
  23. #include <dns/rrset.h>
  24. #include <datasrc/memory/zone_data.h>
  25. #include <datasrc/memory/rdataset.h>
  26. #include <boost/noncopyable.hpp>
  27. #include <string>
  28. namespace isc {
  29. namespace datasrc {
  30. namespace memory {
  31. /// \brief Special RRset for optimizing memory datasource requirement
  32. ///
  33. /// This is a derived class of \c dns::AbstractRRset intended to be used
  34. /// by the in-memory data source finder implementation. It is designed
  35. /// so performance sensitive operations will be lightweight; for example,
  36. /// (in the general case) the construction is just set up references to
  37. /// pre-loaded in-memory objects, not involving any dynamic memory allocation.
  38. /// Its \c toWire() method is also customized so it won't have to use
  39. /// the generic but expensive \c dns::RdataIterator.
  40. ///
  41. /// On the other hand, some other performance-insensitive methods could be
  42. /// even less efficient than the generic version. Those include \c getName(),
  43. /// \c toText(), and \c getRdataIterator() methods.
  44. ///
  45. /// \note Right now, the authoritative server's query processing still needs
  46. /// to use \c getRdataIterator() and \c getName() for relatively rare case
  47. /// operations. We should revise that part of the authoritative server
  48. /// implementation in the next phase in order to eliminate the bottleneck.
  49. ///
  50. /// Since this class is assumed to be instantiated only from the in-memory
  51. /// zone finder, which only returns immutable (const) \c RRset objects,
  52. /// we skip implementing non const virtual methods of this class.
  53. /// Unless the application intentionally breaks the constness or the class
  54. /// is abused outside of the in-memory data source implementation, this
  55. /// should be safe because such methods should never be called.
  56. ///
  57. /// Some other const member methods are still incomplete; if they are called
  58. /// it will result in an exception. In the expected usage of this class
  59. /// it should be safe, but we should eventually provide complete
  60. /// implementations of these methods.
  61. ///
  62. /// This class can internally maintain dynamically allocated resource.
  63. /// It would cause copying a class object complicated while objects of
  64. /// this class are not expected to be copyable in the usage, so it's
  65. /// explicitly defined non copyable.
  66. ///
  67. /// \note This class is exposed in this separate header file so that other
  68. /// part of the in-memory data source implementation and test code
  69. /// can refer to its definition, and only for that purpose. Otherwise this is
  70. /// essentially a private class of the in-memory data source implementation,
  71. /// and an application shouldn't directly refer to this class.
  72. class TreeNodeRRset : boost::noncopyable, public dns::AbstractRRset {
  73. public:
  74. /// \brief Normal case constructor.
  75. ///
  76. /// This class object is basically defined with a \c ZoneNode and
  77. /// \c RdataSet. The former determines the owner name of the RRset,
  78. /// and the latter provides the rest of the RRset parameters.
  79. /// Since the RR class is maintained outside of the \c ZoneData,
  80. /// it must be explicitly given as a constructor parameter.
  81. ///
  82. /// The \c RdataSet may or may not be associated with RRSIGs. It's
  83. /// fixed at the load time, but depending on the query context they
  84. /// may or may not be requested (and supposed to be visible to the
  85. /// caller). Since \c rdataset cannot be modified at the time of
  86. /// construction, a separate parameter (\c dnssec_ok) controls this
  87. /// policy. Any associated RRSIGs are visible if and only if \c dnssec_ok
  88. /// is true. If the RRset is not associated with RRSIGs, the value
  89. /// does not have any effect.
  90. ///
  91. /// In some rare cases \c rdataset may only consist of RRSIGs (when
  92. /// the zone contains an RRSIG that doesn't have covered RRsets).
  93. /// This class works for such cases, too.
  94. ///
  95. /// \throw none
  96. ///
  97. /// \param rrclass The RR class of the RRset. This must be consistent
  98. /// with the corresponding zone class.
  99. /// \param node The \c ZoneNode for the \c RRset. Must not be NULL.
  100. /// \param rdataset The \c RdataSet for the \c RRset. Must not be NULL.
  101. /// \param dnssec_ok Whether the RRSIGs for the RRset (if associated)
  102. /// should be visible to the caller.
  103. TreeNodeRRset(const dns::RRClass& rrclass, const ZoneNode* node,
  104. const RdataSet* rdataset, bool dnssec_ok) :
  105. node_(node), rdataset_(rdataset),
  106. rrsig_count_(rdataset_->getSigRdataCount()), rrclass_(rrclass),
  107. dnssec_ok_(dnssec_ok), name_(NULL), realname_(NULL),
  108. ttl_data_(rdataset->getTTLData()), ttl_(NULL)
  109. {}
  110. /// \brief Constructor with a specific TTL.
  111. ///
  112. /// This constructor is mostly the same as the normal version, but takes
  113. /// an extra parameter, \c ttl_data. It's expected to point to a memory
  114. /// region at least for 32 bits, and the corresponding 32-bit data will
  115. /// be used as wire-format TTL value of the RRset, instead of the TTL
  116. /// associated with \c rdataset.
  117. ///
  118. /// It's the caller's responsibility to guarantee the memory region is
  119. /// valid and intact throughout the lifetime of the RRset.
  120. ///
  121. /// \throw None
  122. TreeNodeRRset(const dns::RRClass& rrclass, const ZoneNode* node,
  123. const RdataSet* rdataset, bool dnssec_ok,
  124. const void* ttl_data) :
  125. node_(node), rdataset_(rdataset),
  126. rrsig_count_(rdataset_->getSigRdataCount()), rrclass_(rrclass),
  127. dnssec_ok_(dnssec_ok), name_(NULL), realname_(NULL),
  128. ttl_data_(ttl_data), ttl_(NULL)
  129. {}
  130. /// \brief Constructor for wildcard-expanded owner name.
  131. ///
  132. /// This constructor is mostly the same as the normal version, but takes
  133. /// an extra parameter, \c realname. It effectively overrides the owner
  134. /// name of the RRset; wherever the owner name is used (e.g., in the
  135. /// \c toWire() method), the specified name will be used instead of
  136. /// the name associated with \c node.
  137. ///
  138. /// The expected usage is \c node has a wildcard name (such as
  139. /// *.example.com), but this constructor does not enforce the assumption.
  140. ///
  141. /// \throw std::bad_alloc Memory allocation fails
  142. TreeNodeRRset(const dns::Name& realname, const dns::RRClass& rrclass,
  143. const ZoneNode* node, const RdataSet* rdataset,
  144. bool dnssec_ok) :
  145. node_(node), rdataset_(rdataset),
  146. rrsig_count_(rdataset_->getSigRdataCount()), rrclass_(rrclass),
  147. dnssec_ok_(dnssec_ok), name_(NULL), realname_(new dns::Name(realname)),
  148. ttl_data_(rdataset->getTTLData()), ttl_(NULL)
  149. {}
  150. virtual ~TreeNodeRRset() {
  151. delete realname_;
  152. delete ttl_;
  153. delete name_;
  154. }
  155. virtual unsigned int getRdataCount() const {
  156. return (rdataset_->getRdataCount());
  157. }
  158. virtual const dns::Name& getName() const;
  159. virtual const dns::RRClass& getClass() const {
  160. return (rrclass_);
  161. }
  162. virtual const dns::RRType& getType() const {
  163. return (rdataset_->type);
  164. }
  165. /// \brief Specialized version of \c getTTL() for \c TreeNodeRRset.
  166. virtual const dns::RRTTL& getTTL() const;
  167. /// \brief Specialized version of \c setTTL() for \c TreeNodeRRset.
  168. ///
  169. /// It throws \c isc::Unexpected unconditionally.
  170. virtual void setTTL(const dns::RRTTL& ttl);
  171. virtual std::string toText() const;
  172. virtual unsigned int toWire(dns::AbstractMessageRenderer& renderer) const;
  173. /// \brief Specialized version of \c toWire(buffer) for \c TreeNodeRRset.
  174. ///
  175. /// It throws \c isc::Unexpected unconditionally.
  176. virtual unsigned int toWire(util::OutputBuffer& buffer) const;
  177. /// \brief Specialized version of \c addRdata() for \c TreeNodeRRset.
  178. ///
  179. /// It throws \c isc::Unexpected unconditionally.
  180. virtual void addRdata(dns::rdata::ConstRdataPtr rdata);
  181. /// \brief Specialized version of \c addRdata() for \c TreeNodeRRset.
  182. ///
  183. /// It throws \c isc::Unexpected unconditionally.
  184. virtual void addRdata(const dns::rdata::Rdata& rdata);
  185. virtual dns::RdataIteratorPtr getRdataIterator() const;
  186. /// \brief Specialized version of \c getRRsig() for \c TreeNodeRRset.
  187. virtual dns::RRsetPtr getRRsig() const;
  188. virtual unsigned int getRRsigDataCount() const {
  189. return (dnssec_ok_ ? rrsig_count_ : 0);
  190. }
  191. ///
  192. /// \name Specialized version of RRsig related methods for
  193. /// \c TreeNodeRRset.
  194. ///
  195. /// These throw \c isc::Unexpected unconditionally.
  196. ////
  197. //@{
  198. virtual void addRRsig(const dns::rdata::ConstRdataPtr& rdata);
  199. virtual void addRRsig(const dns::rdata::RdataPtr& rdata);
  200. virtual void addRRsig(const dns::AbstractRRset& sigs);
  201. virtual void addRRsig(const dns::ConstRRsetPtr& sigs);
  202. virtual void addRRsig(const dns::RRsetPtr& sigs);
  203. virtual void removeRRsig();
  204. //@}
  205. /// \brief Specialized version of \c isSameKind() for \c TreeNodeRRset.
  206. ///
  207. /// As a kind of optimization, this implementation exploits the assumption
  208. /// of how \c TreeNodeRRset objects are created: They must be always
  209. /// created inside the in-memory data source finder implementation,
  210. /// and they are constructed with the \c realname parameter if and only
  211. /// if the corresponding query name is subject to wildcard substitution.
  212. ///
  213. /// So, if the given RRset is of \c TreeNodeRRset, and one and only one of
  214. /// of them has \c realname, they are considered to have different names.
  215. ///
  216. /// Also, this implementation does not compare RR classes explicitly;
  217. /// if two \c TreeNodeRRset objects belong to different RR classes,
  218. /// they should belong to different zone trees (according to the assumption
  219. /// of how the zone data are built), and therefore they cannot be at
  220. /// same zone node. So it's sufficient to compare the (address of the)
  221. /// node; if they are different they cannot be of the same kind.
  222. virtual bool isSameKind(const dns::AbstractRRset& abs_other) const;
  223. private:
  224. dns::RdataIteratorPtr getSigRdataIterator() const;
  225. // Common backend for getRdataIterator() and getSigRdataIterator()
  226. dns::RdataIteratorPtr getRdataIteratorInternal(bool is_rrsig,
  227. size_t count) const;
  228. // Return \c LabelSequence for the owner name regardless of how this
  229. /// class is constructed (with or without 'realname')
  230. dns::LabelSequence getOwnerLabels(
  231. uint8_t labels_buf[dns::LabelSequence::MAX_SERIALIZED_LENGTH]) const
  232. {
  233. if (realname_ != NULL) {
  234. return (dns::LabelSequence(*realname_));
  235. }
  236. return (node_->getAbsoluteLabels(labels_buf));
  237. }
  238. const ZoneNode* node_;
  239. const RdataSet* rdataset_;
  240. const size_t rrsig_count_;
  241. const dns::RRClass rrclass_;
  242. const bool dnssec_ok_;
  243. mutable dns::Name* name_;
  244. const dns::Name* const realname_;
  245. const void* const ttl_data_;
  246. mutable dns::RRTTL* ttl_;
  247. };
  248. typedef boost::shared_ptr<TreeNodeRRset> TreeNodeRRsetPtr;
  249. } // namespace memory
  250. } // namespace datasrc
  251. } // namespace isc
  252. #endif // DATASRC_MEMORY_TREENODE_RRSET_H
  253. // Local Variables:
  254. // mode: c++
  255. // End: