database.cc 48 KB

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