treenode_rrset.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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), ttl_(NULL)
  108. {}
  109. /// \brief Constructor for wildcard-expanded owner name.
  110. ///
  111. /// This constructor is mostly the same as the other version, but takes
  112. /// an extra parameter, \c realname. It effectively overrides the owner
  113. /// name of the RRset; wherever the owner name is used (e.g., in the
  114. /// \c toWire() method), the specified name will be used instead of
  115. /// the name associated with \c node.
  116. ///
  117. /// The expected usage is \c node has a wildcard name (such as
  118. /// *.example.com), but this constructor does not enforce the assumption.
  119. ///
  120. /// \throw std::bad_alloc Memory allocation fails
  121. TreeNodeRRset(const dns::Name& realname, const dns::RRClass& rrclass,
  122. const ZoneNode* node, const RdataSet* rdataset,
  123. bool dnssec_ok) :
  124. node_(node), rdataset_(rdataset),
  125. rrsig_count_(rdataset_->getSigRdataCount()), rrclass_(rrclass),
  126. dnssec_ok_(dnssec_ok), name_(NULL), realname_(new dns::Name(realname)),
  127. ttl_(NULL)
  128. {}
  129. virtual ~TreeNodeRRset() {
  130. delete realname_;
  131. delete ttl_;
  132. delete name_;
  133. }
  134. virtual unsigned int getRdataCount() const {
  135. return (rdataset_->getRdataCount());
  136. }
  137. virtual const dns::Name& getName() const;
  138. virtual const dns::RRClass& getClass() const {
  139. return (rrclass_);
  140. }
  141. virtual const dns::RRType& getType() const {
  142. return (rdataset_->type);
  143. }
  144. /// \brief Specialized version of \c getTTL() for \c TreeNodeRRset.
  145. virtual const dns::RRTTL& getTTL() const;
  146. /// \brief Specialized version of \c setName() for \c TreeNodeRRset.
  147. ///
  148. /// It throws \c isc::Unexpected unconditionally.
  149. virtual void setName(const dns::Name& name);
  150. /// \brief Specialized version of \c setName() for \c TreeNodeRRset.
  151. ///
  152. /// It throws \c isc::Unexpected unconditionally.
  153. virtual void setTTL(const dns::RRTTL& ttl);
  154. virtual std::string toText() const;
  155. virtual unsigned int toWire(dns::AbstractMessageRenderer& renderer) const;
  156. /// \brief Specialized version of \c toWire(buffer) for \c TreeNodeRRset.
  157. ///
  158. /// It throws \c isc::Unexpected unconditionally.
  159. virtual unsigned int toWire(util::OutputBuffer& buffer) const;
  160. /// \brief Specialized version of \c addRdata() for \c TreeNodeRRset.
  161. ///
  162. /// It throws \c isc::Unexpected unconditionally.
  163. virtual void addRdata(dns::rdata::ConstRdataPtr rdata);
  164. /// \brief Specialized version of \c addRdata() for \c TreeNodeRRset.
  165. ///
  166. /// It throws \c isc::Unexpected unconditionally.
  167. virtual void addRdata(const dns::rdata::Rdata& rdata);
  168. virtual dns::RdataIteratorPtr getRdataIterator() const;
  169. /// \brief Specialized version of \c getRRsig() for \c TreeNodeRRset.
  170. virtual dns::RRsetPtr getRRsig() const;
  171. virtual unsigned int getRRsigDataCount() const {
  172. return (dnssec_ok_ ? rrsig_count_ : 0);
  173. }
  174. ///
  175. /// \name Specialized version of RRsig related methods for
  176. /// \c TreeNodeRRset.
  177. ///
  178. /// These throw \c isc::Unexpected unconditionally.
  179. ////
  180. //@{
  181. virtual void addRRsig(const dns::rdata::ConstRdataPtr& rdata);
  182. virtual void addRRsig(const dns::rdata::RdataPtr& rdata);
  183. virtual void addRRsig(const dns::AbstractRRset& sigs);
  184. virtual void addRRsig(const dns::ConstRRsetPtr& sigs);
  185. virtual void addRRsig(const dns::RRsetPtr& sigs);
  186. virtual void removeRRsig();
  187. //@}
  188. /// \brief Specialized version of \c isSameKind() for \c TreeNodeRRset.
  189. ///
  190. /// As a kind of optimization, this implementation exploits the assumption
  191. /// of how \c TreeNodeRRset objects are created: They must be always
  192. /// created inside the in-memory data source finder implementation,
  193. /// and they are constructed with the \c realname parameter if and only
  194. /// if the corresponding query name is subject to wildcard substitution.
  195. ///
  196. /// So, if the given RRset is of \c TreeNodeRRset, and one and only one of
  197. /// of them has \c realname, they are considered to have different names.
  198. ///
  199. /// Also, this implementation does not compare RR classes explicitly;
  200. /// if two \c TreeNodeRRset objects belong to different RR classes,
  201. /// they should belong to different zone trees (according to the assumption
  202. /// of how the zone data are built), and therefore they cannot be at
  203. /// same zone node. So it's sufficient to compare the (address of the)
  204. /// node; if they are different they cannot be of the same kind.
  205. virtual bool isSameKind(const dns::AbstractRRset& abs_other) const;
  206. private:
  207. dns::RdataIteratorPtr getSigRdataIterator() const;
  208. // Common backend for getRdataIterator() and getSigRdataIterator()
  209. dns::RdataIteratorPtr getRdataIteratorInternal(bool is_rrsig,
  210. size_t count) const;
  211. // Return \c LabelSequence for the owner name regardless of how this
  212. /// class is constructed (with or without 'realname')
  213. dns::LabelSequence getOwnerLabels(
  214. uint8_t labels_buf[dns::LabelSequence::MAX_SERIALIZED_LENGTH]) const
  215. {
  216. if (realname_ != NULL) {
  217. return (dns::LabelSequence(*realname_));
  218. }
  219. return (node_->getAbsoluteLabels(labels_buf));
  220. }
  221. const ZoneNode* node_;
  222. const RdataSet* rdataset_;
  223. const size_t rrsig_count_;
  224. const dns::RRClass rrclass_;
  225. const bool dnssec_ok_;
  226. mutable dns::Name* name_;
  227. const dns::Name* const realname_;
  228. mutable dns::RRTTL* ttl_;
  229. };
  230. typedef boost::shared_ptr<TreeNodeRRset> TreeNodeRRsetPtr;
  231. } // namespace memory
  232. } // namespace datasrc
  233. } // namespace isc
  234. #endif // DATASRC_MEMORY_TREENODE_RRSET_H
  235. // Local Variables:
  236. // mode: c++
  237. // End: