callout_handle.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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 CALLOUT_HANDLE_H
  15. #define CALLOUT_HANDLE_H
  16. #include <exceptions/exceptions.h>
  17. #include <hooks/library_handle.h>
  18. #include <boost/any.hpp>
  19. #include <boost/shared_ptr.hpp>
  20. #include <map>
  21. #include <string>
  22. #include <vector>
  23. namespace isc {
  24. namespace hooks {
  25. class ServerHooks;
  26. /// @brief No such argument
  27. ///
  28. /// Thrown if an attempt is made access an argument that does not exist.
  29. class NoSuchArgument : public Exception {
  30. public:
  31. NoSuchArgument(const char* file, size_t line, const char* what) :
  32. isc::Exception(file, line, what) {}
  33. };
  34. /// @brief No such callout context item
  35. ///
  36. /// Thrown if an attempt is made to get an item of data from this callout's
  37. /// context and either the context or an item in the context with that name
  38. /// does not exist.
  39. class NoSuchCalloutContext : public Exception {
  40. public:
  41. NoSuchCalloutContext(const char* file, size_t line, const char* what) :
  42. isc::Exception(file, line, what) {}
  43. };
  44. // Forward declaration of the library handle and related collection classes.
  45. class CalloutManager;
  46. class LibraryHandle;
  47. class LibraryManagerCollection;
  48. /// @brief Per-packet callout handle
  49. ///
  50. /// An object of this class is associated with every packet (or request)
  51. /// processed by the server. It forms the principle means of passing data
  52. /// between the server and the user-library callouts.
  53. ///
  54. /// The class allows access to the following information:
  55. ///
  56. /// - Arguments. When the callouts associated with a hook are called, they
  57. /// are passed information by the server (and can return information to it)
  58. /// through name/value pairs. Each of these pairs is an argument and the
  59. /// information is accessed through the {get,set}Argument() methods.
  60. ///
  61. /// - Per-packet context. Each packet has a context associated with it, this
  62. /// context being on a per-library basis. In other words, As a packet passes
  63. /// through the callouts associated with a given library, the callouts can
  64. /// associate and retrieve information with the packet. The per-library
  65. /// nature of the context means that the callouts within a given library can
  66. /// pass packet-specific information between one another, but they cannot pass
  67. /// information to callous within another library. Typically such context
  68. /// is created in the "context_create" callout and destroyed in the
  69. /// "context_destroy" callout. The information is accessed through the
  70. /// {get,set}Context() methods.
  71. ///
  72. /// - Per-library handle (LibraryHandle). The library handle allows the
  73. /// callout to dynamically register and deregister callouts. In the latter
  74. /// case, only functions registered by functions in the same library as the
  75. /// callout doing the deregistration can be removed: callouts registered by
  76. /// other libraries cannot be modified.
  77. class CalloutHandle {
  78. public:
  79. /// Typedef to allow abbreviation of iterator specification in methods.
  80. /// The std::string is the argument name and the "boost::any" is the
  81. /// corresponding value associated with it.
  82. typedef std::map<std::string, boost::any> ElementCollection;
  83. /// Typedef to allow abbreviations in specifications when accessing
  84. /// context. The ElementCollection is the name/value collection for
  85. /// a particular context. The "int" corresponds to the index of an
  86. /// associated library - there is a 1:1 correspondence between libraries
  87. /// and a name.value collection.
  88. ///
  89. /// The collection of contexts is stored in a map, as not every library
  90. /// will require creation of a context associated with each packet. In
  91. /// addition, the structure is more flexible in that the size does not
  92. /// need to be set when the CalloutHandle is constructed.
  93. typedef std::map<int, ElementCollection> ContextCollection;
  94. /// @brief Constructor
  95. ///
  96. /// Creates the object and calls the callouts on the "context_create"
  97. /// hook.
  98. ///
  99. /// Of the two arguments passed, only the pointer to the callout manager is
  100. /// actively used. The second argument, the pointer to the library manager
  101. /// collection, is used for lifetime control: after use, the callout handle
  102. /// may contain pointers to memory allocated by the loaded libraries. The
  103. /// used of a shared pointer to the collection of library managers means
  104. /// that the libraries that could have allocated memory in a callout handle
  105. /// will not be unloaded until all such handles have been destroyed. This
  106. /// issue is discussed in more detail in the documentation for
  107. /// isc::hooks::LibraryManager.
  108. ///
  109. /// @param manager Pointer to the callout manager object.
  110. /// @param lmcoll Pointer to the library manager collection. This has a
  111. /// null default for testing purposes.
  112. CalloutHandle(const boost::shared_ptr<CalloutManager>& manager,
  113. const boost::shared_ptr<LibraryManagerCollection>& lmcoll =
  114. boost::shared_ptr<LibraryManagerCollection>());
  115. /// @brief Destructor
  116. ///
  117. /// Calls the context_destroy callback to release any per-packet context.
  118. /// It also clears stored data to avoid problems during member destruction.
  119. ~CalloutHandle();
  120. /// @brief Set argument
  121. ///
  122. /// Sets the value of an argument. The argument is created if it does not
  123. /// already exist.
  124. ///
  125. /// @param name Name of the argument.
  126. /// @param value Value to set. That can be of any data type.
  127. template <typename T>
  128. void setArgument(const std::string& name, T value) {
  129. arguments_[name] = value;
  130. }
  131. /// @brief Get argument
  132. ///
  133. /// Gets the value of an argument.
  134. ///
  135. /// @param name Name of the element in the argument list to get.
  136. /// @param value [out] Value to set. The type of "value" is important:
  137. /// it must match the type of the value set.
  138. ///
  139. /// @throw NoSuchArgument No argument with the given name is present.
  140. /// @throw boost::bad_any_cast An argument with the given name is present,
  141. /// but the data type of the value is not the same as the type of
  142. /// the variable provided to receive the value.
  143. template <typename T>
  144. void getArgument(const std::string& name, T& value) const {
  145. ElementCollection::const_iterator element_ptr = arguments_.find(name);
  146. if (element_ptr == arguments_.end()) {
  147. isc_throw(NoSuchArgument, "unable to find argument with name " <<
  148. name);
  149. }
  150. value = boost::any_cast<T>(element_ptr->second);
  151. }
  152. /// @brief Get argument names
  153. ///
  154. /// Returns a vector holding the names of arguments in the argument
  155. /// vector.
  156. ///
  157. /// @return Vector of strings reflecting argument names.
  158. std::vector<std::string> getArgumentNames() const;
  159. /// @brief Delete argument
  160. ///
  161. /// Deletes an argument of the given name. If an argument of that name
  162. /// does not exist, the method is a no-op.
  163. ///
  164. /// N.B. If the element is a raw pointer, the pointed-to data is NOT deleted
  165. /// by this method.
  166. ///
  167. /// @param name Name of the element in the argument list to set.
  168. void deleteArgument(const std::string& name) {
  169. static_cast<void>(arguments_.erase(name));
  170. }
  171. /// @brief Delete all arguments
  172. ///
  173. /// Deletes all arguments associated with this context.
  174. ///
  175. /// N.B. If any elements are raw pointers, the pointed-to data is NOT
  176. /// deleted by this method.
  177. void deleteAllArguments() {
  178. arguments_.clear();
  179. }
  180. /// @brief Set skip flag
  181. ///
  182. /// Sets the "skip" variable in the callout handle. This variable is
  183. /// interrogated by the server to see if the remaining callouts associated
  184. /// with the current hook should be bypassed.
  185. ///
  186. /// @param skip New value of the "skip" flag.
  187. void setSkip(bool skip) {
  188. skip_ = skip;
  189. }
  190. /// @brief Get skip flag
  191. ///
  192. /// Gets the current value of the "skip" flag.
  193. ///
  194. /// @return Current value of the skip flag.
  195. bool getSkip() const {
  196. return (skip_);
  197. }
  198. /// @brief Access current library handle
  199. ///
  200. /// Returns a reference to the current library handle. This function is
  201. /// only available when called by a callout (which in turn is called
  202. /// through the "callCallouts" method), as it is only then that the current
  203. /// library index is valid. A callout uses the library handle to
  204. /// dynamically register or deregister callouts.
  205. ///
  206. /// @return Reference to the library handle.
  207. ///
  208. /// @throw InvalidIndex thrown if this method is called when the current
  209. /// library index is invalid (typically if it is called outside of
  210. /// the active callout).
  211. LibraryHandle& getLibraryHandle() const;
  212. /// @brief Set context
  213. ///
  214. /// Sets an element in the context associated with the current library. If
  215. /// an element of the name is already present, it is replaced.
  216. ///
  217. /// @param name Name of the element in the context to set.
  218. /// @param value Value to set.
  219. template <typename T>
  220. void setContext(const std::string& name, T value) {
  221. getContextForLibrary()[name] = value;
  222. }
  223. /// @brief Get context
  224. ///
  225. /// Gets an element from the context associated with the current library.
  226. ///
  227. /// @param name Name of the element in the context to get.
  228. /// @param value [out] Value to set. The type of "value" is important:
  229. /// it must match the type of the value set.
  230. ///
  231. /// @throw NoSuchCalloutContext Thrown if no context element with the name
  232. /// "name" is present.
  233. /// @throw boost::bad_any_cast Thrown if the context element is present
  234. /// but the type of the data is not the same as the type of the
  235. /// variable provided to receive its value.
  236. template <typename T>
  237. void getContext(const std::string& name, T& value) const {
  238. const ElementCollection& lib_context = getContextForLibrary();
  239. ElementCollection::const_iterator element_ptr = lib_context.find(name);
  240. if (element_ptr == lib_context.end()) {
  241. isc_throw(NoSuchCalloutContext, "unable to find callout context "
  242. "item " << name << " in the context associated with "
  243. "current library");
  244. }
  245. value = boost::any_cast<T>(element_ptr->second);
  246. }
  247. /// @brief Get context names
  248. ///
  249. /// Returns a vector holding the names of items in the context associated
  250. /// with the current library.
  251. ///
  252. /// @return Vector of strings reflecting the names of items in the callout
  253. /// context associated with the current library.
  254. std::vector<std::string> getContextNames() const;
  255. /// @brief Delete context element
  256. ///
  257. /// Deletes an item of the given name from the context associated with the
  258. /// current library. If an item of that name does not exist, the method is
  259. /// a no-op.
  260. ///
  261. /// N.B. If the element is a raw pointer, the pointed-to data is NOT deleted
  262. /// by this.
  263. ///
  264. /// @param name Name of the context item to delete.
  265. void deleteContext(const std::string& name) {
  266. static_cast<void>(getContextForLibrary().erase(name));
  267. }
  268. /// @brief Delete all context items
  269. ///
  270. /// Deletes all items from the context associated with the current library.
  271. ///
  272. /// N.B. If any elements are raw pointers, the pointed-to data is NOT
  273. /// deleted by this.
  274. void deleteAllContext() {
  275. getContextForLibrary().clear();
  276. }
  277. /// @brief Get hook name
  278. ///
  279. /// Get the name of the hook to which the current callout is attached.
  280. /// This can be the null string if the CalloutHandle is being accessed
  281. /// outside of the CalloutManager's "callCallouts" method.
  282. ///
  283. /// @return Name of the current hook or the empty string if none.
  284. std::string getHookName() const;
  285. private:
  286. /// @brief Check index
  287. ///
  288. /// Gets the current library index, throwing an exception if it is not set
  289. /// or is invalid for the current library collection.
  290. ///
  291. /// @return Current library index, valid for this library collection.
  292. ///
  293. /// @throw InvalidIndex current library index is not valid for the library
  294. /// handle collection.
  295. int getLibraryIndex() const;
  296. /// @brief Return reference to context for current library
  297. ///
  298. /// Called by all context-setting functions, this returns a reference to
  299. /// the callout context for the current library, creating a context if it
  300. /// does not exist.
  301. ///
  302. /// @return Reference to the collection of name/value pairs associated
  303. /// with the current library.
  304. ///
  305. /// @throw InvalidIndex current library index is not valid for the library
  306. /// handle collection.
  307. ElementCollection& getContextForLibrary();
  308. /// @brief Return reference to context for current library (const version)
  309. ///
  310. /// Called by all context-accessing functions, this a reference to the
  311. /// callout context for the current library. An exception is thrown if
  312. /// it does not exist.
  313. ///
  314. /// @return Reference to the collection of name/value pairs associated
  315. /// with the current library.
  316. ///
  317. /// @throw NoSuchCalloutContext Thrown if there is no ElementCollection
  318. /// associated with the current library.
  319. const ElementCollection& getContextForLibrary() const;
  320. // Member variables
  321. /// Pointer to the collection of libraries for which this handle has been
  322. /// created.
  323. boost::shared_ptr<LibraryManagerCollection> lm_collection_;
  324. /// Collection of arguments passed to the callouts
  325. ElementCollection arguments_;
  326. /// Context collection - there is one entry per library context.
  327. ContextCollection context_collection_;
  328. /// Callout manager.
  329. boost::shared_ptr<CalloutManager> manager_;
  330. /// Reference to the singleton ServerHooks object. See the
  331. /// @ref hooksmgMaintenanceGuide for information as to why the class holds
  332. /// a reference instead of accessing the singleton within the code.
  333. ServerHooks& server_hooks_;
  334. /// "Skip" flag, indicating if the caller should bypass remaining callouts.
  335. bool skip_;
  336. };
  337. /// A shared pointer to a CalloutHandle object.
  338. typedef boost::shared_ptr<CalloutHandle> CalloutHandlePtr;
  339. } // namespace hooks
  340. } // namespace isc
  341. #endif // CALLOUT_HANDLE_H