Kea: masterload.cc Source File

| Kea 2.2.0

masterload.cc
Go to the documentation of this file.
1// Copyright (C) 2010-2020 Internet Systems Consortium, Inc. ("ISC")
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7#include <config.h>
8
9#include <dns/masterload.h>
10#include <dns/master_loader.h>
11#include <dns/name.h>
12#include <dns/rdata.h>
13#include <dns/rdataclass.h>
14#include <dns/rrclass.h>
15#include <dns/rrset.h>
16#include <dns/rrttl.h>
17#include <dns/rrtype.h>
18#include <dns/rrcollator.h>
19#include <exceptions/exceptions.h>
20
21#include <boost/scoped_ptr.hpp>
22
23#include <functional>
24#include <istream>
25#include <fstream>
26#include <sstream>
27#include <string>
28#include <cctype>
29#include <cerrno>
30
31using namespace isc::dns::rdata;
32
33using namespace std;
34namespace ph = std::placeholders;
35
36namespace isc {
37namespace dns {
38namespace {
39void
40callbackWrapper(const RRsetPtr& rrset, MasterLoadCallback callback,
41 const Name* origin)
42{
43 // Origin related validation:
44 // - reject out-of-zone data
45 // - reject SOA whose owner is not at the top of zone
46 const NameComparisonResult cmp_result =
47 rrset->getName().compare(*origin);
48 if (cmp_result.getRelation() != NameComparisonResult::EQUAL &&
49 cmp_result.getRelation() != NameComparisonResult::SUBDOMAIN) {
50 isc_throw(MasterLoadError, "Out-of-zone data for " << *origin
51 << "/" << rrset->getClass() << ": " << rrset->getName());
52 }
53 if (rrset->getType() == RRType::SOA() &&
54 cmp_result.getRelation() != NameComparisonResult::EQUAL) {
55 isc_throw(MasterLoadError, "SOA not at top of zone: "
56 << *rrset);
57 }
58
59 callback(rrset);
60}
61
62template <typename InputType>
63void
64loadHelper(InputType input, const Name& origin,
65 const RRClass& zone_class, MasterLoadCallback callback)
66{
67 RRCollator rr_collator(std::bind(callbackWrapper, ph::_1, callback,
68 &origin));
69 MasterLoader loader(input, origin, zone_class,
70 MasterLoaderCallbacks::getNullCallbacks(),
71 rr_collator.getCallback());
72 try {
73 loader.load();
74 } catch (const MasterLoaderError& ex) {
75 isc_throw(MasterLoadError, ex.what());
76 }
77 rr_collator.flush();
78}
79}
80
81void
82masterLoad(const char* const filename, const Name& origin,
83 const RRClass& zone_class, MasterLoadCallback callback)
84{
85 if ((filename == NULL) || (*filename == '\0')) {
86 isc_throw(MasterLoadError, "Name of master file must not be null");
87 }
88
89 loadHelper<const char*>(filename, origin, zone_class, callback);
90}
91
92void
93 masterLoad(istream& input, const Name& origin, const RRClass& zone_class,
94 MasterLoadCallback callback, const char*)
95{
96 loadHelper<istream&>(input, origin, zone_class, callback);
97}
98
99} // namespace dns
100} // namespace isc
isc::dns::MasterLoadError
An exception that is thrown if an error occurs while loading a master zone data.
Definition: masterload.h:23
isc::dns::MasterLoaderCallbacks::getNullCallbacks
static MasterLoaderCallbacks getNullCallbacks()
Return a callbacks instance with null callbacks.
Definition: master_loader_callbacks.cc:23
isc::dns::NameComparisonResult::SUBDOMAIN
@ SUBDOMAIN
Definition: name.h:144
isc::dns::NameComparisonResult::EQUAL
@ EQUAL
Definition: name.h:145
isc::dns::Name
The Name class encapsulates DNS names.
Definition: name.h:223
isc::dns::RRClass
The RRClass class encapsulates DNS resource record classes.
Definition: rrclass.h:98
isc::dns::RRType::SOA
static const RRType & SOA()
Definition: rrtype.h:461
exceptions.h
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition: exceptions/exceptions.h:210
master_loader.h
masterload.h
name.h
isc::dns::rdata
Definition: dns_fwd.h:32
isc::dns::masterLoad
void masterLoad(const char *const filename, const Name &origin, const RRClass &zone_class, MasterLoadCallback callback)
Master zone file loader from a file.
Definition: masterload.cc:82
isc::dns::RRsetPtr
boost::shared_ptr< AbstractRRset > RRsetPtr
A pointer-like type pointing to an RRset object.
Definition: rrset.h:47
isc::dns::MasterLoadCallback
std::function< void(RRsetPtr)> MasterLoadCallback
The type of the callback parameter of masterLoad().
Definition: masterload.h:33
isc
Defines the logger used by the top-level component of kea-lfc.
Definition: agent_parser.cc:143
rdata.h
rdataclass.h
rrclass.h
rrcollator.h
rrset.h
rrttl.h
rrtype.h