rcode.cc 2.6 KB

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