Alex 'AdUser' Z
9 years ago
2 changed files with 85 additions and 0 deletions
@ -0,0 +1,66 @@
|
||||
#include "common.h" |
||||
#include "backend.h" |
||||
|
||||
#define BACKEND_LIBRARY_PARAM "load" |
||||
|
||||
f2b_backend_t * |
||||
f2b_backend_create(f2b_config_section_t *config) { |
||||
f2b_config_param_t *param = NULL; |
||||
f2b_backend_t *backend = NULL; |
||||
int flags = RTLD_NOW | RTLD_LOCAL; |
||||
|
||||
assert(config != NULL); |
||||
assert(config->type == t_backend); |
||||
|
||||
f2b_config_find_param(config->param, BACKEND_LIBRARY_PARAM); |
||||
if (!param) |
||||
return NULL; |
||||
|
||||
if ((backend = calloc(1, sizeof(f2b_backend_t))) == NULL) |
||||
return NULL; |
||||
|
||||
if ((backend->h = dlopen(param->value, flags)) == NULL) |
||||
goto cleanup; |
||||
if ((*(void **) (&backend->init) = dlsym(backend->h, "init")) == NULL) |
||||
goto cleanup; |
||||
if ((*(void **) (&backend->ready) = dlsym(backend->h, "ready")) == NULL) |
||||
goto cleanup; |
||||
if ((*(void **) (&backend->ban) = dlsym(backend->h, "ban")) == NULL) |
||||
goto cleanup; |
||||
if ((*(void **) (&backend->unban) = dlsym(backend->h, "unban")) == NULL) |
||||
goto cleanup; |
||||
if ((*(void **) (&backend->exists) = dlsym(backend->h, "exists")) == NULL) |
||||
goto cleanup; |
||||
|
||||
if (backend->ready) |
||||
return backend; |
||||
|
||||
/* try init */ |
||||
for (; param != NULL; param = param->next) { |
||||
if (strcmp(param->name, BACKEND_LIBRARY_PARAM) == 0) |
||||
continue; |
||||
if (!backend->init(param->name, param->value)) { |
||||
f2b_log_msg(log_warn, "param pair not accepted by backend '%s': %s=%s", |
||||
config->name, param->name, param->value); |
||||
} |
||||
} |
||||
|
||||
if (backend->ready) |
||||
return backend; |
||||
|
||||
/* still not ready */ |
||||
f2b_log_msg(log_error, "backend '%s' not fully configured", config->name); |
||||
|
||||
cleanup: |
||||
if (backend->h) |
||||
dlclose(backend->h); |
||||
free(backend); |
||||
return NULL; |
||||
} |
||||
|
||||
void |
||||
f2b_backend_destroy(f2b_backend_t *backend) { |
||||
assert(backend != NULL); |
||||
dlclose(backend->h); |
||||
free(backend); |
||||
} |
@ -0,0 +1,19 @@
|
||||
#ifndef BACKEND_H_ |
||||
#define BACKEND_H_ |
||||
|
||||
#include "config.h" |
||||
#include "log.h" |
||||
|
||||
typedef struct f2b_backend_t { |
||||
void *h; |
||||
bool (*init) (const char *key, const char *value); |
||||
bool (*ready) (void); |
||||
bool (*ban) (const char *ip); |
||||
bool (*unban) (const char *ip); |
||||
bool (*exists)(const char *ip); |
||||
} f2b_backend_t; |
||||
|
||||
f2b_backend_t * f2b_backend_create (f2b_config_section_t *config); |
||||
void f2b_backend_destroy(f2b_backend_t *backend); |
||||
|
||||
#endif /* BACKEND_H_ */ |
Loading…
Reference in new issue