1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
|
// Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
#include <config.h>
#include "resolver_cache.h"
#include "dns/message.h"
#include "rrset_cache.h"
#include "logger.h"
#include <string>
#include <algorithm>
using namespace isc::dns;
using namespace std;
namespace isc {
namespace cache {
ResolverClassCache::ResolverClassCache(const RRClass& cache_class) :
cache_class_(cache_class)
{
LOG_DEBUG(logger, DBG_TRACE_BASIC, CACHE_RESOLVER_INIT).arg(cache_class);
local_zone_data_ = LocalZoneDataPtr(new LocalZoneData(cache_class_.getCode()));
rrsets_cache_ = RRsetCachePtr(new RRsetCache(RRSET_CACHE_DEFAULT_SIZE,
cache_class_.getCode()));
// SOA rrset cache from negative response
negative_soa_cache_ = RRsetCachePtr(new RRsetCache(NEGATIVE_RRSET_CACHE_DEFAULT_SIZE,
cache_class_.getCode()));
messages_cache_ = MessageCachePtr(new MessageCache(rrsets_cache_,
MESSAGE_CACHE_DEFAULT_SIZE,
cache_class_.getCode(),
negative_soa_cache_));
}
ResolverClassCache::ResolverClassCache(const CacheSizeInfo& cache_info) :
cache_class_(cache_info.cclass)
{
LOG_DEBUG(logger, DBG_TRACE_BASIC, CACHE_RESOLVER_INIT_INFO).
arg(cache_class_);
uint16_t klass = cache_class_.getCode();
// TODO We should find one way to load local zone data.
local_zone_data_ = LocalZoneDataPtr(new LocalZoneData(klass));
rrsets_cache_ = RRsetCachePtr(new
RRsetCache(cache_info.rrset_cache_size, klass));
// SOA rrset cache from negative response
negative_soa_cache_ = RRsetCachePtr(new RRsetCache(cache_info.rrset_cache_size,
klass));
messages_cache_ = MessageCachePtr(new MessageCache(rrsets_cache_,
cache_info.message_cache_size,
klass, negative_soa_cache_));
}
const RRClass&
ResolverClassCache::getClass() const {
return (cache_class_);
}
bool
ResolverClassCache::lookup(const isc::dns::Name& qname,
const isc::dns::RRType& qtype,
isc::dns::Message& response) const
{
LOG_DEBUG(logger, DBG_TRACE_DATA, CACHE_RESOLVER_LOOKUP_MSG).
arg(qname).arg(qtype);
// message response should has question section already.
if (response.beginQuestion() == response.endQuestion()) {
LOG_ERROR(logger, CACHE_RESOLVER_NO_QUESTION).arg(qname).arg(qtype);
isc_throw(MessageNoQuestionSection, "Message has no question section");
}
// First, query in local zone, if the rrset(qname, qtype, qclass) can be
// found in local zone, generated reply message with only the rrset in
// answer section.
RRsetPtr rrset_ptr = local_zone_data_->lookup(qname, qtype);
if (rrset_ptr) {
LOG_DEBUG(logger, DBG_TRACE_DATA, CACHE_RESOLVER_LOCAL_MSG).
arg(qname).arg(qtype);
response.addRRset(Message::SECTION_ANSWER, rrset_ptr);
return (true);
}
// Search in class-specific message cache.
return (messages_cache_->lookup(qname, qtype, response));
}
isc::dns::RRsetPtr
ResolverClassCache::lookup(const isc::dns::Name& qname,
const isc::dns::RRType& qtype) const
{
LOG_DEBUG(logger, DBG_TRACE_DATA, CACHE_RESOLVER_LOOKUP_RRSET).
arg(qname).arg(qtype);
// Algorithm:
// 1. Search in local zone data first,
// 2. Then do search in rrsets_cache_.
RRsetPtr rrset_ptr = local_zone_data_->lookup(qname, qtype);
if (rrset_ptr) {
LOG_DEBUG(logger, DBG_TRACE_DATA, CACHE_RESOLVER_LOCAL_RRSET).
arg(qname).arg(qtype);
return (rrset_ptr);
} else {
RRsetEntryPtr rrset_entry = rrsets_cache_->lookup(qname, qtype);
if (rrset_entry) {
return (rrset_entry->getRRset());
} else {
return (RRsetPtr());
}
}
}
bool
ResolverClassCache::update(const isc::dns::Message& msg) {
LOG_DEBUG(logger, DBG_TRACE_DATA, CACHE_RESOLVER_UPDATE_MSG).
arg((*msg.beginQuestion())->getName()).
arg((*msg.beginQuestion())->getType()).
arg((*msg.beginQuestion())->getClass());
return (messages_cache_->update(msg));
}
bool
ResolverClassCache::updateRRsetCache(const isc::dns::ConstRRsetPtr& rrset_ptr,
RRsetCachePtr rrset_cache_ptr)
{
RRsetTrustLevel level;
if (rrset_ptr->getType() == RRType::A() ||
rrset_ptr->getType() == RRType::AAAA()) {
level = RRSET_TRUST_PRIM_GLUE;
} else {
level = RRSET_TRUST_PRIM_ZONE_NONGLUE;
}
rrset_cache_ptr->update((*rrset_ptr.get()), level);
return (true);
}
bool
ResolverClassCache::update(const isc::dns::ConstRRsetPtr& rrset_ptr) {
LOG_DEBUG(logger, DBG_TRACE_DATA, CACHE_RESOLVER_UPDATE_RRSET).
arg(rrset_ptr->getName()).arg(rrset_ptr->getType()).
arg(rrset_ptr->getClass());
// First update local zone, then update rrset cache.
local_zone_data_->update((*rrset_ptr.get()));
updateRRsetCache(rrset_ptr, rrsets_cache_);
return (true);
}
ResolverCache::ResolverCache()
{
class_caches_.push_back(new ResolverClassCache(RRClass::IN()));
}
ResolverCache::ResolverCache(std::vector<CacheSizeInfo> caches_info)
{
for (std::vector<CacheSizeInfo>::size_type i = 0;
i < caches_info.size(); ++i) {
class_caches_.push_back(new ResolverClassCache(caches_info[i]));
}
}
ResolverCache::~ResolverCache()
{
for (std::vector<ResolverClassCache*>::size_type i = 0;
i < class_caches_.size(); ++i) {
delete class_caches_[i];
}
}
bool
ResolverCache::lookup(const isc::dns::Name& qname,
const isc::dns::RRType& qtype,
const isc::dns::RRClass& qclass,
isc::dns::Message& response) const
{
ResolverClassCache* cc = getClassCache(qclass);
if (cc) {
return (cc->lookup(qname, qtype, response));
} else {
LOG_DEBUG(logger, DBG_TRACE_DATA, CACHE_RESOLVER_UNKNOWN_CLASS_MSG).
arg(qclass);
return (false);
}
}
isc::dns::RRsetPtr
ResolverCache::lookup(const isc::dns::Name& qname,
const isc::dns::RRType& qtype,
const isc::dns::RRClass& qclass) const
{
ResolverClassCache* cc = getClassCache(qclass);
if (cc) {
return (cc->lookup(qname, qtype));
} else {
LOG_DEBUG(logger, DBG_TRACE_DATA, CACHE_RESOLVER_UNKNOWN_CLASS_RRSET).
arg(qclass);
return (RRsetPtr());
}
}
isc::dns::RRsetPtr
ResolverCache::lookupDeepestNS(const isc::dns::Name& qname,
const isc::dns::RRClass& qclass) const
{
LOG_DEBUG(logger, DBG_TRACE_DATA, CACHE_RESOLVER_DEEPEST).arg(qname).
arg(qclass);
isc::dns::RRType qtype = RRType::NS();
ResolverClassCache* cc = getClassCache(qclass);
if (cc) {
unsigned int count = qname.getLabelCount();
unsigned int level = 0;
while(level < count) {
Name close_name = qname.split(level);
RRsetPtr rrset_ptr = cc->lookup(close_name, qtype);
if (rrset_ptr) {
return (rrset_ptr);
} else {
++level;
}
}
}
return (RRsetPtr());
}
bool
ResolverCache::update(const isc::dns::Message& msg) {
QuestionIterator iter = msg.beginQuestion();
ResolverClassCache* cc = getClassCache((*iter)->getClass());
if (cc) {
return (cc->update(msg));
} else {
LOG_DEBUG(logger, DBG_TRACE_DATA,
CACHE_RESOLVER_UPDATE_UNKNOWN_CLASS_MSG).
arg((*msg.beginQuestion())->getClass());
return (false);
}
}
bool
ResolverCache::update(const isc::dns::ConstRRsetPtr& rrset_ptr) {
ResolverClassCache* cc = getClassCache(rrset_ptr->getClass());
if (cc) {
return (cc->update(rrset_ptr));
} else {
LOG_DEBUG(logger, DBG_TRACE_DATA,
CACHE_RESOLVER_UPDATE_UNKNOWN_CLASS_RRSET).
arg(rrset_ptr->getClass());
return (false);
}
}
ResolverClassCache*
ResolverCache::getClassCache(const isc::dns::RRClass& cache_class) const {
for (std::vector<ResolverClassCache*>::size_type i = 0;
i < class_caches_.size(); ++i) {
if (class_caches_[i]->getClass() == cache_class) {
return (class_caches_[i]);
}
}
return (NULL);
}
} // namespace cache
} // namespace isc
|