memory_client.cc 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907
  1. // Copyright (C) 2012 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <exceptions/exceptions.h>
  15. #include <datasrc/memory/memory_client.h>
  16. #include <datasrc/memory/logger.h>
  17. #include <datasrc/memory/zone_data.h>
  18. #include <datasrc/memory/rdata_serialization.h>
  19. #include <datasrc/memory/rdataset.h>
  20. #include <datasrc/memory/domaintree.h>
  21. #include <datasrc/memory/segment_object_holder.h>
  22. #include <datasrc/memory/treenode_rrset.h>
  23. #include <util/memory_segment_local.h>
  24. #include <datasrc/data_source.h>
  25. #include <datasrc/factory.h>
  26. #include <datasrc/result.h>
  27. #include <dns/name.h>
  28. #include <dns/nsec3hash.h>
  29. #include <dns/rdataclass.h>
  30. #include <dns/rrclass.h>
  31. #include <dns/rrsetlist.h>
  32. #include <dns/masterload.h>
  33. #include <boost/function.hpp>
  34. #include <boost/shared_ptr.hpp>
  35. #include <boost/scoped_ptr.hpp>
  36. #include <boost/bind.hpp>
  37. #include <boost/foreach.hpp>
  38. #include <algorithm>
  39. #include <map>
  40. #include <utility>
  41. #include <cctype>
  42. #include <cassert>
  43. using namespace std;
  44. using namespace isc::dns;
  45. using namespace isc::dns::rdata;
  46. using namespace isc::datasrc::memory;
  47. using boost::scoped_ptr;
  48. namespace isc {
  49. namespace datasrc {
  50. namespace memory {
  51. using detail::SegmentObjectHolder;
  52. namespace {
  53. // Some type aliases
  54. typedef DomainTree<std::string> FileNameTree;
  55. typedef DomainTreeNode<std::string> FileNameNode;
  56. // A functor type used for loading.
  57. typedef boost::function<void(ConstRRsetPtr)> LoadCallback;
  58. } // end of anonymous namespace
  59. /// Implementation details for \c InMemoryClient hidden from the public
  60. /// interface.
  61. ///
  62. /// For now, \c InMemoryClient only contains a \c ZoneTable object, which
  63. /// consists of (pointers to) \c InMemoryZoneFinder objects, we may add more
  64. /// member variables later for new features.
  65. class InMemoryClient::InMemoryClientImpl {
  66. private:
  67. // The deleter for the filenames stored in the tree.
  68. struct FileNameDeleter {
  69. FileNameDeleter() {}
  70. void operator()(std::string* filename) const {
  71. delete filename;
  72. }
  73. };
  74. public:
  75. InMemoryClientImpl(RRClass rrclass) :
  76. rrclass_(rrclass),
  77. zone_count_(0),
  78. zone_table_(ZoneTable::create(local_mem_sgmt_, rrclass)),
  79. file_name_tree_(FileNameTree::create(local_mem_sgmt_, false))
  80. {}
  81. ~InMemoryClientImpl() {
  82. FileNameDeleter deleter;
  83. FileNameTree::destroy(local_mem_sgmt_, file_name_tree_, deleter);
  84. ZoneTable::destroy(local_mem_sgmt_, zone_table_, rrclass_);
  85. // see above for the assert().
  86. assert(local_mem_sgmt_.allMemoryDeallocated());
  87. }
  88. // Memory segment to allocate/deallocate memory for the zone table.
  89. // (This will eventually have to be abstract; for now we hardcode the
  90. // specific derived segment class).
  91. util::MemorySegmentLocal local_mem_sgmt_;
  92. const RRClass rrclass_;
  93. unsigned int zone_count_;
  94. ZoneTable* zone_table_;
  95. FileNameTree* file_name_tree_;
  96. ConstRRsetPtr last_rrset_;
  97. // Common process for zone load.
  98. // rrset_installer is a functor that takes another functor as an argument,
  99. // and expected to call the latter for each RRset of the zone. How the
  100. // sequence of the RRsets is generated depends on the internal
  101. // details of the loader: either from a textual master file or from
  102. // another data source.
  103. // filename is the file name of the master file or empty if the zone is
  104. // loaded from another data source.
  105. result::Result load(const Name& zone_name, const string& filename,
  106. boost::function<void(LoadCallback)> rrset_installer);
  107. // Add the necessary magic for any wildcard contained in 'name'
  108. // (including itself) to be found in the zone.
  109. //
  110. // In order for wildcard matching to work correctly in the zone finder,
  111. // we must ensure that a node for the wildcarding level exists in the
  112. // backend RBTree.
  113. // E.g. if the wildcard name is "*.sub.example." then we must ensure
  114. // that "sub.example." exists and is marked as a wildcard level.
  115. // Note: the "wildcarding level" is for the parent name of the wildcard
  116. // name (such as "sub.example.").
  117. //
  118. // We also perform the same trick for empty wild card names possibly
  119. // contained in 'name' (e.g., '*.foo.example' in 'bar.*.foo.example').
  120. void addWildcards(const Name& zone_name, ZoneData& zone_data,
  121. const Name& name)
  122. {
  123. Name wname(name);
  124. const unsigned int labels(wname.getLabelCount());
  125. const unsigned int origin_labels(zone_name.getLabelCount());
  126. for (unsigned int l = labels;
  127. l > origin_labels;
  128. --l, wname = wname.split(1)) {
  129. if (wname.isWildcard()) {
  130. LOG_DEBUG(logger, DBG_TRACE_DATA,
  131. DATASRC_MEMORY_MEM_ADD_WILDCARD).arg(name);
  132. // Ensure a separate level exists for the "wildcarding" name,
  133. // and mark the node as "wild".
  134. ZoneNode *node;
  135. zone_data.insertName(local_mem_sgmt_, wname.split(1), &node);
  136. node->setFlag(ZoneData::WILDCARD_NODE);
  137. // Ensure a separate level exists for the wildcard name.
  138. // Note: for 'name' itself we do this later anyway, but the
  139. // overhead should be marginal because wildcard names should
  140. // be rare.
  141. zone_data.insertName(local_mem_sgmt_, wname, &node);
  142. }
  143. }
  144. }
  145. /*
  146. * Does some checks in context of the data that are already in the zone.
  147. * Currently checks for forbidden combinations of RRsets in the same
  148. * domain (CNAME+anything, DNAME+NS).
  149. *
  150. * If such condition is found, it throws AddError.
  151. */
  152. void contextCheck(const Name& zone_name, const AbstractRRset& rrset,
  153. const RdataSet* set) const {
  154. // Ensure CNAME and other type of RR don't coexist for the same
  155. // owner name except with NSEC, which is the only RR that can coexist
  156. // with CNAME (and also RRSIG, which is handled separately)
  157. if (rrset.getType() == RRType::CNAME()) {
  158. for (const RdataSet* sp = set; sp != NULL; sp = sp->getNext()) {
  159. if (sp->type != RRType::NSEC()) {
  160. LOG_ERROR(logger, DATASRC_MEMORY_MEM_CNAME_TO_NONEMPTY).
  161. arg(rrset.getName());
  162. isc_throw(AddError, "CNAME can't be added with "
  163. << sp->type << " RRType for "
  164. << rrset.getName());
  165. }
  166. }
  167. } else if ((rrset.getType() != RRType::NSEC()) &&
  168. (RdataSet::find(set, RRType::CNAME()) != NULL)) {
  169. LOG_ERROR(logger,
  170. DATASRC_MEMORY_MEM_CNAME_COEXIST).arg(rrset.getName());
  171. isc_throw(AddError, "CNAME and " << rrset.getType() <<
  172. " can't coexist for " << rrset.getName());
  173. }
  174. /*
  175. * Similar with DNAME, but it must not coexist only with NS and only in
  176. * non-apex domains.
  177. * RFC 2672 section 3 mentions that it is implied from it and RFC 2181
  178. */
  179. if (rrset.getName() != zone_name &&
  180. // Adding DNAME, NS already there
  181. ((rrset.getType() == RRType::DNAME() &&
  182. RdataSet::find(set, RRType::NS()) != NULL) ||
  183. // Adding NS, DNAME already there
  184. (rrset.getType() == RRType::NS() &&
  185. RdataSet::find(set, RRType::DNAME()) != NULL)))
  186. {
  187. LOG_ERROR(logger, DATASRC_MEMORY_MEM_DNAME_NS).arg(rrset.getName());
  188. isc_throw(AddError, "DNAME can't coexist with NS in non-apex "
  189. "domain " << rrset.getName());
  190. }
  191. }
  192. // Validate rrset before adding it to the zone. If something is wrong
  193. // it throws an exception. It doesn't modify the zone, and provides
  194. // the strong exception guarantee.
  195. void addValidation(const Name& zone_name, const ConstRRsetPtr rrset) {
  196. if (!rrset) {
  197. isc_throw(NullRRset, "The rrset provided is NULL");
  198. }
  199. if (rrset->getRdataCount() == 0) {
  200. isc_throw(AddError, "The rrset provided is empty: " <<
  201. rrset->getName() << "/" << rrset->getType());
  202. }
  203. // Check for singleton RRs. It should probably handled at a different
  204. // layer in future.
  205. if ((rrset->getType() == RRType::CNAME() ||
  206. rrset->getType() == RRType::DNAME()) &&
  207. rrset->getRdataCount() > 1)
  208. {
  209. // XXX: this is not only for CNAME or DNAME. We should generalize
  210. // this code for all other "singleton RR types" (such as SOA) in a
  211. // separate task.
  212. LOG_ERROR(logger,
  213. DATASRC_MEMORY_MEM_SINGLETON).arg(rrset->getName()).
  214. arg(rrset->getType());
  215. isc_throw(AddError, "multiple RRs of singleton type for "
  216. << rrset->getName());
  217. }
  218. // NSEC3/NSEC3PARAM is not a "singleton" per protocol, but this
  219. // implementation requests it be so at the moment.
  220. if ((rrset->getType() == RRType::NSEC3() ||
  221. rrset->getType() == RRType::NSEC3PARAM()) &&
  222. rrset->getRdataCount() > 1) {
  223. isc_throw(AddError, "Multiple NSEC3/NSEC3PARAM RDATA is given for "
  224. << rrset->getName() << " which isn't supported");
  225. }
  226. NameComparisonResult compare(zone_name.compare(rrset->getName()));
  227. if (compare.getRelation() != NameComparisonResult::SUPERDOMAIN &&
  228. compare.getRelation() != NameComparisonResult::EQUAL)
  229. {
  230. LOG_ERROR(logger,
  231. DATASRC_MEMORY_MEM_OUT_OF_ZONE).arg(rrset->getName()).
  232. arg(zone_name);
  233. isc_throw(OutOfZone, "The name " << rrset->getName() <<
  234. " is not contained in zone " << zone_name);
  235. }
  236. // Some RR types do not really work well with a wildcard.
  237. // Even though the protocol specifically doesn't completely ban such
  238. // usage, we refuse to load a zone containing such RR in order to
  239. // keep the lookup logic simpler and more predictable.
  240. // See RFC4592 and (for DNAME) draft-ietf-dnsext-rfc2672bis-dname
  241. // for more technical background. Note also that BIND 9 refuses
  242. // NS at a wildcard, so in that sense we simply provide compatible
  243. // behavior.
  244. if (rrset->getName().isWildcard()) {
  245. if (rrset->getType() == RRType::NS()) {
  246. LOG_ERROR(logger, DATASRC_MEMORY_MEM_WILDCARD_NS).
  247. arg(rrset->getName());
  248. isc_throw(AddError, "Invalid NS owner name (wildcard): " <<
  249. rrset->getName());
  250. }
  251. if (rrset->getType() == RRType::DNAME()) {
  252. LOG_ERROR(logger, DATASRC_MEMORY_MEM_WILDCARD_DNAME).
  253. arg(rrset->getName());
  254. isc_throw(AddError, "Invalid DNAME owner name (wildcard): " <<
  255. rrset->getName());
  256. }
  257. }
  258. // Owner names of NSEC3 have special format as defined in RFC5155,
  259. // and cannot be a wildcard name or must be one label longer than
  260. // the zone origin. While the RFC doesn't prohibit other forms of
  261. // names, no sane zone would have such names for NSEC3.
  262. // BIND 9 also refuses NSEC3 at wildcard.
  263. if (rrset->getType() == RRType::NSEC3() &&
  264. (rrset->getName().isWildcard() ||
  265. rrset->getName().getLabelCount() !=
  266. zone_name.getLabelCount() + 1)) {
  267. LOG_ERROR(logger, DATASRC_MEMORY_BAD_NSEC3_NAME).
  268. arg(rrset->getName());
  269. isc_throw(AddError, "Invalid NSEC3 owner name: " <<
  270. rrset->getName());
  271. }
  272. }
  273. void addNSEC3(const ConstRRsetPtr rrset,
  274. const ConstRRsetPtr rrsig,
  275. ZoneData& zone_data) {
  276. // We know rrset has exactly one RDATA
  277. const generic::NSEC3& nsec3_rdata =
  278. dynamic_cast<const generic::NSEC3&>(
  279. rrset->getRdataIterator()->getCurrent());
  280. NSEC3Data* nsec3_data = zone_data.getNSEC3Data();
  281. if (nsec3_data == NULL) {
  282. nsec3_data = NSEC3Data::create(local_mem_sgmt_, nsec3_rdata);
  283. zone_data.setNSEC3Data(nsec3_data);
  284. } else {
  285. size_t salt_len = nsec3_data->getSaltLen();
  286. const uint8_t* salt_data = nsec3_data->getSaltData();
  287. const vector<uint8_t>& salt_data_2 = nsec3_rdata.getSalt();
  288. if ((nsec3_rdata.getHashalg() != nsec3_data->hashalg) ||
  289. (nsec3_rdata.getIterations() != nsec3_data->iterations) ||
  290. (salt_data_2.size() != salt_len) ||
  291. (std::memcmp(&salt_data_2[0], salt_data, salt_len) != 0)) {
  292. isc_throw(AddError,
  293. "NSEC3 with inconsistent parameters: " <<
  294. rrset->toText());
  295. }
  296. }
  297. string fst_label = rrset->getName().split(0, 1).toText(true);
  298. transform(fst_label.begin(), fst_label.end(), fst_label.begin(),
  299. ::toupper);
  300. ZoneNode *node;
  301. nsec3_data->insertName(local_mem_sgmt_, Name(fst_label), &node);
  302. RdataEncoder encoder;
  303. // We assume that rrsig has already been checked to match rrset
  304. // by the caller.
  305. RdataSet *set = RdataSet::create(local_mem_sgmt_, encoder,
  306. rrset, rrsig);
  307. RdataSet *old_set = node->setData(set);
  308. if (old_set != NULL) {
  309. RdataSet::destroy(local_mem_sgmt_, rrclass_, old_set);
  310. }
  311. }
  312. void addRdataSet(const Name& zone_name, ZoneData& zone_data,
  313. const ConstRRsetPtr rrset, const ConstRRsetPtr rrsig) {
  314. // Only one of these can be passed at a time.
  315. assert(!(rrset && rrsig));
  316. // If rrsig is passed, validate it against the last-saved rrset.
  317. if (rrsig) {
  318. // The covered RRset should have been saved by now.
  319. if (!last_rrset_) {
  320. isc_throw(AddError,
  321. "RRSIG is being added, "
  322. "but doesn't follow its covered RR: "
  323. << rrsig->getName());
  324. }
  325. if (rrsig->getName() != last_rrset_->getName()) {
  326. isc_throw(AddError,
  327. "RRSIG is being added, "
  328. "but doesn't match the last RR's name: "
  329. << last_rrset_->getName() << " vs. "
  330. << rrsig->getName());
  331. }
  332. // Consistency of other types in rrsig are checked in addRRsig().
  333. RdataIteratorPtr rit = rrsig->getRdataIterator();
  334. const RRType covered = dynamic_cast<const generic::RRSIG&>(
  335. rit->getCurrent()).typeCovered();
  336. if (covered != last_rrset_->getType()) {
  337. isc_throw(AddError,
  338. "RRSIG is being added, "
  339. "but doesn't match the last RR's type: "
  340. << last_rrset_->getType() << " vs. "
  341. << covered);
  342. }
  343. }
  344. if (!last_rrset_) {
  345. last_rrset_ = rrset;
  346. return;
  347. }
  348. if (last_rrset_->getType() == RRType::NSEC3()) {
  349. addNSEC3(last_rrset_, rrsig, zone_data);
  350. } else {
  351. ZoneNode* node;
  352. zone_data.insertName(local_mem_sgmt_,
  353. last_rrset_->getName(), &node);
  354. RdataSet* set = node->getData();
  355. // Checks related to the surrounding data.
  356. // Note: when the check fails and the exception is thrown,
  357. // it may break strong exception guarantee. At the moment
  358. // we prefer code simplicity and don't bother to introduce
  359. // complicated recovery code.
  360. contextCheck(zone_name, *last_rrset_, set);
  361. if (RdataSet::find(set, last_rrset_->getType()) != NULL) {
  362. isc_throw(AddError,
  363. "RRset of the type already exists: "
  364. << last_rrset_->getName() << " (type: "
  365. << last_rrset_->getType() << ")");
  366. }
  367. RdataEncoder encoder;
  368. RdataSet *new_set = RdataSet::create(local_mem_sgmt_, encoder,
  369. last_rrset_, rrsig);
  370. new_set->next = set;
  371. node->setData(new_set);
  372. // Ok, we just put it in
  373. // If this RRset creates a zone cut at this node, mark the
  374. // node indicating the need for callback in find().
  375. if (last_rrset_->getType() == RRType::NS() &&
  376. last_rrset_->getName() != zone_name) {
  377. node->setFlag(ZoneNode::FLAG_CALLBACK);
  378. // If it is DNAME, we have a callback as well here
  379. } else if (last_rrset_->getType() == RRType::DNAME()) {
  380. node->setFlag(ZoneNode::FLAG_CALLBACK);
  381. }
  382. // If we've added NSEC3PARAM at zone origin, set up NSEC3
  383. // specific data or check consistency with already set up
  384. // parameters.
  385. if (last_rrset_->getType() == RRType::NSEC3PARAM() &&
  386. last_rrset_->getName() == zone_name) {
  387. // We know rrset has exactly one RDATA
  388. const generic::NSEC3PARAM& param =
  389. dynamic_cast<const generic::NSEC3PARAM&>
  390. (last_rrset_->getRdataIterator()->getCurrent());
  391. NSEC3Data* nsec3_data = zone_data.getNSEC3Data();
  392. if (nsec3_data == NULL) {
  393. nsec3_data = NSEC3Data::create(local_mem_sgmt_, param);
  394. zone_data.setNSEC3Data(nsec3_data);
  395. } else {
  396. size_t salt_len = nsec3_data->getSaltLen();
  397. const uint8_t* salt_data = nsec3_data->getSaltData();
  398. const vector<uint8_t>& salt_data_2 = param.getSalt();
  399. if ((param.getHashalg() != nsec3_data->hashalg) ||
  400. (param.getIterations() != nsec3_data->iterations) ||
  401. (salt_data_2.size() != salt_len) ||
  402. (std::memcmp(&salt_data_2[0],
  403. salt_data, salt_len) != 0)) {
  404. isc_throw(AddError,
  405. "NSEC3PARAM with inconsistent parameters: "
  406. << last_rrset_->toText());
  407. }
  408. }
  409. } else if (last_rrset_->getType() == RRType::NSEC()) {
  410. // If it is NSEC signed zone, so we put a flag there
  411. // (flag is enough)
  412. zone_data.setSigned(true);
  413. }
  414. }
  415. last_rrset_ = rrset;
  416. }
  417. result::Result addRRsig(const ConstRRsetPtr sig_rrset,
  418. const Name& zone_name, ZoneData& zone_data)
  419. {
  420. // Check consistency of the type covered.
  421. // We know the RRset isn't empty, so the following check is safe.
  422. RdataIteratorPtr rit = sig_rrset->getRdataIterator();
  423. const RRType covered = dynamic_cast<const generic::RRSIG&>(
  424. rit->getCurrent()).typeCovered();
  425. for (rit->next(); !rit->isLast(); rit->next()) {
  426. if (dynamic_cast<const generic::RRSIG&>(
  427. rit->getCurrent()).typeCovered() != covered) {
  428. isc_throw(AddError, "RRSIG contains mixed covered types: "
  429. << sig_rrset->toText());
  430. }
  431. }
  432. addRdataSet(zone_name, zone_data, ConstRRsetPtr(), sig_rrset);
  433. return (result::SUCCESS);
  434. }
  435. /*
  436. * Implementation of longer methods. We put them here, because the
  437. * access is without the impl_-> and it will get inlined anyway.
  438. */
  439. // Implementation of InMemoryClient::add()
  440. result::Result add(const ConstRRsetPtr& rrset,
  441. const Name& zone_name, ZoneData& zone_data)
  442. {
  443. // Sanitize input. This will cause an exception to be thrown
  444. // if the input RRset is empty.
  445. addValidation(zone_name, rrset);
  446. // OK, can add the RRset.
  447. LOG_DEBUG(logger, DBG_TRACE_DATA, DATASRC_MEMORY_MEM_ADD_RRSET).
  448. arg(rrset->getName()).arg(rrset->getType()).arg(zone_name);
  449. if (rrset->getType() == RRType::NSEC3()) {
  450. addRdataSet(zone_name, zone_data, rrset, ConstRRsetPtr());
  451. return (result::SUCCESS);
  452. }
  453. // RRSIGs are special in various points, so we handle it in a
  454. // separate dedicated method.
  455. if (rrset->getType() == RRType::RRSIG()) {
  456. return (addRRsig(rrset, zone_name, zone_data));
  457. }
  458. // Add wildcards possibly contained in the owner name to the domain
  459. // tree.
  460. // Note: this can throw an exception, breaking strong exception
  461. // guarantee. (see also the note for contextCheck() below).
  462. addWildcards(zone_name, zone_data, rrset->getName());
  463. addRdataSet(zone_name, zone_data, rrset, ConstRRsetPtr());
  464. return (result::SUCCESS);
  465. }
  466. /*
  467. * Same as above, but it checks the return value and if it already exists,
  468. * it throws.
  469. */
  470. void addFromLoad(const ConstRRsetPtr& set,
  471. const Name& zone_name, ZoneData* zone_data)
  472. {
  473. switch (add(set, zone_name, *zone_data)) {
  474. case result::SUCCESS:
  475. return;
  476. default:
  477. assert(0);
  478. }
  479. }
  480. };
  481. result::Result
  482. InMemoryClient::InMemoryClientImpl::load(
  483. const Name& zone_name,
  484. const string& filename,
  485. boost::function<void(LoadCallback)> rrset_installer)
  486. {
  487. SegmentObjectHolder<ZoneData, RRClass> holder(
  488. local_mem_sgmt_, ZoneData::create(local_mem_sgmt_, zone_name),
  489. rrclass_);
  490. assert(!last_rrset_);
  491. try {
  492. rrset_installer(boost::bind(&InMemoryClientImpl::addFromLoad, this,
  493. _1, zone_name, holder.get()));
  494. // Add any last RRset that was left
  495. addRdataSet(zone_name, *holder.get(),
  496. ConstRRsetPtr(), ConstRRsetPtr());
  497. } catch (...) {
  498. last_rrset_ = ConstRRsetPtr();
  499. throw;
  500. }
  501. assert(!last_rrset_);
  502. const ZoneNode* origin_node = holder.get()->getOriginNode();
  503. const RdataSet* set = origin_node->getData();
  504. // If the zone is NSEC3-signed, check if it has NSEC3PARAM
  505. if (holder.get()->isNSEC3Signed()) {
  506. // Note: origin_data_ is set on creation of ZoneData, and the load
  507. // process only adds new nodes (and their data), so this assertion
  508. // should hold.
  509. if (RdataSet::find(set, RRType::NSEC3PARAM()) == NULL) {
  510. LOG_WARN(logger, DATASRC_MEMORY_MEM_NO_NSEC3PARAM).
  511. arg(zone_name).arg(rrclass_);
  512. }
  513. }
  514. // When an empty zone file is loaded, the origin doesn't even have
  515. // an SOA RR. This condition should be avoided, and hence load()
  516. // should throw when an empty zone is loaded.
  517. if (RdataSet::find(set, RRType::SOA()) == NULL) {
  518. isc_throw(EmptyZone,
  519. "Won't create an empty zone for: " << zone_name);
  520. }
  521. LOG_DEBUG(logger, DBG_TRACE_BASIC, DATASRC_MEMORY_MEM_ADD_ZONE).
  522. arg(zone_name).arg(rrclass_.toText());
  523. // Set the filename in file_name_tree_ now, so that getFileName()
  524. // can use it (during zone reloading).
  525. FileNameNode* node(NULL);
  526. switch (file_name_tree_->insert(local_mem_sgmt_,
  527. zone_name, &node)) {
  528. case FileNameTree::SUCCESS:
  529. case FileNameTree::ALREADYEXISTS:
  530. // These are OK
  531. break;
  532. default:
  533. // Can Not Happen
  534. assert(false);
  535. }
  536. // node must point to a valid node now
  537. assert(node != NULL);
  538. std::string* tstr = node->setData(new std::string(filename));
  539. delete tstr;
  540. ZoneTable::AddResult result(zone_table_->addZone(local_mem_sgmt_,
  541. rrclass_, zone_name));
  542. if (result.code == result::SUCCESS) {
  543. // Only increment the zone count if the zone doesn't already
  544. // exist.
  545. ++zone_count_;
  546. }
  547. ZoneData *data = zone_table_->setZoneData(zone_name, holder.release());
  548. if (data != NULL) {
  549. ZoneData::destroy(local_mem_sgmt_, data, rrclass_);
  550. }
  551. return (result.code);
  552. }
  553. namespace {
  554. // A wrapper for dns::masterLoad used by load() below. Essentially it
  555. // converts the two callback types. Note the mostly redundant wrapper of
  556. // boost::bind. It converts function<void(ConstRRsetPtr)> to
  557. // function<void(RRsetPtr)> (masterLoad() expects the latter). SunStudio
  558. // doesn't seem to do this conversion if we just pass 'callback'.
  559. void
  560. masterLoadWrapper(const char* const filename, const Name& origin,
  561. const RRClass& zone_class, LoadCallback callback)
  562. {
  563. masterLoad(filename, origin, zone_class, boost::bind(callback, _1));
  564. }
  565. // The installer called from Impl::load() for the iterator version of load().
  566. void
  567. generateRRsetFromIterator(ZoneIterator* iterator, LoadCallback callback) {
  568. ConstRRsetPtr rrset;
  569. vector<ConstRRsetPtr> rrsigs; // placeholder for RRSIGs until "commitable".
  570. // The current internal implementation assumes an RRSIG is always added
  571. // after the RRset they cover. So we store any RRSIGs in 'rrsigs' until
  572. // it's safe to add them; based on our assumption if the owner name
  573. // changes, all covered RRsets of the previous name should have been
  574. // installed and any pending RRSIGs can be added at that point. RRSIGs
  575. // of the last name from the iterator must be added separately.
  576. while ((rrset = iterator->getNextRRset()) != NULL) {
  577. if (!rrsigs.empty() && rrset->getName() != rrsigs[0]->getName()) {
  578. BOOST_FOREACH(ConstRRsetPtr sig_rrset, rrsigs) {
  579. callback(sig_rrset);
  580. }
  581. rrsigs.clear();
  582. }
  583. if (rrset->getType() == RRType::RRSIG()) {
  584. rrsigs.push_back(rrset);
  585. } else {
  586. callback(rrset);
  587. }
  588. }
  589. BOOST_FOREACH(ConstRRsetPtr sig_rrset, rrsigs) {
  590. callback(sig_rrset);
  591. }
  592. }
  593. }
  594. InMemoryClient::InMemoryClient(RRClass rrclass) :
  595. impl_(new InMemoryClientImpl(rrclass))
  596. {}
  597. InMemoryClient::~InMemoryClient() {
  598. delete impl_;
  599. }
  600. RRClass
  601. InMemoryClient::getClass() const {
  602. return (impl_->rrclass_);
  603. }
  604. unsigned int
  605. InMemoryClient::getZoneCount() const {
  606. return (impl_->zone_count_);
  607. }
  608. isc::datasrc::memory::ZoneTable::FindResult
  609. InMemoryClient::findZone2(const isc::dns::Name& zone_name) const {
  610. LOG_DEBUG(logger, DBG_TRACE_DATA,
  611. DATASRC_MEMORY_MEM_FIND_ZONE).arg(zone_name);
  612. ZoneTable::FindResult result(impl_->zone_table_->findZone(zone_name));
  613. return (result);
  614. }
  615. isc::datasrc::DataSourceClient::FindResult
  616. InMemoryClient::findZone(const isc::dns::Name&) const {
  617. // This variant of findZone() is not implemented and should be
  618. // removed eventually. It currently throws an exception. It is
  619. // required right now to derive from DataSourceClient.
  620. isc_throw(isc::NotImplemented,
  621. "This variant of findZone() is not implemented.");
  622. }
  623. result::Result
  624. InMemoryClient::load(const isc::dns::Name& zone_name,
  625. const std::string& filename) {
  626. LOG_DEBUG(logger, DBG_TRACE_BASIC, DATASRC_MEMORY_MEM_LOAD).arg(zone_name).
  627. arg(filename);
  628. return (impl_->load(zone_name, filename,
  629. boost::bind(masterLoadWrapper, filename.c_str(),
  630. zone_name, getClass(), _1)));
  631. }
  632. result::Result
  633. InMemoryClient::load(const isc::dns::Name& zone_name,
  634. ZoneIterator& iterator) {
  635. return (impl_->load(zone_name, string(),
  636. boost::bind(generateRRsetFromIterator,
  637. &iterator, _1)));
  638. }
  639. const std::string
  640. InMemoryClient::getFileName(const isc::dns::Name& zone_name) const {
  641. FileNameNode* node(NULL);
  642. FileNameTree::Result result = impl_->file_name_tree_->find(zone_name,
  643. &node);
  644. if (result == FileNameTree::EXACTMATCH) {
  645. return (*node->getData());
  646. } else {
  647. return (std::string());
  648. }
  649. }
  650. result::Result
  651. InMemoryClient::add(const isc::dns::Name& zone_name,
  652. const ConstRRsetPtr& rrset) {
  653. assert(!impl_->last_rrset_);
  654. ZoneTable::FindResult result(impl_->zone_table_->findZone(zone_name));
  655. if (result.code != result::SUCCESS) {
  656. isc_throw(DataSourceError, "No such zone: " + zone_name.toText());
  657. }
  658. result::Result ret(impl_->add(rrset, zone_name, *result.zone_data));
  659. // Add any associated RRSIG too. This has to be done here, as both
  660. // the RRset and its RRSIG have to be passed when constructing an
  661. // RdataSet.
  662. if ((ret == result::SUCCESS) && rrset->getRRsig()) {
  663. impl_->add(rrset->getRRsig(), zone_name, *result.zone_data);
  664. }
  665. // Add any last RRset that was left
  666. impl_->addRdataSet(zone_name, *result.zone_data,
  667. ConstRRsetPtr(), ConstRRsetPtr());
  668. assert(!impl_->last_rrset_);
  669. return (ret);
  670. }
  671. namespace {
  672. class MemoryIterator : public ZoneIterator {
  673. private:
  674. ZoneChain chain_;
  675. const RdataSet* set_node_;
  676. const RRClass rrclass_;
  677. const ZoneTree& tree_;
  678. const ZoneNode* node_;
  679. // Only used when separate_rrs_ is true
  680. ConstRRsetPtr rrset_;
  681. RdataIteratorPtr rdata_iterator_;
  682. bool separate_rrs_;
  683. bool ready_;
  684. public:
  685. MemoryIterator(const RRClass rrclass,
  686. const ZoneTree& tree, const Name& origin,
  687. bool separate_rrs) :
  688. rrclass_(rrclass),
  689. tree_(tree),
  690. separate_rrs_(separate_rrs),
  691. ready_(true)
  692. {
  693. // Find the first node (origin) and preserve the node chain for future
  694. // searches
  695. ZoneTree::Result result(tree_.find(origin, &node_, chain_));
  696. // It can't happen that the origin is not in there
  697. if (result != ZoneTree::EXACTMATCH) {
  698. isc_throw(Unexpected,
  699. "In-memory zone corrupted, missing origin node");
  700. }
  701. // Initialize the iterator if there's somewhere to point to
  702. if (node_ != NULL && node_->getData() != NULL) {
  703. set_node_ = node_->getData();
  704. if (separate_rrs_ && set_node_ != NULL) {
  705. rrset_.reset(new TreeNodeRRset(rrclass_,
  706. node_, set_node_, true));
  707. rdata_iterator_ = rrset_->getRdataIterator();
  708. }
  709. }
  710. }
  711. virtual ConstRRsetPtr getNextRRset() {
  712. if (!ready_) {
  713. isc_throw(Unexpected, "Iterating past the zone end");
  714. }
  715. /*
  716. * This cycle finds the first nonempty node with yet unused
  717. * RdataSset. If it is NULL, we run out of nodes. If it is
  718. * empty, it doesn't contain any RdataSets. If we are at the
  719. * end, just get to next one.
  720. */
  721. while (node_ != NULL &&
  722. (node_->getData() == NULL || set_node_ == NULL)) {
  723. node_ = tree_.nextNode(chain_);
  724. // If there's a node, initialize the iterator and check next time
  725. // if the map is empty or not
  726. if (node_ != NULL && node_->getData() != NULL) {
  727. set_node_ = node_->getData();
  728. // New RRset, so get a new rdata iterator
  729. if (separate_rrs_ && set_node_ != NULL) {
  730. rrset_.reset(new TreeNodeRRset(rrclass_,
  731. node_, set_node_, true));
  732. rdata_iterator_ = rrset_->getRdataIterator();
  733. }
  734. }
  735. }
  736. if (node_ == NULL) {
  737. // That's all, folks
  738. ready_ = false;
  739. return (ConstRRsetPtr());
  740. }
  741. if (separate_rrs_) {
  742. // For separate rrs, reconstruct a new RRset with just the
  743. // 'current' rdata
  744. RRsetPtr result(new RRset(rrset_->getName(),
  745. rrset_->getClass(),
  746. rrset_->getType(),
  747. rrset_->getTTL()));
  748. result->addRdata(rdata_iterator_->getCurrent());
  749. rdata_iterator_->next();
  750. if (rdata_iterator_->isLast()) {
  751. // all used up, next.
  752. set_node_ = set_node_->getNext();
  753. // New RRset, so get a new rdata iterator, but only if this
  754. // was not the final RRset in the chain
  755. if (set_node_ != NULL) {
  756. rrset_.reset(new TreeNodeRRset(rrclass_,
  757. node_, set_node_, true));
  758. rdata_iterator_ = rrset_->getRdataIterator();
  759. }
  760. }
  761. return (result);
  762. } else {
  763. ConstRRsetPtr result(new TreeNodeRRset(rrclass_,
  764. node_, set_node_, true));
  765. // This one is used, move it to the next time for next call
  766. set_node_ = set_node_->getNext();
  767. return (result);
  768. }
  769. }
  770. virtual ConstRRsetPtr getSOA() const {
  771. isc_throw(NotImplemented, "Not implemented");
  772. }
  773. };
  774. } // End of anonymous namespace
  775. ZoneIteratorPtr
  776. InMemoryClient::getIterator(const Name& name, bool separate_rrs) const {
  777. ZoneTable::FindResult result(impl_->zone_table_->findZone(name));
  778. if (result.code != result::SUCCESS) {
  779. isc_throw(DataSourceError, "No such zone: " + name.toText());
  780. }
  781. return (ZoneIteratorPtr(new MemoryIterator(
  782. getClass(),
  783. result.zone_data->getZoneTree(), name,
  784. separate_rrs)));
  785. }
  786. ZoneUpdaterPtr
  787. InMemoryClient::getUpdater(const isc::dns::Name&, bool, bool) const {
  788. isc_throw(isc::NotImplemented, "Update attempt on in memory data source");
  789. }
  790. pair<ZoneJournalReader::Result, ZoneJournalReaderPtr>
  791. InMemoryClient::getJournalReader(const isc::dns::Name&, uint32_t,
  792. uint32_t) const
  793. {
  794. isc_throw(isc::NotImplemented, "Journaling isn't supported for "
  795. "in memory data source");
  796. }
  797. } // end of namespace memory
  798. } // end of namespace datasrc
  799. } // end of namespace isc