sync_udp_server.cc 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // Copyright (C) 2012 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 <config.h>
  15. #include <asio.hpp>
  16. #include <asio/error.hpp>
  17. #include "sync_udp_server.h"
  18. #include "logger.h"
  19. #include <asiolink/dummy_io_cb.h>
  20. #include <asiolink/udp_endpoint.h>
  21. #include <asiolink/udp_socket.h>
  22. #include <boost/bind.hpp>
  23. #include <netinet/in.h>
  24. #include <sys/socket.h>
  25. #include <unistd.h> // for some IPC/network system calls
  26. #include <errno.h>
  27. using namespace std;
  28. using namespace boost;
  29. using namespace isc::asiolink;
  30. namespace isc {
  31. namespace asiodns {
  32. SyncUDPServer::SyncUDPServer(asio::io_service& io_service,
  33. const asio::ip::address& addr,
  34. const uint16_t port,
  35. asiolink::SimpleCallback* checkin,
  36. DNSLookup* lookup, DNSAnswer* answer) :
  37. output_buffer_(new isc::util::OutputBuffer(0)),
  38. query_(new isc::dns::Message(isc::dns::Message::PARSE)),
  39. answer_(new isc::dns::Message(isc::dns::Message::RENDER)),
  40. io_(io_service), checkin_callback_(checkin), lookup_callback_(lookup),
  41. answer_callback_(answer), stopped_(false)
  42. {
  43. // We must use different instantiations for v4 and v6;
  44. // otherwise ASIO will bind to both
  45. asio::ip::udp proto = addr.is_v4() ? asio::ip::udp::v4() :
  46. asio::ip::udp::v6();
  47. socket_.reset(new asio::ip::udp::socket(io_service, proto));
  48. socket_->set_option(asio::socket_base::reuse_address(true));
  49. if (addr.is_v6()) {
  50. socket_->set_option(asio::ip::v6_only(true));
  51. }
  52. socket_->bind(asio::ip::udp::endpoint(addr, port));
  53. }
  54. SyncUDPServer::SyncUDPServer(asio::io_service& io_service, const int fd,
  55. const int af, asiolink::SimpleCallback* checkin,
  56. DNSLookup* lookup, DNSAnswer* answer) :
  57. output_buffer_(new isc::util::OutputBuffer(0)),
  58. query_(new isc::dns::Message(isc::dns::Message::PARSE)),
  59. answer_(new isc::dns::Message(isc::dns::Message::RENDER)),
  60. io_(io_service), checkin_callback_(checkin), lookup_callback_(lookup),
  61. answer_callback_(answer), stopped_(false)
  62. {
  63. if (af != AF_INET && af != AF_INET6) {
  64. isc_throw(InvalidParameter, "Address family must be either AF_INET "
  65. "or AF_INET6, not " << af);
  66. }
  67. LOG_DEBUG(logger, DBGLVL_TRACE_BASIC, ASIODNS_FD_ADD_UDP).arg(fd);
  68. try {
  69. socket_.reset(new asio::ip::udp::socket(io_service));
  70. socket_->assign(af == AF_INET6 ? asio::ip::udp::v6() :
  71. asio::ip::udp::v4(), fd);
  72. } catch (const std::exception& exception) {
  73. // Whatever the thing throws, it is something from ASIO and we
  74. // convert it
  75. isc_throw(IOError, exception.what());
  76. }
  77. }
  78. void
  79. SyncUDPServer::scheduleRead() {
  80. socket_->async_receive_from(asio::buffer(data_, MAX_LENGTH), sender_,
  81. boost::bind(&SyncUDPServer::handleRead, this,
  82. _1, _2));
  83. }
  84. void
  85. SyncUDPServer::handleRead(const asio::error_code& ec, const size_t length) {
  86. // Abort on fatal errors
  87. if (ec) {
  88. using namespace asio::error;
  89. if (ec.value() != would_block && ec.value() != try_again &&
  90. ec.value() != interrupted) {
  91. return;
  92. }
  93. }
  94. // Some kind of interrupt, spurious wakeup, or like that. Just try reading
  95. // again.
  96. if (ec || length == 0) {
  97. scheduleRead();
  98. return;
  99. }
  100. // OK, we have a real packet of data. Let's dig into it!
  101. // XXX: This is taken (and ported) from UDPSocket class. What the hell does
  102. // it really mean?
  103. // The UDP socket class has been extended with asynchronous functions
  104. // and takes as a template parameter a completion callback class. As
  105. // UDPServer does not use these extended functions (only those defined
  106. // in the IOSocket base class) - but needs a UDPSocket to get hold of
  107. // the underlying Boost UDP socket - DummyIOCallback is used. This
  108. // provides the appropriate operator() but is otherwise functionless.
  109. UDPSocket<DummyIOCallback> socket(*socket_);
  110. UDPEndpoint endpoint(sender_);
  111. IOMessage message(data_, length, socket, endpoint);
  112. if (checkin_callback_ != NULL) {
  113. (*checkin_callback_)(message);
  114. if (stopped_) {
  115. return;
  116. }
  117. }
  118. // If we don't have a DNS Lookup provider, there's no point in
  119. // continuing; we exit the coroutine permanently.
  120. if (lookup_callback_ == NULL) {
  121. scheduleRead();
  122. return;
  123. }
  124. // Make sure the buffers are fresh
  125. output_buffer_->clear();
  126. query_->clear(isc::dns::Message::PARSE);
  127. answer_->clear(isc::dns::Message::RENDER);
  128. // Mark that we don't have an answer yet.
  129. done_ = false;
  130. resume_called_ = false;
  131. // Call the actual lookup
  132. (*lookup_callback_)(message, query_, answer_, output_buffer_, this);
  133. if (!resume_called_) {
  134. isc_throw(isc::Unexpected,
  135. "No resume called from the lookup callback");
  136. }
  137. if (stopped_) {
  138. return;
  139. }
  140. if (done_) {
  141. // Good, there's an answer.
  142. // Call the answer callback to render it.
  143. (*answer_callback_)(message, query_, answer_, output_buffer_);
  144. if (stopped_) {
  145. return;
  146. }
  147. socket_->send_to(asio::buffer(output_buffer_->getData(),
  148. output_buffer_->getLength()),
  149. sender_);
  150. }
  151. // And schedule handling another socket.
  152. scheduleRead();
  153. }
  154. void
  155. SyncUDPServer::operator()(asio::error_code, size_t) {
  156. // To start the server, we just schedule reading of data when they
  157. // arrive.
  158. scheduleRead();
  159. }
  160. /// Stop the UDPServer
  161. void
  162. SyncUDPServer::stop() {
  163. /// Using close instead of cancel, because cancel
  164. /// will only cancel the asynchornized event already submitted
  165. /// to io service, the events post to io service after
  166. /// cancel still can be scheduled by io service, if
  167. /// the socket is cloesed, all the asynchronized event
  168. /// for it won't be scheduled by io service not matter it is
  169. /// submit to io serice before or after close call. And we will
  170. //. get bad_descriptor error
  171. socket_->close();
  172. stopped_ = true;
  173. }
  174. /// Post this coroutine on the ASIO service queue so that it will
  175. /// resume processing where it left off. The 'done' parameter indicates
  176. /// whether there is an answer to return to the client.
  177. void
  178. SyncUDPServer::resume(const bool done) {
  179. resume_called_ = true;
  180. done_ = done;
  181. }
  182. bool
  183. SyncUDPServer::hasAnswer() {
  184. return (done_);
  185. }
  186. } // namespace asiodns
  187. } // namespace isc