question.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. // Copyright (C) 2010 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 __QUESTION_H
  15. #define __QUESTION_H 1
  16. #include <iostream>
  17. #include <string>
  18. #include <boost/shared_ptr.hpp>
  19. #include <dns/name.h>
  20. #include <dns/rrclass.h>
  21. #include <dns/rrtype.h>
  22. namespace isc {
  23. namespace dns {
  24. class InputBuffer;
  25. class MessageRenderer;
  26. class Question;
  27. /// \brief A pointer-like type pointing to an \c Question object.
  28. typedef boost::shared_ptr<Question> QuestionPtr;
  29. /// \brief A pointer-like type pointing to an (immutable) \c Question object.
  30. typedef boost::shared_ptr<const Question> ConstQuestionPtr;
  31. /// \brief The \c Question class encapsulates the common search key of DNS
  32. /// lookup, consisting of owner name, RR type and RR class.
  33. ///
  34. /// The primarily intended use case of this class is an entry of the question
  35. /// section of DNS messages.
  36. /// This could also be used as a general purpose lookup key, e.g., in a
  37. /// custom implementation of DNS database.
  38. ///
  39. /// In this initial implementation, the \c Question class is defined as
  40. /// a <em>concrete class</em>; it's not expected to be inherited by
  41. /// a user-defined customized class.
  42. /// It may be worth noting that it's different from the design of the
  43. /// RRset classes (\c AbstractRRset and its derived classes).
  44. /// The RRset classes form an inheritance hierarchy from the base abstract
  45. /// class.
  46. /// This may look odd in that an "RRset" and "Question" are similar from the
  47. /// protocol point of view: Both are used as a semantics unit of DNS messages;
  48. /// both share the same set of components, name, RR type and RR class.
  49. ///
  50. /// In fact, BIND9 didn't introduce a separate data structure for Questions,
  51. /// and use the same \c "rdataset" structure for both RRsets and Questions.
  52. /// We could take the same approach, but chose to adopt the different design.
  53. /// One reason for that is because a Question and an RRset are still
  54. /// different, and a Question might not be cleanly defined if (e.g.) it were
  55. /// a derived class of some "RRset-like" class.
  56. /// For example, we couldn't give a reasonable semantics for \c %getTTL() or
  57. /// \c %setTTL() methods for a Question, since it's not associated with the
  58. /// TTL.
  59. /// In fact, the BIND9 implementation ended up often separating the case where
  60. /// a \c "rdataset" is from the Question section of a DNS message and the
  61. /// case where it comes from other sections.
  62. /// If we cannot treat them completely transparently anyway, separating the
  63. /// class (type) would make more sense because we can exploit compilation
  64. /// time type checks.
  65. ///
  66. /// On the other hand, we do not expect a strong need for customizing the
  67. /// \c Question class, unlike the RRset.
  68. /// Handling the Question section of a DNS message is relatively a
  69. /// simple work comparing to RRset-involved operations, so a unified
  70. /// straightforward implementation should suffice for any use cases
  71. /// including performance sensitive ones.
  72. ///
  73. /// We may, however, still want to have customized version of Question
  74. /// for, e.g, highly optimized behavior, and may revisit this design choice
  75. /// as we have more experiences with this implementation.
  76. ///
  77. /// One disadvantage of defining RRsets and Questions as unrelated classes
  78. /// is that we cannot handle them in a polymorphic way.
  79. /// For example, we might want to iterate over DNS message sections and
  80. /// render the data in the wire format, whether it's an RRset or a Question.
  81. /// If a \c Question were a derived class of some common RRset-like class,
  82. /// we could do this by calling <code>rrset_or_question->%toWire()</code>.
  83. /// But the actual design doesn't allow this approach, which may require
  84. /// duplicate code for almost the same operation.
  85. /// To mitigate this problem, we intentionally used the same names
  86. /// with the same signature for some common methods of \c Question and
  87. /// \c AbstractRRset such as \c %getName() or \c %toWire().
  88. /// So the user class may use a template function that is applicable to both
  89. /// \c Question and \c RRset to avoid writing duplicate code logic.
  90. class Question {
  91. ///
  92. /// \name Constructors and Destructor
  93. ///
  94. /// We use the default versions of destructor, copy constructor,
  95. /// and assignment operator.
  96. ///
  97. /// The default constructor is hidden as a result of defining the other
  98. /// constructors. This is intentional; we don't want to allow a
  99. /// \c Question object to be constructed with an invalid state.
  100. //@{
  101. public:
  102. /// \brief Constructor from wire-format data.
  103. ///
  104. /// It simply constructs a set of \c Name, \c RRType, and \c RRClass
  105. /// object from the \c buffer in this order, and constructs a
  106. /// \c Question object in a straightforward way.
  107. ///
  108. /// It may throw an exception if the construction of these component
  109. /// classes fails.
  110. ///
  111. /// \param buffer A buffer storing the wire format data.
  112. Question(InputBuffer& buffer);
  113. /// \brief Constructor from fixed parameters of the \c Question.
  114. ///
  115. /// This constructor is basically expected to be exception free, but
  116. /// copying the name may involve resource allocation, and if it fails
  117. /// the corresponding standard exception will be thrown.
  118. ///
  119. /// \param name The owner name of the \c Question.
  120. /// \param rrclass The RR class of the \c Question.
  121. /// \param rrtype The RR type of the \c Question.
  122. Question(const Name& name, const RRClass& rrclass, const RRType& rrtype) :
  123. name_(name), rrtype_(rrtype), rrclass_(rrclass)
  124. {}
  125. //@}
  126. ///
  127. /// \name Getter Methods
  128. ///
  129. //@{
  130. /// \brief Returns the owner name of the \c Question.
  131. ///
  132. /// This method never throws an exception.
  133. ///
  134. /// \return A reference to a \c Name class object corresponding to the
  135. /// \c Question owner name.
  136. const Name& getName() const { return (name_); }
  137. /// \brief Returns the RR Class of the \c Question.
  138. ///
  139. /// This method never throws an exception.
  140. ///
  141. /// \return A reference to a \c RRClass class object corresponding to the
  142. /// RR class of the \c Question.
  143. const RRType& getType() const { return (rrtype_); }
  144. /// \brief Returns the RR Type of the \c Question.
  145. ///
  146. /// This method never throws an exception.
  147. ///
  148. /// \return A reference to a \c RRType class object corresponding to the
  149. /// RR type of the \c Question.
  150. const RRClass& getClass() const { return (rrclass_); }
  151. //@}
  152. ///
  153. /// \name Converter Methods
  154. ///
  155. //@{
  156. /// \brief Convert the Question to a string.
  157. ///
  158. /// Unlike other similar methods of this library, this method terminates
  159. /// the resulting string with a trailing newline character
  160. /// (following the BIND9 convention).
  161. ///
  162. /// This method simply calls the \c %toText() methods of the corresponding
  163. /// \c Name, \c RRType and \c RRClass classes for this \c Question, and
  164. /// these methods may throw an exception.
  165. /// In particular, if resource allocation fails, a corresponding standard
  166. /// exception will be thrown.
  167. ///
  168. /// \return A string representation of the \c Question.
  169. std::string toText() const;
  170. /// \brief Render the Question in the wire format with name compression.
  171. ///
  172. /// This method simply calls the \c %toWire() methods of the corresponding
  173. /// \c Name, \c RRType and \c RRClass classes for this \c Question, and
  174. /// these methods may throw an exception.
  175. /// In particular, if resource allocation fails, a corresponding standard
  176. /// exception will be thrown.
  177. ///
  178. /// This method returns 1, which is the number of "questions" contained
  179. /// in the \c Question.
  180. /// This is a meaningless value, but is provided to be consistent with
  181. /// the corresponding method of \c AbstractRRset (see the detailed
  182. /// class description).
  183. ///
  184. /// The owner name will be compressed if possible, although it's an
  185. /// unlikely event in practice because the %Question section a DNS
  186. /// message normally doesn't contain multiple question entries and
  187. /// it's located right after the Header section.
  188. /// Nevertheless, \c renderer records the information of the owner name
  189. /// so that it can be pointed by other RRs in other sections (which is
  190. /// more likely to happen).
  191. ///
  192. /// In theory, an attempt to render a Question may cause truncation
  193. /// (when the Question section contains a large number of entries),
  194. /// but this implementation doesn't catch that situation.
  195. /// It would make the code unnecessarily complicated (though perhaps
  196. /// slightly) for almost impossible case in practice.
  197. /// An upper layer will handle the pathological case as a general error.
  198. ///
  199. /// \param renderer DNS message rendering context that encapsulates the
  200. /// output buffer and name compression information.
  201. /// \return 1
  202. unsigned int toWire(MessageRenderer& renderer) const;
  203. /// \brief Render the Question in the wire format without name compression.
  204. ///
  205. /// This method behaves like the render version except it doesn't compress
  206. /// the owner name.
  207. /// See \c toWire(MessageRenderer& renderer)const.
  208. ///
  209. /// \param buffer An output buffer to store the wire data.
  210. /// \return 1
  211. unsigned int toWire(OutputBuffer& buffer) const;
  212. //@}
  213. ///
  214. /// \name Comparison Operators
  215. ///
  216. //@{
  217. /// A "less than" operator is needed for this class so it can
  218. /// function as an index to std::map.
  219. bool operator <(const Question& rhs) const {
  220. return (rrclass_ < rhs.rrclass_ ||
  221. (rrclass_ == rhs.rrclass_ &&
  222. (rrtype_ < rhs.rrtype_ ||
  223. (rrtype_ == rhs.rrtype_ && (name_ < rhs.name_)))));
  224. }
  225. /// Equality operator. Primarily used to compare the question section in
  226. /// a response to that in the query.
  227. ///
  228. /// \param rhs Question to compare against
  229. /// \return true if name, class and type are equal, false otherwise
  230. bool operator==(const Question& rhs) const {
  231. return ((rrclass_ == rhs.rrclass_) && (rrtype_ == rhs.rrtype_) &&
  232. (name_ == rhs.name_));
  233. }
  234. /// Inequality operator. Primarily used to compare the question section in
  235. /// a response to that in the query.
  236. ///
  237. /// \param rhs Question to compare against
  238. /// \return true if one or more of the name, class and type do not match,
  239. /// false otherwise.
  240. bool operator!=(const Question& rhs) const {
  241. return (!operator==(rhs));
  242. }
  243. //@}
  244. private:
  245. Name name_;
  246. RRType rrtype_;
  247. RRClass rrclass_;
  248. };
  249. /// \brief Insert the \c Question as a string into stream.
  250. ///
  251. /// This method convert the \c question into a string and inserts it into the
  252. /// output stream \c os.
  253. ///
  254. /// This function overloads the global \c operator<< to behave as described in
  255. /// \c %ostream::%operator<< but applied to Question objects.
  256. ///
  257. /// \param os A \c std::ostream object on which the insertion operation is
  258. /// performed.
  259. /// \param question A reference to a \c Question object output by the
  260. /// operation.
  261. /// \return A reference to the same \c std::ostream object referenced by
  262. /// parameter \c os after the insertion operation.
  263. std::ostream& operator<<(std::ostream& os, const Question& question);
  264. } // end of namespace dns
  265. } // end of namespace isc
  266. #endif // __QUESTION_H
  267. // Local Variables:
  268. // mode: c++
  269. // End: