scan.cc 10 KB

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