command_options.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 "option_info.h"
  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, both are the same). The values are stored in an array
  35. /// and 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 CommandOptions Constructor
  43. ///
  44. /// Set values to defaults.
  45. CommandOptions() {
  46. reset();
  47. }
  48. /// \brief Return minimum value for option
  49. ///
  50. /// \param index Index of the command-line option.
  51. ///
  52. /// \return uint32_t holding the minimum value given (or the default if
  53. /// the option was not specified on the command line).
  54. uint32_t minimum(int index) const;
  55. /// \brief Return maximum value for option
  56. ///
  57. /// \param index Index of the command-line option.
  58. ///
  59. /// \return uint32_t holding the maximum value given (or the default if
  60. /// the option was not specified on the command line).
  61. uint32_t maximum(int index) const;
  62. /// \brief Return if option was given on command line
  63. ///
  64. /// \param index Index of the command-line option.
  65. ///
  66. /// \return true if the option was present, false if not.
  67. bool present(int index) const;
  68. /// \brief Return Target Address
  69. std::string getAddress() const {
  70. return address_;
  71. }
  72. /// \brief Return Target Port
  73. uint16_t getPort() const {
  74. return port_;
  75. }
  76. /// \brief Return Timeout
  77. int getTimeout() const {
  78. return timeout_;
  79. }
  80. /// \brief Return qname
  81. std::string getQname() const {
  82. return qname_;
  83. }
  84. /// \brief Reset To Defaults
  85. void reset() {
  86. address_ = "127.0.0.1";
  87. port_ = 53;
  88. timeout_ = 500;
  89. qname_ = "www.example.com";
  90. for (int i = 0; i < OptionInfo::SIZE; ++i) {
  91. options_[i].minimum = OptionInfo::defval(i);
  92. options_[i].maximum = OptionInfo::defval(i);
  93. options_[i].present = false;
  94. }
  95. }
  96. /// \brief Parse Command Line
  97. ///
  98. /// Parses the command line and stores the selected options. The parsing
  99. /// also handles the --help and --version commands: both of these will cause
  100. /// some text to be printed to stdout, after which exit() is called to
  101. /// terminate the program.
  102. ///
  103. /// \param argc Argument count passed to main().
  104. /// \param argv Argument value array passed to main().
  105. void parse(int argc, char* const argv[]);
  106. /// \brief Print Usage Information And Exit Program
  107. void usage();
  108. /// \brief Print Version Information And Exit Program
  109. void version();
  110. // The following are protected to aid testing
  111. protected:
  112. /// \brief Process Option Value
  113. ///
  114. /// Processes a specific command-line option, interpreting the value and
  115. /// placing it in the appropriate location. On error a BadValue exception
  116. /// is thrown.
  117. ///
  118. /// \param c Short form option character from the command line
  119. /// \param value Value of the option read from the command line
  120. void processOptionValue(int c, const char* value);
  121. // Member variables
  122. private:
  123. struct {
  124. uint32_t minimum; ///< Minimum value specified
  125. uint32_t maximum; ///< Maximum value specified
  126. bool present; ///< true if specified on command line
  127. } options_[OptionInfo::SIZE]; ///< Information about command options
  128. ///< Value of options (minimum and maximum)
  129. std::string address_; ///< Address to where query is sent
  130. uint16_t port_; ///< Target port
  131. int timeout_; ///< Timeout for query
  132. std::string qname_; ///< Query to make
  133. };
  134. } // namespace badpacket
  135. } // namespace isc
  136. #endif // __COMMAND_OPTIONS_H