From d82c515f44f81f2764750b0578f3b6169aad48fa Mon Sep 17 00:00:00 2001 From: Alex 'AdUser' Z Date: Mon, 29 Mar 2021 16:13:14 +1000 Subject: [PATCH] + event.[ch] --- src/CMakeLists.txt | 2 +- src/event.c | 33 +++++++++++++++++++++++++++++++++ src/event.h | 44 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 src/event.c create mode 100644 src/event.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 6883a6b..2dbf8d6 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,7 +1,7 @@ set(CMAKE_INCLUDE_CURRENT_DIR ON) set(SOURCES "daemon.c" "strlcpy.c" "config.c" "buf.c" "log.c" "matches.c" "ipaddr.c" - "appconfig.c" "statefile.c" "source.c" "filter.c" "backend.c" "jail.c") + "appconfig.c" "statefile.c" "event.c" "source.c" "filter.c" "backend.c" "jail.c") if (WITH_CSOCKET) list(APPEND SOURCES "md5.c" "commands.c" "csocket.c") diff --git a/src/event.c b/src/event.c new file mode 100644 index 0000000..1cb164e --- /dev/null +++ b/src/event.c @@ -0,0 +1,33 @@ +/* Copyright 2021 Alex 'AdUser' Z (ad_user@runbox.com) + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + */ +#include "common.h" + +static void (*handlers[4])(const char *) = { NULL, NULL, NULL, NULL }; + +void +f2b_event_send(const char *evt) { + for (short int i = 0; i < 3; i++) { + if (!handlers[i]) break; /* end of list */ + handlers[i](evt); + } +} + +bool +f2b_event_handler_register(void (*h)(const char *)) { + for (short int i = 0; i < 3; i++) { + if (handlers[i] == NULL) { + handlers[i] = h; + return true; + } + } + return false; +} + +void +f2b_event_handler_flush() { + memset(handlers, 0x0, sizeof(handlers)); +} diff --git a/src/event.h b/src/event.h new file mode 100644 index 0000000..af91c3d --- /dev/null +++ b/src/event.h @@ -0,0 +1,44 @@ +/* Copyright 2020-2021 Alex 'AdUser' Z (ad_user@runbox.com) + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + */ +#ifndef F2B_EVENT_H_ +#define F2B_EVENT_H_ + +/** + * @file + * This file contains event-related routines + */ + +/** + * @def EVENT_MAX + * Maximum text length of event line + */ +#define EVENT_MAX 128 + +/** + * @brief Send event to all registered handlers + * @param evt Constant string in specified format + */ +void +f2b_event_send(const char *evt); + +/** + * @brief Adds event handler + * @param h Pointer to function + * @note For now you can register not more than three handlers. + * It will be called in order of registration + * @return true on success, false on error + */ +bool +f2b_event_handler_register(void (*h)(const char *evt)); + +/** + * @brief Clean event handlers list + */ +void +f2b_event_handlers_flush(); + +#endif /* F2B_EVENT_H_ */