command_options.h 5.8 KB

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