rrclass-placeholder.h 11 KB

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