Commit 8c29d7e9 authored by Daniel Salzman's avatar Daniel Salzman

layer: don't return a state from knot_layer_* (redundant)

parent 1f31ff08
/* Copyright (C) 2016 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
/* 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
......@@ -35,40 +35,33 @@ void knot_layer_init(knot_layer_t *ctx, knot_mm_t *mm, const knot_layer_api_t *a
ctx->state = KNOT_STATE_NOOP;
}
int knot_layer_begin(knot_layer_t *ctx, void *param)
void knot_layer_begin(knot_layer_t *ctx, void *param)
{
LAYER_CALL(ctx, begin, param);
return ctx->state;
}
int knot_layer_reset(knot_layer_t *ctx)
void knot_layer_reset(knot_layer_t *ctx)
{
LAYER_CALL(ctx, reset);
return ctx->state;
}
int knot_layer_finish(knot_layer_t *ctx)
void knot_layer_finish(knot_layer_t *ctx)
{
LAYER_CALL(ctx, finish);
return ctx->state;
}
int knot_layer_consume(knot_layer_t *ctx, knot_pkt_t *pkt)
void knot_layer_consume(knot_layer_t *ctx, knot_pkt_t *pkt)
{
LAYER_CALL(ctx, consume, pkt);
return ctx->state;
}
int knot_layer_produce(knot_layer_t *ctx, knot_pkt_t *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;
}
return ctx->state;
}
#undef LAYER_STATE_STR
/* Copyright (C) 2016 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
/* 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
......@@ -26,34 +26,30 @@
* Each state represents the state machine transition,
* and determines readiness for the next action.
*/
enum knot_layer_state {
typedef enum {
KNOT_STATE_NOOP = 0, //!< Invalid.
KNOT_STATE_CONSUME, //!< Consume data.
KNOT_STATE_PRODUCE, //!< Produce data.
KNOT_STATE_RESET, //!< Restart processing.
KNOT_STATE_DONE, //!< Finished.
KNOT_STATE_FAIL //!< Error.
};
} knot_layer_state_t;
struct knot_layer_api;
typedef struct knot_layer_api knot_layer_api_t;
struct knot_layer;
typedef struct knot_layer knot_layer_t;
/*! \brief Packet processing context. */
struct knot_layer {
typedef struct {
const knot_layer_api_t *api; //!< Layer API.
knot_mm_t *mm; //!< Processing memory context.
enum knot_layer_state state; //!< Processing state.
knot_layer_state_t state; //!< Processing state.
void *data; //!< Module specific.
const struct knot_layer_api *api;
tsig_ctx_t *tsig; //!< TODO: remove
unsigned flags; //!< Custom flags.
};
} knot_layer_t;
/*! \brief Packet processing module API. */
struct knot_layer_api {
int (*begin)(knot_layer_t *ctx, void *module_param);
int (*begin)(knot_layer_t *ctx, void *params);
int (*reset)(knot_layer_t *ctx);
int (*finish)(knot_layer_t *ctx);
int (*consume)(knot_layer_t *ctx, knot_pkt_t *pkt);
......@@ -75,45 +71,35 @@ void knot_layer_init(knot_layer_t *ctx, knot_mm_t *mm, const knot_layer_api_t *a
*
* \param ctx Layer context.
* \param param Initialization params.
*
* \return Layer state.
*/
int knot_layer_begin(knot_layer_t *ctx, void *param);
void knot_layer_begin(knot_layer_t *ctx, void *param);
/*!
* \brief Reset current packet processing context.
*
* \param ctx Layer context.
*
* \return Layer state.
*/
int knot_layer_reset(knot_layer_t *ctx);
void knot_layer_reset(knot_layer_t *ctx);
/*!
* \brief Finish and close packet processing context.
*
* \param ctx Layer context.
*
* \return Layer state.
*/
int knot_layer_finish(knot_layer_t *ctx);
void knot_layer_finish(knot_layer_t *ctx);
/*!
* \brief Add more data to layer processing.
*
* \param ctx Layer context.
* \param pkt Data packet.
*
* \return Layer state.
*/
int knot_layer_consume(knot_layer_t *ctx, knot_pkt_t *pkt);
void knot_layer_consume(knot_layer_t *ctx, knot_pkt_t *pkt);
/*!
* \brief Generate output from layer.
*
* \param ctx Layer context.
* \param pkt Data packet.
*
* \return Layer state.
*/
int knot_layer_produce(knot_layer_t *ctx, knot_pkt_t *pkt);
void knot_layer_produce(knot_layer_t *ctx, knot_pkt_t *pkt);
/* Copyright (C) 2016 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
/* 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
......@@ -263,7 +263,7 @@ static int request_consume(struct knot_requestor *req,
return KNOT_EOK;
}
static bool layer_active(enum knot_layer_state state)
static bool layer_active(knot_layer_state_t state)
{
switch (state) {
case KNOT_STATE_CONSUME:
......
/* Copyright (C) 2016 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
/* 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
......@@ -40,8 +40,8 @@ enum {
* Requestor holds a FIFO of pending queries.
*/
struct knot_requestor {
knot_mm_t *mm; /*!< Memory context. */
struct knot_layer layer; /*!< Response processing layer. */
knot_mm_t *mm; /*!< Memory context. */
knot_layer_t layer; /*!< Response processing layer. */
};
/*! \brief Request data (socket, payload, response, TSIG and endpoints). */
......
......@@ -39,11 +39,10 @@ static void answer_sanity_check(const uint8_t *query,
} else {
skip_block(3, "ns: can't check DNS header");
}
}
/* Resolve query and check answer for sanity (2 TAP tests). */
static void exec_query(knot_layer_t *query_ctx, const char *name,
static void exec_query(knot_layer_t *layer, const char *name,
knot_pkt_t *query,
uint8_t expected_rcode)
{
......@@ -52,18 +51,19 @@ static void exec_query(knot_layer_t *query_ctx, const char *name,
/* Input packet. */
knot_pkt_parse(query, 0);
int state = knot_layer_consume(query_ctx, query);
knot_layer_consume(layer, query);
ok(state == KNOT_STATE_PRODUCE || state == KNOT_STATE_FAIL, "ns: process %s query", name);
ok(layer->state == KNOT_STATE_PRODUCE ||
layer->state == KNOT_STATE_FAIL, "ns: process %s query", name);
/* Create answer. */
state = knot_layer_produce(query_ctx, answer);
if (state == KNOT_STATE_FAIL) {
knot_layer_produce(layer, answer);
if (layer->state == KNOT_STATE_FAIL) {
/* Allow 1 generic error response. */
state = knot_layer_produce(query_ctx, answer);
knot_layer_produce(layer, answer);
}
ok(state == KNOT_STATE_DONE, "ns: answer %s query", name);
ok(layer->state == KNOT_STATE_DONE, "ns: answer %s query", name);
/* Check answer. */
answer_sanity_check(query->wire, answer->wire, answer->size, expected_rcode, name);
......@@ -164,19 +164,19 @@ int main(int argc, char *argv[])
knot_pkt_put_question(query, ROOT_DNAME, KNOT_CLASS_IN, KNOT_RRTYPE_SOA);
size_t orig_query_size = query->size;
query->size = KNOT_WIRE_HEADER_SIZE - 1;
int state = knot_layer_consume(&proc, query);
ok(state == KNOT_STATE_NOOP, "ns: IN/less-than-header query ignored");
knot_layer_consume(&proc, query);
ok(proc.state == KNOT_STATE_NOOP, "ns: IN/less-than-header query ignored");
query->size = orig_query_size;
/* Query processor (response, ignore). */
knot_layer_reset(&proc);
knot_wire_set_qr(query->wire);
state = knot_layer_consume(&proc, query);
ok(state == KNOT_STATE_NOOP, "ns: IN/less-than-header query ignored");
knot_layer_consume(&proc, query);
ok(proc.state == KNOT_STATE_NOOP, "ns: IN/less-than-header query ignored");
/* Finish. */
state = knot_layer_finish(&proc);
ok(state == KNOT_STATE_NOOP, "ns: processing end" );
knot_layer_finish(&proc);
ok(proc.state == KNOT_STATE_NOOP, "ns: processing end" );
/* Cleanup. */
mp_delete((struct mempool *)mm.ctx);
......
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