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