database.h 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163
  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/zone.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 ZoneFinderContextPtr find(const isc::dns::Name& name,
  707. const isc::dns::RRType& type,
  708. const FindOptions options =
  709. FIND_DEFAULT);
  710. /// \brief Implementation of the ZoneFinder::findAll method.
  711. ///
  712. /// In short, it is mostly the same thing as find, but it returns all
  713. /// RRsets in the named node through the target parameter in successful
  714. /// case. It acts the same in the unsuccessful one.
  715. virtual ZoneFinderContextPtr findAll(
  716. const isc::dns::Name& name,
  717. std::vector<isc::dns::ConstRRsetPtr>& target,
  718. const FindOptions options = FIND_DEFAULT);
  719. /// \brief Implementation of ZoneFinder::findPreviousName method.
  720. virtual isc::dns::Name findPreviousName(const isc::dns::Name& query)
  721. const;
  722. /// Look for NSEC3 for proving (non)existence of given name.
  723. ///
  724. /// See documentation in \c Zone.
  725. virtual FindNSEC3Result
  726. findNSEC3(const isc::dns::Name& name, bool recursive);
  727. /// \brief The zone ID
  728. ///
  729. /// This function provides the stored zone ID as passed to the
  730. /// constructor. This is meant for testing purposes and normal
  731. /// applications shouldn't need it.
  732. int zone_id() const { return (zone_id_); }
  733. /// \brief The database accessor.
  734. ///
  735. /// This function provides the database accessor stored inside as
  736. /// passed to the constructor. This is meant for testing purposes and
  737. /// normal applications shouldn't need it.
  738. const DatabaseAccessor& getAccessor() const {
  739. return (*accessor_);
  740. }
  741. private:
  742. boost::shared_ptr<DatabaseAccessor> accessor_;
  743. const int zone_id_;
  744. const isc::dns::Name origin_;
  745. /// \brief Shortcut name for the result of getRRsets
  746. typedef std::pair<bool, std::map<dns::RRType, dns::RRsetPtr> >
  747. FoundRRsets;
  748. /// \brief Just shortcut for set of types
  749. typedef std::set<dns::RRType> WantedTypes;
  750. /// \brief Internal logit of find and findAll methods.
  751. ///
  752. /// Most of their handling is in the "error" cases and delegations
  753. /// and so on. So they share the logic here and find and findAll provide
  754. /// just an interface for it.
  755. ///
  756. /// Parameters and behaviour is like of those combined together.
  757. /// Unexpected parameters, like type != ANY and having the target, are
  758. /// just that - unexpected and not checked.
  759. ResultContext findInternal(const isc::dns::Name& name,
  760. const isc::dns::RRType& type,
  761. std::vector<isc::dns::ConstRRsetPtr>*
  762. target,
  763. const FindOptions options = FIND_DEFAULT);
  764. /// \brief Searches database for RRsets of one domain.
  765. ///
  766. /// This method scans RRs of single domain specified by name and
  767. /// extracts any RRsets found and requested by parameters.
  768. ///
  769. /// It is used internally by find(), because it is called multiple
  770. /// times (usually with different domains).
  771. ///
  772. /// \param name Which domain name should be scanned.
  773. /// \param types List of types the caller is interested in.
  774. /// \param check_ns If this is set to true, it checks nothing lives
  775. /// together with NS record (with few little exceptions, like RRSIG
  776. /// or NSEC). This check is meant for non-apex NS records.
  777. /// \param construct_name If this is NULL, the resulting RRsets have
  778. /// their name set to name. If it is not NULL, it overrides the name
  779. /// and uses this one (this can be used for wildcard synthesized
  780. /// records).
  781. /// \param any If this is true, it records all the types, not only the
  782. /// ones requested by types. It also puts a NULL pointer under the
  783. /// ANY type into the result, if it finds any RRs at all, to easy the
  784. /// identification of success.
  785. /// \return A pair, where the first element indicates if the domain
  786. /// contains any RRs at all (not only the requested, it may happen
  787. /// this is set to true, but the second part is empty). The second
  788. /// part is map from RRtypes to RRsets of the corresponding types.
  789. /// If the RRset is not present in DB, the RRtype is not there at
  790. /// all (so you'll not find NULL pointer in the result).
  791. /// \throw DataSourceError If there's a low-level error with the
  792. /// database or the database contains bad data.
  793. FoundRRsets getRRsets(const std::string& name,
  794. const WantedTypes& types, bool check_ns,
  795. const std::string* construct_name = NULL,
  796. bool any = false);
  797. /// \brief Search result of \c findDelegationPoint().
  798. ///
  799. /// This is a tuple combining the result of the search - a status code
  800. /// and a pointer to the RRset found - together with additional
  801. /// information needed for subsequent processing, an indication of
  802. /// the first NS RRset found in the search and the number of labels
  803. /// in the last non-empty domain encountered in the search. It is
  804. /// used by \c findDelegationPoint().
  805. ///
  806. /// The last two items are located naturally in the search and although
  807. /// not strictly part of the result, they are passed back to avoid
  808. /// another (duplicate) search later in the processing.
  809. ///
  810. /// Note that the code and rrset elements are the same as that in
  811. /// the \c ZoneFinder::FindResult struct: this structure could be
  812. /// derived from that one, but as it is used just once in the code and
  813. /// will never be treated as a \c FindResult, the obscurity involved in
  814. /// deriving it from a parent class was deemed not worthwhile.
  815. struct DelegationSearchResult {
  816. DelegationSearchResult(const ZoneFinder::Result param_code,
  817. const isc::dns::ConstRRsetPtr param_rrset,
  818. const isc::dns::ConstRRsetPtr param_ns,
  819. size_t param_last_known) :
  820. code(param_code), rrset(param_rrset),
  821. first_ns(param_ns),
  822. last_known(param_last_known)
  823. {}
  824. const ZoneFinder::Result code; ///< Result code
  825. const isc::dns::ConstRRsetPtr rrset; ///< RRset found
  826. const isc::dns::ConstRRsetPtr first_ns; ///< First NS found
  827. const size_t last_known; ///< No. labels in last non-empty domain
  828. };
  829. /// \brief Find delegation point
  830. ///
  831. /// Given a name, searches through the superdomains from the origin
  832. /// down, searching for a point that indicates a delegation (i.e. an
  833. /// NS record or a DNAME).
  834. ///
  835. /// The method operates in two modes, non-glue-ok and glue-ok modes:
  836. ///
  837. /// In non-glue-ok mode, the search is made purely for the NS or DNAME
  838. /// RR. The zone is searched from the origin down looking for one
  839. /// of these RRTypes (and ignoring the NS records at the zone origin).
  840. /// A status is returned indicating what is found: DNAME, DELEGATION
  841. /// of SUCCESS, the last indicating that nothing was found, together
  842. /// with a pointer to the relevant RR.
  843. ///
  844. /// In glue-ok mode, the first NS encountered in the search (apart from
  845. /// the NS at the zone apex) is remembered but otherwise NS records are
  846. /// ignored and the search attempts to find a DNAME. The result is
  847. /// returned in the same format, along with a pointer to the first non-
  848. /// apex NS (if found).
  849. ///
  850. /// \param name The name to find
  851. /// \param options Options about how to search. See the documentation
  852. /// for ZoneFinder::FindOptions.
  853. ///
  854. /// \return Tuple holding the result of the search - the RRset of the
  855. /// delegation point and the type of the point (DELEGATION or
  856. /// DNAME) - and associated information. This latter item
  857. /// comprises two pieces of data: a pointer to the highest
  858. /// encountered NS, and the number of labels in the last known
  859. /// non-empty domain. The associated information is found as
  860. /// a natural part of the search for the delegation point and
  861. /// is used later in the find() processing; it is passed back
  862. /// to avoid the need to perform a second search to obtain it.
  863. DelegationSearchResult
  864. findDelegationPoint(const isc::dns::Name& name,
  865. const FindOptions options);
  866. /// \brief Find wildcard match
  867. ///
  868. /// Having found that the name is not an empty non-terminal, this
  869. /// searches the zone for for wildcards that match the name.
  870. ///
  871. /// It searches superdomains of the name from the zone origin down
  872. /// looking for a wildcard in the zone that matches the name. There
  873. /// are several cases to consider:
  874. ///
  875. /// - If the previous search for a delegation point has found that
  876. /// there is an NS at the superdomain of the point at which the
  877. /// wildcard is found, the delegation is returned.
  878. /// - If there is a match to the name, an appropriate status is
  879. /// returned (match on requested type, delegation, cname, or just
  880. /// the indication of a match but no RRs relevant to the query).
  881. /// - If the match is to an non-empty non-terminal wildcard, a
  882. /// wildcard NXRRSET is returned.
  883. ///
  884. /// Note that if DNSSEC is enabled for the search and the zone uses
  885. /// NSEC for authenticated denial of existence, the search may
  886. /// return NSEC records.
  887. ///
  888. /// \param name The name to find
  889. /// \param type The RRType to find
  890. /// \param options Options about how to search. See the documentation
  891. /// for ZoneFinder::FindOptions.
  892. /// \param dresult Result of the search through the zone for a
  893. /// delegation.
  894. /// \param target If the type happens to be ANY, it will insert all
  895. /// the RRsets of the found name (if any is found) here instead
  896. /// of being returned by the result.
  897. ///
  898. /// \return Tuple holding the result of the search - the RRset of the
  899. /// wildcard records matching the name, together with a status
  900. /// indicating the match type (e.g. CNAME at the wildcard
  901. /// match, no RRs of the requested type at the wildcard,
  902. /// success due to an exact match). Also returned if there
  903. /// is no match is an indication as to whether there was an
  904. /// NXDOMAIN or an NXRRSET.
  905. ResultContext findWildcardMatch(
  906. const isc::dns::Name& name,
  907. const isc::dns::RRType& type,
  908. const FindOptions options,
  909. const DelegationSearchResult& dresult,
  910. std::vector<isc::dns::ConstRRsetPtr>* target);
  911. /// \brief Handle matching results for name
  912. ///
  913. /// This is called when something is found in the underlying database
  914. /// whose domain name is an exact match of the name to be searched for.
  915. /// It explores four possible cases to decide the final lookup result:
  916. /// - The name is a zone cut due to an NS RR.
  917. /// - CNAME is found (while the requested RR type is not CNAME).
  918. /// In this case multiple CNAMEs are checked and rejected with
  919. /// a \c DataSourceError exception.
  920. /// - Requested type is not found at that name.
  921. /// - A record of the requested type is found, or the query is ANY and
  922. /// some records were found.
  923. /// and returns a corresponding find result.
  924. ///
  925. /// This method is commonly used for normal (non wildcard) and wildcard
  926. /// matches.
  927. ///
  928. /// \param name The name to find
  929. /// \param type The RRType to find
  930. /// \param options Options about how to search. See the documentation
  931. /// for ZoneFinder::FindOptions.
  932. /// \param is_origin If name is the zone's origin name.
  933. /// \param found A set of found RRsets in the search for the name
  934. /// and type. It could contain one or more of the requested
  935. /// type, CNAME, NS, and NSEC RRsets of the name.
  936. /// \param wildname If non NULL, the method is called on a wildcard
  937. /// match, and points to a string object representing
  938. /// a textual form of the matched wildcard name;
  939. /// it's NULL in the case of non wildcard match.
  940. /// \param target When the query is any, this must be set to a vector
  941. /// where the result will be stored.
  942. ///
  943. /// \return Tuple holding the result of the search - the RRset of the
  944. /// wildcard records matching the name, together with a status
  945. /// indicating the match type (corresponding to the each of
  946. /// the above 4 cases). The return value is intended to be
  947. /// usable as a return value of the caller of this helper
  948. /// method.
  949. ResultContext findOnNameResult(const isc::dns::Name& name,
  950. const isc::dns::RRType& type,
  951. const FindOptions options,
  952. const bool is_origin,
  953. const FoundRRsets& found,
  954. const std::string* wildname,
  955. std::vector<isc::dns::ConstRRsetPtr>*
  956. target);
  957. /// \brief Handle no match for name
  958. ///
  959. /// This is called when it is known that there is no delegation and
  960. /// there is no exact match for the name (regardless of RR types
  961. /// requested). Before returning NXDOMAIN, we need to check two
  962. /// cases:
  963. /// - Empty non-terminal: if the name has subdomains in the database,
  964. /// flag the fact. An NXRRSET will be returned (along with the
  965. /// NSEC record covering the requested domain name if DNSSEC data
  966. /// is being returned).
  967. /// - Wildcard: is there a wildcard record in the zone that matches
  968. /// requested name? If so, return it. If not, return the relevant
  969. /// NSEC records (if requested).
  970. ///
  971. /// \param name The name to find
  972. /// \param type The RRType to find
  973. /// \param options Options about how to search. See the documentation
  974. /// for ZoneFinder::FindOptions.
  975. /// \param dresult Result of the search through the zone for a
  976. /// delegation.
  977. /// \param target If the query is for type ANY, the successfull result,
  978. /// if there happens to be one, will be returned through the
  979. /// parameter, as it doesn't fit into the result.
  980. ///
  981. /// \return Tuple holding the result of the search - the RRset of the
  982. /// wildcard records matching the name, together with a status
  983. /// indicating the match type (e.g. CNAME at the wildcard
  984. /// match, no RRs of the requested type at the wildcard,
  985. /// success due to an exact match).
  986. ResultContext findNoNameResult(const isc::dns::Name& name,
  987. const isc::dns::RRType& type,
  988. FindOptions options,
  989. const DelegationSearchResult& dresult,
  990. std::vector<isc::dns::ConstRRsetPtr>*
  991. target);
  992. /// Logs condition and creates result
  993. ///
  994. /// A convenience function used by findOnNameResult(), it both creates
  995. /// the FindResult object that find() will return to its caller as well
  996. /// as logging a debug message for the information being returned.
  997. ///
  998. /// \param name Domain name of the RR that was being sought.
  999. /// \param wildname Domain name string of a matched wildcard name or
  1000. /// NULL for non wildcard match.
  1001. /// \param type Type of RR being sought.
  1002. /// \param code Result of the find operation
  1003. /// \param rrset RRset found as a result of the find (which may be
  1004. /// null).
  1005. /// \param log_id ID of the message being logged. Up to five
  1006. /// parameters are available to the message: data source name,
  1007. /// requested domain name, requested class, requested type
  1008. /// and (but only if the search was successful and returned
  1009. /// an RRset) details of the RRset found.
  1010. ///
  1011. /// \return FindResult object constructed from the code and rrset
  1012. /// arguments.
  1013. ResultContext logAndCreateResult(const isc::dns::Name& name,
  1014. const std::string* wildname,
  1015. const isc::dns::RRType& type,
  1016. ZoneFinder::Result code,
  1017. isc::dns::ConstRRsetPtr rrset,
  1018. const isc::log::MessageID& log_id,
  1019. FindResultFlags flags) const;
  1020. /// \brief Checks if something lives below this domain.
  1021. ///
  1022. /// This looks if there's any subdomain of the given name. It can be
  1023. /// used to test if domain is empty non-terminal.
  1024. ///
  1025. /// \param name The domain to check.
  1026. ///
  1027. /// \return true if the name has subdomains, false if not.
  1028. bool hasSubdomains(const std::string& name);
  1029. /// \brief Get the NSEC covering a name.
  1030. ///
  1031. /// This one calls findPreviousName on the given name and extracts an
  1032. /// NSEC record on the result. It handles various error cases. The
  1033. /// method exists to share code present at more than one location.
  1034. dns::ConstRRsetPtr findNSECCover(const dns::Name& name);
  1035. /// \brief Convenience type shortcut.
  1036. ///
  1037. /// To find stuff in the result of getRRsets.
  1038. typedef std::map<dns::RRType, dns::RRsetPtr>::const_iterator
  1039. FoundIterator;
  1040. };
  1041. /// \brief Find a zone in the database
  1042. ///
  1043. /// This queries database's getZone to find the best matching zone.
  1044. /// It will propagate whatever exceptions are thrown from that method
  1045. /// (which is not restricted in any way).
  1046. ///
  1047. /// \param name Name of the zone or data contained there.
  1048. /// \return FindResult containing the code and an instance of Finder, if
  1049. /// anything is found. However, application should not rely on the
  1050. /// ZoneFinder being instance of Finder (possible subclass of this class
  1051. /// may return something else and it may change in future versions), it
  1052. /// should use it as a ZoneFinder only.
  1053. virtual FindResult findZone(const isc::dns::Name& name) const;
  1054. /// \brief Get the zone iterator
  1055. ///
  1056. /// The iterator allows going through the whole zone content. If the
  1057. /// underlying DatabaseConnection is implemented correctly, it should
  1058. /// be possible to have multiple ZoneIterators at once and query data
  1059. /// at the same time.
  1060. ///
  1061. /// \exception DataSourceError if the zone doesn't exist.
  1062. /// \exception isc::NotImplemented if the underlying DatabaseConnection
  1063. /// doesn't implement iteration. But in case it is not implemented
  1064. /// and the zone doesn't exist, DataSourceError is thrown.
  1065. /// \exception Anything else the underlying DatabaseConnection might
  1066. /// want to throw.
  1067. /// \param name The origin of the zone to iterate.
  1068. /// \param separate_rrs If true, the iterator will return each RR as a
  1069. /// new RRset object. If false, the iterator will
  1070. /// combine consecutive RRs with the name and type
  1071. /// into 1 RRset. The capitalization of the RRset will
  1072. /// be that of the first RR read, and TTLs will be
  1073. /// adjusted to the lowest one found.
  1074. /// \return Shared pointer to the iterator (it will never be NULL)
  1075. virtual ZoneIteratorPtr getIterator(const isc::dns::Name& name,
  1076. bool separate_rrs = false) const;
  1077. /// This implementation internally clones the accessor from the one
  1078. /// used in the client and starts a separate transaction using the cloned
  1079. /// accessor. The returned updater will be able to work separately from
  1080. /// the original client.
  1081. virtual ZoneUpdaterPtr getUpdater(const isc::dns::Name& name,
  1082. bool replace,
  1083. bool journaling = false) const;
  1084. /// This implementation internally clones the accessor from the one
  1085. /// used in the client for retrieving diffs and iterating over them.
  1086. /// The returned reader object will be able to work separately from
  1087. /// the original client.
  1088. virtual std::pair<ZoneJournalReader::Result, ZoneJournalReaderPtr>
  1089. getJournalReader(const isc::dns::Name& zone, uint32_t begin_serial,
  1090. uint32_t end_serial) const;
  1091. private:
  1092. /// \brief The RR class that this client handles.
  1093. const isc::dns::RRClass rrclass_;
  1094. /// \brief The accessor to our database.
  1095. const boost::shared_ptr<DatabaseAccessor> accessor_;
  1096. };
  1097. }
  1098. }
  1099. #endif // __DATABASE_DATASRC_H
  1100. // Local Variables:
  1101. // mode: c++
  1102. // End: