nameserver_entry.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 __NAMESERVER_ENTRY_H
  16. #define __NAMESERVER_ENTRY_H
  17. #include <string>
  18. #include <vector>
  19. #include <boost/thread.hpp>
  20. #include <exceptions/exceptions.h>
  21. #include <dns/rrset.h>
  22. #include "address_entry.h"
  23. #include "asiolink.h"
  24. #include "nsas_entry.h"
  25. #include "hash_key.h"
  26. #include "lru_list.h"
  27. #include "fetchable.h"
  28. namespace isc {
  29. namespace nsas {
  30. /// \brief Inconsistent Owner Names
  31. ///
  32. /// Thrown if a NameserverEntry is constructed from both an A and AAAA RRset
  33. /// where the owner names do not match.
  34. class InconsistentOwnerNames : public Exception {
  35. public:
  36. InconsistentOwnerNames(const char* file, size_t line, const char* what) :
  37. isc::Exception(file, line, what)
  38. {}
  39. };
  40. /// \brief Inconsistent Class
  41. ///
  42. /// Thrown if a NameserverEntry is constructed from both an A and AAAA RRset
  43. /// where the classes do not match.
  44. class InconsistentClass : public Exception {
  45. public:
  46. InconsistentClass(const char* file, size_t line, const char* what) :
  47. isc::Exception(file, line, what)
  48. {}
  49. };
  50. class ZoneEntry;
  51. /// \brief Nameserver Entry
  52. ///
  53. /// Describes a nameserver and its addresses. A nameserver be authoritative
  54. /// for several zones (hence is pointed to by more than one zone entry), and
  55. /// may have several addresses associated with it.
  56. ///
  57. /// When created, zero or more addresses may be given. At any time, the list
  58. /// of addresses may be updated. This may occur (a) after creation, either to
  59. /// to get the list of addresses when none have been supplied or to replace
  60. /// glue records, or (b) when the object has been accessed but found to be
  61. /// expired (the address records have reached their TTL).
  62. /// TODO: Add code for update of addresses
  63. ///
  64. /// The addresses expire after their TTL has been reached. For simplicity,
  65. /// (and because it is unlikely that A and AAAA records from the same zone have
  66. /// different TTLs) there is one expiration time for all address records.
  67. /// When that is reached, all records are declared expired and new fetches
  68. /// started for the information.
  69. ///
  70. /// As this object will be stored in the nameserver address store LRU list,
  71. /// it is derived from the LRU list entry class.
  72. class NameserverEntry : public NsasEntry<NameserverEntry>, public Fetchable {
  73. public:
  74. /// List of addresses associated with this nameserver
  75. typedef std::vector<AddressEntry> AddressVector;
  76. typedef AddressVector::iterator AddressVectorIterator;
  77. /// \brief Constructor where no A records are supplied.
  78. ///
  79. /// \param name Name of the nameserver,
  80. /// \param class_code class of the nameserver
  81. NameserverEntry(const std::string& name, uint16_t class_code) :
  82. name_(name), classCode_(class_code)
  83. {}
  84. /// Constructor where one or more RRsets of A/AAAA records are supplied.
  85. /// The class is taken from class of address records and the name from
  86. /// the owner of the records. If both sets of information are supplied
  87. /// and the owner names are different, the V4 set wins out; the V6 set of
  88. /// information is ignored and an error message is logged.
  89. ///
  90. /// \param v4Set RRset of A records
  91. /// \param v6Set RRset of AAAA records
  92. /// \param curtime Current time. Present for testing, but also as a
  93. /// possible optimisation if the caller has the current time (it saves
  94. /// the overhead of a call to time()). The default value of 0 requests
  95. /// the constructor to get its own copy of the current time.
  96. NameserverEntry(const isc::dns::AbstractRRset* v4Set,
  97. const isc::dns::AbstractRRset* v6Set, time_t curtime = 0);
  98. /// \brief Virtual Destructor
  99. virtual ~NameserverEntry()
  100. {}
  101. /// \brief Return Address
  102. ///
  103. /// Returns a vector of addresses corresponding to this nameserver.
  104. /// It is up to the caller to
  105. ///
  106. /// \param addresses Vector of address entries into which will be appended
  107. /// addresses that match the specified criteria. (The reason for choosing
  108. /// this signature is that addresses from more than one nameserver may be
  109. /// retrieved, in which case appending to an existing list of addresses is
  110. /// convenient.)
  111. /// \param family Set to AF_INET/AF_INET6 for V6/V6 addresses, anything
  112. /// else for all addresses.
  113. virtual void getAddresses(NameserverEntry::AddressVector& addresses,
  114. short family = 0) const;
  115. /// \brief Return Address that corresponding to the index
  116. ///
  117. /// \param index The address index in the address vector
  118. virtual asiolink::IOAddress getAddressAtIndex(uint32_t index) const;
  119. /// \brief Update RTT
  120. ///
  121. /// Updates the RTT for a particular address
  122. ///
  123. /// \param address Address to update
  124. /// \param RTT New RTT for the address
  125. virtual void setAddressRTT(const asiolink::IOAddress& address, uint32_t rtt);
  126. /// \brief Update RTT of the address that corresponding to the index
  127. ///
  128. /// \param rtt Round-Trip Time
  129. /// \param index The address's index in address vector
  130. virtual void updateAddressRTTAtIndex(uint32_t rtt, uint32_t index);
  131. /// \brief Set Address Unreachable
  132. ///
  133. /// Sets the specified address to be unreachable
  134. ///
  135. /// \param address Address to update
  136. virtual void setAddressUnreachable(const asiolink::IOAddress& address);
  137. /// \return Owner Name of RRset
  138. virtual std::string getName() const {
  139. return name_;
  140. }
  141. /// \return Class of RRset
  142. virtual short getClass() const {
  143. return classCode_;
  144. }
  145. /// \return Hash Key of the Nameserver
  146. virtual HashKey hashKey() const {
  147. return HashKey(name_, classCode_);
  148. }
  149. /// \return Hash Key of the Nameserver
  150. /// \return Expiration Time of Data
  151. ///
  152. /// Returns the expiration time of addresses for this nameserver. For
  153. /// simplicity, this quantity is calculated as the minimum expiration time
  154. /// of the A and AAAA address records.
  155. virtual time_t getExpiration() const {
  156. return expiration_;
  157. }
  158. /// \brief Predicate for Address Selection
  159. ///
  160. /// Returns false if the address family of a given entry matches the address
  161. /// family given or if the address family is 0 (which means return all
  162. /// addresses). This curious logic is needed for use in the remove_copy_if
  163. /// algorithm, which copies all values apart from those for which the
  164. /// criteria is met.
  165. class AddressSelection : public std::binary_function<short, AddressEntry, bool> {
  166. public:
  167. bool operator()(short family, const AddressEntry& entry) const {
  168. bool match = (entry.getAddress().getFamily() == family) ||
  169. (family == 0);
  170. return (! match);
  171. }
  172. };
  173. private:
  174. boost::mutex mutex_; ///< Mutex protecting this object
  175. std::string name_; ///< Canonical name of the nameserver
  176. uint16_t classCode_; ///< Class of the nameserver
  177. std::vector<AddressEntry> address_; ///< Set of V4/V6 addresses
  178. time_t expiration_; ///< Summary expiration time
  179. time_t last_access_; ///< Last access time to the structure
  180. // We allow ZoneEntry to lock us
  181. friend class ZoneEntry;
  182. };
  183. } // namespace dns
  184. } // namespace isc
  185. #endif // __NAMESERVER_ENTRY_H