tcpdns.cc 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // Copyright (C) 2010 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. // $Id$
  15. #include <config.h>
  16. #include <unistd.h> // for some IPC/network system calls
  17. #include <sys/socket.h>
  18. #include <netinet/in.h>
  19. #include <asio.hpp>
  20. #include <asio/ip/address.hpp>
  21. #include <boost/array.hpp>
  22. #include <boost/shared_ptr.hpp>
  23. #include <dns/buffer.h>
  24. #include <dns/message.h>
  25. #include <asiolink.h>
  26. #include <internal/coroutine.h>
  27. #include <internal/tcpdns.h>
  28. using namespace asio;
  29. using asio::ip::udp;
  30. using asio::ip::tcp;
  31. using namespace std;
  32. using namespace isc::dns;
  33. namespace asiolink {
  34. /// The following functions implement the \c UDPServer class.
  35. ///
  36. /// The constructor
  37. TCPServer::TCPServer(io_service& io_service,
  38. const ip::address& addr, const uint16_t port,
  39. const SimpleCallback* checkin,
  40. const DNSLookup* lookup,
  41. const DNSAnswer* answer) :
  42. io_(io_service), done_(false),
  43. checkin_callback_(checkin), lookup_callback_(lookup),
  44. answer_callback_(answer)
  45. {
  46. tcp::endpoint endpoint(addr, port);
  47. acceptor_.reset(new tcp::acceptor(io_service));
  48. acceptor_->open(endpoint.protocol());
  49. // Set v6-only (we use a separate instantiation for v4,
  50. // otherwise asio will bind to both v4 and v6
  51. if (addr.is_v6()) {
  52. acceptor_->set_option(ip::v6_only(true));
  53. }
  54. acceptor_->set_option(tcp::acceptor::reuse_address(true));
  55. acceptor_->bind(endpoint);
  56. acceptor_->listen();
  57. }
  58. void
  59. TCPServer::operator()(error_code ec, size_t length) {
  60. /// Because the coroutine reeentry block is implemented as
  61. /// a switch statement, inline variable declarations are not
  62. /// permitted. Certain variables used below can be declared here.
  63. boost::array<const_buffer,2> bufs;
  64. OutputBuffer* lenbuf = NULL;
  65. CORO_REENTER (this) {
  66. do {
  67. /// Create a socket to listen for connections
  68. socket_.reset(new tcp::socket(acceptor_->get_io_service()));
  69. /// Wait for new connections. In the event of error,
  70. /// try again
  71. do {
  72. CORO_YIELD acceptor_->async_accept(*socket_, *this);
  73. } while (!ec);
  74. /// Fork the coroutine by creating a copy of this one and
  75. /// scheduling it on the ASIO service queue. The parent
  76. /// will continue listening for DNS connections while the
  77. /// handles the one that has just arrived.
  78. CORO_FORK io_.post(TCPServer(*this));
  79. } while (is_parent());
  80. /// Instantiate the data buffer that will be used by the
  81. /// asynchronous read call.
  82. data_.reset(new char[MAX_LENGTH]);
  83. /// Read the message, in two parts. First, the message length:
  84. CORO_YIELD async_read(*socket_, asio::buffer(data_.get(),
  85. TCP_MESSAGE_LENGTHSIZE), *this);
  86. if (ec) {
  87. CORO_YIELD return;
  88. }
  89. /// Now read the message itself. (This is done in a different scope
  90. /// to allow inline variable declarations.)
  91. CORO_YIELD {
  92. InputBuffer dnsbuffer((const void *) data_.get(), length);
  93. uint16_t msglen = dnsbuffer.readUint16();
  94. async_read(*socket_, asio::buffer(data_.get(), msglen), *this);
  95. }
  96. if (ec) {
  97. CORO_YIELD return;
  98. }
  99. // Create an \c IOMessage object to store the query.
  100. //
  101. // (XXX: It would be good to write a factory function
  102. // that would quickly generate an IOMessage object without
  103. // all these calls to "new".)
  104. peer_.reset(new TCPEndpoint(socket_->remote_endpoint()));
  105. iosock_.reset(new TCPSocket(*socket_));
  106. io_message_.reset(new IOMessage(data_.get(), length, *iosock_, *peer_));
  107. bytes_ = length;
  108. // Perform any necessary operations prior to processing the incoming
  109. // packet (e.g., checking for queued configuration messages).
  110. //
  111. // (XXX: it may be a performance issue to have this called for
  112. // every single incoming packet; we may wish to throttle it somehow
  113. // in the future.)
  114. if (checkin_callback_ != NULL) {
  115. (*checkin_callback_)(*io_message_);
  116. }
  117. // If we don't have a DNS Lookup provider, there's no point in
  118. // continuing; we exit the coroutine permanently.
  119. if (lookup_callback_ == NULL) {
  120. CORO_YIELD return;
  121. }
  122. // Reset or instantiate objects that will be needed by the
  123. // DNS lookup and the write call.
  124. respbuf_.reset(new OutputBuffer(0));
  125. message_.reset(new Message(Message::PARSE));
  126. // Schedule a DNS lookup, and yield. When the lookup is
  127. // finished, the coroutine will resume immediately after
  128. // this point.
  129. CORO_YIELD io_.post(AsyncLookup<TCPServer>(*this));
  130. // The 'done_' flag indicates whether we have an answer
  131. // to send back. If not, exit the coroutine permanently.
  132. if (!done_) {
  133. CORO_YIELD return;
  134. }
  135. // Call the DNS answer provider to render the answer into
  136. // wire format
  137. (*answer_callback_)(*io_message_, message_, respbuf_);
  138. // Set up the response, beginning with two length bytes.
  139. lenbuf = new OutputBuffer(TCP_MESSAGE_LENGTHSIZE);
  140. lenbuf->writeUint16(respbuf_->getLength());
  141. bufs[0] = buffer(lenbuf->getData(), lenbuf->getLength());
  142. bufs[1] = buffer(respbuf_->getData(), respbuf_->getLength());
  143. // Begin an asynchronous send, and then yield. When the
  144. // send completes, we will resume immediately after this point
  145. // (though we have nothing further to do, so the coroutine
  146. // will simply exit at that time).
  147. CORO_YIELD async_write(*socket_, bufs, *this);
  148. }
  149. delete lenbuf;
  150. }
  151. /// Call the DNS lookup provider. (Expected to be called by the
  152. /// AsyncLookup<TCPServer> handler.)
  153. void
  154. TCPServer::asyncLookup() {
  155. (*lookup_callback_)(*io_message_, message_, respbuf_, this);
  156. }
  157. /// Post this coroutine on the ASIO service queue so that it will
  158. /// resume processing where it left off. The 'done' parameter indicates
  159. /// whether there is an answer to return to the client.
  160. void
  161. TCPServer::resume(const bool done) {
  162. done_ = done;
  163. io_.post(*this);
  164. }
  165. }