sync_udp_server.cc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 <cassert>
  24. #include <sys/types.h>
  25. #include <netinet/in.h>
  26. #include <sys/socket.h>
  27. #include <unistd.h> // for some IPC/network system calls
  28. #include <errno.h>
  29. using namespace std;
  30. using namespace isc::asiolink;
  31. namespace isc {
  32. namespace asiodns {
  33. SyncUDPServerPtr
  34. SyncUDPServer::create(asio::io_service& io_service, const int fd,
  35. const int af, DNSLookup* lookup)
  36. {
  37. return (SyncUDPServerPtr(new SyncUDPServer(io_service, fd, af, lookup)));
  38. }
  39. SyncUDPServer::SyncUDPServer(asio::io_service& io_service, const int fd,
  40. const int af, DNSLookup* lookup) :
  41. output_buffer_(new isc::util::OutputBuffer(0)),
  42. query_(new isc::dns::Message(isc::dns::Message::PARSE)),
  43. udp_endpoint_(sender_), lookup_callback_(lookup),
  44. resume_called_(false), done_(false), stopped_(false)
  45. {
  46. if (af != AF_INET && af != AF_INET6) {
  47. isc_throw(InvalidParameter, "Address family must be either AF_INET "
  48. "or AF_INET6, not " << af);
  49. }
  50. if (!lookup) {
  51. isc_throw(InvalidParameter, "null lookup callback given to "
  52. "SyncUDPServer");
  53. }
  54. LOG_DEBUG(logger, DBGLVL_TRACE_BASIC, ASIODNS_FD_ADD_UDP).arg(fd);
  55. try {
  56. socket_.reset(new asio::ip::udp::socket(io_service));
  57. socket_->assign(af == AF_INET6 ? asio::ip::udp::v6() :
  58. asio::ip::udp::v4(), fd);
  59. } catch (const std::exception& exception) {
  60. // Whatever the thing throws, it is something from ASIO and we
  61. // convert it
  62. isc_throw(IOError, exception.what());
  63. }
  64. udp_socket_.reset(new UDPSocket<DummyIOCallback>(*socket_));
  65. }
  66. void
  67. SyncUDPServer::scheduleRead() {
  68. socket_->async_receive_from(
  69. asio::mutable_buffers_1(data_, MAX_LENGTH), sender_,
  70. boost::bind(&SyncUDPServer::handleRead, shared_from_this(), _1, _2));
  71. }
  72. void
  73. SyncUDPServer::handleRead(const asio::error_code& ec, const size_t length) {
  74. if (stopped_) {
  75. // stopped_ can be set to true only after the socket object is closed.
  76. // checking this would also detect premature destruction of 'this'
  77. // object.
  78. assert(socket_ && !socket_->is_open());
  79. return;
  80. }
  81. if (ec) {
  82. using namespace asio::error;
  83. const asio::error_code::value_type err_val = ec.value();
  84. // See TCPServer::operator() for details on error handling.
  85. if (err_val == operation_aborted || err_val == bad_descriptor) {
  86. return;
  87. }
  88. if (err_val != would_block && err_val != try_again &&
  89. err_val != interrupted) {
  90. LOG_ERROR(logger, ASIODNS_UDP_SYNC_RECEIVE_FAIL).arg(ec.message());
  91. }
  92. }
  93. if (ec || length == 0) {
  94. scheduleRead();
  95. return;
  96. }
  97. // OK, we have a real packet of data. Let's dig into it!
  98. // Make sure the buffers are fresh. Note that we don't touch query_
  99. // because it's supposed to be cleared in lookup_callback_. We should
  100. // eventually even remove this member variable (and remove it from
  101. // the lookup_callback_ interface, but until then, any callback
  102. // implementation should be careful that it's the responsibility of
  103. // the callback implementation. See also #2239).
  104. output_buffer_->clear();
  105. // Mark that we don't have an answer yet.
  106. done_ = false;
  107. resume_called_ = false;
  108. // Call the actual lookup
  109. const IOMessage message(data_, length, *udp_socket_, udp_endpoint_);
  110. (*lookup_callback_)(message, query_, answer_, output_buffer_, this);
  111. if (!resume_called_) {
  112. isc_throw(isc::Unexpected,
  113. "No resume called from the lookup callback");
  114. }
  115. if (done_) {
  116. // Good, there's an answer.
  117. socket_->send_to(asio::const_buffers_1(output_buffer_->getData(),
  118. output_buffer_->getLength()),
  119. sender_, 0, ec_);
  120. if (ec_) {
  121. LOG_ERROR(logger, ASIODNS_UDP_SYNC_SEND_FAIL).
  122. arg(sender_.address().to_string()).arg(ec_.message());
  123. }
  124. }
  125. // And schedule handling another socket.
  126. scheduleRead();
  127. }
  128. void
  129. SyncUDPServer::operator()(asio::error_code, size_t) {
  130. // To start the server, we just schedule reading of data when they
  131. // arrive.
  132. scheduleRead();
  133. }
  134. /// Stop the UDPServer
  135. void
  136. SyncUDPServer::stop() {
  137. /// Using close instead of cancel, because cancel
  138. /// will only cancel the asynchronized event already submitted
  139. /// to io service, the events post to io service after
  140. /// cancel still can be scheduled by io service, if
  141. /// the socket is closed, all the asynchronized event
  142. /// for it won't be scheduled by io service not matter it is
  143. /// submit to io service before or after close call. And we will
  144. /// get bad_descriptor error.
  145. socket_->close(ec_);
  146. stopped_ = true;
  147. if (ec_) {
  148. LOG_ERROR(logger, ASIODNS_SYNC_UDP_CLOSE_FAIL).arg(ec_.message());
  149. }
  150. }
  151. void
  152. SyncUDPServer::resume(const bool done) {
  153. resume_called_ = true;
  154. done_ = done;
  155. }
  156. bool
  157. SyncUDPServer::hasAnswer() {
  158. return (done_);
  159. }
  160. } // namespace asiodns
  161. } // namespace isc