dns_service.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. virtual asiolink::IOService& getIOService() = 0;
  78. };
  79. /// \brief Handle DNS Queries
  80. ///
  81. /// DNSService is the service that handles DNS queries and answers with
  82. /// a given IOService. This class is mainly intended to hold all the
  83. /// logic that is shared between the authoritative and the recursive
  84. /// server implementations. As such, it handles asio, including config
  85. /// updates (through the 'Checkinprovider'), and listening sockets.
  86. class DNSService : public DNSServiceBase {
  87. ///
  88. /// \name Constructors and Destructor
  89. ///
  90. /// Note: The copy constructor and the assignment operator are
  91. /// intentionally defined as private, making this class non-copyable.
  92. //@{
  93. private:
  94. DNSService(const DNSService& source);
  95. DNSService& operator=(const DNSService& source);
  96. private:
  97. // Bit or'ed all defined \c ServerFlag values. Used internally for
  98. // compatibility check. Note that this doesn't have to be used by
  99. // applications, and doesn't have to be defined in the "base" class.
  100. static const unsigned int SERVER_DEFINED_FLAGS = 1;
  101. public:
  102. /// \brief The constructor with a specific IP address and port on which
  103. /// the services listen on.
  104. ///
  105. /// \param io_service The IOService to work with
  106. /// \param port the port to listen on
  107. /// \param address the IP address to listen on
  108. /// \param checkin Provider for cc-channel events (see \c SimpleCallback)
  109. /// \param lookup The lookup provider (see \c DNSLookup)
  110. /// \param answer The answer provider (see \c DNSAnswer)
  111. DNSService(asiolink::IOService& io_service, const char& port,
  112. const char& address, isc::asiolink::SimpleCallback* checkin,
  113. DNSLookup* lookup, DNSAnswer* answer);
  114. /// \brief The constructor with a specific port on which the services
  115. /// listen on.
  116. ///
  117. /// It effectively listens on "any" IPv4 and/or IPv6 addresses.
  118. /// IPv4/IPv6 services will be available if and only if \c use_ipv4
  119. /// or \c use_ipv6 is \c true, respectively.
  120. ///
  121. /// \param io_service The IOService to work with
  122. /// \param port the port to listen on
  123. /// \param use_ipv4 If true, listen on ipv4 'any'
  124. /// \param use_ipv6 If true, listen on ipv6 'any'
  125. /// \param checkin Provider for cc-channel events (see \c SimpleCallback)
  126. /// \param lookup The lookup provider (see \c DNSLookup)
  127. /// \param answer The answer provider (see \c DNSAnswer)
  128. DNSService(asiolink::IOService& io_service, const char& port,
  129. const bool use_ipv4, const bool use_ipv6,
  130. isc::asiolink::SimpleCallback* checkin, DNSLookup* lookup,
  131. DNSAnswer* answer);
  132. /// \brief The constructor without any servers.
  133. ///
  134. /// Use addServer() to add some servers.
  135. DNSService(asiolink::IOService& io_service,
  136. isc::asiolink::SimpleCallback* checkin,
  137. DNSLookup* lookup, DNSAnswer* answer);
  138. /// \brief The destructor.
  139. virtual ~DNSService();
  140. //@}
  141. /// \brief Add another server to the service
  142. void addServer(uint16_t port, const std::string &address);
  143. void addServer(const char& port, const std::string& address);
  144. /// \brief Add another TCP server/listener to the service from already
  145. /// opened file descriptor
  146. ///
  147. /// Adds a new TCP server using an already opened file descriptor (eg. it
  148. /// only wraps it so the file descriptor is usable within the event loop).
  149. /// The file descriptor must be associated with a TCP socket of the given
  150. /// address family that is bound to an appropriate port (and possibly a
  151. /// specific address) and is ready for listening to new connection
  152. /// requests but has not actually started listening.
  153. ///
  154. /// At the moment, TCP servers don't support any optional properties;
  155. /// so unlike the UDP version of the method it doesn't have an \c options
  156. /// argument.
  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. /// \throw isc::InvalidParameter if af is neither AF_INET nor AF_INET6.
  162. /// \throw isc::asiolink::IOError when a low-level error happens, like the
  163. /// fd is not a valid descriptor or it can't be listened on.
  164. virtual void addServerTCPFromFD(int fd, int af);
  165. /// \brief Add another UDP server to the service from already opened
  166. /// file descriptor
  167. ///
  168. /// Adds a new UDP server using an already opened file descriptor (eg. it
  169. /// only wraps it so the file descriptor is usable within the event loop).
  170. /// The file descriptor must be associated with a UDP socket of the given
  171. /// address family that is bound to an appropriate port (and possibly a
  172. /// specific address).
  173. ///
  174. /// \param fd the file descriptor to be used.
  175. /// \param af the address family of the file descriptor. Must be either
  176. /// AF_INET or AF_INET6.
  177. /// \param options Optional properties of the server (see ServerFlag).
  178. ///
  179. /// \throw isc::InvalidParameter if af is neither AF_INET nor AF_INET6,
  180. /// or the given \c options include an unsupported or invalid value.
  181. /// \throw isc::asiolink::IOError when a low-level error happens, like the
  182. /// fd is not a valid descriptor or it can't be listened on.
  183. virtual void addServerUDPFromFD(int fd, int af,
  184. ServerFlag options = SERVER_DEFAULT);
  185. /// \brief Remove all servers from the service
  186. void clearServers();
  187. /// \brief Return the native \c io_service object used in this wrapper.
  188. ///
  189. /// This is a short term work around to support other BIND 10 modules
  190. /// that share the same \c io_service with the authoritative server.
  191. /// It will eventually be removed once the wrapper interface is
  192. /// generalized.
  193. asio::io_service& get_io_service() { return io_service_.get_io_service(); }
  194. /// \brief Return the IO Service Object
  195. ///
  196. /// \return IOService object for this DNS service.
  197. virtual asiolink::IOService& getIOService() { return (io_service_);}
  198. private:
  199. DNSServiceImpl* impl_;
  200. asiolink::IOService& io_service_;
  201. };
  202. } // namespace asiodns
  203. } // namespace isc
  204. #endif // __ASIOLINK_DNS_SERVICE_H
  205. // Local Variables:
  206. // mode: c++
  207. // End: