Commit 75068cff authored by Daniel Salzman's avatar Daniel Salzman

modules: unify parameter checks and module unload callbacks

Also don't return KNOT_STATE_FAIL from QPLAN_BEGIN and QPLAN_END if
an error reply doesn't make sense.
parent b0792d6a
......@@ -46,20 +46,18 @@ int check_mod_dnsproxy(conf_check_t *args)
return KNOT_EOK;
}
struct dnsproxy {
typedef struct {
conf_remote_t remote;
bool catch_nxdomain;
int timeout;
};
} dnsproxy_t;
static int dnsproxy_fwd(int state, knot_pkt_t *pkt, struct query_data *qdata, void *ctx)
{
if (pkt == NULL || qdata == NULL || ctx == NULL) {
return KNOT_STATE_FAIL;
}
assert(pkt && qdata && ctx);
/* Forward only queries ending with REFUSED (no zone) or NXDOMAIN (if configured) */
struct dnsproxy *proxy = ctx;
dnsproxy_t *proxy = ctx;
if (!(qdata->rcode == KNOT_RCODE_REFUSED ||
(qdata->rcode == KNOT_RCODE_NXDOMAIN && proxy->catch_nxdomain))) {
return state;
......@@ -106,15 +104,12 @@ static int dnsproxy_fwd(int state, knot_pkt_t *pkt, struct query_data *qdata, vo
int dnsproxy_load(struct query_plan *plan, struct query_module *self,
const knot_dname_t *zone)
{
if (plan == NULL || self == NULL) {
return KNOT_EINVAL;
}
assert(self);
struct dnsproxy *proxy = mm_alloc(self->mm, sizeof(struct dnsproxy));
dnsproxy_t *proxy = mm_alloc(self->mm, sizeof(*proxy));
if (proxy == NULL) {
return KNOT_ENOMEM;
}
memset(proxy, 0, sizeof(struct dnsproxy));
conf_val_t val = conf_mod_get(self->config, MOD_REMOTE, self->id);
proxy->remote = conf_remote(self->config, &val, 0);
......@@ -130,12 +125,11 @@ int dnsproxy_load(struct query_plan *plan, struct query_module *self,
return query_plan_step(plan, QPLAN_END, dnsproxy_fwd, self->ctx);
}
int dnsproxy_unload(struct query_module *self)
void dnsproxy_unload(struct query_module *self)
{
if (self == NULL) {
return KNOT_EINVAL;
}
assert(self);
mm_free(self->mm, self->ctx);
return KNOT_EOK;
dnsproxy_t *ctx = self->ctx;
mm_free(self->mm, ctx);
}
/* Copyright (C) 2014 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
/* Copyright (C) 2016 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
......@@ -14,19 +14,9 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*!
* \file
*
* \brief DNS proxy module
*
* Accepted configurations:
* * "<address>"
*
* Module forwards all unsatisfied queries to the specified server in
* order to solve them, and then sends the response back, i.e. a tiny
* DNS proxy.
*
* \addtogroup query_processing
* @{
*/
#pragma once
......@@ -41,6 +31,4 @@ int check_mod_dnsproxy(conf_check_t *args);
/*! \brief Module interface. */
int dnsproxy_load(struct query_plan *plan, struct query_module *self,
const knot_dname_t *zone);
int dnsproxy_unload(struct query_module *self);
/*! @} */
void dnsproxy_unload(struct query_module *self);
......@@ -64,11 +64,8 @@ typedef struct {
static int log_message(int state, const knot_pkt_t *pkt, struct query_data *qdata,
dnstap_ctx_t *ctx)
{
if (pkt == NULL || qdata == NULL || ctx == NULL) {
return KNOT_STATE_FAIL;
}
assert(pkt && qdata && ctx);
int ret = KNOT_ERROR;
struct fstrm_iothr_queue *ioq =
fstrm_iothr_get_input_queue_idx(ctx->iothread, qdata->param->thread_id);
......@@ -91,17 +88,17 @@ static int log_message(int state, const knot_pkt_t *pkt, struct query_data *qdat
/* Create a dnstap message. */
Dnstap__Message msg;
ret = dt_message_fill(&msg, msgtype,
(const struct sockaddr *)qdata->param->remote,
NULL, /* todo: fill me! */
protocol,
pkt->wire, pkt->size, &tv, &tv);
int ret = dt_message_fill(&msg, msgtype,
(const struct sockaddr *)qdata->param->remote,
NULL, /* todo: fill me! */
protocol, pkt->wire, pkt->size, &tv, &tv);
if (ret != KNOT_EOK) {
return KNOT_STATE_FAIL;
return state;
}
Dnstap__Dnstap dnstap = DNSTAP__DNSTAP__INIT;
dnstap.type = DNSTAP__DNSTAP__TYPE__MESSAGE;
dnstap.message = (Dnstap__Message *)&msg;
dnstap.message = &msg;
/* Set message version and identity. */
if (ctx->identity_len > 0) {
......@@ -120,7 +117,7 @@ static int log_message(int state, const knot_pkt_t *pkt, struct query_data *qdat
size_t size = 0;
dt_pack(&dnstap, &frame, &size);
if (frame == NULL) {
return KNOT_STATE_FAIL;
return state;
}
/* Submit a request. */
......@@ -128,7 +125,7 @@ static int log_message(int state, const knot_pkt_t *pkt, struct query_data *qdat
fstrm_free_wrapper, NULL);
if (res != fstrm_res_success) {
free(frame);
state = KNOT_STATE_FAIL;
return state;
}
return state;
......@@ -138,9 +135,7 @@ static int log_message(int state, const knot_pkt_t *pkt, struct query_data *qdat
static int dnstap_message_log_query(int state, knot_pkt_t *pkt, struct query_data *qdata,
void *ctx)
{
if (qdata == NULL) {
return KNOT_STATE_FAIL;
}
assert(qdata);
return log_message(state, qdata->query, qdata, ctx);
}
......@@ -223,9 +218,7 @@ static struct fstrm_writer* dnstap_writer(const char *path)
int dnstap_load(struct query_plan *plan, struct query_module *self,
const knot_dname_t *zone)
{
if (plan == NULL || self == NULL) {
return KNOT_EINVAL;
}
assert(self);
/* Create dnstap context. */
dnstap_ctx_t *ctx = mm_alloc(self->mm, sizeof(*ctx));
......@@ -306,11 +299,9 @@ fail:
return KNOT_ENOMEM;
}
int dnstap_unload(struct query_module *self)
void dnstap_unload(struct query_module *self)
{
if (self == NULL) {
return KNOT_EINVAL;
}
assert(self);
dnstap_ctx_t *ctx = self->ctx;
......@@ -318,6 +309,4 @@ int dnstap_unload(struct query_module *self)
free(ctx->identity);
free(ctx->version);
mm_free(self->mm, ctx);
return KNOT_EOK;
}
/* Copyright (C) 2014 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
/* Copyright (C) 2016 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
......@@ -13,12 +13,6 @@
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*!
* \file
*
* \addtogroup query_processing
* @{
*/
#pragma once
......@@ -32,6 +26,4 @@ int check_mod_dnstap(conf_check_t *args);
/*! \brief Module interface. */
int dnstap_load(struct query_plan *plan, struct query_module *self,
const knot_dname_t *zone);
int dnstap_unload(struct query_module *self);
/*! @} */
void dnstap_unload(struct query_module *self);
......@@ -28,6 +28,8 @@ static bool is_udp(struct query_data *qdata)
int noudp_begin(int state, knot_pkt_t *pkt, struct query_data *qdata, void *ctx)
{
assert(pkt && qdata);
if (is_udp(qdata)) {
knot_wire_set_tc(pkt->wire);
return KNOT_STATE_DONE;
......@@ -44,7 +46,7 @@ int noudp_load(struct query_plan *plan, struct query_module *self,
return KNOT_EOK;
}
int noudp_unload(struct query_module *self)
void noudp_unload(struct query_module *self)
{
return KNOT_EOK;
return;
}
......@@ -25,4 +25,4 @@ extern const yp_item_t scheme_mod_noudp[];
int noudp_load(struct query_plan *plan, struct query_module *self,
const knot_dname_t *zone);
int noudp_unload(struct query_module *self);
void noudp_unload(struct query_module *self);
......@@ -71,11 +71,9 @@ const yp_item_t scheme_mod_online_sign[] = {
{ NULL }
};
struct online_sign_ctx {
typedef struct {
dnssec_key_t *key;
};
typedef struct online_sign_ctx online_sign_ctx_t;
} online_sign_ctx_t;
static bool want_dnssec(struct query_data *qdata)
{
......@@ -542,7 +540,7 @@ static int online_sign_ctx_new(online_sign_ctx_t **ctx_ptr,
static char *conf_kasp_path(const knot_dname_t *zone)
{
conf_val_t val = { 0 };
conf_val_t val;
val = conf_zone_get(conf(), C_STORAGE, zone);
char *storage = conf_abs_path(&val, NULL);
......@@ -556,7 +554,6 @@ static char *conf_kasp_path(const knot_dname_t *zone)
int online_sign_load(struct query_plan *plan, struct query_module *module,
const knot_dname_t *zone)
{
assert(plan);
assert(module);
assert(zone);
......@@ -594,12 +591,11 @@ int online_sign_load(struct query_plan *plan, struct query_module *module,
return KNOT_EOK;
}
int online_sign_unload(struct query_module *module)
void online_sign_unload(struct query_module *module)
{
assert(module);
online_sign_ctx_free(module->ctx);
module->ctx = NULL;
online_sign_ctx_t *ctx = module->ctx;
return KNOT_EOK;
online_sign_ctx_free(ctx);
}
/* Copyright (C) 2015 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
/* Copyright (C) 2016 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
......@@ -23,4 +23,4 @@ extern const yp_item_t scheme_mod_online_sign[];
int online_sign_load(struct query_plan *plan, struct query_module *self,
const knot_dname_t *zone);
int online_sign_unload(struct query_module *self);
void online_sign_unload(struct query_module *self);
......@@ -585,11 +585,10 @@ static int rosedb_query_txn(MDB_txn *txn, MDB_dbi dbi, knot_pkt_t *pkt, struct q
return ret;
}
static int rosedb_query(int state, knot_pkt_t *pkt, struct query_data *qdata, void *ctx)
static int rosedb_query(int state, knot_pkt_t *pkt, struct query_data *qdata,
void *ctx)
{
if (pkt == NULL || qdata == NULL || ctx == NULL) {
return KNOT_STATE_FAIL;
}
assert(pkt && qdata && ctx);
struct cache *cache = ctx;
......@@ -613,9 +612,7 @@ static int rosedb_query(int state, knot_pkt_t *pkt, struct query_data *qdata, vo
int rosedb_load(struct query_plan *plan, struct query_module *self,
const knot_dname_t *zone)
{
if (plan == NULL || self == NULL) {
return KNOT_EINVAL;
}
assert(self);
conf_val_t val = conf_mod_get(self->config, MOD_DBDIR, self->id);
struct cache *cache = cache_open(conf_str(&val), 0, self->mm);
......@@ -629,12 +626,11 @@ int rosedb_load(struct query_plan *plan, struct query_module *self,
return query_plan_step(plan, QPLAN_BEGIN, rosedb_query, self->ctx);
}
int rosedb_unload(struct query_module *self)
void rosedb_unload(struct query_module *self)
{
if (self == NULL) {
return KNOT_EINVAL;
}
assert(self);
cache_close(self->ctx);
return KNOT_EOK;
struct cache *cache = self->ctx;
cache_close(cache);
}
/* Copyright (C) 2014 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
/* Copyright (C) 2016 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
......@@ -14,18 +14,8 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*!
* \file
*
* \brief Static resource records
*
* Accepted configurations:
* * "<path_to_database>"
*
* The module provides a mean to override responses for certain queries before
* the record is searched in the available zones.
*
* \addtogroup query_processing
* @{
*/
#pragma once
......@@ -40,6 +30,4 @@ int check_mod_rosedb(conf_check_t *args);
/*! \brief Module interface. */
int rosedb_load(struct query_plan *plan, struct query_module *self,
const knot_dname_t *zone);
int rosedb_unload(struct query_module *self);
/*! @} */
void rosedb_unload(struct query_module *self);
......@@ -385,11 +385,10 @@ static int template_match(int state, synth_template_t *tpl, knot_pkt_t *pkt, str
return HIT;
}
static int solve_synth_record(int state, knot_pkt_t *pkt, struct query_data *qdata, void *ctx)
static int solve_synth_record(int state, knot_pkt_t *pkt, struct query_data *qdata,
void *ctx)
{
if (pkt == NULL || qdata == NULL || ctx == NULL) {
return ERROR;
}
assert(pkt && qdata && ctx);
/* Applicable when search in zone fails. */
if (state != MISS) {
......@@ -403,9 +402,7 @@ static int solve_synth_record(int state, knot_pkt_t *pkt, struct query_data *qda
int synth_record_load(struct query_plan *plan, struct query_module *self,
const knot_dname_t *zone)
{
if (plan == NULL || self == NULL) {
return KNOT_EINVAL;
}
assert(self);
/* Create synthesis template. */
struct synth_template *tpl = mm_alloc(self->mm, sizeof(struct synth_template));
......@@ -447,15 +444,13 @@ int synth_record_load(struct query_plan *plan, struct query_module *self,
return query_plan_step(plan, QPLAN_ANSWER, solve_synth_record, self->ctx);
}
int synth_record_unload(struct query_module *self)
void synth_record_unload(struct query_module *self)
{
if (self == NULL) {
return KNOT_EINVAL;
}
assert(self);
synth_template_t *tpl = self->ctx;
synth_template_t *tpl = (synth_template_t *)self->ctx;
free(tpl->zone);
free(tpl->prefix);
mm_free(self->mm, tpl);
return KNOT_EOK;
}
......@@ -14,15 +14,8 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*!
* \file
*
* \brief Synthetic records module
*
* Module synthetises forward/reverse records based on a template when
* the queried record can't be found in the zone contents.
*
* \addtogroup query_processing
* @{
*/
#pragma once
......@@ -37,6 +30,4 @@ int check_mod_synth_record(conf_check_t *args);
/*! \brief Module interface. */
int synth_record_load(struct query_plan *plan, struct query_module *self,
const knot_dname_t *zone);
int synth_record_unload(struct query_module *self);
/*! @} */
void synth_record_unload(struct query_module *self);
......@@ -27,13 +27,9 @@ static int whoami_query(int state, knot_pkt_t *pkt, struct query_data *qdata, vo
knot_rrset_t *rrset = NULL;
/* Sanity checks. */
if (pkt == NULL ||
qdata == NULL ||
qdata->query == NULL ||
qdata->param == NULL || qdata->param->remote == NULL ||
qdata->zone == NULL || qdata->zone->name == NULL ||
qdata->zone->contents == NULL || qdata->zone->contents->apex == NULL)
{
assert(pkt && qdata);
if (qdata->zone == NULL || qdata->zone->contents == NULL ||
qdata->zone->contents->apex == NULL) {
return ERROR;
}
......@@ -129,23 +125,13 @@ static int whoami_query(int state, knot_pkt_t *pkt, struct query_data *qdata, vo
int whoami_load(struct query_plan *plan, struct query_module *self,
const knot_dname_t *zone)
{
/* Sanity checks. */
if (plan == NULL || self == NULL) {
return KNOT_EINVAL;
}
/* Hook to the query plan. */
query_plan_step(plan, QPLAN_ANSWER, whoami_query, NULL);
return KNOT_EOK;
}
int whoami_unload(struct query_module *self)
void whoami_unload(struct query_module *self)
{
/* Sanity check. */
if (self == NULL) {
return KNOT_EINVAL;
}
return KNOT_EOK;
return;
}
......@@ -14,15 +14,6 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*!
* \file
*
* \brief whoami module
*
* \addtogroup query_processing
* @{
*/
#pragma once
#include "knot/nameserver/query_module.h"
......@@ -34,6 +25,4 @@ extern const yp_item_t scheme_mod_whoami[];
/*! \brief Module interface. */
int whoami_load(struct query_plan *plan, struct query_module *self,
const knot_dname_t *zone);
int whoami_unload(struct query_module *self);
/*! @} */
void whoami_unload(struct query_module *self);
......@@ -78,7 +78,7 @@ struct query_plan;
/* Module callback required API. */
typedef int (*qmodule_load_t)(struct query_plan *plan, struct query_module *self, const knot_dname_t *zone);
typedef int (*qmodule_unload_t)(struct query_module *self);
typedef void (*qmodule_unload_t)(struct query_module *self);
typedef int (*qmodule_process_t)(int state, knot_pkt_t *pkt, struct query_data *qdata, void *ctx);
typedef struct static_module {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment