library_handle.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. // Copyright (C) 2013 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 LIBRARY_HANDLE_H
  15. #define LIBRARY_HANDLE_H
  16. #include <map>
  17. #include <string>
  18. #include <boost/any.hpp>
  19. #include <boost/shared_ptr.hpp>
  20. #include <exceptions/exceptions.h>
  21. #include <util/hooks/server_hooks.h>
  22. namespace isc {
  23. namespace util {
  24. /// @brief No such hook
  25. ///
  26. /// Thrown if an attempt is made to use an invalid hook name or hook index.
  27. class NoSuchHook : public Exception {
  28. public:
  29. NoSuchHook(const char* file, size_t line, const char* what) :
  30. isc::Exception(file, line, what) {}
  31. };
  32. /// @brief No Such Context
  33. ///
  34. /// Thrown if an attempt is made to obtain context that has not been previously
  35. /// set.
  36. class NoSuchContext : public Exception {
  37. public:
  38. NoSuchContext(const char* file, size_t line, const char* what) :
  39. isc::Exception(file, line, what) {}
  40. };
  41. // Forward declaration for CalloutHandle
  42. class CalloutHandle;
  43. /// Typedef for a callout pointer
  44. extern "C" {
  45. typedef int (*CalloutPtr)(CalloutHandle&);
  46. };
  47. /// @brief Library handle
  48. ///
  49. /// This class is used to manage a loaded library. It is used by the user
  50. /// library to register callouts and by the HookManager to call them. The
  51. /// class also contains storage for library-specific context.
  52. ///
  53. /// Although there is a persuasive argument for the class to load unload the
  54. /// user library, that is handled by the HookManager to prevent the user library
  55. /// from accessing those functions.
  56. class LibraryHandle {
  57. private:
  58. /// Typedef to allow abbreviation of iterator specification in methods
  59. typedef std::map<std::string, boost::any> ContextCollection;
  60. public:
  61. /// @brief Constructor
  62. ///
  63. /// This is passed the ServerHooks object and an index number: the former
  64. /// allows for the sizing of the internal hook vector, and the latter
  65. /// is used by the CalloutHandle object to access appropriate context
  66. ///
  67. /// @param hooks Pointer to the hooks registered by the server.
  68. LibraryHandle(boost::shared_ptr<ServerHooks>& hooks)
  69. : context_(), hooks_(hooks), hook_vector_(hooks->getCount())
  70. {}
  71. /// @brief Set context
  72. ///
  73. /// Sets an element in the library context. If an element of the name
  74. /// is already present, it is replaced.
  75. ///
  76. /// @param name Name of the element in the context to set
  77. /// @param value Value to set
  78. template <typename T>
  79. void setContext(const std::string& name, T value) {
  80. context_[name] = value;
  81. }
  82. /// @brief Get context
  83. ///
  84. /// Sets an element in the library context. If the name does not exist,
  85. /// a "NoSuchContext" exception is thrown.
  86. ///
  87. /// @param name Name of the element in the context to set.
  88. /// @param value [out] Value to set. The type of "value" is important:
  89. /// it must match the type of the value set.
  90. ///
  91. /// @throw NoSuchContext Thrown if no context element with the name
  92. /// "name" is present.
  93. /// @throw boost::bad_any_cast Thrown if the context element is present,
  94. /// but the type of the element is not that expected
  95. template <typename T>
  96. void getContext(const std::string& name, T& value) const {
  97. ContextCollection::const_iterator element_ptr = context_.find(name);
  98. if (element_ptr == context_.end()) {
  99. isc_throw(NoSuchContext, "unable to find library context datum " <<
  100. name << " in library handle");
  101. }
  102. value = boost::any_cast<T>(element_ptr->second);
  103. }
  104. /// @brief Get context names
  105. ///
  106. /// Returns a vector holding the names of context items.
  107. ///
  108. /// @return Vector of strings reflecting argument names
  109. std::vector<std::string> getContextNames() const;
  110. /// @brief Delete context element
  111. ///
  112. /// Deletes context item of the given name. If an item of that name
  113. /// does not exist, the method is a no-op.
  114. ///
  115. /// N.B. If the element is a raw pointer, the pointed-to data is
  116. /// NOT deleted by this.
  117. ///
  118. /// @param name Name of the element in the argument list to set.
  119. void deleteContext(const std::string& name) {
  120. static_cast<void>(context_.erase(name));
  121. }
  122. /// @brief Delete all arguments
  123. ///
  124. /// Deletes all arguments associated with this context.
  125. ///
  126. /// N.B. If any elements are raw pointers, the pointed-to data is
  127. /// NOT deleted by this.
  128. void deleteAllContext() {
  129. context_.clear();
  130. }
  131. /// @brief Register a callout
  132. ///
  133. /// Registers a callout function with a given hook. The callout is added
  134. /// to the end of the callouts associated with the hook.
  135. ///
  136. /// @param name Name of the hook to which the callout is added.
  137. /// @param callout Pointer to the callout function to be registered.
  138. ///
  139. /// @throw NoSuchHook Thrown if the hook name is unrecognised.
  140. /// @throw Unexpected Hooks name is valid but internal data structure is
  141. /// of the wrong size.
  142. void registerCallout(const std::string& name, CalloutPtr callout);
  143. /// @brief De-Register a callout
  144. ///
  145. /// Searches through the functions associated with the named hook and
  146. /// removes all entries matching the callout. If there are no matching
  147. /// callouts, the result is a no-op.
  148. ///
  149. /// @param name Name of the hook from which the callout is removed.
  150. /// @param callout Pointer to the callout function to be removed.
  151. ///
  152. /// @throw NoSuchHook Thrown if the hook name is unrecognised.
  153. /// @throw Unexpected Hooks name is valid but internal data structure is
  154. /// of the wrong size.
  155. void deregisterCallout(const std::string& name, CalloutPtr callout);
  156. /// @brief Removes all callouts
  157. ///
  158. /// Removes all callouts associated with a given hook. This is a no-op
  159. /// if there are no callouts associated with the hook.
  160. ///
  161. /// @param name Name of the hook from which the callouts are removed.
  162. ///
  163. /// @throw NoSuchHook Thrown if the hook name is unrecognised.
  164. void deregisterAll(const std::string& name);
  165. /// @brief Checks if callouts are present
  166. ///
  167. /// @param index Hook index for which callouts are checked.
  168. ///
  169. /// @return true if callouts are present, false if not.
  170. ///
  171. /// @throw NoSuchHook Thrown if the index is not valid.
  172. bool calloutsPresent(int index) const;
  173. /// @brief Calls the callouts for a given hook
  174. ///
  175. /// Calls the callouts associated with the given hook index.
  176. ///
  177. /// @param index Index of the hook to call.
  178. /// @param callout_handle Reference to the CalloutHandle object for the
  179. /// current object being processed.
  180. ///
  181. /// @return Status return.
  182. ///
  183. int callCallouts(int index, CalloutHandle& callout_handle);
  184. private:
  185. /// @brief Check hook index
  186. ///
  187. /// Checks that the hook index is valid for the hook vector. If not,
  188. /// an exception is thrown.
  189. ///
  190. /// @param index Hooks index to check.
  191. ///
  192. /// @throw NoSuchHook Thrown if the index is not valid for the hook vector.
  193. void checkHookIndex(int index) const;
  194. /// @brief Get hook index
  195. ///
  196. /// Utility function to return the index associated with a hook name. It
  197. /// also checks for validity of the index: if the name is valid, the
  198. /// index should be valid. However, as the class only keeps a pointer to
  199. /// a shared ServerHooks object, it is possible that the object was modified
  200. /// after the hook_vector_ was sized. This function performs some checks
  201. /// on the name and throws an exception if the checks fail.
  202. ///
  203. /// @param name Name of the hook to check
  204. ///
  205. /// @return Index of the hook in the hook_vector_
  206. ///
  207. /// @throw NoSuchHook Thrown if the hook name is unrecognised.
  208. /// @throw Unexpected Index not valid for the hook vector.
  209. int getHookIndex(const std::string& name) const;
  210. // Member variables
  211. /// Context - mapping of names variables that can be of different types.
  212. ContextCollection context_;
  213. /// Pointer to the list of hooks registered by the server
  214. boost::shared_ptr<ServerHooks> hooks_; ///< Pointer to hooks
  215. /// Each element in the following vector corresponds to a single hook and
  216. /// is an ordered list of callouts for that hook.
  217. std::vector<std::vector<CalloutPtr> > hook_vector_;
  218. };
  219. /// @brief Collection of Library Handles
  220. ///
  221. /// This simple class is a collection of handles for all libraries loaded.
  222. /// It is pointed to by the CalloutHandle object and is used by that object
  223. /// to locate the correct LibraryHandle to pass to a callout function. To
  224. /// do this, the class contains an index indicating the "current" handle. This
  225. /// is used in the calling of callouts: prior to calling a callout associated
  226. /// with a LibraryHandle, the index is updated to point to that handle.
  227. /// If the callout requests access to the LibraryHandle, it is passed a
  228. /// reference to the correct one.
  229. class LibraryHandleCollection {
  230. private:
  231. /// Private typedef to abbreviate statements in class methods.
  232. typedef std::vector<boost::shared_ptr<LibraryHandle> > HandleVector;
  233. public:
  234. /// @brief Constructor
  235. ///
  236. /// Initializes member variables, in particular setting the current index
  237. /// to an invalid value.
  238. LibraryHandleCollection() : curidx_(-1), handles_()
  239. {}
  240. /// @brief Add library handle
  241. ///
  242. /// Adds a library handle to the collection. The collection is ordered,
  243. /// and this adds a library handle to the end of the current list.
  244. ///
  245. /// @param library_handle Pointer to the a library handle to be added.
  246. void addLibraryHandle(boost::shared_ptr<LibraryHandle>& handle) {
  247. handles_.push_back(handle);
  248. }
  249. /// @brief Get current library handle
  250. ///
  251. /// Returns a pointer to the current library handle. This method can
  252. /// only be called while the code is iterating through the list of
  253. /// library handles: calling it at any other time is meaningless and will
  254. /// cause an exception to be thrown.
  255. ///
  256. /// @return Pointer to current library handle. This is the handle for
  257. /// which a callout is being called.
  258. boost::shared_ptr<LibraryHandle> getLibraryHandle() const {
  259. return (boost::shared_ptr<LibraryHandle>(curidx_));
  260. }
  261. /// @brief Checks if callouts are present
  262. ///
  263. /// Checks all loaded libraries and returns true if at least one callout
  264. /// has been registered for a hook in any of them.
  265. ///
  266. /// @param index Hook index for which callouts are checked.
  267. ///
  268. /// @return true if callouts are present, false if not.
  269. ///
  270. /// @throw NoSuchHook Thrown if the index is not valid.
  271. bool calloutsPresent(int index) const;
  272. /// @brief Calls the callouts for a given hook
  273. ///
  274. /// Iterates through the libray handles and calls the callouts associated
  275. /// with the given hook index.
  276. ///
  277. /// @param index Index of the hook to call.
  278. /// @param callout_handle Reference to the CalloutHandle object for the
  279. /// current object being processed.
  280. ///
  281. /// @return Status return.
  282. ///
  283. int callCallouts(int index, CalloutHandle& callout_handle);
  284. private:
  285. /// Index of the current library handle during iteration.
  286. int curidx_;
  287. /// Vector of pointers to library handles.
  288. HandleVector handles_;
  289. };
  290. } // namespace util
  291. } // namespace isc
  292. #endif // LIBRARY_HANDLE_H