command_options.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 __COMMAND_OPTIONS_H
  15. #define __COMMAND_OPTIONS_H
  16. #include <cstdlib>
  17. #include <stdint.h>
  18. #include <utility>
  19. #include <boost/program_options.hpp>
  20. namespace isc {
  21. namespace badpacket {
  22. /// \brief Command Options
  23. ///
  24. /// This class is responsible for parsing the command-line and storing the
  25. /// specified options.
  26. ///
  27. /// Each option setting the state of one of the fields in the flags word in the
  28. /// DNS packet can be specified as either:
  29. ///
  30. /// - \c --option value
  31. /// - \c --option value1-value2
  32. ///
  33. /// Either way, two values are extracted the low value and the high value (in
  34. /// the former case, bost are the same). The values are stored in a
  35. /// "FlagValues" structure, which can be returned on request.
  36. ///
  37. /// For simplicity, the class also takes care of the --help and --version flags,
  38. /// each of which will cause a message to be printed to stdout and the program
  39. /// to terminate.
  40. class CommandOptions {
  41. public:
  42. /// \brief Flags Word Values
  43. ///
  44. /// Structure holding the values for the flag settings. Each variable in
  45. /// the structure corresponds to one of the fields in the flags word. The
  46. /// variables are two-ewlement arrays: element 0 of the array holds the low
  47. /// value in the range given, and element 1 the high value. If only a
  48. /// single value is given, both elements hold the same value.
  49. struct FlagValues {
  50. uint32_t qr[2]; // QR bit
  51. uint32_t op[2]; // OPCODE field
  52. uint32_t aa[2]; // AA bit
  53. uint32_t tc[2]; // TC bit
  54. uint32_t rd[2]; // RD bit
  55. uint32_t ra[2]; // RA bit
  56. uint32_t z[2]; // Z bit (reserved bit)
  57. uint32_t ad[2]; // AD bit
  58. uint32_t cd[2]; // CD bit
  59. uint32_t rc[2]; // RCODE field
  60. /// \brief Default Constructor
  61. ///
  62. /// Sets everything to zero.
  63. FlagValues() {
  64. reset();
  65. }
  66. /// \brief Reset All Values to Zero
  67. void reset() {
  68. qr[0] = qr[1] = 0;
  69. op[0] = op[1] = 0;
  70. aa[0] = aa[1] = 0;
  71. tc[0] = tc[1] = 0;
  72. rd[0] = rd[1] = 0;
  73. ra[0] = ra[1] = 0;
  74. z[0] = z[1] = 0;
  75. ad[0] = ad[1] = 0;
  76. cd[0] = cd[1] = 0;
  77. rc[0] = rc[1] = 0;
  78. }
  79. };
  80. /// \brief CommandOptions Constructor
  81. ///
  82. /// Set values to defaults.
  83. CommandOptions() {
  84. reset();
  85. }
  86. /// \brief Return Flags Word Values
  87. ///
  88. /// Returns a copy of the flags word structure for use by the caller. This
  89. /// structure holds the flags field settings specified on the command line.
  90. ///
  91. /// \return Copy of the values specified on the command line.
  92. FlagValues getFlagValues() const {
  93. return values_;
  94. }
  95. /// \brief Return Target Address
  96. std::string getAddress() const {
  97. return address_;
  98. }
  99. /// \brief Return Target Port
  100. uint16_t getPort() const {
  101. return port_;
  102. }
  103. /// \brief Return Timeout
  104. int getTimeout() const {
  105. return timeout_;
  106. }
  107. /// \brief Return qname
  108. std::string getQname() const {
  109. return qname_;
  110. }
  111. /// \brief Reset To Defaults
  112. void reset() {
  113. values_.reset();
  114. address_ = "127.0.0.1";
  115. port_ = 53;
  116. timeout_ = 500;
  117. qname_ = "www.example.com";
  118. }
  119. /// \brief Parse Command Line
  120. ///
  121. /// Parses the command line and stores the selected options. The parsing
  122. /// also handles the --help and --version commands: both of these will cause
  123. /// some text to be printed to stdout, after which exit() is called to
  124. /// terminate the program.
  125. ///
  126. /// \param argc Argument count passed to main().
  127. /// \param argv Argument value array passed to main().
  128. void parse(int argc, char* const argv[]);
  129. /// \brief Print Usage Information
  130. void usage();
  131. /// \brief Print Version Information
  132. void version();
  133. // The following are protected to aid testing
  134. protected:
  135. /// \brief Process Option Value
  136. ///
  137. /// Processes a specific command-line option, interpreting the value and
  138. /// placing it in the appropriate location. On error a BadValue exception
  139. /// is thrown.
  140. ///
  141. /// \param arg flag argument read from the command line
  142. /// \param where Two-element uint32_t array into which the data is put
  143. /// \param minval Minimum allowed value
  144. /// \param maxval Maximum allowed value
  145. void processOptionValue(const char* arg, uint32_t* where, uint32_t minval,
  146. uint32_t maxval);
  147. // Member variables
  148. private:
  149. FlagValues values_; ///< Values read from command line
  150. std::string address_; ///< Address to where query is sent
  151. uint16_t port_; ///< Target port
  152. int timeout_; ///< Timeout for query
  153. std::string qname_; ///< Query to make
  154. };
  155. } // namespace badpacket
  156. } // namespace isc
  157. #endif // __COMMAND_OPTIONS_H