message_entry.h 7.0 KB

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