database.h 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152
  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. #ifndef __DATABASE_DATASRC_H
  15. #define __DATABASE_DATASRC_H
  16. #include <string>
  17. #include <boost/scoped_ptr.hpp>
  18. #include <boost/tuple/tuple.hpp>
  19. #include <dns/rrclass.h>
  20. #include <dns/rrset.h>
  21. #include <dns/rrtype.h>
  22. #include <datasrc/data_source.h>
  23. #include <datasrc/client.h>
  24. #include <datasrc/client.h>
  25. #include <datasrc/logger.h>
  26. #include <dns/name.h>
  27. #include <exceptions/exceptions.h>
  28. #include <map>
  29. #include <set>
  30. namespace isc {
  31. namespace datasrc {
  32. /// \brief Abstraction of lowlevel database with DNS data
  33. ///
  34. /// This class is defines interface to databases. Each supported database
  35. /// will provide methods for accessing the data stored there in a generic
  36. /// manner. The methods are meant to be low-level, without much or any knowledge
  37. /// about DNS and should be possible to translate directly to queries.
  38. ///
  39. /// On the other hand, how the communication with database is done and in what
  40. /// schema (in case of relational/SQL database) is up to the concrete classes.
  41. ///
  42. /// This class is non-copyable, as copying connections to database makes little
  43. /// sense and will not be needed.
  44. ///
  45. /// \todo Is it true this does not need to be copied? For example the zone
  46. /// iterator might need it's own copy. But a virtual clone() method might
  47. /// be better for that than copy constructor.
  48. ///
  49. /// \note The same application may create multiple connections to the same
  50. /// database, having multiple instances of this class. If the database
  51. /// allows having multiple open queries at one connection, the connection
  52. /// class may share it.
  53. class DatabaseAccessor : boost::noncopyable {
  54. public:
  55. /// \brief Data columns for by IteratorContext::getNext()
  56. ///
  57. /// When implementing getNext(), the columns array should be filled with
  58. /// the values as described in this enumeration, in this order, i.e.
  59. /// - TYPE_COLUMN should be the first element (index 0) of the array,
  60. /// - TTL_COLUMN should be the second element (index 1),
  61. /// - etc.
  62. enum RecordColumns {
  63. TYPE_COLUMN = 0, ///< The RRType of the record (A/NS/TXT etc.)
  64. TTL_COLUMN = 1, ///< The TTL of the record (a
  65. SIGTYPE_COLUMN = 2, ///< For RRSIG records, this contains the RRTYPEs
  66. ///< the RRSIG cover. In the current implementation,
  67. ///< this field is ignored.
  68. RDATA_COLUMN = 3, ///< Full text representation of the record's RDATA
  69. NAME_COLUMN = 4, ///< The domain name of this RR
  70. COLUMN_COUNT = 5 ///< The total number of columns, MUST be value of
  71. ///< the largest other element in this enum plus 1.
  72. };
  73. /// \brief Definitions of the fields to be passed to addRecordToZone()
  74. ///
  75. /// Each derived implementation of addRecordToZone() should expect
  76. /// the "columns" array to be filled with the values as described in this
  77. /// enumeration, in this order.
  78. enum AddRecordColumns {
  79. ADD_NAME = 0, ///< The owner name of the record (a domain name)
  80. ADD_REV_NAME = 1, ///< Reversed name of NAME (used for DNSSEC)
  81. ADD_TTL = 2, ///< The TTL of the record (in numeric form)
  82. ADD_TYPE = 3, ///< The RRType of the record (A/NS/TXT etc.)
  83. ADD_SIGTYPE = 4, ///< RRSIGs only: RRTYPEs the RRSIG covers.
  84. ADD_RDATA = 5, ///< Full text representation of the record's RDATA
  85. ADD_COLUMN_COUNT = 6 ///< Number of columns
  86. };
  87. /// \brief Definitions of the fields to be passed to deleteRecordInZone()
  88. ///
  89. /// Each derived implementation of deleteRecordInZone() should expect
  90. /// the "params" array to be filled with the values as described in this
  91. /// enumeration, in this order.
  92. enum DeleteRecordParams {
  93. DEL_NAME = 0, ///< The owner name of the record (a domain name)
  94. DEL_TYPE = 1, ///< The RRType of the record (A/NS/TXT etc.)
  95. DEL_RDATA = 2, ///< Full text representation of the record's RDATA
  96. DEL_PARAM_COUNT = 3 ///< Number of parameters
  97. };
  98. /// \brief Operation mode when adding a record diff.
  99. ///
  100. /// This is used as the "operation" parameter value of addRecordDiff().
  101. enum DiffOperation {
  102. DIFF_ADD = 0, ///< This diff is for adding an RR
  103. DIFF_DELETE = 1 ///< This diff is for deleting an RR
  104. };
  105. /// \brief Definitions of the fields to be passed to addRecordDiff().
  106. ///
  107. /// Each derived implementation of addRecordDiff() should expect
  108. /// the "params" array to be filled with the values as described in this
  109. /// enumeration, in this order.
  110. enum DiffRecordParams {
  111. DIFF_NAME = 0, ///< Owner name of the record (a domain name)
  112. DIFF_TYPE = 1, ///< The RRType of the record (A/NS/TXT etc.)
  113. DIFF_TTL = 2, ///< The TTL of the record (in numeric form)
  114. DIFF_RDATA = 3, ///< Full text representation of record's RDATA
  115. DIFF_PARAM_COUNT = 4 ///< Number of parameters
  116. };
  117. /// \brief Destructor
  118. ///
  119. /// It is empty, but needs a virtual one, since we will use the derived
  120. /// classes in polymorphic way.
  121. virtual ~DatabaseAccessor() { }
  122. /// \brief Retrieve a zone identifier
  123. ///
  124. /// This method looks up a zone for the given name in the database. It
  125. /// should match only exact zone name (eg. name is equal to the zone's
  126. /// apex), as the DatabaseClient will loop trough the labels itself and
  127. /// find the most suitable zone.
  128. ///
  129. /// It is not specified if and what implementation of this method may throw,
  130. /// so code should expect anything.
  131. ///
  132. /// \param name The (fully qualified) domain name of the zone's apex to be
  133. /// looked up.
  134. /// \return The first part of the result indicates if a matching zone
  135. /// was found. In case it was, the second part is internal zone ID.
  136. /// This one will be passed to methods finding data in the zone.
  137. /// It is not required to keep them, in which case whatever might
  138. /// be returned - the ID is only passed back to the database as
  139. /// an opaque handle.
  140. virtual std::pair<bool, int> getZone(const std::string& name) const = 0;
  141. /// \brief This holds the internal context of ZoneIterator for databases
  142. ///
  143. /// While the ZoneIterator implementation from DatabaseClient does all the
  144. /// translation from strings to DNS classes and validation, this class
  145. /// holds the pointer to where the database is at reading the data.
  146. ///
  147. /// It can either hold shared pointer to the connection which created it
  148. /// and have some kind of statement inside (in case single database
  149. /// connection can handle multiple concurrent SQL statements) or it can
  150. /// create a new connection (or, if it is more convenient, the connection
  151. /// itself can inherit both from DatabaseConnection and IteratorContext
  152. /// and just clone itself).
  153. class IteratorContext : public boost::noncopyable {
  154. public:
  155. /// \brief Destructor
  156. ///
  157. /// Virtual destructor, so any descendand class is destroyed correctly.
  158. virtual ~IteratorContext() { }
  159. /// \brief Function to provide next resource record
  160. ///
  161. /// This function should provide data about the next resource record
  162. /// from the data that is searched. The data is not converted yet.
  163. ///
  164. /// Depending on how the iterator was constructed, there is a difference
  165. /// in behaviour; for a 'full zone iterator', created with
  166. /// getAllRecords(), all COLUMN_COUNT elements of the array are
  167. /// overwritten.
  168. /// For a 'name iterator', created with getRecords(), the column
  169. /// NAME_COLUMN is untouched, since what would be added here is by
  170. /// definition already known to the caller (it already passes it as
  171. /// an argument to getRecords()).
  172. ///
  173. /// Once this function returns false, any subsequent call to it should
  174. /// result in false. The implementation of a derived class must ensure
  175. /// it doesn't cause any disruption due to that such as a crash or
  176. /// exception.
  177. ///
  178. /// \note The order of RRs is not strictly set, but the RRs for single
  179. /// RRset must not be interleaved with any other RRs (eg. RRsets must be
  180. /// "together").
  181. ///
  182. /// \param columns The data will be returned through here. The order
  183. /// is specified by the RecordColumns enum, and the size must be
  184. /// COLUMN_COUNT
  185. /// \todo Do we consider databases where it is stored in binary blob
  186. /// format?
  187. /// \throw DataSourceError if there's database-related error. If the
  188. /// exception (or any other in case of derived class) is thrown,
  189. /// the iterator can't be safely used any more.
  190. /// \return true if a record was found, and the columns array was
  191. /// updated. false if there was no more data, in which case
  192. /// the columns array is untouched.
  193. virtual bool getNext(std::string (&columns)[COLUMN_COUNT]) = 0;
  194. };
  195. typedef boost::shared_ptr<IteratorContext> IteratorContextPtr;
  196. /// \brief Creates an iterator context for a specific name.
  197. ///
  198. /// Returns an IteratorContextPtr that contains all records of the
  199. /// given name from the given zone.
  200. ///
  201. /// The implementation of the iterator that is returned may leave the
  202. /// NAME_COLUMN column of the array passed to getNext() untouched, as that
  203. /// data is already known (it is the same as the name argument here)
  204. ///
  205. /// \exception any Since any implementation can be used, the caller should
  206. /// expect any exception to be thrown.
  207. ///
  208. /// \param name The name to search for. This should be a FQDN.
  209. /// \param id The ID of the zone, returned from getZone().
  210. /// \param subdomains If set to true, match subdomains of name instead
  211. /// of name itself. It is used to find empty domains and match
  212. /// wildcards.
  213. /// \return Newly created iterator context. Must not be NULL.
  214. virtual IteratorContextPtr getRecords(const std::string& name,
  215. int id,
  216. bool subdomains = false) const = 0;
  217. /// \brief Creates an iterator context for the whole zone.
  218. ///
  219. /// Returns an IteratorContextPtr that contains all records of the
  220. /// zone with the given zone id.
  221. ///
  222. /// Each call to getNext() on the returned iterator should copy all
  223. /// column fields of the array that is passed, as defined in the
  224. /// RecordColumns enum.
  225. ///
  226. /// \exception any Since any implementation can be used, the caller should
  227. /// expect any exception to be thrown.
  228. ///
  229. /// \param id The ID of the zone, returned from getZone().
  230. /// \return Newly created iterator context. Must not be NULL.
  231. virtual IteratorContextPtr getAllRecords(int id) const = 0;
  232. /// \brief Creates an iterator context for a set of differences.
  233. ///
  234. /// Returns an IteratorContextPtr that contains all difference records for
  235. /// the given zone between two versions of a zone.
  236. ///
  237. /// The difference records are the set of records that would appear in an
  238. /// IXFR serving a request for the difference between two versions of a
  239. /// zone. The records are returned in the same order as they would be in
  240. /// the IXFR. This means that if the the difference between versions of a
  241. /// zone with SOA serial numbers of "start" and "end" is required, and the
  242. /// zone contains the differences between serial number "start" to serial
  243. /// number "intermediate" and from serial number "intermediate" to serial
  244. /// number "end", the returned records will be (in order):
  245. ///
  246. /// \li SOA for serial "start"
  247. /// \li Records removed from the zone between versions "start" and
  248. /// "intermediate" of the zone. The order of these is not guaranteed.
  249. /// \li SOA for serial "intermediate"
  250. /// \li Records added to the zone between versions "start" and
  251. /// "intermediate" of the zone. The order of these is not guaranteed.
  252. /// \li SOA for serial "intermediate"
  253. /// \li Records removed from the zone between versions "intermediate" and
  254. /// "end" of the zone. The order of these is not guaranteed.
  255. /// \li SOA for serial "end"
  256. /// \li Records added to the zone between versions "intermediate" and "end"
  257. /// of the zone. The order of these is not guaranteed.
  258. ///
  259. /// Note that there is no requirement that "start" be less than "end".
  260. /// Owing to serial number arithmetic, it is entirely possible that a later
  261. /// version of a zone will have a smaller SOA serial number than an earlier
  262. /// version.
  263. ///
  264. /// Each call to getNext() on the returned iterator should copy all column
  265. /// fields of the array that is passed, as defined in the RecordColumns
  266. /// enum.
  267. ///
  268. /// \exception any Since any implementation can be used, the caller should
  269. /// expect any exception to be thrown.
  270. ///
  271. /// \param id The ID of the zone, returned from getZone().
  272. /// \param start The SOA serial number of the version of the zone from
  273. /// which the difference sequence should start.
  274. /// \param end The SOA serial number of the version of the zone at which
  275. /// the difference sequence should end.
  276. ///
  277. /// \return Newly created iterator context. Must not be NULL.
  278. virtual IteratorContextPtr
  279. getDiffs(int id, uint32_t start, uint32_t end) const = 0;
  280. /// \brief Start a transaction for updating a zone.
  281. ///
  282. /// Each derived class version of this method starts a database
  283. /// transaction to make updates to the given name of zone (whose class was
  284. /// specified at the construction of the class).
  285. ///
  286. /// If \c replace is true, any existing records of the zone will be
  287. /// deleted on successful completion of updates (after
  288. /// \c commitUpdateZone()); if it's false, the existing records will be
  289. /// intact unless explicitly deleted by \c deleteRecordInZone().
  290. ///
  291. /// A single \c DatabaseAccessor instance can perform at most one
  292. /// transaction; a duplicate call to this method before
  293. /// \c commitUpdateZone() or \c rollbackUpdateZone(), or a call to this
  294. /// method within another transaction started by \c startTransaction()
  295. /// will result in a \c DataSourceError exception.
  296. /// If multiple update attempts need to be performed concurrently (and
  297. /// if the underlying database allows such operation), separate
  298. /// \c DatabaseAccessor instance must be created.
  299. ///
  300. /// \note The underlying database may not allow concurrent updates to
  301. /// the same database instance even if different "connections" (or
  302. /// something similar specific to the database implementation) are used
  303. /// for different sets of updates. For example, it doesn't seem to be
  304. /// possible for SQLite3 unless different databases are used. MySQL
  305. /// allows concurrent updates to different tables of the same database,
  306. /// but a specific operation may block others. As such, this interface
  307. /// doesn't require derived classes to allow concurrent updates with
  308. /// multiple \c DatabaseAccessor instances; however, the implementation
  309. /// is encouraged to do the best for making it more likely to succeed
  310. /// as long as the underlying database system allows concurrent updates.
  311. ///
  312. /// This method returns a pair of \c bool and \c int. Its first element
  313. /// indicates whether the given name of zone is found. If it's false,
  314. /// the transaction isn't considered to be started; a subsequent call to
  315. /// this method with an existing zone name should succeed. Likewise,
  316. /// if a call to this method results in an exception, the transaction
  317. /// isn't considered to be started. Note also that if the zone is not
  318. /// found this method doesn't try to create a new one in the database.
  319. /// It must have been created by some other means beforehand.
  320. ///
  321. /// The second element is the internal zone ID used for subsequent
  322. /// updates. Depending on implementation details of the actual derived
  323. /// class method, it may be different from the one returned by
  324. /// \c getZone(); for example, a specific implementation may use a
  325. /// completely new zone ID when \c replace is true.
  326. ///
  327. /// \exception DataSourceError Duplicate call to this method, call to
  328. /// this method within another transaction, or some internal database
  329. /// related error.
  330. ///
  331. /// \param zone_name A string representation of the zone name to be updated
  332. /// \param replace Whether to replace the entire zone (see above)
  333. ///
  334. /// \return A pair of bool and int, indicating whether the specified zone
  335. /// exists and (if so) the zone ID to be used for the update, respectively.
  336. virtual std::pair<bool, int> startUpdateZone(const std::string& zone_name,
  337. bool replace) = 0;
  338. /// \brief Add a single record to the zone to be updated.
  339. ///
  340. /// This method provides a simple interface to insert a new record
  341. /// (a database "row") to the zone in the update context started by
  342. /// \c startUpdateZone(). The zone to which the record to be added
  343. /// is the one specified at the time of the \c startUpdateZone() call.
  344. ///
  345. /// A successful call to \c startUpdateZone() must have preceded to
  346. /// this call; otherwise a \c DataSourceError exception will be thrown.
  347. ///
  348. /// The row is defined as a vector of strings that has exactly
  349. /// ADD_COLUMN_COUNT number of elements. See AddRecordColumns for
  350. /// the semantics of each element.
  351. ///
  352. /// Derived class methods are not required to check whether the given
  353. /// values in \c columns are valid in terms of the expected semantics;
  354. /// in general, it's the caller's responsibility.
  355. /// For example, TTLs would normally be expected to be a textual
  356. /// representation of decimal numbers, but this interface doesn't require
  357. /// the implementation to perform this level of validation. It may check
  358. /// the values, however, and in that case if it detects an error it
  359. /// should throw a \c DataSourceError exception.
  360. ///
  361. /// Likewise, derived class methods are not required to detect any
  362. /// duplicate record that is already in the zone.
  363. ///
  364. /// \note The underlying database schema may not have a trivial mapping
  365. /// from this style of definition of rows to actual database records.
  366. /// It's the implementation's responsibility to implement the mapping
  367. /// in the actual derived method.
  368. ///
  369. /// \exception DataSourceError Invalid call without starting a transaction,
  370. /// or other internal database error.
  371. ///
  372. /// \param columns An array of strings that defines a record to be added
  373. /// to the zone.
  374. virtual void addRecordToZone(
  375. const std::string (&columns)[ADD_COLUMN_COUNT]) = 0;
  376. /// \brief Delete a single record from the zone to be updated.
  377. ///
  378. /// This method provides a simple interface to delete a record
  379. /// (a database "row") from the zone in the update context started by
  380. /// \c startUpdateZone(). The zone from which the record to be deleted
  381. /// is the one specified at the time of the \c startUpdateZone() call.
  382. ///
  383. /// A successful call to \c startUpdateZone() must have preceded to
  384. /// this call; otherwise a \c DataSourceError exception will be thrown.
  385. ///
  386. /// The record to be deleted is specified by a vector of strings that has
  387. /// exactly DEL_PARAM_COUNT number of elements. See DeleteRecordParams
  388. /// for the semantics of each element.
  389. ///
  390. /// \note In IXFR, TTL may also be specified, but we intentionally
  391. /// ignore that in this interface, because it's not guaranteed
  392. /// that all records have the same TTL (unlike the RRset
  393. /// assumption) and there can even be multiple records for the
  394. /// same name, type and rdata with different TTLs. If we only
  395. /// delete one of them, subsequent lookup will still return a
  396. /// positive answer, which would be confusing. It's a higher
  397. /// layer's responsibility to check if there is at least one
  398. /// record in the database that has the given TTL.
  399. ///
  400. /// Like \c addRecordToZone, derived class methods are not required to
  401. /// validate the semantics of the given parameters or to check if there
  402. /// is a record that matches the specified parameter; if there isn't
  403. /// it simply ignores the result.
  404. ///
  405. /// \exception DataSourceError Invalid call without starting a transaction,
  406. /// or other internal database error.
  407. ///
  408. /// \param params An array of strings that defines a record to be deleted
  409. /// from the zone.
  410. virtual void deleteRecordInZone(
  411. const std::string (&params)[DEL_PARAM_COUNT]) = 0;
  412. /// \brief Start a general transaction.
  413. ///
  414. /// Each derived class version of this method starts a database
  415. /// transaction in a way specific to the database details. Any subsequent
  416. /// operations on the accessor are guaranteed to be not susceptible to
  417. /// any update attempts made during the transaction. The transaction
  418. /// must be terminated by either \c commit() or \c rollback().
  419. ///
  420. /// In practice, this transaction is intended to be used to perform
  421. /// a set of atomic reads and work as a read-only lock. So, in many
  422. /// cases \c commit() and \c rollback() will have the same effect.
  423. ///
  424. /// This transaction cannot coexist with an update transaction started
  425. /// by \c startUpdateZone(). Such an attempt will result in
  426. /// \c DataSourceError.
  427. ///
  428. /// \exception DataSourceError An attempt of nested transaction, or some
  429. /// internal database related error.
  430. virtual void startTransaction() = 0;
  431. /// \brief Commit a transaction.
  432. ///
  433. /// This method completes a transaction started by \c startTransaction
  434. /// or \c startUpdateZone.
  435. ///
  436. /// A successful call to one of the "start" methods must have preceded to
  437. /// this call; otherwise a \c DataSourceError exception will be thrown.
  438. /// Once this method successfully completes, the transaction isn't
  439. /// considered to exist any more. So a new transaction can now be
  440. /// started. On the other hand, a duplicate call to this method after
  441. /// a successful completion of it is invalid and should result in
  442. /// a \c DataSourceError exception.
  443. ///
  444. /// If some internal database error happens, a \c DataSourceError
  445. /// exception must be thrown. In that case the transaction is still
  446. /// considered to be valid; the caller must explicitly rollback it
  447. /// or (if it's confident that the error is temporary) try to commit it
  448. /// again.
  449. ///
  450. /// \exception DataSourceError Call without a transaction, duplicate call
  451. /// to the method or internal database error.
  452. virtual void commit() = 0;
  453. /// \brief Rollback any changes in a transaction made so far.
  454. ///
  455. /// This method rollbacks a transaction started by \c startTransaction or
  456. /// \c startUpdateZone. When it succeeds (it normally should, but see
  457. /// below), the underlying database should be reverted to the point
  458. /// before performing the corresponding "start" method.
  459. ///
  460. /// A successful call to one of the "start" method must have preceded to
  461. /// this call; otherwise a \c DataSourceError exception will be thrown.
  462. /// Once this method successfully completes, the transaction isn't
  463. /// considered to exist any more. So a new transaction can now be
  464. /// started. On the other hand, a duplicate call to this method after
  465. /// a successful completion of it is invalid and should result in
  466. /// a \c DataSourceError exception.
  467. ///
  468. /// Normally this method should not fail. But it may not always be
  469. /// possible to guarantee it depending on the characteristics of the
  470. /// underlying database system. So this interface doesn't require the
  471. /// actual implementation for the error free property. But if a specific
  472. /// implementation of this method can fail, it is encouraged to document
  473. /// when that can happen with its implication.
  474. ///
  475. /// \exception DataSourceError Call without a transaction, duplicate call
  476. /// to the method or internal database error.
  477. virtual void rollback() = 0;
  478. /// \brief Install a single RR diff in difference sequences for zone update.
  479. ///
  480. /// This method inserts parameters of an update operation for a single RR
  481. /// (either adding or deleting one) in the underlying database.
  482. /// (These parameters would normally be a separate database table, but
  483. /// actual realization can differ in specific implementations).
  484. /// The information given via this method generally corresponds to either
  485. /// a single call to \c addRecordToZone() or \c deleteRecordInZone(),
  486. /// and this method is expected to be called immediately after (or before)
  487. /// a call to either of those methods.
  488. ///
  489. /// Note, however, that this method passes more detailed information
  490. /// than those update methods: it passes "serial", even if the diff
  491. /// is not for the SOA RR; it passes TTL for a diff that deletes an RR
  492. /// while in \c deleteRecordInZone() it's omitted. This is because
  493. /// the stored diffs are expected to be retrieved in the form that
  494. /// \c getDiffs() is expected to meet. This means if the caller
  495. /// wants to use this method with other update operations, it must
  496. /// ensure the additional information is ready when this method is called.
  497. ///
  498. /// The caller of this method must ensure that the added diffs via
  499. /// this method in a single transaction form an IXFR-style difference
  500. /// sequences: Each difference sequence is a sequence of RRs:
  501. /// an older version of SOA (to be deleted), zero or more other deleted
  502. /// RRs, the post-transaction SOA (to be added), and zero or more other
  503. /// added RRs. So, for example, the first call to this method in a
  504. /// transaction must always be deleting an SOA. Also, the \c serial
  505. /// parameter must be equal to the value of the serial field of the
  506. /// SOA that was last added or deleted (if the call is to add or delete
  507. /// an SOA RR, \c serial must be identical to the serial of that SOA).
  508. /// The underlying derived class implementation may or may not check
  509. /// this condition, but if the caller doesn't meet the condition
  510. /// a subsequent call to \c getDiffs() will not work as expected.
  511. ///
  512. /// Any call to this method must be in a transaction, and, for now,
  513. /// it must be a transaction triggered by \c startUpdateZone() (that is,
  514. /// it cannot be a transaction started by \c startTransaction()).
  515. /// All calls to this method are considered to be part of an atomic
  516. /// transaction: Until \c commit() is performed, the added diffs are
  517. /// not visible outside the transaction; if \c rollback() is performed,
  518. /// all added diffs are canceled; and the added sequences are not
  519. /// affected by any concurrent attempt of adding diffs (conflict resolution
  520. /// is up to the database implementation).
  521. ///
  522. /// Also for now, all diffs are assumed to be for the zone that is
  523. /// being updated in the context of \c startUpdateZone(). So the
  524. /// \c zone_id parameter must be identical to the zone ID returned by
  525. /// \c startUpdateZone().
  526. ///
  527. /// In a future version we may loosen this condition so that diffs can be
  528. /// added in a generic transaction and may not even have to belong to
  529. /// a single zone. For this possible extension \c zone_id parameter is
  530. /// included even if it's redundant under the current restriction.
  531. ///
  532. /// The support for adding (or retrieving) diffs is optional; if it's
  533. /// not supported in a specific data source, this method for the
  534. /// corresponding derived class will throw an \c NotImplemented exception.
  535. ///
  536. /// \exception DataSourceError Invalid call without starting a transaction,
  537. /// zone ID doesn't match the zone being updated, or other internal
  538. /// database error.
  539. /// \exception NotImplemented Adding diffs is not supported in the
  540. /// data source.
  541. /// \exception Other The concrete derived method may throw other
  542. /// data source specific exceptions.
  543. ///
  544. /// \param zone_id The zone for the diff to be added.
  545. /// \param serial The SOA serial to which the diff belongs.
  546. /// \param operation Either \c DIFF_ADD or \c DIFF_DELETE.
  547. /// \param params An array of strings that defines a record for the diff.
  548. virtual void addRecordDiff(
  549. int zone_id, uint32_t serial, DiffOperation operation,
  550. const std::string (&params)[DIFF_PARAM_COUNT]) = 0;
  551. /// \brief Clone the accessor with the same configuration.
  552. ///
  553. /// Each derived class implementation of this method will create a new
  554. /// accessor of the same derived class with the same configuration
  555. /// (such as the database server address) as that of the caller object
  556. /// and return it.
  557. ///
  558. /// Note that other internal states won't be copied to the new accessor
  559. /// even though the name of "clone" may indicate so. For example, even
  560. /// if the calling accessor is in the middle of a update transaction,
  561. /// the new accessor will not start a transaction to trace the same
  562. /// updates.
  563. ///
  564. /// The intended use case of cloning is to create a separate context
  565. /// where a specific set of database operations can be performed
  566. /// independently from the original accessor. The updater will use it
  567. /// so that multiple updaters can be created concurrently even if the
  568. /// underlying database system doesn't allow running multiple transactions
  569. /// in a single database connection.
  570. ///
  571. /// The underlying database system may not support the functionality
  572. /// that would be needed to implement this method. For example, it
  573. /// may not allow a single thread (or process) to have more than one
  574. /// database connections. In such a case the derived class implementation
  575. /// should throw a \c DataSourceError exception.
  576. ///
  577. /// \return A shared pointer to the cloned accessor.
  578. virtual boost::shared_ptr<DatabaseAccessor> clone() = 0;
  579. /// \brief Returns a string identifying this dabase backend
  580. ///
  581. /// The returned string is mainly intended to be used for
  582. /// debugging/logging purposes.
  583. ///
  584. /// Any implementation is free to choose the exact string content,
  585. /// but it is advisable to make it a name that is distinguishable
  586. /// from the others.
  587. ///
  588. /// \return the name of the database
  589. virtual const std::string& getDBName() const = 0;
  590. /// \brief It returns the previous name in DNSSEC order.
  591. ///
  592. /// This is used in DatabaseClient::findPreviousName and does more
  593. /// or less the real work, except for working on strings.
  594. ///
  595. /// \param rname The name to ask for previous of, in reversed form.
  596. /// We use the reversed form (see isc::dns::Name::reverse),
  597. /// because then the case insensitive order of string representation
  598. /// and the DNSSEC order correspond (eg. org.example.a is followed
  599. /// by org.example.a.b which is followed by org.example.b, etc).
  600. /// \param zone_id The zone to look through.
  601. /// \return The previous name.
  602. /// \note This function must return previous name even in case
  603. /// the queried rname does not exist in the zone.
  604. /// \note This method must skip under-the-zone-cut data (glue data).
  605. /// This might be implemented by looking for NSEC records (as glue
  606. /// data don't have them) in the zone or in some other way.
  607. ///
  608. /// \throw DataSourceError if there's a problem with the database.
  609. /// \throw NotImplemented if this database doesn't support DNSSEC
  610. /// or there's no previous name for the queried one (the NSECs
  611. /// might be missing or the queried name is less or equal the
  612. /// apex of the zone).
  613. virtual std::string findPreviousName(int zone_id,
  614. const std::string& rname) const = 0;
  615. };
  616. /// \brief Concrete data source client oriented at database backends.
  617. ///
  618. /// This class (together with corresponding versions of ZoneFinder,
  619. /// ZoneIterator, etc.) translates high-level data source queries to
  620. /// low-level calls on DatabaseAccessor. It calls multiple queries
  621. /// if necessary and validates data from the database, allowing the
  622. /// DatabaseAccessor to be just simple translation to SQL/other
  623. /// queries to database.
  624. ///
  625. /// While it is possible to subclass it for specific database in case
  626. /// of special needs, it is not expected to be needed. This should just
  627. /// work as it is with whatever DatabaseAccessor.
  628. class DatabaseClient : public DataSourceClient {
  629. public:
  630. /// \brief Constructor
  631. ///
  632. /// It initializes the client with a database via the given accessor.
  633. ///
  634. /// \exception isc::InvalidParameter if accessor is NULL. It might throw
  635. /// standard allocation exception as well, but doesn't throw anything else.
  636. ///
  637. /// \param rrclass The RR class of the zones that this client will handle.
  638. /// \param accessor The accessor to the database to use to get data.
  639. /// As the parameter suggests, the client takes ownership of the accessor
  640. /// and will delete it when itself deleted.
  641. DatabaseClient(isc::dns::RRClass rrclass,
  642. boost::shared_ptr<DatabaseAccessor> accessor);
  643. /// \brief Corresponding ZoneFinder implementation
  644. ///
  645. /// The zone finder implementation for database data sources. Similarly
  646. /// to the DatabaseClient, it translates the queries to methods of the
  647. /// database.
  648. ///
  649. /// Application should not come directly in contact with this class
  650. /// (it should handle it trough generic ZoneFinder pointer), therefore
  651. /// it could be completely hidden in the .cc file. But it is provided
  652. /// to allow testing and for rare cases when a database needs slightly
  653. /// different handling, so it can be subclassed.
  654. ///
  655. /// Methods directly corresponds to the ones in ZoneFinder.
  656. class Finder : public ZoneFinder {
  657. public:
  658. /// \brief Constructor
  659. ///
  660. /// \param database The database (shared with DatabaseClient) to
  661. /// be used for queries (the one asked for ID before).
  662. /// \param zone_id The zone ID which was returned from
  663. /// DatabaseAccessor::getZone and which will be passed to further
  664. /// calls to the database.
  665. /// \param origin The name of the origin of this zone. It could query
  666. /// it from database, but as the DatabaseClient just searched for
  667. /// the zone using the name, it should have it.
  668. Finder(boost::shared_ptr<DatabaseAccessor> database, int zone_id,
  669. const isc::dns::Name& origin);
  670. // The following three methods are just implementations of inherited
  671. // ZoneFinder's pure virtual methods.
  672. virtual isc::dns::Name getOrigin() const;
  673. virtual isc::dns::RRClass getClass() const;
  674. /// \brief Find an RRset in the datasource
  675. ///
  676. /// Searches the datasource for an RRset of the given name and
  677. /// type. If there is a CNAME at the given name, the CNAME rrset
  678. /// is returned.
  679. /// (this implementation is not complete, and currently only
  680. /// does full matches, CNAMES, and the signatures for matches and
  681. /// CNAMEs)
  682. ///
  683. /// \note Maybe counter intuitively, this method is not a const member
  684. /// function. This is intentional; some of the underlying
  685. /// implementations are expected to use a database backend, and would
  686. /// internally contain some abstraction of "database connection". In
  687. /// the most strict sense any (even read only) operation might change
  688. /// the internal state of such a connection, and in that sense the
  689. /// operation cannot be considered "const". In order to avoid giving a
  690. /// false sense of safety to the caller, we indicate a call to this
  691. /// method may have a surprising side effect. That said, this view may
  692. /// be too strict and it may make sense to say the internal database
  693. /// connection doesn't affect external behavior in terms of the
  694. /// interface of this method. As we gain more experiences with various
  695. /// kinds of backends we may revisit the constness.
  696. ///
  697. /// \exception DataSourceError when there is a problem reading
  698. /// the data from the dabase backend.
  699. /// This can be a connection, code, or
  700. /// data (parse) error.
  701. ///
  702. /// \param name The name to find
  703. /// \param type The RRType to find
  704. /// \param options Options about how to search.
  705. /// See ZoneFinder::FindOptions.
  706. virtual FindResult find(const isc::dns::Name& name,
  707. const isc::dns::RRType& type,
  708. const FindOptions options = FIND_DEFAULT);
  709. /// \brief Implementation of the ZoneFinder::findAll method.
  710. ///
  711. /// In short, it is mostly the same thing as find, but it returns all
  712. /// RRsets in the named node through the target parameter in successful
  713. /// case. It acts the same in the unsuccessful one.
  714. virtual FindResult findAll(const isc::dns::Name& name,
  715. std::vector<isc::dns::ConstRRsetPtr>& target,
  716. const FindOptions options = FIND_DEFAULT);
  717. /// \brief Implementation of ZoneFinder::findPreviousName method.
  718. virtual isc::dns::Name findPreviousName(const isc::dns::Name& query)
  719. const;
  720. /// Look for NSEC3 for proving (non)existence of given name.
  721. ///
  722. /// See documentation in \c Zone.
  723. virtual FindNSEC3Result
  724. findNSEC3(const isc::dns::Name& name, bool recursive);
  725. /// \brief The zone ID
  726. ///
  727. /// This function provides the stored zone ID as passed to the
  728. /// constructor. This is meant for testing purposes and normal
  729. /// applications shouldn't need it.
  730. int zone_id() const { return (zone_id_); }
  731. /// \brief The database accessor.
  732. ///
  733. /// This function provides the database accessor stored inside as
  734. /// passed to the constructor. This is meant for testing purposes and
  735. /// normal applications shouldn't need it.
  736. const DatabaseAccessor& getAccessor() const {
  737. return (*accessor_);
  738. }
  739. private:
  740. boost::shared_ptr<DatabaseAccessor> accessor_;
  741. const int zone_id_;
  742. const isc::dns::Name origin_;
  743. /// \brief Shortcut name for the result of getRRsets
  744. typedef std::pair<bool, std::map<dns::RRType, dns::RRsetPtr> >
  745. FoundRRsets;
  746. /// \brief Just shortcut for set of types
  747. typedef std::set<dns::RRType> WantedTypes;
  748. /// \brief Internal logit of find and findAll methods.
  749. ///
  750. /// Most of their handling is in the "error" cases and delegations
  751. /// and so on. So they share the logic here and find and findAll provide
  752. /// just an interface for it.
  753. ///
  754. /// Parameters and behaviour is like of those combined together.
  755. /// Unexpected parameters, like type != ANY and having the target, are
  756. /// just that - unexpected and not checked.
  757. FindResult findInternal(const isc::dns::Name& name,
  758. const isc::dns::RRType& type,
  759. std::vector<isc::dns::ConstRRsetPtr>* target,
  760. const FindOptions options = FIND_DEFAULT);
  761. /// \brief Searches database for RRsets of one domain.
  762. ///
  763. /// This method scans RRs of single domain specified by name and
  764. /// extracts any RRsets found and requested by parameters.
  765. ///
  766. /// It is used internally by find(), because it is called multiple
  767. /// times (usually with different domains).
  768. ///
  769. /// \param name Which domain name should be scanned.
  770. /// \param types List of types the caller is interested in.
  771. /// \param check_ns If this is set to true, it checks nothing lives
  772. /// together with NS record (with few little exceptions, like RRSIG
  773. /// or NSEC). This check is meant for non-apex NS records.
  774. /// \param construct_name If this is NULL, the resulting RRsets have
  775. /// their name set to name. If it is not NULL, it overrides the name
  776. /// and uses this one (this can be used for wildcard synthesized
  777. /// records).
  778. /// \param any If this is true, it records all the types, not only the
  779. /// ones requested by types. It also puts a NULL pointer under the
  780. /// ANY type into the result, if it finds any RRs at all, to easy the
  781. /// identification of success.
  782. /// \return A pair, where the first element indicates if the domain
  783. /// contains any RRs at all (not only the requested, it may happen
  784. /// this is set to true, but the second part is empty). The second
  785. /// part is map from RRtypes to RRsets of the corresponding types.
  786. /// If the RRset is not present in DB, the RRtype is not there at
  787. /// all (so you'll not find NULL pointer in the result).
  788. /// \throw DataSourceError If there's a low-level error with the
  789. /// database or the database contains bad data.
  790. FoundRRsets getRRsets(const std::string& name,
  791. const WantedTypes& types, bool check_ns,
  792. const std::string* construct_name = NULL,
  793. bool any = false);
  794. /// \brief Search result of \c findDelegationPoint().
  795. ///
  796. /// This is a tuple combining the result of the search - a status code
  797. /// and a pointer to the RRset found - together with additional
  798. /// information needed for subsequent processing, an indication of
  799. /// the first NS RRset found in the search and the number of labels
  800. /// in the last non-empty domain encountered in the search. It is
  801. /// used by \c findDelegationPoint().
  802. ///
  803. /// The last two items are located naturally in the search and although
  804. /// not strictly part of the result, they are passed back to avoid
  805. /// another (duplicate) search later in the processing.
  806. ///
  807. /// Note that the code and rrset elements are the same as that in
  808. /// the \c ZoneFinder::FindResult struct: this structure could be
  809. /// derived from that one, but as it is used just once in the code and
  810. /// will never be treated as a \c FindResult, the obscurity involved in
  811. /// deriving it from a parent class was deemed not worthwhile.
  812. struct DelegationSearchResult {
  813. DelegationSearchResult(const ZoneFinder::Result param_code,
  814. const isc::dns::ConstRRsetPtr param_rrset,
  815. const isc::dns::ConstRRsetPtr param_ns,
  816. size_t param_last_known) :
  817. code(param_code), rrset(param_rrset),
  818. first_ns(param_ns),
  819. last_known(param_last_known)
  820. {}
  821. const ZoneFinder::Result code; ///< Result code
  822. const isc::dns::ConstRRsetPtr rrset; ///< RRset found
  823. const isc::dns::ConstRRsetPtr first_ns; ///< First NS found
  824. const size_t last_known; ///< No. labels in last non-empty domain
  825. };
  826. /// \brief Find delegation point
  827. ///
  828. /// Given a name, searches through the superdomains from the origin
  829. /// down, searching for a point that indicates a delegation (i.e. an
  830. /// NS record or a DNAME).
  831. ///
  832. /// The method operates in two modes, non-glue-ok and glue-ok modes:
  833. ///
  834. /// In non-glue-ok mode, the search is made purely for the NS or DNAME
  835. /// RR. The zone is searched from the origin down looking for one
  836. /// of these RRTypes (and ignoring the NS records at the zone origin).
  837. /// A status is returned indicating what is found: DNAME, DELEGATION
  838. /// of SUCCESS, the last indicating that nothing was found, together
  839. /// with a pointer to the relevant RR.
  840. ///
  841. /// In glue-ok mode, the first NS encountered in the search (apart from
  842. /// the NS at the zone apex) is remembered but otherwise NS records are
  843. /// ignored and the search attempts to find a DNAME. The result is
  844. /// returned in the same format, along with a pointer to the first non-
  845. /// apex NS (if found).
  846. ///
  847. /// \param name The name to find
  848. /// \param options Options about how to search. See the documentation
  849. /// for ZoneFinder::FindOptions.
  850. ///
  851. /// \return Tuple holding the result of the search - the RRset of the
  852. /// delegation point and the type of the point (DELEGATION or
  853. /// DNAME) - and associated information. This latter item
  854. /// comprises two pieces of data: a pointer to the highest
  855. /// encountered NS, and the number of labels in the last known
  856. /// non-empty domain. The associated information is found as
  857. /// a natural part of the search for the delegation point and
  858. /// is used later in the find() processing; it is passed back
  859. /// to avoid the need to perform a second search to obtain it.
  860. DelegationSearchResult
  861. findDelegationPoint(const isc::dns::Name& name,
  862. const FindOptions options);
  863. /// \brief Find wildcard match
  864. ///
  865. /// Having found that the name is not an empty non-terminal, this
  866. /// searches the zone for for wildcards that match the name.
  867. ///
  868. /// It searches superdomains of the name from the zone origin down
  869. /// looking for a wildcard in the zone that matches the name. There
  870. /// are several cases to consider:
  871. ///
  872. /// - If the previous search for a delegation point has found that
  873. /// there is an NS at the superdomain of the point at which the
  874. /// wildcard is found, the delegation is returned.
  875. /// - If there is a match to the name, an appropriate status is
  876. /// returned (match on requested type, delegation, cname, or just
  877. /// the indication of a match but no RRs relevant to the query).
  878. /// - If the match is to an non-empty non-terminal wildcard, a
  879. /// wildcard NXRRSET is returned.
  880. ///
  881. /// Note that if DNSSEC is enabled for the search and the zone uses
  882. /// NSEC for authenticated denial of existence, the search may
  883. /// return NSEC records.
  884. ///
  885. /// \param name The name to find
  886. /// \param type The RRType to find
  887. /// \param options Options about how to search. See the documentation
  888. /// for ZoneFinder::FindOptions.
  889. /// \param dresult Result of the search through the zone for a
  890. /// delegation.
  891. /// \param target If the type happens to be ANY, it will insert all
  892. /// the RRsets of the found name (if any is found) here instead
  893. /// of being returned by the result.
  894. ///
  895. /// \return Tuple holding the result of the search - the RRset of the
  896. /// wildcard records matching the name, together with a status
  897. /// indicating the match type (e.g. CNAME at the wildcard
  898. /// match, no RRs of the requested type at the wildcard,
  899. /// success due to an exact match). Also returned if there
  900. /// is no match is an indication as to whether there was an
  901. /// NXDOMAIN or an NXRRSET.
  902. FindResult findWildcardMatch(
  903. const isc::dns::Name& name,
  904. const isc::dns::RRType& type, const FindOptions options,
  905. const DelegationSearchResult& dresult,
  906. std::vector<isc::dns::ConstRRsetPtr>* target);
  907. /// \brief Handle matching results for name
  908. ///
  909. /// This is called when something is found in the underlying database
  910. /// whose domain name is an exact match of the name to be searched for.
  911. /// It explores four possible cases to decide the final lookup result:
  912. /// - The name is a zone cut due to an NS RR.
  913. /// - CNAME is found (while the requested RR type is not CNAME).
  914. /// In this case multiple CNAMEs are checked and rejected with
  915. /// a \c DataSourceError exception.
  916. /// - Requested type is not found at that name.
  917. /// - A record of the requested type is found, or the query is ANY and
  918. /// some records were found.
  919. /// and returns a corresponding find result.
  920. ///
  921. /// This method is commonly used for normal (non wildcard) and wildcard
  922. /// matches.
  923. ///
  924. /// \param name The name to find
  925. /// \param type The RRType to find
  926. /// \param options Options about how to search. See the documentation
  927. /// for ZoneFinder::FindOptions.
  928. /// \param is_origin If name is the zone's origin name.
  929. /// \param found A set of found RRsets in the search for the name
  930. /// and type. It could contain one or more of the requested
  931. /// type, CNAME, NS, and NSEC RRsets of the name.
  932. /// \param wildname If non NULL, the method is called on a wildcard
  933. /// match, and points to a string object representing
  934. /// a textual form of the matched wildcard name;
  935. /// it's NULL in the case of non wildcard match.
  936. /// \param target When the query is any, this must be set to a vector
  937. /// where the result will be stored.
  938. ///
  939. /// \return Tuple holding the result of the search - the RRset of the
  940. /// wildcard records matching the name, together with a status
  941. /// indicating the match type (corresponding to the each of
  942. /// the above 4 cases). The return value is intended to be
  943. /// usable as a return value of the caller of this helper
  944. /// method.
  945. FindResult findOnNameResult(const isc::dns::Name& name,
  946. const isc::dns::RRType& type,
  947. const FindOptions options,
  948. const bool is_origin,
  949. const FoundRRsets& found,
  950. const std::string* wildname,
  951. std::vector<isc::dns::ConstRRsetPtr>* target);
  952. /// \brief Handle no match for name
  953. ///
  954. /// This is called when it is known that there is no delegation and
  955. /// there is no exact match for the name (regardless of RR types
  956. /// requested). Before returning NXDOMAIN, we need to check two
  957. /// cases:
  958. /// - Empty non-terminal: if the name has subdomains in the database,
  959. /// flag the fact. An NXRRSET will be returned (along with the
  960. /// NSEC record covering the requested domain name if DNSSEC data
  961. /// is being returned).
  962. /// - Wildcard: is there a wildcard record in the zone that matches
  963. /// requested name? If so, return it. If not, return the relevant
  964. /// NSEC records (if requested).
  965. ///
  966. /// \param name The name to find
  967. /// \param type The RRType to find
  968. /// \param options Options about how to search. See the documentation
  969. /// for ZoneFinder::FindOptions.
  970. /// \param dresult Result of the search through the zone for a
  971. /// delegation.
  972. /// \param target If the query is for type ANY, the successfull result,
  973. /// if there happens to be one, will be returned through the
  974. /// parameter, as it doesn't fit into the result.
  975. ///
  976. /// \return Tuple holding the result of the search - the RRset of the
  977. /// wildcard records matching the name, together with a status
  978. /// indicating the match type (e.g. CNAME at the wildcard
  979. /// match, no RRs of the requested type at the wildcard,
  980. /// success due to an exact match).
  981. FindResult findNoNameResult(const isc::dns::Name& name,
  982. const isc::dns::RRType& type,
  983. FindOptions options,
  984. const DelegationSearchResult& dresult,
  985. std::vector<isc::dns::ConstRRsetPtr>*
  986. target);
  987. /// Logs condition and creates result
  988. ///
  989. /// A convenience function used by findOnNameResult(), it both creates
  990. /// the FindResult object that find() will return to its caller as well
  991. /// as logging a debug message for the information being returned.
  992. ///
  993. /// \param name Domain name of the RR that was being sought.
  994. /// \param wildname Domain name string of a matched wildcard name or
  995. /// NULL for non wildcard match.
  996. /// \param type Type of RR being sought.
  997. /// \param code Result of the find operation
  998. /// \param rrset RRset found as a result of the find (which may be
  999. /// null).
  1000. /// \param log_id ID of the message being logged. Up to five
  1001. /// parameters are available to the message: data source name,
  1002. /// requested domain name, requested class, requested type
  1003. /// and (but only if the search was successful and returned
  1004. /// an RRset) details of the RRset found.
  1005. ///
  1006. /// \return FindResult object constructed from the code and rrset
  1007. /// arguments.
  1008. FindResult logAndCreateResult(const isc::dns::Name& name,
  1009. const std::string* wildname,
  1010. const isc::dns::RRType& type,
  1011. ZoneFinder::Result code,
  1012. isc::dns::ConstRRsetPtr rrset,
  1013. const isc::log::MessageID& log_id,
  1014. FindResultFlags flags) const;
  1015. /// \brief Checks if something lives below this domain.
  1016. ///
  1017. /// This looks if there's any subdomain of the given name. It can be
  1018. /// used to test if domain is empty non-terminal.
  1019. ///
  1020. /// \param name The domain to check.
  1021. ///
  1022. /// \return true if the name has subdomains, false if not.
  1023. bool hasSubdomains(const std::string& name);
  1024. /// \brief Get the NSEC covering a name.
  1025. ///
  1026. /// This one calls findPreviousName on the given name and extracts an
  1027. /// NSEC record on the result. It handles various error cases. The
  1028. /// method exists to share code present at more than one location.
  1029. dns::ConstRRsetPtr findNSECCover(const dns::Name& name);
  1030. /// \brief Convenience type shortcut.
  1031. ///
  1032. /// To find stuff in the result of getRRsets.
  1033. typedef std::map<dns::RRType, dns::RRsetPtr>::const_iterator
  1034. FoundIterator;
  1035. };
  1036. /// \brief Find a zone in the database
  1037. ///
  1038. /// This queries database's getZone to find the best matching zone.
  1039. /// It will propagate whatever exceptions are thrown from that method
  1040. /// (which is not restricted in any way).
  1041. ///
  1042. /// \param name Name of the zone or data contained there.
  1043. /// \return FindResult containing the code and an instance of Finder, if
  1044. /// anything is found. However, application should not rely on the
  1045. /// ZoneFinder being instance of Finder (possible subclass of this class
  1046. /// may return something else and it may change in future versions), it
  1047. /// should use it as a ZoneFinder only.
  1048. virtual FindResult findZone(const isc::dns::Name& name) const;
  1049. /// \brief Get the zone iterator
  1050. ///
  1051. /// The iterator allows going through the whole zone content. If the
  1052. /// underlying DatabaseConnection is implemented correctly, it should
  1053. /// be possible to have multiple ZoneIterators at once and query data
  1054. /// at the same time.
  1055. ///
  1056. /// \exception DataSourceError if the zone doesn't exist.
  1057. /// \exception isc::NotImplemented if the underlying DatabaseConnection
  1058. /// doesn't implement iteration. But in case it is not implemented
  1059. /// and the zone doesn't exist, DataSourceError is thrown.
  1060. /// \exception Anything else the underlying DatabaseConnection might
  1061. /// want to throw.
  1062. /// \param name The origin of the zone to iterate.
  1063. /// \param separate_rrs If true, the iterator will return each RR as a
  1064. /// new RRset object. If false, the iterator will
  1065. /// combine consecutive RRs with the name and type
  1066. /// into 1 RRset. The capitalization of the RRset will
  1067. /// be that of the first RR read, and TTLs will be
  1068. /// adjusted to the lowest one found.
  1069. /// \return Shared pointer to the iterator (it will never be NULL)
  1070. virtual ZoneIteratorPtr getIterator(const isc::dns::Name& name,
  1071. bool separate_rrs = false) const;
  1072. /// This implementation internally clones the accessor from the one
  1073. /// used in the client and starts a separate transaction using the cloned
  1074. /// accessor. The returned updater will be able to work separately from
  1075. /// the original client.
  1076. virtual ZoneUpdaterPtr getUpdater(const isc::dns::Name& name,
  1077. bool replace,
  1078. bool journaling = false) const;
  1079. /// This implementation internally clones the accessor from the one
  1080. /// used in the client for retrieving diffs and iterating over them.
  1081. /// The returned reader object will be able to work separately from
  1082. /// the original client.
  1083. virtual std::pair<ZoneJournalReader::Result, ZoneJournalReaderPtr>
  1084. getJournalReader(const isc::dns::Name& zone, uint32_t begin_serial,
  1085. uint32_t end_serial) const;
  1086. private:
  1087. /// \brief The RR class that this client handles.
  1088. const isc::dns::RRClass rrclass_;
  1089. /// \brief The accessor to our database.
  1090. const boost::shared_ptr<DatabaseAccessor> accessor_;
  1091. };
  1092. }
  1093. }
  1094. #endif // __DATABASE_DATASRC_H