Commit 83b56d9a authored by Daniel Salzman's avatar Daniel Salzman

modules: switch to the new API

parent e59377d3
...@@ -233,6 +233,7 @@ src/knot/events/log.c ...@@ -233,6 +233,7 @@ src/knot/events/log.c
src/knot/events/log.h src/knot/events/log.h
src/knot/events/replan.c src/knot/events/replan.c
src/knot/events/replan.h src/knot/events/replan.h
src/knot/include/module.h
src/knot/journal/journal.c src/knot/journal/journal.c
src/knot/journal/journal.h src/knot/journal/journal.h
src/knot/journal/old_journal.c src/knot/journal/old_journal.c
...@@ -240,28 +241,19 @@ src/knot/journal/old_journal.h ...@@ -240,28 +241,19 @@ src/knot/journal/old_journal.h
src/knot/journal/serialization.c src/knot/journal/serialization.c
src/knot/journal/serialization.h src/knot/journal/serialization.h
src/knot/modules/dnsproxy/dnsproxy.c src/knot/modules/dnsproxy/dnsproxy.c
src/knot/modules/dnsproxy/dnsproxy.h
src/knot/modules/dnstap/dnstap.c src/knot/modules/dnstap/dnstap.c
src/knot/modules/dnstap/dnstap.h
src/knot/modules/noudp/noudp.c src/knot/modules/noudp/noudp.c
src/knot/modules/noudp/noudp.h src/knot/modules/onlinesign/nsec_next.c
src/knot/modules/online_sign/nsec_next.c src/knot/modules/onlinesign/nsec_next.h
src/knot/modules/online_sign/nsec_next.h src/knot/modules/onlinesign/onlinesign.c
src/knot/modules/online_sign/online_sign.c
src/knot/modules/online_sign/online_sign.h
src/knot/modules/rosedb/rosedb.c src/knot/modules/rosedb/rosedb.c
src/knot/modules/rosedb/rosedb.h
src/knot/modules/rosedb/rosedb_tool.c src/knot/modules/rosedb/rosedb_tool.c
src/knot/modules/rrl/functions.c src/knot/modules/rrl/functions.c
src/knot/modules/rrl/functions.h src/knot/modules/rrl/functions.h
src/knot/modules/rrl/rrl.c src/knot/modules/rrl/rrl.c
src/knot/modules/rrl/rrl.h
src/knot/modules/stats/stats.c src/knot/modules/stats/stats.c
src/knot/modules/stats/stats.h src/knot/modules/synthrecord/synthrecord.c
src/knot/modules/synth_record/synth_record.c
src/knot/modules/synth_record/synth_record.h
src/knot/modules/whoami/whoami.c src/knot/modules/whoami/whoami.c
src/knot/modules/whoami/whoami.h
src/knot/nameserver/axfr.c src/knot/nameserver/axfr.c
src/knot/nameserver/axfr.h src/knot/nameserver/axfr.h
src/knot/nameserver/chaos.c src/knot/nameserver/chaos.c
...@@ -532,7 +524,7 @@ tests/libknot/test_tsig.c ...@@ -532,7 +524,7 @@ tests/libknot/test_tsig.c
tests/libknot/test_yparser.c tests/libknot/test_yparser.c
tests/libknot/test_ypscheme.c tests/libknot/test_ypscheme.c
tests/libknot/test_yptrafo.c tests/libknot/test_yptrafo.c
tests/modules/test_online_sign.c tests/modules/test_onlinesign.c
tests/modules/test_rrl.c tests/modules/test_rrl.c
tests/test_acl.c tests/test_acl.c
tests/test_changeset.c tests/test_changeset.c
......
...@@ -297,24 +297,6 @@ libknotd_la_SOURCES = \ ...@@ -297,24 +297,6 @@ libknotd_la_SOURCES = \
knot/events/log.h \ knot/events/log.h \
knot/events/replan.c \ knot/events/replan.c \
knot/events/replan.h \ knot/events/replan.h \
knot/modules/dnsproxy/dnsproxy.c \
knot/modules/dnsproxy/dnsproxy.h \
knot/modules/noudp/noudp.c \
knot/modules/noudp/noudp.h \
knot/modules/online_sign/online_sign.c \
knot/modules/online_sign/online_sign.h \
knot/modules/online_sign/nsec_next.c \
knot/modules/online_sign/nsec_next.h \
knot/modules/rrl/functions.c \
knot/modules/rrl/functions.h \
knot/modules/rrl/rrl.c \
knot/modules/rrl/rrl.h \
knot/modules/stats/stats.c \
knot/modules/stats/stats.h \
knot/modules/synth_record/synth_record.c\
knot/modules/synth_record/synth_record.h\
knot/modules/whoami/whoami.c \
knot/modules/whoami/whoami.h \
knot/nameserver/axfr.c \ knot/nameserver/axfr.c \
knot/nameserver/axfr.h \ knot/nameserver/axfr.h \
knot/nameserver/chaos.c \ knot/nameserver/chaos.c \
...@@ -467,20 +449,6 @@ keymgr_LDADD = $(libidn_LIBS) $(liburcu_LIBS) libknotd.la libcontrib.la \ ...@@ -467,20 +449,6 @@ keymgr_LDADD = $(libidn_LIBS) $(liburcu_LIBS) libknotd.la libcontrib.la \
# Optional Knot DNS Daemon modules # # Optional Knot DNS Daemon modules #
#################################### ####################################
if HAVE_DNSTAP
libknotd_la_SOURCES += \
knot/modules/dnstap/dnstap.c \
knot/modules/dnstap/dnstap.h
libknotd_la_CPPFLAGS += $(DNSTAP_CFLAGS)
libknotd_la_LIBADD += $(DNSTAP_LIBS) contrib/dnstap/libdnstap.la
endif # HAVE_DNSTAP
if HAVE_ROSEDB
libknotd_la_SOURCES += \
knot/modules/rosedb/rosedb.c \
knot/modules/rosedb/rosedb.h
endif # HAVE_ROSEDB
# Create storage and run-time directories # Create storage and run-time directories
install-data-hook: install-data-hook:
$(INSTALL) -d $(DESTDIR)/@config_dir@ $(INSTALL) -d $(DESTDIR)/@config_dir@
...@@ -558,12 +526,20 @@ kdig_CPPFLAGS += $(DNSTAP_CFLAGS) ...@@ -558,12 +526,20 @@ kdig_CPPFLAGS += $(DNSTAP_CFLAGS)
khost_CPPFLAGS += $(DNSTAP_CFLAGS) khost_CPPFLAGS += $(DNSTAP_CFLAGS)
endif # HAVE_DNSTAP endif # HAVE_DNSTAP
if HAVE_ROSEDB
bin_PROGRAMS += rosedb_tool
rosedb_tool_SOURCES = knot/modules/rosedb/rosedb_tool.c
rosedb_tool_LDADD = libknotd.la libcontrib.la
rosedb_tool_CFLAGS = $(AM_CFLAGS) $(lmdb_CFLAGS) $(DNSTAP_CFLAGS)
rosedb_tool_LDFLAGS = $(AM_LDFLAGS) $(lmdb_LIBS)
endif # HAVE_ROSEDB
endif # HAVE_UTILS endif # HAVE_UTILS
KNOTD_MOD_CPPFLAGS = $(AM_CPPFLAGS) $(CFLAG_VISIBILITY)
KNOTD_MOD_LDFLAGS = $(AM_LDFLAGS) -module -shared -avoid-version
pkglibdir = $(module_instdir)
pkglib_LTLIBRARIES =
include $(srcdir)/knot/modules/dnsproxy/Makefile.inc
include $(srcdir)/knot/modules/dnstap/Makefile.inc
include $(srcdir)/knot/modules/noudp/Makefile.inc
include $(srcdir)/knot/modules/onlinesign/Makefile.inc
include $(srcdir)/knot/modules/rosedb/Makefile.inc
include $(srcdir)/knot/modules/rrl/Makefile.inc
include $(srcdir)/knot/modules/stats/Makefile.inc
include $(srcdir)/knot/modules/synthrecord/Makefile.inc
include $(srcdir)/knot/modules/whoami/Makefile.inc
knot_modules_dnsproxy_la_SOURCES = knot/modules/dnsproxy/dnsproxy.c
if STATIC_MODULE_dnsproxy
libknotd_la_SOURCES += $(knot_modules_dnsproxy_la_SOURCES)
endif
if SHARED_MODULE_dnsproxy
knot_modules_dnsproxy_la_LDFLAGS = $(KNOTD_MOD_LDFLAGS)
knot_modules_dnsproxy_la_CPPFLAGS = $(KNOTD_MOD_CPPFLAGS)
knot_modules_dnsproxy_la_LIBADD = libcontrib.la
pkglib_LTLIBRARIES += knot/modules/dnsproxy.la
endif
...@@ -14,11 +14,11 @@ ...@@ -14,11 +14,11 @@
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "contrib/mempattern.h"
#include "contrib/net.h" #include "contrib/net.h"
#include "knot/modules/dnsproxy/dnsproxy.h" #include "knot/include/module.h"
#include "knot/query/capture.h" #include "knot/conf/scheme.h"
#include "knot/query/requestor.h" #include "knot/query/capture.h" // Forces static module!
#include "knot/query/requestor.h" // Forces static module!
/* Module configuration scheme. */ /* Module configuration scheme. */
#define MOD_REMOTE "\x06""remote" #define MOD_REMOTE "\x06""remote"
...@@ -26,21 +26,19 @@ ...@@ -26,21 +26,19 @@
#define MOD_FALLBACK "\x08""fallback" #define MOD_FALLBACK "\x08""fallback"
#define MOD_CATCH_NXDOMAIN "\x0E""catch-nxdomain" #define MOD_CATCH_NXDOMAIN "\x0E""catch-nxdomain"
const yp_item_t scheme_mod_dnsproxy[] = { const yp_item_t dnsproxy_conf[] = {
{ C_ID, YP_TSTR, YP_VNONE }, { MOD_REMOTE, YP_TREF, YP_VREF = { C_RMT }, YP_FNONE,
{ MOD_REMOTE, YP_TREF, YP_VREF = { C_RMT }, YP_FNONE, { check_ref } }, { knotd_conf_check_ref } },
{ MOD_TIMEOUT, YP_TINT, YP_VINT = { 0, INT32_MAX, 500 } }, { MOD_TIMEOUT, YP_TINT, YP_VINT = { 0, INT32_MAX, 500 } },
{ MOD_FALLBACK, YP_TBOOL, YP_VBOOL = { true } }, { MOD_FALLBACK, YP_TBOOL, YP_VBOOL = { true } },
{ MOD_CATCH_NXDOMAIN, YP_TBOOL, YP_VNONE }, { MOD_CATCH_NXDOMAIN, YP_TBOOL, YP_VNONE },
{ C_COMMENT, YP_TSTR, YP_VNONE },
{ NULL } { NULL }
}; };
int check_mod_dnsproxy(conf_check_t *args) int dnsproxy_conf_check(knotd_conf_check_args_t *args)
{ {
conf_val_t rmt = conf_rawid_get_txn(args->conf, args->txn, C_MOD_DNSPROXY, knotd_conf_t rmt = knotd_conf_check_item(args, MOD_REMOTE);
MOD_REMOTE, args->id, args->id_len); if (rmt.count == 0) {
if (rmt.code != KNOT_EOK) {
args->err_str = "no remote server specified"; args->err_str = "no remote server specified";
return KNOT_EINVAL; return KNOT_EINVAL;
} }
...@@ -49,17 +47,19 @@ int check_mod_dnsproxy(conf_check_t *args) ...@@ -49,17 +47,19 @@ int check_mod_dnsproxy(conf_check_t *args)
} }
typedef struct { typedef struct {
conf_remote_t remote; struct sockaddr_storage remote;
struct sockaddr_storage via;
bool fallback; bool fallback;
bool catch_nxdomain; bool catch_nxdomain;
int timeout; int timeout;
} dnsproxy_t; } dnsproxy_t;
static int dnsproxy_fwd(int state, knot_pkt_t *pkt, struct query_data *qdata, void *ctx) static knotd_state_t dnsproxy_fwd(knotd_state_t state, knot_pkt_t *pkt,
knotd_qdata_t *qdata, knotd_mod_t *mod)
{ {
assert(pkt && qdata && ctx); assert(pkt && qdata && mod);
dnsproxy_t *proxy = ctx; dnsproxy_t *proxy = knotd_mod_ctx(mod);
/* Forward only queries ending with REFUSED (no zone) or NXDOMAIN (if configured) */ /* Forward only queries ending with REFUSED (no zone) or NXDOMAIN (if configured) */
if (proxy->fallback && !(qdata->rcode == KNOT_RCODE_REFUSED || if (proxy->fallback && !(qdata->rcode == KNOT_RCODE_REFUSED ||
...@@ -86,9 +86,9 @@ static int dnsproxy_fwd(int state, knot_pkt_t *pkt, struct query_data *qdata, vo ...@@ -86,9 +86,9 @@ static int dnsproxy_fwd(int state, knot_pkt_t *pkt, struct query_data *qdata, vo
return state; /* Ignore, not enough memory. */ return state; /* Ignore, not enough memory. */
} }
bool is_tcp = net_is_stream(qdata->param->socket); bool is_tcp = net_is_stream(qdata->params->socket);
const struct sockaddr *dst = (const struct sockaddr *)&proxy->remote.addr; const struct sockaddr *dst = (const struct sockaddr *)&proxy->remote;
const struct sockaddr *src = (const struct sockaddr *)&proxy->remote.via; const struct sockaddr *src = (const struct sockaddr *)&proxy->via;
struct knot_request *req = knot_request_make(re.mm, dst, src, qdata->query, NULL, struct knot_request *req = knot_request_make(re.mm, dst, src, qdata->query, NULL,
is_tcp ? 0 : KNOT_RQ_UDP); is_tcp ? 0 : KNOT_RQ_UDP);
if (req == NULL) { if (req == NULL) {
...@@ -105,7 +105,7 @@ static int dnsproxy_fwd(int state, knot_pkt_t *pkt, struct query_data *qdata, vo ...@@ -105,7 +105,7 @@ static int dnsproxy_fwd(int state, knot_pkt_t *pkt, struct query_data *qdata, vo
/* Check result. */ /* Check result. */
if (ret != KNOT_EOK) { if (ret != KNOT_EOK) {
qdata->rcode = KNOT_RCODE_SERVFAIL; qdata->rcode = KNOT_RCODE_SERVFAIL;
return KNOT_STATE_FAIL; /* Forwarding failed, SERVFAIL. */ return KNOTD_STATE_FAIL; /* Forwarding failed, SERVFAIL. */
} else { } else {
qdata->rcode = knot_pkt_ext_rcode(pkt); qdata->rcode = knot_pkt_ext_rcode(pkt);
} }
...@@ -115,44 +115,50 @@ static int dnsproxy_fwd(int state, knot_pkt_t *pkt, struct query_data *qdata, vo ...@@ -115,44 +115,50 @@ static int dnsproxy_fwd(int state, knot_pkt_t *pkt, struct query_data *qdata, vo
knot_tsig_append(pkt->wire, &pkt->size, pkt->max_size, pkt->tsig_rr); knot_tsig_append(pkt->wire, &pkt->size, pkt->max_size, pkt->tsig_rr);
} }
return KNOT_STATE_DONE; return KNOTD_STATE_DONE;
} }
int dnsproxy_load(struct query_module *self) int dnsproxy_load(knotd_mod_t *mod)
{ {
assert(self); dnsproxy_t *proxy = calloc(1, sizeof(*proxy));
dnsproxy_t *proxy = mm_alloc(self->mm, sizeof(*proxy));
if (proxy == NULL) { if (proxy == NULL) {
return KNOT_ENOMEM; return KNOT_ENOMEM;
} }
conf_val_t val = conf_mod_get(self->config, MOD_REMOTE, self->id); knotd_conf_t remote = knotd_conf_mod(mod, MOD_REMOTE);
proxy->remote = conf_remote(self->config, &val, 0); knotd_conf_t conf = knotd_conf(mod, C_RMT, C_ADDR, &remote);
if (conf.count > 0) {
proxy->remote = conf.multi[0].addr;
knotd_conf_free(&conf);
}
conf = knotd_conf(mod, C_RMT, C_VIA, &remote);
if (conf.count > 0) {
proxy->via = conf.multi[0].addr;
knotd_conf_free(&conf);
}
val = conf_mod_get(self->config, MOD_TIMEOUT, self->id); conf = knotd_conf_mod(mod, MOD_TIMEOUT);
proxy->timeout = conf_int(&val); proxy->timeout = conf.single.integer;
val = conf_mod_get(self->config, MOD_FALLBACK, self->id); conf = knotd_conf_mod(mod, MOD_FALLBACK);
proxy->fallback = conf_bool(&val); proxy->fallback = conf.single.boolean;
val = conf_mod_get(self->config, MOD_CATCH_NXDOMAIN, self->id); conf = knotd_conf_mod(mod, MOD_CATCH_NXDOMAIN);
proxy->catch_nxdomain = conf_bool(&val); proxy->catch_nxdomain = conf.single.boolean;
self->ctx = proxy; knotd_mod_ctx_set(mod, proxy);
if (proxy->fallback) { if (proxy->fallback) {
return query_module_step(self, QPLAN_END, dnsproxy_fwd); return knotd_mod_hook(mod, KNOTD_STAGE_END, dnsproxy_fwd);
} else { } else {
return query_module_step(self, QPLAN_BEGIN, dnsproxy_fwd); return knotd_mod_hook(mod, KNOTD_STAGE_BEGIN, dnsproxy_fwd);
} }
} }
void dnsproxy_unload(struct query_module *self) void dnsproxy_unload(knotd_mod_t *mod)
{ {
assert(self); free(knotd_mod_ctx(mod));
dnsproxy_t *ctx = self->ctx;
mm_free(self->mm, ctx);
} }
KNOTD_MOD_API(dnsproxy, KNOTD_MOD_FLAG_SCOPE_ANY,
dnsproxy_load, dnsproxy_unload, dnsproxy_conf, dnsproxy_conf_check);
/* 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
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*!
* 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.
*/
#pragma once
#include "knot/nameserver/query_module.h"
/*! \brief Module scheme. */
#define C_MOD_DNSPROXY "\x0C""mod-dnsproxy"
extern const yp_item_t scheme_mod_dnsproxy[];
int check_mod_dnsproxy(conf_check_t *args);
/*! \brief Module interface. */
int dnsproxy_load(struct query_module *self);
void dnsproxy_unload(struct query_module *self);
knot_modules_dnstap_la_SOURCES = knot/modules/dnstap/dnstap.c
if STATIC_MODULE_dnstap
libknotd_la_SOURCES += $(knot_modules_dnstap_la_SOURCES)
libknotd_la_CPPFLAGS += $(DNSTAP_CFLAGS)
libknotd_la_LIBADD += $(DNSTAP_LIBS) contrib/dnstap/libdnstap.la
endif
if SHARED_MODULE_dnstap
knot_modules_dnstap_la_LDFLAGS = $(KNOTD_MOD_LDFLAGS)
knot_modules_dnstap_la_CPPFLAGS = $(KNOTD_MOD_CPPFLAGS) $(DNSTAP_CFLAGS)
knot_modules_dnstap_la_LIBADD = $(DNSTAP_LIBS) contrib/dnstap/libdnstap.la libcontrib.la
pkglib_LTLIBRARIES += knot/modules/dnstap.la
endif
...@@ -14,39 +14,34 @@ ...@@ -14,39 +14,34 @@
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <sys/stat.h> #include <netinet/in.h>
#include "contrib/dnstap/dnstap.h" #include "contrib/dnstap/dnstap.h"
#include "contrib/dnstap/dnstap.pb-c.h" #include "contrib/dnstap/dnstap.pb-c.h"
#include "contrib/dnstap/message.h" #include "contrib/dnstap/message.h"
#include "contrib/dnstap/writer.h" #include "contrib/dnstap/writer.h"
#include "contrib/mempattern.h"
#include "contrib/time.h" #include "contrib/time.h"
#include "knot/modules/dnstap/dnstap.h" #include "knot/include/module.h"
/* Module configuration scheme. */
#define MOD_SINK "\x04""sink" #define MOD_SINK "\x04""sink"
#define MOD_IDENTITY "\x08""identity" #define MOD_IDENTITY "\x08""identity"
#define MOD_VERSION "\x07""version" #define MOD_VERSION "\x07""version"
#define MOD_QUERIES "\x0B""log-queries" #define MOD_QUERIES "\x0B""log-queries"
#define MOD_RESPONSES "\x0D""log-responses" #define MOD_RESPONSES "\x0D""log-responses"
const yp_item_t scheme_mod_dnstap[] = { const yp_item_t dnstap_conf[] = {
{ C_ID, YP_TSTR, YP_VNONE }, { MOD_SINK, YP_TSTR, YP_VNONE },
{ MOD_SINK, YP_TSTR, YP_VSTR = { "" } },
{ MOD_IDENTITY, YP_TSTR, YP_VNONE }, { MOD_IDENTITY, YP_TSTR, YP_VNONE },
{ MOD_VERSION, YP_TSTR, YP_VSTR = { "Knot DNS " PACKAGE_VERSION } }, { MOD_VERSION, YP_TSTR, YP_VNONE },
{ MOD_QUERIES, YP_TBOOL, YP_VBOOL = { true } }, { MOD_QUERIES, YP_TBOOL, YP_VBOOL = { true } },
{ MOD_RESPONSES, YP_TBOOL, YP_VBOOL = { true } }, { MOD_RESPONSES, YP_TBOOL, YP_VBOOL = { true } },
{ C_COMMENT, YP_TSTR, YP_VNONE },
{ NULL } { NULL }
}; };
int check_mod_dnstap(conf_check_t *args) int dnstap_conf_check(knotd_conf_check_args_t *args)
{ {
conf_val_t sink = conf_rawid_get_txn(args->conf, args->txn, C_MOD_DNSTAP, knotd_conf_t sink = knotd_conf_check_item(args, MOD_SINK);
MOD_SINK, args->id, args->id_len); if (sink.count == 0 || sink.single.string[0] == '\0') {
if (conf_str(&sink)[0] == '\0') {
args->err_str = "no sink specified"; args->err_str = "no sink specified";
return KNOT_EINVAL; return KNOT_EINVAL;
} }
...@@ -62,18 +57,20 @@ typedef struct { ...@@ -62,18 +57,20 @@ typedef struct {
size_t version_len; size_t version_len;
} dnstap_ctx_t; } dnstap_ctx_t;
static int log_message(int state, const knot_pkt_t *pkt, struct query_data *qdata, static knotd_state_t log_message(knotd_state_t state, const knot_pkt_t *pkt,
dnstap_ctx_t *ctx) knotd_qdata_t *qdata, knotd_mod_t *mod)
{ {
assert(pkt && qdata && ctx); assert(pkt && qdata && mod);
/* Skip empty packet. */ /* Skip empty packet. */
if (pkt->size == 0) { if (pkt->size == 0) {
return state; return state;
} }
dnstap_ctx_t *ctx = knotd_mod_ctx(mod);
struct fstrm_iothr_queue *ioq = struct fstrm_iothr_queue *ioq =
fstrm_iothr_get_input_queue_idx(ctx->iothread, qdata->param->thread_id); fstrm_iothr_get_input_queue_idx(ctx->iothread, qdata->params->thread_id);
/* Unless we want to measure the time it takes to process each query, /* Unless we want to measure the time it takes to process each query,
* we can treat Q/R times the same. */ * we can treat Q/R times the same. */
...@@ -87,14 +84,14 @@ static int log_message(int state, const knot_pkt_t *pkt, struct query_data *qdat ...@@ -87,14 +84,14 @@ static int log_message(int state, const knot_pkt_t *pkt, struct query_data *qdat
/* Determine whether we run on UDP/TCP. */ /* Determine whether we run on UDP/TCP. */
int protocol = IPPROTO_TCP; int protocol = IPPROTO_TCP;
if (qdata->param->proc_flags & NS_QUERY_LIMIT_SIZE) { if (qdata->params->flags & KNOTD_QUERY_FLAG_LIMIT_SIZE) {
protocol = IPPROTO_UDP; protocol = IPPROTO_UDP;
} }
/* Create a dnstap message. */ /* Create a dnstap message. */
Dnstap__Message msg; Dnstap__Message msg;
int ret = dt_message_fill(&msg, msgtype, int ret = dt_message_fill(&msg, msgtype,
(const struct sockaddr *)qdata->param->remote, (const struct sockaddr *)qdata->params->remote,
NULL, /* todo: fill me! */ NULL, /* todo: fill me! */
protocol, pkt->wire, pkt->size, &tv, &tv); protocol, pkt->wire, pkt->size, &tv, &tv);
if (ret != KNOT_EOK) { if (ret != KNOT_EOK) {
...@@ -137,19 +134,19 @@ static int log_message(int state, const knot_pkt_t *pkt, struct query_data *qdat ...@@ -137,19 +134,19 @@ static int log_message(int state, const knot_pkt_t *pkt, struct query_data *qdat
} }
/*! \brief Submit message - query. */ /*! \brief Submit message - query. */
static int dnstap_message_log_query(int state, knot_pkt_t *pkt, struct query_data *qdata, static knotd_state_t dnstap_message_log_query(knotd_state_t state, knot_pkt_t *pkt,
void *ctx) knotd_qdata_t *qdata, knotd_mod_t *mod)
{ {
assert(qdata); assert(qdata);
return log_message(state, qdata->query, qdata, ctx); return log_message(state, qdata->query, qdata, mod);
} }
/*! \brief Submit message - response. */ /*! \brief Submit message - response. */
static int dnstap_message_log_response(int state, knot_pkt_t *pkt, struct query_data *qdata, static knotd_state_t dnstap_message_log_response(knotd_state_t state, knot_pkt_t *pkt,
void *ctx) knotd_qdata_t *qdata, knotd_mod_t *mod)
{ {
return log_message(state, pkt, qdata, ctx); return log_message(state, pkt, qdata, mod);
} }
/*! \brief Create a UNIX socket sink. */ /*! \brief Create a UNIX socket sink. */
...@@ -220,44 +217,47 @@ static struct fstrm_writer* dnstap_writer(const char *path) ...@@ -220,44 +217,47 @@ static struct fstrm_writer* dnstap_writer(const char *path)
return dnstap_file_writer(path); return dnstap_file_writer(path);
} }
int dnstap_load(struct query_module *self) int dnstap_load(knotd_mod_t *mod)
{ {
assert(self);
/* Create dnstap context. */ /* Create dnstap context. */
dnstap_ctx_t *ctx = mm_alloc(self->mm, sizeof(*ctx)); dnstap_ctx_t *ctx = calloc(1, sizeof(*ctx));
if (ctx == NULL) { if (ctx == NULL) {
return KNOT_ENOMEM; return KNOT_ENOMEM;
} }
conf_val_t val;
/* Set identity. */ /* Set identity. */
val = conf_mod_get(self->config, MOD_IDENTITY, self->id); knotd_conf_t conf = knotd_conf_mod(mod, MOD_IDENTITY);
if (val.code == KNOT_EOK) { if (conf.count == 1) {
const char *ident = conf_str(&val); ctx->identity = (conf.single.string != NULL) ?
ctx->identity = (ident != NULL) ? strdup(ident) : NULL; strdup(conf.single.string) : NULL;
} else { } else {
ctx->identity = strdup(self->config->hostname); knotd_conf_t host = knotd_conf_env(mod, KNOTD_CONF_ENV_HOSTNAME);
ctx->identity = strdup(host.single.string);
} }
ctx->identity_len = (ctx->identity != NULL) ? strlen(ctx->identity) : 0; ctx->identity_len = (ctx->identity != NULL) ? strlen(ctx->identity) : 0;
/* Set version. */ /* Set version. */
val = conf_mod_get(self->config, MOD_VERSION, self->id); conf = knotd_conf_mod(mod, MOD_VERSION);
ctx->version = strdup(conf_str(&val)); // Default ensures != NULL. if (conf.count == 1) {
ctx->version_len = strlen(ctx->version); ctx->version = (conf.single.string != NULL) ?
strdup(conf.single.string) : NULL;
} else {
knotd_conf_t version = knotd_conf_env(mod, KNOTD_CONF_ENV_VERSION);
ctx->version = strdup(version.single.string);
}
ctx->version_len = (ctx->version != NULL) ? strlen(ctx->version) : 0;
/* Set sink. */ /* Set sink. */
val = conf_mod_get(self->config, MOD_SINK, self->id); conf = knotd_conf_mod(mod, MOD_SINK);
const char *sink = conf_str(&val); const char *sink = conf.single.string;
/* Set log_queries. */ /* Set log_queries. */
val = conf_mod_get(self->config, MOD_QUERIES, self->id); conf = knotd_conf_mod(mod, MOD_QUERIES);
const bool log_queries = conf_bool(&val); const bool log_queries = conf.single.boolean;
/* Set log_responses. */ /* Set log_responses. */
val = conf_mod_get(self->config, MOD_RESPONSES, self->id); conf = knotd_conf_mod(mod, MOD_RESPONSES);
const bool log_responses = conf_bool(&val); const bool log_responses = conf.single.boolean;
/* Initialize the writer and the options. */ /* Initialize the writer and the options. */
struct fstrm_writer *writer = dnstap_writer(sink); struct fstrm_writer *writer = dnstap_writer(sink);
...@@ -272,8 +272,9 @@ int dnstap_load(struct query_module *self) ...@@ -272,8 +272,9 @@ int dnstap_load(struct query_module *self)
} }
/* Initialize queues. */ /* Initialize queues. */
size_t qcount = conf_udp_threads(self->config) + knotd_conf_t udp = knotd_conf_env(mod, KNOTD_CONF_ENV_WORKERS_UDP);
conf_tcp_threads(self->config); knotd_conf_t tcp = knotd_conf_env(mod, KNOTD_CONF_ENV_WORKERS_TCP);
size_t qcount = udp.single.integer + tcp.single.integer;
fstrm_iothr_options_set_num_input_queues(opt, qcount); fstrm_iothr_options_set_num_input_queues(opt, qcount);
/* Create the I/O thread. */ /* Create the I/O thread. */
...@@ -284,35 +285,36 @@ int dnstap_load(struct query_module *self) ...@@ -284,35 +285,36 @@ int dnstap_load(struct query_module *self)
goto fail; goto fail;
} }
self->ctx = ctx; knotd_mod_ctx_set(mod, ctx);
/* Hook to the query plan. */ /* Hook to the query plan. */
if (log_queries) { if (log_queries) {
query_module_step(self, QPLAN_BEGIN, dnstap_message_log_query); knotd_mod_hook(mod, KNOTD_STAGE_BEGIN, dnstap_message_log_query);
} }
if (log_responses) { if (log_responses) {
query_module_step(self, QPLAN_END, dnstap_message_log_response); knotd_mod_hook(mod, KNOTD_STAGE_END, dnstap_message_log_response);
} }
return KNOT_EOK; return KNOT_EOK;
fail: fail:
MODULE_ERR(C_MOD_DNSTAP, "failed to init sink '%s'", sink);