udpdns.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. // Copyright (C) 2010 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 __UDPDNS_H
  15. #define __UDPDNS_H 1
  16. #include <config.h>
  17. #include <asio.hpp>
  18. #include <boost/shared_array.hpp>
  19. #include <boost/shared_ptr.hpp>
  20. #include <dns/buffer.h>
  21. #include <dns/message.h>
  22. #include <dns/messagerenderer.h>
  23. #include <asiolink/asiolink.h>
  24. #include <asiolink/internal/coroutine.h>
  25. // This file contains UDP-specific implementations of generic classes
  26. // defined in asiolink.h. It is *not* intended to be part of the public
  27. // API.
  28. namespace asiolink {
  29. /// \brief The \c UDPEndpoint class is a concrete derived class of
  30. /// \c IOEndpoint that represents an endpoint of a UDP packet.
  31. ///
  32. /// Other notes about \c TCPEndpoint applies to this class, too.
  33. class UDPEndpoint : public IOEndpoint {
  34. public:
  35. ///
  36. /// \name Constructors and Destructor.
  37. ///
  38. //@{
  39. /// \brief Constructor from a pair of address and port.
  40. ///
  41. /// \param address The IP address of the endpoint.
  42. /// \param port The UDP port number of the endpoint.
  43. UDPEndpoint(const IOAddress& address, const unsigned short port) :
  44. asio_endpoint_placeholder_(
  45. new asio::ip::udp::endpoint(asio::ip::address::from_string(address.toText()),
  46. port)),
  47. asio_endpoint_(*asio_endpoint_placeholder_)
  48. {}
  49. /// \brief Constructor from an ASIO UDP endpoint.
  50. ///
  51. /// This constructor is designed to be an efficient wrapper for the
  52. /// corresponding ASIO class, \c udp::endpoint.
  53. ///
  54. /// \param asio_endpoint The ASIO representation of the UDP endpoint.
  55. UDPEndpoint(const asio::ip::udp::endpoint& asio_endpoint) :
  56. asio_endpoint_placeholder_(NULL), asio_endpoint_(asio_endpoint)
  57. {}
  58. /// \brief The destructor.
  59. ~UDPEndpoint() { delete asio_endpoint_placeholder_; }
  60. //@}
  61. inline IOAddress getAddress() const {
  62. return (asio_endpoint_.address());
  63. }
  64. inline uint16_t getPort() const {
  65. return (asio_endpoint_.port());
  66. }
  67. inline short getProtocol() const {
  68. return (asio_endpoint_.protocol().protocol());
  69. }
  70. inline short getFamily() const {
  71. return (asio_endpoint_.protocol().family());
  72. }
  73. // This is not part of the exosed IOEndpoint API but allows
  74. // direct access to the ASIO implementation of the endpoint
  75. inline const asio::ip::udp::endpoint& getASIOEndpoint() const {
  76. return (asio_endpoint_);
  77. }
  78. private:
  79. const asio::ip::udp::endpoint* asio_endpoint_placeholder_;
  80. const asio::ip::udp::endpoint& asio_endpoint_;
  81. };
  82. /// \brief The \c UDPSocket class is a concrete derived class of
  83. /// \c IOSocket that represents a UDP socket.
  84. ///
  85. /// Other notes about \c TCPSocket applies to this class, too.
  86. class UDPSocket : public IOSocket {
  87. private:
  88. UDPSocket(const UDPSocket& source);
  89. UDPSocket& operator=(const UDPSocket& source);
  90. public:
  91. /// \brief Constructor from an ASIO UDP socket.
  92. ///
  93. /// \param socket The ASIO representation of the UDP socket.
  94. UDPSocket(asio::ip::udp::socket& socket) : socket_(socket) {}
  95. virtual int getNative() const { return (socket_.native()); }
  96. virtual int getProtocol() const { return (IPPROTO_UDP); }
  97. private:
  98. asio::ip::udp::socket& socket_;
  99. };
  100. //
  101. // Asynchronous UDP server coroutine
  102. //
  103. ///
  104. /// \brief This class implements the coroutine to handle UDP
  105. /// DNS query event. As such, it is both a \c DNSServer and
  106. /// a \c coroutine
  107. ///
  108. class UDPServer : public virtual DNSServer, public virtual coroutine {
  109. public:
  110. /// \brief Constructor
  111. /// \param io_service the asio::io_service to work with
  112. /// \param addr the IP address to listen for queries on
  113. /// \param port the port to listen for queries on
  114. /// \param checkin the callbackprovider for non-DNS events
  115. /// \param lookup the callbackprovider for DNS lookup events
  116. /// \param answer the callbackprovider for DNS answer events
  117. explicit UDPServer(asio::io_service& io_service,
  118. const asio::ip::address& addr, const uint16_t port,
  119. SimpleCallback* checkin = NULL,
  120. DNSLookup* lookup = NULL,
  121. DNSAnswer* answer = NULL);
  122. /// \brief The function operator
  123. void operator()(asio::error_code ec = asio::error_code(),
  124. size_t length = 0);
  125. /// \brief Calls the lookup callback
  126. void asyncLookup();
  127. /// \brief Resume operation
  128. ///
  129. /// \param done Set this to true if the lookup action is done and
  130. /// we have an answer
  131. void resume(const bool done);
  132. /// \brief Check if we have an answer
  133. ///
  134. /// \return true if we have an answer
  135. bool hasAnswer();
  136. /// \brief Returns the coroutine state value
  137. ///
  138. /// \return the coroutine state value
  139. int value() { return (get_value()); }
  140. /// \brief Clones the object
  141. ///
  142. /// \return a newly allocated copy of this object
  143. DNSServer* clone() {
  144. UDPServer* s = new UDPServer(*this);
  145. return (s);
  146. }
  147. private:
  148. enum { MAX_LENGTH = 4096 };
  149. /**
  150. * \brief Internal state and data.
  151. *
  152. * We use the pimple design pattern, but not because we need to hide
  153. * internal data. This class and whole header is for private use anyway.
  154. * It turned out that UDPServer is copied a lot, because it is a coroutine.
  155. * This way the overhead of copying is lower, we copy only one shared
  156. * pointer instead of about 10 of them.
  157. */
  158. class Data;
  159. boost::shared_ptr<Data> data_;
  160. };
  161. //
  162. // Asynchronous UDP coroutine for upstream queries
  163. //
  164. class UDPQuery : public coroutine {
  165. public:
  166. // TODO Maybe this should be more generic than just for UDPQuery?
  167. ///
  168. /// \brief Result of the query
  169. ///
  170. /// This is related only to contacting the remote server. If the answer
  171. ///indicates error, it is still counted as SUCCESS here, if it comes back.
  172. ///
  173. enum Result {
  174. SUCCESS,
  175. TIME_OUT,
  176. STOPPED
  177. };
  178. /// Abstract callback for the UDPQuery.
  179. class Callback {
  180. public:
  181. virtual ~Callback() {}
  182. /// This will be called when the UDPQuery is completed
  183. virtual void operator()(Result result) = 0;
  184. };
  185. ///
  186. /// \brief Constructor.
  187. ///
  188. /// It creates the query.
  189. /// @param callback will be called when we terminate. It is your task to
  190. /// delete it if allocated on heap.
  191. ///@param timeout in ms.
  192. ///
  193. explicit UDPQuery(asio::io_service& io_service,
  194. const isc::dns::Question& q,
  195. const IOAddress& addr, uint16_t port,
  196. isc::dns::OutputBufferPtr buffer,
  197. Callback* callback, int timeout = -1);
  198. void operator()(asio::error_code ec = asio::error_code(),
  199. size_t length = 0);
  200. /// Terminate the query.
  201. void stop(Result reason = STOPPED);
  202. private:
  203. enum { MAX_LENGTH = 4096 };
  204. ///
  205. /// \short Private data
  206. ///
  207. /// They are not private because of stability of the
  208. /// interface (this is private class anyway), but because this class
  209. /// will be copyed often (it is used as a coroutine and passed as callback
  210. /// to many async_*() functions) and we want keep the same data. Some of
  211. /// the data is not copyable too.
  212. ///
  213. struct PrivateData;
  214. boost::shared_ptr<PrivateData> data_;
  215. };
  216. }
  217. #endif // __UDPDNS_H
  218. // Local Variables:
  219. // mode: c++
  220. // End: