sync_udp_server.h 7.3 KB

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