From: Marek VavruĊĦa Date: Wed, 6 May 2015 08:27:47 +0000 (+0200) Subject: lib/modules: layer() api now accept module name X-Git-Tag: v1.0.0-beta1~212 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=daa89fe8c093cd47a4e64a560ca18bcf54b9c9bf;p=thirdparty%2Fknot-resolver.git lib/modules: layer() api now accept module name module api can now store userdata, e.g. owner --- diff --git a/lib/layer/iterate.c b/lib/layer/iterate.c index 9b69beb57..fbe45bfc3 100644 --- a/lib/layer/iterate.c +++ b/lib/layer/iterate.c @@ -442,7 +442,7 @@ static int resolve(knot_layer_t *ctx, knot_pkt_t *pkt) } /** Module implementation. */ -const knot_layer_api_t *iterate_layer(void) +const knot_layer_api_t *iterate_layer(struct kr_module *module) { static const knot_layer_api_t _layer = { .begin = &begin, diff --git a/lib/layer/iterate.h b/lib/layer/iterate.h index 4ae49a217..dbdcf81c8 100644 --- a/lib/layer/iterate.h +++ b/lib/layer/iterate.h @@ -19,9 +19,6 @@ #include "lib/layer.h" #include "lib/rplan.h" -/* Processing module implementation. */ -extern const knot_layer_api_t *iterate_layer(void); - /* Packet classification. */ enum { PKT_NOERROR = 1 << 0, /* Positive response */ @@ -34,4 +31,4 @@ enum { int kr_response_classify(knot_pkt_t *pkt); /* Processing module implementation. */ -const knot_layer_api_t *iterate_layer(void); \ No newline at end of file +const knot_layer_api_t *iterate_layer(struct kr_module *module); \ No newline at end of file diff --git a/lib/layer/pktcache.c b/lib/layer/pktcache.c index 12db00def..383ad4e10 100644 --- a/lib/layer/pktcache.c +++ b/lib/layer/pktcache.c @@ -148,7 +148,7 @@ static int stash(knot_layer_t *ctx) } /** Module implementation. */ -const knot_layer_api_t *pktcache_layer(void) +const knot_layer_api_t *pktcache_layer(struct kr_module *module) { static const knot_layer_api_t _layer = { .begin = &begin, diff --git a/lib/layer/rrcache.c b/lib/layer/rrcache.c index 94a590dee..d5ef9b2f8 100644 --- a/lib/layer/rrcache.c +++ b/lib/layer/rrcache.c @@ -247,7 +247,7 @@ static int stash(knot_layer_t *ctx, knot_pkt_t *pkt) } /** Module implementation. */ -const knot_layer_api_t *rrcache_layer(void) +const knot_layer_api_t *rrcache_layer(struct kr_module *module) { static const knot_layer_api_t _layer = { .begin = &begin, diff --git a/lib/layer/rrcache.h b/lib/layer/rrcache.h index cb5474664..305addaff 100644 --- a/lib/layer/rrcache.h +++ b/lib/layer/rrcache.h @@ -19,4 +19,4 @@ #include "lib/layer.h" /* Processing module implementation. */ -extern const knot_layer_api_t *rrcache_layer(void); +const knot_layer_api_t *rrcache_layer(struct kr_module *module); diff --git a/lib/module.h b/lib/module.h index 2b8c9c7ef..eb5a6ebfe 100644 --- a/lib/module.h +++ b/lib/module.h @@ -34,10 +34,10 @@ typedef uint32_t (module_api_cb)(void); typedef int (module_init_cb)(struct kr_module *); typedef int (module_deinit_cb)(struct kr_module *); typedef int (module_config_cb)(struct kr_module *, const char *); -typedef const knot_layer_api_t* (module_layer_cb)(void); +typedef const knot_layer_api_t* (module_layer_cb)(struct kr_module *); typedef struct kr_prop *(module_prop_cb)(void); typedef char *(kr_prop_cb)(void *, struct kr_module *, const char *); -#define KR_MODULE_API ((uint32_t) 0x20150401) +#define KR_MODULE_API ((uint32_t) 0x20150402) /* @endcond */ /** diff --git a/lib/resolve.c b/lib/resolve.c index 1ebf77a8a..8dbc0c6ed 100644 --- a/lib/resolve.c +++ b/lib/resolve.c @@ -61,7 +61,7 @@ static void prepare_layers(struct kr_request *param) for (size_t i = 0; i < ctx->modules->len; ++i) { struct kr_module *mod = &ctx->modules->at[i]; if (mod->layer) { - knot_overlay_add(¶m->overlay, mod->layer(), param); + knot_overlay_add(¶m->overlay, mod->layer(mod), param); } } } diff --git a/modules/README.rst b/modules/README.rst index 660722654..a731dc761 100644 --- a/modules/README.rst +++ b/modules/README.rst @@ -30,11 +30,11 @@ A module is a shared library defining specific functions, here's an overview of .. csv-table:: :header: "C", "Go", "Params", "Comment" - "``X_api()`` [#]_", "``Api()``", "", "Implemented API (``uint32_t``)" + "``X_api()`` [#]_", "``Api()``", "", "Implemented API (``uint32_t``)" "``X_init()``", "``Init()``", "``module``", "Constructor" "``X_deinit()``", "``Deinit()``", "``module, key``", "Destructor" "``X_config()``", "``Config()``", "``module``", "Configuration" - "``X_layer()``", "``Layer()``", "", ":ref:`Module layer `" + "``X_layer()``", "``Layer()``", "``module``", ":ref:`Module layer `" "``X_props()``", "``Props()``", "", "NULL-terminated list of properties" .. [#] Mandatory symbol. @@ -170,7 +170,7 @@ Now we can add the implementations for the ``Begin`` and ``Finish`` functions, a return 0 } - func Layer() *C.knot_layer_api_t { + func Layer(module *C.struct_kr_module) *C.knot_layer_api_t { // Wrapping the inline trampoline function return C._layer() } diff --git a/modules/gostats/gostats.go b/modules/gostats/gostats.go index 0c3b4ee61..92fd2ebd4 100644 --- a/modules/gostats/gostats.go +++ b/modules/gostats/gostats.go @@ -41,6 +41,6 @@ func Finish(ctx *C.knot_layer_t) C.int { return 0 } -func Layer() *C.knot_layer_api_t { +func Layer(module *C.struct_kr_module) *C.knot_layer_api_t { return C._layer() } diff --git a/modules/hints/hints.c b/modules/hints/hints.c index 8a0b2a9cf..36a71aa7e 100644 --- a/modules/hints/hints.c +++ b/modules/hints/hints.c @@ -36,9 +36,6 @@ #define DEFAULT_FILE "/etc/hosts" #define DEBUG_MSG(qry, fmt...) QRDEBUG(qry, "hint", fmt) -/** @todo Hack until layers can store userdata. */ -static struct kr_zonecut *g_map = NULL; - static int begin(knot_layer_t *ctx, void *module_param) { ctx->data = module_param; @@ -78,7 +75,6 @@ static int answer_query(knot_pkt_t *pkt, pack_t *addr_set, struct kr_request *pa static int query(knot_layer_t *ctx, knot_pkt_t *pkt) { - assert(pkt && ctx); struct kr_request *param = ctx->data; struct kr_query *qry = kr_rplan_current(¶m->rplan); if (!qry || ctx->state & (KNOT_STATE_DONE|KNOT_STATE_FAIL)) { @@ -89,7 +85,9 @@ static int query(knot_layer_t *ctx, knot_pkt_t *pkt) } /* Find a matching name */ - pack_t *pack = kr_zonecut_find(g_map, qry->sname); + struct kr_module *module = ctx->api->data; + struct kr_zonecut *hint_map = module->data; + pack_t *pack = kr_zonecut_find(hint_map, qry->sname); if (!pack || pack->len == 0) { return ctx->state; } @@ -182,7 +180,6 @@ static int load(struct kr_module *module, const char *path) struct kr_zonecut *hints = mm_alloc(pool, sizeof(*hints)); kr_zonecut_init(hints, (const uint8_t *)(""), pool); module->data = hints; - g_map = hints; return load_map(hints, fp); } @@ -268,12 +265,14 @@ static char* hint_get(void *env, struct kr_module *module, const char *args) * Module implementation. */ -const knot_layer_api_t *hints_layer(void) +const knot_layer_api_t *hints_layer(struct kr_module *module) { - static const knot_layer_api_t _layer = { + static knot_layer_api_t _layer = { .begin = &begin, - .produce = &query + .produce = &query, }; + /* Store module reference */ + _layer.data = module; return &_layer; }