memory_datasrc.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 __MEMORY_DATA_SOURCE_H
  15. #define __MEMORY_DATA_SOURCE_H 1
  16. #include <datasrc/zonetable.h>
  17. namespace isc {
  18. namespace dns {
  19. class Name;
  20. };
  21. namespace datasrc {
  22. /// A derived zone class intended to be used with the memory data source.
  23. class MemoryZone : public Zone {
  24. ///
  25. /// \name Constructors and Destructor.
  26. ///
  27. /// \b Note:
  28. /// The copy constructor and the assignment operator are intentionally
  29. /// defined as private, making this class non copyable.
  30. //@{
  31. private:
  32. MemoryZone(const MemoryZone& source);
  33. MemoryZone& operator=(const MemoryZone& source);
  34. public:
  35. /// \brief Constructor from zone parameters.
  36. ///
  37. /// This constructor internally involves resource allocation, and if
  38. /// it fails, a corresponding standard exception will be thrown.
  39. /// It never throws an exception otherwise.
  40. ///
  41. /// \param rrclass The RR class of the zone.
  42. /// \param origin The origin name of the zone.
  43. MemoryZone(const isc::dns::RRClass& rrclass, const isc::dns::Name& origin);
  44. /// The destructor.
  45. virtual ~MemoryZone();
  46. //@}
  47. /// \brief Returns the origin of the zone.
  48. virtual const isc::dns::Name& getOrigin() const;
  49. /// \brief Returns the class of the zone.
  50. virtual const isc::dns::RRClass& getClass() const;
  51. /// \brief Looks up an RRset in the zone.
  52. ///
  53. /// See documentation in \c Zone.
  54. ///
  55. /// It returns NULL pointer in case of NXDOMAIN and NXRRSET
  56. /// (the base class documentation does not seem to require that).
  57. virtual FindResult find(const isc::dns::Name& name,
  58. const isc::dns::RRType& type) const;
  59. /// \brief Inserts an rrset into the zone.
  60. ///
  61. /// It puts another RRset into the zone.
  62. ///
  63. /// It throws NullRRset or OutOfZone if the provided rrset is invalid. It
  64. /// might throw standard allocation exceptions, in which case this function
  65. /// does not guarantee strong exception safety (it is currently not needed,
  66. /// if it is needed in future, it should be implemented).
  67. ///
  68. /// \param rrset The set to add.
  69. /// \return SUCCESS or EXIST (if an rrset for given name and type already
  70. /// exists).
  71. result::Result add(const isc::dns::ConstRRsetPtr& rrset);
  72. /// \brief RRSet out of zone exception.
  73. ///
  74. /// This is thrown if addition of an RRset that doesn't belong under the
  75. /// zone's origin is requested.
  76. struct OutOfZone : public InvalidParameter {
  77. OutOfZone(const char* file, size_t line, const char* what) :
  78. InvalidParameter(file, line, what)
  79. { }
  80. };
  81. /// \brief RRset is NULL exception.
  82. ///
  83. /// This is thrown if the provided RRset parameter is NULL.
  84. struct NullRRset : public InvalidParameter {
  85. NullRRset(const char* file, size_t line, const char* what) :
  86. InvalidParameter(file, line, what)
  87. { }
  88. };
  89. /// \brief Load zone from masterfile.
  90. ///
  91. /// This loads data from masterfile specified by filename. It replaces
  92. /// current content. The masterfile parsing ability is kind of limited,
  93. /// see isc::dns::masterLoad.
  94. ///
  95. /// This throws isc::dns::MasterLoadError if there is problem with loading
  96. /// (missing file, malformed, it contains different zone, etc - see
  97. /// isc::dns::masterLoad for details).
  98. ///
  99. /// In case of internal problems, OutOfZone, NullRRset or AssertError could
  100. /// be thrown, but they should not be expected. Exceptions caused by
  101. /// allocation may be thrown as well.
  102. ///
  103. /// If anything is thrown, the previous content is preserved (so it can
  104. /// be used to update the data, but if user makes a typo, the old one
  105. /// is kept).
  106. ///
  107. /// \param filename The master file to load.
  108. ///
  109. /// \todo We may need to split it to some kind of build and commit/abort.
  110. /// This will probably be needed when a better implementation of
  111. /// configuration reloading is written.
  112. void load(const std::string& filename);
  113. private:
  114. /// \name Hidden private data
  115. //@{
  116. struct MemoryZoneImpl;
  117. MemoryZoneImpl* impl_;
  118. //@}
  119. };
  120. /// \brief A data source that uses in memory dedicated backend.
  121. ///
  122. /// The \c MemoryDataSrc class represents a data source and provides a
  123. /// basic interface to help DNS lookup processing. For a given domain
  124. /// name, its \c findZone() method searches the in memory dedicated backend
  125. /// for the zone that gives a longest match against that name.
  126. ///
  127. /// The in memory dedicated backend are assumed to be of the same RR class,
  128. /// but the \c MemoryDataSrc class does not enforce the assumption through
  129. /// its interface.
  130. /// For example, the \c addZone() method does not check if the new zone is of
  131. /// the same RR class as that of the others already in the dedicated backend.
  132. /// It is caller's responsibility to ensure this assumption.
  133. ///
  134. /// <b>Notes to developer:</b>
  135. ///
  136. /// For now, we don't make it a derived class of AbstractDataSrc because the
  137. /// interface is so different (we'll eventually consider this as part of the
  138. /// generalization work).
  139. ///
  140. /// The addZone() method takes a (Boost) shared pointer because it would be
  141. /// inconvenient to require the caller to maintain the ownership of zones,
  142. /// while it wouldn't be safe to delete unnecessary zones inside the dedicated
  143. /// backend.
  144. ///
  145. /// The findZone() method takes a domain name and returns the best matching \c
  146. /// MemoryZone in the form of (Boost) shared pointer, so that it can provide
  147. /// the general interface for all data sources.
  148. ///
  149. /// Currently, \c FindResult::zone is immutable for safety.
  150. /// In future versions we may want to make it changeable. For example,
  151. /// we may want to allow configuration update on an existing zone.
  152. class MemoryDataSrc {
  153. public:
  154. /// \brief A helper structure to represent the search result of
  155. /// <code>MemoryDataSrc::find()</code>.
  156. ///
  157. /// This is a straightforward pair of the result code and a share pointer
  158. /// to the found zone to represent the result of \c find().
  159. /// We use this in order to avoid overloading the return value for both
  160. /// the result code ("success" or "not found") and the found object,
  161. /// i.e., avoid using \c NULL to mean "not found", etc.
  162. ///
  163. /// This is a simple value class with no internal state, so for
  164. /// convenience we allow the applications to refer to the members
  165. /// directly.
  166. ///
  167. /// See the description of \c find() for the semantics of the member
  168. /// variables.
  169. struct FindResult {
  170. FindResult(result::Result param_code, const ConstZonePtr param_zone) :
  171. code(param_code), zone(param_zone)
  172. {}
  173. const result::Result code;
  174. const ConstZonePtr zone;
  175. };
  176. ///
  177. /// \name Constructors and Destructor.
  178. ///
  179. /// \b Note:
  180. /// The copy constructor and the assignment operator are intentionally
  181. /// defined as private, making this class non copyable.
  182. //@{
  183. private:
  184. MemoryDataSrc(const MemoryDataSrc& source);
  185. MemoryDataSrc& operator=(const MemoryDataSrc& source);
  186. public:
  187. /// Default constructor.
  188. ///
  189. /// This constructor internally involves resource allocation, and if
  190. /// it fails, a corresponding standard exception will be thrown.
  191. /// It never throws an exception otherwise.
  192. MemoryDataSrc();
  193. /// The destructor.
  194. ~MemoryDataSrc();
  195. //@}
  196. /// Return the number of zones stored in the data source.
  197. ///
  198. /// This method never throws an exception.
  199. ///
  200. /// \return The number of zones stored in the data source.
  201. unsigned int getZoneCount() const;
  202. /// Add a \c Zone to the \c MemoryDataSrc.
  203. ///
  204. /// \c Zone must not be associated with a NULL pointer; otherwise
  205. /// an exception of class \c InvalidParameter will be thrown.
  206. /// If internal resource allocation fails, a corresponding standard
  207. /// exception will be thrown.
  208. /// This method never throws an exception otherwise.
  209. ///
  210. /// \param zone A \c Zone object to be added.
  211. /// \return \c result::SUCCESS If the zone is successfully
  212. /// added to the memory data source.
  213. /// \return \c result::EXIST The memory data source already
  214. /// stores a zone that has the same origin.
  215. result::Result addZone(ZonePtr zone);
  216. /// Find a \c Zone that best matches the given name in the \c MemoryDataSrc.
  217. ///
  218. /// It searches the internal storage for a \c Zone that gives the
  219. /// longest match against \c name, and returns the result in the
  220. /// form of a \c FindResult object as follows:
  221. /// - \c code: The result code of the operation.
  222. /// - \c result::SUCCESS: A zone that gives an exact match
  223. // is found
  224. /// - \c result::PARTIALMATCH: A zone whose origin is a
  225. // super domain of \c name is found (but there is no exact match)
  226. /// - \c result::NOTFOUND: For all other cases.
  227. /// - \c zone: A <Boost> shared pointer to the found \c Zone object if one
  228. // is found; otherwise \c NULL.
  229. ///
  230. /// This method never throws an exception.
  231. ///
  232. /// \param name A domain name for which the search is performed.
  233. /// \return A \c FindResult object enclosing the search result (see above).
  234. FindResult findZone(const isc::dns::Name& name) const;
  235. private:
  236. class MemoryDataSrcImpl;
  237. MemoryDataSrcImpl* impl_;
  238. };
  239. }
  240. }
  241. #endif // __DATA_SOURCE_MEMORY_H
  242. // Local Variables:
  243. // mode: c++
  244. // End: