tsigerror.cc 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. #include <ostream>
  15. #include <string>
  16. #include <boost/lexical_cast.hpp>
  17. #include <exceptions/exceptions.h>
  18. #include <dns/rcode.h>
  19. #include <dns/tsigerror.h>
  20. namespace isc {
  21. namespace dns {
  22. namespace {
  23. const char* const tsigerror_text[] = {
  24. "BADSIG",
  25. "BADKEY",
  26. "BADTIME"
  27. };
  28. }
  29. TSIGError::TSIGError(Rcode rcode) : code_(rcode.getCode()) {
  30. if (code_ > MAX_RCODE_FOR_TSIGERROR) {
  31. isc_throw(OutOfRange, "Invalid RCODE for TSIG Error: " << rcode);
  32. }
  33. }
  34. std::string
  35. TSIGError::toText() const {
  36. if (code_ <= MAX_RCODE_FOR_TSIGERROR) {
  37. return (Rcode(code_).toText());
  38. } else if (code_ <= BAD_TIME_CODE) {
  39. return (tsigerror_text[code_ - (MAX_RCODE_FOR_TSIGERROR + 1)]);
  40. } else {
  41. return (boost::lexical_cast<std::string>(code_));
  42. }
  43. }
  44. Rcode
  45. TSIGError::toRcode() const {
  46. if (code_ <= MAX_RCODE_FOR_TSIGERROR) {
  47. return (Rcode(code_));
  48. }
  49. if (code_ > BAD_TIME_CODE) {
  50. return (Rcode::SERVFAIL());
  51. }
  52. return (Rcode::NOTAUTH());
  53. }
  54. std::ostream&
  55. operator<<(std::ostream& os, const TSIGError& error) {
  56. return (os << error.toText());
  57. }
  58. } // namespace dns
  59. } // namespace isc