auth_srv.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. // Copyright (C) 2009 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. // $Id$
  15. #ifndef __AUTH_SRV_H
  16. #define __AUTH_SRV_H 1
  17. #include <string>
  18. // For MemoryDataSrcPtr below. This should be a temporary definition until
  19. // we reorganize the data source framework.
  20. #include <boost/shared_ptr.hpp>
  21. #include <cc/data.h>
  22. #include <config/ccsession.h>
  23. #include <asiolink/asiolink.h>
  24. #include <auth/statistics.h>
  25. namespace isc {
  26. namespace datasrc {
  27. class MemoryDataSrc;
  28. }
  29. namespace xfr {
  30. class AbstractXfroutClient;
  31. }
  32. }
  33. /// \brief The implementation class for the \c AuthSrv class using the pimpl
  34. /// idiom.
  35. class AuthSrvImpl;
  36. /// \brief The authoritative nameserver class.
  37. ///
  38. /// \c AuthSrv is a concrete class that implements authoritative DNS server
  39. /// protocol processing.
  40. /// An \c AuthSrv object is primarily responsible for handling incoming DNS
  41. /// requests: It parses the request and dispatches subsequent processing to
  42. /// the corresponding module (which may be an internal library or a separate
  43. /// process) depending on the request type. For normal queries, the
  44. /// \c AuthSrv object searches configured data sources for the answer to the
  45. /// query, and builds a response containing the answer.
  46. ///
  47. /// This class uses the "pimpl" idiom, and hides detailed implementation
  48. /// through the \c impl_ pointer (which points to an instance of the
  49. /// \c AuthSrvImpl class). An \c AuthSrv object is supposed to exist for quite
  50. /// a long period, and only a few \c AuthSrv objects will be created (in fact,
  51. /// in this current implementation there will only be one object), so the
  52. /// construction overhead of this approach should be acceptable.
  53. ///
  54. /// The design of this class is still in flux. It's quite likely to change
  55. /// in future versions.
  56. ///
  57. class AuthSrv {
  58. ///
  59. /// \name Constructors, Assignment Operator and Destructor.
  60. ///
  61. /// Note: The copy constructor and the assignment operator are
  62. /// intentionally defined as private.
  63. //@{
  64. private:
  65. AuthSrv(const AuthSrv& source);
  66. AuthSrv& operator=(const AuthSrv& source);
  67. public:
  68. /// The constructor.
  69. ///
  70. /// \param use_cache Whether to enable hot spot cache for lookup results.
  71. /// \param xfrout_client Communication interface with a separate xfrout
  72. /// process. It's normally a reference to an xfr::XfroutClient object,
  73. /// but can refer to a local mock object for testing (or other
  74. /// experimental) purposes.
  75. AuthSrv(const bool use_cache,
  76. isc::xfr::AbstractXfroutClient& xfrout_client);
  77. ~AuthSrv();
  78. //@}
  79. /// \brief Process an incoming DNS message, then signal 'server' to resume
  80. ///
  81. /// A DNS query (or other message) has been received by a \c DNSServer
  82. /// object. Find an answer, then post the \c DNSServer object on the
  83. /// I/O service queue and return. When the server resumes, it can
  84. /// send the reply.
  85. ///
  86. /// \param io_message The raw message received
  87. /// \param message Pointer to the \c Message object
  88. /// \param buffer Pointer to an \c OutputBuffer for the resposne
  89. /// \param server Pointer to the \c DNSServer
  90. ///
  91. /// \throw isc::Unexpected Protocol type of \a message is unexpected
  92. void processMessage(const asiolink::IOMessage& io_message,
  93. isc::dns::MessagePtr message,
  94. isc::dns::OutputBufferPtr buffer,
  95. asiolink::DNSServer* server);
  96. /// \brief Set verbose flag
  97. ///
  98. /// \param on The new value of the verbose flag
  99. /// \brief Enable or disable verbose logging.
  100. ///
  101. /// This method never throws an exception.
  102. ///
  103. /// \param on \c true to enable verbose logging; \c false to disable
  104. /// verbose logging.
  105. void setVerbose(const bool on);
  106. /// \brief Returns the logging verbosity of the \c AuthSrv object.
  107. ///
  108. /// This method never throws an exception.
  109. ///
  110. /// \return \c true if verbose logging is enabled; otherwise \c false.
  111. /// \brief Get the current value of the verbose flag
  112. bool getVerbose() const;
  113. /// \brief Updates the data source for the \c AuthSrv object.
  114. ///
  115. /// This method installs or replaces the data source that the \c AuthSrv
  116. /// object refers to for query processing.
  117. /// Although the method name is generic, the only thing it does is to
  118. /// update the data source information.
  119. /// If there is a data source installed, it will be replaced with the
  120. /// new one.
  121. ///
  122. /// In the current implementation, the SQLite data source and MemoryDataSrc
  123. /// are assumed.
  124. /// We can enable memory data source and get the path of SQLite database by
  125. /// the \c config parameter. If we disabled memory data source, the SQLite
  126. /// data source will be used.
  127. ///
  128. /// On success this method returns a data \c Element (in the form of a
  129. /// pointer like object) indicating the successful result,
  130. /// i.e., {"result": [0]}.
  131. /// Otherwise, it returns a data \c Element explaining the error:
  132. /// {"result": [1, <error-description>]}.
  133. ///
  134. /// This method is mostly exception free (error conditions are represented
  135. /// via the return value). But it may still throw a standard exception
  136. /// if memory allocation fails inside the method.
  137. /// When a standard exception is thrown or an implementation specific
  138. /// exception is triggered and caught internally, this function provides
  139. /// the strong exception guarantee: Unless everything succeeds, currently
  140. /// installed data source (if any) won't be replaced.
  141. ///
  142. /// \param config An immutable pointer-like object to a data \c Element,
  143. /// possibly containing the data source information to be used.
  144. /// \return An immutable pointer-like object to a data \c Element
  145. /// containing the result of the update operation.
  146. isc::data::ConstElementPtr updateConfig(isc::data::ConstElementPtr config);
  147. /// \brief Returns the command and configuration session for the
  148. /// \c AuthSrv.
  149. ///
  150. /// This method never throws an exception.
  151. ///
  152. /// \return A pointer to \c ModuleCCSession object stored in the
  153. /// \c AuthSrv object. In this implementation it could be NULL.
  154. isc::config::ModuleCCSession* getConfigSession() const;
  155. /// \brief Set the command and configuration session for the \c AuthSrv.
  156. ///
  157. /// Note: this interface is tentative. We'll revisit the ASIO and session
  158. /// frameworks, at which point the session will probably be passed on
  159. /// construction of the server.
  160. /// In the current implementation, this method is expected to be called
  161. /// exactly once as part of initialization. If this method is called
  162. /// multiple times, previously specified session is silently overridden.
  163. ///
  164. /// This method never throws an exception.
  165. ///
  166. /// \param config_session A pointer to \c ModuleCCSession object to receive
  167. /// control commands and configuration updates.
  168. void setConfigSession(isc::config::ModuleCCSession* config_session);
  169. /// \brief Assign an ASIO IO Service queue to this Recursor object
  170. void setIOService(asiolink::IOService& ios) { io_service_ = &ios; }
  171. /// \brief Return this object's ASIO IO Service queue
  172. asiolink::IOService& getIOService() const { return (*io_service_); }
  173. /// \brief Return pointer to the DNS Lookup callback function
  174. asiolink::DNSLookup* getDNSLookupProvider() const { return (dns_lookup_); }
  175. /// \brief Return pointer to the DNS Answer callback function
  176. asiolink::DNSAnswer* getDNSAnswerProvider() const { return (dns_answer_); }
  177. /// \brief Return pointer to the Checkin callback function
  178. asiolink::SimpleCallback* getCheckinProvider() const { return (checkin_); }
  179. /// \brief Set or update the size (number of slots) of hot spot cache.
  180. ///
  181. /// If the specified size is 0, it means the size will be unlimited.
  182. /// The specified size is recorded even if the cache is disabled; the
  183. /// new size will be effective when the cache is enabled.
  184. ///
  185. /// This method never throws an exception.
  186. ///
  187. /// \param slots The number of cache slots.
  188. void setCacheSlots(const size_t slots);
  189. /// \brief Get the current size (number of slots) of hot spot cache.
  190. ///
  191. /// It always returns the recorded size regardless of the cache is enabled.
  192. ///
  193. /// This method never throws an exception.
  194. ///
  195. /// \return The current number of cache slots.
  196. size_t getCacheSlots() const;
  197. /// \brief Set the communication session with a separate process for
  198. /// outgoing zone transfers.
  199. ///
  200. /// Note: this interface is tentative. We'll revisit the ASIO and session
  201. /// frameworks, at which point the session will probably be passed on
  202. /// construction of the server.
  203. ///
  204. /// \param xfrin_session A Session object over which NOTIFY message
  205. /// information is exchanged with a XFRIN handler.
  206. /// The session must be established before setting in the server
  207. /// object.
  208. /// Ownership isn't transferred: the caller is responsible for keeping
  209. /// this object to be valid while the server object is working and for
  210. /// disconnecting the session and destroying the object when the server
  211. /// is shutdown.
  212. ///
  213. void setXfrinSession(isc::cc::AbstractSession* xfrin_session);
  214. /// A shared pointer type for \c MemoryDataSrc.
  215. ///
  216. /// This is defined inside the \c AuthSrv class as it's supposed to be
  217. /// a short term interface until we integrate the in-memory and other
  218. /// data source frameworks.
  219. typedef boost::shared_ptr<isc::datasrc::MemoryDataSrc> MemoryDataSrcPtr;
  220. /// An immutable shared pointer type for \c MemoryDataSrc.
  221. typedef boost::shared_ptr<const isc::datasrc::MemoryDataSrc>
  222. ConstMemoryDataSrcPtr;
  223. /// Returns the in-memory data source configured for the \c AuthSrv,
  224. /// if any.
  225. ///
  226. /// The in-memory data source is configured per RR class. However,
  227. /// the data source may not be available for all RR classes.
  228. /// If it is not available for the specified RR class, an exception of
  229. /// class \c InvalidParameter will be thrown.
  230. /// This method never throws an exception otherwise.
  231. ///
  232. /// Even for supported RR classes, the in-memory data source is not
  233. /// configured by default. In that case a NULL (shared) pointer will
  234. /// be returned.
  235. ///
  236. /// \param rrclass The RR class of the requested in-memory data source.
  237. /// \return A pointer to the in-memory data source, if configured;
  238. /// otherwise NULL.
  239. ConstMemoryDataSrcPtr
  240. getMemoryDataSrc(const isc::dns::RRClass& rrclass) const;
  241. /// Sets or replaces the in-memory data source of the specified RR class.
  242. ///
  243. /// As noted in \c getMemoryDataSrc(), some RR classes may not be
  244. /// supported, in which case an exception of class \c InvalidParameter
  245. /// will be thrown.
  246. /// This method never throws an exception otherwise.
  247. ///
  248. /// If there is already an in memory data source configured, it will be
  249. /// replaced with the newly specified one.
  250. /// \c memory_datasrc can be NULL, in which case it will (re)disable the
  251. /// in-memory data source.
  252. ///
  253. /// \param rrclass The RR class of the in-memory data source to be set.
  254. /// \param memory_datasrc A (shared) pointer to \c MemoryDataSrc to be set.
  255. void setMemoryDataSrc(const isc::dns::RRClass& rrclass,
  256. MemoryDataSrcPtr memory_datasrc);
  257. /// \brief Set the communication session with Statistics.
  258. ///
  259. /// This function never throws an exception as far as
  260. /// AuthCounters::setStatisticsSession() doesn't throw.
  261. ///
  262. /// Note: this interface is tentative. We'll revisit the ASIO and
  263. /// session frameworks, at which point the session will probably
  264. /// be passed on construction of the server.
  265. ///
  266. /// \param statistics_session A Session object over which statistics
  267. /// information is exchanged with statistics module.
  268. /// The session must be established before setting in the server
  269. /// object.
  270. /// Ownership isn't transferred: the caller is responsible for keeping
  271. /// this object to be valid while the server object is working and for
  272. /// disconnecting the session and destroying the object when the server
  273. /// is shutdown.
  274. void setStatisticsSession(isc::cc::AbstractSession* statistics_session);
  275. /// \brief Submit statistics counters to statistics module.
  276. ///
  277. /// This function can throw an exception from
  278. /// AuthCounters::submitStatistics().
  279. ///
  280. /// \return true on success, false on failure (e.g. session timeout,
  281. /// session error).
  282. bool submitStatistics() const;
  283. /// \brief Get the value of counter in the AuthCounters.
  284. ///
  285. /// This function calls AuthCounters::getCounter() and
  286. /// returns its return value.
  287. ///
  288. /// This function never throws an exception as far as
  289. /// AuthCounters::getCounter() doesn't throw.
  290. ///
  291. /// Note: Currently this function is for testing purpose only.
  292. ///
  293. /// \param type Type of a counter to get the value of
  294. ///
  295. /// \return the value of the counter.
  296. uint64_t getCounter(const AuthCounters::CounterType type) const;
  297. private:
  298. AuthSrvImpl* impl_;
  299. asiolink::IOService* io_service_;
  300. asiolink::SimpleCallback* checkin_;
  301. asiolink::DNSLookup* dns_lookup_;
  302. asiolink::DNSAnswer* dns_answer_;
  303. };
  304. #endif // __AUTH_SRV_H
  305. // Local Variables:
  306. // mode: c++
  307. // End: