udpdns.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. // $Id$
  15. #ifndef __UDPDNS_H
  16. #define __UDPDNS_H 1
  17. #include <config.h>
  18. #include <asio.hpp>
  19. #include <boost/shared_array.hpp>
  20. #include <boost/shared_ptr.hpp>
  21. #include <dns/buffer.h>
  22. #include <dns/message.h>
  23. #include <dns/messagerenderer.h>
  24. #include <asiolink/asiolink.h>
  25. #include <asiolink/internal/coroutine.h>
  26. // This file contains UDP-specific implementations of generic classes
  27. // defined in asiolink.h. It is *not* intended to be part of the public
  28. // API.
  29. namespace asiolink {
  30. // Note: this implementation is optimized for the case where this object
  31. // is created from an ASIO endpoint object in a receiving code path
  32. // by avoiding to make a copy of the base endpoint. Otherwise, when we
  33. // receive UDP packets at a high rate, the copy overhead might be significant.
  34. class UDPEndpoint : public IOEndpoint {
  35. public:
  36. UDPEndpoint(const IOAddress& address, const unsigned short port) :
  37. asio_endpoint_placeholder_(
  38. new asio::ip::udp::endpoint(asio::ip::address::from_string(address.toText()),
  39. port)),
  40. asio_endpoint_(*asio_endpoint_placeholder_)
  41. {}
  42. UDPEndpoint(const asio::ip::udp::endpoint& asio_endpoint) :
  43. asio_endpoint_placeholder_(NULL), asio_endpoint_(asio_endpoint)
  44. {}
  45. ~UDPEndpoint() { delete asio_endpoint_placeholder_; }
  46. inline IOAddress getAddress() const {
  47. return (asio_endpoint_.address());
  48. }
  49. inline uint16_t getPort() const {
  50. return (asio_endpoint_.port());
  51. }
  52. inline short getProtocol() const {
  53. return (asio_endpoint_.protocol().protocol());
  54. }
  55. inline short getFamily() const {
  56. return (asio_endpoint_.protocol().family());
  57. }
  58. // This is not part of the exosed IOEndpoint API but allows
  59. // direct access to the ASIO implementation of the endpoint
  60. inline const asio::ip::udp::endpoint& getASIOEndpoint() const {
  61. return (asio_endpoint_);
  62. }
  63. private:
  64. const asio::ip::udp::endpoint* asio_endpoint_placeholder_;
  65. const asio::ip::udp::endpoint& asio_endpoint_;
  66. };
  67. class UDPSocket : public IOSocket {
  68. private:
  69. UDPSocket(const UDPSocket& source);
  70. UDPSocket& operator=(const UDPSocket& source);
  71. public:
  72. UDPSocket(asio::ip::udp::socket& socket) : socket_(socket) {}
  73. virtual int getNative() const { return (socket_.native()); }
  74. virtual int getProtocol() const { return (IPPROTO_UDP); }
  75. private:
  76. asio::ip::udp::socket& socket_;
  77. };
  78. //
  79. // Asynchronous UDP server coroutine
  80. //
  81. class UDPServer : public virtual DNSServer, public virtual coroutine {
  82. public:
  83. explicit UDPServer(asio::io_service& io_service,
  84. const asio::ip::address& addr, const uint16_t port,
  85. SimpleCallback* checkin = NULL,
  86. DNSLookup* lookup = NULL,
  87. DNSAnswer* answer = NULL);
  88. void operator()(asio::error_code ec = asio::error_code(),
  89. size_t length = 0);
  90. void asyncLookup();
  91. void resume(const bool done);
  92. bool hasAnswer() { return (done_); }
  93. int value() { return (get_value()); }
  94. DNSServer* clone() {
  95. UDPServer* s = new UDPServer(*this);
  96. return (s);
  97. }
  98. private:
  99. enum { MAX_LENGTH = 4096 };
  100. // The ASIO service object
  101. asio::io_service& io_;
  102. // Class member variables which are dynamic, and changes to which
  103. // need to accessible from both sides of a coroutine fork or from
  104. // outside of the coroutine (i.e., from an asynchronous I/O call),
  105. // should be declared here as pointers and allocated in the
  106. // constructor or in the coroutine. This allows state information
  107. // to persist when an individual copy of the coroutine falls out
  108. // scope while waiting for an event, *so long as* there is another
  109. // object that is referencing the same data. As a side-benefit, using
  110. // pointers also reduces copy overhead for coroutine objects.
  111. //
  112. // Note: Currently these objects are allocated by "new" in the
  113. // constructor, or in the function operator while processing a query.
  114. // Repeated allocations from the heap for every incoming query is
  115. // clearly a performance issue; this must be optimized in the future.
  116. // The plan is to have a structure pre-allocate several "server state"
  117. // objects which can be pulled off a free list and placed on an in-use
  118. // list whenever a query comes in. This will serve the dual purpose
  119. // of improving performance and guaranteeing that state information
  120. // will *not* be destroyed when any one instance of the coroutine
  121. // falls out of scope while waiting for an event.
  122. //
  123. // Socket used to for listen for queries. Created in the
  124. // constructor and stored in a shared_ptr because socket objects
  125. // are not copyable.
  126. boost::shared_ptr<asio::ip::udp::socket> socket_;
  127. // The ASIO-enternal endpoint object representing the client
  128. boost::shared_ptr<asio::ip::udp::endpoint> sender_;
  129. // \c IOMessage and \c Message objects to be passed to the
  130. // DNS lookup and answer providers
  131. boost::shared_ptr<asiolink::IOMessage> io_message_;
  132. isc::dns::MessagePtr message_;
  133. // The buffer into which the response is written
  134. isc::dns::OutputBufferPtr respbuf_;
  135. // The buffer into which the query packet is written
  136. boost::shared_ptr<char> data_;
  137. // State information that is entirely internal to a given instance
  138. // of the coroutine can be declared here.
  139. size_t bytes_;
  140. bool done_;
  141. // Callback functions provided by the caller
  142. const SimpleCallback* checkin_callback_;
  143. const DNSLookup* lookup_callback_;
  144. const DNSAnswer* answer_callback_;
  145. };
  146. //
  147. // Asynchronous UDP coroutine for upstream queries
  148. //
  149. class UDPQuery : public coroutine {
  150. public:
  151. // TODO Maybe this should be more generic than just for UDPQuery?
  152. /**
  153. * \short Result of the query
  154. *
  155. * This is related only to contacting the remote server. If the answer
  156. * indicates error, it is still counted as SUCCESS here, if it comes back.
  157. */
  158. enum Result {
  159. SUCCESS,
  160. TIME_OUT,
  161. STOPPED
  162. };
  163. /// Abstract callback for the UDPQuery.
  164. class Callback {
  165. public:
  166. /// This will be called when the UDPQuery is completed
  167. virtual void operator()(Result result) = 0;
  168. };
  169. /**
  170. * \short Constructor.
  171. *
  172. * It creates the query.
  173. * @param callback will be called when we terminate. It is your task to
  174. * delete it if allocated on heap.
  175. * @param timeout in ms.
  176. */
  177. explicit UDPQuery(asio::io_service& io_service,
  178. const isc::dns::Question& q,
  179. const IOAddress& addr, uint16_t port,
  180. isc::dns::OutputBufferPtr buffer,
  181. Callback *callback, int timeout = -1);
  182. void operator()(asio::error_code ec = asio::error_code(),
  183. size_t length = 0);
  184. /// Terminate the query.
  185. void stop(Result reason = STOPPED);
  186. private:
  187. enum { MAX_LENGTH = 4096 };
  188. // The \c UDPQuery coroutine never forks, but it is copied whenever
  189. // it calls an async_*() function, so it's best to keep copy overhead
  190. // small by using pointers or references when possible. However, this
  191. // is not always possible.
  192. //
  193. // Socket used to for upstream queries. Created in the
  194. // constructor and stored in a shared_ptr because socket objects
  195. // are not copyable.
  196. boost::shared_ptr<asio::ip::udp::socket> socket_;
  197. // The remote endpoint. Instantiated in the constructor. Not
  198. // stored as a shared_ptr because copy overhead of an endpoint
  199. // object is no larger than that of a shared_ptr.
  200. asio::ip::udp::endpoint remote_;
  201. // The question being answered. Copied rather than referenced
  202. // because the object that created it is not guaranteed to persist.
  203. isc::dns::Question question_;
  204. // The output buffer supplied by the caller. The resposne frmo
  205. // the upstream server will be copied here.
  206. isc::dns::OutputBufferPtr buffer_;
  207. // These are allocated for each new query and are stored as
  208. // shared pointers to minimize copy overhead.
  209. isc::dns::OutputBufferPtr msgbuf_;
  210. boost::shared_array<char> data_;
  211. // This will be called when we are done.
  212. Callback *callback_;
  213. };
  214. }
  215. #endif // __UDPDNS_H
  216. // Local Variables:
  217. // mode: c++
  218. // End: