zone.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. // Copyright (C) 2010 CZ NIC
  2. // Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
  3. //
  4. // Permission to use, copy, modify, and/or distribute this software for any
  5. // purpose with or without fee is hereby granted, provided that the above
  6. // copyright notice and this permission notice appear in all copies.
  7. //
  8. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  9. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  10. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  11. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  12. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  13. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  14. // PERFORMANCE OF THIS SOFTWARE.
  15. #ifndef __ZONE_H
  16. #define __ZONE_H 1
  17. #include <datasrc/result.h>
  18. #include <dns/rrsetlist.h>
  19. namespace isc {
  20. namespace datasrc {
  21. /// \brief The base class for a single authoritative zone
  22. ///
  23. /// The \c Zone class is an abstract base class for representing
  24. /// a DNS zone as part of data source.
  25. ///
  26. /// At the moment this is provided mainly for making the \c ZoneTable class
  27. /// and the authoritative query logic testable, and only provides a minimal
  28. /// set of features.
  29. /// This is why this class is defined in the same header file, but it may
  30. /// have to move to a separate header file when we understand what is
  31. /// necessary for this class for actual operation.
  32. ///
  33. /// The idea is to provide a specific derived zone class for each data
  34. /// source, beginning with in memory one. At that point the derived classes
  35. /// will have more specific features. For example, they will maintain
  36. /// information about the location of a zone file, whether it's loaded in
  37. /// memory, etc.
  38. ///
  39. /// It's not yet clear how the derived zone classes work with various other
  40. /// data sources when we integrate these components, but one possibility is
  41. /// something like this:
  42. /// - If the underlying database such as some variant of SQL doesn't have an
  43. /// explicit representation of zones (as part of public interface), we can
  44. /// probably use a "default" zone class that simply encapsulates the
  45. /// corresponding data source and calls a common "find" like method.
  46. /// - Some data source may want to specialize it by inheritance as an
  47. /// optimization. For example, in the current schema design of the sqlite3
  48. /// data source, its (derived) zone class would contain the information of
  49. /// the "zone ID".
  50. ///
  51. /// <b>Note:</b> Unlike some other abstract base classes we don't name the
  52. /// class beginning with "Abstract". This is because we want to have
  53. /// commonly used definitions such as \c Result and \c ZonePtr, and we want
  54. /// to make them look more intuitive.
  55. class Zone {
  56. public:
  57. /// Result codes of the \c find() method.
  58. ///
  59. /// Note: the codes are tentative. We may need more, or we may find
  60. /// some of them unnecessary as we implement more details.
  61. enum Result {
  62. SUCCESS, ///< An exact match is found.
  63. DELEGATION, ///< The search encounters a zone cut.
  64. NXDOMAIN, ///< There is no domain name that matches the search name
  65. NXRRSET, ///< There is a matching name but no RRset of the search type
  66. CNAME, ///< The search encounters and returns a CNAME RR
  67. DNAME ///< The search encounters and returns a DNAME RR
  68. };
  69. /// A helper structure to represent the search result of \c find().
  70. ///
  71. /// This is a straightforward tuple of the result code and a pointer
  72. /// to the found RRset to represent the result of \c find()
  73. /// (there will be more members in the future - see the class
  74. /// description).
  75. /// We use this in order to avoid overloading the return value for both
  76. /// the result code ("success" or "not found") and the found object,
  77. /// i.e., avoid using \c NULL to mean "not found", etc.
  78. ///
  79. /// This is a simple value class whose internal state never changes,
  80. /// so for convenience we allow the applications to refer to the members
  81. /// directly.
  82. ///
  83. /// Note: we should eventually include a notion of "zone node", which
  84. /// corresponds to a particular domain name of the zone, so that we can
  85. /// find RRsets of a different RR type for that name (e.g. for type ANY
  86. /// query or to include DS RRs with delegation).
  87. ///
  88. /// Note: we may also want to include the closest enclosure "node" to
  89. /// optimize including the NSEC for no-wildcard proof (FWIW NSD does that).
  90. struct FindResult {
  91. FindResult(Result param_code,
  92. const isc::dns::ConstRRsetPtr param_rrset) :
  93. code(param_code), rrset(param_rrset)
  94. {}
  95. const Result code;
  96. const isc::dns::ConstRRsetPtr rrset;
  97. };
  98. /// Find options.
  99. ///
  100. /// The option values are used as a parameter for \c find().
  101. /// These are values of a bitmask type. Bitwise operations can be
  102. /// performed on these values to express compound options.
  103. enum FindOptions {
  104. FIND_DEFAULT = 0, ///< The default options
  105. FIND_GLUE_OK = 1 ///< Allow search under a zone cut
  106. };
  107. ///
  108. /// \name Constructors and Destructor.
  109. ///
  110. //@{
  111. protected:
  112. /// The default constructor.
  113. ///
  114. /// This is intentionally defined as \c protected as this base class should
  115. /// never be instantiated (except as part of a derived class).
  116. Zone() {}
  117. public:
  118. /// The destructor.
  119. virtual ~Zone() {}
  120. //@}
  121. ///
  122. /// \name Getter Methods
  123. ///
  124. /// These methods should never throw an exception.
  125. //@{
  126. /// Return the origin name of the zone.
  127. virtual const isc::dns::Name& getOrigin() const = 0;
  128. /// Return the RR class of the zone.
  129. virtual const isc::dns::RRClass& getClass() const = 0;
  130. //@}
  131. ///
  132. /// \name Search Method
  133. ///
  134. //@{
  135. /// Search the zone for a given pair of domain name and RR type.
  136. ///
  137. /// Each derived version of this method searches the underlying backend
  138. /// for the data that best matches the given name and type.
  139. /// This method is expected to be "intelligent", and identifies the
  140. /// best possible answer for the search key. Specifically,
  141. /// - If the search name belongs under a zone cut, it returns the code
  142. /// of \c DELEGATION and the NS RRset at the zone cut.
  143. /// - If there is no matching name, it returns the code of \c NXDOMAIN,
  144. /// and, if DNSSEC is requested, the NSEC RRset that proves the
  145. /// non-existence.
  146. /// - If there is a matching name but no RRset of the search type, it
  147. /// returns the code of \c NXRRSET, and, if DNSSEC is required,
  148. /// the NSEC RRset for that name.
  149. /// - If there is a CNAME RR of the searched name but there is no
  150. /// RR of the searched type of the name (so this type is different from
  151. /// CNAME), it returns the code of \c CNAME and that CNAME RR.
  152. /// Note that if the searched RR type is CNAME, it is considered
  153. /// a successful match, and the code of \c SUCCESS will be returned.
  154. /// - If the search name matches a delegation point of DNAME, it returns
  155. /// the code of \c DNAME and that DNAME RR.
  156. /// - If the target isn't NULL, all RRsets under the domain are inserted
  157. /// there and SUCCESS (or NXDOMAIN, in case of empty domain) is returned
  158. /// instead of normall processing. This is intended to handle ANY query.
  159. ///
  160. /// The \c options parameter specifies customized behavior of the search.
  161. /// Their semantics is as follows:
  162. /// - \c GLUE_OK Allow search under a zone cut. By default the search
  163. /// will stop once it encounters a zone cut. If this option is specified
  164. /// it remembers information about the highest zone cut and continues
  165. /// the search until it finds an exact match for the given name or it
  166. /// detects there is no exact match. If an exact match is found,
  167. /// RRsets for that name are searched just like the normal case;
  168. /// otherwise, if the search has encountered a zone cut, \c DELEGATION
  169. /// with the information of the highest zone cut will be returned.
  170. ///
  171. /// A derived version of this method may involve internal resource
  172. /// allocation, especially for constructing the resulting RRset, and may
  173. /// throw an exception if it fails.
  174. /// It throws DuplicateRRset exception if there are duplicate rrsets under
  175. /// the same domain.
  176. /// It should not throw other types of exceptions.
  177. ///
  178. /// \param name The domain name to be searched for.
  179. /// \param type The RR type to be searched for.
  180. /// \param target If target is not NULL, insert all RRs under the domain
  181. /// into it.
  182. /// \param options The search options.
  183. /// \return A \c FindResult object enclosing the search result (see above).
  184. virtual FindResult find(const isc::dns::Name& name,
  185. const isc::dns::RRType& type,
  186. isc::dns::RRsetList* target = NULL,
  187. const FindOptions options
  188. = FIND_DEFAULT) const = 0;
  189. //@}
  190. };
  191. /// \brief A pointer-like type pointing to a \c Zone object.
  192. typedef boost::shared_ptr<Zone> ZonePtr;
  193. /// \brief A pointer-like type pointing to a \c Zone object.
  194. typedef boost::shared_ptr<const Zone> ConstZonePtr;
  195. }
  196. }
  197. #endif // __ZONE_H
  198. // Local Variables:
  199. // mode: c++
  200. // End: