sync_udp_server.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. #ifndef SYNC_UDP_SERVER_H
  15. #define SYNC_UDP_SERVER_H 1
  16. #ifndef ASIO_HPP
  17. #error "asio.hpp must be included before including this, see asiolink.h as to why"
  18. #endif
  19. #include "dns_answer.h"
  20. #include "dns_lookup.h"
  21. #include "dns_server.h"
  22. #include <dns/message.h>
  23. #include <asiolink/simple_callback.h>
  24. #include <asiolink/dummy_io_cb.h>
  25. #include <asiolink/udp_socket.h>
  26. #include <util/buffer.h>
  27. #include <exceptions/exceptions.h>
  28. #include <boost/function.hpp>
  29. #include <boost/noncopyable.hpp>
  30. #include <boost/scoped_ptr.hpp>
  31. #include <stdint.h>
  32. namespace isc {
  33. namespace asiodns {
  34. /// \brief An UDP server that doesn't asynchronous lookup handlers.
  35. ///
  36. /// That means, the lookup handler must provide the answer right away.
  37. /// This allows for implementation with less overhead, compared with
  38. /// the UDPClass.
  39. class SyncUDPServer : public DNSServer, public boost::noncopyable {
  40. public:
  41. /// \brief Constructor
  42. ///
  43. /// Due to the nature of this server, it's meaningless if the lookup
  44. /// callback is NULL. So the constructor explicitly rejects that case
  45. /// with an exception. Likewise, it doesn't take "checkin" or "answer"
  46. /// callbacks. In fact, calling "checkin" from receive callback does not
  47. /// make sense for any of the DNSServer variants; "answer" callback is
  48. /// simply unnecessary for this class because a complete answer is build
  49. /// in the lookup callback (it's the user's responsibility to guarantee
  50. /// that condition).
  51. ///
  52. /// \param io_service the asio::io_service to work with
  53. /// \param fd the file descriptor of opened UDP socket
  54. /// \param af address family, either AF_INET or AF_INET6
  55. /// \param lookup the callbackprovider for DNS lookup events (must not be
  56. /// NULL)
  57. ///
  58. /// \throw isc::InvalidParameter if af is neither AF_INET nor AF_INET6
  59. /// \throw isc::InvalidParameter lookup is NULL
  60. /// \throw isc::asiolink::IOError when a low-level error happens, like the
  61. /// fd is not a valid descriptor.
  62. SyncUDPServer(asio::io_service& io_service, const int fd, const int af,
  63. DNSLookup* lookup);
  64. /// \brief Start the SyncUDPServer.
  65. ///
  66. /// This is the function operator to keep interface with other server
  67. /// classes. They need that because they're coroutines.
  68. virtual void operator()(asio::error_code ec = asio::error_code(),
  69. size_t length = 0);
  70. /// \brief Calls the lookup callback
  71. virtual void asyncLookup() {
  72. isc_throw(Unexpected,
  73. "SyncUDPServer doesn't support asyncLookup by design, use "
  74. "UDPServer if you need it.");
  75. }
  76. /// \brief Stop the running server
  77. /// \note once the server stopped, it can't restart
  78. virtual void stop();
  79. /// \brief Resume operation
  80. ///
  81. /// Note that unlike other servers, this one expects it to be called
  82. /// directly from the lookup callback. If it isn't, the server will
  83. /// throw an Unexpected exception (probably to the event loop, which
  84. /// would usually lead to termination of the program, but that's OK,
  85. /// as it would be serious programmer error).
  86. ///
  87. /// \param done Set this to true if the lookup action is done and
  88. /// we have an answer
  89. virtual void resume(const bool done);
  90. /// \brief Check if we have an answer
  91. ///
  92. /// \return true if we have an answer
  93. virtual bool hasAnswer();
  94. /// \brief Clones the object
  95. ///
  96. /// Since cloning is for the use of coroutines, the synchronous UDP server
  97. /// does not need to be cloned. Therefore supporting it would be needless
  98. /// work, and trying to clone it would be a programmer error anyway, this
  99. /// throws Unexpected.
  100. ///
  101. /// \return a newly allocated copy of this object
  102. virtual DNSServer* clone() {
  103. isc_throw(Unexpected, "SyncUDPServer can't be cloned.");
  104. }
  105. private:
  106. // Internal state & buffers. We don't use the PIMPL idiom, as this class
  107. // isn't usually used directly anyway.
  108. // Maximum size of incoming UDP packet
  109. static const size_t MAX_LENGTH = 4096;
  110. // Buffer for incoming data
  111. uint8_t data_[MAX_LENGTH];
  112. // The buffer to render the output to and send it.
  113. // If it was OK to have just a buffer, not the wrapper class,
  114. // we could reuse the data_
  115. isc::util::OutputBufferPtr output_buffer_;
  116. // Objects to hold the query message and the answer: these are not used
  117. isc::dns::MessagePtr query_, answer_;
  118. // The socket used for the communication
  119. std::auto_ptr<asio::ip::udp::socket> socket_;
  120. // Wrapper of socket_ in the form of asiolink::IOSocket.
  121. // "DummyIOCallback" is not necessary for this class, but using the
  122. // template is the easiest way to create a UDP instance of IOSocket.
  123. boost::scoped_ptr<asiolink::UDPSocket<asiolink::DummyIOCallback> >
  124. udp_socket_;
  125. // Place the socket puts the sender of a packet when it is received
  126. asio::ip::udp::endpoint sender_;
  127. // Wrapper of sender_ in the form of asiolink::IOEndpoint. It's set to
  128. // refer to sender_ on initialization, and keeps the reference throughout
  129. // this server class.
  130. asiolink::UDPEndpoint udp_endpoint_;
  131. // Callback
  132. const DNSLookup* lookup_callback_;
  133. // Answers from the lookup callback (not sent directly, but signalled
  134. // through resume()
  135. bool resume_called_, done_;
  136. // This turns true when the server stops. Allows for not sending the
  137. // answer after we closed the socket.
  138. bool stopped_;
  139. // Placeholder for error code object. It will be passed to ASIO library
  140. // to have it set in case of error.
  141. asio::error_code ec_;
  142. // The callback functor for internal asynchronous read event. This is
  143. // stateless (and it will be copied in the ASIO library anyway), so
  144. // can be const
  145. const boost::function<void(const asio::error_code&, size_t)>
  146. recv_callback_;
  147. // Auxiliary functions
  148. // Schedule next read on the socket. Just a wrapper around
  149. // socket_->async_read_from with the correct parameters.
  150. void scheduleRead();
  151. // Callback from the socket's read call (called when there's an error or
  152. // when a new packet comes).
  153. void handleRead(const asio::error_code& ec, const size_t length);
  154. };
  155. } // namespace asiodns
  156. } // namespace isc
  157. #endif // SYNC_UDP_SERVER_H
  158. // Local Variables:
  159. // mode: c++
  160. // End: