zonetable.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 __ZONETABLE_H
  15. #define __ZONETABLE_H 1
  16. #include <boost/shared_ptr.hpp>
  17. namespace isc {
  18. namespace dns {
  19. class Name;
  20. class RRClass;
  21. };
  22. namespace datasrc {
  23. /// \brief A single authoritative zone
  24. ///
  25. /// The \c Zone class represents a DNS zone as part of %data source.
  26. ///
  27. /// At the moment this is provided mainly for making the \c ZoneTable class
  28. /// testable, and only provides a minimal 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. /// Likewise, it will have more features. For example, it will maintain
  33. /// information about the location of a zone file, whether it's loaded in
  34. /// memory, etc.
  35. class Zone {
  36. ///
  37. /// \name Constructors and Destructor.
  38. ///
  39. /// \b Note:
  40. /// The copy constructor and the assignment operator are intentionally
  41. /// defined as private, making this class non copyable.
  42. //@{
  43. private:
  44. Zone(const Zone& source);
  45. Zone& operator=(const Zone& source);
  46. public:
  47. /// \brief Constructor from zone parameters.
  48. ///
  49. /// This constructor internally involves resource allocation, and if
  50. /// it fails, a corresponding standard exception will be thrown.
  51. /// It never throws an exception otherwise.
  52. ///
  53. /// \param rrclass The RR class of the zone.
  54. /// \param origin The origin name of the zone.
  55. Zone(const isc::dns::RRClass& rrclass, const isc::dns::Name& origin);
  56. /// The destructor.
  57. ~Zone();
  58. //@}
  59. ///
  60. /// \name Getter Methods
  61. ///
  62. /// These methods never throw an exception.
  63. //@{
  64. /// \brief Return the origin name of the zone.
  65. const isc::dns::Name& getOrigin() const;
  66. /// \brief Return the RR class of the zone.
  67. const isc::dns::RRClass& getClass() const;
  68. //@}
  69. private:
  70. struct ZoneImpl;
  71. ZoneImpl* impl_;
  72. };
  73. /// \brief A pointer-like type pointing to a \c Zone object.
  74. typedef boost::shared_ptr<Zone> ZonePtr;
  75. /// \brief A pointer-like type pointing to a \c Zone object.
  76. typedef boost::shared_ptr<const Zone> ConstZonePtr;
  77. /// \brief A set of authoritative zones.
  78. ///
  79. /// The \c ZoneTable class represents a set of zones of the same RR class
  80. /// and provides a basic interface to help DNS lookup processing.
  81. /// For a given domain name, its \c find() method searches the set for a zone
  82. /// that gives a longest match against that name.
  83. ///
  84. /// The set of zones are assumed to be of the same RR class, but the
  85. /// \c ZoneTable class does not enforce the assumption through its interface.
  86. /// For example, the \c add() method does not check if the new zone
  87. /// is of the same RR class as that of the others already in the table.
  88. /// It is caller's responsibility to ensure this assumption.
  89. ///
  90. /// <b>Notes to developer:</b>
  91. ///
  92. /// The add() method takes a (Boost) shared pointer because it would be
  93. /// inconvenient to require the caller to maintain the ownership of zones,
  94. /// while it wouldn't be safe to delete unnecessary zones inside the zone
  95. /// table.
  96. ///
  97. /// On the other hand, the find() method returns a bare pointer, rather than
  98. /// the shared pointer, in order to minimize the dependency on Boost
  99. /// definitions in our public interfaces. This means the caller can only
  100. /// refer to the returned object (via the pointer) for a short period.
  101. /// It should be okay for simple lookup purposes, but if we see the need
  102. /// for keeping a \c Zone object for a longer period of context, we may
  103. /// have to revisit this decision.
  104. ///
  105. /// Currently, \c FindResult::zone is immutable for safety.
  106. /// In future versions we may want to make it changeable. For example,
  107. /// we may want to allow configuration update on an existing zone.
  108. ///
  109. /// In BIND 9's "zt" module, the equivalent of \c find() has an "option"
  110. /// parameter. The only defined option is the one to specify the "no exact"
  111. /// mode, and the only purpose of that mode is to prefer a second longest match
  112. /// even if there is an exact match in order to deal with type DS query.
  113. /// This trick may help enhance performance, but it also seems to make the
  114. /// implementation complicated for a very limited, minor case. So, for now,
  115. /// we don't introduce the special mode, and, since it was the only reason to
  116. /// have search options in BIND 9, our initial implementation doesn't provide
  117. /// a switch for options.
  118. class ZoneTable {
  119. public:
  120. /// Result codes of various public methods of \c ZoneTable.
  121. ///
  122. /// The detailed semantics may differ in different methods.
  123. /// See the description of specific methods for more details.
  124. enum Result {
  125. SUCCESS, ///< The operation is successful.
  126. EXIST, ///< A zone is already stored in \c ZoneTable.
  127. NOTFOUND, ///< The specified zone is not found in \c ZoneTable.
  128. PARTIALMATCH ///< \c Only a partial match is found in \c find().
  129. };
  130. /// \brief A helper structure to represent the search result of
  131. /// <code>ZoneTable::find()</code>.
  132. ///
  133. /// This is a straightforward pair of the result code and a pointer
  134. /// to the found zone to represent the result of \c find().
  135. /// We use this in order to avoid overloading the return value for both
  136. /// the result code ("success" or "not found") and the found object,
  137. /// i.e., avoid using \c NULL to mean "not found", etc.
  138. ///
  139. /// This is a simple value class with no internal state, so for
  140. /// convenience we allow the applications to refer to the members
  141. /// directly.
  142. ///
  143. /// See the description of \c find() for the semantics of the member
  144. /// variables.
  145. struct FindResult {
  146. FindResult(Result param_code, const Zone* param_zone) :
  147. code(param_code), zone(param_zone)
  148. {}
  149. const Result code;
  150. const Zone* const zone;
  151. };
  152. ///
  153. /// \name Constructors and Destructor.
  154. ///
  155. /// \b Note:
  156. /// The copy constructor and the assignment operator are intentionally
  157. /// defined as private, making this class non copyable.
  158. //@{
  159. private:
  160. ZoneTable(const ZoneTable& source);
  161. ZoneTable& operator=(const ZoneTable& source);
  162. public:
  163. /// Default constructor.
  164. ///
  165. /// This constructor internally involves resource allocation, and if
  166. /// it fails, a corresponding standard exception will be thrown.
  167. /// It never throws an exception otherwise.
  168. ZoneTable();
  169. /// The destructor.
  170. ~ZoneTable();
  171. //@}
  172. /// Add a \c Zone to the \c ZoneTable.
  173. ///
  174. /// \c zone must not be associated with a NULL pointer; otherwise
  175. /// an exception of class \c InvalidParameter will be thrown.
  176. /// If internal resource allocation fails, a corresponding standard
  177. /// exception will be thrown.
  178. /// This method never throws an exception otherwise.
  179. ///
  180. /// \param zone A \c Zone object to be added.
  181. /// \return \c SUCCESS If the zone is successfully added to the zone table.
  182. /// \return \c EXIST The zone table already stores a zone that has the
  183. /// same origin.
  184. Result add(ZonePtr zone);
  185. /// Remove a \c Zone of the given origin name from the \c ZoneTable.
  186. ///
  187. /// This method never throws an exception.
  188. ///
  189. /// \param origin The origin name of the zone to be removed.
  190. /// \return \c SUCCESS If the zone is successfully removed from the
  191. /// zone table.
  192. /// \return \c NOTFOUND The zone table does not store the zone that matches
  193. /// \c origin.
  194. Result remove(const isc::dns::Name& origin);
  195. /// Find a \c Zone that best matches the given name in the \c ZoneTable.
  196. ///
  197. /// It searches the internal storage for a \c Zone that gives the
  198. /// longest match against \c name, and returns the result in the
  199. /// form of a \c FindResult object as follows:
  200. /// - \c code: The result code of the operation.
  201. /// - \c SUCCESS: A zone that gives an exact match is found
  202. /// - \c PARTIALMATCH: A zone whose origin is a super domain of
  203. /// \c name is found (but there is no exact match)
  204. /// - \c NOTFOUND: For all other cases.
  205. /// - \c zone: A pointer to the found \c Zone object if one is found;
  206. /// otherwise \c NULL.
  207. ///
  208. /// The pointer returned in the \c FindResult object is only valid until
  209. /// the corresponding zone is removed from the zone table.
  210. /// The caller must ensure that the zone is held in the zone table while
  211. /// it needs to refer to it.
  212. ///
  213. /// This method never throws an exception.
  214. ///
  215. /// \param name A domain name for which the search is performed.
  216. /// \return A \c FindResult object enclosing the search result (see above).
  217. FindResult find(const isc::dns::Name& name) const;
  218. private:
  219. struct ZoneTableImpl;
  220. ZoneTableImpl* impl_;
  221. };
  222. }
  223. }
  224. #endif // __ZONETABLE_H
  225. // Local Variables:
  226. // mode: c++
  227. // End: