message_entry.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 __MESSAGE_ENTRY_H
  15. #define __MESSAGE_ENTRY_H
  16. #include <vector>
  17. #include <dns/message.h>
  18. #include <dns/rrset.h>
  19. #include <nsas/nsas_entry.h>
  20. #include "rrset_cache.h"
  21. #include "rrset_entry.h"
  22. using namespace isc::nsas;
  23. namespace isc {
  24. namespace cache {
  25. class RRsetEntry;
  26. /// \brief Message Entry
  27. ///
  28. /// The object of MessageEntry represents one response message
  29. /// answered to the resolver client.
  30. class MessageEntry : public NsasEntry<MessageEntry> {
  31. // Noncopyable
  32. private:
  33. MessageEntry(const MessageEntry& source);
  34. MessageEntry& operator=(const MessageEntry& source);
  35. /// \brief Information to refer an RRset.
  36. ///
  37. /// There is no class information here, since the rrsets are cached in
  38. /// the class-specific rrset cache.
  39. struct RRsetRef{
  40. /// \brief Constructor
  41. ///
  42. /// \param name The Name for the RRset
  43. /// \param type The RRType for the RRrset
  44. /// \param cache Which cache the RRset is stored in
  45. RRsetRef(const isc::dns::Name& name, const isc::dns::RRType& type,
  46. RRsetCache* cache):
  47. name_(name), type_(type), cache_(cache)
  48. {}
  49. isc::dns::Name name_; // Name of rrset.
  50. isc::dns::RRType type_; // Type of rrset.
  51. RRsetCache* cache_; //Which cache the RRset is stored
  52. };
  53. public:
  54. /// \brief Initialize the message entry object with one dns
  55. /// message.
  56. /// \param message The message used to initialize MessageEntry.
  57. /// \param rrset_cache the pointer of RRsetCache. When one message
  58. /// entry is created, rrset cache needs to be updated,
  59. /// since some new rrset entries may be inserted into
  60. /// rrset cache, or the existed rrset entries need
  61. /// to be updated.
  62. /// \param negative_soa_cache the pointer of RRsetCache. This
  63. /// cache is used only for storing SOA rrset from negative
  64. /// response (NXDOMAIN or NOERROR_NODATA)
  65. MessageEntry(const isc::dns::Message& message,
  66. const RRsetCachePtr& rrset_cache,
  67. const RRsetCachePtr& negative_soa_cache);
  68. ~MessageEntry() { delete hash_key_ptr_; };
  69. /// \brief generate one dns message according
  70. /// the rrsets information of the message.
  71. ///
  72. /// \param time_now set the ttl of each rrset in the message
  73. /// as "expire_time - time_now" (expire_time is the
  74. /// expiration time of the rrset).
  75. /// \param response generated dns message.
  76. /// \return return true if the response message can be generated
  77. /// from the cached information, or else, return false.
  78. bool genMessage(const time_t& time_now, isc::dns::Message& response);
  79. /// \brief Get the hash key of the message entry.
  80. ///
  81. /// \return return hash key
  82. virtual HashKey hashKey() const {
  83. return (*hash_key_ptr_);
  84. }
  85. /// \brief Get expire time of the message entry.
  86. /// \return return the expire time of message entry.
  87. time_t getExpireTime() const {
  88. return (expire_time_);
  89. }
  90. /// \short Protected memebers, so they can be accessed by tests.
  91. //@{
  92. protected:
  93. /// \brief Initialize the message entry with dns message.
  94. ///
  95. /// \param message The Message to initialize the entry with
  96. void initMessageEntry(const isc::dns::Message& message);
  97. /// \brief Parse the rrsets in specified section.
  98. ///
  99. /// \param msg The message to parse the RRsets from
  100. /// \param section The Section to parse the RRsets from
  101. /// \param smaller_ttl Get the smallest ttl of rrsets in
  102. /// specified section, if it's smaller than the given value.
  103. /// \param rrset_count the rrset count of the section.
  104. /// (TODO for Message, getRRsetCount() should be one
  105. /// interface provided by Message.)
  106. void parseSection(const isc::dns::Message& msg,
  107. const isc::dns::Message::Section& section,
  108. uint32_t& smaller_ttl,
  109. uint16_t& rrset_count);
  110. /// \brief Parse the RRsets in the authority section of
  111. /// negative response. The SOA RRset need to be located and
  112. /// stored in a seperate cache
  113. /// \param msg The message to parse the RRsets from
  114. /// \param min_ttl Get the minimum ttl of rrset in the authority section
  115. /// \param rrset_count the rrset count of the authority section
  116. void parseNegativeResponseAuthoritySection(const isc::dns::Message& msg,
  117. uint32_t& min_ttl,
  118. uint16_t& rrset_count);
  119. /// \brief Get RRset Trustworthiness
  120. /// The algorithm refers to RFC2181 section 5.4.1
  121. /// Only the rrset can be updated by the rrsets
  122. /// with higher trust level.
  123. ///
  124. /// \param message Message that the rrset belongs to
  125. /// \param rrset specified rrset which needs to get its
  126. /// trust worthiness
  127. /// \param section Section of the rrset
  128. /// \return return rrset trust level.
  129. RRsetTrustLevel getRRsetTrustLevel(const isc::dns::Message& message,
  130. const isc::dns::RRsetPtr& rrset,
  131. const isc::dns::Message::Section& section);
  132. /// \brief Add rrset to one section of message.
  133. ///
  134. /// \param message The message to add rrsets to.
  135. /// \param rrset_entry_vec vector for rrset entries in
  136. /// different sections.
  137. /// \param section The section to add to
  138. void addRRset(isc::dns::Message& message,
  139. const std::vector<RRsetEntryPtr>& rrset_entry_vec,
  140. const isc::dns::Message::Section& section);
  141. /// \brief Get the all the rrset entries for the message entry.
  142. ///
  143. /// \param rrset_entry_vec vector to add unexpired rrset entries to
  144. /// \param time_now the time of now. Used to compare with rrset
  145. /// entry's expire time.
  146. /// \return return false if any rrset entry has expired, true
  147. /// otherwise.
  148. bool getRRsetEntries(std::vector<RRsetEntryPtr>& rrset_entry_vec,
  149. const time_t time_now);
  150. time_t expire_time_; // Expiration time of the message.
  151. //@}
  152. private:
  153. std::string entry_name_; // The name for this entry(name + type)
  154. HashKey* hash_key_ptr_; // the key for messag entry in hash table.
  155. std::vector<RRsetRef> rrsets_;
  156. RRsetCachePtr rrset_cache_; //Normal rrset cache
  157. // SOA rrset from negative response
  158. RRsetCachePtr negative_soa_cache_;
  159. std::string query_name_; // query name of the message.
  160. uint16_t query_class_; // query class of the message.
  161. uint16_t query_type_; // query type of message.
  162. uint16_t query_count_; // query count in query section.
  163. uint16_t answer_count_; // rrset count in answer section.
  164. uint16_t authority_count_; // rrset count in authority section.
  165. uint16_t additional_count_; // rrset count in addition section.
  166. //TODO, there should be a better way to cache these header flags
  167. bool headerflag_aa_; // Whether AA bit is set.
  168. bool headerflag_tc_; // Whether TC bit is set.
  169. };
  170. typedef boost::shared_ptr<MessageEntry> MessageEntryPtr;
  171. } // namespace cache
  172. } // namespace isc
  173. #endif // __MESSAGE_ENTRY_H