option_info.cc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 "option_info.h"
  15. namespace {
  16. // Define the various options for the command switches. This includes both the
  17. // long form and short form of the switch. Unfortunately this means that the
  18. // information is duplicated here and where the long options are specified for
  19. // getopt_long, but this inconvenience is outweighed by the simplified command
  20. // processing.
  21. isc::badpacket::OptionInfo::Parameter option_information[] = {
  22. {'Q', "qr", 2, 0x8000, 15, 0, 0, 1},
  23. {'O', "op", 2, 0x7800, 11, 0, 0, 15},
  24. {'A', "aa", 2, 0x0400, 10, 0, 0, 1},
  25. {'T', "tc", 2, 0x0200, 9, 0, 0, 1},
  26. {'D', "rd", 2, 0x0100, 8, 0, 0, 1},
  27. {'R', "ra", 2, 0x0080, 7, 0, 0, 1},
  28. {'Z', "z", 2, 0x0040, 6, 0, 0, 1},
  29. {'U', "ad", 2, 0x0020, 5, 0, 0, 1},
  30. {'C', "cd", 2, 0x0010, 4, 0, 0, 1},
  31. {'E', "rc", 2, 0x000F, 0, 0, 0, 15},
  32. {'Y', "qc", 4, 0, 0, 1, 0, 0xFFFF},
  33. {'W', "ac", 6, 0, 0, 0, 0, 0xFFFF},
  34. {'H', "uc", 8, 0, 0, 0, 0, 0xFFFF},
  35. {'I', "dc", 10, 0, 0, 0, 0, 0xFFFF}
  36. };
  37. } // Anonymous namespace
  38. namespace isc {
  39. namespace badpacket {
  40. // Locate the index of the information in the array from the short switch.
  41. int
  42. OptionInfo::getIndex(int c) {
  43. for (int i = 0; i < SIZE; ++i) {
  44. if (c == option_information[i].short_form) {
  45. return (i);
  46. }
  47. }
  48. isc_throw(isc::BadValue, "unknown option: " << c);
  49. }
  50. // Methods to return values from the array
  51. const char*
  52. OptionInfo::name(int i) {
  53. checkIndex(i);
  54. return (option_information[i].long_form);
  55. }
  56. uint16_t
  57. OptionInfo::mask(int i) {
  58. checkIndex(i);
  59. return (option_information[i].mask);
  60. }
  61. int
  62. OptionInfo::word(int i) {
  63. checkIndex(i);
  64. return (option_information[i].word);
  65. }
  66. int
  67. OptionInfo::offset(int i) {
  68. checkIndex(i);
  69. return (option_information[i].offset);
  70. }
  71. uint32_t
  72. OptionInfo::minval(int i) {
  73. checkIndex(i);
  74. return (option_information[i].minval);
  75. }
  76. uint32_t
  77. OptionInfo::defval(int i) {
  78. checkIndex(i);
  79. return (option_information[i].defval);
  80. }
  81. uint32_t
  82. OptionInfo::maxval(int i) {
  83. checkIndex(i);
  84. return (option_information[i].maxval);
  85. }
  86. } // namespace badpacket
  87. } // namespace isc