load_callout_library.cc 3.3 KB

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