[mod_geoip] use config_plugin_values_init()
This commit is contained in:
parent
66a460d977
commit
b07bc5d266
277
src/mod_geoip.c
277
src/mod_geoip.c
|
@ -55,162 +55,163 @@
|
|||
*/
|
||||
|
||||
|
||||
/* plugin config for all request/connections */
|
||||
|
||||
typedef struct {
|
||||
unsigned short mem_cache;
|
||||
buffer *db_name;
|
||||
GeoIP *gi;
|
||||
GeoIP *gi;
|
||||
} plugin_config;
|
||||
|
||||
typedef struct {
|
||||
PLUGIN_DATA;
|
||||
|
||||
plugin_config **config_storage;
|
||||
|
||||
plugin_config conf;
|
||||
PLUGIN_DATA;
|
||||
plugin_config defaults;
|
||||
plugin_config conf;
|
||||
} plugin_data;
|
||||
|
||||
/* init the plugin data */
|
||||
INIT_FUNC(mod_geoip_init) {
|
||||
plugin_data *p;
|
||||
|
||||
p = calloc(1, sizeof(*p));
|
||||
|
||||
return p;
|
||||
return calloc(1, sizeof(plugin_data));
|
||||
}
|
||||
|
||||
static void mod_geoip_free_config(plugin_data * const p) {
|
||||
if (NULL == p->cvlist) return;
|
||||
/* (init i to 0 if global context; to 1 to skip empty global context) */
|
||||
for (int i = !p->cvlist[0].v.u2[1], used = p->nconfig; i < used; ++i) {
|
||||
config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0];
|
||||
for (; -1 != cpv->k_id; ++cpv) {
|
||||
switch (cpv->k_id) {
|
||||
case 0: /* geoip.db-filename */
|
||||
if (cpv->vtype == T_CONFIG_LOCAL && NULL != cpv->v.v)
|
||||
GeoIP_delete(cpv->v.v);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* destroy the plugin data */
|
||||
FREE_FUNC(mod_geoip_free) {
|
||||
plugin_data *p = p_d;
|
||||
plugin_data * const p = p_d;
|
||||
if (!p) return HANDLER_GO_ON;
|
||||
UNUSED(srv);
|
||||
|
||||
UNUSED(srv);
|
||||
mod_geoip_free_config(p);
|
||||
|
||||
if (!p) return HANDLER_GO_ON;
|
||||
free(p->cvlist);
|
||||
free(p);
|
||||
|
||||
if (p->config_storage) {
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < srv->config_context->used; i++) {
|
||||
plugin_config *s = p->config_storage[i];
|
||||
|
||||
if (!s) continue;
|
||||
|
||||
buffer_free(s->db_name);
|
||||
|
||||
/* clean up */
|
||||
if (s->gi) GeoIP_delete(s->gi);
|
||||
|
||||
free(s);
|
||||
}
|
||||
free(p->config_storage);
|
||||
}
|
||||
|
||||
free(p);
|
||||
|
||||
return HANDLER_GO_ON;
|
||||
return HANDLER_GO_ON;
|
||||
}
|
||||
|
||||
/* handle plugin config and check values */
|
||||
static int mod_geoip_open_db(server *srv, config_plugin_value_t * const cpv, int mem_cache) {
|
||||
/* country db filename is required! */
|
||||
if (buffer_is_empty(cpv->v.b)) {
|
||||
cpv->v.v = NULL;
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* let's start cooking */
|
||||
const int mode = (mem_cache)
|
||||
? GEOIP_MEMORY_CACHE | GEOIP_CHECK_CACHE
|
||||
: GEOIP_STANDARD | GEOIP_CHECK_CACHE;
|
||||
|
||||
GeoIP *gi = GeoIP_open(cpv->v.b->ptr, mode);
|
||||
if (NULL == gi) {
|
||||
log_error(srv->errh, __FILE__, __LINE__,
|
||||
"failed to open GeoIP database!!!");
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* is the db supported ? */
|
||||
if ( gi->databaseType != GEOIP_COUNTRY_EDITION
|
||||
&& gi->databaseType != GEOIP_CITY_EDITION_REV0
|
||||
&& gi->databaseType != GEOIP_CITY_EDITION_REV1) {
|
||||
log_error(srv->errh, __FILE__, __LINE__,
|
||||
"GeoIP database is of unsupported type!!!");
|
||||
GeoIP_delete(gi);
|
||||
return 0;
|
||||
}
|
||||
|
||||
cpv->vtype = T_CONFIG_LOCAL;
|
||||
cpv->v.v = gi;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void mod_geoip_merge_config_cpv(plugin_config * const pconf, const config_plugin_value_t * const cpv) {
|
||||
switch (cpv->k_id) { /* index into static config_plugin_keys_t cpk[] */
|
||||
case 0: /* geoip.db-filename */
|
||||
if (cpv->vtype != T_CONFIG_LOCAL) break;
|
||||
pconf->gi = cpv->v.v;
|
||||
break;
|
||||
case 1: /* geoip.memory-cache */
|
||||
break;
|
||||
default:/* should not happen */
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
static void mod_geoip_merge_config(plugin_config * const pconf, const config_plugin_value_t *cpv) {
|
||||
do {
|
||||
mod_geoip_merge_config_cpv(pconf, cpv);
|
||||
} while ((++cpv)->k_id != -1);
|
||||
}
|
||||
|
||||
static void mod_geoip_patch_config(connection * const con, plugin_data * const p) {
|
||||
memcpy(&p->conf, &p->defaults, sizeof(plugin_config));
|
||||
for (int i = 1, used = p->nconfig; i < used; ++i) {
|
||||
if (config_check_cond(con, (uint32_t)p->cvlist[i].k_id))
|
||||
mod_geoip_merge_config(&p->conf, p->cvlist + p->cvlist[i].v.u2[0]);
|
||||
}
|
||||
}
|
||||
|
||||
SETDEFAULTS_FUNC(mod_geoip_set_defaults) {
|
||||
plugin_data *p = p_d;
|
||||
size_t i = 0;
|
||||
static const config_plugin_keys_t cpk[] = {
|
||||
{ CONST_STR_LEN("geoip.db-filename"),
|
||||
T_CONFIG_STRING,
|
||||
T_CONFIG_SCOPE_CONNECTION }
|
||||
,{ CONST_STR_LEN("geoip.memory-cache"),
|
||||
T_CONFIG_BOOL,
|
||||
T_CONFIG_SCOPE_CONNECTION }
|
||||
,{ NULL, 0,
|
||||
T_CONFIG_UNSET,
|
||||
T_CONFIG_SCOPE_UNSET }
|
||||
};
|
||||
|
||||
config_values_t cv[] = {
|
||||
{ "geoip.db-filename", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 0 */
|
||||
{ "geoip.memory-cache", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 1 */
|
||||
{ NULL, NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET }
|
||||
};
|
||||
plugin_data * const p = p_d;
|
||||
if (!config_plugin_values_init(srv, p, cpk, "mod_geoip"))
|
||||
return HANDLER_ERROR;
|
||||
|
||||
if (!p) return HANDLER_ERROR;
|
||||
/* process and validate config directives
|
||||
* (init i to 0 if global context; to 1 to skip empty global context) */
|
||||
for (int i = !p->cvlist[0].v.u2[1]; i < p->nconfig; ++i) {
|
||||
config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0];
|
||||
config_plugin_value_t *fn = NULL;
|
||||
int mem_cache = 0;
|
||||
for (; -1 != cpv->k_id; ++cpv) {
|
||||
switch (cpv->k_id) {
|
||||
case 0: /* geoip.db-filename */
|
||||
fn = cpv;
|
||||
break;
|
||||
case 1: /* geoip.memory-cache */
|
||||
mem_cache = cpv->v.u;
|
||||
break;
|
||||
default:/* should not happen */
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (fn) {
|
||||
if (!mod_geoip_open_db(srv, fn, mem_cache))
|
||||
return HANDLER_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
p->config_storage = calloc(srv->config_context->used, sizeof(plugin_config *));
|
||||
/* initialize p->defaults from global config context */
|
||||
if (p->nconfig > 0 && p->cvlist->v.u2[1]) {
|
||||
const config_plugin_value_t *cpv = p->cvlist + p->cvlist->v.u2[0];
|
||||
if (-1 != cpv->k_id)
|
||||
mod_geoip_merge_config(&p->defaults, cpv);
|
||||
}
|
||||
|
||||
for (i = 0; i < srv->config_context->used; i++) {
|
||||
data_config const* config = (data_config const*)srv->config_context->data[i];
|
||||
plugin_config *s;
|
||||
int mode;
|
||||
|
||||
s = calloc(1, sizeof(plugin_config));
|
||||
|
||||
s->db_name = buffer_init();
|
||||
s->mem_cache = 0; /* default: do not load db to cache */
|
||||
s->gi = NULL;
|
||||
|
||||
cv[0].destination = s->db_name;
|
||||
cv[1].destination = &(s->mem_cache);
|
||||
|
||||
p->config_storage[i] = s;
|
||||
|
||||
if (0 != config_insert_values_global(srv, config->value, cv, i == 0 ? T_CONFIG_SCOPE_SERVER : T_CONFIG_SCOPE_CONNECTION)) {
|
||||
return HANDLER_ERROR;
|
||||
}
|
||||
|
||||
mode = GEOIP_STANDARD | GEOIP_CHECK_CACHE;
|
||||
|
||||
/* country db filename is requeried! */
|
||||
if (!buffer_is_empty(s->db_name)) {
|
||||
|
||||
/* let's start cooking */
|
||||
if (s->mem_cache != 0)
|
||||
mode = GEOIP_MEMORY_CACHE | GEOIP_CHECK_CACHE;
|
||||
|
||||
if (NULL == (s->gi = GeoIP_open(s->db_name->ptr, mode))) {
|
||||
log_error_write(srv, __FILE__, __LINE__, "s",
|
||||
"failed to open GeoIP database!!!");
|
||||
|
||||
return HANDLER_ERROR;
|
||||
}
|
||||
|
||||
/* is the db supported ? */
|
||||
if (s->gi->databaseType != GEOIP_COUNTRY_EDITION &&
|
||||
s->gi->databaseType != GEOIP_CITY_EDITION_REV0 &&
|
||||
s->gi->databaseType != GEOIP_CITY_EDITION_REV1) {
|
||||
log_error_write(srv, __FILE__, __LINE__, "s",
|
||||
"GeoIP database is of unsupported type!!!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return HANDLER_GO_ON;
|
||||
return HANDLER_GO_ON;
|
||||
}
|
||||
|
||||
#define PATCH(x) \
|
||||
p->conf.x = s->x;
|
||||
static int mod_geoip_patch_connection(server *srv, connection *con, plugin_data *p) {
|
||||
size_t i, j;
|
||||
plugin_config *s = p->config_storage[0];
|
||||
|
||||
PATCH(db_name);
|
||||
PATCH(mem_cache);
|
||||
PATCH(gi);
|
||||
|
||||
/* skip the first, the global context */
|
||||
for (i = 1; i < srv->config_context->used; i++) {
|
||||
if (!config_check_cond(con, i)) continue; /* condition not matched */
|
||||
|
||||
data_config *dc = (data_config *)srv->config_context->data[i];
|
||||
s = p->config_storage[i];
|
||||
|
||||
/* merge config */
|
||||
for (j = 0; j < dc->value->used; j++) {
|
||||
data_unset *du = dc->value->data[j];
|
||||
|
||||
if (buffer_is_equal_string(&du->key, CONST_STR_LEN("geoip.db-filename"))) {
|
||||
PATCH(db_name);
|
||||
}
|
||||
|
||||
if (buffer_is_equal_string(&du->key, CONST_STR_LEN("geoip.memory-cache"))) {
|
||||
PATCH(mem_cache);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
#undef PATCH
|
||||
|
||||
static handler_t mod_geoip_query (connection *con, plugin_data *p) {
|
||||
GeoIPRecord *gir;
|
||||
const char *remote_ip = con->dst_addr_buf->ptr;
|
||||
|
@ -279,14 +280,12 @@ static handler_t mod_geoip_query (connection *con, plugin_data *p) {
|
|||
}
|
||||
|
||||
CONNECTION_FUNC(mod_geoip_handle_request_env) {
|
||||
plugin_data *p = p_d;
|
||||
mod_geoip_patch_connection(srv, con, p);
|
||||
if (buffer_is_empty(p->conf.db_name)) return HANDLER_GO_ON;
|
||||
|
||||
return mod_geoip_query(con, p);
|
||||
UNUSED(srv);
|
||||
plugin_data *p = p_d;
|
||||
mod_geoip_patch_config(con, p);
|
||||
return (p->conf.gi) ? mod_geoip_query(con, p) : HANDLER_GO_ON;
|
||||
}
|
||||
|
||||
/* this function is called at dlopen() time and inits the callbacks */
|
||||
|
||||
int mod_geoip_plugin_init(plugin *p);
|
||||
int mod_geoip_plugin_init(plugin *p) {
|
||||
|
|
Loading…
Reference in New Issue