rrclass-placeholder.h 12 KB

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