memory_datasrc.cc 28.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
#include <datasrc/logger.h>
28
29
30
31
32
33
34

using namespace std;
using namespace isc::dns;

namespace isc {
namespace datasrc {

35
36
// Private data and hidden methods of InMemoryZoneFinder
struct InMemoryZoneFinder::InMemoryZoneFinderImpl {
Michal Vaner's avatar
Michal Vaner committed
37
    // Constructor
38
    InMemoryZoneFinderImpl(const RRClass& zone_class, const Name& origin) :
39
        zone_class_(zone_class), origin_(origin), origin_data_(NULL),
40
        domains_(true)
41
42
43
44
45
46
    {
        // We create the node for origin (it needs to exist anyway in future)
        domains_.insert(origin, &origin_data_);
        DomainPtr origin_domain(new Domain);
        origin_data_->setData(origin_domain);
    }
Michal Vaner's avatar
Michal Vaner committed
47
48
49
50
51

    // Some type aliases
    /*
     * Each domain consists of some RRsets. They will be looked up by the
     * RRType.
52
53
54
55
56
57
58
     *
     * 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
59
     */
Michal Vaner's avatar
Michal Vaner committed
60
    typedef map<RRType, ConstRRsetPtr> Domain;
Michal Vaner's avatar
Michal Vaner committed
61
    typedef Domain::value_type DomainPair;
Michal Vaner's avatar
Michal Vaner committed
62
    typedef boost::shared_ptr<Domain> DomainPtr;
Michal Vaner's avatar
Michal Vaner committed
63
    // The tree stores domains
Michal Vaner's avatar
Michal Vaner committed
64
65
    typedef RBTree<Domain> DomainTree;
    typedef RBNode<Domain> DomainNode;
66
    static const DomainNode::Flags DOMAINFLAG_WILD = DomainNode::FLAG_USER1;
67
68
69
70
71
72
73

    // Information about the zone
    RRClass zone_class_;
    Name origin_;
    DomainNode* origin_data_;
    string file_name_;

Michal Vaner's avatar
Michal Vaner committed
74
    // The actual zone data
Michal Vaner's avatar
Michal Vaner committed
75
    DomainTree domains_;
Michal Vaner's avatar
Michal Vaner committed
76

77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
    // Add the necessary magic for any wildcard contained in 'name'
    // (including itself) to be found in the zone.
    //
    // In order for wildcard matching to work correctly in find(),
    // we must ensure that a node for the wildcarding level exists in the
    // backend RBTree.
    // E.g. if the wildcard name is "*.sub.example." then we must ensure
    // that "sub.example." exists and is marked as a wildcard level.
    // Note: the "wildcarding level" is for the parent name of the wildcard
    // name (such as "sub.example.").
    //
    // We also perform the same trick for empty wild card names possibly
    // contained in 'name' (e.g., '*.foo.example' in 'bar.*.foo.example').
    void addWildcards(DomainTree& domains, const Name& name) {
        Name wname(name);
        const unsigned int labels(wname.getLabelCount());
        const unsigned int origin_labels(origin_.getLabelCount());
        for (unsigned int l = labels;
             l > origin_labels;
             --l, wname = wname.split(1)) {
            if (wname.isWildcard()) {
98
                LOG_DEBUG(logger, DBG_TRACE_DATA, DATASRC_MEM_ADD_WILDCARD).
99
                    arg(name);
100
101
                // Ensure a separate level exists for the "wildcarding" name,
                // and mark the node as "wild".
102
103
104
                DomainNode* node;
                DomainTree::Result result(domains.insert(wname.split(1),
                                                         &node));
105
106
                assert(result == DomainTree::SUCCESS ||
                       result == DomainTree::ALREADYEXISTS);
107
                node->setFlag(DOMAINFLAG_WILD);
108

109
110
111
112
                // Ensure a separate level exists for the wildcard name.
                // Note: for 'name' itself we do this later anyway, but the
                // overhead should be marginal because wildcard names should
                // be rare.
113
114
115
116
117
118
119
                result = domains.insert(wname, &node);
                assert(result == DomainTree::SUCCESS ||
                       result == DomainTree::ALREADYEXISTS);
            }
        }
    }

120
121
122
123
124
125
126
    /*
     * Does some checks in context of the data that are already in the zone.
     * Currently checks for forbidden combinations of RRsets in the same
     * domain (CNAME+anything, DNAME+NS).
     *
     * If such condition is found, it throws AddError.
     */
127
128
    void contextCheck(const ConstRRsetPtr& rrset,
                      const DomainPtr& domain) const {
129
130
131
        // Ensure CNAME and other type of RR don't coexist for the same
        // owner name.
        if (rrset->getType() == RRType::CNAME()) {
132
            // TODO: this check will become incorrect when we support DNSSEC
133
134
135
            // (depending on how we support DNSSEC).  We should revisit it
            // at that point.
            if (!domain->empty()) {
136
                LOG_ERROR(logger, DATASRC_MEM_CNAME_TO_NONEMPTY).
137
                    arg(rrset->getName());
138
139
140
141
                isc_throw(AddError, "CNAME can't be added with other data for "
                          << rrset->getName());
            }
        } else if (domain->find(RRType::CNAME()) != domain->end()) {
142
            LOG_ERROR(logger, DATASRC_MEM_CNAME_COEXIST).arg(rrset->getName());
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
            isc_throw(AddError, "CNAME and " << rrset->getType() <<
                      " can't coexist for " << rrset->getName());
        }

        /*
         * Similar with DNAME, but it must not coexist only with NS and only in
         * non-apex domains.
         * RFC 2672 section 3 mentions that it is implied from it and RFC 2181
         */
        if (rrset->getName() != origin_ &&
            // Adding DNAME, NS already there
            ((rrset->getType() == RRType::DNAME() &&
            domain->find(RRType::NS()) != domain->end()) ||
            // Adding NS, DNAME already there
            (rrset->getType() == RRType::NS() &&
            domain->find(RRType::DNAME()) != domain->end())))
        {
160
            LOG_ERROR(logger, DATASRC_MEM_DNAME_NS).arg(rrset->getName());
161
162
163
164
165
            isc_throw(AddError, "DNAME can't coexist with NS in non-apex "
                "domain " << rrset->getName());
        }
    }

166
167
    // Validate rrset before adding it to the zone.  If something is wrong
    // it throws an exception.  It doesn't modify the zone, and provides
JINMEI Tatuya's avatar
JINMEI Tatuya committed
168
    // the strong exception guarantee.
169
    void addValidation(const ConstRRsetPtr rrset) {
Michal Vaner's avatar
Michal Vaner committed
170
171
172
        if (!rrset) {
            isc_throw(NullRRset, "The rrset provided is NULL");
        }
173
174
        // Check for singleton RRs. It should probably handled at a different
        // in future.
175
176
177
178
179
180
        if ((rrset->getType() == RRType::CNAME() ||
            rrset->getType() == RRType::DNAME()) &&
            rrset->getRdataCount() > 1)
        {
            // XXX: this is not only for CNAME or DNAME. We should generalize
            // this code for all other "singleton RR types" (such as SOA) in a
181
            // separate task.
182
            LOG_ERROR(logger, DATASRC_MEM_SINGLETON).arg(rrset->getName()).
183
                arg(rrset->getType());
184
185
186
187
            isc_throw(AddError, "multiple RRs of singleton type for "
                      << rrset->getName());
        }

188
        NameComparisonResult compare(origin_.compare(rrset->getName()));
Michal Vaner's avatar
Michal Vaner committed
189
190
191
        if (compare.getRelation() != NameComparisonResult::SUPERDOMAIN &&
            compare.getRelation() != NameComparisonResult::EQUAL)
        {
192
            LOG_ERROR(logger, DATASRC_MEM_OUT_OF_ZONE).arg(rrset->getName()).
193
                arg(origin_);
194
            isc_throw(OutOfZone, "The name " << rrset->getName() <<
Michal Vaner's avatar
Michal Vaner committed
195
196
                " is not contained in zone " << origin_);
        }
197
198
199
200
201
202
203
204
205

        // Some RR types do not really work well with a wildcard.
        // Even though the protocol specifically doesn't completely ban such
        // usage, we refuse to load a zone containing such RR in order to
        // keep the lookup logic simpler and more predictable.
        // See RFC4592 and (for DNAME) draft-ietf-dnsext-rfc2672bis-dname
        // for more technical background.  Note also that BIND 9 refuses
        // NS at a wildcard, so in that sense we simply provide compatible
        // behavior.
206
        if (rrset->getName().isWildcard()) {
207
            if (rrset->getType() == RRType::NS()) {
208
209
                LOG_ERROR(logger, DATASRC_MEM_WILDCARD_NS).
                    arg(rrset->getName());
210
                isc_throw(AddError, "Invalid NS owner name (wildcard): " <<
211
                          rrset->getName());
212
213
            }
            if (rrset->getType() == RRType::DNAME()) {
214
215
                LOG_ERROR(logger, DATASRC_MEM_WILDCARD_DNAME).
                    arg(rrset->getName());
216
                isc_throw(AddError, "Invalid DNAME owner name (wildcard): " <<
217
                          rrset->getName());
218
219
            }
        }
220
221
222
223
224
225
    }

    /*
     * Implementation of longer methods. We put them here, because the
     * access is without the impl_-> and it will get inlined anyway.
     */
226
    // Implementation of InMemoryZoneFinder::add
227
    result::Result add(const ConstRRsetPtr& rrset, DomainTree* domains) {
228
229
230
231
232
        // Sanitize input.  This will cause an exception to be thrown
        // if the input RRset is empty.
        addValidation(rrset);

        // OK, can add the RRset.
233
234
        LOG_DEBUG(logger, DBG_TRACE_DATA, DATASRC_MEM_ADD_RRSET).
            arg(rrset->getName()).arg(rrset->getType()).arg(origin_);
235
236
237

        // Add wildcards possibly contained in the owner name to the domain
        // tree.
238
239
        // Note: this can throw an exception, breaking strong exception
        // guarantee.  (see also the note for contextCheck() below).
240
        addWildcards(*domains, rrset->getName());
241

Michal Vaner's avatar
Michal Vaner committed
242
243
        // Get the node
        DomainNode* node;
244
245
246
247
        DomainTree::Result result = domains->insert(rrset->getName(), &node);
        // Just check it returns reasonable results
        assert((result == DomainTree::SUCCESS ||
                result == DomainTree::ALREADYEXISTS) && node!= NULL);
Michal Vaner's avatar
Michal Vaner committed
248
249
250
251
252
253
254
255
256
257
258

        // 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();
        }

259
        // Checks related to the surrounding data.
260
261
262
263
        // 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.
264
        contextCheck(rrset, domain);
265

Michal Vaner's avatar
Michal Vaner committed
266
267
268
        // Try inserting the rrset there
        if (domain->insert(DomainPair(rrset->getType(), rrset)).second) {
            // Ok, we just put it in
269
270
271
272
273

            // If this RRset creates a zone cut at this node, mark the node
            // indicating the need for callback in find().
            if (rrset->getType() == RRType::NS() &&
                rrset->getName() != origin_) {
274
                node->setFlag(DomainNode::FLAG_CALLBACK);
275
276
            // If it is DNAME, we have a callback as well here
            } else if (rrset->getType() == RRType::DNAME()) {
277
                node->setFlag(DomainNode::FLAG_CALLBACK);
278
279
            }

Michal Vaner's avatar
Michal Vaner committed
280
281
282
283
284
285
            return (result::SUCCESS);
        } else {
            // The RRSet of given type was already there
            return (result::EXIST);
        }
    }
Michal Vaner's avatar
Michal Vaner committed
286

287
288
289
290
291
292
293
    /*
     * 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:
294
295
                    LOG_ERROR(logger, DATASRC_MEM_DUP_RRSET).
                        arg(set->getName()).arg(set->getType());
296
297
298
299
300
301
302
303
304
                    isc_throw(dns::MasterLoadError, "Duplicate rrset: " <<
                        set->toText());
                case result::SUCCESS:
                    return;
                default:
                    assert(0);
            }
    }

305
306
307
308
309
310
    // 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 {
311
312
313
314
        FindState(FindOptions options) :
            zonecut_node_(NULL),
            dname_node_(NULL),
            options_(options)
315
316
        {}
        const DomainNode* zonecut_node_;
317
        const DomainNode* dname_node_;
318
319
        ConstRRsetPtr rrset_;
        const FindOptions options_;
320
321
    };

322
323
324
325
326
    // A callback called from possible zone cut nodes and nodes with DNAME.
    // This will be passed from the \c find() method to \c RBTree::find().
    static bool cutCallback(const DomainNode& node, FindState* state) {
        // We need to look for DNAME first, there's allowed case where
        // DNAME and NS coexist in the apex. DNAME is the one to notice,
327
328
        // the NS is authoritative, not delegation (corner case explicitly
        // allowed by section 3 of 2672)
329
330
331
        const Domain::const_iterator foundDNAME(node.getData()->find(
            RRType::DNAME()));
        if (foundDNAME != node.getData()->end()) {
332
333
            LOG_DEBUG(logger, DBG_TRACE_DETAILED,
                      DATASRC_MEM_DNAME_ENCOUNTERED);
334
335
            state->dname_node_ = &node;
            state->rrset_ = foundDNAME->second;
336
337
338
339
340
341
            // No more processing below the DNAME (RFC 2672, section 3
            // forbids anything to exist below it, so there's no need
            // to actually search for it). This is strictly speaking
            // a different way than described in 4.1 of that RFC,
            // but because of the assumption in section 3, it has the
            // same behaviour.
342
            return (true);
343
344
        }

345
346
347
348
        // Look for NS
        const Domain::const_iterator foundNS(node.getData()->find(
            RRType::NS()));
        if (foundNS != node.getData()->end()) {
349
350
351
352
353
354
            // We perform callback check only for the highest zone cut in the
            // rare case of nested zone cuts.
            if (state->zonecut_node_ != NULL) {
                return (false);
            }

355
            LOG_DEBUG(logger, DBG_TRACE_DETAILED, DATASRC_MEM_NS_ENCOUNTERED);
356

357
358
359
360
361
362
            // BIND 9 checks if this node is not the origin.  That's probably
            // because it can support multiple versions for dynamic updates
            // and IXFR, and it's possible that the callback is called at
            // the apex and the DNAME doesn't exist for a particular version.
            // It cannot happen for us (at least for now), so we don't do
            // that check.
363
            state->zonecut_node_ = &node;
364
            state->rrset_ = foundNS->second;
365
366
367
368

            // 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);
369
370
        }

JINMEI Tatuya's avatar
JINMEI Tatuya committed
371
372
373
        // This case should not happen because we enable callback only
        // when we add an RR searched for above.
        assert(0);
374
375
376
        // 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".
377
        return (false);
378
379
    }

380
381
382
383
384
385
386
387
388
389
390
391
    /*
     * Prepares a rrset to be return as a result.
     *
     * If rename is false, it returns the one provided. If it is true, it
     * creates a new rrset with the same data but with provided name.
     * It is designed for wildcard case, where we create the rrsets
     * dynamically.
     */
    static ConstRRsetPtr prepareRRset(const Name& name, const ConstRRsetPtr&
        rrset, bool rename)
    {
        if (rename) {
392
            LOG_DEBUG(logger, DBG_TRACE_DETAILED, DATASRC_MEM_RENAME).
393
                arg(rrset->getName()).arg(name);
Michal 'vorner' Vaner's avatar
Michal 'vorner' Vaner committed
394
395
396
397
398
399
            /*
             * We lose a signature here. But it would be wrong anyway, because
             * the name changed. This might turn out to be unimportant in
             * future, because wildcards will probably be handled somehow
             * by DNSSEC.
             */
400
401
402
403
404
405
406
407
408
409
410
            RRsetPtr result(new RRset(name, rrset->getClass(),
                rrset->getType(), rrset->getTTL()));
            for (RdataIteratorPtr i(rrset->getRdataIterator()); !i->isLast();
                i->next()) {
                result->addRdata(i->getCurrent());
            }
            return (result);
        } else {
            return (rrset);
        }
    }
chenzhengzhang's avatar
chenzhengzhang committed
411

412
    // Implementation of InMemoryZoneFinder::find
413
    FindResult find(const Name& name, RRType type,
414
                    RRsetList* target, const FindOptions options) const
415
    {
416
417
        LOG_DEBUG(logger, DBG_TRACE_BASIC, DATASRC_MEM_FIND).arg(name).
            arg(type);
Michal Vaner's avatar
Michal Vaner committed
418
        // Get the node
Michal Vaner's avatar
Michal Vaner committed
419
        DomainNode* node(NULL);
420
        FindState state(options);
421
        RBTreeNodeChain<Domain> node_path;
422
        bool rename(false);
423
        switch (domains_.find(name, &node, node_path, cutCallback, &state)) {
Michal Vaner's avatar
Michal Vaner committed
424
            case DomainTree::PARTIALMATCH:
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
                /*
                 * In fact, we could use a single variable instead of
                 * dname_node_ and zonecut_node_. But then we would need
                 * to distinquish these two cases by something else and
                 * it seemed little more confusing to me when I wrote it.
                 *
                 * Usually at most one of them will be something else than
                 * NULL (it might happen both are NULL, in which case we
                 * consider it NOT FOUND). There's one corner case when
                 * both might be something else than NULL and it is in case
                 * there's a DNAME under a zone cut and we search in
                 * glue OK mode ‒ in that case we don't stop on the domain
                 * with NS and ignore it for the answer, but it gets set
                 * anyway. Then we find the DNAME and we need to act by it,
                 * therefore we first check for DNAME and then for NS. In
440
                 * all other cases it doesn't matter, as at least one of them
441
442
                 * is NULL.
                 */
443
                if (state.dname_node_ != NULL) {
444
                    LOG_DEBUG(logger, DBG_TRACE_DATA, DATASRC_MEM_DNAME_FOUND).
445
                        arg(state.rrset_->getName());
446
447
                    // We were traversing a DNAME node (and wanted to go
                    // lower below it), so return the DNAME
448
449
                    return (FindResult(DNAME, prepareRRset(name, state.rrset_,
                        rename)));
450
                }
451
                if (state.zonecut_node_ != NULL) {
452
                    LOG_DEBUG(logger, DBG_TRACE_DATA, DATASRC_MEM_DELEG_FOUND).
453
                        arg(state.rrset_->getName());
454
455
                    return (FindResult(DELEGATION, prepareRRset(name,
                        state.rrset_, rename)));
456
                }
457
458
459
460
461
462

                // If the RBTree search stopped at a node for a super domain
                // of the search name, it means the search name exists in
                // the zone but is empty.  Treat it as NXRRSET.
                if (node_path.getLastComparisonResult().getRelation() ==
                    NameComparisonResult::SUPERDOMAIN) {
463
464
                    LOG_DEBUG(logger, DBG_TRACE_DATA, DATASRC_MEM_SUPER_STOP).
                        arg(node_path.getAbsoluteName()).arg(name);
465
466
467
                    return (FindResult(NXRRSET, ConstRRsetPtr()));
                }

468
469
                /*
                 * No redirection anywhere. Let's try if it is a wildcard.
470
471
472
473
474
                 *
                 * The wildcard is checked after the empty non-terminal domain
                 * case above, because if that one triggers, it means we should
                 * not match according to 4.3.3 of RFC 1034 (the query name
                 * is known to exist).
475
476
                 */
                if (node->getFlag(DOMAINFLAG_WILD)) {
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
                    /* Should we cancel this match?
                     *
                     * If we compare with some node and get a common ancestor,
                     * it might mean we are comparing with a non-wildcard node.
                     * In that case, we check which part is common. If we have
                     * something in common that lives below the node we got
                     * (the one above *), then we should cancel the match
                     * according to section 4.3.3 of RFC 1034 (as the name
                     * between the wildcard domain and the query name is known
                     * to exist).
                     *
                     * Because the way the tree stores relative names, we will
                     * have exactly one common label (the ".") in case we have
                     * nothing common under the node we got and we will get
                     * more common labels otherwise (yes, this relies on the
492
                     * internal RBTree structure, which leaks out through this
493
494
495
496
497
498
499
500
501
                     * little bit).
                     *
                     * If the empty non-terminal node actually exists in the
                     * tree, then this cancellation is not needed, because we
                     * will not get here at all.
                     */
                    if (node_path.getLastComparisonResult().getRelation() ==
                        NameComparisonResult::COMMONANCESTOR && node_path.
                        getLastComparisonResult().getCommonLabels() > 1) {
502
                        LOG_DEBUG(logger, DBG_TRACE_DATA,
503
                                     DATASRC_MEM_WILDCARD_CANCEL).arg(name);
504
505
                        return (FindResult(NXDOMAIN, ConstRRsetPtr()));
                    }
506
507
508
509
510
511
512
                    Name wildcard(Name("*").concatenate(
                        node_path.getAbsoluteName()));
                    DomainTree::Result result(domains_.find(wildcard, &node));
                    /*
                     * Otherwise, why would the DOMAINFLAG_WILD be there if
                     * there was no wildcard under it?
                     */
513
                    assert(result == DomainTree::EXACTMATCH);
514
515
516
517
                    /*
                     * We have the wildcard node now. Jump below the switch,
                     * where handling of the common (exact-match) case is.
                     *
518
                     * However, rename it to the searched name.
519
                     */
520
                    rename = true;
521
522
                    break;
                }
523
524

                // fall through
Michal Vaner's avatar
Michal Vaner committed
525
            case DomainTree::NOTFOUND:
526
                LOG_DEBUG(logger, DBG_TRACE_DATA, DATASRC_MEM_NOT_FOUND).
527
                    arg(name);
Michal Vaner's avatar
Michal Vaner committed
528
529
530
531
532
                return (FindResult(NXDOMAIN, ConstRRsetPtr()));
            case DomainTree::EXACTMATCH: // This one is OK, handle it
                break;
            default:
                assert(0);
Michal Vaner's avatar
Michal Vaner committed
533
        }
534
        assert(node != NULL);
535
536
537
538

        // If there is an exact match but the node is empty, it's equivalent
        // to NXRRSET.
        if (node->isEmpty()) {
539
540
            LOG_DEBUG(logger, DBG_TRACE_DATA, DATASRC_MEM_DOMAIN_EMPTY).
                arg(name);
541
542
            return (FindResult(NXRRSET, ConstRRsetPtr()));
        }
Michal Vaner's avatar
Michal Vaner committed
543

544
545
546
        Domain::const_iterator found;

        // If the node callback is enabled, this may be a zone cut.  If it
547
        // has a NS RR, we should return a delegation, but not in the apex.
548
        if (node->getFlag(DomainNode::FLAG_CALLBACK) && node != origin_data_) {
549
550
            found = node->getData()->find(RRType::NS());
            if (found != node->getData()->end()) {
551
552
                LOG_DEBUG(logger, DBG_TRACE_DATA,
                          DATASRC_MEM_EXACT_DELEGATION).arg(name);
553
554
                return (FindResult(DELEGATION, prepareRRset(name,
                    found->second, rename)));
555
556
557
            }
        }

chenzhengzhang's avatar
chenzhengzhang committed
558
        // handle type any query
559
        if (target != NULL && !node->getData()->empty()) {
560
561
            // Empty domain will be handled as NXRRSET by normal processing
            for (found = node->getData()->begin();
562
                 found != node->getData()->end(); ++found)
563
564
            {
                target->addRRset(
565
566
                    boost::const_pointer_cast<RRset>(prepareRRset(name,
                    found->second, rename)));
chenzhengzhang's avatar
chenzhengzhang committed
567
            }
568
569
            LOG_DEBUG(logger, DBG_TRACE_DATA, DATASRC_MEM_ANY_SUCCESS).
                arg(name);
570
            return (FindResult(SUCCESS, ConstRRsetPtr()));
chenzhengzhang's avatar
chenzhengzhang committed
571
572
        }

573
        found = node->getData()->find(type);
Michal Vaner's avatar
Michal Vaner committed
574
575
        if (found != node->getData()->end()) {
            // Good, it is here
576
            LOG_DEBUG(logger, DBG_TRACE_DATA, DATASRC_MEM_SUCCESS).arg(name).
577
                arg(type);
578
579
            return (FindResult(SUCCESS, prepareRRset(name, found->second,
                rename)));
Michal Vaner's avatar
Michal Vaner committed
580
        } else {
581
            // Next, try CNAME.
582
583
            found = node->getData()->find(RRType::CNAME());
            if (found != node->getData()->end()) {
584
                LOG_DEBUG(logger, DBG_TRACE_DATA, DATASRC_MEM_CNAME).arg(name);
585
586
                return (FindResult(CNAME, prepareRRset(name, found->second,
                    rename)));
587
            }
Michal Vaner's avatar
Michal Vaner committed
588
        }
589
        // No exact match or CNAME.  Return NXRRSET.
590
591
        LOG_DEBUG(logger, DBG_TRACE_DATA, DATASRC_MEM_NXRRSET).arg(type).
            arg(name);
592
        return (FindResult(NXRRSET, ConstRRsetPtr()));
Michal Vaner's avatar
Michal Vaner committed
593
    }
594
595
};

596
597
InMemoryZoneFinder::InMemoryZoneFinder(const RRClass& zone_class, const Name& origin) :
    impl_(new InMemoryZoneFinderImpl(zone_class, origin))
598
{
599
    LOG_DEBUG(logger, DBG_TRACE_BASIC, DATASRC_MEM_CREATE).arg(origin).
600
        arg(zone_class);
601
602
}

603
InMemoryZoneFinder::~InMemoryZoneFinder() {
604
    LOG_DEBUG(logger, DBG_TRACE_BASIC, DATASRC_MEM_DESTROY).arg(getOrigin()).
605
        arg(getClass());
606
607
608
    delete impl_;
}

609
Name
610
InMemoryZoneFinder::getOrigin() const {
611
612
613
    return (impl_->origin_);
}

614
RRClass
615
InMemoryZoneFinder::getClass() const {
616
617
618
    return (impl_->zone_class_);
}

619
ZoneFinder::FindResult
620
InMemoryZoneFinder::find(const Name& name, const RRType& type,
Jelte Jansen's avatar
Jelte Jansen committed
621
                 RRsetList* target, const FindOptions options)
chenzhengzhang's avatar
chenzhengzhang committed
622
{
623
    return (impl_->find(name, type, target, options));
chenzhengzhang's avatar
chenzhengzhang committed
624
625
}

Michal Vaner's avatar
Michal Vaner committed
626
result::Result
627
InMemoryZoneFinder::add(const ConstRRsetPtr& rrset) {
628
    return (impl_->add(rrset, &impl_->domains_));
629
630
}

Michal Vaner's avatar
Michal Vaner committed
631
632

void
633
InMemoryZoneFinder::load(const string& filename) {
634
    LOG_DEBUG(logger, DBG_TRACE_BASIC, DATASRC_MEM_LOAD).arg(getOrigin()).
635
        arg(filename);
636
    // Load it into a temporary tree
637
    InMemoryZoneFinderImpl::DomainTree tmp;
638
    masterLoad(filename.c_str(), getOrigin(), getClass(),
639
        boost::bind(&InMemoryZoneFinderImpl::addFromLoad, impl_, _1, &tmp));
640
    // If it went well, put it inside
641
    impl_->file_name_ = filename;
642
643
    tmp.swap(impl_->domains_);
    // And let the old data die with tmp
644
645
}

646
void
647
InMemoryZoneFinder::swap(InMemoryZoneFinder& zone_finder) {
648
    LOG_DEBUG(logger, DBG_TRACE_BASIC, DATASRC_MEM_SWAP).arg(getOrigin()).
649
650
        arg(zone_finder.getOrigin());
    std::swap(impl_, zone_finder.impl_);
651
652
653
}

const string
654
InMemoryZoneFinder::getFileName() const {
655
656
657
    return (impl_->file_name_);
}

658
/// Implementation details for \c InMemoryClient hidden from the public
659
660
/// interface.
///
661
/// For now, \c InMemoryClient only contains a \c ZoneTable object, which
662
/// consists of (pointers to) \c InMemoryZoneFinder objects, we may add more
663
/// member variables later for new features.
664
class InMemoryClient::InMemoryClientImpl {
665
public:
666
    InMemoryClientImpl() : zone_count(0) {}
667
    unsigned int zone_count;
668
669
670
    ZoneTable zone_table;
};

671
InMemoryClient::InMemoryClient() : impl_(new InMemoryClientImpl)
672
673
{}

674
InMemoryClient::~InMemoryClient() {
675
676
677
    delete impl_;
}

678
unsigned int
679
InMemoryClient::getZoneCount() const {
680
681
682
    return (impl_->zone_count);
}

683
result::Result
684
685
InMemoryClient::addZone(ZoneFinderPtr zone_finder) {
    if (!zone_finder) {
686
        isc_throw(InvalidParameter,
687
                  "Null pointer is passed to InMemoryClient::addZone()");
688
    }
689

690
    LOG_DEBUG(logger, DBG_TRACE_BASIC, DATASRC_MEM_ADD_ZONE).
691
        arg(zone_finder->getOrigin()).arg(zone_finder->getClass().toText());
692

693
    const result::Result result = impl_->zone_table.addZone(zone_finder);
694
695
696
697
    if (result == result::SUCCESS) {
        ++impl_->zone_count;
    }
    return (result);
698
699
}

700
701
InMemoryClient::FindResult
InMemoryClient::findZone(const isc::dns::Name& name) const {
702
    LOG_DEBUG(logger, DBG_TRACE_DATA, DATASRC_MEM_FIND_ZONE).arg(name);
703
704
705
706
707
    return (FindResult(impl_->zone_table.findZone(name).code,
                       impl_->zone_table.findZone(name).zone));
}
} // end of namespace datasrc
} // end of namespace dns