callout_handle.h 16 KB

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