rrclass-placeholder.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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: rrclass.h 530 2010-01-26 22:15:42Z jinmei $
  15. #ifndef __RRCLASS_H
  16. #define __RRCLASS_H 1
  17. #include <stdint.h>
  18. #include <string>
  19. #include <ostream>
  20. #include <exceptions/exceptions.h>
  21. namespace isc {
  22. namespace dns {
  23. // forward declarations
  24. class InputBuffer;
  25. class OutputBuffer;
  26. class MessageRenderer;
  27. ///
  28. /// \brief A standard DNS module exception that is thrown if an RRClass object
  29. /// is being constructed from an unrecognized string.
  30. ///
  31. class InvalidRRClass : public Exception {
  32. public:
  33. InvalidRRClass(const char* file, size_t line, const char* what) :
  34. isc::Exception(file, line, what) {}
  35. };
  36. ///
  37. /// \brief A standard DNS module exception that is thrown if an RRClass object
  38. /// is being constructed from a incomplete (too short) wire-format data.
  39. ///
  40. class IncompleteRRClass : public Exception {
  41. public:
  42. IncompleteRRClass(const char* file, size_t line, const char* what) :
  43. isc::Exception(file, line, what) {}
  44. };
  45. ///
  46. /// The \c RRClass class encapsulates DNS resource record classes.
  47. ///
  48. /// This class manages the 16-bit integer class codes in quite a straightforward
  49. /// way. The only non trivial task is to handle textual representations of
  50. /// RR classes, such as "IN", "CH", or "CLASS65534".
  51. ///
  52. /// This class consults a helper \c RRParamRegistry class, which is a registry
  53. /// of RR related parameters and has the singleton object. This registry
  54. /// provides a mapping between RR class codes and their "well-known" textual
  55. /// representations.
  56. /// Parameters of RR classes defined by DNS protocol standards are automatically
  57. /// registered at initialization time and are ensured to be always available for
  58. /// applications unless the application explicitly modifies the registry.
  59. ///
  60. /// For convenience, this class defines constant class objects corresponding to
  61. /// standard RR classes. These are generally referred to as the form of
  62. /// <code>RRClass::{class-text}()</code>.
  63. /// For example, \c RRClass::IN() is an \c RRClass object corresponding to the
  64. /// IN class (class code 1).
  65. /// Note that these constants are used through a "proxy" function.
  66. /// This is because they may be used to initialize another non-local (e.g.
  67. /// global or namespace-scope) static object as follows:
  68. ///
  69. /// \code
  70. /// namespace foo {
  71. /// const RRClass default_class = RRClass::IN();
  72. /// } \endcode
  73. ///
  74. /// In order to ensure that the constant RRClass object has been initialized
  75. /// before the initialization for \c default_class, we need help from
  76. /// the proxy function.
  77. ///
  78. /// Note to developers: same note as \c RRType applies.
  79. class RRClass {
  80. public:
  81. ///
  82. /// \name Constructors and Destructor
  83. ///
  84. //@{
  85. /// Constructor from an integer class code.
  86. ///
  87. /// This constructor never throws an exception.
  88. ///
  89. /// \param classcode An 16-bit integer code corresponding to the RRClass.
  90. explicit RRClass(uint16_t classcode) : classcode_(classcode) {}
  91. ///
  92. /// A valid string is one of "well-known" textual class representations
  93. /// such as "IN" or "CH", or in the standard format for "unknown"
  94. /// classes as defined in RFC3597, i.e., "CLASSnnnn".
  95. ///
  96. /// More precisely, the "well-known" representations are the ones stored
  97. /// in the \c RRParamRegistry registry (see the class description).
  98. ///
  99. /// As for the format of "CLASSnnnn", "nnnn" must represent a valid 16-bit
  100. /// unsigned integer, which may contain leading 0's as long as it consists
  101. /// of at most 5 characters (inclusive).
  102. /// For example, "CLASS1" and "CLASSS001" are valid and represent the same
  103. /// class, but "CLASS65536" and "CLASS000001" are invalid.
  104. /// A "CLASSnnnn" representation is valid even if the corresponding class
  105. /// code is registered in the \c RRParamRegistry object. For example, both
  106. /// "IN" and "CLASS1" are valid and represent the same class.
  107. ///
  108. /// All of these representations are case insensitive; "IN" and "in", and
  109. /// "CLASS1" and "class1" are all valid and represent the same classes,
  110. /// respectively.
  111. ///
  112. /// If the given string is not recognized as a valid representation of
  113. /// an RR class, an exception of class \c InvalidRRClass will be thrown.
  114. ///
  115. /// \param classstr A string representation of the \c RRClass
  116. explicit RRClass(const std::string& classstr);
  117. /// Constructor from wire-format data.
  118. ///
  119. /// The \c buffer parameter normally stores a complete DNS message
  120. /// containing the RRClass to be constructed. The current read position of
  121. /// the buffer points to the head of the class.
  122. ///
  123. /// If the given data does not large enough to contain a 16-bit integer,
  124. /// an exception of class \c IncompleteRRClass will be thrown.
  125. ///
  126. /// \param buffer A buffer storing the wire format data.
  127. explicit RRClass(InputBuffer& buffer);
  128. ///
  129. /// We use the default copy constructor intentionally.
  130. //@}
  131. /// We use the default copy assignment operator intentionally.
  132. ///
  133. ///
  134. /// \name Converter methods
  135. ///
  136. //@{
  137. /// \brief Convert the \c RRClass to a string.
  138. ///
  139. /// If a "well known" textual representation for the class code is
  140. /// registered in the RR parameter registry (see the class description),
  141. /// that will be used as the return value of this method. Otherwise, this
  142. /// method creates a new string for an "unknown" class in the format defined
  143. /// in RFC3597, i.e., "CLASSnnnn", and returns it.
  144. ///
  145. /// If resource allocation for the string fails, a corresponding standard
  146. /// exception will be thrown.
  147. ///
  148. /// \return A string representation of the \c RRClass.
  149. const std::string toText() const;
  150. /// \brief Render the \c RRClass in the wire format.
  151. ///
  152. /// This method renders the class code in network byte order via
  153. /// \c renderer, which encapsulates output buffer and other rendering
  154. /// contexts.
  155. ///
  156. /// If resource allocation in rendering process fails, a corresponding
  157. /// standard exception will be thrown.
  158. ///
  159. /// \param buffer An output buffer to store the wire data.
  160. void toWire(MessageRenderer& renderer) const;
  161. /// \brief Render the \c RRClass in the wire format.
  162. ///
  163. /// This method renders the class code in network byte order into the
  164. /// \c buffer.
  165. ///
  166. /// If resource allocation in rendering process fails, a corresponding
  167. /// standard exception will be thrown.
  168. ///
  169. /// \param renderer DNS message rendering context that encapsulates the
  170. /// output buffer in which the RRClass is to be stored.
  171. void toWire(OutputBuffer& buffer) const;
  172. //@}
  173. ///
  174. /// \name Getter Methods
  175. ///
  176. //@{
  177. /// \brief Returns the RR class code as a 16-bit unsigned integer.
  178. ///
  179. /// This method never throws an exception.
  180. ///
  181. /// \return An 16-bit integer code corresponding to the RRClass.
  182. uint16_t getCode() const { return (classcode_); }
  183. //@}
  184. ///
  185. /// \name Comparison methods
  186. ///
  187. //@{
  188. /// \brief Return true iff two RRClasses are equal.
  189. ///
  190. /// Two RRClasses are equal iff their class codes are equal.
  191. ///
  192. /// This method never throws an exception.
  193. ///
  194. /// \param other the \c RRClass object to compare against.
  195. /// \return true if the two RRClasses are equal; otherwise false.
  196. bool equals(const RRClass& other) const
  197. { return (classcode_ == other.classcode_); }
  198. /// \brief Same as \c equals().
  199. bool operator==(const RRClass& other) const { return (equals(other)); }
  200. /// \brief Return true iff two RRClasses are equal.
  201. ///
  202. /// This method never throws an exception.
  203. ///
  204. /// \param other the \c RRClass object to compare against.
  205. /// \return true if the two RRClasses are not equal; otherwise false.
  206. bool nequals(const RRClass& other) const
  207. { return (classcode_ != other.classcode_); }
  208. /// \brief Same as \c nequals().
  209. bool operator!=(const RRClass& other) const { return (nequals(other)); }
  210. /// \brief Less-than comparison for RRClass against \c other
  211. ///
  212. /// We define the less-than relationship based on their class codes;
  213. /// one RRClass is less than the other iff the code of the former is less
  214. /// than that of the other as unsigned integers.
  215. /// The relationship is meaningless in terms of DNS protocol; the only
  216. /// reason we define this method is that RRClass objects can be stored in
  217. /// STL containers without requiring user-defined less-than relationship.
  218. /// We therefore don't define other comparison operators.
  219. ///
  220. /// This method never throws an exception.
  221. ///
  222. /// \param other the \c RRClass object to compare against.
  223. /// \return true if \c this RRClass is less than the \c other; otherwise
  224. /// false.
  225. bool operator<(const RRClass& other) const
  226. { return (classcode_ < other.classcode_); }
  227. // BEGIN_WELL_KNOWN_CLASS_DECLARATIONS
  228. // END_WELL_KNOWN_CLASS_DECLARATIONS
  229. static const RRClass& NONE();
  230. static const RRClass& ANY();
  231. private:
  232. // \brief Meta-classes
  233. enum {
  234. RRCLASS_RESERVED0 = 0,
  235. RRCLASS_NONE = 254,
  236. RRCLASS_ANY = 255
  237. };
  238. uint16_t classcode_;
  239. };
  240. // BEGIN_WELL_KNOWN_CLASS_DEFINITIONS
  241. // END_WELL_KNOWN_CLASS_DEFINITIONS
  242. inline const RRClass&
  243. RRClass::NONE()
  244. {
  245. static RRClass rrclass(RRCLASS_NONE);
  246. return (rrclass);
  247. }
  248. inline const RRClass&
  249. RRClass::ANY()
  250. {
  251. static RRClass rrclass(RRCLASS_ANY);
  252. return (rrclass);
  253. }
  254. ///
  255. /// \brief Insert the \c RRClass as a string into stream.
  256. ///
  257. /// This method convert the \c rrclass into a string and inserts it into the
  258. /// output stream \c os.
  259. ///
  260. /// This function overloads the global operator<< to behave as described in
  261. /// ostream::operator<< but applied to \c RRClass objects.
  262. ///
  263. /// \param os A \c std::ostream object on which the insertion operation is
  264. /// performed.
  265. /// \param rrclass The \c RRClass object output by the operation.
  266. /// \return A reference to the same \c std::ostream object referenced by
  267. /// parameter \c os after the insertion operation.
  268. std::ostream&
  269. operator<<(std::ostream& os, const RRClass& rrclass);
  270. }
  271. }
  272. #endif // __RRCLASS_H
  273. // Local Variables:
  274. // mode: c++
  275. // End: