Kea 2.5.5
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>
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 &&
50 isc_throw(MasterLoadError, "Out-of-zone data for " << *origin
51 << "/" << rrset->getClass() << ": " << rrset->getName());
52 }
53 if (rrset->getType() == RRType::SOA() &&
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,
71 rr_collator.getCallback());
72 try {
73 loader.load();
74 } catch (const MasterLoaderError& ex) {
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
93masterLoad(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
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
An exception that is thrown if an error occurs while loading a master zone data.
Definition: masterload.h:23
static MasterLoaderCallbacks getNullCallbacks()
Return a callbacks instance with null callbacks.
Error while loading by MasterLoader without specifying the MANY_ERRORS option.
Definition: master_loader.h:22
A class able to load DNS master files.
Definition: master_loader.h:36
This is a supplemental class used only as a return value of Name::compare() and LabelSequence::compar...
Definition: name.h:117
NameRelation getRelation() const
Returns the NameRelation of the comparison result.
Definition: name.h:172
The Name class encapsulates DNS names.
Definition: name.h:223
The RRClass class encapsulates DNS resource record classes.
Definition: rrclass.h:98
A converter from a stream of RRs to a stream of collated RRsets.
Definition: rrcollator.h:45
static const RRType & SOA()
Definition: rrtype.h:461
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
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
boost::shared_ptr< AbstractRRset > RRsetPtr
A pointer-like type pointing to an RRset object.
Definition: rrset.h:53
std::function< void(RRsetPtr)> MasterLoadCallback
The type of the callback parameter of masterLoad().
Definition: masterload.h:33
Defines the logger used by the top-level component of kea-lfc.