dns_service.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. /// Codes for UDPServers used in addServer()method.
  25. ///
  26. /// Note: the codes only used in how to create the UDPServers.
  27. enum UDPVersion {
  28. SYNC_ = 1, ///< used synchronous UDPServer
  29. ASYNC_ = 2 ///< used asynchronous UDPServer
  30. };
  31. /// \brief Handle DNS Queries
  32. ///
  33. /// DNSService is the service that handles DNS queries and answers with
  34. /// a given IOService. This class is mainly intended to hold all the
  35. /// logic that is shared between the authoritative and the recursive
  36. /// server implementations. As such, it handles asio, including config
  37. /// updates (through the 'Checkinprovider'), and listening sockets.
  38. class DNSService {
  39. ///
  40. /// \name Constructors and Destructor
  41. ///
  42. /// Note: The copy constructor and the assignment operator are
  43. /// intentionally defined as private, making this class non-copyable.
  44. //@{
  45. private:
  46. DNSService(const DNSService& source);
  47. DNSService& operator=(const DNSService& source);
  48. public:
  49. /// \brief The constructor with a specific IP address and port on which
  50. /// the services listen on.
  51. ///
  52. /// \param io_service The IOService to work with
  53. /// \param port the port to listen on
  54. /// \param address the IP address to listen on
  55. /// \param checkin Provider for cc-channel events (see \c SimpleCallback)
  56. /// \param lookup The lookup provider (see \c DNSLookup)
  57. /// \param answer The answer provider (see \c DNSAnswer)
  58. DNSService(asiolink::IOService& io_service, const char& port,
  59. const char& address, isc::asiolink::SimpleCallback* checkin,
  60. DNSLookup* lookup, DNSAnswer* answer,
  61. const UDPVersion param_flags = SYNC_);
  62. /// \brief The constructor with a specific port on which the services
  63. /// listen on.
  64. ///
  65. /// It effectively listens on "any" IPv4 and/or IPv6 addresses.
  66. /// IPv4/IPv6 services will be available if and only if \c use_ipv4
  67. /// or \c use_ipv6 is \c true, respectively.
  68. ///
  69. /// \param io_service The IOService to work with
  70. /// \param port the port to listen on
  71. /// \param use_ipv4 If true, listen on ipv4 'any'
  72. /// \param use_ipv6 If true, listen on ipv6 'any'
  73. /// \param checkin Provider for cc-channel events (see \c SimpleCallback)
  74. /// \param lookup The lookup provider (see \c DNSLookup)
  75. /// \param answer The answer provider (see \c DNSAnswer)
  76. DNSService(asiolink::IOService& io_service, const char& port,
  77. const bool use_ipv4, const bool use_ipv6,
  78. isc::asiolink::SimpleCallback* checkin, DNSLookup* lookup,
  79. DNSAnswer* answer,
  80. const UDPVersion param_flags = SYNC_);
  81. /// \brief The constructor without any servers.
  82. ///
  83. /// Use addServer() to add some servers.
  84. DNSService(asiolink::IOService& io_service, isc::asiolink::SimpleCallback* checkin,
  85. DNSLookup* lookup, DNSAnswer* answer,
  86. const UDPVersion param_flags = SYNC_);
  87. /// \brief The destructor.
  88. ~DNSService();
  89. //@}
  90. /// \brief Add another server to the service
  91. void addServer(uint16_t port, const std::string &address,
  92. const UDPVersion param_flags = SYNC_);
  93. void addServer(const char &port, const std::string &address,
  94. const UDPVersion param_flags = SYNC_);
  95. /// \brief Add another TCP server/listener to the service from already
  96. /// opened file descriptor
  97. ///
  98. /// Adds a new TCP server using an already opened file descriptor (eg. it
  99. /// only wraps it so the file descriptor is usable within the event loop).
  100. /// The file descriptor must be associated with a TCP socket of the given
  101. /// address family that is bound to an appropriate port (and possibly a
  102. /// specific address) and is ready for listening to new connection
  103. /// requests but has not actually started listening.
  104. ///
  105. /// \param fd the file descriptor to be used.
  106. /// \param af the address family of the file descriptor. Must be either
  107. /// AF_INET or AF_INET6.
  108. /// \throw isc::InvalidParameter if af is neither AF_INET nor AF_INET6.
  109. /// \throw isc::asiolink::IOError when a low-level error happens, like the
  110. /// fd is not a valid descriptor or it can't be listened on.
  111. void addServerTCPFromFD(int fd, int af);
  112. /// \brief Add another UDP server to the service from already opened
  113. /// file descriptor
  114. ///
  115. /// Adds a new UDP server using an already opened file descriptor (eg. it
  116. /// only wraps it so the file descriptor is usable within the event loop).
  117. /// The file descriptor must be associated with a UDP socket of the given
  118. /// address family that is bound to an appropriate port (and possibly a
  119. /// specific address).
  120. ///
  121. /// \param fd the file descriptor to be used.
  122. /// \param af the address family of the file descriptor. Must be either
  123. /// AF_INET or AF_INET6.
  124. /// \throw isc::InvalidParameter if af is neither AF_INET nor AF_INET6.
  125. /// \throw isc::asiolink::IOError when a low-level error happens, like the
  126. /// fd is not a valid descriptor or it can't be listened on.
  127. void addServerUDPFromFD(int fd, int af,const UDPVersion param_flags = SYNC_);
  128. /// \brief Remove all servers from the service
  129. void clearServers();
  130. /// \brief Return the native \c io_service object used in this wrapper.
  131. ///
  132. /// This is a short term work around to support other BIND 10 modules
  133. /// that share the same \c io_service with the authoritative server.
  134. /// It will eventually be removed once the wrapper interface is
  135. /// generalized.
  136. asio::io_service& get_io_service() { return io_service_.get_io_service(); }
  137. /// \brief Return the IO Service Object
  138. ///
  139. /// \return IOService object for this DNS service.
  140. asiolink::IOService& getIOService() { return (io_service_);}
  141. private:
  142. DNSServiceImpl* impl_;
  143. asiolink::IOService& io_service_;
  144. };
  145. } // namespace asiodns
  146. } // namespace isc
  147. #endif // __ASIOLINK_DNS_SERVICE_H