rrclass-placeholder.h 12 KB

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