memory_datasrc.cc 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  1. // Copyright (C) 2010 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 <map>
  15. #include <cassert>
  16. #include <boost/shared_ptr.hpp>
  17. #include <boost/bind.hpp>
  18. #include <dns/name.h>
  19. #include <dns/rrclass.h>
  20. #include <dns/rrsetlist.h>
  21. #include <dns/masterload.h>
  22. #include <datasrc/memory_datasrc.h>
  23. #include <datasrc/rbtree.h>
  24. #include <datasrc/logger.h>
  25. using namespace std;
  26. using namespace isc::dns;
  27. namespace isc {
  28. namespace datasrc {
  29. // Private data and hidden methods of InMemoryZoneFinder
  30. struct InMemoryZoneFinder::InMemoryZoneFinderImpl {
  31. // Constructor
  32. InMemoryZoneFinderImpl(const RRClass& zone_class, const Name& origin) :
  33. zone_class_(zone_class), origin_(origin), origin_data_(NULL),
  34. domains_(true)
  35. {
  36. // We create the node for origin (it needs to exist anyway in future)
  37. domains_.insert(origin, &origin_data_);
  38. DomainPtr origin_domain(new Domain);
  39. origin_data_->setData(origin_domain);
  40. }
  41. // Some type aliases
  42. /*
  43. * Each domain consists of some RRsets. They will be looked up by the
  44. * RRType.
  45. *
  46. * The use of map is questionable with regard to performance - there'll
  47. * be usually only few RRsets in the domain, so the log n benefit isn't
  48. * much and a vector/array might be faster due to its simplicity and
  49. * continuous memory location. But this is unlikely to be a performance
  50. * critical place and map has better interface for the lookups, so we use
  51. * that.
  52. */
  53. typedef map<RRType, ConstRRsetPtr> Domain;
  54. typedef Domain::value_type DomainPair;
  55. typedef boost::shared_ptr<Domain> DomainPtr;
  56. // The tree stores domains
  57. typedef RBTree<Domain> DomainTree;
  58. typedef RBNode<Domain> DomainNode;
  59. static const DomainNode::Flags DOMAINFLAG_WILD = DomainNode::FLAG_USER1;
  60. // Information about the zone
  61. RRClass zone_class_;
  62. Name origin_;
  63. DomainNode* origin_data_;
  64. string file_name_;
  65. // The actual zone data
  66. DomainTree domains_;
  67. // Add the necessary magic for any wildcard contained in 'name'
  68. // (including itself) to be found in the zone.
  69. //
  70. // In order for wildcard matching to work correctly in find(),
  71. // we must ensure that a node for the wildcarding level exists in the
  72. // backend RBTree.
  73. // E.g. if the wildcard name is "*.sub.example." then we must ensure
  74. // that "sub.example." exists and is marked as a wildcard level.
  75. // Note: the "wildcarding level" is for the parent name of the wildcard
  76. // name (such as "sub.example.").
  77. //
  78. // We also perform the same trick for empty wild card names possibly
  79. // contained in 'name' (e.g., '*.foo.example' in 'bar.*.foo.example').
  80. void addWildcards(DomainTree& domains, const Name& name) {
  81. Name wname(name);
  82. const unsigned int labels(wname.getLabelCount());
  83. const unsigned int origin_labels(origin_.getLabelCount());
  84. for (unsigned int l = labels;
  85. l > origin_labels;
  86. --l, wname = wname.split(1)) {
  87. if (wname.isWildcard()) {
  88. LOG_DEBUG(logger, DBG_TRACE_DATA, DATASRC_MEM_ADD_WILDCARD).
  89. arg(name);
  90. // Ensure a separate level exists for the "wildcarding" name,
  91. // and mark the node as "wild".
  92. DomainNode* node;
  93. DomainTree::Result result(domains.insert(wname.split(1),
  94. &node));
  95. assert(result == DomainTree::SUCCESS ||
  96. result == DomainTree::ALREADYEXISTS);
  97. node->setFlag(DOMAINFLAG_WILD);
  98. // Ensure a separate level exists for the wildcard name.
  99. // Note: for 'name' itself we do this later anyway, but the
  100. // overhead should be marginal because wildcard names should
  101. // be rare.
  102. result = domains.insert(wname, &node);
  103. assert(result == DomainTree::SUCCESS ||
  104. result == DomainTree::ALREADYEXISTS);
  105. }
  106. }
  107. }
  108. /*
  109. * Does some checks in context of the data that are already in the zone.
  110. * Currently checks for forbidden combinations of RRsets in the same
  111. * domain (CNAME+anything, DNAME+NS).
  112. *
  113. * If such condition is found, it throws AddError.
  114. */
  115. void contextCheck(const ConstRRsetPtr& rrset,
  116. const DomainPtr& domain) const {
  117. // Ensure CNAME and other type of RR don't coexist for the same
  118. // owner name.
  119. if (rrset->getType() == RRType::CNAME()) {
  120. // TODO: this check will become incorrect when we support DNSSEC
  121. // (depending on how we support DNSSEC). We should revisit it
  122. // at that point.
  123. if (!domain->empty()) {
  124. LOG_ERROR(logger, DATASRC_MEM_CNAME_TO_NONEMPTY).
  125. arg(rrset->getName());
  126. isc_throw(AddError, "CNAME can't be added with other data for "
  127. << rrset->getName());
  128. }
  129. } else if (domain->find(RRType::CNAME()) != domain->end()) {
  130. LOG_ERROR(logger, DATASRC_MEM_CNAME_COEXIST).arg(rrset->getName());
  131. isc_throw(AddError, "CNAME and " << rrset->getType() <<
  132. " can't coexist for " << rrset->getName());
  133. }
  134. /*
  135. * Similar with DNAME, but it must not coexist only with NS and only in
  136. * non-apex domains.
  137. * RFC 2672 section 3 mentions that it is implied from it and RFC 2181
  138. */
  139. if (rrset->getName() != origin_ &&
  140. // Adding DNAME, NS already there
  141. ((rrset->getType() == RRType::DNAME() &&
  142. domain->find(RRType::NS()) != domain->end()) ||
  143. // Adding NS, DNAME already there
  144. (rrset->getType() == RRType::NS() &&
  145. domain->find(RRType::DNAME()) != domain->end())))
  146. {
  147. LOG_ERROR(logger, DATASRC_MEM_DNAME_NS).arg(rrset->getName());
  148. isc_throw(AddError, "DNAME can't coexist with NS in non-apex "
  149. "domain " << rrset->getName());
  150. }
  151. }
  152. // Validate rrset before adding it to the zone. If something is wrong
  153. // it throws an exception. It doesn't modify the zone, and provides
  154. // the strong exception guarantee.
  155. void addValidation(const ConstRRsetPtr rrset) {
  156. if (!rrset) {
  157. isc_throw(NullRRset, "The rrset provided is NULL");
  158. }
  159. // Check for singleton RRs. It should probably handled at a different
  160. // in future.
  161. if ((rrset->getType() == RRType::CNAME() ||
  162. rrset->getType() == RRType::DNAME()) &&
  163. rrset->getRdataCount() > 1)
  164. {
  165. // XXX: this is not only for CNAME or DNAME. We should generalize
  166. // this code for all other "singleton RR types" (such as SOA) in a
  167. // separate task.
  168. LOG_ERROR(logger, DATASRC_MEM_SINGLETON).arg(rrset->getName()).
  169. arg(rrset->getType());
  170. isc_throw(AddError, "multiple RRs of singleton type for "
  171. << rrset->getName());
  172. }
  173. NameComparisonResult compare(origin_.compare(rrset->getName()));
  174. if (compare.getRelation() != NameComparisonResult::SUPERDOMAIN &&
  175. compare.getRelation() != NameComparisonResult::EQUAL)
  176. {
  177. LOG_ERROR(logger, DATASRC_MEM_OUT_OF_ZONE).arg(rrset->getName()).
  178. arg(origin_);
  179. isc_throw(OutOfZone, "The name " << rrset->getName() <<
  180. " is not contained in zone " << origin_);
  181. }
  182. // Some RR types do not really work well with a wildcard.
  183. // Even though the protocol specifically doesn't completely ban such
  184. // usage, we refuse to load a zone containing such RR in order to
  185. // keep the lookup logic simpler and more predictable.
  186. // See RFC4592 and (for DNAME) draft-ietf-dnsext-rfc2672bis-dname
  187. // for more technical background. Note also that BIND 9 refuses
  188. // NS at a wildcard, so in that sense we simply provide compatible
  189. // behavior.
  190. if (rrset->getName().isWildcard()) {
  191. if (rrset->getType() == RRType::NS()) {
  192. LOG_ERROR(logger, DATASRC_MEM_WILDCARD_NS).
  193. arg(rrset->getName());
  194. isc_throw(AddError, "Invalid NS owner name (wildcard): " <<
  195. rrset->getName());
  196. }
  197. if (rrset->getType() == RRType::DNAME()) {
  198. LOG_ERROR(logger, DATASRC_MEM_WILDCARD_DNAME).
  199. arg(rrset->getName());
  200. isc_throw(AddError, "Invalid DNAME owner name (wildcard): " <<
  201. rrset->getName());
  202. }
  203. }
  204. }
  205. /*
  206. * Implementation of longer methods. We put them here, because the
  207. * access is without the impl_-> and it will get inlined anyway.
  208. */
  209. // Implementation of InMemoryZoneFinder::add
  210. result::Result add(const ConstRRsetPtr& rrset, DomainTree* domains) {
  211. // Sanitize input. This will cause an exception to be thrown
  212. // if the input RRset is empty.
  213. addValidation(rrset);
  214. // OK, can add the RRset.
  215. LOG_DEBUG(logger, DBG_TRACE_DATA, DATASRC_MEM_ADD_RRSET).
  216. arg(rrset->getName()).arg(rrset->getType()).arg(origin_);
  217. // Add wildcards possibly contained in the owner name to the domain
  218. // tree.
  219. // Note: this can throw an exception, breaking strong exception
  220. // guarantee. (see also the note for contextCheck() below).
  221. addWildcards(*domains, rrset->getName());
  222. // Get the node
  223. DomainNode* node;
  224. DomainTree::Result result = domains->insert(rrset->getName(), &node);
  225. // Just check it returns reasonable results
  226. assert((result == DomainTree::SUCCESS ||
  227. result == DomainTree::ALREADYEXISTS) && node!= NULL);
  228. // Now get the domain
  229. DomainPtr domain;
  230. // It didn't exist yet, create it
  231. if (node->isEmpty()) {
  232. domain.reset(new Domain);
  233. node->setData(domain);
  234. } else { // Get existing one
  235. domain = node->getData();
  236. }
  237. // Checks related to the surrounding data.
  238. // Note: when the check fails and the exception is thrown, it may
  239. // break strong exception guarantee. At the moment we prefer
  240. // code simplicity and don't bother to introduce complicated
  241. // recovery code.
  242. contextCheck(rrset, domain);
  243. // Try inserting the rrset there
  244. if (domain->insert(DomainPair(rrset->getType(), rrset)).second) {
  245. // Ok, we just put it in
  246. // If this RRset creates a zone cut at this node, mark the node
  247. // indicating the need for callback in find().
  248. if (rrset->getType() == RRType::NS() &&
  249. rrset->getName() != origin_) {
  250. node->setFlag(DomainNode::FLAG_CALLBACK);
  251. // If it is DNAME, we have a callback as well here
  252. } else if (rrset->getType() == RRType::DNAME()) {
  253. node->setFlag(DomainNode::FLAG_CALLBACK);
  254. }
  255. return (result::SUCCESS);
  256. } else {
  257. // The RRSet of given type was already there
  258. return (result::EXIST);
  259. }
  260. }
  261. /*
  262. * Same as above, but it checks the return value and if it already exists,
  263. * it throws.
  264. */
  265. void addFromLoad(const ConstRRsetPtr& set, DomainTree* domains) {
  266. switch (add(set, domains)) {
  267. case result::EXIST:
  268. LOG_ERROR(logger, DATASRC_MEM_DUP_RRSET).
  269. arg(set->getName()).arg(set->getType());
  270. isc_throw(dns::MasterLoadError, "Duplicate rrset: " <<
  271. set->toText());
  272. case result::SUCCESS:
  273. return;
  274. default:
  275. assert(0);
  276. }
  277. }
  278. // Maintain intermediate data specific to the search context used in
  279. /// \c find().
  280. ///
  281. /// It will be passed to \c zonecutCallback() and record a possible
  282. /// zone cut node and related RRset (normally NS or DNAME).
  283. struct FindState {
  284. FindState(FindOptions options) :
  285. zonecut_node_(NULL),
  286. dname_node_(NULL),
  287. options_(options)
  288. {}
  289. const DomainNode* zonecut_node_;
  290. const DomainNode* dname_node_;
  291. ConstRRsetPtr rrset_;
  292. const FindOptions options_;
  293. };
  294. // A callback called from possible zone cut nodes and nodes with DNAME.
  295. // This will be passed from the \c find() method to \c RBTree::find().
  296. static bool cutCallback(const DomainNode& node, FindState* state) {
  297. // We need to look for DNAME first, there's allowed case where
  298. // DNAME and NS coexist in the apex. DNAME is the one to notice,
  299. // the NS is authoritative, not delegation (corner case explicitly
  300. // allowed by section 3 of 2672)
  301. const Domain::const_iterator foundDNAME(node.getData()->find(
  302. RRType::DNAME()));
  303. if (foundDNAME != node.getData()->end()) {
  304. LOG_DEBUG(logger, DBG_TRACE_DETAILED,
  305. DATASRC_MEM_DNAME_ENCOUNTERED);
  306. state->dname_node_ = &node;
  307. state->rrset_ = foundDNAME->second;
  308. // No more processing below the DNAME (RFC 2672, section 3
  309. // forbids anything to exist below it, so there's no need
  310. // to actually search for it). This is strictly speaking
  311. // a different way than described in 4.1 of that RFC,
  312. // but because of the assumption in section 3, it has the
  313. // same behaviour.
  314. return (true);
  315. }
  316. // Look for NS
  317. const Domain::const_iterator foundNS(node.getData()->find(
  318. RRType::NS()));
  319. if (foundNS != node.getData()->end()) {
  320. // We perform callback check only for the highest zone cut in the
  321. // rare case of nested zone cuts.
  322. if (state->zonecut_node_ != NULL) {
  323. return (false);
  324. }
  325. LOG_DEBUG(logger, DBG_TRACE_DETAILED, DATASRC_MEM_NS_ENCOUNTERED);
  326. // BIND 9 checks if this node is not the origin. That's probably
  327. // because it can support multiple versions for dynamic updates
  328. // and IXFR, and it's possible that the callback is called at
  329. // the apex and the DNAME doesn't exist for a particular version.
  330. // It cannot happen for us (at least for now), so we don't do
  331. // that check.
  332. state->zonecut_node_ = &node;
  333. state->rrset_ = foundNS->second;
  334. // Unless glue is allowed the search stops here, so we return
  335. // false; otherwise return true to continue the search.
  336. return ((state->options_ & FIND_GLUE_OK) == 0);
  337. }
  338. // This case should not happen because we enable callback only
  339. // when we add an RR searched for above.
  340. assert(0);
  341. // This is here to avoid warning (therefore compilation error)
  342. // in case assert is turned off. Otherwise we could get "Control
  343. // reached end of non-void function".
  344. return (false);
  345. }
  346. /*
  347. * Prepares a rrset to be return as a result.
  348. *
  349. * If rename is false, it returns the one provided. If it is true, it
  350. * creates a new rrset with the same data but with provided name.
  351. * It is designed for wildcard case, where we create the rrsets
  352. * dynamically.
  353. */
  354. static ConstRRsetPtr prepareRRset(const Name& name, const ConstRRsetPtr&
  355. rrset, bool rename)
  356. {
  357. if (rename) {
  358. LOG_DEBUG(logger, DBG_TRACE_DETAILED, DATASRC_MEM_RENAME).
  359. arg(rrset->getName()).arg(name);
  360. /*
  361. * We lose a signature here. But it would be wrong anyway, because
  362. * the name changed. This might turn out to be unimportant in
  363. * future, because wildcards will probably be handled somehow
  364. * by DNSSEC.
  365. */
  366. RRsetPtr result(new RRset(name, rrset->getClass(),
  367. rrset->getType(), rrset->getTTL()));
  368. for (RdataIteratorPtr i(rrset->getRdataIterator()); !i->isLast();
  369. i->next()) {
  370. result->addRdata(i->getCurrent());
  371. }
  372. return (result);
  373. } else {
  374. return (rrset);
  375. }
  376. }
  377. // Implementation of InMemoryZoneFinder::find
  378. FindResult find(const Name& name, RRType type,
  379. RRsetList* target, const FindOptions options) const
  380. {
  381. LOG_DEBUG(logger, DBG_TRACE_BASIC, DATASRC_MEM_FIND).arg(name).
  382. arg(type);
  383. // Get the node
  384. DomainNode* node(NULL);
  385. FindState state(options);
  386. RBTreeNodeChain<Domain> node_path;
  387. bool rename(false);
  388. switch (domains_.find(name, &node, node_path, cutCallback, &state)) {
  389. case DomainTree::PARTIALMATCH:
  390. /*
  391. * In fact, we could use a single variable instead of
  392. * dname_node_ and zonecut_node_. But then we would need
  393. * to distinquish these two cases by something else and
  394. * it seemed little more confusing to me when I wrote it.
  395. *
  396. * Usually at most one of them will be something else than
  397. * NULL (it might happen both are NULL, in which case we
  398. * consider it NOT FOUND). There's one corner case when
  399. * both might be something else than NULL and it is in case
  400. * there's a DNAME under a zone cut and we search in
  401. * glue OK mode ‒ in that case we don't stop on the domain
  402. * with NS and ignore it for the answer, but it gets set
  403. * anyway. Then we find the DNAME and we need to act by it,
  404. * therefore we first check for DNAME and then for NS. In
  405. * all other cases it doesn't matter, as at least one of them
  406. * is NULL.
  407. */
  408. if (state.dname_node_ != NULL) {
  409. LOG_DEBUG(logger, DBG_TRACE_DATA, DATASRC_MEM_DNAME_FOUND).
  410. arg(state.rrset_->getName());
  411. // We were traversing a DNAME node (and wanted to go
  412. // lower below it), so return the DNAME
  413. return (FindResult(DNAME, prepareRRset(name, state.rrset_,
  414. rename)));
  415. }
  416. if (state.zonecut_node_ != NULL) {
  417. LOG_DEBUG(logger, DBG_TRACE_DATA, DATASRC_MEM_DELEG_FOUND).
  418. arg(state.rrset_->getName());
  419. return (FindResult(DELEGATION, prepareRRset(name,
  420. state.rrset_, rename)));
  421. }
  422. // If the RBTree search stopped at a node for a super domain
  423. // of the search name, it means the search name exists in
  424. // the zone but is empty. Treat it as NXRRSET.
  425. if (node_path.getLastComparisonResult().getRelation() ==
  426. NameComparisonResult::SUPERDOMAIN) {
  427. LOG_DEBUG(logger, DBG_TRACE_DATA, DATASRC_MEM_SUPER_STOP).
  428. arg(node_path.getAbsoluteName()).arg(name);
  429. return (FindResult(NXRRSET, ConstRRsetPtr()));
  430. }
  431. /*
  432. * No redirection anywhere. Let's try if it is a wildcard.
  433. *
  434. * The wildcard is checked after the empty non-terminal domain
  435. * case above, because if that one triggers, it means we should
  436. * not match according to 4.3.3 of RFC 1034 (the query name
  437. * is known to exist).
  438. */
  439. if (node->getFlag(DOMAINFLAG_WILD)) {
  440. /* Should we cancel this match?
  441. *
  442. * If we compare with some node and get a common ancestor,
  443. * it might mean we are comparing with a non-wildcard node.
  444. * In that case, we check which part is common. If we have
  445. * something in common that lives below the node we got
  446. * (the one above *), then we should cancel the match
  447. * according to section 4.3.3 of RFC 1034 (as the name
  448. * between the wildcard domain and the query name is known
  449. * to exist).
  450. *
  451. * Because the way the tree stores relative names, we will
  452. * have exactly one common label (the ".") in case we have
  453. * nothing common under the node we got and we will get
  454. * more common labels otherwise (yes, this relies on the
  455. * internal RBTree structure, which leaks out through this
  456. * little bit).
  457. *
  458. * If the empty non-terminal node actually exists in the
  459. * tree, then this cancellation is not needed, because we
  460. * will not get here at all.
  461. */
  462. if (node_path.getLastComparisonResult().getRelation() ==
  463. NameComparisonResult::COMMONANCESTOR && node_path.
  464. getLastComparisonResult().getCommonLabels() > 1) {
  465. LOG_DEBUG(logger, DBG_TRACE_DATA,
  466. DATASRC_MEM_WILDCARD_CANCEL).arg(name);
  467. return (FindResult(NXDOMAIN, ConstRRsetPtr()));
  468. }
  469. Name wildcard(Name("*").concatenate(
  470. node_path.getAbsoluteName()));
  471. DomainTree::Result result(domains_.find(wildcard, &node));
  472. /*
  473. * Otherwise, why would the DOMAINFLAG_WILD be there if
  474. * there was no wildcard under it?
  475. */
  476. assert(result == DomainTree::EXACTMATCH);
  477. /*
  478. * We have the wildcard node now. Jump below the switch,
  479. * where handling of the common (exact-match) case is.
  480. *
  481. * However, rename it to the searched name.
  482. */
  483. rename = true;
  484. break;
  485. }
  486. // fall through
  487. case DomainTree::NOTFOUND:
  488. LOG_DEBUG(logger, DBG_TRACE_DATA, DATASRC_MEM_NOT_FOUND).
  489. arg(name);
  490. return (FindResult(NXDOMAIN, ConstRRsetPtr()));
  491. case DomainTree::EXACTMATCH: // This one is OK, handle it
  492. break;
  493. default:
  494. assert(0);
  495. }
  496. assert(node != NULL);
  497. // If there is an exact match but the node is empty, it's equivalent
  498. // to NXRRSET.
  499. if (node->isEmpty()) {
  500. LOG_DEBUG(logger, DBG_TRACE_DATA, DATASRC_MEM_DOMAIN_EMPTY).
  501. arg(name);
  502. return (FindResult(NXRRSET, ConstRRsetPtr()));
  503. }
  504. Domain::const_iterator found;
  505. // If the node callback is enabled, this may be a zone cut. If it
  506. // has a NS RR, we should return a delegation, but not in the apex.
  507. if (node->getFlag(DomainNode::FLAG_CALLBACK) && node != origin_data_) {
  508. found = node->getData()->find(RRType::NS());
  509. if (found != node->getData()->end()) {
  510. LOG_DEBUG(logger, DBG_TRACE_DATA,
  511. DATASRC_MEM_EXACT_DELEGATION).arg(name);
  512. return (FindResult(DELEGATION, prepareRRset(name,
  513. found->second, rename)));
  514. }
  515. }
  516. // handle type any query
  517. if (target != NULL && !node->getData()->empty()) {
  518. // Empty domain will be handled as NXRRSET by normal processing
  519. for (found = node->getData()->begin();
  520. found != node->getData()->end(); ++found)
  521. {
  522. target->addRRset(
  523. boost::const_pointer_cast<RRset>(prepareRRset(name,
  524. found->second, rename)));
  525. }
  526. LOG_DEBUG(logger, DBG_TRACE_DATA, DATASRC_MEM_ANY_SUCCESS).
  527. arg(name);
  528. return (FindResult(SUCCESS, ConstRRsetPtr()));
  529. }
  530. found = node->getData()->find(type);
  531. if (found != node->getData()->end()) {
  532. // Good, it is here
  533. LOG_DEBUG(logger, DBG_TRACE_DATA, DATASRC_MEM_SUCCESS).arg(name).
  534. arg(type);
  535. return (FindResult(SUCCESS, prepareRRset(name, found->second,
  536. rename)));
  537. } else {
  538. // Next, try CNAME.
  539. found = node->getData()->find(RRType::CNAME());
  540. if (found != node->getData()->end()) {
  541. LOG_DEBUG(logger, DBG_TRACE_DATA, DATASRC_MEM_CNAME).arg(name);
  542. return (FindResult(CNAME, prepareRRset(name, found->second,
  543. rename)));
  544. }
  545. }
  546. // No exact match or CNAME. Return NXRRSET.
  547. LOG_DEBUG(logger, DBG_TRACE_DATA, DATASRC_MEM_NXRRSET).arg(type).
  548. arg(name);
  549. return (FindResult(NXRRSET, ConstRRsetPtr()));
  550. }
  551. };
  552. InMemoryZoneFinder::InMemoryZoneFinder(const RRClass& zone_class, const Name& origin) :
  553. impl_(new InMemoryZoneFinderImpl(zone_class, origin))
  554. {
  555. LOG_DEBUG(logger, DBG_TRACE_BASIC, DATASRC_MEM_CREATE).arg(origin).
  556. arg(zone_class);
  557. }
  558. InMemoryZoneFinder::~InMemoryZoneFinder() {
  559. LOG_DEBUG(logger, DBG_TRACE_BASIC, DATASRC_MEM_DESTROY).arg(getOrigin()).
  560. arg(getClass());
  561. delete impl_;
  562. }
  563. Name
  564. InMemoryZoneFinder::getOrigin() const {
  565. return (impl_->origin_);
  566. }
  567. RRClass
  568. InMemoryZoneFinder::getClass() const {
  569. return (impl_->zone_class_);
  570. }
  571. ZoneFinder::FindResult
  572. InMemoryZoneFinder::find(const Name& name, const RRType& type,
  573. RRsetList* target, const FindOptions options) const
  574. {
  575. return (impl_->find(name, type, target, options));
  576. }
  577. result::Result
  578. InMemoryZoneFinder::add(const ConstRRsetPtr& rrset) {
  579. return (impl_->add(rrset, &impl_->domains_));
  580. }
  581. void
  582. InMemoryZoneFinder::load(const string& filename) {
  583. LOG_DEBUG(logger, DBG_TRACE_BASIC, DATASRC_MEM_LOAD).arg(getOrigin()).
  584. arg(filename);
  585. // Load it into a temporary tree
  586. InMemoryZoneFinderImpl::DomainTree tmp;
  587. masterLoad(filename.c_str(), getOrigin(), getClass(),
  588. boost::bind(&InMemoryZoneFinderImpl::addFromLoad, impl_, _1, &tmp));
  589. // If it went well, put it inside
  590. impl_->file_name_ = filename;
  591. tmp.swap(impl_->domains_);
  592. // And let the old data die with tmp
  593. }
  594. void
  595. InMemoryZoneFinder::swap(InMemoryZoneFinder& zone_finder) {
  596. LOG_DEBUG(logger, DBG_TRACE_BASIC, DATASRC_MEM_SWAP).arg(getOrigin()).
  597. arg(zone_finder.getOrigin());
  598. std::swap(impl_, zone_finder.impl_);
  599. }
  600. const string
  601. InMemoryZoneFinder::getFileName() const {
  602. return (impl_->file_name_);
  603. }
  604. /// Implementation details for \c InMemoryClient hidden from the public
  605. /// interface.
  606. ///
  607. /// For now, \c InMemoryClient only contains a \c ZoneTable object, which
  608. /// consists of (pointers to) \c InMemoryZoneFinder objects, we may add more
  609. /// member variables later for new features.
  610. class InMemoryClient::InMemoryClientImpl {
  611. public:
  612. InMemoryClientImpl() : zone_count(0) {}
  613. unsigned int zone_count;
  614. ZoneTable zone_table;
  615. };
  616. InMemoryClient::InMemoryClient() : impl_(new InMemoryClientImpl)
  617. {}
  618. InMemoryClient::~InMemoryClient() {
  619. delete impl_;
  620. }
  621. unsigned int
  622. InMemoryClient::getZoneCount() const {
  623. return (impl_->zone_count);
  624. }
  625. result::Result
  626. InMemoryClient::addZone(ZoneFinderPtr zone_finder) {
  627. if (!zone_finder) {
  628. isc_throw(InvalidParameter,
  629. "Null pointer is passed to InMemoryClient::addZone()");
  630. }
  631. LOG_DEBUG(logger, DBG_TRACE_BASIC, DATASRC_MEM_ADD_ZONE).
  632. arg(zone_finder->getOrigin()).arg(zone_finder->getClass().toText());
  633. const result::Result result = impl_->zone_table.addZone(zone_finder);
  634. if (result == result::SUCCESS) {
  635. ++impl_->zone_count;
  636. }
  637. return (result);
  638. }
  639. InMemoryClient::FindResult
  640. InMemoryClient::findZone(const isc::dns::Name& name) const {
  641. LOG_DEBUG(logger, DBG_TRACE_DATA, DATASRC_MEM_FIND_ZONE).arg(name);
  642. return (FindResult(impl_->zone_table.findZone(name).code,
  643. impl_->zone_table.findZone(name).zone));
  644. }
  645. } // end of namespace datasrc
  646. } // end of namespace dns