command_options.h 5.5 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. /// Some of the options perform general control (like setting the address of the
  28. /// nameserver under test, while the rest set values in the DNS message being
  29. /// sent. Each of the latter options can be specified as either:
  30. ///
  31. /// - \c --option value
  32. /// - \c --option value1-value2
  33. ///
  34. /// Either way, two values are extracted the low value and the high value (in
  35. /// the former case, both are the same). The values are stored and can be
  36. /// returned on request.
  37. ///
  38. /// For simplicity, the class takes care of the --help and --version flags,
  39. /// each of which will cause a message to be printed to stdout and the program
  40. /// to terminate.
  41. class CommandOptions {
  42. public:
  43. /// \brief Default Constructor
  44. ///
  45. /// Set values to defaults.
  46. CommandOptions() {
  47. reset();
  48. }
  49. /// \brief Return minimum value for option
  50. ///
  51. /// Applicable only to an option affecting a field in the message, this
  52. /// method returns the minimum value that was given on the command line.
  53. /// (If only a single value was given, it will be that value returned.)
  54. /// If the option was not specified on the command line, the default value
  55. /// set in the OptionsInfo class will be returned.
  56. ///
  57. /// \param index Index of the command-line option.
  58. ///
  59. /// \return uint32_t holding the minimum value given (or the default if
  60. /// the option was not specified on the command line).
  61. uint32_t minimum(int index) const;
  62. /// \brief Return maximum value for option
  63. ///
  64. /// Applicable only to an option affecting a field in the message, this
  65. /// method returns the maximum value that was given on the command line.
  66. /// (If only a single value was given, it will be that value returned.)
  67. /// If the option was not specified on the command line, the default value
  68. /// set in the OptionsInfo class will be returned.
  69. ///
  70. /// \param index Index of the command-line option.
  71. ///
  72. /// \return uint32_t holding the maximum value given (or the default if
  73. /// the option was not specified on the command line).
  74. uint32_t maximum(int index) const;
  75. /// \brief Reports if option was given on command line
  76. ///
  77. /// \param index Index of the command-line option.
  78. ///
  79. /// \return true if the option was present, false if not.
  80. bool present(int index) const;
  81. /// \brief Return target address
  82. std::string getAddress() const {
  83. return address_;
  84. }
  85. /// \brief Return target port
  86. uint16_t getPort() const {
  87. return port_;
  88. }
  89. /// \brief Return timeout
  90. int getTimeout() const {
  91. return timeout_;
  92. }
  93. /// \brief Return qname
  94. std::string getQname() const {
  95. return qname_;
  96. }
  97. /// \brief Reset to defaults
  98. ///
  99. /// Resets the CommandOptions object to default values.
  100. void reset();
  101. /// \brief Parse command line
  102. ///
  103. /// Parses the command line and stores the selected options. The parsing
  104. /// also handles the --help and --version commands: both of these will cause
  105. /// some text to be printed to stdout, after which exit() is called to
  106. /// terminate the program.
  107. ///
  108. /// \param argc Argument count passed to main().
  109. /// \param argv Argument value array passed to main().
  110. void parse(int argc, char* const argv[]);
  111. /// \brief Print usage information and exit program
  112. void usage();
  113. /// \brief Print version information and exit program
  114. void version();
  115. private:
  116. /// \brief Process Option Value
  117. ///
  118. /// Processes a specific command-line option, interpreting the value and
  119. /// placing it in the appropriate location. On error a BadValue exception
  120. /// is thrown.
  121. ///
  122. /// \param c Short form option character from the command line
  123. /// \param value Value of the option read from the command line
  124. void processOptionValue(int c, const char* value);
  125. // Member variables
  126. struct {
  127. uint32_t minimum; ///< Minimum value specified
  128. uint32_t maximum; ///< Maximum value specified
  129. bool present; ///< true if specified on command line
  130. } options_[OptionInfo::SIZE]; ///< Information about command options
  131. std::string address_; ///< Address to where query is sent
  132. uint16_t port_; ///< Target port
  133. int timeout_; ///< Timeout for query
  134. std::string qname_; ///< Query to make
  135. };
  136. } // namespace badpacket
  137. } // namespace isc
  138. #endif // __COMMAND_OPTIONS_H