resolver.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. // Copyright (C) 2009 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 __RESOLVER_H
  15. #define __RESOLVER_H 1
  16. #include <string>
  17. #include <vector>
  18. #include <utility>
  19. #include <boost/shared_ptr.hpp>
  20. #include <cc/data.h>
  21. #include <config/ccsession.h>
  22. #include <acl/dns.h>
  23. #include <dns/message.h>
  24. #include <util/buffer.h>
  25. #include <asiodns/dns_server.h>
  26. #include <asiodns/dns_service.h>
  27. #include <asiodns/dns_lookup.h>
  28. #include <asiodns/dns_answer.h>
  29. #include <asiolink/io_message.h>
  30. #include <asiolink/io_service.h>
  31. #include <asiolink/simple_callback.h>
  32. #include <nsas/nameserver_address_store.h>
  33. #include <cache/resolver_cache.h>
  34. #include <resolve/resolver_interface.h>
  35. class ResolverImpl;
  36. /**
  37. * \short The recursive nameserver.
  38. *
  39. * It is a concreate class implementing recursive DNS server protocol
  40. * processing. It is responsible for handling incoming DNS requests. It parses
  41. * them, passes them deeper into the resolving machinery and then creates the
  42. * answer. It doesn't really know about chasing referrals and similar, it
  43. * simply plugs the parts that know into the network handling code.
  44. */
  45. class Resolver : public isc::resolve::ResolverInterface {
  46. ///
  47. /// \name Constructors, Assignment Operator and Destructor.
  48. ///
  49. /// Note: The copy constructor and the assignment operator are
  50. /// intentionally defined as private.
  51. //@{
  52. private:
  53. Resolver(const Resolver& source);
  54. Resolver& operator=(const Resolver& source);
  55. public:
  56. /// The constructor.
  57. Resolver();
  58. ~Resolver();
  59. //@}
  60. virtual void resolve(
  61. const isc::dns::QuestionPtr& question,
  62. const isc::resolve::ResolverInterface::CallbackPtr& callback);
  63. /// \brief Process an incoming DNS message, then signal 'server' to resume
  64. ///
  65. /// A DNS query (or other message) has been received by a \c DNSServer
  66. /// object. Find an answer, then post the \c DNSServer object on the
  67. /// I/O service queue and return. When the server resumes, it can
  68. /// send the reply.
  69. ///
  70. /// \param io_message The raw message received
  71. /// \param query_message Pointer to the query Message object we
  72. /// received from the client
  73. /// \param answer_message Pointer to the anwer Message object we
  74. /// shall return to the client
  75. /// \param buffer Pointer to an \c OutputBuffer for the resposne
  76. /// \param server Pointer to the \c DNSServer
  77. void processMessage(const isc::asiolink::IOMessage& io_message,
  78. isc::dns::MessagePtr query_message,
  79. isc::dns::MessagePtr answer_message,
  80. isc::util::OutputBufferPtr buffer,
  81. isc::asiodns::DNSServer* server);
  82. /// \brief Set and get the config session
  83. isc::config::ModuleCCSession* getConfigSession() const;
  84. void setConfigSession(isc::config::ModuleCCSession* config_session);
  85. /// \brief Handle commands from the config session.
  86. ///
  87. /// By default, or if \c startup is set to false explicitly, this method
  88. /// will ignore any other configuration parameters if listen_on fails;
  89. /// if \c startup is true, it will install as many parameters as possible.
  90. isc::data::ConstElementPtr updateConfig(isc::data::ConstElementPtr config,
  91. bool startup = false);
  92. /// \brief Assign an ASIO IO Service queue to this Resolver object
  93. void setDNSService(isc::asiodns::DNSServiceBase& dnss);
  94. /// \brief Assign a NameserverAddressStore to this Resolver object
  95. void setNameserverAddressStore(isc::nsas::NameserverAddressStore &nsas);
  96. /// \brief Assign a cache to this Resolver object
  97. void setCache(isc::cache::ResolverCache& cache);
  98. /// \brief Return this object's ASIO IO Service queue
  99. isc::asiodns::DNSServiceBase& getDNSService() const { return (*dnss_); }
  100. /// \brief Returns this object's NSAS
  101. isc::nsas::NameserverAddressStore& getNameserverAddressStore() const {
  102. return *nsas_;
  103. };
  104. /// \brief Returns this object's ResolverCache
  105. isc::cache::ResolverCache& getResolverCache() const {
  106. return *cache_;
  107. };
  108. /// \brief Return pointer to the DNS Lookup callback function
  109. isc::asiodns::DNSLookup* getDNSLookupProvider() { return (dns_lookup_); }
  110. /// \brief Return pointer to the DNS Answer callback function
  111. isc::asiodns::DNSAnswer* getDNSAnswerProvider() { return (dns_answer_); }
  112. /// \brief Return pointer to the Checkin callback function
  113. isc::asiolink::SimpleCallback* getCheckinProvider() { return (checkin_); }
  114. /**
  115. * \brief Specify the list of upstream servers.
  116. *
  117. * Specify the list off addresses of upstream servers to forward queries
  118. * to. If the list is empty, this server is set to full recursive mode.
  119. * If it is non-empty, it switches to forwarder.
  120. *
  121. * @param addresses The list of addresses to use (each one is the address
  122. * and port pair).
  123. */
  124. void setForwardAddresses(const std::vector<std::pair<std::string,
  125. uint16_t> >& addresses);
  126. /**
  127. * \short Get list of upstream addresses.
  128. *
  129. * \see setForwardAddresses.
  130. */
  131. std::vector<std::pair<std::string, uint16_t> > getForwardAddresses() const;
  132. /// Return if we are in forwarding mode (if not, we are in fully recursive)
  133. bool isForwarding() const;
  134. /**
  135. * \brief Specify the list of root nameservers.
  136. *
  137. * Specify the list of addresses of root nameservers
  138. *
  139. * @param addresses The list of addresses to use (each one is the address
  140. * and port pair).
  141. */
  142. void setRootAddresses(const std::vector<std::pair<std::string,
  143. uint16_t> >& addresses);
  144. /**
  145. * \short Get list of root addresses.
  146. *
  147. * \see setRootAddresses.
  148. */
  149. std::vector<std::pair<std::string, uint16_t> > getRootAddresses() const;
  150. /**
  151. * Set and get the addresses we listen on.
  152. */
  153. void setListenAddresses(const std::vector<std::pair<std::string,
  154. uint16_t> >& addresses);
  155. std::vector<std::pair<std::string, uint16_t> > getListenAddresses() const;
  156. /**
  157. * \short Set options related to timeouts.
  158. *
  159. * This sets the time of timeout and number of retries.
  160. * \param query_timeout The timeout we use for queries we send
  161. * \param client_timeout The timeout at which point we send back a
  162. * SERVFAIL (while continuing to resolve the query)
  163. * \param lookup_timeout The timeout at which point we give up and
  164. * stop.
  165. * \param retries The number of retries (0 means try the first time only,
  166. * do not retry).
  167. */
  168. void setTimeouts(int query_timeout = 2000,
  169. int client_timeout = 4000,
  170. int lookup_timeout = 30000,
  171. unsigned retries = 3);
  172. /**
  173. * \short Get info about timeouts.
  174. *
  175. * \returns Timeout and retries (as described in setTimeouts).
  176. */
  177. std::pair<int, unsigned> getTimeouts() const;
  178. /**
  179. * \brief Get the timeout for outgoing queries
  180. *
  181. * \returns Timeout for outgoing queries
  182. */
  183. int getQueryTimeout() const;
  184. /**
  185. * \brief Get the timeout for incoming client queries
  186. *
  187. * After this timeout, a SERVFAIL shall be sent back
  188. * (internal resolving on the query will continue, see
  189. * \c getLookupTimeout())
  190. *
  191. * \returns Timeout for outgoing queries
  192. */
  193. int getClientTimeout() const;
  194. /**
  195. * \brief Get the timeout for lookups
  196. *
  197. * After this timeout, internal processing shall stop
  198. */
  199. int getLookupTimeout() const;
  200. /**
  201. * \brief Get the number of retries for outgoing queries
  202. *
  203. * If a query times out (value of \c getQueryTimeout()), we
  204. * will retry this number of times
  205. */
  206. int getRetries() const;
  207. /// Get the query ACL.
  208. ///
  209. /// \exception None
  210. const isc::acl::dns::RequestACL& getQueryACL() const;
  211. /// Set the new query ACL.
  212. ///
  213. /// This method replaces the existing query ACL completely.
  214. /// Normally this method will be called via the configuration handler,
  215. /// but is publicly available for convenience of tests (and other
  216. /// experimental purposes).
  217. /// \c new_acl must not be a NULL pointer.
  218. ///
  219. /// \exception InvalidParameter The given pointer is NULL
  220. ///
  221. /// \param new_acl The new ACL to replace the existing one.
  222. void setQueryACL(boost::shared_ptr<const isc::acl::dns::RequestACL>
  223. new_acl);
  224. private:
  225. ResolverImpl* impl_;
  226. isc::asiodns::DNSServiceBase* dnss_;
  227. isc::asiolink::SimpleCallback* checkin_;
  228. isc::asiodns::DNSLookup* dns_lookup_;
  229. isc::asiodns::DNSAnswer* dns_answer_;
  230. isc::nsas::NameserverAddressStore* nsas_;
  231. isc::cache::ResolverCache* cache_;
  232. };
  233. #endif // __RESOLVER_H
  234. // Local Variables:
  235. // mode: c++
  236. // End: