scan.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 __SCAN_H
  15. #define __SCAN_H
  16. #include <stdint.h>
  17. #include <boost/scoped_ptr.hpp>
  18. #include <config.h>
  19. #include <asiolink/io_service.h>
  20. #include <asiodns/io_fetch.h>
  21. #include <util/buffer.h>
  22. #include "command_options.h"
  23. #include "header_flags.h"
  24. namespace isc {
  25. namespace badpacket {
  26. /// \brief Field Scan
  27. ///
  28. /// This class implements a field scan. Given a CommandOptions object, it will
  29. /// cycle through combinations of the given options, sending and receiving
  30. /// messages. For each packet exchange, a summary is written to stdout.
  31. class Scan : public isc::asiodns::IOFetch::Callback {
  32. public:
  33. /// \brief Constructor
  34. Scan() : service_(), result_()
  35. {}
  36. /// \brief Run Scan
  37. ///
  38. /// Actually performs the scan for the combination of options.
  39. ///
  40. /// \param options Command-line options
  41. void scan(const CommandOptions& options);
  42. /// \brief Callback
  43. ///
  44. /// This class is derived from the IOFetch::Callback class as it acts as the
  45. /// callback object. When an asynchronous I/I has completed, this method
  46. /// will be called.
  47. ///
  48. /// \param result Result of the asynchronous I/O. Zero implies success.
  49. virtual void operator()(isc::asiodns::IOFetch::Result result);
  50. private:
  51. /// \brief Iterate over flags fields options
  52. ///
  53. /// This method relies on the fact that data concerning the settings for
  54. /// the fields in the flags word of the DNS message are held at adjacent
  55. /// elements in the various data arrays and so can be accessed by a set
  56. /// of contiguous index values.
  57. ///
  58. /// The method is passed an index value and the maximum valid index value.
  59. /// If a set of values for the field at the given index was specified on
  60. /// the command line, it loops through those values and sets the appropriate
  61. /// value in the a copy of the DNS message header flags. It then calls
  62. /// itself with an incremented index. If the value was not given, it just
  63. /// sets a default value calls itself (with the incremented index). When
  64. /// called with an index above the maximum valid index, the header flags
  65. /// in the message buffer are set and the next stage of processing called.
  66. ///
  67. /// In this way, all fields can be cycled through without the need for a
  68. /// single function to nest loops very deeply.
  69. ///
  70. /// \param msgbuf Message that will be sent to the remote nameserver. The
  71. /// QID given will be ignored - the value used will be determined by
  72. /// the sending code
  73. /// \param options Command-line options (the important ones being address,
  74. /// port and timeout).
  75. /// \param flags Header flags
  76. /// \param index Index of the current field to be processed.
  77. /// \param maxindex Maximum valid index value
  78. void iterateFlags(isc::util::OutputBufferPtr& msgbuf,
  79. const CommandOptions& options, HeaderFlags& flags,
  80. int index, int maxindex);
  81. /// \brief Start iterating over flags field options
  82. ///
  83. /// Kicks off the call to \c iterateFlags by calling it with the initial
  84. /// index value.
  85. ///
  86. /// \param msgbuf Message that will be sent to the remote nameserver. The
  87. /// QID given will be ignored - the value used will be determined by
  88. /// the sending code
  89. /// \param options Command-line options (the important ones being address,
  90. /// port and timeout).
  91. void iterateFlagsStart(isc::util::OutputBufferPtr& msgbuf,
  92. const CommandOptions& options);
  93. /// \brief Iterate over count fields
  94. ///
  95. /// In a manner similar to iterateFlags, this iterates over all specified
  96. /// values for each count field, recursively calling itself to process the
  97. /// next field. When all fields have been processed, it chains to the
  98. /// next stage of packet processing.
  99. ///
  100. /// \param msgbuf Message that will be sent to the remote nameserver. The
  101. /// QID given will be ignored - the value used will be determined by
  102. /// the sending code
  103. /// \param options Command-line options (the important ones being address,
  104. /// port and timeout).
  105. /// \param index Index of the current field to be processed.
  106. /// \param maxindex Maximum valid index value
  107. void iterateCount(isc::util::OutputBufferPtr& msgbuf,
  108. const CommandOptions& options, int index, int maxindex);
  109. /// \brief Start iterating over count fields
  110. ///
  111. /// Kicks off the call to \c iterateCount by calling it with the initial
  112. /// index value.
  113. ///
  114. /// \param msgbuf Message that will be sent to the remote nameserver. The
  115. /// QID given will be ignored - the value used will be determined by
  116. /// the sending code
  117. /// \param options Command-line options (the important ones being address,
  118. /// port and timeout).
  119. void iterateCountStart(isc::util::OutputBufferPtr& msgbuf,
  120. const CommandOptions& options);
  121. /// \brief Iterate over message sizes
  122. ///
  123. /// If the message size option is given on the command line, the message
  124. /// sent to the remote system is either truncated or extended (with zeroes)
  125. /// before being set.
  126. ///
  127. /// \param msgbuf Message that will be sent to the remote nameserver. The
  128. /// QID given will be ignored - the value used will be determined by
  129. /// the sending code
  130. /// \param options Command-line options (the important ones being address,
  131. /// port and timeout).
  132. void sizeMessage(isc::util::OutputBufferPtr& msgbuf,
  133. const CommandOptions& options);
  134. /// \brief Scan One Value
  135. ///
  136. /// Performs one exchange of messages with the remote nameserver, sending
  137. /// the specified message.
  138. ///
  139. /// \param msgbuf Message that will be sent to the remote nameserver. The
  140. /// QID given will be ignored - the value used will be determined by
  141. /// the sending code
  142. /// \param options Command-line options (the important ones being address,
  143. /// port and timeout).
  144. void scanOne(isc::util::OutputBufferPtr& msgbuf,
  145. const CommandOptions& options);
  146. /// \brief Perform I/O
  147. ///
  148. /// Performs a single query to the nameserver and reads the response. It
  149. /// outputs a summary of the result.
  150. ///
  151. /// \param sendbuf Buffer sent to the nameserver
  152. /// \param recvbuf Buffer to hold reply from the nameserver
  153. /// \param options Command-line options
  154. void performIO(isc::util::OutputBufferPtr& sendbuf,
  155. isc::util::OutputBufferPtr& recvbuf,
  156. const CommandOptions& options);
  157. /// \brief Get Fields
  158. ///
  159. /// Interprets the fields in a DNS message and converts them to a brief
  160. /// textual format.
  161. ///
  162. /// \param msg Message for which the header is value
  163. ///
  164. /// \return A string that holds a textual interpretation of all the fields
  165. /// in the header.
  166. std::string getFields(isc::util::OutputBufferPtr& msgbuf);
  167. // Member variables
  168. boost::scoped_ptr<isc::asiolink::IOService> service_;
  169. ///< Service to run the scan
  170. isc::asiodns::IOFetch::Result result_; ///< Result of the I/O
  171. };
  172. } // namespace test
  173. } // namespace isc
  174. #endif // __SCAN_H