callout_handle.h 14 KB

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