rrset_collection.cc 4.07 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// 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 <dns/rrset_collection.h>
#include <dns/master_loader_callbacks.h>
#include <dns/master_loader.h>

19
20
#include <exceptions/exceptions.h>

21
22
#include <boost/bind.hpp>

23
24
using namespace isc;

25
26
27
28
29
30
namespace isc {
namespace dns {

void
RRsetCollection::loaderCallback(const std::string&, size_t, const std::string&)
{
31
     // We just ignore callbacks for errors and warnings.
32
33
34
35
36
37
38
39
40
41
42
43
44
45
}

void
RRsetCollection::addRRset(const Name& name, const RRClass& rrclass,
                          const RRType& rrtype, const RRTTL& rrttl,
                          const rdata::RdataPtr& data)
{
    RRsetPtr rrset(new BasicRRset(name, rrclass, rrtype, rrttl));
    rrset->addRdata(data);
    addRRset(rrset);
}

void
RRsetCollection::addRRset(RRsetPtr rrset) {
46
47
    const CollectionKey key(rrset->getClass(), rrset->getType(),
                            rrset->getName());
48
49
50
51
52
53
54
    CollectionMap::const_iterator it = rrsets_.find(key);
    if (it != rrsets_.end()) {
        isc_throw(InvalidParameter,
                  "RRset for " << rrset->getName() << "/" << rrset->getClass()
                  << " with type " << rrset->getType() << " already exists");
    }

55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
    rrsets_.insert(std::pair<CollectionKey, RRsetPtr>(key, rrset));
}

RRsetCollection::RRsetCollection(const char* filename, const Name& origin,
                                 const RRClass& rrclass)
{
    MasterLoaderCallbacks callbacks
        (boost::bind(&RRsetCollection::loaderCallback, this, _1, _2, _3),
         boost::bind(&RRsetCollection::loaderCallback, this, _1, _2, _3));
    MasterLoader loader(filename, origin, rrclass, callbacks,
                        boost::bind(&RRsetCollection::addRRset,
                                    this, _1, _2, _3, _4, _5),
                        MasterLoader::DEFAULT);
    loader.load();
}

const AbstractRRset*
72
73
74
RRsetCollection::find(const Name& name, const RRType& rrtype,
                      const RRClass& rrclass) const {
    const CollectionKey key(rrclass, rrtype, name);
75
76
77
78
79
80
81
82
    CollectionMap::const_iterator it = rrsets_.find(key);
    if (it != rrsets_.end()) {
        return (&(*it->second));
    }
    return (NULL);
}

RRsetPtr
83
84
85
RRsetCollection::find(const Name& name, const RRClass& rrclass,
                      const RRType& rrtype) {
    const CollectionKey key(rrclass, rrtype, name);
86
87
88
89
90
91
92
93
    CollectionMap::iterator it = rrsets_.find(key);
    if (it != rrsets_.end()) {
        return (it->second);
    }
    return (RRsetPtr());
}

ConstRRsetPtr
94
RRsetCollection::find(const Name& name, const RRClass& rrclass,
95
96
                      const RRType& rrtype) const
{
97
    const CollectionKey key(rrclass, rrtype, name);
98
99
100
101
102
103
104
105
    CollectionMap::const_iterator it = rrsets_.find(key);
    if (it != rrsets_.end()) {
        return (it->second);
    }
    return (ConstRRsetPtr());
}

void
106
RRsetCollection::removeRRset(const Name& name, const RRClass& rrclass,
107
108
                             const RRType& rrtype)
{
109
    const CollectionKey key(rrclass, rrtype, name);
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
    rrsets_.erase(key);
}

RRsetCollectionBase::IterPtr
RRsetCollection::getBeginning() {
    CollectionMap::iterator it = rrsets_.begin();
    return (RRsetCollectionBase::IterPtr(new DnsIter(it)));
}

RRsetCollectionBase::IterPtr
RRsetCollection::getEnd() {
    CollectionMap::iterator it = rrsets_.end();
    return (RRsetCollectionBase::IterPtr(new DnsIter(it)));
}

} // end of namespace dns
} // end of namespace isc