rrclass-placeholder.h 13 KB

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