rrclass-placeholder.h 11 KB

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