memory_datasrc.h 11 KB

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