zone_entry.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. // $Id$
  15. #ifndef __ZONE_ENTRY_H
  16. #define __ZONE_ENTRY_H
  17. #include <string>
  18. #include <vector>
  19. #include <list>
  20. #include <boost/thread.hpp>
  21. #include <boost/shared_ptr.hpp>
  22. #include <dns/rrset.h>
  23. #include "hash_key.h"
  24. #include "nsas_entry.h"
  25. #include "asiolink.h"
  26. #include "fetchable.h"
  27. namespace isc {
  28. namespace nsas {
  29. class NameserverEntry;
  30. class AddressRequestCallback;
  31. /// \brief Zone Entry
  32. ///
  33. /// The zone entry object describes a zone for which nameserver address
  34. /// information is held.
  35. ///
  36. /// Although the interface is simple, the internal processing is fairly
  37. /// complicated, in that the class takes account of triggering fetches for
  38. /// addresses of nameservers when the address records expire.
  39. class ZoneEntry : public NsasEntry<ZoneEntry>, public Fetchable {
  40. public:
  41. /// \brief Constructor where no NS records are supplied
  42. ///
  43. /// \param name Name of the zone
  44. /// \param class_code Class of this zone (zones of different classes have
  45. /// different objects.
  46. ZoneEntry(const std::string& name, uint16_t class_code) :
  47. name_(name), classCode_(class_code)
  48. {}
  49. /// \brief Constructor
  50. ///
  51. /// Creates a zone entry object with an RRset representing the nameservers,
  52. /// plus possibly additional RRsets holding address information.
  53. //ZoneEntry(isc::dns::AbstractRRset* nsrrset,
  54. // const std::vector<isc::dns::AbstractRRset*>& additional);
  55. /// \brief Destructor
  56. virtual ~ZoneEntry()
  57. {}
  58. /// \return Name of the zone
  59. virtual std::string getName() const {
  60. return name_;
  61. }
  62. /// \return Class of zone
  63. virtual short getClass() const {
  64. return classCode_;
  65. }
  66. /// \return Return Hash Key
  67. virtual HashKey hashKey() const {
  68. return HashKey(name_, classCode_);
  69. }
  70. // TODO The callbacks must be distinguished - A, AAAA or any of them
  71. /// \short Add another callback here
  72. void addCallback(boost::shared_ptr<AddressRequestCallback> callback);
  73. /// \short Is there at last one callback waiting?
  74. bool hasCallbacks() const;
  75. /// \short Remove a callback from queue and return it
  76. boost::shared_ptr<AddressRequestCallback> popCallback();
  77. /// \short Nameserver entry pointer
  78. typedef boost::shared_ptr<NameserverEntry> NameserverPtr;
  79. /// \short Vector of nameservers
  80. typedef std::vector<NameserverPtr> NameserverVector;
  81. /**
  82. * \name Iterators
  83. *
  84. * They iterate over the nameservers.
  85. */
  86. //@{
  87. typedef NameserverVector::iterator iterator;
  88. typedef NameserverVector::const_iterator const_iterator;
  89. //@}
  90. /**
  91. * \short Add a nameserver pointer to this zone.
  92. *
  93. * This does not lock, as it should be called while it is being created.
  94. * No new nameservers should be added later (it should timeout first and
  95. * be rebuild). Calling this after addition to the NameserverAddressStore
  96. * is undefined (it is not thread safe).
  97. */
  98. void nameserverAdd(NameserverPtr ns) { nameservers_.push_back(ns); }
  99. /**
  100. * \name Iterator access
  101. *
  102. * They work similar to usual stl iterator access functions. They iterate
  103. * over the nameservers.
  104. *
  105. * They do not lock, as the nameservers should be read only during
  106. * the life of the zone.
  107. */
  108. //@{
  109. iterator begin() { return (nameservers_.begin()); }
  110. iterator end() { return (nameservers_.end()); }
  111. const_iterator begin() const { return (nameservers_.begin()); }
  112. const_iterator end() const { return (nameservers_.end()); }
  113. //@}
  114. // TODO Get rid of this
  115. /**
  116. * \short Lock of the zone entry.
  117. *
  118. * Something like a scope lock for the zone entry. It can be copyed (so
  119. * the result of the getLock() can be assigned to a local variable). The
  120. * lock is released once all copies of the getLock result are destroyed.
  121. * However, it is not reentrant (another call to getLock will block).
  122. *
  123. * This locks both the zone entry and all nameserver entries in a manner
  124. * avoiding deadlocks (sorts the nameserver entry pointers before trying to
  125. * lock them). However, it asumes no one does any other kind of locking
  126. * of multiple mutices.
  127. *
  128. * Copy constructor, assignment operator and destructor are default.
  129. * The constructor that creates a new lock is private, use getLock()
  130. * to lock a zone entry.
  131. *
  132. * It is an error for the lock to survive destruction of its zone entry.
  133. */
  134. class Lock {
  135. private:
  136. struct Impl;
  137. boost::shared_ptr<Impl> impl_;
  138. Lock(boost::shared_ptr<Impl>);
  139. friend class ZoneEntry;
  140. };
  141. /**
  142. * \short Acquire a lock.
  143. *
  144. * \see Lock
  145. */
  146. Lock getLock();
  147. private:
  148. // TODO Read-Write lock?
  149. mutable boost::mutex mutex_; ///< Mutex protecting this zone entry
  150. std::string name_; ///< Canonical zone name
  151. uint16_t classCode_; ///< Class code
  152. NameserverVector nameservers_; ///< Nameservers
  153. time_t expiry_; ///< Expiry time of this entry
  154. std::list<boost::shared_ptr<AddressRequestCallback> > callbacks_;
  155. };
  156. } // namespace nsas
  157. } // namespace isc
  158. #endif // __ZONE_ENTRY_H