Browse Source

+ event.[ch]

master
Alex 'AdUser' Z 4 years ago
parent
commit
d82c515f44
  1. 2
      src/CMakeLists.txt
  2. 33
      src/event.c
  3. 44
      src/event.h

2
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")

33
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));
}

44
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_ */
Loading…
Cancel
Save