dns_service.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. // Copyright (C) 2011 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 ASIOLINK_DNS_SERVICE_H
  15. #define ASIOLINK_DNS_SERVICE_H 1
  16. #include <resolve/resolver_interface.h>
  17. #include <asiolink/io_service.h>
  18. #include <asiolink/simple_callback.h>
  19. namespace isc {
  20. namespace asiodns {
  21. class DNSLookup;
  22. class DNSAnswer;
  23. class DNSServiceImpl;
  24. /// \brief A base class for common \c DNSService interfaces.
  25. ///
  26. /// This class is defined mainly for test code so it can use a faked/mock
  27. /// version of a derived class and test scenarios that would involve
  28. /// \c DNSService without actually instantiating the real service class.
  29. ///
  30. /// It doesn't intend to be a customization for other purposes - we generally
  31. /// expect non test code only use \c DNSService directly.
  32. /// For this reason most of the detailed description are given in the
  33. /// \c DNSService class. See that for further details of specific methods
  34. /// and class behaviors.
  35. class DNSServiceBase {
  36. protected:
  37. /// \brief Default constructor.
  38. ///
  39. /// This is protected so this class couldn't be accidentally instantiated
  40. /// directly, even if there were no pure virtual functions.
  41. DNSServiceBase() {}
  42. public:
  43. /// \brief Flags for optional server properties.
  44. ///
  45. /// The values of this enumerable type are intended to be used to specify
  46. /// a particular property of the server created via the \c addServer
  47. /// variants. As we see need for more such properties, a compound
  48. /// form of flags (i.e., a single value generated by bitwise OR'ed
  49. /// multiple flag values) will be allowed.
  50. ///
  51. /// Note: the description is given here because it's used in the method
  52. /// signature. It essentially belongs to the derived \c DNSService
  53. /// class.
  54. enum ServerFlag {
  55. SERVER_DEFAULT = 0, ///< The default flag (no particular property)
  56. SERVER_SYNC_OK = 1 ///< The server can act in the "synchronous" mode.
  57. ///< In this mode, the client ensures that the
  58. ///< lookup provider always completes the query
  59. ///< process and it immediately releases the
  60. ///< ownership of the given buffer. This allows
  61. ///< the server implementation to introduce some
  62. ///< optimization such as omitting unnecessary
  63. ///< operation or reusing internal resources.
  64. ///< Note that in functionality the non
  65. ///< "synchronous" mode is compatible with the
  66. ///< synchronous mode; it's up to the server
  67. ///< implementation whether it exploits the
  68. ///< information given by the client.
  69. };
  70. public:
  71. /// \brief The destructor.
  72. virtual ~DNSServiceBase() {}
  73. virtual void addServerTCPFromFD(int fd, int af) = 0;
  74. virtual void addServerUDPFromFD(int fd, int af,
  75. ServerFlag options = SERVER_DEFAULT) = 0;
  76. virtual void clearServers() = 0;
  77. /// \brief Set the timeout for TCP DNS services
  78. ///
  79. /// The timeout is used for incoming TCP connections, so
  80. /// that the connection is dropped if not all query data
  81. /// is read.
  82. ///
  83. /// For existing DNSServer objects, where the timeout is
  84. /// relevant (i.e. TCPServer instances), the timeout value
  85. /// is updated.
  86. /// The given value is also kept to use for DNSServer instances
  87. /// which are created later
  88. ///
  89. /// \param timeout The timeout in milliseconds
  90. virtual void setTCPRecvTimeout(size_t timeout) = 0;
  91. virtual asiolink::IOService& getIOService() = 0;
  92. };
  93. /// \brief Handle DNS Queries
  94. ///
  95. /// DNSService is the service that handles DNS queries and answers with
  96. /// a given IOService. This class is mainly intended to hold all the
  97. /// logic that is shared between the authoritative and the recursive
  98. /// server implementations. As such, it handles asio and listening
  99. /// sockets.
  100. class DNSService : public DNSServiceBase {
  101. ///
  102. /// \name Constructors and Destructor
  103. ///
  104. /// Note: The copy constructor and the assignment operator are
  105. /// intentionally defined as private, making this class non-copyable.
  106. //@{
  107. private:
  108. DNSService(const DNSService& source);
  109. DNSService& operator=(const DNSService& source);
  110. private:
  111. // Bit or'ed all defined \c ServerFlag values. Used internally for
  112. // compatibility check. Note that this doesn't have to be used by
  113. // applications, and doesn't have to be defined in the "base" class.
  114. static const unsigned int SERVER_DEFINED_FLAGS = 1;
  115. public:
  116. /// \brief The constructor without any servers.
  117. ///
  118. /// Use addServerTCPFromFD() or addServerUDPFromFD() to add some servers.
  119. ///
  120. /// \param io_service The IOService to work with
  121. /// \param lookup The lookup provider (see \c DNSLookup)
  122. /// \param answer The answer provider (see \c DNSAnswer)
  123. DNSService(asiolink::IOService& io_service,
  124. DNSLookup* lookup, DNSAnswer* answer);
  125. /// \brief The destructor.
  126. virtual ~DNSService();
  127. //@}
  128. /// \brief Add another TCP server/listener to the service from already
  129. /// opened file descriptor
  130. ///
  131. /// Adds a new TCP server using an already opened file descriptor (eg. it
  132. /// only wraps it so the file descriptor is usable within the event loop).
  133. /// The file descriptor must be associated with a TCP socket of the given
  134. /// address family that is bound to an appropriate port (and possibly a
  135. /// specific address) and is ready for listening to new connection
  136. /// requests but has not actually started listening.
  137. ///
  138. /// At the moment, TCP servers don't support any optional properties;
  139. /// so unlike the UDP version of the method it doesn't have an \c options
  140. /// argument.
  141. ///
  142. /// \param fd the file descriptor to be used.
  143. /// \param af the address family of the file descriptor. Must be either
  144. /// AF_INET or AF_INET6.
  145. /// \throw isc::InvalidParameter if af is neither AF_INET nor AF_INET6.
  146. /// \throw isc::asiolink::IOError when a low-level error happens, like the
  147. /// fd is not a valid descriptor or it can't be listened on.
  148. virtual void addServerTCPFromFD(int fd, int af);
  149. /// \brief Add another UDP server to the service from already opened
  150. /// file descriptor
  151. ///
  152. /// Adds a new UDP server using an already opened file descriptor (eg. it
  153. /// only wraps it so the file descriptor is usable within the event loop).
  154. /// The file descriptor must be associated with a UDP socket of the given
  155. /// address family that is bound to an appropriate port (and possibly a
  156. /// specific address).
  157. ///
  158. /// \param fd the file descriptor to be used.
  159. /// \param af the address family of the file descriptor. Must be either
  160. /// AF_INET or AF_INET6.
  161. /// \param options Optional properties of the server (see ServerFlag).
  162. ///
  163. /// \throw isc::InvalidParameter if af is neither AF_INET nor AF_INET6,
  164. /// or the given \c options include an unsupported or invalid value.
  165. /// \throw isc::asiolink::IOError when a low-level error happens, like the
  166. /// fd is not a valid descriptor or it can't be listened on.
  167. virtual void addServerUDPFromFD(int fd, int af,
  168. ServerFlag options = SERVER_DEFAULT);
  169. /// \brief Remove all servers from the service
  170. void clearServers();
  171. /// \brief Return the native \c io_service object used in this wrapper.
  172. ///
  173. /// This is a short term work around to support other BIND 10 modules
  174. /// that share the same \c io_service with the authoritative server.
  175. /// It will eventually be removed once the wrapper interface is
  176. /// generalized.
  177. asio::io_service& get_io_service() { return io_service_.get_io_service(); }
  178. /// \brief Return the IO Service Object
  179. ///
  180. /// \return IOService object for this DNS service.
  181. virtual asiolink::IOService& getIOService() { return (io_service_);}
  182. virtual void setTCPRecvTimeout(size_t timeout);
  183. private:
  184. DNSServiceImpl* impl_;
  185. asiolink::IOService& io_service_;
  186. };
  187. } // namespace asiodns
  188. } // namespace isc
  189. #endif // ASIOLINK_DNS_SERVICE_H
  190. // Local Variables:
  191. // mode: c++
  192. // End: