recursive_query.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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_RECURSIVE_QUERY_H
  15. #define __ASIOLINK_RECURSIVE_QUERY_H 1
  16. #include <asiolink/dns_service.h>
  17. #include <asiolink/dns_server.h>
  18. #include <dns/buffer.h>
  19. #include <cache/resolver_cache.h>
  20. namespace asiolink {
  21. /// \brief The \c RecursiveQuery class provides a layer of abstraction around
  22. /// the ASIO code that carries out an upstream query.
  23. ///
  24. /// This design is very preliminary; currently it is only capable of
  25. /// handling simple forward requests to a single resolver.
  26. class RecursiveQuery {
  27. ///
  28. /// \name Constructors
  29. ///
  30. //@{
  31. public:
  32. /// \brief Constructor
  33. ///
  34. /// This is currently the only way to construct \c RecursiveQuery
  35. /// object. If the addresses of the forward nameservers is specified,
  36. /// and every upstream query will be sent to one random address, and
  37. /// the result sent back directly. If not, it will do full resolving.
  38. ///
  39. /// \param dns_service The DNS Service to perform the recursive
  40. /// query on.
  41. /// \param upstream Addresses and ports of the upstream servers
  42. /// to forward queries to.
  43. /// \param upstream_root Addresses and ports of the root servers
  44. /// to use when resolving.
  45. /// \param query_timeout Timeout value for queries we sent, in ms
  46. /// \param client_timeout Timeout value for when we send back an
  47. /// error, in ms
  48. /// \param lookup_timeout Timeout value for when we give up, in ms
  49. /// \param retries how many times we try again (0 means just send and
  50. /// and return if it returs).
  51. RecursiveQuery(DNSService& dns_service,
  52. const std::vector<std::pair<std::string, uint16_t> >&
  53. upstream,
  54. const std::vector<std::pair<std::string, uint16_t> >&
  55. upstream_root,
  56. int query_timeout = 2000,
  57. int client_timeout = 4000,
  58. int lookup_timeout = 30000,
  59. unsigned retries = 3);
  60. //@}
  61. /// \brief Initiate resolving
  62. ///
  63. /// When sendQuery() is called, a (set of) message(s) is sent
  64. /// asynchronously. If upstream servers are set, one is chosen
  65. /// and the response (if any) from that server will be returned.
  66. ///
  67. /// If not upstream is set, a root server is chosen from the
  68. /// root_servers, and the RunningQuery shall do a full resolve
  69. /// (i.e. if the answer is a delegation, it will be followed, etc.)
  70. /// until there is an answer or an error.
  71. ///
  72. /// When there is a response or an error and we give up, the given
  73. /// CallbackPtr object shall be called (with either success() or
  74. /// failure(). See ResolverInterface::Callback for more information.
  75. ///
  76. /// \param question The question being answered <qname/qclass/qtype>
  77. /// \param callback Callback object. See
  78. /// \c ResolverInterface::Callback for more information
  79. void resolve(const isc::dns::QuestionPtr& question,
  80. const isc::resolve::ResolverInterface::CallbackPtr callback);
  81. /// \brief Initiates resolving for the given question.
  82. ///
  83. /// This actually calls the previous sendQuery() with a default
  84. /// callback object, which calls resume() on the given DNSServer
  85. /// object.
  86. ///
  87. /// \param question The question being answered <qname/qclass/qtype>
  88. /// \param answer_message An output Message into which the final response will be copied
  89. /// \param buffer An output buffer into which the intermediate responses will be copied
  90. /// \param server A pointer to the \c DNSServer object handling the client
  91. void resolve(const isc::dns::Question& question,
  92. isc::dns::MessagePtr answer_message,
  93. isc::dns::OutputBufferPtr buffer,
  94. DNSServer* server);
  95. private:
  96. DNSService& dns_service_;
  97. boost::shared_ptr<std::vector<std::pair<std::string, uint16_t> > >
  98. upstream_;
  99. boost::shared_ptr<std::vector<std::pair<std::string, uint16_t> > >
  100. upstream_root_;
  101. int query_timeout_;
  102. int client_timeout_;
  103. int lookup_timeout_;
  104. unsigned retries_;
  105. // Cache. TODO: I think we want this initialized in Resolver class,
  106. // not here
  107. isc::cache::ResolverCache cache_;
  108. };
  109. } // namespace asiolink
  110. #endif // __ASIOLINK_RECURSIVE_QUERY_H