zone_entry.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // Copyright (C) 2010 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 __ZONE_ENTRY_H
  15. #define __ZONE_ENTRY_H
  16. #include <string>
  17. #include <vector>
  18. #include <set>
  19. #include <boost/shared_ptr.hpp>
  20. #include <boost/enable_shared_from_this.hpp>
  21. #include <dns/rrset.h>
  22. #include <resolve/resolver_interface.h>
  23. #include <util/locks.h>
  24. #include <util/random/random_number_generator.h>
  25. #include "hash_key.h"
  26. #include "nsas_entry.h"
  27. #include "asiolink.h"
  28. #include "fetchable.h"
  29. #include "nsas_types.h"
  30. #include "glue_hints.h"
  31. namespace isc {
  32. namespace nsas {
  33. class NameserverEntry;
  34. class AddressRequestCallback;
  35. /// \brief Zone Entry
  36. ///
  37. /// The zone entry object describes a zone for which nameserver address
  38. /// information is held.
  39. ///
  40. /// Although the interface is simple, the internal processing is fairly
  41. /// complicated, in that the class takes account of triggering fetches for
  42. /// addresses of nameservers when the address records expire.
  43. ///
  44. /// It uses shared_from_this in its methods. It must live inside a shared_ptr.
  45. class ZoneEntry : public NsasEntry<ZoneEntry>, public Fetchable {
  46. public:
  47. /**
  48. * \brief Constructor.
  49. *
  50. * It asks the resolver any needed questions to get the nameservers.
  51. *
  52. * \param resolver The resolver used to ask for IP addresses
  53. * \param name Name of the zone
  54. * \param class_code Class of this zone (zones of different classes have
  55. * different objects.
  56. * \param nameserver_table Hashtable of NameServerEntry objects for
  57. * this zone
  58. * \param namesever_lru LRU for the nameserver entries
  59. * \todo Move to cc file, include the lookup (if NSAS uses resolver for
  60. * everything)
  61. */
  62. ZoneEntry(isc::resolve::ResolverInterface* resolver,
  63. const std::string& name, const isc::dns::RRClass& class_code,
  64. boost::shared_ptr<HashTable<NameserverEntry> > nameserver_table,
  65. boost::shared_ptr<isc::util::LruList<NameserverEntry> > nameserver_lru);
  66. /// \return Name of the zone
  67. std::string getName() const {
  68. return name_;
  69. }
  70. /// \return Class of zone
  71. const isc::dns::RRClass& getClass() const {
  72. return class_code_;
  73. }
  74. /// \return Return Hash Key
  75. virtual HashKey hashKey() const {
  76. return HashKey(name_, class_code_);
  77. }
  78. /**
  79. * \short Put another callback inside.
  80. *
  81. * This callback is either executed right away, if it is possible,
  82. * or queued for later.
  83. *
  84. * \param callback The callback itself.
  85. * \param family Which address family is acceptable as an answer?
  86. * \param glue_hints If a non-empty glue-hints object is passed,
  87. * and the NSAS does not have an immediate answer, it will
  88. * call back immediately with one of the glue hints.
  89. */
  90. void addCallback(boost::shared_ptr<AddressRequestCallback>
  91. callback, AddressFamily family,
  92. const GlueHints& glue_hints = GlueHints());
  93. /**
  94. * \short Remove a callback from the list
  95. *
  96. * \param callback The callback itself.
  97. * \param family Which address family is acceptable as an answer?
  98. */
  99. void removeCallback(const boost::shared_ptr<AddressRequestCallback>&
  100. callback, AddressFamily family);
  101. /// \short Protected members, so they can be accessed by tests.
  102. //@{
  103. protected:
  104. // TODO Read-Write lock?
  105. typedef boost::shared_ptr<NameserverEntry> NameserverPtr;
  106. typedef std::vector<NameserverPtr> NameserverVector;
  107. NameserverVector nameservers_; ///< Nameservers
  108. // Which nameservers didn't have any of our callbacks yet
  109. std::set<NameserverPtr> nameservers_not_asked_;
  110. /*
  111. * Callbacks. For each fimily type one vector, so we can process
  112. * them separately.
  113. */
  114. std::vector<boost::shared_ptr<AddressRequestCallback> >
  115. callbacks_[ADDR_REQ_MAX];
  116. time_t expiry_; ///< Expiry time of this entry, 0 means not set
  117. //}@
  118. private:
  119. mutable isc::util::locks::recursive_mutex mutex_;///< Mutex protecting this zone entry
  120. std::string name_; ///< Canonical zone name
  121. isc::dns::RRClass class_code_; ///< Class code
  122. /**
  123. * \short Process all the callbacks that can be processed
  124. *
  125. * The purpose of this funtion is to ask all nameservers for their IP
  126. * addresses and execute all callbacks that can be executed. It is
  127. * called whenever new callback appears and there's a chance it could
  128. * be answered or when new information is available (list of nameservers,
  129. * nameserver is unreachable or has an address).
  130. * \param family Which is the interesting address family where the change
  131. * happened. ADDR_REQ_MAX means it could be any of them and it will
  132. * trigger processing of all callbacks no matter what their family
  133. * was.
  134. * \param nameserver Pass a nameserver if the change was triggered by
  135. * the nameserver (if it wasn't triggered by a nameserver, pass empty
  136. * pointer). This one will be accepted even with 0 TTL, the information
  137. * just arrived and we are allowed to use it just now.
  138. * \todo With the recursive locks now, we might want to simplify executing
  139. * callbacks (here and other functions as well);
  140. */
  141. void process(AddressFamily family,
  142. const boost::shared_ptr<NameserverEntry>& nameserver);
  143. // Resolver we use
  144. isc::resolve::ResolverInterface* resolver_;
  145. // We store the nameserver table and lru, so we can look up when there's
  146. // update
  147. boost::shared_ptr<HashTable<NameserverEntry> > nameserver_table_;
  148. boost::shared_ptr<isc::util::LruList<NameserverEntry> > nameserver_lru_;
  149. // Resolver callback class, documentation with the class declaration
  150. class ResolverCallback;
  151. // It has direct access to us
  152. friend class ResolverCallback;
  153. // Guard class to eliminate missing finally
  154. class ProcessGuard;
  155. friend class ProcessGuard;
  156. // Are we in the process method?
  157. bool in_process_[ADDR_REQ_MAX];
  158. // Callback from nameserver entry (documented with the class)
  159. class NameserverCallback;
  160. // And it can get into our internals as well (call process)
  161. friend class NameserverCallback;
  162. // This dispatches callbacks of given family with failures
  163. void dispatchFailures(AddressFamily family);
  164. // Put a callback into the nameserver entry. Same ADDR_REQ_MAX means for
  165. // all families
  166. void insertCallback(NameserverPtr nameserver, AddressFamily family);
  167. // A random generator for this zone entry
  168. // TODO: A more global one? Per thread one?
  169. isc::util::random::WeightedRandomIntegerGenerator address_selector;
  170. };
  171. } // namespace nsas
  172. } // namespace isc
  173. #endif // __ZONE_ENTRY_H