question.h 12 KB

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