sync_udp_server.cc 5.6 KB

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