tsigerror.h 10 KB

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