opcode.cc 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 <ostream>
  16. #include <exceptions/exceptions.h>
  17. #include <dns/opcode.h>
  18. using namespace std;
  19. namespace isc {
  20. namespace dns {
  21. namespace {
  22. const char *opcodetext[] = {
  23. "QUERY",
  24. "IQUERY",
  25. "STATUS",
  26. "RESERVED3",
  27. "NOTIFY",
  28. "UPDATE",
  29. "RESERVED6",
  30. "RESERVED7",
  31. "RESERVED8",
  32. "RESERVED9",
  33. "RESERVED10",
  34. "RESERVED11",
  35. "RESERVED12",
  36. "RESERVED13",
  37. "RESERVED14",
  38. "RESERVED15"
  39. };
  40. // OPCODEs are 4-bit values. So 15 is the highest code.
  41. const uint8_t MAX_OPCODE = 15;
  42. }
  43. Opcode::Opcode(const uint8_t code) : code_(static_cast<CodeValue>(code)) {
  44. if (code > MAX_OPCODE) {
  45. isc_throw(OutOfRange,
  46. "DNS Opcode is too large to construct: " << code);
  47. }
  48. }
  49. string
  50. Opcode::toText() const {
  51. return (opcodetext[code_]);
  52. }
  53. ostream&
  54. operator<<(std::ostream& os, const Opcode& opcode) {
  55. return (os << opcode.toText());
  56. }
  57. }
  58. }