summaryrefslogtreecommitdiffstats
path: root/src/lib/dns/rrcollator.cc
blob: 153de04439d7d4a0ab3d450bb3b65702c0892e2b (plain)
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
// 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 <exceptions/exceptions.h>

// include this first to check the header is self-contained.
#include <dns/rrcollator.h>

#include <dns/name.h>
#include <dns/rdataclass.h>
#include <dns/rrclass.h>
#include <dns/rrtype.h>
#include <dns/rrttl.h>
#include <dns/rdata.h>
#include <dns/rrset.h>

#include <boost/bind.hpp>

#include <algorithm>

namespace isc {
namespace dns {
using namespace rdata;

class RRCollator::Impl {
public:
    Impl(const AddRRsetCallback& callback) : callback_(callback) {}

    void addRR(const Name& name, const RRClass& rrclass,
               const RRType& rrtype, const RRTTL& rrttl,
               const RdataPtr& rdata);

    RRsetPtr current_rrset_;
    const AddRRsetCallback callback_;
};

namespace {
inline bool
isSameType(RRType type1, const ConstRdataPtr& rdata1,
           const ConstRRsetPtr& rrset)
{
    if (type1 != rrset->getType()) {
        return (false);
    }
    if (type1 == RRType::RRSIG()) {
        RdataIteratorPtr rit = rrset->getRdataIterator();
        return (dynamic_cast<const generic::RRSIG&>(*rdata1).typeCovered()
                == dynamic_cast<const generic::RRSIG&>(
                    rit->getCurrent()).typeCovered());
    }
    return (true);
}
}

void
RRCollator::Impl::addRR(const Name& name, const RRClass& rrclass,
                        const RRType& rrtype, const RRTTL& rrttl,
                        const RdataPtr& rdata)
{
    if (current_rrset_ && (!isSameType(rrtype, rdata, current_rrset_) ||
                           current_rrset_->getClass() != rrclass ||
                           current_rrset_->getName() != name)) {
        callback_(current_rrset_);
        current_rrset_.reset();
    }

    if (!current_rrset_) {
        current_rrset_ = RRsetPtr(new RRset(name, rrclass, rrtype, rrttl));
    } else if (current_rrset_->getTTL() != rrttl) {
        // RRs with different TTLs are given.  Smaller TTL should win.
        current_rrset_->setTTL(std::min(current_rrset_->getTTL(), rrttl));
    }
    current_rrset_->addRdata(rdata);
}

RRCollator::RRCollator(const AddRRsetCallback& callback) :
    impl_(new Impl(callback))
{}

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

AddRRCallback
RRCollator::getCallback() {
    return (boost::bind(&RRCollator::Impl::addRR, this->impl_,
                        _1, _2, _3, _4, _5));
}

void
RRCollator::flush() {
    if (impl_->current_rrset_) {
        impl_->callback_(impl_->current_rrset_);
        impl_->current_rrset_.reset();
    }
}

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