option_info.h 6.9 KB

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