memory_datasrc.cc 12.9 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// 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.

Michal Vaner's avatar
Michal Vaner committed
15
#include <map>
Michal Vaner's avatar
Michal Vaner committed
16
#include <cassert>
Michal Vaner's avatar
Michal Vaner committed
17
#include <boost/shared_ptr.hpp>
Michal Vaner's avatar
Michal Vaner committed
18
#include <boost/bind.hpp>
Michal Vaner's avatar
Michal Vaner committed
19

20
#include <dns/name.h>
21
#include <dns/rrclass.h>
chenzhengzhang's avatar
chenzhengzhang committed
22
#include <dns/rrsetlist.h>
Michal Vaner's avatar
Michal Vaner committed
23
#include <dns/masterload.h>
24

25
#include <datasrc/memory_datasrc.h>
Michal Vaner's avatar
Michal Vaner committed
26
#include <datasrc/rbtree.h>
27
28
29
30
31
32
33

using namespace std;
using namespace isc::dns;

namespace isc {
namespace datasrc {

Michal Vaner's avatar
Michal Vaner committed
34
// Private data and hidden methods of MemoryZone
35
struct MemoryZone::MemoryZoneImpl {
Michal Vaner's avatar
Michal Vaner committed
36
    // Constructor
37
38
39
    MemoryZoneImpl(const RRClass& zone_class, const Name& origin) :
        zone_class_(zone_class), origin_(origin)
    {}
Michal Vaner's avatar
Michal Vaner committed
40
41

    // Information about the zone
42
43
    RRClass zone_class_;
    Name origin_;
44
    string file_name_;
Michal Vaner's avatar
Michal Vaner committed
45
46
47
48
49

    // Some type aliases
    /*
     * Each domain consists of some RRsets. They will be looked up by the
     * RRType.
50
51
52
53
54
55
56
     *
     * The use of map is questionable with regard to performance - there'll
     * be usually only few RRsets in the domain, so the log n benefit isn't
     * much and a vector/array might be faster due to its simplicity and
     * continuous memory location. But this is unlikely to be a performance
     * critical place and map has better interface for the lookups, so we use
     * that.
Michal Vaner's avatar
Michal Vaner committed
57
     */
Michal Vaner's avatar
Michal Vaner committed
58
    typedef map<RRType, ConstRRsetPtr> Domain;
Michal Vaner's avatar
Michal Vaner committed
59
    typedef Domain::value_type DomainPair;
Michal Vaner's avatar
Michal Vaner committed
60
    typedef boost::shared_ptr<Domain> DomainPtr;
Michal Vaner's avatar
Michal Vaner committed
61
    // The tree stores domains
Michal Vaner's avatar
Michal Vaner committed
62
63
    typedef RBTree<Domain> DomainTree;
    typedef RBNode<Domain> DomainNode;
Michal Vaner's avatar
Michal Vaner committed
64
    // The actual zone data
Michal Vaner's avatar
Michal Vaner committed
65
    DomainTree domains_;
Michal Vaner's avatar
Michal Vaner committed
66
67
68
69
70

    /*
     * Implementation of longer methods. We put them here, because the
     * access is without the impl_-> and it will get inlined anyway.
     */
Michal Vaner's avatar
Michal Vaner committed
71
    // Implementation of MemoryZone::add
Michal Vaner's avatar
Michal Vaner committed
72
    result::Result add(const ConstRRsetPtr& rrset, DomainTree* domains) {
Michal Vaner's avatar
Michal Vaner committed
73
74
75
76
        // Sanitize input
        if (!rrset) {
            isc_throw(NullRRset, "The rrset provided is NULL");
        }
77
78
79
80
81
82
83
84
85
        if (rrset->getType() == RRType::CNAME() &&
            rrset->getRdataCount() > 1) {
            // XXX: this is not only for CNAME.  We should generalize this
            // code for all other "singleton RR types" (such as SOA) in a
            // separate task.
            isc_throw(AddError, "multiple RRs of singleton type for "
                      << rrset->getName());
        }

Michal Vaner's avatar
Michal Vaner committed
86
87
88
89
90
91
92
93
        Name name(rrset->getName());
        NameComparisonResult compare(origin_.compare(name));
        if (compare.getRelation() != NameComparisonResult::SUPERDOMAIN &&
            compare.getRelation() != NameComparisonResult::EQUAL)
        {
            isc_throw(OutOfZone, "The name " << name <<
                " is not contained in zone " << origin_);
        }
Michal Vaner's avatar
Michal Vaner committed
94
95
        // Get the node
        DomainNode* node;
96
        switch (domains->insert(name, &node)) {
Michal Vaner's avatar
Michal Vaner committed
97
            // Just check it returns reasonable results
Michal Vaner's avatar
Michal Vaner committed
98
99
            case DomainTree::SUCCESS:
            case DomainTree::ALREADYEXISTS:
Michal Vaner's avatar
Michal Vaner committed
100
101
102
                break;
            // Something odd got out
            default:
Michal Vaner's avatar
Michal Vaner committed
103
                assert(0);
Michal Vaner's avatar
Michal Vaner committed
104
        }
105
        assert(node != NULL);
Michal Vaner's avatar
Michal Vaner committed
106
107
108
109
110
111
112
113
114
115
116

        // Now get the domain
        DomainPtr domain;
        // It didn't exist yet, create it
        if (node->isEmpty()) {
            domain.reset(new Domain);
            node->setData(domain);
        } else { // Get existing one
            domain = node->getData();
        }

117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
        // Ensure CNAME and other type of RR don't coexist for the same
        // owner name.
        // Note: when the check fails and the exception is thrown, it may
        // break strong exception guarantee.  At the moment we prefer
        // code simplicity and don't bother to introduce complicated
        // recovery code.
        if (rrset->getType() == RRType::CNAME()) {
            // XXX: this check will become incorrect when we support DNSSEC
            // (depending on how we support DNSSEC).  We should revisit it
            // at that point.
            if (!domain->empty()) {
                isc_throw(AddError, "CNAME can't be added with other data for "
                          << rrset->getName());
            }
        } else if (domain->find(RRType::CNAME()) != domain->end()) {
            isc_throw(AddError, "CNAME and " << rrset->getType() <<
                      " can't coexist for " << rrset->getName());
        }

Michal Vaner's avatar
Michal Vaner committed
136
137
138
        // Try inserting the rrset there
        if (domain->insert(DomainPair(rrset->getType(), rrset)).second) {
            // Ok, we just put it in
139
140
141
142
143
144
145
146
147

            // If this RRset creates a zone cut at this node, mark the node
            // indicating the need for callback in find().
            // TBD: handle DNAME, too
            if (rrset->getType() == RRType::NS() &&
                rrset->getName() != origin_) {
                node->enableCallback();
            }

Michal Vaner's avatar
Michal Vaner committed
148
149
150
151
152
153
            return (result::SUCCESS);
        } else {
            // The RRSet of given type was already there
            return (result::EXIST);
        }
    }
Michal Vaner's avatar
Michal Vaner committed
154

155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
    /*
     * Same as above, but it checks the return value and if it already exists,
     * it throws.
     */
    void addFromLoad(const ConstRRsetPtr& set, DomainTree* domains) {
            switch (add(set, domains)) {
                case result::EXIST:
                    isc_throw(dns::MasterLoadError, "Duplicate rrset: " <<
                        set->toText());
                case result::SUCCESS:
                    return;
                default:
                    assert(0);
            }
    }

171
172
173
174
175
176
    // Maintain intermediate data specific to the search context used in
    /// \c find().
    ///
    /// It will be passed to \c zonecutCallback() and record a possible
    /// zone cut node and related RRset (normally NS or DNAME).
    struct FindState {
177
178
179
180
181
182
        FindState(FindOptions options) : zonecut_node_(NULL),
                                         options_(options)
        {}
        const DomainNode* zonecut_node_;
        ConstRRsetPtr rrset_;
        const FindOptions options_;
183
184
185
186
187
188
189
    };

    // A callback called from possible zone cut nodes.  This will be passed
    // from the \c find() method to \c RBTree::find().
    static bool zonecutCallback(const DomainNode& node, FindState* state) {
        // We perform callback check only for the highest zone cut in the
        // rare case of nested zone cuts.
190
        if (state->zonecut_node_ != NULL) {
191
192
193
194
195
196
197
198
199
            return (false);
        }

        const Domain::const_iterator found(node.getData()->find(RRType::NS()));
        if (found != node.getData()->end()) {
            // BIND 9 checks if this node is not the origin.  But it cannot
            // be the origin because we don't enable the callback at the
            // origin node (see MemoryZoneImpl::add()).  Or should we do a
            // double check for it?
200
201
            state->zonecut_node_ = &node;
            state->rrset_ = found->second;
202
203
204
205

            // Unless glue is allowed the search stops here, so we return
            // false; otherwise return true to continue the search.
            return ((state->options_ & FIND_GLUE_OK) == 0);
206
207
        }

JINMEI Tatuya's avatar
JINMEI Tatuya committed
208
209
210
        // This case should not happen because we enable callback only
        // when we add an RR searched for above.
        assert(0);
211
212
213
        // This is here to avoid warning (therefore compilation error)
        // in case assert is turned off. Otherwise we could get "Control
        // reached end of non-void function".
214
        return (false);
215
216
    }

chenzhengzhang's avatar
chenzhengzhang committed
217

Michal Vaner's avatar
Michal Vaner committed
218
    // Implementation of MemoryZone::find
219
    FindResult find(const Name& name, RRType type,
220
                    RRsetList* target, const FindOptions options) const
221
    {
Michal Vaner's avatar
Michal Vaner committed
222
        // Get the node
Michal Vaner's avatar
Michal Vaner committed
223
        DomainNode* node(NULL);
224
        FindState state(options);
225
        switch (domains_.find(name, &node, zonecutCallback, &state)) {
Michal Vaner's avatar
Michal Vaner committed
226
            case DomainTree::PARTIALMATCH:
227
228
                if (state.zonecut_node_ != NULL) {
                    return (FindResult(DELEGATION, state.rrset_));
229
230
231
232
233
                }
                // TODO: we should also cover empty non-terminal cases, which
                // will require non trivial code and is deferred for later
                // development.  For now, we regard any partial match that
                // didn't hit a zone cut as "not found".
Michal Vaner's avatar
Michal Vaner committed
234
235
236
237
238
239
            case DomainTree::NOTFOUND:
                return (FindResult(NXDOMAIN, ConstRRsetPtr()));
            case DomainTree::EXACTMATCH: // This one is OK, handle it
                break;
            default:
                assert(0);
Michal Vaner's avatar
Michal Vaner committed
240
        }
Michal Vaner's avatar
Michal Vaner committed
241
242
243
        assert(node);
        assert(!node->isEmpty());

244
245
246
247
248
249
250
251
252
253
254
        Domain::const_iterator found;

        // If the node callback is enabled, this may be a zone cut.  If it
        // has a NS RR, we should return a delegation.
        if (node->isCallbackEnabled()) {
            found = node->getData()->find(RRType::NS());
            if (found != node->getData()->end()) {
                return (FindResult(DELEGATION, found->second));
            }
        }

chenzhengzhang's avatar
chenzhengzhang committed
255
        // handle type any query
256
        if (target != NULL && !node->getData()->empty()) {
257
258
259
260
261
262
            // Empty domain will be handled as NXRRSET by normal processing
            for (found = node->getData()->begin();
                 found != node->getData()->end(); found++)
            {
                target->addRRset(
                    boost::const_pointer_cast<RRset>(found->second));
chenzhengzhang's avatar
chenzhengzhang committed
263
            }
264
            return (FindResult(SUCCESS, ConstRRsetPtr()));
chenzhengzhang's avatar
chenzhengzhang committed
265
266
        }

267
        found = node->getData()->find(type);
Michal Vaner's avatar
Michal Vaner committed
268
269
270
271
        if (found != node->getData()->end()) {
            // Good, it is here
            return (FindResult(SUCCESS, found->second));
        } else {
272
            // Next, try CNAME.
273
274
275
276
            found = node->getData()->find(RRType::CNAME());
            if (found != node->getData()->end()) {
                return (FindResult(CNAME, found->second));
            }
Michal Vaner's avatar
Michal Vaner committed
277
        }
278
279
        // No exact match or CNAME.  Return NXRRSET.
        return (FindResult(NXRRSET, ConstRRsetPtr()));
Michal Vaner's avatar
Michal Vaner committed
280
    }
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
};

MemoryZone::MemoryZone(const RRClass& zone_class, const Name& origin) :
    impl_(new MemoryZoneImpl(zone_class, origin))
{
}

MemoryZone::~MemoryZone() {
    delete impl_;
}

const Name&
MemoryZone::getOrigin() const {
    return (impl_->origin_);
}

const RRClass&
MemoryZone::getClass() const {
    return (impl_->zone_class_);
}

Zone::FindResult
303
MemoryZone::find(const Name& name, const RRType& type,
304
                 RRsetList* target, const FindOptions options) const
chenzhengzhang's avatar
chenzhengzhang committed
305
{
306
    return (impl_->find(name, type, target, options));
chenzhengzhang's avatar
chenzhengzhang committed
307
308
}

Michal Vaner's avatar
Michal Vaner committed
309
310
result::Result
MemoryZone::add(const ConstRRsetPtr& rrset) {
311
    return (impl_->add(rrset, &impl_->domains_));
312
313
}

Michal Vaner's avatar
Michal Vaner committed
314
315
316

void
MemoryZone::load(const string& filename) {
317
318
319
320
321
    // Load it into a temporary tree
    MemoryZoneImpl::DomainTree tmp;
    masterLoad(filename.c_str(), getOrigin(), getClass(),
        boost::bind(&MemoryZoneImpl::addFromLoad, impl_, _1, &tmp));
    // If it went well, put it inside
322
    impl_->file_name_ = filename;
323
324
    tmp.swap(impl_->domains_);
    // And let the old data die with tmp
325
326
}

327
328
329
330
331
332
333
334
335
336
void
MemoryZone::swap(MemoryZone& zone) {
    std::swap(impl_, zone.impl_);
}

const string
MemoryZone::getFileName() const {
    return (impl_->file_name_);
}

337
338
339
340
341
342
/// Implementation details for \c MemoryDataSrc hidden from the public
/// interface.
///
/// For now, \c MemoryDataSrc only contains a \c ZoneTable object, which
/// consists of (pointers to) \c MemoryZone objects, we may add more
/// member variables later for new features.
343
344
345
346
class MemoryDataSrc::MemoryDataSrcImpl {
public:
    MemoryDataSrcImpl() : zone_count(0) {}
    unsigned int zone_count;
347
348
349
350
351
352
353
354
355
356
    ZoneTable zone_table;
};

MemoryDataSrc::MemoryDataSrc() : impl_(new MemoryDataSrcImpl)
{}

MemoryDataSrc::~MemoryDataSrc() {
    delete impl_;
}

357
358
359
360
361
unsigned int
MemoryDataSrc::getZoneCount() const {
    return (impl_->zone_count);
}

362
363
364
365
366
367
result::Result
MemoryDataSrc::addZone(ZonePtr zone) {
    if (!zone) {
        isc_throw(InvalidParameter,
                  "Null pointer is passed to MemoryDataSrc::addZone()");
    }
368
369
370
371
372
373

    const result::Result result = impl_->zone_table.addZone(zone);
    if (result == result::SUCCESS) {
        ++impl_->zone_count;
    }
    return (result);
374
375
376
377
378
379
380
381
382
}

MemoryDataSrc::FindResult
MemoryDataSrc::findZone(const isc::dns::Name& name) const {
    return (FindResult(impl_->zone_table.findZone(name).code,
                       impl_->zone_table.findZone(name).zone));
}
} // end of namespace datasrc
} // end of namespace dns