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