zone.h 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  1. // Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #ifndef __ZONE_H
  15. #define __ZONE_H 1
  16. #include <utility>
  17. #include <vector>
  18. #include <dns/rrset.h>
  19. #include <dns/rrsetlist.h>
  20. #include <datasrc/result.h>
  21. namespace isc {
  22. namespace datasrc {
  23. /// \brief The base class to search a zone for RRsets
  24. ///
  25. /// The \c ZoneFinder class is an abstract base class for representing
  26. /// an object that performs DNS lookups in a specific zone accessible via
  27. /// a data source. In general, different types of data sources (in-memory,
  28. /// database-based, etc) define their own derived classes of \c ZoneFinder,
  29. /// implementing ways to retrieve the required data through the common
  30. /// interfaces declared in the base class. Each concrete \c ZoneFinder
  31. /// object is therefore (conceptually) associated with a specific zone
  32. /// of one specific data source instance.
  33. ///
  34. /// The origin name and the RR class of the associated zone are available
  35. /// via the \c getOrigin() and \c getClass() methods, respectively.
  36. ///
  37. /// The most important method of this class is \c find(), which performs
  38. /// the lookup for a given domain and type. See the description of the
  39. /// method for details.
  40. ///
  41. /// \note It's not clear whether we should request that a zone finder form a
  42. /// "transaction", that is, whether to ensure the finder is not susceptible
  43. /// to changes made by someone else than the creator of the finder. If we
  44. /// don't request that, for example, two different lookup results for the
  45. /// same name and type can be different if other threads or programs make
  46. /// updates to the zone between the lookups. We should revisit this point
  47. /// as we gain more experiences.
  48. class ZoneFinder {
  49. public:
  50. /// Result codes of the \c find() method.
  51. ///
  52. /// Note: the codes are tentative. We may need more, or we may find
  53. /// some of them unnecessary as we implement more details.
  54. ///
  55. /// Some are synonyms of others in terms of RCODE returned to user.
  56. /// But they help the logic to decide if it should ask for a NSEC
  57. /// that covers something or not (for example, in case of NXRRSET,
  58. /// the directly returned NSEC is sufficient, but with wildcard one,
  59. /// we need to add one proving there's no exact match and this is
  60. /// actually the best wildcard we have). Data sources that don't
  61. /// support DNSSEC don't need to distinguish them.
  62. ///
  63. /// In case of CNAME, if the CNAME is a wildcard (i.e., its owner name
  64. /// starts with the label "*"), WILDCARD_CNAME will be returned instead
  65. /// of CNAME.
  66. ///
  67. /// In case of NXDOMAIN, the returned NSEC covers the queried domain
  68. /// that proves that the query name does not exist in the zone. Note that
  69. /// this does not necessarily prove it doesn't even match a wildcard
  70. /// (even if the result of NXDOMAIN can only happen when there's no
  71. /// matching wildcard either). It is caller's responsibility to provide
  72. /// a proof that there is no matching wildcard if that proof is necessary.
  73. ///
  74. /// Various variants of "no data" cases are complicated, when involves
  75. /// DNSSEC and wildcard processing. Referring to Section 3.1.3 of
  76. /// RFC4035, we need to consider the following cases:
  77. /// -# (Normal) no data: there is a matching non-wildcard name with a
  78. /// different RR type. This is the "No Data" case of the RFC.
  79. /// -# (Normal) empty non terminal: there is no matching (exact or
  80. /// wildcard) name, but there is a subdomain with an RR of the query
  81. /// name. This is one case of "Name Error" of the RFC.
  82. /// -# Wildcard empty non terminal: similar to 2a, but the empty name
  83. /// is a wildcard, and matches the query name by wildcard expansion.
  84. /// This is a special case of "Name Error" of the RFC.
  85. /// -# Wildcard no data: there is no exact match name, but there is a
  86. /// wildcard name that matches the query name with a different type
  87. /// of RR. This is the "Wildcard No Data" case of the RFC.
  88. ///
  89. /// In any case, \c find() will result in \c NXRRSET with no RRset
  90. /// unless the \c FIND_DNSSEC option is specified. The rest of the
  91. /// discussion only applies to the case where this option is specified.
  92. ///
  93. /// In case 1, \c find() will result in NXRRSET, and return NSEC of the
  94. /// matching name.
  95. ///
  96. /// In case 2, \c find() will result in NXRRSET, and return NSEC for the
  97. /// interval where the empty nonterminal lives. The end of the interval
  98. /// is the subdomain causing existence of the empty nonterminal (if
  99. /// there's sub.x.example.com, and no record in x.example.com, then
  100. /// x.example.com exists implicitly - is the empty nonterminal and
  101. /// sub.x.example.com is the subdomain causing it). Note that this NSEC
  102. /// proves not only the existence of empty non terminal name but also
  103. /// the non existence of possibly matching wildcard name, because
  104. /// there can be no better wildcard match than the exact matching empty
  105. /// name.
  106. ///
  107. /// In case 3, \c find() will result in WILDCARD_NXRRSET, and return NSEC
  108. /// for the interval where the wildcard empty nonterminal lives.
  109. /// Cases 2 and 3 are especially complicated and confusing. See the
  110. /// examples below.
  111. ///
  112. /// In case 4, \c find() will result in WILDCARD_NXRRSET, and return
  113. /// NSEC of the matching wildcard name.
  114. ///
  115. /// Examples: if zone "example.com" has the following record:
  116. /// \code
  117. /// a.example.com. NSEC a.b.example.com.
  118. /// \endcode
  119. /// a call to \c find() for "b.example.com." with the FIND_DNSSEC option
  120. /// will result in NXRRSET, and this NSEC will be returned.
  121. /// Likewise, if zone "example.org" has the following record,
  122. /// \code
  123. /// a.example.org. NSEC x.*.b.example.org.
  124. /// \endcode
  125. /// a call to \c find() for "y.b.example.org" with FIND_DNSSEC will
  126. /// result in NXRRSET_NXRRSET, and this NSEC will be returned.
  127. enum Result {
  128. SUCCESS, ///< An exact match is found.
  129. DELEGATION, ///< The search encounters a zone cut.
  130. NXDOMAIN, ///< There is no domain name that matches the search name
  131. NXRRSET, ///< There is a matching name but no RRset of the search type
  132. CNAME, ///< The search encounters and returns a CNAME RR
  133. DNAME, ///< The search encounters and returns a DNAME RR
  134. WILDCARD, ///< Succes by wildcard match, for DNSSEC
  135. WILDCARD_CNAME, ///< CNAME on wildcard, search returns CNAME, for DNSSEC
  136. WILDCARD_NXRRSET ///< NXRRSET on wildcard, for DNSSEC
  137. };
  138. /// A helper structure to represent the search result of \c find().
  139. ///
  140. /// This is a straightforward tuple of the result code and a pointer
  141. /// to the found RRset to represent the result of \c find()
  142. /// (there will be more members in the future - see the class
  143. /// description).
  144. /// We use this in order to avoid overloading the return value for both
  145. /// the result code ("success" or "not found") and the found object,
  146. /// i.e., avoid using \c NULL to mean "not found", etc.
  147. ///
  148. /// This is a simple value class whose internal state never changes,
  149. /// so for convenience we allow the applications to refer to the members
  150. /// directly.
  151. ///
  152. /// Note: we should eventually include a notion of "zone node", which
  153. /// corresponds to a particular domain name of the zone, so that we can
  154. /// find RRsets of a different RR type for that name (e.g. for type ANY
  155. /// query or to include DS RRs with delegation).
  156. ///
  157. /// Note: we may also want to include the closest enclosure "node" to
  158. /// optimize including the NSEC for no-wildcard proof (FWIW NSD does that).
  159. struct FindResult {
  160. FindResult(Result param_code,
  161. const isc::dns::ConstRRsetPtr param_rrset) :
  162. code(param_code), rrset(param_rrset)
  163. {}
  164. const Result code;
  165. const isc::dns::ConstRRsetPtr rrset;
  166. };
  167. /// Find options.
  168. ///
  169. /// The option values are used as a parameter for \c find().
  170. /// These are values of a bitmask type. Bitwise operations can be
  171. /// performed on these values to express compound options.
  172. enum FindOptions {
  173. FIND_DEFAULT = 0, ///< The default options
  174. FIND_GLUE_OK = 1, ///< Allow search under a zone cut
  175. FIND_DNSSEC = 2, ///< Require DNSSEC data in the answer
  176. ///< (RRSIG, NSEC, etc.). The implementation
  177. ///< is allowed to include it even if it is
  178. ///< not set.
  179. NO_WILDCARD = 4 ///< Do not try wildcard matching.
  180. };
  181. ///
  182. /// \name Constructors and Destructor.
  183. ///
  184. //@{
  185. protected:
  186. /// The default constructor.
  187. ///
  188. /// This is intentionally defined as \c protected as this base class should
  189. /// never be instantiated (except as part of a derived class).
  190. ZoneFinder() {}
  191. public:
  192. /// The destructor.
  193. virtual ~ZoneFinder() {}
  194. //@}
  195. ///
  196. /// \name Getter Methods
  197. ///
  198. /// These methods should never throw an exception.
  199. //@{
  200. /// Return the origin name of the zone.
  201. virtual isc::dns::Name getOrigin() const = 0;
  202. /// Return the RR class of the zone.
  203. virtual isc::dns::RRClass getClass() const = 0;
  204. //@}
  205. ///
  206. /// \name Search Methods
  207. ///
  208. //@{
  209. /// Search the zone for a given pair of domain name and RR type.
  210. ///
  211. /// Each derived version of this method searches the underlying backend
  212. /// for the data that best matches the given name and type.
  213. /// This method is expected to be "intelligent", and identifies the
  214. /// best possible answer for the search key. Specifically,
  215. ///
  216. /// - If the search name belongs under a zone cut, it returns the code
  217. /// of \c DELEGATION and the NS RRset at the zone cut.
  218. /// - If there is no matching name, it returns the code of \c NXDOMAIN,
  219. /// and, if DNSSEC is requested, the NSEC RRset that proves the
  220. /// non-existence if the zone is signed with NSEC; if it's signed with
  221. /// NSEC3, an empty NSEC3 RRset (an RRset that doesn't have RDATA)
  222. /// whose name is the closest encloser of the given name.
  223. /// - If there is a matching name but no RRset of the search type, it
  224. /// returns the code of \c NXRRSET, and, if DNSSEC is required,
  225. /// the NSEC RRset for that name if the zone is signed with NSEC;
  226. /// if it's signed with NSEC3, an empty NSEC3 RRset whose name is the
  227. /// given name.
  228. /// - If there is no matching name but there is a matching wild card name,
  229. /// but it doesn't have a requested type of RR, and if DNSSEC is
  230. /// required, then it returns the code of \c WILDCARD_NXRRSET.
  231. /// If the zone is signed with NSEC, it returns corresponding NSEC
  232. /// (see the description of \c Result); if it's signed with NSEC3,
  233. /// it returns an empty NSEC3 RRset whose name is the matching wildcard.
  234. /// - If there is a CNAME RR of the searched name but there is no
  235. /// RR of the searched type of the name (so this type is different from
  236. /// CNAME), it returns the code of \c CNAME and that CNAME RR.
  237. /// Note that if the searched RR type is CNAME, it is considered
  238. /// a successful match, and the code of \c SUCCESS will be returned.
  239. /// - If the search name matches a delegation point of DNAME, it returns
  240. /// the code of \c DNAME and that DNAME RR.
  241. ///
  242. /// The \c options parameter specifies customized behavior of the search.
  243. /// Their semantics is as follows (they are or bit-field):
  244. ///
  245. /// - \c FIND_GLUE_OK Allow search under a zone cut. By default the search
  246. /// will stop once it encounters a zone cut. If this option is specified
  247. /// it remembers information about the highest zone cut and continues
  248. /// the search until it finds an exact match for the given name or it
  249. /// detects there is no exact match. If an exact match is found,
  250. /// RRsets for that name are searched just like the normal case;
  251. /// otherwise, if the search has encountered a zone cut, \c DELEGATION
  252. /// with the information of the highest zone cut will be returned.
  253. /// - \c FIND_DNSSEC Request that DNSSEC data (like NSEC, RRSIGs) are
  254. /// returned with the answer. It is allowed for the data source to
  255. /// include them even when not requested.
  256. /// - \c NO_WILDCARD Do not try wildcard matching. This option is of no
  257. /// use for normal lookups; it's intended to be used to get a DNSSEC
  258. /// proof of the non existence of any matching wildcard or non existence
  259. /// of an exact match when a wildcard match is found.
  260. ///
  261. /// In general, \c name is expected to be included in the zone, that is,
  262. /// it should be equal to or a subdomain of the zone origin. Otherwise
  263. /// this method will return \c NXDOMAIN with an empty RRset. But such a
  264. /// case should rather be considered a caller's bug.
  265. ///
  266. /// \note For this reason it's probably better to throw an exception
  267. /// than returning \c NXDOMAIN. This point should be revisited in a near
  268. /// future version. In any case applications shouldn't call this method
  269. /// for an out-of-zone name.
  270. ///
  271. /// \exception std::bad_alloc Memory allocation such as for constructing
  272. /// the resulting RRset fails
  273. /// \exception DataSourceError Derived class specific exception, e.g.
  274. /// when encountering a bad zone configuration or database connection
  275. /// failure. Although these are considered rare, exceptional events,
  276. /// it can happen under relatively usual conditions (unlike memory
  277. /// allocation failure). So, in general, the application is expected
  278. /// to catch this exception, either specifically or as a result of
  279. /// catching a base exception class, and handle it gracefully.
  280. ///
  281. /// \param name The domain name to be searched for.
  282. /// \param type The RR type to be searched for.
  283. /// \param options The search options.
  284. /// \return A \c FindResult object enclosing the search result (see above).
  285. virtual FindResult find(const isc::dns::Name& name,
  286. const isc::dns::RRType& type,
  287. const FindOptions options
  288. = FIND_DEFAULT) = 0;
  289. ///
  290. /// \brief Finds all RRsets in the given name.
  291. ///
  292. /// This function works almost exactly in the same way as the find one. The
  293. /// only difference is, when the lookup is successful (eg. the code is
  294. /// SUCCESS or WILDCARD), all the RRsets residing in the named node are
  295. /// copied into the \c target parameter and the rrset member of the result
  296. /// is NULL. All the other (unsuccessful) cases are handled the same,
  297. /// including returning delegations, NSEC/NSEC3 proofs, etc. The options
  298. /// parameter works the same way and it should conform to the same exception
  299. /// restrictions.
  300. ///
  301. /// \param name \see find, parameter name
  302. /// \param target the successfull result is returned through this
  303. /// \param options \see find, parameter options
  304. /// \return \see find and it's result
  305. virtual FindResult findAll(const isc::dns::Name& name,
  306. std::vector<isc::dns::ConstRRsetPtr> &target,
  307. const FindOptions options = FIND_DEFAULT) = 0;
  308. /// A helper structure to represent the search result of \c findNSEC3().
  309. ///
  310. /// The idea is similar to that of \c FindResult, but \c findNSEC3() has
  311. /// special interface and semantics, we use a different structure to
  312. /// represent the result.
  313. struct FindNSEC3Result {
  314. FindNSEC3Result(bool param_matched,
  315. isc::dns::ConstRRsetPtr param_closest_proof,
  316. isc::dns::ConstRRsetPtr param_next_proof) :
  317. matched(param_matched), closest_proof(param_closest_proof),
  318. next_proof(param_next_proof)
  319. {}
  320. /// true iff closest_proof is a matching NSEC3
  321. const bool matched;
  322. /// Either the NSEC3 for the closest provable encloser of the given
  323. /// name or NSEC3 that covers the name
  324. const isc::dns::ConstRRsetPtr closest_proof;
  325. /// When non NULL, NSEC3 for the next closer name.
  326. const isc::dns::ConstRRsetPtr next_proof;
  327. };
  328. /// Search the zone for the NSEC3 RR(s) that prove existence or non
  329. /// existence of a give name.
  330. ///
  331. /// It searches the NSEC3 namespace of the zone (how that namespace is
  332. /// implemented can vary in specific data source implementation) for NSEC3
  333. /// RRs that match or cover the NSEC3 hash value for the given name.
  334. ///
  335. /// If \c recursive is false, it will first look for the NSEC3 that has
  336. /// a matching hash. If it doesn't exist, it identifies the covering NSEC3
  337. /// for the hash. In either case the search stops at that point and the
  338. /// found NSEC3 RR(set) will be returned in the closest_proof member of
  339. /// \c FindNSEC3Result. \c matched is true or false depending on
  340. /// the found NSEC3 is a matched one or covering one. \c next_proof
  341. /// is always NULL.
  342. ///
  343. /// If \c recursive is true, it will continue the search toward the zone
  344. /// apex (origin name) until it finds a provable encloser, that is,
  345. /// an ancestor of \c name that has a matching NSEC3. This is the closest
  346. /// provable encloser of \c name as defined in RFC5155. In this case,
  347. /// if the found encloser is not equal to \c name, the search should
  348. /// have seen a covering NSEC3 for the immediate child of the found
  349. /// encloser. That child name is the next closer name as defined in
  350. /// RFC5155. In this case, this method returns the NSEC3 for the
  351. /// closest encloser in \c closest_proof, and the NSEC3 for the next
  352. /// closer name in \c next_proof of \c FindNSEC3Result. This set of
  353. /// NSEC3 RRs provide the closest encloser proof as defined in RFC5155.
  354. /// If, on the other hand, the found closest name is equal to \c name,
  355. /// this method simply returns it in \c closest_proof. \c next_proof
  356. /// is set to NULL. In all cases \c matched is set to true.
  357. ///
  358. /// When looking for NSEC3, this method retrieves NSEC3 parameters from
  359. /// the corresponding zone to calculate hash values. Actual implementation
  360. /// of how to do this will defer in different data sources. If the
  361. /// NSEC3 parameters are not available \c DataSourceError exception
  362. /// will be thrown.
  363. ///
  364. /// \note This implicitly means this method assumes the zone does not
  365. /// have more than one set of parameters. This assumption should be
  366. /// reasonable in actual deployment and will help simplify the interface
  367. /// and implementation. But if there's a real need for supporting
  368. /// multiple sets of parameters in a single zone, we will have to
  369. /// extend this method so that, e.g., the caller can specify the parameter
  370. /// set.
  371. ///
  372. /// This method takes an optional parameter \c known_encloser. If it's
  373. /// non NULL, its owner name must be the closest encloser of \c name.
  374. /// Its RR type is expected to be NSEC3, but other attributes other than
  375. /// the owner name is not important for this method and will generally be
  376. /// ignored. When this parameter is provided, the actual implementation
  377. /// of the derived class can use it as a hint for identifying the closest
  378. /// provable encloser (it can be helpful if \c name is known to be non
  379. /// existent and possibly contains many labels below the closest encloser).
  380. /// The underlying data source may also specialize the RRset to hold
  381. /// some information specific to the data source implementation to allow
  382. /// further optimization. Whether or not this parameter is non NULL,
  383. /// the result of this method should be the same; this parameter is only
  384. /// provided to possibly enable some implementation specific optimization.
  385. /// When it's non NULL, however, its owner name must be equal to \c name
  386. /// when \c recursive is false and must be a real (non equal) super domain
  387. /// of \c name when \c recursive is true; otherwise
  388. /// \c isc::InvalidParameter exception will be thrown.
  389. ///
  390. /// In general, this method expects the zone is properly signed with NSEC3
  391. /// RRs. Specifically, it assumes at least the apex node has a matching
  392. /// NSEC3 RR (so the search in the recursive mode must always succeed);
  393. /// it also assumes that it can retrieve NSEC parameters (iterations,
  394. /// algorithm, and salt) from the zone as noted above. If these
  395. /// assumptions aren't met, \c DataSourceError exception will be thrown.
  396. ///
  397. /// \exception InvalidParameter name is not a subdomain of the zone origin;
  398. /// known_encloser does not meet the requirement (see above)
  399. /// \exception DataSourceError Low-level or internal datasource errors
  400. /// happened, or the zone isn't properly signed with NSEC3
  401. /// (NSEC3 parameters cannot be found, no NSEC3s are available, etc).
  402. /// \exception std::bad_alloc The underlying implementation involves
  403. /// memory allocation and it fails
  404. ///
  405. /// \param name The name for which NSEC3 RRs are to be found. It must
  406. /// be a subdomain of the zone.
  407. /// \param recursive Whether or not search should continue until it finds
  408. /// a provable encloser (see above).
  409. /// \param known_encloser If non NULL, specifies the closest encloser
  410. /// (may or may not be provable) of \c name via its owner name.
  411. ///
  412. /// \return The search result and whether or not the closest_proof is
  413. /// a matching NSEC3, in the form of \c FindNSEC3Result object.
  414. virtual FindNSEC3Result
  415. findNSEC3(const isc::dns::Name& name, bool recursive,
  416. const isc::dns::ConstRRsetPtr known_encloser =
  417. isc::dns::ConstRRsetPtr()) = 0;
  418. /// \brief Get previous name in the zone
  419. ///
  420. /// Gets the previous name in the DNSSEC order. This can be used
  421. /// to find the correct NSEC records for proving nonexistence
  422. /// of domains.
  423. ///
  424. /// The concrete implementation might throw anything it thinks appropriate,
  425. /// however it is recommended to stick to the ones listed here. The user
  426. /// of this method should be able to handle any exceptions.
  427. ///
  428. /// This method does not include under-zone-cut data (glue data).
  429. ///
  430. /// \param query The name for which one we look for a previous one. The
  431. /// queried name doesn't have to exist in the zone.
  432. /// \return The preceding name
  433. ///
  434. /// \throw NotImplemented in case the data source backend doesn't support
  435. /// DNSSEC or there is no previous in the zone (NSEC records might be
  436. /// missing in the DB, the queried name is less or equal to the apex).
  437. /// \throw DataSourceError for low-level or internal datasource errors
  438. /// (like broken connection to database, wrong data living there).
  439. /// \throw std::bad_alloc For allocation errors.
  440. virtual isc::dns::Name findPreviousName(const isc::dns::Name& query)
  441. const = 0;
  442. //@}
  443. };
  444. /// \brief Operator to combine FindOptions
  445. ///
  446. /// We would need to manually static-cast the options if we put or
  447. /// between them, which is undesired with bit-flag options. Therefore
  448. /// we hide the cast here, which is the simplest solution and it still
  449. /// provides reasonable level of type safety.
  450. inline ZoneFinder::FindOptions operator |(ZoneFinder::FindOptions a,
  451. ZoneFinder::FindOptions b)
  452. {
  453. return (static_cast<ZoneFinder::FindOptions>(static_cast<unsigned>(a) |
  454. static_cast<unsigned>(b)));
  455. }
  456. /// \brief A pointer-like type pointing to a \c ZoneFinder object.
  457. typedef boost::shared_ptr<ZoneFinder> ZoneFinderPtr;
  458. /// \brief A pointer-like type pointing to a \c ZoneFinder object.
  459. typedef boost::shared_ptr<const ZoneFinder> ConstZoneFinderPtr;
  460. /// The base class to make updates to a single zone.
  461. ///
  462. /// On construction, each derived class object will start a "transaction"
  463. /// for making updates to a specific zone (this means a constructor of
  464. /// a derived class would normally take parameters to identify the zone
  465. /// to be updated). The underlying realization of a "transaction" will differ
  466. /// for different derived classes; if it uses a general purpose database
  467. /// as a backend, it will involve performing some form of "begin transaction"
  468. /// statement for the database.
  469. ///
  470. /// Updates (adding or deleting RRs) are made via \c addRRset() and
  471. /// \c deleteRRset() methods. Until the \c commit() method is called the
  472. /// changes are local to the updater object. For example, they won't be
  473. /// visible via a \c ZoneFinder object except the one returned by the
  474. /// updater's own \c getFinder() method. The \c commit() completes the
  475. /// transaction and makes the changes visible to others.
  476. ///
  477. /// This class does not provide an explicit "rollback" interface. If
  478. /// something wrong or unexpected happens during the updates and the
  479. /// caller wants to cancel the intermediate updates, the caller should
  480. /// simply destruct the updater object without calling \c commit().
  481. /// The destructor is supposed to perform the "rollback" operation,
  482. /// depending on the internal details of the derived class.
  483. ///
  484. /// \note This initial implementation provides a quite simple interface of
  485. /// adding and deleting RRs (see the description of the related methods).
  486. /// It may be revisited as we gain more experiences.
  487. class ZoneUpdater {
  488. protected:
  489. /// The default constructor.
  490. ///
  491. /// This is intentionally defined as protected to ensure that this base
  492. /// class is never instantiated directly.
  493. ZoneUpdater() {}
  494. public:
  495. /// The destructor
  496. ///
  497. /// Each derived class implementation must ensure that if \c commit()
  498. /// has not been performed by the time of the call to it, then it
  499. /// "rollbacks" the updates made via the updater so far.
  500. virtual ~ZoneUpdater() {}
  501. /// Return a finder for the zone being updated.
  502. ///
  503. /// The returned finder provides the functionalities of \c ZoneFinder
  504. /// for the zone as updates are made via the updater. That is, before
  505. /// making any update, the finder will be able to find all RRsets that
  506. /// exist in the zone at the time the updater is created. If RRsets
  507. /// are added or deleted via \c addRRset() or \c deleteRRset(),
  508. /// this finder will find the added ones or miss the deleted ones
  509. /// respectively.
  510. ///
  511. /// The finder returned by this method is effective only while the updates
  512. /// are performed, i.e., from the construction of the corresponding
  513. /// updater until \c commit() is performed or the updater is destructed
  514. /// without commit. The result of a subsequent call to this method (or
  515. /// the use of the result) after that is undefined.
  516. ///
  517. /// \return A reference to a \c ZoneFinder for the updated zone
  518. virtual ZoneFinder& getFinder() = 0;
  519. /// Add an RRset to a zone via the updater
  520. ///
  521. /// This may be revisited in a future version, but right now the intended
  522. /// behavior of this method is simple: It "naively" adds the specified
  523. /// RRset to the zone specified on creation of the updater.
  524. /// It performs minimum level of validation on the specified RRset:
  525. /// - Whether the RR class is identical to that for the zone to be updated
  526. /// - Whether the RRset is not empty, i.e., it has at least one RDATA
  527. /// - Whether the RRset is not associated with an RRSIG, i.e.,
  528. /// whether \c getRRsig() on the RRset returns a NULL pointer.
  529. ///
  530. /// and otherwise does not check any oddity. For example, it doesn't
  531. /// check whether the owner name of the specified RRset is a subdomain
  532. /// of the zone's origin; it doesn't care whether or not there is already
  533. /// an RRset of the same name and RR type in the zone, and if there is,
  534. /// whether any of the existing RRs have duplicate RDATA with the added
  535. /// ones. If these conditions matter the calling application must examine
  536. /// the existing data beforehand using the \c ZoneFinder returned by
  537. /// \c getFinder().
  538. ///
  539. /// The validation requirement on the associated RRSIG is temporary.
  540. /// If we find it more reasonable and useful to allow adding a pair of
  541. /// RRset and its RRSIG RRset as we gain experiences with the interface,
  542. /// we may remove this restriction. Until then we explicitly check it
  543. /// to prevent accidental misuse.
  544. ///
  545. /// Conceptually, on successful call to this method, the zone will have
  546. /// the specified RRset, and if there is already an RRset of the same
  547. /// name and RR type, these two sets will be "merged". "Merged" means
  548. /// that a subsequent call to \c ZoneFinder::find() for the name and type
  549. /// will result in success and the returned RRset will contain all
  550. /// previously existing and newly added RDATAs with the TTL being the
  551. /// minimum of the two RRsets. The underlying representation of the
  552. /// "merged" RRsets may vary depending on the characteristic of the
  553. /// underlying data source. For example, if it uses a general purpose
  554. /// database that stores each RR of the same RRset separately, it may
  555. /// simply be a larger sets of RRs based on both the existing and added
  556. /// RRsets; the TTLs of the RRs may be different within the database, and
  557. /// there may even be duplicate RRs in different database rows. As long
  558. /// as the RRset returned via \c ZoneFinder::find() conforms to the
  559. /// concept of "merge", the actual internal representation is up to the
  560. /// implementation.
  561. ///
  562. /// This method must not be called once commit() is performed. If it
  563. /// calls after \c commit() the implementation must throw a
  564. /// \c DataSourceError exception.
  565. ///
  566. /// If journaling was requested when getting this updater, it will reject
  567. /// to add the RRset if the squence doesn't look like and IXFR (see
  568. /// DataSourceClient::getUpdater). In such case isc::BadValue is thrown.
  569. ///
  570. /// \todo As noted above we may have to revisit the design details as we
  571. /// gain experiences:
  572. ///
  573. /// - we may want to check (and maybe reject) if there is already a
  574. /// duplicate RR (that has the same RDATA).
  575. /// - we may want to check (and maybe reject) if there is already an
  576. /// RRset of the same name and RR type with different TTL
  577. /// - we may even want to check if there is already any RRset of the
  578. /// same name and RR type.
  579. /// - we may want to add an "options" parameter that can control the
  580. /// above points
  581. /// - we may want to have this method return a value containing the
  582. /// information on whether there's a duplicate, etc.
  583. ///
  584. /// \exception DataSourceError Called after \c commit(), RRset is invalid
  585. /// (see above), internal data source error
  586. /// \exception isc::BadValue Journaling is enabled and the current RRset
  587. /// doesn't fit into the IXFR sequence (see above).
  588. /// \exception std::bad_alloc Resource allocation failure
  589. ///
  590. /// \param rrset The RRset to be added
  591. virtual void addRRset(const isc::dns::RRset& rrset) = 0;
  592. /// Delete an RRset from a zone via the updater
  593. ///
  594. /// Like \c addRRset(), the detailed semantics and behavior of this method
  595. /// may have to be revisited in a future version. The following are
  596. /// based on the initial implementation decisions.
  597. ///
  598. /// On successful completion of this method, it will remove from the zone
  599. /// the RRs of the specified owner name and RR type that match one of
  600. /// the RDATAs of the specified RRset. There are several points to be
  601. /// noted:
  602. /// - Existing RRs that don't match any of the specified RDATAs will
  603. /// remain in the zone.
  604. /// - Any RRs of the specified RRset that doesn't exist in the zone will
  605. /// simply be ignored; the implementation of this method is not supposed
  606. /// to check that condition.
  607. /// - The TTL of the RRset is ignored; matching is only performed by
  608. /// the owner name, RR type and RDATA
  609. ///
  610. /// Ignoring the TTL may not look sensible, but it's based on the
  611. /// observation that it will result in more intuitive result, especially
  612. /// when the underlying data source is a general purpose database.
  613. /// See also \c DatabaseAccessor::deleteRecordInZone() on this point.
  614. /// It also matches the dynamic update protocol (RFC2136), where TTLs
  615. /// are ignored when deleting RRs.
  616. ///
  617. /// \note Since the TTL is ignored, this method could take the RRset
  618. /// to be deleted as a tuple of name, RR type, and a list of RDATAs.
  619. /// But in practice, it's quite likely that the caller has the RRset
  620. /// in the form of the \c RRset object (e.g., extracted from a dynamic
  621. /// update request message), so this interface would rather be more
  622. /// convenient. If it turns out not to be true we can change or extend
  623. /// the method signature.
  624. ///
  625. /// This method performs minimum level of validation on the specified
  626. /// RRset:
  627. /// - Whether the RR class is identical to that for the zone to be updated
  628. /// - Whether the RRset is not empty, i.e., it has at least one RDATA
  629. /// - Whether the RRset is not associated with an RRSIG, i.e.,
  630. /// whether \c getRRsig() on the RRset returns a NULL pointer.
  631. ///
  632. /// This method must not be called once commit() is performed. If it
  633. /// calls after \c commit() the implementation must throw a
  634. /// \c DataSourceError exception.
  635. ///
  636. /// If journaling was requested when getting this updater, it will reject
  637. /// to add the RRset if the squence doesn't look like and IXFR (see
  638. /// DataSourceClient::getUpdater). In such case isc::BadValue is thrown.
  639. ///
  640. /// \todo As noted above we may have to revisit the design details as we
  641. /// gain experiences:
  642. ///
  643. /// - we may want to check (and maybe reject) if some or all of the RRs
  644. /// for the specified RRset don't exist in the zone
  645. /// - we may want to allow an option to "delete everything" for specified
  646. /// name and/or specified name + RR type.
  647. /// - as mentioned above, we may want to include the TTL in matching the
  648. /// deleted RRs
  649. /// - we may want to add an "options" parameter that can control the
  650. /// above points
  651. /// - we may want to have this method return a value containing the
  652. /// information on whether there's any RRs that are specified but don't
  653. /// exit, the number of actually deleted RRs, etc.
  654. ///
  655. /// \exception DataSourceError Called after \c commit(), RRset is invalid
  656. /// (see above), internal data source error
  657. /// \exception isc::BadValue Journaling is enabled and the current RRset
  658. /// doesn't fit into the IXFR sequence (see above).
  659. /// \exception std::bad_alloc Resource allocation failure
  660. ///
  661. /// \param rrset The RRset to be deleted
  662. virtual void deleteRRset(const isc::dns::RRset& rrset) = 0;
  663. /// Commit the updates made in the updater to the zone
  664. ///
  665. /// This method completes the "transaction" started at the creation
  666. /// of the updater. After successful completion of this method, the
  667. /// updates will be visible outside the scope of the updater.
  668. /// The actual internal behavior will defer for different derived classes.
  669. /// For a derived class with a general purpose database as a backend,
  670. /// for example, this method would perform a "commit" statement for the
  671. /// database.
  672. ///
  673. /// This operation can only be performed at most once. A duplicate call
  674. /// must result in a DatasourceError exception.
  675. ///
  676. /// \exception DataSourceError Duplicate call of the method,
  677. /// internal data source error
  678. /// \exception isc::BadValue Journaling is enabled and the update is not
  679. /// complete IXFR sequence.
  680. virtual void commit() = 0;
  681. };
  682. /// \brief A pointer-like type pointing to a \c ZoneUpdater object.
  683. typedef boost::shared_ptr<ZoneUpdater> ZoneUpdaterPtr;
  684. /// The base class for retrieving differences between two versions of a zone.
  685. ///
  686. /// On construction, each derived class object will internally set up
  687. /// retrieving sequences of differences between two specific version of
  688. /// a specific zone managed in a particular data source. So the constructor
  689. /// of a derived class would normally take parameters to identify the zone
  690. /// and the two versions for which the differences should be retrieved.
  691. /// See \c DataSourceClient::getJournalReader for more concrete details
  692. /// used in this API.
  693. ///
  694. /// Once constructed, an object of this class will act like an iterator
  695. /// over the sequences. Every time the \c getNextDiff() method is called
  696. /// it returns one element of the differences in the form of an \c RRset
  697. /// until it reaches the end of the entire sequences.
  698. class ZoneJournalReader {
  699. public:
  700. /// Result codes used by a factory method for \c ZoneJournalReader
  701. enum Result {
  702. SUCCESS, ///< A \c ZoneJournalReader object successfully created
  703. NO_SUCH_ZONE, ///< Specified zone does not exist in the data source
  704. NO_SUCH_VERSION ///< Specified versions do not exist in the diff storage
  705. };
  706. protected:
  707. /// The default constructor.
  708. ///
  709. /// This is intentionally defined as protected to ensure that this base
  710. /// class is never instantiated directly.
  711. ZoneJournalReader() {}
  712. public:
  713. /// The destructor
  714. virtual ~ZoneJournalReader() {}
  715. /// Return the next difference RR of difference sequences.
  716. ///
  717. /// In this API, the difference between two versions of a zone is
  718. /// conceptually represented as IXFR-style difference sequences:
  719. /// Each difference sequence is a sequence of RRs: an older version of
  720. /// SOA (to be deleted), zero or more other deleted RRs, the
  721. /// post-transaction SOA (to be added), and zero or more other
  722. /// added RRs. (Note, however, that the underlying data source
  723. /// implementation may or may not represent the difference in
  724. /// straightforward realization of this concept. The mapping between
  725. /// the conceptual difference and the actual implementation is hidden
  726. /// in each derived class).
  727. ///
  728. /// This method provides an application with a higher level interface
  729. /// to retrieve the difference along with the conceptual model: the
  730. /// \c ZoneJournalReader object iterates over the entire sequences
  731. /// from the beginning SOA (which is to be deleted) to one of the
  732. /// added RR of with the ending SOA, and each call to this method returns
  733. /// one RR in the form of an \c RRset that contains exactly one RDATA
  734. /// in the order of the sequences.
  735. ///
  736. /// Note that the ordering of the sequences specifies the semantics of
  737. /// each difference: add or delete. For example, the first RR is to
  738. /// be deleted, and the last RR is to be added. So the return value
  739. /// of this method does not explicitly indicate whether the RR is to be
  740. /// added or deleted.
  741. ///
  742. /// This method ensures the returned \c RRset represents an RR, that is,
  743. /// it contains exactly one RDATA. However, it does not necessarily
  744. /// ensure that the resulting sequences are in the form of IXFR-style.
  745. /// For example, the first RR is supposed to be an SOA, and it should
  746. /// normally be the case, but this interface does not necessarily require
  747. /// the derived class implementation ensure this. Normally the
  748. /// differences are expected to be stored using this API (via a
  749. /// \c ZoneUpdater object), and as long as that is the case and the
  750. /// underlying implementation follows the requirement of the API, the
  751. /// result of this method should be a valid IXFR-style sequences.
  752. /// So this API does not mandate the almost redundant check as part of
  753. /// the interface. If the application needs to make it sure 100%, it
  754. /// must check the resulting sequence itself.
  755. ///
  756. /// Once the object reaches the end of the sequences, this method returns
  757. /// \c Null. Any subsequent call will result in an exception of
  758. /// class \c InvalidOperation.
  759. ///
  760. /// \exception InvalidOperation The method is called beyond the end of
  761. /// the difference sequences.
  762. /// \exception DataSourceError Underlying data is broken and the RR
  763. /// cannot be created or other low level data source error.
  764. ///
  765. /// \return An \c RRset that contains one RDATA corresponding to the
  766. /// next difference in the sequences.
  767. virtual isc::dns::ConstRRsetPtr getNextDiff() = 0;
  768. };
  769. /// \brief A pointer-like type pointing to a \c ZoneUpdater object.
  770. typedef boost::shared_ptr<ZoneJournalReader> ZoneJournalReaderPtr;
  771. } // end of datasrc
  772. } // end of isc
  773. #endif // __ZONE_H
  774. // Local Variables:
  775. // mode: c++
  776. // End: