auth_srv.h 15 KB

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