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. /// \brief generate one dns message according
  69. /// the rrsets information of the message.
  70. ///
  71. /// \param time_now set the ttl of each rrset in the message
  72. /// as "expire_time - time_now" (expire_time is the
  73. /// expiration time of the rrset).
  74. /// \param response generated dns message.
  75. /// \return return true if the response message can be generated
  76. /// from the cached information, or else, return false.
  77. bool genMessage(const time_t& time_now, isc::dns::Message& response);
  78. /// \brief Get the hash key of the message entry.
  79. ///
  80. /// \return return hash key
  81. virtual HashKey hashKey() const {
  82. return (*hash_key_ptr_);
  83. }
  84. /// \brief Get expire time of the message entry.
  85. /// \return return the expire time of message entry.
  86. time_t getExpireTime() const {
  87. return (expire_time_);
  88. }
  89. /// \short Protected memebers, so they can be accessed by tests.
  90. //@{
  91. protected:
  92. /// \brief Initialize the message entry with dns message.
  93. ///
  94. /// \param message The Message to initialize the entry with
  95. void initMessageEntry(const isc::dns::Message& message);
  96. /// \brief Parse the rrsets in specified section.
  97. ///
  98. /// \param msg The message to parse the RRsets from
  99. /// \param section The Section to parse the RRsets from
  100. /// \param smaller_ttl Get the smallest ttl of rrsets in
  101. /// specified section, if it's smaller than the given value.
  102. /// \param rrset_count the rrset count of the section.
  103. /// (TODO for Message, getRRsetCount() should be one
  104. /// interface provided by Message.)
  105. void parseSection(const isc::dns::Message& msg,
  106. const isc::dns::Message::Section& section,
  107. uint32_t& smaller_ttl,
  108. uint16_t& rrset_count);
  109. /// \brief Parse the RRsets in the authority section of
  110. /// negative response. The SOA RRset need to be located and
  111. /// stored in a seperate cache
  112. /// \param msg The message to parse the RRsets from
  113. /// \param min_ttl Get the minimum ttl of rrset in the authority section
  114. /// \param rrset_count the rrset count of the authority section
  115. void parseNegativeResponseAuthoritySection(const isc::dns::Message& msg,
  116. uint32_t& min_ttl,
  117. uint16_t& rrset_count);
  118. /// \brief Get RRset Trustworthiness
  119. /// The algorithm refers to RFC2181 section 5.4.1
  120. /// Only the rrset can be updated by the rrsets
  121. /// with higher trust level.
  122. ///
  123. /// \param message Message that the rrset belongs to
  124. /// \param rrset specified rrset which needs to get its
  125. /// trust worthiness
  126. /// \param section Section of the rrset
  127. /// \return return rrset trust level.
  128. RRsetTrustLevel getRRsetTrustLevel(const isc::dns::Message& message,
  129. const isc::dns::RRsetPtr& rrset,
  130. const isc::dns::Message::Section& section);
  131. /// \brief Add rrset to one section of message.
  132. ///
  133. /// \param message The message to add rrsets to.
  134. /// \param rrset_entry_vec vector for rrset entries in
  135. /// different sections.
  136. /// \param section The section to add to
  137. /// \param dnssec_need need dnssec records or not.
  138. void addRRset(isc::dns::Message& message,
  139. const std::vector<RRsetEntryPtr>& rrset_entry_vec,
  140. const isc::dns::Message::Section& section,
  141. bool dnssec_need);
  142. /// \brief Get the all the rrset entries for the message entry.
  143. ///
  144. /// \param rrset_entry_vec vector to add unexpired rrset entries to
  145. /// \param time_now the time of now. Used to compare with rrset
  146. /// entry's expire time.
  147. /// \return return false if any rrset entry has expired, true
  148. /// otherwise.
  149. bool getRRsetEntries(std::vector<RRsetEntryPtr>& rrset_entry_vec,
  150. const time_t time_now);
  151. time_t expire_time_; // Expiration time of the message.
  152. //@}
  153. private:
  154. std::string entry_name_; // The name for this entry(name + type)
  155. HashKey* hash_key_ptr_; // the key for messag entry in hash table.
  156. std::vector<RRsetRef> rrsets_;
  157. RRsetCachePtr rrset_cache_; //Normal rrset cache
  158. // SOA rrset from negative response
  159. RRsetCachePtr negative_soa_cache_;
  160. std::string query_name_; // query name of the message.
  161. uint16_t query_class_; // query class of the message.
  162. uint16_t query_type_; // query type of message.
  163. uint16_t query_count_; // query count in query section.
  164. uint16_t answer_count_; // rrset count in answer section.
  165. uint16_t authority_count_; // rrset count in authority section.
  166. uint16_t additional_count_; // rrset count in addition section.
  167. //TODO, there should be a better way to cache these header flags
  168. bool headerflag_aa_; // Whether AA bit is set.
  169. bool headerflag_tc_; // Whether TC bit is set.
  170. };
  171. typedef boost::shared_ptr<MessageEntry> MessageEntryPtr;
  172. } // namespace cache
  173. } // namespace isc
  174. #endif // __MESSAGE_ENTRY_H