opcode.cc 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // Copyright (C) 2010, 2015 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: "
  47. << static_cast<unsigned>(code));
  48. }
  49. }
  50. string
  51. Opcode::toText() const {
  52. return (opcodetext[code_]);
  53. }
  54. ostream&
  55. operator<<(std::ostream& os, const Opcode& opcode) {
  56. return (os << opcode.toText());
  57. }
  58. }
  59. }