layer.h 4.2 KB
Newer Older
1
/*  Copyright (C) 2014-2017 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
2

Marek Vavruša's avatar
Marek Vavruša committed
3 4 5 6
    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.
7

Marek Vavruša's avatar
Marek Vavruša committed
8 9 10 11
    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.
12

Marek Vavruša's avatar
Marek Vavruša committed
13
    You should have received a copy of the GNU General Public License
14
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
Marek Vavruša's avatar
Marek Vavruša committed
15
 */
16 17 18

#pragma once

19
#include "kresconfig.h"
20
#include "lib/defines.h"
21
#include "lib/utils.h"
22

23 24
#ifdef NOVERBOSELOG
	#define QRVERBOSE(query, cls, ...)
25
#else
26 27 28 29 30 31 32 33 34 35 36 37 38
	/** Print a debug message related to resolution.
	 * \param _query	associated kr_query, may be NULL
	 * \param _cls	identifying string, typically of length exactly four (padded)
	 * \param ...	printf-compatible list of parameters
	 */
	#define QRVERBOSE(_query, _cls, ...) do { \
		const struct kr_query *_qry = (_query); \
		if (kr_log_trace_enabled(_qry)) { \
			kr_log_trace(_qry, (_cls), __VA_ARGS__); \
		} else if (VERBOSE_STATUS) { \
			kr_log_qverbose_impl(_qry, (_cls), __VA_ARGS__); \
		}  \
	} while (false)
39 40
#endif

41 42
/** Layer processing states.  Only one value at a time (but see TODO).
 *
43 44
 *  Each state represents the state machine transition,
 *  and determines readiness for the next action.
45 46 47
 *  See struct kr_layer_api for the actions.
 *
 *  TODO: the cookie module sometimes sets (_FAIL | _DONE) on purpose (!)
48
 */
49 50 51
enum kr_layer_state {
	KR_STATE_CONSUME = 1 << 0, /*!< Consume data. */
	KR_STATE_PRODUCE = 1 << 1, /*!< Produce data. */
52 53 54 55 56 57

	/*! Finished successfully or a special case: in CONSUME phase this can
	 * be used (by iterator) to do a transition to PRODUCE phase again,
	 * in which case the packet wasn't accepted for some reason. */
	KR_STATE_DONE    = 1 << 2,

58 59
	KR_STATE_FAIL    = 1 << 3, /*!< Error. */
	KR_STATE_YIELD   = 1 << 4, /*!< Paused, waiting for a sub-query. */
60 61 62
};

/* Forward declarations. */
63
struct kr_layer_api;
64

65
/** Packet processing context. */
66
typedef struct kr_layer {
67 68
	int state; /*!< The current state; bitmap of enum kr_layer_state. */
	struct kr_request *req; /*!< The corresponding request. */
69
	const struct kr_layer_api *api;
70 71 72
	knot_pkt_t *pkt; /*!< In glue for lua kr_layer_api it's used to pass the parameter. */
	struct sockaddr *dst; /*!< In glue for checkout layer it's used to pass the parameter. */
	bool is_stream;       /*!< In glue for checkout layer it's used to pass the parameter. */
73
} kr_layer_t;
74

75
/** Packet processing module API.  All functions return the new kr_layer_state. */
76
struct kr_layer_api {
77 78 79
      	/** Start of processing the DNS request. */
	int (*begin)(kr_layer_t *ctx);

80
	int (*reset)(kr_layer_t *ctx);
81 82

	/** Paired to begin, called both on successes and failures. */
83
	int (*finish)(kr_layer_t *ctx);
84

85 86
	/** Processing an answer from upstream or the answer to the request.
	 * Lua API: call is omitted iff (state & KR_STATE_FAIL). */
87
	int (*consume)(kr_layer_t *ctx, knot_pkt_t *pkt);
88

89 90
	/** Produce either an answer to the request or a query for upstream (or fail).
	 * Lua API: call is omitted iff (state & KR_STATE_FAIL). */
91
	int (*produce)(kr_layer_t *ctx, knot_pkt_t *pkt);
92

93 94
	/** Finalises the outbound query packet with the knowledge of the IP addresses.
	 * The checkout layer doesn't persist the state, so canceled subrequests
95 96
	 * don't affect the resolution or rest of the processing.
	 * Lua API: call is omitted iff (state & KR_STATE_FAIL). */
97 98
	int (*checkout)(kr_layer_t *ctx, knot_pkt_t *packet, struct sockaddr *dst, int type);

99 100 101 102
	/** Finalises the answer.
	 * Last chance to affect what will get into the answer, including EDNS.*/
	int (*answer_finalize)(kr_layer_t *ctx);

103
	/** The C module can store anything in here. */
104
	void *data;
105 106 107

	/** Internal to ./daemon/ffimodule.c. */
	int cb_slots[];
108 109
};

110
typedef struct kr_layer_api kr_layer_api_t;
111

112 113 114
/** Pickled layer state (api, input, state). */
struct kr_layer_pickle {
    struct kr_layer_pickle *next;
115
    const struct kr_layer_api *api;
116 117 118 119
    knot_pkt_t *pkt;
    unsigned state;
};