Skip to content

Commit

Permalink
Simplify the npfext library API. (#122)
Browse files Browse the repository at this point in the history
  • Loading branch information
rmind authored Jul 25, 2021
1 parent f78cd49 commit ed624b3
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 86 deletions.
20 changes: 2 additions & 18 deletions src/lib/ext_log/npfext_log.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,25 +45,9 @@ __RCSID("$NetBSD$");

#include <npf.h>

int npfext_log_init(void);
nl_ext_t * npfext_log_construct(const char *);
int npfext_log_param(nl_ext_t *, const char *, const char *);
int npfext_log_param(nl_ext_t *, const char *, const char *);

int
npfext_log_init(void)
{
/* Nothing to initialise. */
return 0;
}

nl_ext_t *
npfext_log_construct(const char *name)
{
assert(strcmp(name, "log") == 0);
return npf_ext_construct(name);
}

int
__dso_public int
npfext_log_param(nl_ext_t *ext, const char *param, const char *val __unused)
{
unsigned int if_idx;
Expand Down
20 changes: 2 additions & 18 deletions src/lib/ext_normalize/npfext_normalize.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,9 @@ __RCSID("$NetBSD$");

#include <npf.h>

int npfext_normalize_init(void);
nl_ext_t * npfext_normalize_construct(const char *);
int npfext_normalize_param(nl_ext_t *, const char *, const char *);
int npfext_normalize_param(nl_ext_t *, const char *, const char *);

int
npfext_normalize_init(void)
{
/* Nothing to initialize. */
return 0;
}

nl_ext_t *
npfext_normalize_construct(const char *name)
{
assert(strcmp(name, "normalize") == 0);
return npf_ext_construct(name);
}

int
__dso_public int
npfext_normalize_param(nl_ext_t *ext, const char *param, const char *val)
{
enum ptype {
Expand Down
20 changes: 2 additions & 18 deletions src/lib/ext_ratelimit/npfext_ratelimit.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,9 @@ __RCSID("$NetBSD$");

#include <npf.h>

int npfext_ratelimit_init(void);
nl_ext_t * npfext_ratelimit_construct(const char *);
int npfext_ratelimit_param(nl_ext_t *, const char *, const char *);
int npfext_ratelimit_param(nl_ext_t *, const char *, const char *);

int
npfext_ratelimit_init(void)
{
/* Nothing to initialisz. */
return 0;
}

nl_ext_t *
npfext_ratelimit_construct(const char *name)
{
assert(strcmp(name, "ratelimit") == 0);
return npf_ext_construct(name);
}

int
__dso_public int
npfext_ratelimit_param(nl_ext_t *ext, const char *param, const char *val)
{
static const char *params[] = {
Expand Down
20 changes: 2 additions & 18 deletions src/lib/ext_rndblock/npfext_rndblock.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,9 @@ __RCSID("$NetBSD$");

#include <npf.h>

int npfext_rndblock_init(void);
nl_ext_t * npfext_rndblock_construct(const char *);
int npfext_rndblock_param(nl_ext_t *, const char *, const char *);
int npfext_rndblock_param(nl_ext_t *, const char *, const char *);

int
npfext_rndblock_init(void)
{
/* Nothing to initialise. */
return 0;
}

nl_ext_t *
npfext_rndblock_construct(const char *name)
{
assert(strcmp(name, "rndblock") == 0);
return npf_ext_construct(name);
}

int
__dso_public int
npfext_rndblock_param(nl_ext_t *ext, const char *param, const char *val)
{
enum ptype { PARAM_U32 };
Expand Down
28 changes: 14 additions & 14 deletions src/npfctl/npf_extmod.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ struct npf_extmod {
static npf_extmod_t * npf_extmod_list;

static void *
npf_extmod_sym(void *handle, const char *name, const char *func)
npf_extmod_sym(void *handle, const char *name, const char *func, bool required)
{
char buf[64];
void *sym;

snprintf(buf, sizeof(buf), "npfext_%s_%s", name, func);
sym = dlsym(handle, buf);
if (sym == NULL) {
if (required && sym == NULL) {
errx(EXIT_FAILURE, "dlsym: %s", dlerror());
}
return sym;
Expand All @@ -81,12 +81,12 @@ npf_extmod_load(const char *name)

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");
ext->init = npf_extmod_sym(handle, name, "init", false);
ext->cons = npf_extmod_sym(handle, name, "construct", false);
ext->param = npf_extmod_sym(handle, name, "param", true);

/* Initialise the module. */
if (ext->init() != 0) {
/* Run the initialization hook for the module, if needed. */
if (ext->init && ext->init() != 0) {
free(ext);
return NULL;
}
Expand All @@ -102,19 +102,19 @@ 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;
if (strcmp(extmod->name, name) == 0) {
goto found;
}
extmod = extmod->next;
}

extmod = npf_extmod_load(name);
if (extmod && (*extcall = extmod->cons(name)) != NULL) {
return extmod;
found:
if (!extmod) {
return NULL;
}

return NULL;
*extcall = extmod->cons ? extmod->cons(name) : npf_ext_construct(name);
return *extcall ? extmod : NULL;
}

int
Expand Down

0 comments on commit ed624b3

Please sign in to comment.