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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
|
// Copyright (C) 2012 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 <datasrc/master_loader_callbacks.h>
#include <datasrc/memory/zone_data_loader.h>
#include <datasrc/memory/zone_data_updater.h>
#include <datasrc/memory/logger.h>
#include <datasrc/memory/segment_object_holder.h>
#include <datasrc/memory/util_internal.h>
#include <datasrc/memory/rrset_collection.h>
#include <dns/master_loader.h>
#include <dns/rrcollator.h>
#include <dns/rdataclass.h>
#include <dns/rrset.h>
#include <dns/zone_checker.h>
#include <boost/foreach.hpp>
#include <boost/bind.hpp>
#include <boost/noncopyable.hpp>
#include <map>
using namespace isc::dns;
using namespace isc::dns::rdata;
namespace isc {
namespace datasrc {
namespace memory {
using detail::SegmentObjectHolder;
using detail::getCoveredType;
namespace { // unnamed namespace
// A functor type used for loading.
typedef boost::function<void(isc::dns::ConstRRsetPtr)> LoadCallback;
// A helper internal class for \c loadZoneData(). make it non-copyable
// to avoid accidental copy.
//
// The current internal implementation no longer expects that both a
// normal (non RRSIG) RRset and (when signed) its RRSIG are added at
// once, but we do that here anyway to avoid merging RdataSets every
// single time which can be inefficient.
//
// We hold all RRsets of the same owner name in node_rrsets_ and
// node_rrsigsets_, and add the matching pairs of RRsets to the zone
// when we see a new owner name. We do this to limit the size of
// NodeRRsets below. However, RRsets can occur in any order.
//
// The caller is responsible for adding the RRsets of the last group
// in the input sequence by explicitly calling flushNodeRRsets() at the
// end. It's cleaner and more robust if we let the destructor of this class
// do it, but since we cannot guarantee the adding operation is exception free,
// we don't choose that option to maintain the common expectation for
// destructors.
class ZoneDataLoader : boost::noncopyable {
public:
ZoneDataLoader(util::MemorySegment& mem_sgmt,
const isc::dns::RRClass rrclass,
const isc::dns::Name& zone_name, ZoneData& zone_data) :
updater_(mem_sgmt, rrclass, zone_name, zone_data)
{}
void addFromLoad(const isc::dns::ConstRRsetPtr& rrset);
void flushNodeRRsets();
private:
typedef std::map<isc::dns::RRType, isc::dns::ConstRRsetPtr> NodeRRsets;
typedef NodeRRsets::value_type NodeRRsetsVal;
// A helper to identify the covered type of an RRSIG.
const isc::dns::Name& getCurrentName() const;
private:
NodeRRsets node_rrsets_;
NodeRRsets node_rrsigsets_;
std::vector<isc::dns::ConstRRsetPtr> non_consecutive_rrsets_;
ZoneDataUpdater updater_;
};
void
ZoneDataLoader::addFromLoad(const ConstRRsetPtr& rrset) {
// If we see a new name, flush the temporary holders, adding the
// pairs of RRsets and RRSIGs of the previous name to the zone.
if ((!node_rrsets_.empty() || !node_rrsigsets_.empty() ||
!non_consecutive_rrsets_.empty()) &&
(getCurrentName() != rrset->getName())) {
flushNodeRRsets();
}
// Store this RRset until it can be added to the zone. If an rrtype
// that's already been seen is found, queue it in a different vector
// to be merged later.
const bool is_rrsig = rrset->getType() == RRType::RRSIG();
NodeRRsets& node_rrsets = is_rrsig ? node_rrsigsets_ : node_rrsets_;
const RRType& rrtype = is_rrsig ? getCoveredType(rrset) : rrset->getType();
if (!node_rrsets.insert(NodeRRsetsVal(rrtype, rrset)).second) {
non_consecutive_rrsets_.insert(non_consecutive_rrsets_.begin(), rrset);
}
if (rrset->getRRsig()) {
addFromLoad(rrset->getRRsig());
}
}
void
ZoneDataLoader::flushNodeRRsets() {
BOOST_FOREACH(NodeRRsetsVal val, node_rrsets_) {
// Identify the corresponding RRSIG for the RRset, if any. If
// found add both the RRset and its RRSIG at once.
ConstRRsetPtr sig_rrset;
NodeRRsets::iterator sig_it = node_rrsigsets_.find(val.first);
if (sig_it != node_rrsigsets_.end()) {
sig_rrset = sig_it->second;
node_rrsigsets_.erase(sig_it);
}
updater_.add(val.second, sig_rrset);
}
// Normally rrsigsets map should be empty at this point, but it's still
// possible that an RRSIG that doesn't have covered RRset is added; they
// still remain in the map. We add them to the zone separately.
BOOST_FOREACH(NodeRRsetsVal val, node_rrsigsets_) {
updater_.add(ConstRRsetPtr(), val.second);
}
// Add any non-consecutive rrsets too.
BOOST_FOREACH(ConstRRsetPtr rrset, non_consecutive_rrsets_) {
if (rrset->getType() == RRType::RRSIG()) {
updater_.add(ConstRRsetPtr(), rrset);
} else {
updater_.add(rrset, ConstRRsetPtr());
}
}
node_rrsets_.clear();
node_rrsigsets_.clear();
non_consecutive_rrsets_.clear();
}
const Name&
ZoneDataLoader::getCurrentName() const {
if (!node_rrsets_.empty()) {
return (node_rrsets_.begin()->second->getName());
}
assert(!node_rrsigsets_.empty());
return (node_rrsigsets_.begin()->second->getName());
}
void
logWarning(const dns::Name* zone_name, const dns::RRClass* rrclass,
const std::string& reason)
{
LOG_WARN(logger, DATASRC_MEMORY_CHECK_WARNING).arg(*zone_name).
arg(*rrclass).arg(reason);
}
void
logError(const dns::Name* zone_name, const dns::RRClass* rrclass,
const std::string& reason)
{
LOG_ERROR(logger, DATASRC_MEMORY_CHECK_ERROR).arg(*zone_name).arg(*rrclass).
arg(reason);
}
ZoneData*
loadZoneDataInternal(util::MemorySegment& mem_sgmt,
const isc::dns::RRClass& rrclass,
const Name& zone_name,
boost::function<void(LoadCallback)> rrset_installer)
{
while (true) { // Try as long as it takes to load and grow the segment
bool created = false;
try {
SegmentObjectHolder<ZoneData, RRClass> holder(mem_sgmt, rrclass);
holder.set(ZoneData::create(mem_sgmt, zone_name));
// Nothing from this point on should throw MemorySegmentGrown.
// It is handled inside here.
created = true;
ZoneDataLoader loader(mem_sgmt, rrclass, zone_name, *holder.get());
rrset_installer(boost::bind(&ZoneDataLoader::addFromLoad, &loader,
_1));
// Add any last RRsets that were left
loader.flushNodeRRsets();
const ZoneNode* origin_node = holder.get()->getOriginNode();
const RdataSet* rdataset = origin_node->getData();
// If the zone is NSEC3-signed, check if it has NSEC3PARAM
if (holder.get()->isNSEC3Signed()) {
if (RdataSet::find(rdataset, RRType::NSEC3PARAM()) == NULL) {
LOG_WARN(logger, DATASRC_MEMORY_MEM_NO_NSEC3PARAM).
arg(zone_name).arg(rrclass);
}
}
RRsetCollection collection(*(holder.get()), rrclass);
const dns::ZoneCheckerCallbacks
callbacks(boost::bind(&logError, &zone_name, &rrclass, _1),
boost::bind(&logWarning, &zone_name, &rrclass, _1));
if (!dns::checkZone(zone_name, rrclass, collection, callbacks)) {
isc_throw(ZoneValidationError,
"Errors found when validating zone: "
<< zone_name << "/" << rrclass);
}
return (holder.release());
} catch (const util::MemorySegmentGrown&) {
assert(!created);
}
}
}
// A wrapper for dns::MasterLoader used by loadZoneData() below. Essentially
// it converts the two callback types. Note the mostly redundant wrapper of
// boost::bind. It converts function<void(ConstRRsetPtr)> to
// function<void(RRsetPtr)> (MasterLoader expects the latter). SunStudio
// doesn't seem to do this conversion if we just pass 'callback'.
void
masterLoaderWrapper(const char* const filename, const Name& origin,
const RRClass& zone_class, LoadCallback callback)
{
bool load_ok = false; // (we don't use it)
dns::RRCollator collator(boost::bind(callback, _1));
try {
dns::MasterLoader(filename, origin, zone_class,
createMasterLoaderCallbacks(origin, zone_class,
&load_ok),
collator.getCallback()).load();
collator.flush();
} catch (const dns::MasterLoaderError& e) {
isc_throw(ZoneLoaderException, e.what());
}
}
// The installer called from the iterator version of loadZoneData().
void
generateRRsetFromIterator(ZoneIterator* iterator, LoadCallback callback) {
ConstRRsetPtr rrset;
while ((rrset = iterator->getNextRRset()) != NULL) {
callback(rrset);
}
}
} // end of unnamed namespace
ZoneData*
loadZoneData(util::MemorySegment& mem_sgmt,
const isc::dns::RRClass& rrclass,
const isc::dns::Name& zone_name,
const std::string& zone_file)
{
LOG_DEBUG(logger, DBG_TRACE_BASIC, DATASRC_MEMORY_MEM_LOAD_FROM_FILE).
arg(zone_name).arg(rrclass).arg(zone_file);
return (loadZoneDataInternal(mem_sgmt, rrclass, zone_name,
boost::bind(masterLoaderWrapper,
zone_file.c_str(),
zone_name, rrclass,
_1)));
}
ZoneData*
loadZoneData(util::MemorySegment& mem_sgmt,
const isc::dns::RRClass& rrclass,
const isc::dns::Name& zone_name,
ZoneIterator& iterator)
{
LOG_DEBUG(logger, DBG_TRACE_BASIC, DATASRC_MEMORY_MEM_LOAD_FROM_DATASRC).
arg(zone_name).arg(rrclass);
return (loadZoneDataInternal(mem_sgmt, rrclass, zone_name,
boost::bind(generateRRsetFromIterator,
&iterator, _1)));
}
} // namespace memory
} // namespace datasrc
} // namespace isc
|