question.h 11 KB

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