tsigerror.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. // Copyright (C) 2011-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 TSIGERROR_H
  7. #define TSIGERROR_H 1
  8. #include <ostream>
  9. #include <string>
  10. #include <dns/rcode.h>
  11. namespace isc {
  12. namespace dns {
  13. /// TSIG errors
  14. ///
  15. /// The \c TSIGError class objects represent standard errors related to
  16. /// TSIG protocol operations as defined in related specifications, mainly
  17. /// in RFC2845, RFC2930 and RFC4635.
  18. class TSIGError {
  19. public:
  20. /// Constants for pre-defined TSIG error values.
  21. ///
  22. /// Code values from 0 through 15 (inclusive) are derived from those of
  23. /// RCODE and are not defined here. See the \c Rcode class.
  24. ///
  25. /// \note Unfortunately some systems define "BADSIG" as a macro in a public
  26. /// header file. To avoid conflict with it we add an underscore to our
  27. /// definitions.
  28. enum CodeValue {
  29. BAD_SIG_CODE = 16, ///< 16: TSIG verification failure
  30. BAD_KEY_CODE = 17, ///< 17: TSIG key is not recognized
  31. BAD_TIME_CODE = 18, ///< 18: Current time and time signed are too different
  32. BAD_MODE_CODE = 19, ///< 19: Bad TKEY mode
  33. BAD_NAME_CODE = 20, ///< 20: Duplicate TKEY name
  34. BAD_ALG_CODE = 21, ///< 21: TKEY algorithm not supported
  35. BAD_TRUNC_CODE = 22 ///< 22: Bad truncation
  36. };
  37. /// \name Constructors
  38. ///
  39. /// We use the default versions of destructor, copy constructor,
  40. /// and assignment operator.
  41. //@{
  42. /// Constructor from the code value.
  43. ///
  44. /// \exception None
  45. ///
  46. /// \param error_code The underlying 16-bit error code value of the \c TSIGError.
  47. explicit TSIGError(uint16_t error_code) : code_(error_code) {}
  48. /// Constructor from \c Rcode.
  49. ///
  50. /// As defined in RFC2845, error code values from 0 to 15 (inclusive) are
  51. /// derived from the DNS RCODEs, which are represented via the \c Rcode
  52. /// class in this library. This constructor works as a converter from
  53. /// these RCODEs to corresponding TSIGError objects.
  54. ///
  55. /// \exception isc::OutOfRange Given rcode is not convertible to
  56. /// TSIGErrors.
  57. ///
  58. /// \param rcode the \c Rcode from which the TSIGError should be derived.
  59. explicit TSIGError(Rcode rcode);
  60. //@}
  61. /// \brief Returns the \c TSIGCode error code value.
  62. ///
  63. /// \exception None
  64. ///
  65. /// \return The underlying code value corresponding to the \c TSIGError.
  66. uint16_t getCode() const { return (code_); }
  67. /// \brief Return true iff two \c TSIGError objects are equal.
  68. ///
  69. /// Two TSIGError objects are equal iff their error codes are equal.
  70. ///
  71. /// \exception None
  72. ///
  73. /// \param other the \c TSIGError object to compare against.
  74. /// \return true if the two TSIGError are equal; otherwise false.
  75. bool equals(const TSIGError& other) const
  76. { return (code_ == other.code_); }
  77. /// \brief Same as \c equals().
  78. bool operator==(const TSIGError& other) const { return (equals(other)); }
  79. /// \brief Return true iff two \c TSIGError objects are not equal.
  80. ///
  81. /// \exception None
  82. ///
  83. /// \param other the \c TSIGError object to compare against.
  84. /// \return true if the two TSIGError objects are not equal;
  85. /// otherwise false.
  86. bool nequals(const TSIGError& other) const
  87. { return (code_ != other.code_); }
  88. /// \brief Same as \c nequals().
  89. bool operator!=(const TSIGError& other) const { return (nequals(other)); }
  90. /// \brief Convert the \c TSIGError to a string.
  91. ///
  92. /// For codes derived from RCODEs up to 15, this method returns the
  93. /// same string as \c Rcode::toText() for the corresponding code.
  94. /// For other pre-defined code values (see TSIGError::CodeValue),
  95. /// this method returns a string representation of the "mnemonic' used
  96. /// for the enum and constant objects as defined in RFC2845.
  97. /// For example, the string for code value 16 is "BADSIG", etc.
  98. /// For other code values it returns a string representation of the decimal
  99. /// number of the value, e.g. "32", "100", etc.
  100. ///
  101. /// \exception std::bad_alloc Resource allocation for the string fails
  102. ///
  103. /// \return A string representation of the \c TSIGError.
  104. std::string toText() const;
  105. /// \brief Convert the \c TSIGError to a \c Rcode
  106. ///
  107. /// This method returns an \c Rcode object that is corresponding to
  108. /// the TSIG error. The returned \c Rcode is expected to be used
  109. /// by a verifying server to specify the RCODE of a response when
  110. /// TSIG verification fails.
  111. ///
  112. /// Specifically, this method returns \c Rcode::NOTAUTH() for the
  113. /// TSIG specific errors, BADSIG, BADKEY, BADTIME, as described in
  114. /// RFC2845. For errors derived from the standard Rcode (code 0-15),
  115. /// it returns the corresponding \c Rcode. For others, this method
  116. /// returns \c Rcode::SERVFAIL() as a last resort.
  117. ///
  118. /// \exception None
  119. Rcode toRcode() const;
  120. /// A constant TSIG error object derived from \c Rcode::NOERROR()
  121. static const TSIGError& NOERROR();
  122. /// A constant TSIG error object derived from \c Rcode::FORMERR()
  123. static const TSIGError& FORMERR();
  124. /// A constant TSIG error object derived from \c Rcode::SERVFAIL()
  125. static const TSIGError& SERVFAIL();
  126. /// A constant TSIG error object derived from \c Rcode::NXDOMAIN()
  127. static const TSIGError& NXDOMAIN();
  128. /// A constant TSIG error object derived from \c Rcode::NOTIMP()
  129. static const TSIGError& NOTIMP();
  130. /// A constant TSIG error object derived from \c Rcode::REFUSED()
  131. static const TSIGError& REFUSED();
  132. /// A constant TSIG error object derived from \c Rcode::YXDOMAIN()
  133. static const TSIGError& YXDOMAIN();
  134. /// A constant TSIG error object derived from \c Rcode::YXRRSET()
  135. static const TSIGError& YXRRSET();
  136. /// A constant TSIG error object derived from \c Rcode::NXRRSET()
  137. static const TSIGError& NXRRSET();
  138. /// A constant TSIG error object derived from \c Rcode::NOTAUTH()
  139. static const TSIGError& NOTAUTH();
  140. /// A constant TSIG error object derived from \c Rcode::NOTZONE()
  141. static const TSIGError& NOTZONE();
  142. /// A constant TSIG error object derived from \c Rcode::RESERVED11()
  143. static const TSIGError& RESERVED11();
  144. /// A constant TSIG error object derived from \c Rcode::RESERVED12()
  145. static const TSIGError& RESERVED12();
  146. /// A constant TSIG error object derived from \c Rcode::RESERVED13()
  147. static const TSIGError& RESERVED13();
  148. /// A constant TSIG error object derived from \c Rcode::RESERVED14()
  149. static const TSIGError& RESERVED14();
  150. /// A constant TSIG error object derived from \c Rcode::RESERVED15()
  151. static const TSIGError& RESERVED15();
  152. /// A constant TSIG error object for the BADSIG code
  153. /// (see \c TSIGError::BAD_SIG_CODE).
  154. static const TSIGError& BAD_SIG();
  155. /// A constant TSIG error object for the BADKEY code
  156. /// (see \c TSIGError::BAD_KEY_CODE).
  157. static const TSIGError& BAD_KEY();
  158. /// A constant TSIG error object for the BADTIME code
  159. /// (see \c TSIGError::BAD_TIME_CODE).
  160. static const TSIGError& BAD_TIME();
  161. /// A constant TSIG error object for the BADMODE code
  162. /// (see \c TSIGError::BAD_MODE_CODE).
  163. static const TSIGError& BAD_MODE();
  164. /// A constant TSIG error object for the BADNAME code
  165. /// (see \c TSIGError::BAD_NAME_CODE).
  166. static const TSIGError& BAD_NAME();
  167. /// A constant TSIG error object for the BADALG code
  168. /// (see \c TSIGError::BAD_ALG_CODE).
  169. static const TSIGError& BAD_ALG();
  170. /// A constant TSIG error object for the BADTRUNC code
  171. /// (see \c TSIGError::BAD_TRUNC_CODE).
  172. static const TSIGError& BAD_TRUNC();
  173. private:
  174. // This is internally used to specify the maximum possible RCODE value
  175. // that can be convertible to TSIGErrors.
  176. static const int MAX_RCODE_FOR_TSIGERROR = 15;
  177. uint16_t code_;
  178. };
  179. inline const TSIGError&
  180. TSIGError::NOERROR() {
  181. static TSIGError e(Rcode::NOERROR());
  182. return (e);
  183. }
  184. inline const TSIGError&
  185. TSIGError::FORMERR() {
  186. static TSIGError e(Rcode::FORMERR());
  187. return (e);
  188. }
  189. inline const TSIGError&
  190. TSIGError::SERVFAIL() {
  191. static TSIGError e(Rcode::SERVFAIL());
  192. return (e);
  193. }
  194. inline const TSIGError&
  195. TSIGError::NXDOMAIN() {
  196. static TSIGError e(Rcode::NXDOMAIN());
  197. return (e);
  198. }
  199. inline const TSIGError&
  200. TSIGError::NOTIMP() {
  201. static TSIGError e(Rcode::NOTIMP());
  202. return (e);
  203. }
  204. inline const TSIGError&
  205. TSIGError::REFUSED() {
  206. static TSIGError e(Rcode::REFUSED());
  207. return (e);
  208. }
  209. inline const TSIGError&
  210. TSIGError::YXDOMAIN() {
  211. static TSIGError e(Rcode::YXDOMAIN());
  212. return (e);
  213. }
  214. inline const TSIGError&
  215. TSIGError::YXRRSET() {
  216. static TSIGError e(Rcode::YXRRSET());
  217. return (e);
  218. }
  219. inline const TSIGError&
  220. TSIGError::NXRRSET() {
  221. static TSIGError e(Rcode::NXRRSET());
  222. return (e);
  223. }
  224. inline const TSIGError&
  225. TSIGError::NOTAUTH() {
  226. static TSIGError e(Rcode::NOTAUTH());
  227. return (e);
  228. }
  229. inline const TSIGError&
  230. TSIGError::NOTZONE() {
  231. static TSIGError e(Rcode::NOTZONE());
  232. return (e);
  233. }
  234. inline const TSIGError&
  235. TSIGError::RESERVED11() {
  236. static TSIGError e(Rcode::RESERVED11());
  237. return (e);
  238. }
  239. inline const TSIGError&
  240. TSIGError::RESERVED12() {
  241. static TSIGError e(Rcode::RESERVED12());
  242. return (e);
  243. }
  244. inline const TSIGError&
  245. TSIGError::RESERVED13() {
  246. static TSIGError e(Rcode::RESERVED13());
  247. return (e);
  248. }
  249. inline const TSIGError&
  250. TSIGError::RESERVED14() {
  251. static TSIGError e(Rcode::RESERVED14());
  252. return (e);
  253. }
  254. inline const TSIGError&
  255. TSIGError::RESERVED15() {
  256. static TSIGError e(Rcode::RESERVED15());
  257. return (e);
  258. }
  259. inline const TSIGError&
  260. TSIGError::BAD_SIG() {
  261. static TSIGError e(BAD_SIG_CODE);
  262. return (e);
  263. }
  264. inline const TSIGError&
  265. TSIGError::BAD_KEY() {
  266. static TSIGError e(BAD_KEY_CODE);
  267. return (e);
  268. }
  269. inline const TSIGError&
  270. TSIGError::BAD_TIME() {
  271. static TSIGError e(BAD_TIME_CODE);
  272. return (e);
  273. }
  274. inline const TSIGError&
  275. TSIGError::BAD_MODE() {
  276. static TSIGError e(BAD_MODE_CODE);
  277. return (e);
  278. }
  279. inline const TSIGError&
  280. TSIGError::BAD_NAME() {
  281. static TSIGError e(BAD_NAME_CODE);
  282. return (e);
  283. }
  284. inline const TSIGError&
  285. TSIGError::BAD_ALG() {
  286. static TSIGError e(BAD_ALG_CODE);
  287. return (e);
  288. }
  289. inline const TSIGError&
  290. TSIGError::BAD_TRUNC() {
  291. static TSIGError e(BAD_TRUNC_CODE);
  292. return (e);
  293. }
  294. /// Insert the \c TSIGError as a string into stream.
  295. ///
  296. /// This method convert \c tsig_error into a string and inserts it into the
  297. /// output stream \c os.
  298. ///
  299. /// \param os A \c std::ostream object on which the insertion operation is
  300. /// performed.
  301. /// \param tsig_error An \c TSIGError object output by the operation.
  302. /// \return A reference to the same \c std::ostream object referenced by
  303. /// parameter \c os after the insertion operation.
  304. std::ostream& operator<<(std::ostream& os, const TSIGError& tsig_error);
  305. }
  306. }
  307. #endif // TSIGERROR_H
  308. // Local Variables:
  309. // mode: c++
  310. // End: