Kea  1.5.0
masterload.cc
Go to the documentation of this file.
1 // Copyright (C) 2010-2015 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 <istream>
10 #include <fstream>
11 #include <sstream>
12 #include <string>
13 #include <cctype>
14 #include <cerrno>
15 
16 #include <boost/bind.hpp>
17 #include <boost/scoped_ptr.hpp>
18 
19 #include <exceptions/exceptions.h>
20 
21 #include <dns/masterload.h>
22 #include <dns/master_loader.h>
23 #include <dns/name.h>
24 #include <dns/rdata.h>
25 #include <dns/rdataclass.h>
26 #include <dns/rrclass.h>
27 #include <dns/rrset.h>
28 #include <dns/rrttl.h>
29 #include <dns/rrtype.h>
30 #include <dns/rrcollator.h>
31 
32 using namespace std;
33 using namespace boost;
34 using namespace isc::dns::rdata;
35 
36 namespace isc {
37 namespace dns {
38 namespace {
39 void
40 callbackWrapper(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 
62 template <typename InputType>
63 void
64 loadHelper(InputType input, const Name& origin,
65  const RRClass& zone_class, MasterLoadCallback callback)
66 {
67  RRCollator rr_collator(boost::bind(callbackWrapper, _1,
68  callback, &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 
81 void
82 masterLoad(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 
92 void
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
rdata.h
isc::dns::RRsetPtr
boost::shared_ptr< AbstractRRset > RRsetPtr
A pointer-like type pointing to an RRset object.
Definition: rrset.h:47
rrcollator.h
rrttl.h
isc::dns::MasterLoadError
An exception that is thrown if an error occurs while loading a master zone data.
Definition: masterload.h:25
name.h
boost
Definition: io_service.h:12
rdataclass.h
isc::dns::MasterLoadCallback
boost::function< void(RRsetPtr)> MasterLoadCallback
The type of the callback parameter of masterLoad().
Definition: masterload.h:35
rrclass.h
isc
Defines the logger used by the top-level component of kea-dhcp-ddns.
Definition: agent_parser.cc:144
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition: exceptions/exceptions.h:192
isc::dns::masterLoad
void masterLoad(istream &input, const Name &origin, const RRClass &zone_class, MasterLoadCallback callback, const char *)
Master zone file loader from input stream.
Definition: masterload.cc:93
rrset.h
isc::dns::Name
The Name class encapsulates DNS names.
Definition: name.h:223
rrtype.h
masterload.h
isc::dns::rdata
Definition: dns_fwd.h:32
exceptions.h
master_loader.h
isc::dns::RRClass
The RRClass class encapsulates DNS resource record classes.
Definition: rrclass.h:98