rcode.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /*
  2. * Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  9. * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  10. * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  11. * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  12. * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  13. * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  14. * PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include <stdint.h>
  17. #include <ostream>
  18. #ifndef RCODE_H
  19. #define RCODE_H 1
  20. namespace isc {
  21. namespace dns {
  22. /// \brief DNS Response Codes (RCODEs) class.
  23. ///
  24. /// The \c Rcode class objects represent standard Response Codes
  25. /// (RCODEs) of the header section of DNS messages, and extended response
  26. /// codes as defined in the EDNS specification.
  27. ///
  28. /// Originally RCODEs were defined as 4-bit integers (RFC1035), and then
  29. /// extended to 12 bits as part of the %EDNS specification (RFC2671).
  30. /// This API uses the 12-bit version of the definition from the beginning;
  31. /// applications don't have to aware of the original definition except when
  32. /// dealing with the wire-format representation of the %EDNS OPT RR
  33. /// (which is rare).
  34. ///
  35. /// Like the \c Opcode class, Rcodes could be represented as bare integers,
  36. /// but we define a separate class to benefit from C++ type safety.
  37. ///
  38. /// For convenience we also provide
  39. /// an enum type for pre-defined RCODE values, but it is generally advisable
  40. /// to handle RCODEs through this class. In fact, public interfaces of
  41. /// this library uses this class to pass or return RCODEs instead of the
  42. /// bare code values.
  43. class Rcode {
  44. public:
  45. /// Constants for pre-defined RCODE values.
  46. enum CodeValue {
  47. NOERROR_CODE = 0, ///< 0: No error (RFC1035)
  48. FORMERR_CODE = 1, ///< 1: Format error (RFC1035)
  49. SERVFAIL_CODE = 2, ///< 2: Server failure (RFC1035)
  50. NXDOMAIN_CODE = 3, ///< 3: Name Error (RFC1035)
  51. NOTIMP_CODE = 4, ///< 4: Not Implemented (RFC1035)
  52. REFUSED_CODE = 5, ///< 5: Refused (RFC1035)
  53. YXDOMAIN_CODE = 6, ///< 6: Name unexpectedly exists (RFC2136)
  54. YXRRSET_CODE = 7, ///< 7: RRset unexpectedly exists (RFC2136)
  55. NXRRSET_CODE = 8, ///< 8: RRset should exist but not (RFC2136)
  56. NOTAUTH_CODE = 9, ///< 9: Server isn't authoritative (RFC2136)
  57. NOTZONE_CODE = 10, ///< 10: Name is not within the zone (RFC2136)
  58. RESERVED11_CODE = 11, ///< 11: Reserved for future use (RFC1035)
  59. RESERVED12_CODE = 12, ///< 12: Reserved for future use (RFC1035)
  60. RESERVED13_CODE = 13, ///< 13: Reserved for future use (RFC1035)
  61. RESERVED14_CODE = 14, ///< 14: Reserved for future use (RFC1035)
  62. RESERVED15_CODE = 15, ///< 15: Reserved for future use (RFC1035)
  63. BADVERS_CODE = 16 ///< 16: EDNS version not implemented (RFC2671)
  64. };
  65. /// \name Constructors and Destructor
  66. ///
  67. /// We use the default versions of destructor, copy constructor,
  68. /// and assignment operator.
  69. ///
  70. /// The default constructor is hidden as a result of defining the other
  71. /// constructors. This is intentional; we don't want to allow an
  72. /// \c Rcode object to be constructed with an invalid state.
  73. //@{
  74. /// \brief Constructor from the code value.
  75. ///
  76. /// Since RCODEs are 12-bit values, parameters larger than 0xfff are
  77. /// invalid.
  78. /// If \c code is larger than 0xfff an exception of class
  79. /// \c isc::OutOfRange will be thrown.
  80. ///
  81. /// \param code The underlying 12-bit code value of the \c Rcode.
  82. explicit Rcode(const uint16_t code);
  83. /// \brief Constructor from a pair of base and extended parts of code.
  84. ///
  85. /// This constructor takes two parameters, one for the lower 4 bits of
  86. /// the code value, the other for the upper 8 bits, and combines them
  87. /// to build a complete 12-bit code value.
  88. ///
  89. /// The first parameter, \c code, is the lower 4 bits, and therefore must
  90. /// not exceed 15. Otherwise, an exception of class
  91. /// \c isc::OutOfRange will be thrown.
  92. ///
  93. /// This version of constructor is provided specifically for constructing
  94. /// an Rcode from a DNS header and an %EDNS OPT RR. Normal applications
  95. /// won't have to use this constructor.
  96. ///
  97. /// \param code The lower 4 bits of the underlying code value.
  98. /// \param extended_code The upper 8 bits of the underlying code value.
  99. Rcode(const uint8_t code, const uint8_t extended_code);
  100. //@}
  101. /// \brief Returns the \c Rcode code value.
  102. ///
  103. /// This method never throws an exception.
  104. ///
  105. /// \return The underlying code value corresponding to the \c Rcode.
  106. uint16_t getCode() const { return (code_); }
  107. /// \brief Returns the upper 8-bit of the \c Rcode code value.
  108. ///
  109. /// Normal applications won't have to use this method. This is provided
  110. /// in case the upper 8 bits are necessary for the EDNS protocol
  111. /// processing.
  112. ///
  113. /// This method never throws an exception.
  114. ///
  115. /// \return The upper 8-bit of the underlying code value.
  116. uint8_t getExtendedCode() const;
  117. /// \brief Return true iff two Rcodes are equal.
  118. ///
  119. /// Two Rcodes are equal iff their type codes are equal.
  120. ///
  121. /// This method never throws an exception.
  122. ///
  123. /// \param other the \c Rcode object to compare against.
  124. /// \return true if the two Rcodes are equal; otherwise false.
  125. bool equals(const Rcode& other) const
  126. { return (code_ == other.code_); }
  127. /// \brief Same as \c equals().
  128. bool operator==(const Rcode& other) const { return (equals(other)); }
  129. /// \brief Return true iff two Rcodes are not equal.
  130. ///
  131. /// This method never throws an exception.
  132. ///
  133. /// \param other the \c Rcode object to compare against.
  134. /// \return true if the two Rcodes are not equal; otherwise false.
  135. bool nequals(const Rcode& other) const
  136. { return (code_ != other.code_); }
  137. /// \brief Same as \c nequals().
  138. bool operator!=(const Rcode& other) const { return (nequals(other)); }
  139. /// \brief Convert the \c Rcode to a string.
  140. ///
  141. /// For pre-defined code values (see Rcode::CodeValue),
  142. /// this method returns a string representation of the "mnemonic' used
  143. /// for the enum and constant objects. For example, the string for
  144. /// code value 0 is "NOERROR", etc.
  145. /// For other code values it returns a string representation of the decimal
  146. /// number of the value, e.g. "32", "100", etc.
  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 Rcode.
  152. std::string toText() const;
  153. /// A constant object for the NOERROR Rcode (see \c Rcode::NOERROR_CODE).
  154. static const Rcode& NOERROR();
  155. /// A constant object for the FORMERR Rcode (see \c Rcode::FORMERR_CODE).
  156. static const Rcode& FORMERR();
  157. /// A constant object for the SERVFAIL Rcode (see \c Rcode::SERVFAIL_CODE).
  158. static const Rcode& SERVFAIL();
  159. /// A constant object for the NXDOMAIN Rcode (see \c Rcode::NXDOMAIN_CODE).
  160. static const Rcode& NXDOMAIN();
  161. /// A constant object for the NOTIMP Rcode (see \c Rcode::NOTIMP_CODE).
  162. static const Rcode& NOTIMP();
  163. /// A constant object for the REFUSED Rcode (see \c Rcode::REFUSED_CODE).
  164. static const Rcode& REFUSED();
  165. /// A constant object for the YXDOMAIN Rcode (see \c Rcode::YXDOMAIN_CODE).
  166. static const Rcode& YXDOMAIN();
  167. /// A constant object for the YXRRSET Rcode (see \c Rcode::YXRRSET_CODE).
  168. static const Rcode& YXRRSET();
  169. /// A constant object for the NXRRSET Rcode (see \c Rcode::NXRRSET_CODE).
  170. static const Rcode& NXRRSET();
  171. /// A constant object for the NOTAUTH Rcode (see \c Rcode::NOTAUTH_CODE).
  172. static const Rcode& NOTAUTH();
  173. /// A constant object for the NOTZONE Rcode (see \c Rcode::NOTZONE_CODE).
  174. static const Rcode& NOTZONE();
  175. /// A constant object for a reserved (code 11) Rcode.
  176. /// (see \c Rcode::RESERVED11_CODE).
  177. static const Rcode& RESERVED11();
  178. /// A constant object for a reserved (code 12) Rcode.
  179. /// (see \c Rcode::RESERVED12_CODE).
  180. static const Rcode& RESERVED12();
  181. /// A constant object for a reserved (code 13) Rcode.
  182. /// (see \c Rcode::RESERVED13_CODE).
  183. static const Rcode& RESERVED13();
  184. /// A constant object for a reserved (code 14) Rcode.
  185. /// (see \c Rcode::RESERVED14_CODE).
  186. static const Rcode& RESERVED14();
  187. /// A constant object for a reserved (code 15) Rcode.
  188. /// (see \c Rcode::RESERVED15_CODE).
  189. static const Rcode& RESERVED15();
  190. /// A constant object for the BADVERS Rcode (see \c Rcode::BADVERS_CODE).
  191. static const Rcode& BADVERS();
  192. private:
  193. uint16_t code_;
  194. };
  195. inline const Rcode&
  196. Rcode::NOERROR() {
  197. static Rcode c(0);
  198. return (c);
  199. }
  200. inline const Rcode&
  201. Rcode::FORMERR() {
  202. static Rcode c(1);
  203. return (c);
  204. }
  205. inline const Rcode&
  206. Rcode::SERVFAIL() {
  207. static Rcode c(2);
  208. return (c);
  209. }
  210. inline const Rcode&
  211. Rcode::NXDOMAIN() {
  212. static Rcode c(3);
  213. return (c);
  214. }
  215. inline const Rcode&
  216. Rcode::NOTIMP() {
  217. static Rcode c(4);
  218. return (c);
  219. }
  220. inline const Rcode&
  221. Rcode::REFUSED() {
  222. static Rcode c(5);
  223. return (c);
  224. }
  225. inline const Rcode&
  226. Rcode::YXDOMAIN() {
  227. static Rcode c(6);
  228. return (c);
  229. }
  230. inline const Rcode&
  231. Rcode::YXRRSET() {
  232. static Rcode c(7);
  233. return (c);
  234. }
  235. inline const Rcode&
  236. Rcode::NXRRSET() {
  237. static Rcode c(8);
  238. return (c);
  239. }
  240. inline const Rcode&
  241. Rcode::NOTAUTH() {
  242. static Rcode c(9);
  243. return (c);
  244. }
  245. inline const Rcode&
  246. Rcode::NOTZONE() {
  247. static Rcode c(10);
  248. return (c);
  249. }
  250. inline const Rcode&
  251. Rcode::RESERVED11() {
  252. static Rcode c(11);
  253. return (c);
  254. }
  255. inline const Rcode&
  256. Rcode::RESERVED12() {
  257. static Rcode c(12);
  258. return (c);
  259. }
  260. inline const Rcode&
  261. Rcode::RESERVED13() {
  262. static Rcode c(13);
  263. return (c);
  264. }
  265. inline const Rcode&
  266. Rcode::RESERVED14() {
  267. static Rcode c(14);
  268. return (c);
  269. }
  270. inline const Rcode&
  271. Rcode::RESERVED15() {
  272. static Rcode c(15);
  273. return (c);
  274. }
  275. inline const Rcode&
  276. Rcode::BADVERS() {
  277. static Rcode c(16);
  278. return (c);
  279. }
  280. /// \brief Insert the \c Rcode as a string into stream.
  281. ///
  282. /// This method convert \c rcode into a string and inserts it into the
  283. /// output stream \c os.
  284. ///
  285. /// \param os A \c std::ostream object on which the insertion operation is
  286. /// performed.
  287. /// \param rcode A reference to an \c Rcode object output by the operation.
  288. /// \return A reference to the same \c std::ostream object referenced by
  289. /// parameter \c os after the insertion operation.
  290. std::ostream& operator<<(std::ostream& os, const Rcode& rcode);
  291. }
  292. }
  293. #endif // RCODE_H
  294. // Local Variables:
  295. // mode: c++
  296. // End: