scan.cc 10 KB

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