scan.cc 10 KB

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