Browse Source

* refactor backend modules library interface

master
Alex 'AdUser' Z 4 years ago
parent
commit
5d9f6df5ff
  1. 6
      src/backend-test.c
  2. 5
      src/backend.c
  3. 9
      src/backend.h
  4. 15
      src/jail.c

6
src/backend-test.c

@ -90,7 +90,7 @@ int main(int argc, char *argv[]) {
}
if (!f2b_backend_start(backend)) {
f2b_log_msg(log_error, "action 'ban' failed: %s", f2b_backend_error(backend));
f2b_log_msg(log_error, "action 'ban' failed");
goto cleanup;
}
@ -120,9 +120,7 @@ int main(int argc, char *argv[]) {
if (handler(backend, addr)) {
fputs("ok\n", stdout);
} else {
fputs("failure: ", stdout);
fputs(f2b_backend_error(backend), stdout);
fputc('\n', stdout);
fputs("failure\n", stdout);
}
} /* while */

5
src/backend.c

@ -36,7 +36,7 @@ f2b_backend_create(f2b_config_section_t *config, const char *id) {
goto cleanup;
if ((*(void **) (&backend->ready) = dlsym(backend->h, "ready")) == NULL)
goto cleanup;
if ((*(void **) (&backend->error) = dlsym(backend->h, "error")) == NULL)
if ((*(void **) (&backend->logcb) = dlsym(backend->h, "logcb")) == NULL)
goto cleanup;
if ((*(void **) (&backend->start) = dlsym(backend->h, "start")) == NULL)
goto cleanup;
@ -58,6 +58,8 @@ f2b_backend_create(f2b_config_section_t *config, const char *id) {
goto cleanup;
}
backend->logcb(backend->cfg, f2b_log_mod_cb);
/* try init */
for (param = config->param; param != NULL; param = param->next) {
if (strcmp(param->name, BACKEND_LIBRARY_PARAM) == 0)
@ -109,7 +111,6 @@ f2b_backend_ ## CMD(f2b_backend_t *backend, const char *ip) { \
return backend->CMD(backend->cfg, ip); \
}
BACKEND_CMD_ARG0(error, const char *)
BACKEND_CMD_ARG0(start, bool)
BACKEND_CMD_ARG0(stop, bool)
BACKEND_CMD_ARG0(ping, bool)

9
src/backend.h

@ -27,7 +27,7 @@ typedef struct f2b_backend_t {
/** dlsym pointer to handler of @a ready command */
bool (*ready) (void *cfg);
/** dlsym pointer to handler of @a error command */
char *(*error) (void *cfg);
void (*logcb) (void *cfg, void (*cb)(log_msgtype_t lvl, const char *msg));
/** dlsym pointer to handler of @a start command */
bool (*start) (void *cfg);
/** dlsym pointer to handler of @a stop command */
@ -58,13 +58,6 @@ f2b_backend_t * f2b_backend_create (f2b_config_section_t *config, const char *id
void f2b_backend_destroy(f2b_backend_t *b);
/* helpers */
/**
* @brief Get last backend error
* @param b Pointer to backend struct
* @returns Pointer to string with description of last error
*/
const char *
f2b_backend_error (f2b_backend_t *b);
/**
* @brief Start given backend
* @param b Pointer to backend struct

15
src/jail.c

@ -170,8 +170,7 @@ f2b_jail_ban(f2b_jail_t *jail, f2b_ipaddr_t *addr) {
return true;
}
f2b_log_msg(log_error, "jail '%s': can't ban ip %s -- %s",
jail->name, addr->text, f2b_backend_error(jail->backend));
f2b_log_msg(log_error, "jail '%s': can't ban ip %s", jail->name, addr->text);
return false;
}
@ -189,8 +188,7 @@ f2b_jail_unban(f2b_jail_t *jail, f2b_ipaddr_t *addr) {
return true;
}
f2b_log_msg(log_error, "jail '%s': can't release ip %s -- %s",
jail->name, addr->text, f2b_backend_error(jail->backend));
f2b_log_msg(log_error, "jail '%s': can't release ip %s", jail->name, addr->text);
return false;
}
@ -383,8 +381,7 @@ f2b_jail_init(f2b_jail_t *jail, f2b_config_t *config) {
goto cleanup;
}
if (!f2b_backend_start(jail->backend)) {
f2b_log_msg(log_warn, "jail '%s': backend action 'start' failed -- %s",
jail->name, f2b_backend_error(jail->backend));
f2b_log_msg(log_warn, "jail '%s': backend action 'start' failed", jail->name);
goto cleanup;
}
@ -433,8 +430,7 @@ f2b_jail_start(f2b_jail_t *jail) {
f2b_log_msg(log_note, "jail '%s': restored ban of ip %s (%.1fhrs remain)",
jail->name, addr->text, (float) remains / 3600);
} else {
f2b_log_msg(log_error, "jail '%s': can't ban ip %s -- %s",
jail->name, addr->text, f2b_backend_error(jail->backend));
f2b_log_msg(log_error, "jail '%s': can't ban ip %s", jail->name, addr->text);
}
}
@ -469,8 +465,7 @@ f2b_jail_stop(f2b_jail_t *jail) {
f2b_addrlist_destroy(jail->ipaddrs);
if (!f2b_backend_stop(jail->backend)) {
f2b_log_msg(log_error, "jail '%s': action 'stop' for backend failed: %s",
jail->name, f2b_backend_error(jail->backend));
f2b_log_msg(log_error, "jail '%s': action 'stop' for backend failed", jail->name);
errors = true;
}

Loading…
Cancel
Save