Commit ee7db495 authored by Daniel Salzman's avatar Daniel Salzman

layer: make the interface static inline + remove automatic fail callback execution

parent 8c29d7e9
...@@ -280,7 +280,6 @@ src/knot/nameserver/xfr.c ...@@ -280,7 +280,6 @@ src/knot/nameserver/xfr.c
src/knot/nameserver/xfr.h src/knot/nameserver/xfr.h
src/knot/query/capture.c src/knot/query/capture.c
src/knot/query/capture.h src/knot/query/capture.h
src/knot/query/layer.c
src/knot/query/layer.h src/knot/query/layer.h
src/knot/query/query.c src/knot/query/query.c
src/knot/query/query.h src/knot/query/query.h
......
...@@ -323,7 +323,6 @@ libknotd_la_SOURCES = \ ...@@ -323,7 +323,6 @@ libknotd_la_SOURCES = \
knot/nameserver/xfr.h \ knot/nameserver/xfr.h \
knot/query/capture.c \ knot/query/capture.c \
knot/query/capture.h \ knot/query/capture.h \
knot/query/layer.c \
knot/query/layer.h \ knot/query/layer.h \
knot/query/query.c \ knot/query/query.c \
knot/query/query.h \ knot/query/query.h \
......
...@@ -856,7 +856,6 @@ const knot_layer_api_t *process_query_layer(void) ...@@ -856,7 +856,6 @@ const knot_layer_api_t *process_query_layer(void)
.finish = &process_query_finish, .finish = &process_query_finish,
.consume = &process_query_in, .consume = &process_query_in,
.produce = &process_query_out, .produce = &process_query_out,
.fail = &process_query_err
}; };
return &api; return &api;
} }
/* Copyright (C) 2017 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/>.
*/
#include <assert.h>
#include "libknot/attribute.h"
#include "knot/query/layer.h"
/*! \brief Helper for conditional layer call. */
#define LAYER_CALL(layer, func, ...) \
assert(layer->api); \
if (layer->api->func) { \
layer->state = layer->api->func(layer, ##__VA_ARGS__); \
}
void knot_layer_init(knot_layer_t *ctx, knot_mm_t *mm, const knot_layer_api_t *api)
{
memset(ctx, 0, sizeof(*ctx));
ctx->mm = mm;
ctx->api = api;
ctx->state = KNOT_STATE_NOOP;
}
void knot_layer_begin(knot_layer_t *ctx, void *param)
{
LAYER_CALL(ctx, begin, param);
}
void knot_layer_reset(knot_layer_t *ctx)
{
LAYER_CALL(ctx, reset);
}
void knot_layer_finish(knot_layer_t *ctx)
{
LAYER_CALL(ctx, finish);
}
void knot_layer_consume(knot_layer_t *ctx, knot_pkt_t *pkt)
{
LAYER_CALL(ctx, consume, pkt);
}
void knot_layer_produce(knot_layer_t *ctx, knot_pkt_t *pkt)
{
switch (ctx->state) {
case KNOT_STATE_FAIL: LAYER_CALL(ctx, fail, pkt); break;
case KNOT_STATE_PRODUCE:
default: LAYER_CALL(ctx, produce, pkt); break;
}
}
#undef LAYER_STATE_STR
...@@ -54,9 +54,15 @@ struct knot_layer_api { ...@@ -54,9 +54,15 @@ struct knot_layer_api {
int (*finish)(knot_layer_t *ctx); int (*finish)(knot_layer_t *ctx);
int (*consume)(knot_layer_t *ctx, knot_pkt_t *pkt); int (*consume)(knot_layer_t *ctx, knot_pkt_t *pkt);
int (*produce)(knot_layer_t *ctx, knot_pkt_t *pkt); int (*produce)(knot_layer_t *ctx, knot_pkt_t *pkt);
int (*fail)(knot_layer_t *ctx, knot_pkt_t *pkt);
}; };
/*! \brief Helper for conditional layer call. */
#define LAYER_CALL(layer, func, ...) \
assert(layer->api); \
if (layer->api->func) { \
layer->state = layer->api->func(layer, ##__VA_ARGS__); \
}
/*! /*!
* \brief Initialize packet processing context. * \brief Initialize packet processing context.
* *
...@@ -64,29 +70,46 @@ struct knot_layer_api { ...@@ -64,29 +70,46 @@ struct knot_layer_api {
* \param mm Memory context. * \param mm Memory context.
* \param api Layer API. * \param api Layer API.
*/ */
void knot_layer_init(knot_layer_t *ctx, knot_mm_t *mm, const knot_layer_api_t *api); inline static void knot_layer_init(knot_layer_t *ctx, knot_mm_t *mm,
const knot_layer_api_t *api)
{
memset(ctx, 0, sizeof(*ctx));
ctx->mm = mm;
ctx->api = api;
ctx->state = KNOT_STATE_NOOP;
}
/*! /*!
* \brief Prepare packet processing. * \brief Prepare packet processing.
* *
* \param ctx Layer context. * \param ctx Layer context.
* \param param Initialization params. * \param params Initialization params.
*/ */
void knot_layer_begin(knot_layer_t *ctx, void *param); inline static void knot_layer_begin(knot_layer_t *ctx, void *params)
{
LAYER_CALL(ctx, begin, params);
}
/*! /*!
* \brief Reset current packet processing context. * \brief Reset current packet processing context.
* *
* \param ctx Layer context. * \param ctx Layer context.
*/ */
void knot_layer_reset(knot_layer_t *ctx); inline static void knot_layer_reset(knot_layer_t *ctx)
{
LAYER_CALL(ctx, reset);
}
/*! /*!
* \brief Finish and close packet processing context. * \brief Finish and close packet processing context.
* *
* \param ctx Layer context. * \param ctx Layer context.
*/ */
void knot_layer_finish(knot_layer_t *ctx); inline static void knot_layer_finish(knot_layer_t *ctx)
{
LAYER_CALL(ctx, finish);
}
/*! /*!
* \brief Add more data to layer processing. * \brief Add more data to layer processing.
...@@ -94,7 +117,10 @@ void knot_layer_finish(knot_layer_t *ctx); ...@@ -94,7 +117,10 @@ void knot_layer_finish(knot_layer_t *ctx);
* \param ctx Layer context. * \param ctx Layer context.
* \param pkt Data packet. * \param pkt Data packet.
*/ */
void knot_layer_consume(knot_layer_t *ctx, knot_pkt_t *pkt); inline static void knot_layer_consume(knot_layer_t *ctx, knot_pkt_t *pkt)
{
LAYER_CALL(ctx, consume, pkt);
}
/*! /*!
* \brief Generate output from layer. * \brief Generate output from layer.
...@@ -102,4 +128,7 @@ void knot_layer_consume(knot_layer_t *ctx, knot_pkt_t *pkt); ...@@ -102,4 +128,7 @@ void knot_layer_consume(knot_layer_t *ctx, knot_pkt_t *pkt);
* \param ctx Layer context. * \param ctx Layer context.
* \param pkt Data packet. * \param pkt Data packet.
*/ */
void knot_layer_produce(knot_layer_t *ctx, knot_pkt_t *pkt); inline static void knot_layer_produce(knot_layer_t *ctx, knot_pkt_t *pkt)
{
LAYER_CALL(ctx, produce, pkt);
}
...@@ -42,7 +42,7 @@ static const int TIMEOUT = 2000; ...@@ -42,7 +42,7 @@ static const int TIMEOUT = 2000;
/*! \brief Dummy answer processing module. */ /*! \brief Dummy answer processing module. */
const knot_layer_api_t dummy_module = { const knot_layer_api_t dummy_module = {
&begin, &reset, &finish, &in, &out, NULL &begin, &reset, &finish, &in, &out
}; };
static void set_blocking_mode(int sock) static void set_blocking_mode(int sock)
......
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