option_info.cc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. //
  22. // Fields are:
  23. // * Short option name
  24. // * Long option name
  25. // * Offset of 16-bit word holding datum in DNS message (if applicable)
  26. // * Bit mask for the data (if applicable)
  27. // * Offset of the bit field in the word (if applicable)
  28. // * Default value (this can be ignored if applicable)
  29. // * Minimum value specified on command line
  30. // * Maximum value specified on command line
  31. isc::badpacket::OptionInfo::Parameter option_information[] = {
  32. {'Q', "qr", 2, 0x8000, 15, 0, 0, 1},
  33. {'O', "op", 2, 0x7800, 11, 0, 0, 15},
  34. {'A', "aa", 2, 0x0400, 10, 0, 0, 1},
  35. {'T', "tc", 2, 0x0200, 9, 0, 0, 1},
  36. {'D', "rd", 2, 0x0100, 8, 0, 0, 1},
  37. {'R', "ra", 2, 0x0080, 7, 0, 0, 1},
  38. {'Z', "z", 2, 0x0040, 6, 0, 0, 1},
  39. {'U', "ad", 2, 0x0020, 5, 0, 0, 1},
  40. {'C', "cd", 2, 0x0010, 4, 0, 0, 1},
  41. {'E', "rc", 2, 0x000F, 0, 0, 0, 15},
  42. {'Y', "qc", 4, 0, 0, 1, 0, 0xFFFF},
  43. {'W', "ac", 6, 0, 0, 0, 0, 0xFFFF},
  44. {'H', "uc", 8, 0, 0, 0, 0, 0xFFFF},
  45. {'I', "dc", 10, 0, 0, 0, 0, 0xFFFF},
  46. {'M', "ms", 0, 0, 0, 0, 1, 65536}
  47. };
  48. } // Anonymous namespace
  49. namespace isc {
  50. namespace badpacket {
  51. // Locate the index of the information in the array from the short switch.
  52. int
  53. OptionInfo::getIndex(int c) {
  54. for (int i = 0; i < SIZE; ++i) {
  55. if (option_information[i].short_form == c) {
  56. return (i);
  57. }
  58. }
  59. isc_throw(isc::BadValue, "unknown option: " << c);
  60. }
  61. // Methods to return values from the array
  62. const char*
  63. OptionInfo::name(int i) {
  64. checkIndex(i);
  65. return (option_information[i].long_form);
  66. }
  67. uint16_t
  68. OptionInfo::mask(int i) {
  69. checkIndex(i);
  70. return (option_information[i].mask);
  71. }
  72. int
  73. OptionInfo::word(int i) {
  74. checkIndex(i);
  75. return (option_information[i].word);
  76. }
  77. int
  78. OptionInfo::offset(int i) {
  79. checkIndex(i);
  80. return (option_information[i].offset);
  81. }
  82. uint32_t
  83. OptionInfo::minval(int i) {
  84. checkIndex(i);
  85. return (option_information[i].minval);
  86. }
  87. uint32_t
  88. OptionInfo::defval(int i) {
  89. checkIndex(i);
  90. return (option_information[i].defval);
  91. }
  92. uint32_t
  93. OptionInfo::maxval(int i) {
  94. checkIndex(i);
  95. return (option_information[i].maxval);
  96. }
  97. } // namespace badpacket
  98. } // namespace isc