scan.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. #include <iostream>
  15. #include <iomanip>
  16. #include <sstream>
  17. #include <string>
  18. #include <stdlib.h>
  19. #include <config.h>
  20. #include <asio.hpp>
  21. #include <asiolink/io_address.h>
  22. #include <asiodns/io_fetch.h>
  23. #include <util/buffer.h>
  24. #include <dns/message.h>
  25. #include <dns/messagerenderer.h>
  26. #include <dns/opcode.h>
  27. #include <dns/question.h>
  28. #include <dns/rcode.h>
  29. #include <dns/rrclass.h>
  30. #include <dns/rrtype.h>
  31. #include <util/strutil.h>
  32. #include "command_options.h"
  33. #include "header_flags.h"
  34. #include "scan.h"
  35. using namespace std;
  36. using namespace isc::asiolink;
  37. using namespace isc::asiodns;
  38. using namespace isc::dns;
  39. using namespace isc::util;
  40. using namespace isc::util::str;
  41. namespace isc {
  42. namespace badpacket {
  43. // Perform the scan from start to end on a single question.
  44. void
  45. Scan::scan(const CommandOptions& options) {
  46. // Create the message buffer to use
  47. Message message(Message::RENDER);
  48. message.setOpcode(Opcode::QUERY());
  49. message.setRcode(Rcode::NOERROR());
  50. message.addQuestion(Question(Name(options.getQname()), RRClass::IN(),
  51. RRType::A()));
  52. OutputBufferPtr msgbuf(new OutputBuffer(512));
  53. MessageRenderer renderer;
  54. renderer.setBuffer(msgbuf.get());
  55. message.toWire(renderer);
  56. iterateFlagsStart(msgbuf, options);
  57. }
  58. // Iterate through the various settings in the flags fields.
  59. void
  60. Scan::iterateFlagsStart(OutputBufferPtr& msgbuf, const CommandOptions& options) {
  61. HeaderFlags flags;
  62. iterateFlags(msgbuf, options, flags, OptionInfo::FLAGS_START,
  63. OptionInfo::FLAGS_END);
  64. }
  65. void
  66. Scan::iterateFlags(OutputBufferPtr& msgbuf, const CommandOptions& options,
  67. HeaderFlags& flags, int index, int maxindex)
  68. {
  69. // Is the index valid?
  70. if (index <= maxindex) {
  71. // Index is valid, did the command line specify a range of values for
  72. // this field?
  73. if (options.present(index)) {
  74. // It did, so loop between minimum and maximum values given.
  75. for (uint32_t i = options.minimum(index);
  76. i <= options.maximum(index); ++i) {
  77. flags.set(index, i);
  78. // Recurse to set the next option.
  79. iterateFlags(msgbuf, options, flags, (index + 1), maxindex);
  80. }
  81. } else {
  82. // Not specified on command line, so set the default value in the
  83. // flags and process the next index.
  84. flags.set(index, OptionInfo::defval(index));
  85. iterateFlags(msgbuf, options, flags, (index + 1), maxindex);
  86. }
  87. } else {
  88. // Index is not valid, so we have recursed enough to process all the
  89. // flags fields. Set the value in the message header and call the next
  90. // stage in the processing.
  91. //
  92. // (In the next statement, the "word" offset of in the header is the
  93. // same for all flags options, so the value for an arbitrary field
  94. // (QR) has been used.)
  95. msgbuf->writeUint16At(flags.getValue(),
  96. OptionInfo::word(OptionInfo::QR));
  97. iterateCountStart(msgbuf, options);
  98. }
  99. }
  100. // Iterate through the various count fields
  101. void
  102. Scan::iterateCountStart(OutputBufferPtr& msgbuf, const CommandOptions& options)
  103. {
  104. iterateCount(msgbuf, options, OptionInfo::COUNT_START,
  105. OptionInfo::COUNT_END);
  106. }
  107. void
  108. Scan::iterateCount(OutputBufferPtr& msgbuf, const CommandOptions& options,
  109. int index, int maxindex)
  110. {
  111. // If the index is valid, process the iteration over the range for this
  112. // flags field.
  113. if (index <= maxindex) {
  114. // Index is valid, did the command line specify a range of values for
  115. // this field?
  116. if (options.present(index)) {
  117. // It did, so loop between minimum and maximum values given.
  118. for (uint32_t i = options.minimum(index);
  119. i <= options.maximum(index); ++i) {
  120. // Set the value in the message buffer header and recurse to
  121. // the next option.
  122. msgbuf->writeUint16At(i, OptionInfo::word(index));
  123. iterateCount(msgbuf, options, (index + 1), maxindex);
  124. }
  125. } else {
  126. // Not specified on command line, so do change anything and process
  127. // the next index.
  128. iterateCount(msgbuf, options, (index + 1), maxindex);
  129. }
  130. } else {
  131. // Index is not valid, so we have recursed enough to process all the
  132. // flags fields. Do the next stage of the processing.
  133. sizeMessage(msgbuf, options);
  134. }
  135. }
  136. // Alter the message size.
  137. void
  138. Scan::sizeMessage(OutputBufferPtr& msgbuf, const CommandOptions& options) {
  139. if (options.present(OptionInfo::MS)) {
  140. // Iterate over the range of message sizes
  141. for (size_t i = options.minimum(OptionInfo::MS);
  142. i <= options.maximum(OptionInfo::MS); ++i) {
  143. // Copy the data into a new buffer.
  144. OutputBufferPtr newbuf(new OutputBuffer(i));
  145. newbuf->writeData(msgbuf->getData(), min(msgbuf->getLength(), i));
  146. // Pad with junk (actually pseudo-random data) if the new buffer is
  147. // longer than the old.
  148. for (size_t j = newbuf->getLength(); j < i; ++j) {
  149. newbuf->writeUint8(static_cast<uint8_t>(rand() & 0xFFU));
  150. }
  151. // ... and process.
  152. scanOne(newbuf, options);
  153. }
  154. } else {
  155. // No packet size given, just process the message as it.
  156. scanOne(msgbuf, options);
  157. }
  158. }
  159. // Perform the message exchange for a single combination command options.
  160. void
  161. Scan::scanOne(isc::util::OutputBufferPtr& msgbuf, const CommandOptions& options) {
  162. // Store the interpretation of outgoing message.
  163. string fields = string("(") + getFields(msgbuf) + string(")");
  164. // Do the I/O, waiting for a reply
  165. OutputBufferPtr replybuf(new OutputBuffer(512));
  166. performIO(msgbuf, replybuf, options);
  167. string status = "";
  168. string returned = "";
  169. switch (result_) {
  170. case IOFetch::SUCCESS:
  171. {
  172. status = "SUCCESS";
  173. // Parse the reply and get the fields
  174. returned = string("(") + getFields(replybuf) + string(")");
  175. lowercase(returned);
  176. }
  177. break;
  178. case IOFetch::TIME_OUT:
  179. status = "TIMEOUT";
  180. break;
  181. case IOFetch::STOPPED:
  182. status = "STOPPED";
  183. break;
  184. default:
  185. status = "UNKNOWN";
  186. }
  187. // ... and output the result
  188. cout << status << ": " << fields << " " << returned << "\n";
  189. }
  190. // Get interpretation of the message fields.
  191. std::string
  192. Scan::getFields(isc::util::OutputBufferPtr& msgbuf) {
  193. // Header flags. (Note that all come from the same word in the message, so
  194. // using the word offset for the QR flag as the position in the buffer from
  195. // which to extract the values is valid.)
  196. HeaderFlags flags;
  197. InputBuffer inbuf(msgbuf->getData(), msgbuf->getLength());
  198. inbuf.setPosition(OptionInfo::word(OptionInfo::QR));
  199. flags.setValue(inbuf.readUint16());
  200. std::ostringstream os;
  201. os << std::hex << std::uppercase <<
  202. "QR:" << flags.get(OptionInfo::QR) <<
  203. " OP:" << flags.get(OptionInfo::OP) <<
  204. " AA:" << flags.get(OptionInfo::AA) <<
  205. " TC:" << flags.get(OptionInfo::TC) <<
  206. " RD:" << flags.get(OptionInfo::RD) <<
  207. " RA:" << flags.get(OptionInfo::RA) <<
  208. " Z:" << flags.get(OptionInfo::Z) <<
  209. " AD:" << flags.get(OptionInfo::AD) <<
  210. " CD:" << flags.get(OptionInfo::CD) <<
  211. " RC:" << flags.get(OptionInfo::RC);
  212. // Section count fields.
  213. inbuf.setPosition(OptionInfo::word(OptionInfo::QC));
  214. os << std::dec << std::uppercase <<
  215. " QC:" << inbuf.readUint16();
  216. inbuf.setPosition(OptionInfo::word(OptionInfo::AC));
  217. os << std::dec << std::uppercase <<
  218. " AC:" << inbuf.readUint16();
  219. inbuf.setPosition(OptionInfo::word(OptionInfo::UC));
  220. os << std::dec << std::uppercase <<
  221. " UC:" << inbuf.readUint16();
  222. inbuf.setPosition(OptionInfo::word(OptionInfo::DC));
  223. os << std::dec << std::uppercase <<
  224. " DC:" << inbuf.readUint16();
  225. // ... and message size.
  226. os << std::dec << std::uppercase <<
  227. " MS:" << msgbuf->getLength();
  228. return (os.str());
  229. }
  230. // Perform the I/O to the nameserver.
  231. void
  232. Scan::performIO(OutputBufferPtr& sendbuf, OutputBufferPtr& recvbuf,
  233. const CommandOptions& options)
  234. {
  235. // Set up an I/O service for the I/O. This needs to be initialized before
  236. // every call as the callback called when the I/O completes stops it.
  237. service_.reset(new IOService());
  238. // The object that will do the I/O
  239. IOFetch fetch(IOFetch::UDP, *service_, sendbuf,
  240. IOAddress(options.getAddress()), options.getPort(), recvbuf,
  241. this, options.getTimeout());
  242. // Execute the message exchange. The call to run() will return when a
  243. // response is received or when the I/O times out.
  244. (service_->get_io_service()).post(fetch);
  245. service_->run();
  246. }
  247. // I/O Callback. Called when the message exchange completes or times out.
  248. void
  249. Scan::operator()(IOFetch::Result result) {
  250. // Record the result. This is accessed when deciding what was returned
  251. // (if a timeout, nothing was returned).
  252. result_ = result;
  253. // Stop the I/O service. This will cause the call to run() to return.
  254. service_->stop();
  255. }
  256. } // namespace test
  257. } // namespace isc