zone_finder.h 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  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 DATASRC_ZONE_FINDER_H
  15. #define DATASRC_ZONE_FINDER_H 1
  16. #include <dns/name.h>
  17. #include <dns/rrset.h>
  18. #include <dns/rrtype.h>
  19. #include <datasrc/exceptions.h>
  20. #include <datasrc/result.h>
  21. #include <datasrc/rrset_collection_base.h>
  22. #include <utility>
  23. #include <vector>
  24. namespace isc {
  25. namespace datasrc {
  26. /// \brief Out of zone exception
  27. ///
  28. /// This is thrown when a method is called for a name or RRset which
  29. /// is not in or below the zone.
  30. class OutOfZone : public ZoneException {
  31. public:
  32. OutOfZone(const char* file, size_t line, const char* what) :
  33. ZoneException(file, line, what) {}
  34. };
  35. /// \brief The base class to search a zone for RRsets
  36. ///
  37. /// The \c ZoneFinder class is an abstract base class for representing
  38. /// an object that performs DNS lookups in a specific zone accessible via
  39. /// a data source. In general, different types of data sources (in-memory,
  40. /// database-based, etc) define their own derived classes of \c ZoneFinder,
  41. /// implementing ways to retrieve the required data through the common
  42. /// interfaces declared in the base class. Each concrete \c ZoneFinder
  43. /// object is therefore (conceptually) associated with a specific zone
  44. /// of one specific data source instance.
  45. ///
  46. /// The origin name and the RR class of the associated zone are available
  47. /// via the \c getOrigin() and \c getClass() methods, respectively.
  48. ///
  49. /// The most important method of this class is \c find(), which performs
  50. /// the lookup for a given domain and type. See the description of the
  51. /// method for details.
  52. ///
  53. /// \note It's not clear whether we should request that a zone finder form a
  54. /// "transaction", that is, whether to ensure the finder is not susceptible
  55. /// to changes made by someone else than the creator of the finder. If we
  56. /// don't request that, for example, two different lookup results for the
  57. /// same name and type can be different if other threads or programs make
  58. /// updates to the zone between the lookups. We should revisit this point
  59. /// as we gain more experiences.
  60. class ZoneFinder {
  61. public:
  62. /// Result codes of the \c find() method.
  63. ///
  64. /// Note: the codes are tentative. We may need more, or we may find
  65. /// some of them unnecessary as we implement more details.
  66. ///
  67. /// See the description of \c find() for further details of how
  68. /// these results should be interpreted.
  69. enum Result {
  70. SUCCESS, ///< An exact match is found.
  71. DELEGATION, ///< The search encounters a zone cut.
  72. NXDOMAIN, ///< There is no domain name that matches the search name
  73. NXRRSET, ///< There is a matching name but no RRset of the search type
  74. CNAME, ///< The search encounters and returns a CNAME RR
  75. DNAME ///< The search encounters and returns a DNAME RR
  76. };
  77. /// Special attribute flags on the result of the \c find() method
  78. ///
  79. /// The flag values defined here are intended to signal to the caller
  80. /// that it may need special handling on the result. This is particularly
  81. /// of concern when DNSSEC is requested. For example, for negative
  82. /// responses the caller would want to know whether the zone is signed
  83. /// with NSEC or NSEC3 so that it can subsequently provide necessary
  84. /// proof of the result.
  85. ///
  86. /// The caller is generally expected to get access to the information
  87. /// via read-only getter methods of \c FindContext so that it won't rely
  88. /// on specific details of the representation of the flags. So these
  89. /// definitions are basically only meaningful for data source
  90. /// implementations.
  91. enum FindResultFlags {
  92. RESULT_DEFAULT = 0, ///< The default flags
  93. RESULT_WILDCARD = 1, ///< find() resulted in a wildcard match
  94. RESULT_NSEC_SIGNED = 2, ///< The zone is signed with NSEC RRs
  95. RESULT_NSEC3_SIGNED = 4 ///< The zone is signed with NSEC3 RRs
  96. };
  97. /// Find options.
  98. ///
  99. /// The option values are used as a parameter for \c find().
  100. /// These are values of a bitmask type. Bitwise operations can be
  101. /// performed on these values to express compound options.
  102. enum FindOptions {
  103. FIND_DEFAULT = 0, ///< The default options
  104. FIND_GLUE_OK = 1, ///< Allow search under a zone cut
  105. FIND_DNSSEC = 2, ///< Require DNSSEC data in the answer
  106. ///< (RRSIG, NSEC, etc.). The implementation
  107. ///< is allowed to include it even if it is
  108. ///< not set.
  109. NO_WILDCARD = 4 ///< Do not try wildcard matching.
  110. };
  111. protected:
  112. /// \brief A convenient tuple representing a set of find() results.
  113. ///
  114. /// This helper structure is specifically expected to be used as an input
  115. /// for the construct of the \c Context class object used by derived
  116. /// ZoneFinder implementations. This is therefore defined as protected.
  117. struct ResultContext {
  118. ResultContext(Result code_param,
  119. isc::dns::ConstRRsetPtr rrset_param,
  120. FindResultFlags flags_param = RESULT_DEFAULT) :
  121. code(code_param), rrset(rrset_param), flags(flags_param)
  122. {}
  123. const Result code;
  124. const isc::dns::ConstRRsetPtr rrset;
  125. const FindResultFlags flags;
  126. };
  127. public:
  128. /// \brief A helper function to strip RRSIGs when FIND_DNSSEC is not
  129. /// requested.
  130. static isc::dns::ConstRRsetPtr
  131. stripRRsigs(isc::dns::ConstRRsetPtr rp, const FindOptions options);
  132. /// \brief Context of the result of a find() call.
  133. ///
  134. /// This class encapsulates results and (possibly) associated context
  135. /// of a call to the \c find() method. The public member variables of
  136. /// this class represent the result of the call. They are a
  137. /// straightforward tuple of the result code and a pointer (and
  138. /// optionally special flags) to the found RRset.
  139. ///
  140. /// These member variables will be initialized on construction and never
  141. /// change, so for convenience we allow the applications to refer to some
  142. /// of the members directly. For some others we provide read-only accessor
  143. /// methods to hide specific representation.
  144. ///
  145. /// Another role of this class is to provide the interface to some common
  146. /// processing logic that may be necessary using the result of \c find().
  147. /// Specifically, it's expected to be used in the context of DNS query
  148. /// handling, where the caller would need to look into the data source
  149. /// again based on the \c find() result. For example, it would need to
  150. /// get A and/or AAAA records for some of the answer or authority RRs.
  151. ///
  152. /// This class defines (a set of) method(s) that can be commonly used
  153. /// for such purposes for any type of data source (as long as it conforms
  154. /// to the public \c find() interface). In some cases, a specific data
  155. /// source implementation may want to (and can) optimize the processing
  156. /// exploiting its internal data structure and the knowledge of the context
  157. /// of the precedent \c find() call. Such a data source implementation
  158. /// can define a derived class of the base Context and override the
  159. /// specific virtual method.
  160. ///
  161. /// This base class defines these common protected methods along with
  162. /// some helper pure virtual methods that would be necessary for the
  163. /// common methods. If a derived class wants to use the common version
  164. /// of the protected method, it needs to provide expected result through
  165. /// their implementation of the pure virtual methods.
  166. ///
  167. /// This class object is generally expected to be associated with the
  168. /// ZoneFinder that originally performed the \c find() call, and expects
  169. /// the finder is valid throughout the lifetime of this object. It's
  170. /// caller's responsibility to ensure that assumption.
  171. class Context {
  172. public:
  173. /// \brief The constructor.
  174. ///
  175. /// \param options The find options specified for the find() call.
  176. /// \param result The result of the find() call.
  177. Context(FindOptions options, const ResultContext& result) :
  178. code(result.code), rrset(result.rrset),
  179. flags_(result.flags), options_(options)
  180. {}
  181. /// \brief The destructor.
  182. virtual ~Context() {}
  183. const Result code;
  184. const isc::dns::ConstRRsetPtr rrset;
  185. /// Return true iff find() results in a wildcard match.
  186. bool isWildcard() const { return ((flags_ & RESULT_WILDCARD) != 0); }
  187. /// Return true when the underlying zone is signed with NSEC.
  188. ///
  189. /// The \c find() implementation allows this to return false if
  190. /// \c FIND_DNSSEC isn't specified regardless of whether the zone
  191. /// is signed or which of NSEC/NSEC3 is used.
  192. ///
  193. /// When this is returned, the implementation of find() must ensure
  194. /// that \c rrset be a valid NSEC RRset as described in \c find()
  195. /// documentation.
  196. bool isNSECSigned() const {
  197. return ((flags_ & RESULT_NSEC_SIGNED) != 0);
  198. }
  199. /// Return true when the underlying zone is signed with NSEC3.
  200. ///
  201. /// The \c find() implementation allows this to return false if
  202. /// \c FIND_DNSSEC isn't specified regardless of whether the zone
  203. /// is signed or which of NSEC/NSEC3 is used.
  204. bool isNSEC3Signed() const {
  205. return ((flags_ & RESULT_NSEC3_SIGNED) != 0);
  206. }
  207. /// \brief Find and return additional RRsets corresponding to the
  208. /// result of \c find().
  209. ///
  210. /// If this context is based on a normal find() call that resulted
  211. /// in SUCCESS or DELEGATION, it examines the returned RRset (in many
  212. /// cases NS, sometimes MX or others), searches the data source for
  213. /// specified type of additional RRs for each RDATA of the RRset
  214. /// (e.g., A or AAAA for the name server addresses), and stores the
  215. /// result in the given vector. The vector may not be empty; this
  216. /// method appends any found RRsets to it, without touching existing
  217. /// elements.
  218. ///
  219. /// If this context is based on a findAll() call that resulted in
  220. /// SUCCESS, it performs the same process for each RRset returned in
  221. /// the \c findAll() call.
  222. ///
  223. /// The caller specifies desired RR types of the additional RRsets
  224. /// in \c requested_types. Normally it consists of A and/or AAAA
  225. /// types, but other types can be specified.
  226. ///
  227. /// This method is meaningful only when the precedent find()/findAll()
  228. /// call resulted in SUCCESS or DELEGATION. Otherwise this method
  229. /// does nothing.
  230. ///
  231. /// \note The additional RRsets returned via method are limited to
  232. /// ones contained in the zone which the corresponding find/findAll
  233. /// call searched (possibly including glues under a zone cut where
  234. /// they are applicable). If the caller needs to get out-of-zone
  235. /// additional RRsets, it needs to explicitly finds them by
  236. /// identifying the corresponding zone and calls \c find() for it.
  237. ///
  238. /// \param requested_types A vector of RR types for desired additional
  239. /// RRsets.
  240. /// \param result A vector to which any found additional RRsets are
  241. /// to be inserted.
  242. void getAdditional(
  243. const std::vector<isc::dns::RRType>& requested_types,
  244. std::vector<isc::dns::ConstRRsetPtr>& result)
  245. {
  246. // Perform common checks, and delegate the process to the default
  247. // or specialized implementation.
  248. if (code != SUCCESS && code != DELEGATION) {
  249. return;
  250. }
  251. getAdditionalImpl(requested_types, result);
  252. }
  253. protected:
  254. /// \brief Return the \c ZoneFinder that created this \c Context.
  255. ///
  256. /// A derived class implementation can return NULL if it defines
  257. /// other protected methods that require a non NULL result from
  258. /// this method. Otherwise it must return a valid, non NULL pointer
  259. /// to the \c ZoneFinder object.
  260. ///
  261. /// When returning non NULL, the ownership of the pointed object
  262. /// was not transferred to the caller; it cannot be assumed to be
  263. /// valid after the originating \c Context object is destroyed.
  264. /// Also, the caller must not try to delete the returned object.
  265. virtual ZoneFinder* getFinder() = 0;
  266. /// \brief Return a vector of RRsets corresponding to findAll() result.
  267. ///
  268. /// This method returns a set of RRsets that correspond to the
  269. /// returned RRsets to a prior \c findAll() call.
  270. ///
  271. /// A derived class implementation can return NULL if it defines
  272. /// other protected methods that require a non NULL result from
  273. /// this method. Otherwise it must return a valid, non NULL pointer
  274. /// to a vector that correspond to the expected set of RRsets.
  275. ///
  276. /// When returning non NULL, the ownership of the pointed object
  277. /// was not transferred to the caller; it cannot be assumed to be
  278. /// valid after the originating \c Context object is destroyed.
  279. /// Also, the caller must not try to delete the returned object.
  280. virtual const std::vector<isc::dns::ConstRRsetPtr>*
  281. getAllRRsets() const = 0;
  282. /// \brief Actual implementation of getAdditional().
  283. ///
  284. /// This base class defines a default implementation that can be
  285. /// used for any type of data sources. A data source implementation
  286. /// can override it.
  287. ///
  288. /// The default version of this implementation requires both
  289. /// \c getFinder() and \c getAllRRsets() return valid results.
  290. virtual void getAdditionalImpl(
  291. const std::vector<isc::dns::RRType>& requested_types,
  292. std::vector<isc::dns::ConstRRsetPtr>& result);
  293. private:
  294. const FindResultFlags flags_;
  295. protected:
  296. const FindOptions options_;
  297. };
  298. /// \brief Generic ZoneFinder context that works for all implementations.
  299. ///
  300. /// This is a concrete derived class of \c ZoneFinder::Context that
  301. /// only use the generic (default) versions of the protected methods
  302. /// and therefore work for any data source implementation.
  303. ///
  304. /// A data source implementation can use this class to create a
  305. /// \c Context object as a return value of \c find() or \c findAll()
  306. /// method if it doesn't have to optimize specific protected methods.
  307. class GenericContext : public Context {
  308. public:
  309. /// \brief The constructor for the normal find call.
  310. ///
  311. /// This constructor is expected to be called from the \c find()
  312. /// method when it constructs the return value.
  313. ///
  314. /// \param finder The ZoneFinder on which find() is called.
  315. /// \param options See the \c Context class.
  316. /// \param result See the \c Context class.
  317. GenericContext(ZoneFinder& finder, FindOptions options,
  318. const ResultContext& result) :
  319. Context(options, result), finder_(finder)
  320. {}
  321. /// \brief The constructor for the normal findAll call.
  322. ///
  323. /// This constructor is expected to be called from the \c findAll()
  324. /// method when it constructs the return value.
  325. ///
  326. /// It copies the vector that is to be returned to the caller of
  327. /// \c findAll() for possible subsequent use. Note that it cannot
  328. /// simply hold a reference to the vector because the caller may
  329. /// alter it after the \c findAll() call.
  330. ///
  331. /// \param finder The ZoneFinder on which findAll() is called.
  332. /// \param options See the \c Context class.
  333. /// \param result See the \c Context class.
  334. /// \param all_set Reference to the vector given by the caller of
  335. /// \c findAll(), storing the RRsets to be returned.
  336. GenericContext(ZoneFinder& finder, FindOptions options,
  337. const ResultContext& result,
  338. const std::vector<isc::dns::ConstRRsetPtr>& all_set) :
  339. Context(options, result), finder_(finder), all_set_(all_set)
  340. {}
  341. protected:
  342. virtual ZoneFinder* getFinder() { return (&finder_); }
  343. virtual const std::vector<isc::dns::ConstRRsetPtr>*
  344. getAllRRsets() const {
  345. return (&all_set_);
  346. }
  347. private:
  348. ZoneFinder& finder_;
  349. std::vector<isc::dns::ConstRRsetPtr> all_set_;
  350. };
  351. ///
  352. /// \name Constructors and Destructor.
  353. ///
  354. //@{
  355. protected:
  356. /// The default constructor.
  357. ///
  358. /// This is intentionally defined as \c protected as this base class should
  359. /// never be instantiated (except as part of a derived class).
  360. ZoneFinder() {}
  361. public:
  362. /// The destructor.
  363. virtual ~ZoneFinder() {}
  364. //@}
  365. ///
  366. /// \name Getter Methods
  367. ///
  368. /// These methods should never throw an exception.
  369. //@{
  370. /// Return the origin name of the zone.
  371. virtual isc::dns::Name getOrigin() const = 0;
  372. /// Return the RR class of the zone.
  373. virtual isc::dns::RRClass getClass() const = 0;
  374. //@}
  375. ///
  376. /// \name Search Methods
  377. ///
  378. //@{
  379. /// \brief Search the zone for a given pair of domain name and RR type.
  380. ///
  381. /// Each derived version of this method searches the underlying backend
  382. /// for the data that best matches the given name and type.
  383. /// This method is expected to be "intelligent", and identifies the
  384. /// best possible answer for the search key. Specifically,
  385. ///
  386. /// - If the search name belongs under a zone cut, it returns the code
  387. /// of \c DELEGATION and the NS RRset at the zone cut.
  388. /// - If there is no matching name, it returns the code of \c NXDOMAIN.
  389. /// - If there is a matching name but no RRset of the search type, it
  390. /// returns the code of \c NXRRSET. This case includes the search name
  391. /// matches an empty node of the zone.
  392. /// - If there is a CNAME RR of the searched name but there is no
  393. /// RR of the searched type of the name (so this type is different from
  394. /// CNAME), it returns the code of \c CNAME and that CNAME RR.
  395. /// Note that if the searched RR type is CNAME, it is considered
  396. /// a successful match, and the code of \c SUCCESS will be returned.
  397. /// - If the search name matches a delegation point of DNAME, it returns
  398. /// the code of \c DNAME and that DNAME RR.
  399. ///
  400. /// No RRset will be returned in the \c NXDOMAIN and \c NXRRSET cases
  401. /// (\c rrset member of \c FindContext will be NULL), unless DNSSEC data
  402. /// are required. See below for the cases with DNSSEC.
  403. ///
  404. /// The returned \c FindContext object can also provide supplemental
  405. /// information about the search result via its methods returning a
  406. /// boolean value. Such information may be useful for the caller if
  407. /// the caller wants to collect additional DNSSEC proofs based on the
  408. /// search result.
  409. ///
  410. /// The \c options parameter specifies customized behavior of the search.
  411. /// Their semantics is as follows (they are or bit-field):
  412. ///
  413. /// - \c FIND_GLUE_OK Allow search under a zone cut. By default the search
  414. /// will stop once it encounters a zone cut. If this option is specified
  415. /// it remembers information about the highest zone cut and continues
  416. /// the search until it finds an exact match for the given name or it
  417. /// detects there is no exact match. If an exact match is found,
  418. /// RRsets for that name are searched just like the normal case;
  419. /// otherwise, if the search has encountered a zone cut, \c DELEGATION
  420. /// with the information of the highest zone cut will be returned.
  421. /// Note: the term "glue" in the DNS protocol standard may sometimes
  422. /// cause confusion: some people use this term strictly for an address
  423. /// record (type AAAA or A) for the name used in the RDATA of an NS RR;
  424. /// some others seem to give it broader flexibility. Nevertheless,
  425. /// in this API the "GLUE OK" simply means the search by find() can
  426. /// continue beyond a zone cut; the derived class implementation does
  427. /// not have to, and should not, check whether the type is an address
  428. /// record or whether the query name is pointed by some NS RR.
  429. /// It's up to the caller with which definition of "glue" the search
  430. /// result with this option should be used.
  431. /// - \c FIND_DNSSEC Request that DNSSEC data (like NSEC, RRSIGs) are
  432. /// returned with the answer. It is allowed for the data source to
  433. /// include them even when not requested.
  434. /// - \c NO_WILDCARD Do not try wildcard matching. This option is of no
  435. /// use for normal lookups; it's intended to be used to get a DNSSEC
  436. /// proof of the non existence of any matching wildcard or non existence
  437. /// of an exact match when a wildcard match is found.
  438. ///
  439. /// In general, \c name is expected to be included in the zone, that is,
  440. /// it should be equal to or a subdomain of the zone origin. Otherwise
  441. /// this method will return \c NXDOMAIN with an empty RRset. But such a
  442. /// case should rather be considered a caller's bug.
  443. ///
  444. /// \note For this reason it's probably better to throw an exception
  445. /// than returning \c NXDOMAIN. This point should be revisited in a near
  446. /// future version. In any case applications shouldn't call this method
  447. /// for an out-of-zone name.
  448. ///
  449. /// <b>DNSSEC considerations:</b>
  450. /// The result when DNSSEC data are required can be very complicated,
  451. /// especially if it involves negative result or wildcard match.
  452. /// Specifically, if an application calls this method for DNS query
  453. /// processing with DNSSEC data, and if the search result code is
  454. /// either \c NXDOMAIN or \c NXRRRSET, and/or \c isWildcard() returns
  455. /// true, then the application will need to find additional NSEC or
  456. /// NSEC3 records for supplemental proofs. This method helps the
  457. /// application for such post search processing.
  458. ///
  459. /// First, it tells the application whether the zone is signed with
  460. /// NSEC or NSEC3 via the \c isNSEC(3)Signed() method. Any sanely signed
  461. /// zone should be signed with either (and only one) of these two types
  462. /// of RRs; however, the application should expect that the zone could
  463. /// be broken and these methods could both return false. But this method
  464. /// should ensure that not both of these methods return true.
  465. ///
  466. /// In case it's signed with NSEC3, there is no further information
  467. /// returned from this method.
  468. ///
  469. /// In case it's signed with NSEC, this method will possibly return
  470. /// a related NSEC RRset in the \c rrset member of \c FindContext.
  471. /// What kind of NSEC is returned depends on the result code
  472. /// (\c NXDOMAIN or \c NXRRSET) and on whether it's a wildcard match:
  473. ///
  474. /// - In case of NXDOMAIN, the returned NSEC covers the queried domain
  475. /// that proves that the query name does not exist in the zone. Note
  476. /// that this does not necessarily prove it doesn't even match a
  477. /// wildcard (even if the result of NXDOMAIN can only happen when
  478. /// there's no matching wildcard either). It is caller's
  479. /// responsibility to provide a proof that there is no matching
  480. /// wildcard if that proof is necessary.
  481. /// - In case of NXRRSET, we need to consider the following cases
  482. /// referring to Section 3.1.3 of RFC4035:
  483. ///
  484. /// -# (Normal) no data: there is a matching non-wildcard name with a
  485. /// different RR type. This is the "No Data" case of the RFC.
  486. /// -# (Normal) empty non terminal: there is no matching (exact or
  487. /// wildcard) name, but there is a subdomain with an RR of the query
  488. /// name. This is one case of "Name Error" of the RFC.
  489. /// -# Wildcard empty non terminal: similar to 2a, but the empty name
  490. /// is a wildcard, and matches the query name by wildcard expansion.
  491. /// This is a special case of "Name Error" of the RFC.
  492. /// -# Wildcard no data: there is no exact match name, but there is a
  493. /// wildcard name that matches the query name with a different type
  494. /// of RR. This is the "Wildcard No Data" case of the RFC.
  495. ///
  496. /// In case 1, \c find() returns NSEC of the matching name.
  497. ///
  498. /// In case 2, \c find() will return NSEC for the interval where the
  499. /// empty nonterminal lives. The end of the interval is the subdomain
  500. /// causing existence of the empty nonterminal (if there's
  501. /// sub.x.example.com, and no record in x.example.com, then
  502. /// x.example.com exists implicitly - is the empty nonterminal and
  503. /// sub.x.example.com is the subdomain causing it). Note that this NSEC
  504. /// proves not only the existence of empty non terminal name but also
  505. /// the non existence of possibly matching wildcard name, because
  506. /// there can be no better wildcard match than the exact matching empty
  507. /// name.
  508. ///
  509. /// In case 3, \c find() will return NSEC for the interval where the
  510. /// wildcard empty nonterminal lives. Cases 2 and 3 are especially
  511. /// complicated and confusing. See the examples below.
  512. ///
  513. /// In case 4, \c find() will return NSEC of the matching wildcard name.
  514. ///
  515. /// Examples: if zone "example.com" has the following record:
  516. /// \code
  517. /// a.example.com. NSEC a.b.example.com.
  518. /// \endcode
  519. /// a call to \c find() for "b.example.com." with the FIND_DNSSEC option
  520. /// will result in NXRRSET, and this NSEC will be returned.
  521. /// Likewise, if zone "example.org" has the following record,
  522. /// \code
  523. /// a.example.org. NSEC x.*.b.example.org.
  524. /// \endcode
  525. /// a call to \c find() for "y.b.example.org" with FIND_DNSSEC will
  526. /// result in NXRRSET and this NSEC; \c isWildcard() on the returned
  527. /// \c FindContext object will return true.
  528. ///
  529. /// \exception std::bad_alloc Memory allocation such as for constructing
  530. /// the resulting RRset fails
  531. /// \throw OutOfZone The Name \c name is outside of the origin of the
  532. /// zone of this ZoneFinder.
  533. /// \exception DataSourceError Derived class specific exception, e.g.
  534. /// when encountering a bad zone configuration or database connection
  535. /// failure. Although these are considered rare, exceptional events,
  536. /// it can happen under relatively usual conditions (unlike memory
  537. /// allocation failure). So, in general, the application is expected
  538. /// to catch this exception, either specifically or as a result of
  539. /// catching a base exception class, and handle it gracefully.
  540. ///
  541. /// \param name The domain name to be searched for.
  542. /// \param type The RR type to be searched for.
  543. /// \param options The search options.
  544. /// \return A \c FindContext object enclosing the search result
  545. /// (see above).
  546. virtual boost::shared_ptr<Context> find(const isc::dns::Name& name,
  547. const isc::dns::RRType& type,
  548. const FindOptions options
  549. = FIND_DEFAULT) = 0;
  550. /// \brief Search for an RRset of given RR type at the zone origin.
  551. ///
  552. /// In terms of API this method is equivalent to a call to \c find() where
  553. /// the \c name parameter is the zone origin (return value of
  554. /// \c getOrigin()) and is redundant. This method is provided as an
  555. /// optimization point for some kind of finder implementations that can
  556. /// exploit the fact that the query name is the zone origin and for
  557. /// applications that want to possibly benefit from such implementations.
  558. ///
  559. /// If \c use_minttl is set to \c true and the returned context would
  560. /// contain a non NULL RRset, its RR TTL is (possibly) adjusted so that
  561. /// it's set to the minimum of its own TTL and the minimum TTL field value
  562. /// of the zone's SOA record. If the RRset contains an RRSIG, its TTL
  563. /// is also adjusted in the same way.
  564. ///
  565. /// The origin of a zone is special in some points: for any valid zone
  566. /// there should always be an SOA and at least one NS RR there, which
  567. /// also means the origin name is never empty. Also, the SOA record can
  568. /// be used in a DNS response for negative answers, in which case the
  569. /// RR TTL must be set to minimum of its own RRTTL and the value of the
  570. /// minimum TTL field. Although these operations can be performed
  571. /// through other public interfaces, they can be sometimes suboptimal
  572. /// in performance or could be more efficient in a specialized
  573. /// implementation. For example, a specific implementation of
  574. /// \c getOrigin() could involve a dynamic creation of a \c Name object,
  575. /// which is less efficient; on the other hand, the underlying finder
  576. /// implementation may have an efficient way to access RRs of the origin
  577. /// in implementation specific way; and, while reconstructing an RRset
  578. /// with replacing the TTL is relatively expensive, this can be done
  579. /// much faster if the need for it is known beforehand.
  580. ///
  581. /// If the underlying finder implementation wants to optimize these cases,
  582. /// it can do so by specializing the method. It has the default
  583. /// implementation for any other implementations, which should work for
  584. /// any finder implementation as long as it conforms to other public
  585. /// interfaces.
  586. ///
  587. /// So, an implementation of a finder does not have to care about this
  588. /// method unless it sees the need for optimizing the behavior.
  589. /// Also, applications normally do not have to use this interface;
  590. /// using the generic \c find() method (with some post call processing)
  591. /// can do everything this method can provide. The default implementation
  592. /// may even be slower than such straightforward usage due to the
  593. /// internal overhead. This method should be used if and only if the
  594. /// application needs to achieve the possible best performance with an
  595. /// optimized finder implementation.
  596. ///
  597. /// \param type The RR type to be searched for.
  598. /// \param use_minttl Whether to adjust the TTL (see the description).
  599. /// \param options The search options. Same for \c find().
  600. ///
  601. /// \return A \c FindContext object enclosing the search result.
  602. /// See \c find().
  603. virtual boost::shared_ptr<Context> findAtOrigin(
  604. const isc::dns::RRType& type, bool use_minttl,
  605. FindOptions options);
  606. public:
  607. ///
  608. /// \brief Finds all RRsets in the given name.
  609. ///
  610. /// This function works almost exactly in the same way as the find one. The
  611. /// only difference is, when the lookup is successful (eg. the code is
  612. /// SUCCESS), all the RRsets residing in the named node are
  613. /// copied into the \c target parameter and the rrset member of the result
  614. /// is NULL. All the other (unsuccessful) cases are handled the same,
  615. /// including returning delegations, NSEC/NSEC3 availability and NSEC
  616. /// proofs, wildcard information etc. The options parameter works the
  617. /// same way and it should conform to the same exception restrictions.
  618. ///
  619. /// \param name \see find, parameter name
  620. /// \param target the successfull result is returned through this
  621. /// \param options \see find, parameter options
  622. /// \return \see find and it's result
  623. virtual boost::shared_ptr<Context> findAll(
  624. const isc::dns::Name& name,
  625. std::vector<isc::dns::ConstRRsetPtr> &target,
  626. const FindOptions options = FIND_DEFAULT) = 0;
  627. /// A helper structure to represent the search result of \c findNSEC3().
  628. ///
  629. /// The idea is similar to that of \c FindContext, but \c findNSEC3() has
  630. /// special interface and semantics, we use a different structure to
  631. /// represent the result.
  632. struct FindNSEC3Result {
  633. FindNSEC3Result(bool param_matched, uint8_t param_closest_labels,
  634. isc::dns::ConstRRsetPtr param_closest_proof,
  635. isc::dns::ConstRRsetPtr param_next_proof) :
  636. matched(param_matched), closest_labels(param_closest_labels),
  637. closest_proof(param_closest_proof),
  638. next_proof(param_next_proof)
  639. {}
  640. /// true iff closest_proof is a matching NSEC3
  641. const bool matched;
  642. /// The number of labels of the identified closest encloser.
  643. const uint8_t closest_labels;
  644. /// Either the NSEC3 for the closest provable encloser of the given
  645. /// name or NSEC3 that covers the name
  646. const isc::dns::ConstRRsetPtr closest_proof;
  647. /// When non NULL, NSEC3 for the next closer name.
  648. const isc::dns::ConstRRsetPtr next_proof;
  649. };
  650. /// Search the zone for the NSEC3 RR(s) that prove existence or non
  651. /// existence of a give name.
  652. ///
  653. /// It searches the NSEC3 namespace of the zone (how that namespace is
  654. /// implemented can vary in specific data source implementation) for NSEC3
  655. /// RRs that match or cover the NSEC3 hash value for the given name.
  656. ///
  657. /// If \c recursive is false, it will first look for the NSEC3 that has
  658. /// a matching hash. If it doesn't exist, it identifies the covering NSEC3
  659. /// for the hash. In either case the search stops at that point and the
  660. /// found NSEC3 RR(set) will be returned in the closest_proof member of
  661. /// \c FindNSEC3Result. \c matched is true or false depending on
  662. /// the found NSEC3 is a matched one or covering one. \c next_proof
  663. /// is always NULL. closest_labels must be equal to the number of
  664. /// labels of \c name (and therefore meaningless).
  665. ///
  666. /// If \c recursive is true, it will continue the search toward the zone
  667. /// apex (origin name) until it finds a provable encloser, that is,
  668. /// an ancestor of \c name that has a matching NSEC3. This is the closest
  669. /// provable encloser of \c name as defined in RFC5155. In this case,
  670. /// if the found encloser is not equal to \c name, the search should
  671. /// have seen a covering NSEC3 for the immediate child of the found
  672. /// encloser. That child name is the next closer name as defined in
  673. /// RFC5155. In this case, this method returns the NSEC3 for the
  674. /// closest encloser in \c closest_proof, and the NSEC3 for the next
  675. /// closer name in \c next_proof of \c FindNSEC3Result. This set of
  676. /// NSEC3 RRs provide the closest encloser proof as defined in RFC5155.
  677. /// closest_labels will be set to the number of labels of the identified
  678. /// closest encloser. This will be useful when the caller needs to
  679. /// construct the closest encloser name from the original \c name.
  680. /// If, on the other hand, the found closest name is equal to \c name,
  681. /// this method simply returns it in \c closest_proof. \c next_proof
  682. /// is set to NULL. In all cases \c matched is set to true.
  683. /// closest_labels will be set to the number of labels of \c name.
  684. ///
  685. /// When looking for NSEC3, this method retrieves NSEC3 parameters from
  686. /// the corresponding zone to calculate hash values. Actual implementation
  687. /// of how to do this will differ in different data sources. If the
  688. /// NSEC3 parameters are not available \c DataSourceError exception
  689. /// will be thrown.
  690. ///
  691. /// \note This implicitly means this method assumes the zone does not
  692. /// have more than one set of parameters. This assumption should be
  693. /// reasonable in actual deployment and will help simplify the interface
  694. /// and implementation. But if there's a real need for supporting
  695. /// multiple sets of parameters in a single zone, we will have to
  696. /// extend this method so that, e.g., the caller can specify the parameter
  697. /// set.
  698. ///
  699. /// In general, this method expects the zone is properly signed with NSEC3
  700. /// RRs. Specifically, it assumes at least the apex node has a matching
  701. /// NSEC3 RR (so the search in the recursive mode must always succeed);
  702. /// it also assumes that it can retrieve NSEC parameters (iterations,
  703. /// algorithm, and salt) from the zone as noted above. If these
  704. /// assumptions aren't met, \c DataSourceError exception will be thrown.
  705. ///
  706. /// \exception OutOfZone name is not a subdomain of the zone origin
  707. /// \exception DataSourceError Low-level or internal datasource errors
  708. /// happened, or the zone isn't properly signed with NSEC3
  709. /// (NSEC3 parameters cannot be found, no NSEC3s are available, etc).
  710. /// \exception std::bad_alloc The underlying implementation involves
  711. /// memory allocation and it fails
  712. ///
  713. /// \param name The name for which NSEC3 RRs are to be found. It must
  714. /// be a subdomain of the zone.
  715. /// \param recursive Whether or not search should continue until it finds
  716. /// a provable encloser (see above).
  717. ///
  718. /// \return The search result and whether or not the closest_proof is
  719. /// a matching NSEC3, in the form of \c FindNSEC3Result object.
  720. virtual FindNSEC3Result
  721. findNSEC3(const isc::dns::Name& name, bool recursive) = 0;
  722. //@}
  723. };
  724. /// \brief Operator to combine FindOptions
  725. ///
  726. /// We would need to manually static-cast the options if we put or
  727. /// between them, which is undesired with bit-flag options. Therefore
  728. /// we hide the cast here, which is the simplest solution and it still
  729. /// provides reasonable level of type safety.
  730. inline ZoneFinder::FindOptions operator |(ZoneFinder::FindOptions a,
  731. ZoneFinder::FindOptions b)
  732. {
  733. return (static_cast<ZoneFinder::FindOptions>(static_cast<unsigned>(a) |
  734. static_cast<unsigned>(b)));
  735. }
  736. /// \brief Operator to combine FindResultFlags
  737. ///
  738. /// Similar to the same operator for \c FindOptions. Refer to the description
  739. /// of that function.
  740. inline ZoneFinder::FindResultFlags operator |(
  741. ZoneFinder::FindResultFlags a,
  742. ZoneFinder::FindResultFlags b)
  743. {
  744. return (static_cast<ZoneFinder::FindResultFlags>(
  745. static_cast<unsigned>(a) | static_cast<unsigned>(b)));
  746. }
  747. /// \brief A pointer-like type pointing to a \c ZoneFinder object.
  748. typedef boost::shared_ptr<ZoneFinder> ZoneFinderPtr;
  749. /// \brief A pointer-like type pointing to an immutable \c ZoneFinder object.
  750. typedef boost::shared_ptr<const ZoneFinder> ConstZoneFinderPtr;
  751. /// \brief A pointer-like type pointing to a \c ZoneFinder::Context object.
  752. typedef boost::shared_ptr<ZoneFinder::Context> ZoneFinderContextPtr;
  753. /// \brief A pointer-like type pointing to an immutable
  754. /// \c ZoneFinder::Context object.
  755. typedef boost::shared_ptr<ZoneFinder::Context> ConstZoneFinderContextPtr;
  756. } // end of datasrc
  757. } // end of isc
  758. #endif // DATASRC_ZONE_FINDER_H
  759. // Local Variables:
  760. // mode: c++
  761. // End: