#include <sys/cdefs.h>
__RCSID("$NetBSD: npf_extmod.c,v 1.5 2018/09/29 14:41:36 rmind Exp $");
#include <stdlib.h>
#include <inttypes.h>
#include <string.h>
#include <err.h>
#include <dlfcn.h>
#include "npfctl.h"
struct npf_extmod {
char * name;
npfext_initfunc_t init;
npfext_consfunc_t cons;
npfext_paramfunc_t param;
struct npf_extmod * next;
};
static npf_extmod_t * npf_extmod_list;
static void *
npf_extmod_sym(void *handle, const char *name, const char *func)
{
char buf[64];
void *sym;
snprintf(buf, sizeof(buf), "npfext_%s_%s", name, func);
sym = dlsym(handle, buf);
if (sym == NULL) {
errx(EXIT_FAILURE, "dlsym: %s", dlerror());
}
return sym;
}
static npf_extmod_t *
npf_extmod_load(const char *name)
{
npf_extmod_t *ext;
void *handle;
char extlib[PATH_MAX];
snprintf(extlib, sizeof(extlib), "/lib/npf/ext_%s.so", name);
handle = dlopen(extlib, RTLD_LAZY | RTLD_LOCAL);
if (handle == NULL) {
errx(EXIT_FAILURE, "dlopen: %s", dlerror());
}
ext = ecalloc(1, sizeof(npf_extmod_t));
ext->name = estrdup(name);
ext->init = npf_extmod_sym(handle, name, "init");
ext->cons = npf_extmod_sym(handle, name, "construct");
ext->param = npf_extmod_sym(handle, name, "param");
if (ext->init() != 0) {
free(ext);
return NULL;
}
ext->next = npf_extmod_list;
npf_extmod_list = ext;
return ext;
}
npf_extmod_t *
npf_extmod_get(const char *name, nl_ext_t **extcall)
{
npf_extmod_t *extmod = npf_extmod_list;
while (extmod) {
if ((strcmp(extmod->name, name) == 0) &&
(*extcall = extmod->cons(name)) != NULL) {
return extmod;
}
extmod = extmod->next;
}
extmod = npf_extmod_load(name);
if (extmod && (*extcall = extmod->cons(name)) != NULL) {
return extmod;
}
return NULL;
}
int
npf_extmod_param(npf_extmod_t *extmod, nl_ext_t *ext,
const char *param, const char *val)
{
return extmod->param(ext, param, val);
}