load_callout_library.cc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. /// @file
  7. /// @brief Basic library with load() function
  8. ///
  9. /// This is source of a test library for various test (LibraryManager and
  10. /// HooksManager). The characteristics of the library produced from this
  11. /// file are:
  12. ///
  13. /// - The "version" and "load" framework functions are supplied. One "standard"
  14. /// callout is supplied ("hookpt_one") and two non-standard ones which are
  15. /// registered during the call to "load" on the hooks "hookpt_two" and
  16. /// "hookpt_three".
  17. ///
  18. /// All callouts do trivial calculations, the result of all being called in
  19. /// sequence being
  20. ///
  21. /// @f[ ((5 * data_1) + data_2) * data_3 @f]
  22. ///
  23. /// ...where data_1, data_2 and data_3 are the values passed in arguments of
  24. /// the same name to the three callouts (data_1 passed to hookpt_one, data_2
  25. /// to hookpt_two etc.) and the result is returned in the argument "result".
  26. #include <config.h>
  27. #include <hooks/hooks.h>
  28. using namespace isc::hooks;
  29. extern "C" {
  30. // Callouts
  31. int
  32. context_create(CalloutHandle& handle) {
  33. handle.setContext("result", static_cast<int>(5));
  34. handle.setArgument("result", static_cast<int>(5));
  35. return (0);
  36. }
  37. // First callout adds the passed "data_1" argument to the initialized context
  38. // value of 5. (Note that the value set by context_create is accessed through
  39. // context and not the argument, so checking that context is correctly passed
  40. // between callouts in the same library.)
  41. int
  42. hookpt_one(CalloutHandle& handle) {
  43. int data;
  44. handle.getArgument("data_1", data);
  45. int result;
  46. handle.getContext("result", result);
  47. result *= data;
  48. handle.setArgument("result", result);
  49. return (0);
  50. }
  51. // Second callout multiplies the current context value by the "data_2"
  52. // argument.
  53. static int
  54. hook_nonstandard_two(CalloutHandle& handle) {
  55. int data;
  56. handle.getArgument("data_2", data);
  57. int result;
  58. handle.getArgument("result", result);
  59. result += data;
  60. handle.setArgument("result", result);
  61. return (0);
  62. }
  63. // Final callout adds "data_3" to the result.
  64. static int
  65. hook_nonstandard_three(CalloutHandle& handle) {
  66. int data;
  67. handle.getArgument("data_3", data);
  68. int result;
  69. handle.getArgument("result", result);
  70. result *= data;
  71. handle.setArgument("result", result);
  72. return (0);
  73. }
  74. // Framework functions
  75. int
  76. version() {
  77. return (KEA_HOOKS_VERSION);
  78. }
  79. int load(LibraryHandle& handle) {
  80. // Initialize the user library if the main image was statically linked
  81. #ifdef USE_STATIC_LINK
  82. hooksStaticLinkInit();
  83. #endif
  84. // Register the non-standard functions
  85. handle.registerCallout("hookpt_two", hook_nonstandard_two);
  86. handle.registerCallout("hookpt_three", hook_nonstandard_three);
  87. return (0);
  88. }
  89. };