rcode.cc 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright (C) 2010 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 <string>
  15. #include <sstream>
  16. #include <ostream>
  17. #include <exceptions/exceptions.h>
  18. #include <dns/rcode.h>
  19. using namespace std;
  20. namespace isc {
  21. namespace dns {
  22. namespace {
  23. // This diagram shows the wire-format representation of the 12-bit extended
  24. // form RCODEs and its relationship with implementation specific parameters.
  25. //
  26. // 0 3 11 15
  27. // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  28. // |UNUSED | EXTENDED-RCODE | RCODE |
  29. // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  30. // <= EXTRCODE_SHIFT (4 bits)
  31. const unsigned int EXTRCODE_SHIFT = 4;
  32. const unsigned int RCODE_MASK = 0x000f;
  33. // EDNS-extended RCODEs are 12-bit unsigned integers. 0xfff is the highest.
  34. const uint16_t MAX_RCODE = 0xfff;
  35. const char* const rcodetext[] = {
  36. "NOERROR",
  37. "FORMERR",
  38. "SERVFAIL",
  39. "NXDOMAIN",
  40. "NOTIMP",
  41. "REFUSED",
  42. "YXDOMAIN",
  43. "YXRRSET",
  44. "NXRRSET",
  45. "NOTAUTH",
  46. "NOTZONE",
  47. "RESERVED11",
  48. "RESERVED12",
  49. "RESERVED13",
  50. "RESERVED14",
  51. "RESERVED15",
  52. "BADVERS"
  53. };
  54. }
  55. Rcode::Rcode(const uint16_t code) : code_(code) {
  56. if (code_ > MAX_RCODE) {
  57. isc_throw(OutOfRange, "Rcode is too large to construct");
  58. }
  59. }
  60. Rcode::Rcode(const uint8_t code, const uint8_t extended_code) :
  61. code_((extended_code << EXTRCODE_SHIFT) | (code & RCODE_MASK))
  62. {
  63. if (code > RCODE_MASK) {
  64. isc_throw(OutOfRange,
  65. "Base Rcode is too large to construct: "
  66. << static_cast<unsigned int>(code));
  67. }
  68. }
  69. uint8_t
  70. Rcode::getExtendedCode() const {
  71. return (code_ >> EXTRCODE_SHIFT);
  72. }
  73. string
  74. Rcode::toText() const {
  75. if (code_ < sizeof(rcodetext) / sizeof (const char*)) {
  76. return (rcodetext[code_]);
  77. }
  78. ostringstream oss;
  79. oss << code_;
  80. return (oss.str());
  81. }
  82. ostream&
  83. operator<<(std::ostream& os, const Rcode& rcode) {
  84. return (os << rcode.toText());
  85. }
  86. }
  87. }