---
 lib/Makefile.am |   2 +
 lib/hook.c      |  40 ++++++++++++++++++
 lib/hook.h      | 126 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 168 insertions(+)
 create mode 100644 lib/hook.c
 create mode 100644 lib/hook.h

diff --git a/lib/Makefile.am b/lib/Makefile.am
index 1caa7d9..6ba1724 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -17,6 +17,7 @@ libzebra_la_SOURCES = \
        memory.c \
        memory_vty.c \
        plugin.c \
+       hook.c \
        # end
 
 BUILT_SOURCES = route_types.h gitversion.h
@@ -35,6 +36,7 @@ pkginclude_HEADERS = \
        workqueue.h route_types.h libospf.h vrf.h fifo.h \
        memory_vty.h \
        plugin.h \
+       hook.h \
        # end
 
 noinst_HEADERS = \
diff --git a/lib/hook.c b/lib/hook.c
new file mode 100644
index 0000000..f30c766
--- /dev/null
+++ b/lib/hook.c
@@ -0,0 +1,40 @@
+/*
+ * Copyright (c) 2016  David Lamparter, for NetDEF, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#include "memory.h"
+#include "hook.h"
+
+DEFINE_MTYPE_STATIC(LIB, HOOK_ENTRY, "Hook entry")
+
+void _hook_register(struct hook *hook, void *funcptr, void *arg,
+                  struct qplug_runtime *plugin, const char *funcname)
+{
+       struct hookent *he = XCALLOC(MTYPE_TMP, sizeof(*he));
+       he->hookfn = funcptr;
+       he->hookarg = arg;
+       he->plugin = plugin;
+       he->fnname = funcname;
+
+       he->next = hook->entries;
+       hook->entries = he;
+}
+
diff --git a/lib/hook.h b/lib/hook.h
new file mode 100644
index 0000000..94dd083
--- /dev/null
+++ b/lib/hook.h
@@ -0,0 +1,126 @@
+/*
+ * Copyright (c) 2016  David Lamparter, for NetDEF, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#ifndef _QUAGGA_HOOK_H
+#define _QUAGGA_HOOK_H
+
+#include "plugin.h"
+#include "memory.h"
+
+/* type-safe subscribable hook points
+ *
+ * where "type-safe" applies to the function pointers used for subscriptions
+ *
+ * overall usage:
+ *
+ *   mydaemon.h:
+ *     DECLARE_HOOK (some_update_event, (struct eventinfo *info), (info))
+ *
+ *   mydaemon.c:
+ *     DEFINE_HOOK (some_update_event, (struct eventinfo *info), (info))
+ *     ...
+ *     hook_call (some_update_event, info)
+ *
+ *   myaddon.c:
+ *     #include "mydaemon.h"
+ *     void event_handler (void *addonptr, struct eventinfo *info) { ... }
+ *     ...
+ *     hook_register (some_update_event, event_handler, addonptr);
+ */
+
+/* TODO:
+ * - hook_unregister()
+ * - hook_unregister_all_plugin()
+ * - introspection / CLI / debug
+ * - testcases ;)
+ */
+
+struct hookent {
+       struct hookent *next;
+       void *hookfn;           /* actually a function pointer */
+       void *hookarg;
+       struct qplug_runtime *plugin;
+       const char *fnname;
+};
+
+struct hook {
+       const char *name;
+       struct hookent *entries;
+};
+
+/* subscribe/add callback function to a hook
+ *
+ * always use hook_register(), which uses the static inline helper from
+ * DECLARE_HOOK in order to get type safety
+ */
+extern void _hook_register(struct hook *hook, void *funcptr, void *arg,
+                          struct qplug_runtime *plugin, const char *funcname);
+#define hook_register(hookname, func, argptr) \
+       _hook_reg_ ## hookname (func, argptr, THIS_PLUGIN, #func)
+
+/* invoke hooks
+ * this is private (static) to the file that has the DEFINE_HOOK statement
+ */
+#define hook_call(hookname, ...) \
+       hook_call_ ## hookname (__VA_ARGS__)
+
+/* helpers to add the void * arg */
+#define HOOK_ADDDEF(...) (void *hookarg , ## __VA_ARGS__)
+#define HOOK_ADDARG(...) (hookarg , ## __VA_ARGS__)
+
+/* use in header file - declares the hook and its arguments
+ * usage:  DECLARE_HOOK(my_hook, (int arg1, struct foo *arg2), (arg1, arg2))
+ * "passlist" must use the same order and same names as "arglist"
+ */
+#define DECLARE_HOOK(hookname, arglist, passlist) \
+       extern struct hook _hook_ ## hookname; \
+       static inline void _hook_reg_ ## hookname ( \
+                       void (*funcptr) HOOK_ADDDEF arglist, \
+                       void *arg, struct qplug_runtime *plugin, \
+                       const char *funcname) { \
+               _hook_register(&_hook_ ## hookname, (void *)funcptr, arg, \
+                               plugin, funcname); \
+       }
+
+/* use in source file - contains hook-related definitions.
+ * use with exact same parameters as DECLARE_HOOK
+ */
+#define DEFINE_HOOK(hookname, arglist, passlist) \
+       struct hook _hook_ ## hookname = { \
+               .name = #hookname, \
+               .entries = NULL, \
+       }; \
+       static void hook_call_ ## hookname arglist { \
+               struct hookent *he = _hook_ ## hookname .entries; \
+               void *hookarg; \
+               union { \
+                       void *voidptr; \
+                       void (*fptr) HOOK_ADDDEF arglist; \
+               } u; \
+               for (; he; he = he->next) { \
+                       hookarg = he->hookarg; \
+                       u.voidptr = he->hookfn; \
+                       u.fptr HOOK_ADDARG passlist; \
+               } \
+       }
+
+#endif /* _QUAGGA_HOOK_H */
-- 
2.7.3


_______________________________________________
Quagga-dev mailing list
[email protected]
https://lists.quagga.net/mailman/listinfo/quagga-dev

Reply via email to