database.cc 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174
  1. // Copyright (C) 2011 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 <string>
  15. #include <utility>
  16. #include <vector>
  17. #include <datasrc/database.h>
  18. #include <datasrc/data_source.h>
  19. #include <datasrc/iterator.h>
  20. #include <exceptions/exceptions.h>
  21. #include <dns/name.h>
  22. #include <dns/rrclass.h>
  23. #include <dns/rrttl.h>
  24. #include <dns/rrset.h>
  25. #include <dns/rdata.h>
  26. #include <dns/rdataclass.h>
  27. #include <datasrc/data_source.h>
  28. #include <datasrc/logger.h>
  29. #include <boost/foreach.hpp>
  30. using namespace isc::dns;
  31. using namespace std;
  32. using boost::shared_ptr;
  33. using namespace isc::dns::rdata;
  34. namespace isc {
  35. namespace datasrc {
  36. DatabaseClient::DatabaseClient(RRClass rrclass,
  37. boost::shared_ptr<DatabaseAccessor>
  38. accessor) :
  39. rrclass_(rrclass), accessor_(accessor)
  40. {
  41. if (!accessor_) {
  42. isc_throw(isc::InvalidParameter,
  43. "No database provided to DatabaseClient");
  44. }
  45. }
  46. DataSourceClient::FindResult
  47. DatabaseClient::findZone(const Name& name) const {
  48. std::pair<bool, int> zone(accessor_->getZone(name.toText()));
  49. // Try exact first
  50. if (zone.first) {
  51. return (FindResult(result::SUCCESS,
  52. ZoneFinderPtr(new Finder(accessor_,
  53. zone.second, name))));
  54. }
  55. // Then super domains
  56. // Start from 1, as 0 is covered above
  57. for (size_t i(1); i < name.getLabelCount(); ++i) {
  58. isc::dns::Name superdomain(name.split(i));
  59. zone = accessor_->getZone(superdomain.toText());
  60. if (zone.first) {
  61. return (FindResult(result::PARTIALMATCH,
  62. ZoneFinderPtr(new Finder(accessor_,
  63. zone.second,
  64. superdomain))));
  65. }
  66. }
  67. // No, really nothing
  68. return (FindResult(result::NOTFOUND, ZoneFinderPtr()));
  69. }
  70. DatabaseClient::Finder::Finder(boost::shared_ptr<DatabaseAccessor> accessor,
  71. int zone_id, const isc::dns::Name& origin) :
  72. accessor_(accessor),
  73. zone_id_(zone_id),
  74. origin_(origin)
  75. { }
  76. namespace {
  77. // Adds the given Rdata to the given RRset
  78. // If the rrset is an empty pointer, a new one is
  79. // created with the given name, class, type and ttl
  80. // The type is checked if the rrset exists, but the
  81. // name is not.
  82. //
  83. // Then adds the given rdata to the set
  84. //
  85. // Raises a DataSourceError if the type does not
  86. // match, or if the given rdata string does not
  87. // parse correctly for the given type and class
  88. //
  89. // The DatabaseAccessor is passed to print the
  90. // database name in the log message if the TTL is
  91. // modified
  92. void addOrCreate(isc::dns::RRsetPtr& rrset,
  93. const isc::dns::Name& name,
  94. const isc::dns::RRClass& cls,
  95. const isc::dns::RRType& type,
  96. const isc::dns::RRTTL& ttl,
  97. const std::string& rdata_str,
  98. const DatabaseAccessor& db
  99. )
  100. {
  101. if (!rrset) {
  102. rrset.reset(new isc::dns::RRset(name, cls, type, ttl));
  103. } else {
  104. // This is a check to make sure find() is not messing things up
  105. assert(type == rrset->getType());
  106. if (ttl != rrset->getTTL()) {
  107. if (ttl < rrset->getTTL()) {
  108. rrset->setTTL(ttl);
  109. }
  110. logger.warn(DATASRC_DATABASE_FIND_TTL_MISMATCH)
  111. .arg(db.getDBName()).arg(name).arg(cls)
  112. .arg(type).arg(rrset->getTTL());
  113. }
  114. }
  115. try {
  116. rrset->addRdata(isc::dns::rdata::createRdata(type, cls, rdata_str));
  117. } catch (const isc::dns::rdata::InvalidRdataText& ivrt) {
  118. // at this point, rrset may have been initialised for no reason,
  119. // and won't be used. But the caller would drop the shared_ptr
  120. // on such an error anyway, so we don't care.
  121. isc_throw(DataSourceError,
  122. "bad rdata in database for " << name << " "
  123. << type << ": " << ivrt.what());
  124. }
  125. }
  126. // This class keeps a short-lived store of RRSIG records encountered
  127. // during a call to find(). If the backend happens to return signatures
  128. // before the actual data, we might not know which signatures we will need
  129. // So if they may be relevant, we store the in this class.
  130. //
  131. // (If this class seems useful in other places, we might want to move
  132. // it to util. That would also provide an opportunity to add unit tests)
  133. class RRsigStore {
  134. public:
  135. // Adds the given signature Rdata to the store
  136. // The signature rdata MUST be of the RRSIG rdata type
  137. // (the caller must make sure of this).
  138. // NOTE: if we move this class to a public namespace,
  139. // we should add a type_covered argument, so as not
  140. // to have to do this cast here.
  141. void addSig(isc::dns::rdata::RdataPtr sig_rdata) {
  142. const isc::dns::RRType& type_covered =
  143. static_cast<isc::dns::rdata::generic::RRSIG*>(
  144. sig_rdata.get())->typeCovered();
  145. sigs[type_covered].push_back(sig_rdata);
  146. }
  147. // If the store contains signatures for the type of the given
  148. // rrset, they are appended to it.
  149. void appendSignatures(isc::dns::RRsetPtr& rrset) const {
  150. std::map<isc::dns::RRType,
  151. std::vector<isc::dns::rdata::RdataPtr> >::const_iterator
  152. found = sigs.find(rrset->getType());
  153. if (found != sigs.end()) {
  154. BOOST_FOREACH(isc::dns::rdata::RdataPtr sig, found->second) {
  155. rrset->addRRsig(sig);
  156. }
  157. }
  158. }
  159. private:
  160. std::map<isc::dns::RRType, std::vector<isc::dns::rdata::RdataPtr> > sigs;
  161. };
  162. }
  163. DatabaseClient::Finder::FoundRRsets
  164. DatabaseClient::Finder::getRRsets(const string& name, const WantedTypes& types,
  165. bool check_ns, const string* construct_name)
  166. {
  167. RRsigStore sig_store;
  168. bool records_found = false;
  169. std::map<RRType, RRsetPtr> result;
  170. // Request the context
  171. DatabaseAccessor::IteratorContextPtr
  172. context(accessor_->getRecords(name, zone_id_));
  173. // It must not return NULL, that's a bug of the implementation
  174. if (!context) {
  175. isc_throw(isc::Unexpected, "Iterator context null at " + name);
  176. }
  177. std::string columns[DatabaseAccessor::COLUMN_COUNT];
  178. if (construct_name == NULL) {
  179. construct_name = &name;
  180. }
  181. const Name construct_name_object(*construct_name);
  182. bool seen_cname(false);
  183. bool seen_ds(false);
  184. bool seen_other(false);
  185. bool seen_ns(false);
  186. while (context->getNext(columns)) {
  187. // The domain is not empty
  188. records_found = true;
  189. try {
  190. const RRType cur_type(columns[DatabaseAccessor::TYPE_COLUMN]);
  191. if (cur_type == RRType::RRSIG()) {
  192. // If we get signatures before we get the actual data, we
  193. // can't know which ones to keep and which to drop...
  194. // So we keep a separate store of any signature that may be
  195. // relevant and add them to the final RRset when we are
  196. // done.
  197. // A possible optimization here is to not store them for
  198. // types we are certain we don't need
  199. sig_store.addSig(rdata::createRdata(cur_type, getClass(),
  200. columns[DatabaseAccessor::RDATA_COLUMN]));
  201. }
  202. if (types.find(cur_type) != types.end()) {
  203. // This type is requested, so put it into result
  204. const RRTTL cur_ttl(columns[DatabaseAccessor::TTL_COLUMN]);
  205. // Ths sigtype column was an optimization for finding the
  206. // relevant RRSIG RRs for a lookup. Currently this column is
  207. // not used in this revised datasource implementation. We
  208. // should either start using it again, or remove it from use
  209. // completely (i.e. also remove it from the schema and the
  210. // backend implementation).
  211. // Note that because we don't use it now, we also won't notice
  212. // it if the value is wrong (i.e. if the sigtype column
  213. // contains an rrtype that is different from the actual value
  214. // of the 'type covered' field in the RRSIG Rdata).
  215. //cur_sigtype(columns[SIGTYPE_COLUMN]);
  216. addOrCreate(result[cur_type], construct_name_object,
  217. getClass(), cur_type, cur_ttl,
  218. columns[DatabaseAccessor::RDATA_COLUMN],
  219. *accessor_);
  220. }
  221. if (cur_type == RRType::CNAME()) {
  222. seen_cname = true;
  223. } else if (cur_type == RRType::NS()) {
  224. seen_ns = true;
  225. } else if (cur_type == RRType::DS()) {
  226. seen_ds = true;
  227. } else if (cur_type != RRType::RRSIG() &&
  228. cur_type != RRType::NSEC3() &&
  229. cur_type != RRType::NSEC()) {
  230. // NSEC and RRSIG can coexist with anything, otherwise
  231. // we've seen something that can't live together with potential
  232. // CNAME or NS
  233. //
  234. // NSEC3 lives in separate namespace from everything, therefore
  235. // we just ignore it here for these checks as well.
  236. seen_other = true;
  237. }
  238. } catch (const InvalidRRType&) {
  239. isc_throw(DataSourceError, "Invalid RRType in database for " <<
  240. name << ": " << columns[DatabaseAccessor::
  241. TYPE_COLUMN]);
  242. } catch (const InvalidRRTTL&) {
  243. isc_throw(DataSourceError, "Invalid TTL in database for " <<
  244. name << ": " << columns[DatabaseAccessor::
  245. TTL_COLUMN]);
  246. } catch (const rdata::InvalidRdataText&) {
  247. isc_throw(DataSourceError, "Invalid rdata in database for " <<
  248. name << ": " << columns[DatabaseAccessor::
  249. RDATA_COLUMN]);
  250. }
  251. }
  252. if (seen_cname && (seen_other || seen_ns || seen_ds)) {
  253. isc_throw(DataSourceError, "CNAME shares domain " << name <<
  254. " with something else");
  255. }
  256. if (check_ns && seen_ns && seen_other) {
  257. isc_throw(DataSourceError, "NS shares domain " << name <<
  258. " with something else");
  259. }
  260. // Add signatures to all found RRsets
  261. for (std::map<RRType, RRsetPtr>::iterator i(result.begin());
  262. i != result.end(); ++ i) {
  263. sig_store.appendSignatures(i->second);
  264. }
  265. return (FoundRRsets(records_found, result));
  266. }
  267. bool
  268. DatabaseClient::Finder::hasSubdomains(const std::string& name) {
  269. // Request the context
  270. DatabaseAccessor::IteratorContextPtr
  271. context(accessor_->getRecords(name, zone_id_, true));
  272. // It must not return NULL, that's a bug of the implementation
  273. if (!context) {
  274. isc_throw(isc::Unexpected, "Iterator context null at " + name);
  275. }
  276. std::string columns[DatabaseAccessor::COLUMN_COUNT];
  277. return (context->getNext(columns));
  278. }
  279. // Some manipulation with RRType sets
  280. namespace {
  281. // Bunch of functions to construct specific sets of RRTypes we will
  282. // ask from it.
  283. typedef std::set<RRType> WantedTypes;
  284. const WantedTypes&
  285. NSEC_TYPES() {
  286. static bool initialized(false);
  287. static WantedTypes result;
  288. if (!initialized) {
  289. result.insert(RRType::NSEC());
  290. initialized = true;
  291. }
  292. return (result);
  293. }
  294. const WantedTypes&
  295. DELEGATION_TYPES() {
  296. static bool initialized(false);
  297. static WantedTypes result;
  298. if (!initialized) {
  299. result.insert(RRType::DNAME());
  300. result.insert(RRType::NS());
  301. initialized = true;
  302. }
  303. return (result);
  304. }
  305. const WantedTypes&
  306. FINAL_TYPES() {
  307. static bool initialized(false);
  308. static WantedTypes result;
  309. if (!initialized) {
  310. result.insert(RRType::CNAME());
  311. result.insert(RRType::NS());
  312. result.insert(RRType::NSEC());
  313. initialized = true;
  314. }
  315. return (result);
  316. }
  317. }
  318. RRsetPtr
  319. DatabaseClient::Finder::findNSECCover(const Name& name) {
  320. try {
  321. // Which one should contain the NSEC record?
  322. const Name coverName(findPreviousName(name));
  323. // Get the record and copy it out
  324. const FoundRRsets found = getRRsets(coverName.toText(), NSEC_TYPES(),
  325. coverName != getOrigin());
  326. const FoundIterator
  327. nci(found.second.find(RRType::NSEC()));
  328. if (nci != found.second.end()) {
  329. return (nci->second);
  330. } else {
  331. // The previous doesn't contain NSEC.
  332. // Badly signed zone or a bug?
  333. // FIXME: Currently, if the zone is not signed, we could get
  334. // here. In that case we can't really throw, but for now, we can't
  335. // recognize it. So we don't throw at all, enable it once
  336. // we have a is_signed flag or something.
  337. #if 0
  338. isc_throw(DataSourceError, "No NSEC in " +
  339. coverName.toText() + ", but it was "
  340. "returned as previous - "
  341. "accessor error? Badly signed zone?");
  342. #endif
  343. }
  344. }
  345. catch (const isc::NotImplemented&) {
  346. // Well, they want DNSSEC, but there is no available.
  347. // So we don't provide anything.
  348. LOG_INFO(logger, DATASRC_DATABASE_COVER_NSEC_UNSUPPORTED).
  349. arg(accessor_->getDBName()).arg(name);
  350. }
  351. // We didn't find it, return nothing
  352. return (RRsetPtr());
  353. }
  354. ZoneFinder::FindResult
  355. DatabaseClient::Finder::find(const isc::dns::Name& name,
  356. const isc::dns::RRType& type,
  357. isc::dns::RRsetList*,
  358. const FindOptions options)
  359. {
  360. // This variable is used to determine the difference between
  361. // NXDOMAIN and NXRRSET
  362. bool records_found = false;
  363. bool glue_ok((options & FIND_GLUE_OK) != 0);
  364. const bool dnssec_data((options & FIND_DNSSEC) != 0);
  365. bool get_cover(false);
  366. isc::dns::RRsetPtr result_rrset;
  367. ZoneFinder::Result result_status = SUCCESS;
  368. FoundRRsets found;
  369. logger.debug(DBG_TRACE_DETAILED, DATASRC_DATABASE_FIND_RECORDS)
  370. .arg(accessor_->getDBName()).arg(name).arg(type);
  371. // In case we are in GLUE_OK mode and start matching wildcards,
  372. // we can't do it under NS, so we store it here to check
  373. isc::dns::RRsetPtr first_ns;
  374. // First, do we have any kind of delegation (NS/DNAME) here?
  375. const Name origin(getOrigin());
  376. const size_t origin_label_count(origin.getLabelCount());
  377. // Number of labels in the last known non-empty domain
  378. size_t last_known(origin_label_count);
  379. const size_t current_label_count(name.getLabelCount());
  380. // This is how many labels we remove to get origin
  381. const size_t remove_labels(current_label_count - origin_label_count);
  382. // Now go trough all superdomains from origin down
  383. for (int i(remove_labels); i > 0; --i) {
  384. Name superdomain(name.split(i));
  385. // Look if there's NS or DNAME (but ignore the NS in origin)
  386. found = getRRsets(superdomain.toText(), DELEGATION_TYPES(),
  387. i != remove_labels);
  388. if (found.first) {
  389. // It contains some RRs, so it exists.
  390. last_known = superdomain.getLabelCount();
  391. const FoundIterator nsi(found.second.find(RRType::NS()));
  392. const FoundIterator dni(found.second.find(RRType::DNAME()));
  393. // In case we are in GLUE_OK mode, we want to store the
  394. // highest encountered NS (but not apex)
  395. if (glue_ok && !first_ns && i != remove_labels &&
  396. nsi != found.second.end()) {
  397. first_ns = nsi->second;
  398. } else if (!glue_ok && i != remove_labels &&
  399. nsi != found.second.end()) {
  400. // Do a NS delegation, but ignore NS in glue_ok mode. Ignore
  401. // delegation in apex
  402. LOG_DEBUG(logger, DBG_TRACE_DETAILED,
  403. DATASRC_DATABASE_FOUND_DELEGATION).
  404. arg(accessor_->getDBName()).arg(superdomain);
  405. result_rrset = nsi->second;
  406. result_status = DELEGATION;
  407. // No need to go lower, found
  408. break;
  409. } else if (dni != found.second.end()) {
  410. // Very similar with DNAME
  411. LOG_DEBUG(logger, DBG_TRACE_DETAILED,
  412. DATASRC_DATABASE_FOUND_DNAME).
  413. arg(accessor_->getDBName()).arg(superdomain);
  414. result_rrset = dni->second;
  415. result_status = DNAME;
  416. if (result_rrset->getRdataCount() != 1) {
  417. isc_throw(DataSourceError, "DNAME at " << superdomain <<
  418. " has " << result_rrset->getRdataCount() <<
  419. " rdata, 1 expected");
  420. }
  421. break;
  422. }
  423. }
  424. }
  425. if (!result_rrset) { // Only if we didn't find a redirect already
  426. // Try getting the final result and extract it
  427. // It is special if there's a CNAME or NS, DNAME is ignored here
  428. // And we don't consider the NS in origin
  429. WantedTypes final_types(FINAL_TYPES());
  430. final_types.insert(type);
  431. found = getRRsets(name.toText(), final_types, name != origin);
  432. records_found = found.first;
  433. // NS records, CNAME record and Wanted Type records
  434. const FoundIterator nsi(found.second.find(RRType::NS()));
  435. const FoundIterator cni(found.second.find(RRType::CNAME()));
  436. const FoundIterator wti(found.second.find(type));
  437. if (name != origin && !glue_ok && nsi != found.second.end()) {
  438. // There's a delegation at the exact node.
  439. LOG_DEBUG(logger, DBG_TRACE_DETAILED,
  440. DATASRC_DATABASE_FOUND_DELEGATION_EXACT).
  441. arg(accessor_->getDBName()).arg(name);
  442. result_status = DELEGATION;
  443. result_rrset = nsi->second;
  444. } else if (type != isc::dns::RRType::CNAME() &&
  445. cni != found.second.end()) {
  446. // A CNAME here
  447. result_status = CNAME;
  448. result_rrset = cni->second;
  449. if (result_rrset->getRdataCount() != 1) {
  450. isc_throw(DataSourceError, "CNAME with " <<
  451. result_rrset->getRdataCount() <<
  452. " rdata at " << name << ", expected 1");
  453. }
  454. } else if (wti != found.second.end()) {
  455. // Just get the answer
  456. result_rrset = wti->second;
  457. } else if (!records_found) {
  458. // Nothing lives here.
  459. // But check if something lives below this
  460. // domain and if so, pretend something is here as well.
  461. if (hasSubdomains(name.toText())) {
  462. LOG_DEBUG(logger, DBG_TRACE_DETAILED,
  463. DATASRC_DATABASE_FOUND_EMPTY_NONTERMINAL).
  464. arg(accessor_->getDBName()).arg(name);
  465. records_found = true;
  466. get_cover = dnssec_data;
  467. } else if ((options & NO_WILDCARD) != 0) {
  468. // If wildcard check is disabled, the search will ultimately
  469. // terminate with NXDOMAIN. If DNSSEC is enabled, flag that
  470. // we need to get the NSEC records to prove this.
  471. if (dnssec_data) {
  472. get_cover = true;
  473. }
  474. } else {
  475. // It's not empty non-terminal. So check for wildcards.
  476. // We remove labels one by one and look for the wildcard there.
  477. // Go up to first non-empty domain.
  478. for (size_t i(1); i + last_known <= current_label_count; ++i) {
  479. // Construct the name with *
  480. const Name superdomain(name.split(i));
  481. const string wildcard("*." + superdomain.toText());
  482. const string construct_name(name.toText());
  483. // TODO What do we do about DNAME here?
  484. // The types are the same as with original query
  485. found = getRRsets(wildcard, final_types, true,
  486. &construct_name);
  487. if (found.first) {
  488. if (first_ns) {
  489. // In case we are under NS, we don't
  490. // wildcard-match, but return delegation
  491. result_rrset = first_ns;
  492. result_status = DELEGATION;
  493. records_found = true;
  494. // We pretend to switch to non-glue_ok mode
  495. glue_ok = false;
  496. LOG_DEBUG(logger, DBG_TRACE_DETAILED,
  497. DATASRC_DATABASE_WILDCARD_CANCEL_NS).
  498. arg(accessor_->getDBName()).arg(wildcard).
  499. arg(first_ns->getName());
  500. } else if (!hasSubdomains(name.split(i - 1).toText()))
  501. {
  502. // Nothing we added as part of the * can exist
  503. // directly, as we go up only to first existing
  504. // domain, but it could be empty non-terminal. In
  505. // that case, we need to cancel the match.
  506. records_found = true;
  507. const FoundIterator
  508. cni(found.second.find(RRType::CNAME()));
  509. const FoundIterator
  510. nsi(found.second.find(RRType::NS()));
  511. const FoundIterator
  512. nci(found.second.find(RRType::NSEC()));
  513. const FoundIterator wti(found.second.find(type));
  514. if (cni != found.second.end() &&
  515. type != RRType::CNAME()) {
  516. result_rrset = cni->second;
  517. result_status = WILDCARD_CNAME;
  518. } else if (nsi != found.second.end()) {
  519. result_rrset = nsi->second;
  520. result_status = DELEGATION;
  521. } else if (wti != found.second.end()) {
  522. result_rrset = wti->second;
  523. result_status = WILDCARD;
  524. } else {
  525. // NXRRSET case in the wildcard
  526. result_status = WILDCARD_NXRRSET;
  527. if (dnssec_data &&
  528. nci != found.second.end()) {
  529. // User wants a proof the wildcard doesn't
  530. // contain it
  531. //
  532. // However, we need to get the RRset in the
  533. // name of the wildcard, not the constructed
  534. // one, so we walk it again
  535. found = getRRsets(wildcard, NSEC_TYPES(),
  536. true);
  537. result_rrset =
  538. found.second.find(RRType::NSEC())->
  539. second;
  540. }
  541. }
  542. LOG_DEBUG(logger, DBG_TRACE_DETAILED,
  543. DATASRC_DATABASE_WILDCARD).
  544. arg(accessor_->getDBName()).arg(wildcard).
  545. arg(name);
  546. } else {
  547. LOG_DEBUG(logger, DBG_TRACE_DETAILED,
  548. DATASRC_DATABASE_WILDCARD_CANCEL_SUB).
  549. arg(accessor_->getDBName()).arg(wildcard).
  550. arg(name).arg(superdomain);
  551. }
  552. break;
  553. } else if (hasSubdomains(wildcard)) {
  554. // Empty non-terminal asterisk
  555. records_found = true;
  556. LOG_DEBUG(logger, DBG_TRACE_DETAILED,
  557. DATASRC_DATABASE_WILDCARD_EMPTY).
  558. arg(accessor_->getDBName()).arg(wildcard).
  559. arg(name);
  560. if (dnssec_data) {
  561. result_rrset = findNSECCover(Name(wildcard));
  562. if (result_rrset) {
  563. result_status = WILDCARD_NXRRSET;
  564. }
  565. }
  566. break;
  567. }
  568. }
  569. // This is the NXDOMAIN case (nothing found anywhere). If
  570. // they want DNSSEC data, try getting the NSEC record
  571. if (dnssec_data && !records_found) {
  572. get_cover = true;
  573. }
  574. }
  575. } else if (dnssec_data) {
  576. // This is the "usual" NXRRSET case
  577. // So in case they want DNSSEC, provide the NSEC
  578. // (which should be available already here)
  579. result_status = NXRRSET;
  580. const FoundIterator nci(found.second.find(RRType::NSEC()));
  581. if (nci != found.second.end()) {
  582. result_rrset = nci->second;
  583. }
  584. }
  585. }
  586. if (!result_rrset) {
  587. if (result_status == SUCCESS) {
  588. // Should we look for NSEC covering the name?
  589. if (get_cover) {
  590. result_rrset = findNSECCover(name);
  591. if (result_rrset) {
  592. result_status = NXDOMAIN;
  593. }
  594. }
  595. // Something is not here and we didn't decide yet what
  596. if (records_found) {
  597. logger.debug(DBG_TRACE_DETAILED,
  598. DATASRC_DATABASE_FOUND_NXRRSET)
  599. .arg(accessor_->getDBName()).arg(name)
  600. .arg(getClass()).arg(type);
  601. result_status = NXRRSET;
  602. } else {
  603. logger.debug(DBG_TRACE_DETAILED,
  604. DATASRC_DATABASE_FOUND_NXDOMAIN)
  605. .arg(accessor_->getDBName()).arg(name)
  606. .arg(getClass()).arg(type);
  607. result_status = NXDOMAIN;
  608. }
  609. }
  610. } else {
  611. logger.debug(DBG_TRACE_DETAILED,
  612. DATASRC_DATABASE_FOUND_RRSET)
  613. .arg(accessor_->getDBName()).arg(*result_rrset);
  614. }
  615. return (FindResult(result_status, result_rrset));
  616. }
  617. Name
  618. DatabaseClient::Finder::findPreviousName(const Name& name) const {
  619. const string str(accessor_->findPreviousName(zone_id_,
  620. name.reverse().toText()));
  621. try {
  622. return (Name(str));
  623. }
  624. /*
  625. * To avoid having the same code many times, we just catch all the
  626. * exceptions and handle them in a common code below
  627. */
  628. catch (const isc::dns::EmptyLabel&) {}
  629. catch (const isc::dns::TooLongLabel&) {}
  630. catch (const isc::dns::BadLabelType&) {}
  631. catch (const isc::dns::BadEscape&) {}
  632. catch (const isc::dns::TooLongName&) {}
  633. catch (const isc::dns::IncompleteName&) {}
  634. isc_throw(DataSourceError, "Bad name " + str + " from findPreviousName");
  635. }
  636. Name
  637. DatabaseClient::Finder::getOrigin() const {
  638. return (origin_);
  639. }
  640. isc::dns::RRClass
  641. DatabaseClient::Finder::getClass() const {
  642. // TODO Implement
  643. return isc::dns::RRClass::IN();
  644. }
  645. namespace {
  646. /*
  647. * This needs, beside of converting all data from textual representation, group
  648. * together rdata of the same RRsets. To do this, we hold one row of data ahead
  649. * of iteration. When we get a request to provide data, we create it from this
  650. * data and load a new one. If it is to be put to the same rrset, we add it.
  651. * Otherwise we just return what we have and keep the row as the one ahead
  652. * for next time.
  653. */
  654. class DatabaseIterator : public ZoneIterator {
  655. public:
  656. DatabaseIterator(shared_ptr<DatabaseAccessor> accessor,
  657. const Name& zone_name,
  658. const RRClass& rrclass,
  659. bool separate_rrs) :
  660. accessor_(accessor),
  661. class_(rrclass),
  662. ready_(true),
  663. separate_rrs_(separate_rrs)
  664. {
  665. // Get the zone
  666. const pair<bool, int> zone(accessor_->getZone(zone_name.toText()));
  667. if (!zone.first) {
  668. // No such zone, can't continue
  669. isc_throw(DataSourceError, "Zone " + zone_name.toText() +
  670. " can not be iterated, because it doesn't exist "
  671. "in this data source");
  672. }
  673. // Start a separate transaction.
  674. accessor_->startTransaction();
  675. // Find the SOA of the zone (may or may not succeed). Note that
  676. // this must be done before starting the iteration context.
  677. soa_ = DatabaseClient::Finder(accessor_, zone.second, zone_name).
  678. find(zone_name, RRType::SOA(), NULL).rrset;
  679. // Request the context
  680. context_ = accessor_->getAllRecords(zone.second);
  681. // It must not return NULL, that's a bug of the implementation
  682. if (!context_) {
  683. isc_throw(isc::Unexpected, "Iterator context null at " +
  684. zone_name.toText());
  685. }
  686. // Prepare data for the next time
  687. getData();
  688. }
  689. virtual ~DatabaseIterator() {
  690. if (ready_) {
  691. accessor_->commit();
  692. }
  693. }
  694. virtual ConstRRsetPtr getSOA() const {
  695. return (soa_);
  696. }
  697. virtual isc::dns::ConstRRsetPtr getNextRRset() {
  698. if (!ready_) {
  699. isc_throw(isc::Unexpected, "Iterating past the zone end");
  700. }
  701. if (!data_ready_) {
  702. // At the end of zone
  703. accessor_->commit();
  704. ready_ = false;
  705. LOG_DEBUG(logger, DBG_TRACE_DETAILED,
  706. DATASRC_DATABASE_ITERATE_END);
  707. return (ConstRRsetPtr());
  708. }
  709. const string name_str(name_), rtype_str(rtype_), ttl(ttl_);
  710. const Name name(name_str);
  711. const RRType rtype(rtype_str);
  712. RRsetPtr rrset(new RRset(name, class_, rtype, RRTTL(ttl)));
  713. while (data_ready_ && name_ == name_str && rtype_str == rtype_) {
  714. if (ttl_ != ttl) {
  715. if (ttl < ttl_) {
  716. ttl_ = ttl;
  717. rrset->setTTL(RRTTL(ttl));
  718. }
  719. LOG_WARN(logger, DATASRC_DATABASE_ITERATE_TTL_MISMATCH).
  720. arg(name_).arg(class_).arg(rtype_).arg(rrset->getTTL());
  721. }
  722. rrset->addRdata(rdata::createRdata(rtype, class_, rdata_));
  723. getData();
  724. if (separate_rrs_) {
  725. break;
  726. }
  727. }
  728. LOG_DEBUG(logger, DBG_TRACE_DETAILED, DATASRC_DATABASE_ITERATE_NEXT).
  729. arg(rrset->getName()).arg(rrset->getType());
  730. return (rrset);
  731. }
  732. private:
  733. // Load next row of data
  734. void getData() {
  735. string data[DatabaseAccessor::COLUMN_COUNT];
  736. data_ready_ = context_->getNext(data);
  737. name_ = data[DatabaseAccessor::NAME_COLUMN];
  738. rtype_ = data[DatabaseAccessor::TYPE_COLUMN];
  739. ttl_ = data[DatabaseAccessor::TTL_COLUMN];
  740. rdata_ = data[DatabaseAccessor::RDATA_COLUMN];
  741. }
  742. // The dedicated accessor
  743. shared_ptr<DatabaseAccessor> accessor_;
  744. // The context
  745. DatabaseAccessor::IteratorContextPtr context_;
  746. // Class of the zone
  747. const RRClass class_;
  748. // SOA of the zone, if any (it should normally exist)
  749. ConstRRsetPtr soa_;
  750. // Status
  751. bool ready_, data_ready_;
  752. // Data of the next row
  753. string name_, rtype_, rdata_, ttl_;
  754. // Whether to modify differing TTL values, or treat a different TTL as
  755. // a different RRset
  756. bool separate_rrs_;
  757. };
  758. }
  759. ZoneIteratorPtr
  760. DatabaseClient::getIterator(const isc::dns::Name& name,
  761. bool separate_rrs) const
  762. {
  763. ZoneIteratorPtr iterator = ZoneIteratorPtr(new DatabaseIterator(
  764. accessor_->clone(), name,
  765. rrclass_, separate_rrs));
  766. LOG_DEBUG(logger, DBG_TRACE_DETAILED, DATASRC_DATABASE_ITERATE).
  767. arg(name);
  768. return (iterator);
  769. }
  770. //
  771. // Zone updater using some database system as the underlying data source.
  772. //
  773. class DatabaseUpdater : public ZoneUpdater {
  774. public:
  775. DatabaseUpdater(shared_ptr<DatabaseAccessor> accessor, int zone_id,
  776. const Name& zone_name, const RRClass& zone_class,
  777. bool journaling) :
  778. committed_(false), accessor_(accessor), zone_id_(zone_id),
  779. db_name_(accessor->getDBName()), zone_name_(zone_name.toText()),
  780. zone_class_(zone_class), journaling_(journaling),
  781. diff_phase_(NOT_STARTED), serial_(0),
  782. finder_(new DatabaseClient::Finder(accessor_, zone_id_, zone_name))
  783. {
  784. logger.debug(DBG_TRACE_DATA, DATASRC_DATABASE_UPDATER_CREATED)
  785. .arg(zone_name_).arg(zone_class_).arg(db_name_);
  786. }
  787. virtual ~DatabaseUpdater() {
  788. if (!committed_) {
  789. try {
  790. accessor_->rollback();
  791. logger.info(DATASRC_DATABASE_UPDATER_ROLLBACK)
  792. .arg(zone_name_).arg(zone_class_).arg(db_name_);
  793. } catch (const DataSourceError& e) {
  794. // We generally expect that rollback always succeeds, and
  795. // it should in fact succeed in a way we execute it. But
  796. // as the public API allows rollback() to fail and
  797. // throw, we should expect it. Obviously we cannot re-throw
  798. // it. The best we can do is to log it as a critical error.
  799. logger.error(DATASRC_DATABASE_UPDATER_ROLLBACKFAIL)
  800. .arg(zone_name_).arg(zone_class_).arg(db_name_)
  801. .arg(e.what());
  802. }
  803. }
  804. logger.debug(DBG_TRACE_DATA, DATASRC_DATABASE_UPDATER_DESTROYED)
  805. .arg(zone_name_).arg(zone_class_).arg(db_name_);
  806. }
  807. virtual ZoneFinder& getFinder() { return (*finder_); }
  808. virtual void addRRset(const RRset& rrset);
  809. virtual void deleteRRset(const RRset& rrset);
  810. virtual void commit();
  811. private:
  812. // A short cut typedef only for making the code shorter.
  813. typedef DatabaseAccessor Accessor;
  814. bool committed_;
  815. shared_ptr<DatabaseAccessor> accessor_;
  816. const int zone_id_;
  817. const string db_name_;
  818. const string zone_name_;
  819. const RRClass zone_class_;
  820. const bool journaling_;
  821. // For the journals
  822. enum DiffPhase {
  823. NOT_STARTED,
  824. DELETE,
  825. ADD
  826. };
  827. DiffPhase diff_phase_;
  828. Serial serial_;
  829. boost::scoped_ptr<DatabaseClient::Finder> finder_;
  830. // This is a set of validation checks commonly used for addRRset() and
  831. // deleteRRset to minimize duplicate code logic and to make the main
  832. // code concise.
  833. void validateAddOrDelete(const char* const op_str, const RRset& rrset,
  834. DiffPhase prev_phase,
  835. DiffPhase current_phase) const;
  836. };
  837. void
  838. DatabaseUpdater::validateAddOrDelete(const char* const op_str,
  839. const RRset& rrset,
  840. DiffPhase prev_phase,
  841. DiffPhase current_phase) const
  842. {
  843. if (committed_) {
  844. isc_throw(DataSourceError, op_str << " attempt after commit to zone: "
  845. << zone_name_ << "/" << zone_class_);
  846. }
  847. if (rrset.getRdataCount() == 0) {
  848. isc_throw(DataSourceError, op_str << " attempt with an empty RRset: "
  849. << rrset.getName() << "/" << zone_class_ << "/"
  850. << rrset.getType());
  851. }
  852. if (rrset.getClass() != zone_class_) {
  853. isc_throw(DataSourceError, op_str << " attempt for a different class "
  854. << zone_name_ << "/" << zone_class_ << ": "
  855. << rrset.toText());
  856. }
  857. if (rrset.getRRsig()) {
  858. isc_throw(DataSourceError, op_str << " attempt for RRset with RRSIG "
  859. << zone_name_ << "/" << zone_class_ << ": "
  860. << rrset.toText());
  861. }
  862. if (journaling_) {
  863. const RRType rrtype(rrset.getType());
  864. if (rrtype == RRType::SOA() && diff_phase_ != prev_phase) {
  865. isc_throw(isc::BadValue, op_str << " attempt in an invalid "
  866. << "diff phase: " << diff_phase_ << ", rrset: " <<
  867. rrset.toText());
  868. }
  869. if (rrtype != RRType::SOA() && diff_phase_ != current_phase) {
  870. isc_throw(isc::BadValue, "diff state change by non SOA: "
  871. << rrset.toText());
  872. }
  873. }
  874. }
  875. void
  876. DatabaseUpdater::addRRset(const RRset& rrset) {
  877. validateAddOrDelete("add", rrset, DELETE, ADD);
  878. // It's guaranteed rrset has at least one RDATA at this point.
  879. RdataIteratorPtr it = rrset.getRdataIterator();
  880. string columns[Accessor::ADD_COLUMN_COUNT]; // initialized with ""
  881. columns[Accessor::ADD_NAME] = rrset.getName().toText();
  882. columns[Accessor::ADD_REV_NAME] = rrset.getName().reverse().toText();
  883. columns[Accessor::ADD_TTL] = rrset.getTTL().toText();
  884. columns[Accessor::ADD_TYPE] = rrset.getType().toText();
  885. string journal[Accessor::DIFF_PARAM_COUNT];
  886. if (journaling_) {
  887. journal[Accessor::DIFF_NAME] = columns[Accessor::ADD_NAME];
  888. journal[Accessor::DIFF_TYPE] = columns[Accessor::ADD_TYPE];
  889. journal[Accessor::DIFF_TTL] = columns[Accessor::ADD_TTL];
  890. diff_phase_ = ADD;
  891. if (rrset.getType() == RRType::SOA()) {
  892. serial_ =
  893. dynamic_cast<const generic::SOA&>(it->getCurrent()).
  894. getSerial();
  895. }
  896. }
  897. for (; !it->isLast(); it->next()) {
  898. if (rrset.getType() == RRType::RRSIG()) {
  899. // XXX: the current interface (based on the current sqlite3
  900. // data source schema) requires a separate "sigtype" column,
  901. // even though it won't be used in a newer implementation.
  902. // We should eventually clean up the schema design and simplify
  903. // the interface, but until then we have to conform to the schema.
  904. const generic::RRSIG& rrsig_rdata =
  905. dynamic_cast<const generic::RRSIG&>(it->getCurrent());
  906. columns[Accessor::ADD_SIGTYPE] =
  907. rrsig_rdata.typeCovered().toText();
  908. }
  909. columns[Accessor::ADD_RDATA] = it->getCurrent().toText();
  910. if (journaling_) {
  911. journal[Accessor::DIFF_RDATA] = columns[Accessor::ADD_RDATA];
  912. accessor_->addRecordDiff(zone_id_, serial_.getValue(),
  913. Accessor::DIFF_ADD, journal);
  914. }
  915. accessor_->addRecordToZone(columns);
  916. }
  917. }
  918. void
  919. DatabaseUpdater::deleteRRset(const RRset& rrset) {
  920. // If this is the first operation, pretend we are starting a new delete
  921. // sequence after adds. This will simplify the validation below.
  922. if (diff_phase_ == NOT_STARTED) {
  923. diff_phase_ = ADD;
  924. }
  925. validateAddOrDelete("delete", rrset, ADD, DELETE);
  926. RdataIteratorPtr it = rrset.getRdataIterator();
  927. string params[Accessor::DEL_PARAM_COUNT]; // initialized with ""
  928. params[Accessor::DEL_NAME] = rrset.getName().toText();
  929. params[Accessor::DEL_TYPE] = rrset.getType().toText();
  930. string journal[Accessor::DIFF_PARAM_COUNT];
  931. if (journaling_) {
  932. journal[Accessor::DIFF_NAME] = params[Accessor::DEL_NAME];
  933. journal[Accessor::DIFF_TYPE] = params[Accessor::DEL_TYPE];
  934. journal[Accessor::DIFF_TTL] = rrset.getTTL().toText();
  935. diff_phase_ = DELETE;
  936. if (rrset.getType() == RRType::SOA()) {
  937. serial_ =
  938. dynamic_cast<const generic::SOA&>(it->getCurrent()).
  939. getSerial();
  940. }
  941. }
  942. for (; !it->isLast(); it->next()) {
  943. params[Accessor::DEL_RDATA] = it->getCurrent().toText();
  944. if (journaling_) {
  945. journal[Accessor::DIFF_RDATA] = params[Accessor::DEL_RDATA];
  946. accessor_->addRecordDiff(zone_id_, serial_.getValue(),
  947. Accessor::DIFF_DELETE, journal);
  948. }
  949. accessor_->deleteRecordInZone(params);
  950. }
  951. }
  952. void
  953. DatabaseUpdater::commit() {
  954. if (committed_) {
  955. isc_throw(DataSourceError, "Duplicate commit attempt for "
  956. << zone_name_ << "/" << zone_class_ << " on "
  957. << db_name_);
  958. }
  959. if (journaling_ && diff_phase_ == DELETE) {
  960. isc_throw(isc::BadValue, "Update sequence not complete");
  961. }
  962. accessor_->commit();
  963. committed_ = true; // make sure the destructor won't trigger rollback
  964. // We release the accessor immediately after commit is completed so that
  965. // we don't hold the possible internal resource any longer.
  966. accessor_.reset();
  967. logger.debug(DBG_TRACE_DATA, DATASRC_DATABASE_UPDATER_COMMIT)
  968. .arg(zone_name_).arg(zone_class_).arg(db_name_);
  969. }
  970. // The updater factory
  971. ZoneUpdaterPtr
  972. DatabaseClient::getUpdater(const isc::dns::Name& name, bool replace,
  973. bool journaling) const
  974. {
  975. if (replace && journaling) {
  976. isc_throw(isc::BadValue, "Can't store journal and replace the whole "
  977. "zone at the same time");
  978. }
  979. shared_ptr<DatabaseAccessor> update_accessor(accessor_->clone());
  980. const std::pair<bool, int> zone(update_accessor->startUpdateZone(
  981. name.toText(), replace));
  982. if (!zone.first) {
  983. return (ZoneUpdaterPtr());
  984. }
  985. return (ZoneUpdaterPtr(new DatabaseUpdater(update_accessor, zone.second,
  986. name, rrclass_, journaling)));
  987. }
  988. //
  989. // Zone journal reader using some database system as the underlying data
  990. // source.
  991. //
  992. class DatabaseJournalReader : public ZoneJournalReader {
  993. private:
  994. // A shortcut typedef to keep the code concise.
  995. typedef DatabaseAccessor Accessor;
  996. public:
  997. DatabaseJournalReader(shared_ptr<Accessor> accessor, const Name& zone,
  998. int zone_id, const RRClass& rrclass, uint32_t begin,
  999. uint32_t end) :
  1000. accessor_(accessor), zone_(zone), rrclass_(rrclass),
  1001. begin_(begin), end_(end), finished_(false)
  1002. {
  1003. context_ = accessor_->getDiffs(zone_id, begin, end);
  1004. }
  1005. virtual ~DatabaseJournalReader() {}
  1006. virtual ConstRRsetPtr getNextDiff() {
  1007. if (finished_) {
  1008. isc_throw(InvalidOperation,
  1009. "Diff read attempt past the end of sequence on "
  1010. << accessor_->getDBName());
  1011. }
  1012. string data[Accessor::COLUMN_COUNT];
  1013. if (!context_->getNext(data)) {
  1014. finished_ = true;
  1015. LOG_DEBUG(logger, DBG_TRACE_BASIC,
  1016. DATASRC_DATABASE_JOURNALREADER_END).
  1017. arg(zone_).arg(rrclass_).arg(accessor_->getDBName()).
  1018. arg(begin_).arg(end_);
  1019. return (ConstRRsetPtr());
  1020. }
  1021. try {
  1022. RRsetPtr rrset(new RRset(Name(data[Accessor::NAME_COLUMN]),
  1023. rrclass_,
  1024. RRType(data[Accessor::TYPE_COLUMN]),
  1025. RRTTL(data[Accessor::TTL_COLUMN])));
  1026. rrset->addRdata(rdata::createRdata(rrset->getType(), rrclass_,
  1027. data[Accessor::RDATA_COLUMN]));
  1028. LOG_DEBUG(logger, DBG_TRACE_DETAILED,
  1029. DATASRC_DATABASE_JOURNALREADER_NEXT).
  1030. arg(rrset->getName()).arg(rrset->getType()).
  1031. arg(zone_).arg(rrclass_).arg(accessor_->getDBName());
  1032. return (rrset);
  1033. } catch (const Exception& ex) {
  1034. LOG_ERROR(logger, DATASRC_DATABASE_JOURNALREADR_BADDATA).
  1035. arg(zone_).arg(rrclass_).arg(accessor_->getDBName()).
  1036. arg(begin_).arg(end_).arg(ex.what());
  1037. isc_throw(DataSourceError, "Failed to create RRset from diff on "
  1038. << accessor_->getDBName());
  1039. }
  1040. }
  1041. private:
  1042. shared_ptr<Accessor> accessor_;
  1043. const Name zone_;
  1044. const RRClass rrclass_;
  1045. Accessor::IteratorContextPtr context_;
  1046. const uint32_t begin_;
  1047. const uint32_t end_;
  1048. bool finished_;
  1049. };
  1050. // The JournalReader factory
  1051. pair<ZoneJournalReader::Result, ZoneJournalReaderPtr>
  1052. DatabaseClient::getJournalReader(const isc::dns::Name& zone,
  1053. uint32_t begin_serial,
  1054. uint32_t end_serial) const
  1055. {
  1056. shared_ptr<DatabaseAccessor> jnl_accessor(accessor_->clone());
  1057. const pair<bool, int> zoneinfo(jnl_accessor->getZone(zone.toText()));
  1058. if (!zoneinfo.first) {
  1059. return (pair<ZoneJournalReader::Result, ZoneJournalReaderPtr>(
  1060. ZoneJournalReader::NO_SUCH_ZONE,
  1061. ZoneJournalReaderPtr()));
  1062. }
  1063. try {
  1064. const pair<ZoneJournalReader::Result, ZoneJournalReaderPtr> ret(
  1065. ZoneJournalReader::SUCCESS,
  1066. ZoneJournalReaderPtr(new DatabaseJournalReader(jnl_accessor,
  1067. zone,
  1068. zoneinfo.second,
  1069. rrclass_,
  1070. begin_serial,
  1071. end_serial)));
  1072. LOG_DEBUG(logger, DBG_TRACE_BASIC,
  1073. DATASRC_DATABASE_JOURNALREADER_START).arg(zone).arg(rrclass_).
  1074. arg(jnl_accessor->getDBName()).arg(begin_serial).arg(end_serial);
  1075. return (ret);
  1076. } catch (const NoSuchSerial&) {
  1077. return (pair<ZoneJournalReader::Result, ZoneJournalReaderPtr>(
  1078. ZoneJournalReader::NO_SUCH_VERSION,
  1079. ZoneJournalReaderPtr()));
  1080. }
  1081. }
  1082. }
  1083. }