resolver.h 9.7 KB

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