scan.h 7.9 KB

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