option_info.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. #ifndef __OPTION_INFO_H
  15. #define __OPTION_INFO_H
  16. #include <stdint.h>
  17. #include "exceptions/exceptions.h"
  18. namespace isc {
  19. namespace badpacket {
  20. /// \brief Option Information
  21. ///
  22. /// Holds details about the options that can be specified on the command line
  23. /// that require values and which control data put in the DNS message sent to
  24. /// the remote system.
  25. ///
  26. /// Currently all of these options correspond to fields in the flags word of the
  27. /// DNS message header, so the information includes details about the position
  28. /// of the fields and an appropriate bit mask.
  29. ///
  30. /// Note that the class does not hold values specified on the command line - it
  31. /// only holds information about command-line options.
  32. class OptionInfo {
  33. public:
  34. /// \brief Array Indexes
  35. ///
  36. /// The data for the flags options are held in an array. Although an enum,
  37. /// only the numeric values are used - they are indexes into arrays.
  38. enum Index {
  39. QR = 0, // Query/response
  40. OP = 1, // Opcode
  41. AA = 2, // Authoritative answer
  42. TC = 3, // Truncated
  43. RD = 4, // Recursion desired
  44. RA = 5, // Recursion available
  45. Z = 6, // Zero (reserved)
  46. AD = 7, // Authenticated data
  47. CD = 8, // Checking disabled
  48. RC = 9, // Response code
  49. QC = 10, // Query count
  50. AC = 11, // Answer count
  51. UC = 12, // Authority count
  52. DC = 13, // Additional count
  53. SIZE = 14 // Number of index values
  54. };
  55. /// \brief Option Parameters
  56. ///
  57. /// Defines a structure that holds information associated with each of the
  58. /// flags field command options. Note all members of the structure are
  59. /// relevant to all options
  60. struct Parameter {
  61. const char short_form; // Short form of the command switch
  62. const char* long_form; // Long form of the command switch
  63. int word; // Byte offset of word in the header
  64. uint16_t mask; // Bit mask of field in the flags word
  65. int offset; // Offset of field in flags word
  66. uint32_t minval; // Minimum valid value for this field
  67. uint32_t defval; // Default value
  68. uint32_t maxval; // Maximum valid value for this field
  69. };
  70. /// \brief Return index for command option
  71. ///
  72. /// Given the short form of a switch, return the index into the options
  73. /// array.
  74. ///
  75. /// \param c The character that is the short form of the command line option.
  76. /// An 'int' is used as the value passed will be the return vaue from
  77. /// 'getopt()' (or equivalent) which is an int.
  78. ///
  79. /// \return A valid index value (else an exception is thrown).
  80. static int getIndex(int c);
  81. /// \brief Return long form of command switch for this field
  82. ///
  83. /// \param index A valid index (one of the values in the 'Index' enum).
  84. ///
  85. /// \return The long option name (e.q. "qr" for the Query/Response field).
  86. static const char* name(int index);
  87. /// \brief Return header word offset
  88. ///
  89. /// Returns the byte offset in the DNS message header of the two-byte word
  90. /// holding the data in question.
  91. ///
  92. /// \param index A valid index (one of the values in the 'Index' enum).
  93. ///
  94. /// \return The offset in the header foe this datum.
  95. static int word(int index);
  96. /// \brief Return mask associated with switch field
  97. ///
  98. /// \param index A valid index (one of the values in the 'Index' enum).
  99. ///
  100. /// \return The mask for this particular option in the DNS message flags
  101. /// field.
  102. static uint16_t mask(int index);
  103. /// \brief Return offset associated with switch field
  104. ///
  105. /// \param index A valid index (one of the values in the 'Index' enum).
  106. ///
  107. /// \return The offset of the field corresponding to this option in the DNS
  108. /// message flags field.
  109. static int offset(int index);
  110. /// \brief Return minimum allowed value of field
  111. ///
  112. /// \param index A valid index (one of the values in the 'Index' enum).
  113. ///
  114. /// \return Minimum allowed value for this option. This is usually 0.
  115. static uint32_t minval(int index);
  116. /// \brief Return default value of a field
  117. ///
  118. /// \param index A valid index (one of the values in the 'Index' enum).
  119. ///
  120. /// \return Default value for this option
  121. static uint32_t defval(int index);
  122. /// \brief Return maximum allowed value of field
  123. ///
  124. /// \param index A valid index (one of the values in the 'Index' enum).
  125. ///
  126. /// \return Maximum allowed value for this option. If the option is a bit
  127. /// in the flags field of the DNS message hearder, this will be 1.
  128. static uint32_t maxval(int index);
  129. /// \brief Check Array Index
  130. ///
  131. /// Checks the passed field index and throws an exception if it does not
  132. /// correspond to one of the valid indexes in the 'Index' enum.
  133. ///
  134. /// \param index An index value.
  135. ///
  136. static void checkIndex(int i) {
  137. if ((i < 0) || (i >= SIZE)) {
  138. isc_throw(isc::OutOfRange, "option index must be in the range "
  139. "0 to " << SIZE);
  140. }
  141. }
  142. };
  143. } // namespace badpacket
  144. } // namespace isc
  145. #endif // __OPTION_INFO_H