scan.cc 10.0 KB

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