max_age_len = asprintf(&max_age, "%s%" PRIu32, directive_max_age, ctx->payload.ttl);
kr_require(max_age_len >= 0);
- /* TODO: add a per-protolayer_grp option for content-type if we
+ /* TODO: add a per-kr_proto option for content-type if we
* need to support protocols other than DNS here */
push_nv(&hdrs, MAKE_STATIC_NV("content-type", "application/dns-message"));
push_nv(&hdrs, MAKE_STATIC_KEY_NV("content-length", size, size_len));
memcpy(send_ctx->data, data, length);
kr_log_debug(DOH, "[%p] send_callback: %p\n", (void *)h2, (void *)send_ctx->data);
- session2_wrap_after(http->h.session, PROTOLAYER_PROTOCOL_HTTP,
+ session2_wrap_after(http->h.session, PROTOLAYER_TYPE_HTTP,
protolayer_buffer(send_ctx->data, length, false), NULL,
callback_finished_free_baton, send_ctx);
dest_iov[cur++] = (struct iovec){ (void *)padding, padlen - 1 };
kr_assert(cur == iovcnt);
- int ret = session2_wrap_after(http->h.session, PROTOLAYER_PROTOCOL_HTTP,
+ int ret = session2_wrap_after(http->h.session, PROTOLAYER_TYPE_HTTP,
protolayer_iovec(dest_iov, cur, false),
NULL, callback_finished_free_baton, sdctx);
}
ret = 0;
- session2_unwrap_after(ctx->h.session, PROTOLAYER_PROTOCOL_HTTP,
+ session2_unwrap_after(ctx->h.session, PROTOLAYER_TYPE_HTTP,
protolayer_wire_buf(wb, false), NULL, NULL, NULL);
cleanup:
http_cleanup_stream(ctx);
void http_protolayers_init(void)
{
- protolayer_globals[PROTOLAYER_PROTOCOL_HTTP] = (struct protolayer_globals) {
+ protolayer_globals[PROTOLAYER_TYPE_HTTP] = (struct protolayer_globals) {
.sess_size = sizeof(struct pl_http_sess_data),
.sess_deinit = pl_http_sess_deinit,
.wire_buf_overhead = HTTP_MAX_FRAME_SIZE,
void io_protolayers_init(void)
{
- protolayer_globals[PROTOLAYER_PROTOCOL_UDP] = (struct protolayer_globals){
+ protolayer_globals[PROTOLAYER_TYPE_UDP] = (struct protolayer_globals){
.iter_size = sizeof(struct pl_udp_iter_data),
.unwrap = pl_udp_unwrap,
.event_wrap = pl_udp_event_wrap,
};
- protolayer_globals[PROTOLAYER_PROTOCOL_TCP] = (struct protolayer_globals){
+ protolayer_globals[PROTOLAYER_TYPE_TCP] = (struct protolayer_globals){
.sess_size = sizeof(struct pl_tcp_sess_data),
.sess_init = pl_tcp_sess_init,
.sess_deinit = pl_tcp_sess_deinit,
uv_handle_t *h = (uv_handle_t *)handle;
check_bufsize(h);
/* Handle is already created, just create context. */
- struct session2 *s = session2_new_io(h, PROTOLAYER_GRP_DOUDP, NULL, 0, false);
+ struct session2 *s = session2_new_io(h, KR_PROTO_UDP53, NULL, 0, false);
kr_require(s);
int socklen = sizeof(union kr_sockaddr);
NULL, NULL, NULL);
}
-static void _tcp_accept(uv_stream_t *master, int status, enum protolayer_grp grp)
+static void tcp_accept_internal(uv_stream_t *master, int status, enum kr_proto grp)
{
if (status != 0) {
return;
static void tcp_accept(uv_stream_t *master, int status)
{
- _tcp_accept(master, status, PROTOLAYER_GRP_DOTCP);
+ tcp_accept_internal(master, status, KR_PROTO_TCP53);
}
static void tls_accept(uv_stream_t *master, int status)
{
- _tcp_accept(master, status, PROTOLAYER_GRP_DOTLS);
+ tcp_accept_internal(master, status, KR_PROTO_DOT);
}
#if ENABLE_DOH2
static void https_accept(uv_stream_t *master, int status)
{
- _tcp_accept(master, status, PROTOLAYER_GRP_DOHTTPS);
+ tcp_accept_internal(master, status, KR_PROTO_DOH);
}
#endif
return kr_error(ret);
}
- xhd->session = session2_new_io(ep->handle, PROTOLAYER_GRP_DOUDP,
+ xhd->session = session2_new_io(ep->handle, KR_PROTO_UDP53,
NULL, 0, false);
kr_require(xhd->session);
session2_get_sockname(xhd->session)->sa_family = AF_XDP; // to have something in there
#endif
int io_create(uv_loop_t *loop, struct session2 **out_session, int type,
- unsigned family, enum protolayer_grp grp,
+ unsigned family, enum kr_proto grp,
struct protolayer_data_param *layer_param,
size_t layer_param_count, bool outgoing)
{
* \param family = AF_*
* \param has_tls has meanings only when type is SOCK_STREAM */
int io_create(uv_loop_t *loop, struct session2 **out_session, int type,
- unsigned family, enum protolayer_grp grp,
+ unsigned family, enum kr_proto grp,
struct protolayer_data_param *layer_param,
size_t layer_param_count, bool outgoing);
void io_free(uv_handle_t *handle);
static uint32_t next_log_id = 1;
-struct protolayer_globals protolayer_globals[PROTOLAYER_PROTOCOL_COUNT] = {{0}};
+struct protolayer_globals protolayer_globals[PROTOLAYER_TYPE_COUNT] = {{0}};
-static const enum protolayer_protocol protolayer_grp_doudp[] = {
- PROTOLAYER_PROTOCOL_UDP,
- PROTOLAYER_PROTOCOL_DNS_DGRAM,
- PROTOLAYER_PROTOCOL_NULL
+static const enum protolayer_type protolayer_grp_udp53[] = {
+ PROTOLAYER_TYPE_UDP,
+ PROTOLAYER_TYPE_DNS_DGRAM,
+ PROTOLAYER_TYPE_NULL
};
-static const enum protolayer_protocol protolayer_grp_dotcp[] = {
- PROTOLAYER_PROTOCOL_TCP,
- PROTOLAYER_PROTOCOL_DNS_MULTI_STREAM,
- PROTOLAYER_PROTOCOL_NULL
+static const enum protolayer_type protolayer_grp_tcp53[] = {
+ PROTOLAYER_TYPE_TCP,
+ PROTOLAYER_TYPE_DNS_MULTI_STREAM,
+ PROTOLAYER_TYPE_NULL
};
-static const enum protolayer_protocol protolayer_grp_dot[] = {
- PROTOLAYER_PROTOCOL_TCP,
- PROTOLAYER_PROTOCOL_TLS,
- PROTOLAYER_PROTOCOL_DNS_MULTI_STREAM,
- PROTOLAYER_PROTOCOL_NULL
+static const enum protolayer_type protolayer_grp_dot[] = {
+ PROTOLAYER_TYPE_TCP,
+ PROTOLAYER_TYPE_TLS,
+ PROTOLAYER_TYPE_DNS_MULTI_STREAM,
+ PROTOLAYER_TYPE_NULL
};
-static const enum protolayer_protocol protolayer_grp_doh[] = {
- PROTOLAYER_PROTOCOL_TCP,
- PROTOLAYER_PROTOCOL_TLS,
- PROTOLAYER_PROTOCOL_HTTP,
- PROTOLAYER_PROTOCOL_DNS_UNSIZED_STREAM,
- PROTOLAYER_PROTOCOL_NULL
+static const enum protolayer_type protolayer_grp_doh[] = {
+ PROTOLAYER_TYPE_TCP,
+ PROTOLAYER_TYPE_TLS,
+ PROTOLAYER_TYPE_HTTP,
+ PROTOLAYER_TYPE_DNS_UNSIZED_STREAM,
+ PROTOLAYER_TYPE_NULL
};
-/** Sequences of layers, mapped by `enum protolayer_grp`.
+static const enum protolayer_type protolayer_grp_doq[] = {
+ // not yet used
+ PROTOLAYER_TYPE_NULL
+};
+
+/** Sequences of layers, or groups, mapped by `enum kr_proto`.
+ *
+ * Each group represents a sequence of layers in the unwrap direction (wrap
+ * direction being the opposite). The sequence dictates the order in which
+ * individual layers are processed. This macro is used to generate global data
+ * about groups.
*
- * To define a new group, add a new entry in the `PROTOLAYER_GRP_MAP` macro and
+ * To define a new group, add a new entry in the `KR_PROTO_MAP()` macro and
* create a new static `protolayer_grp_*` array above, similarly to the already
- * existing ones. Each array must end with `PROTOLAYER_GRP_NULL`, to indicate
- * the end of the list of protocol layers. The array name's suffix must be the
- * one defined as *Variable name* (2nd parameter) in the `PROTOLAYER_GRP_MAP`
- * macro. */
-static const enum protolayer_protocol *protolayer_grps[PROTOLAYER_GRP_COUNT] = {
-#define XX(cid, vid, name) [PROTOLAYER_GRP_ ## cid] = protolayer_grp_ ## vid,
- PROTOLAYER_GRP_MAP(XX)
+ * existing ones. Each array must end with `PROTOLAYER_TYPE_NULL`, to
+ * indicate the end of the list of protocol layers. The array name's suffix must
+ * be the one defined as *Variable name* (2nd parameter) in the
+ * `KR_PROTO_MAP` macro. */
+static const enum protolayer_type *protolayer_grps[KR_PROTO_COUNT] = {
+#define XX(cid, vid, name) [KR_PROTO_##cid] = protolayer_grp_##vid,
+ KR_PROTO_MAP(XX)
#undef XX
};
-const char *protolayer_protocol_name(enum protolayer_protocol p)
+const char *protolayer_layer_name(enum protolayer_type p)
{
switch (p) {
- case PROTOLAYER_PROTOCOL_NULL:
+ case PROTOLAYER_TYPE_NULL:
return "(null)";
-#define XX(cid) case PROTOLAYER_PROTOCOL_ ## cid: \
+#define XX(cid) case PROTOLAYER_TYPE_ ## cid: \
return #cid;
- PROTOLAYER_PROTOCOL_MAP(XX)
-#undef XX
- default:
- return "(invalid)";
- }
-}
-
-const char *protolayer_grp_name(enum protolayer_grp g)
-{
- switch (g) {
- case PROTOLAYER_GRP_NULL:
- return "(null)";
-#define XX(cid, vid, name) case PROTOLAYER_GRP_ ## cid: \
- return (name);
- PROTOLAYER_GRP_MAP(XX)
+ PROTOLAYER_TYPE_MAP(XX)
#undef XX
default:
return "(invalid)";
}
static inline ssize_t protolayer_manager_get_protocol(
- struct protolayer_manager *m, enum protolayer_protocol protocol)
+ struct protolayer_manager *m, enum protolayer_type protocol)
{
for (ssize_t i = 0; i < m->num_layers; i++) {
- enum protolayer_protocol found = protolayer_grps[m->grp][i];
+ enum protolayer_type found = protolayer_grps[m->grp][i];
if (protocol == found)
return i;
}
ctx->layer_ix--;
}
-static inline const char *layer_name(enum protolayer_grp grp, ssize_t layer_ix)
+static inline const char *layer_name(enum kr_proto grp, ssize_t layer_ix)
{
- if (grp >= PROTOLAYER_GRP_COUNT)
+ if (grp >= KR_PROTO_COUNT)
return "(invalid)";
- enum protolayer_protocol p = protolayer_grps[grp][layer_ix];
- return protolayer_protocol_name(p);
+ enum protolayer_type p = protolayer_grps[grp][layer_ix];
+ return protolayer_layer_name(p);
}
static inline const char *layer_name_ctx(struct protolayer_iter_ctx *ctx)
if (ret)
VERBOSE_LOG(session, "layer context of group '%s' (on %u: %s) ended with return code %d\n",
- protolayer_grp_name(ctx->manager->grp),
+ kr_proto_name(ctx->manager->grp),
ctx->layer_ix, layer_name_ctx(ctx), ret);
if (ctx->status)
VERBOSE_LOG(session, "iteration of group '%s' (on %u: %s) ended with status %d\n",
- protolayer_grp_name(ctx->manager->grp),
+ kr_proto_name(ctx->manager->grp),
ctx->layer_ix, layer_name_ctx(ctx), ctx->status);
if (ctx->finished_cb)
static int protolayer_step(struct protolayer_iter_ctx *ctx)
{
while (true) {
- if (kr_fails_assert(ctx->manager->grp < PROTOLAYER_GRP_COUNT))
+ if (kr_fails_assert(ctx->manager->grp < KR_PROTO_COUNT))
return kr_error(EFAULT);
- enum protolayer_protocol protocol = protolayer_grps[ctx->manager->grp][ctx->layer_ix];
+ enum protolayer_type protocol = protolayer_grps[ctx->manager->grp][ctx->layer_ix];
struct protolayer_globals *globals = &protolayer_globals[protocol];
ctx->async_mode = false;
VERBOSE_LOG(manager->session,
"%s submitted to grp '%s' in %s direction (%zu: %s)\n",
protolayer_payload_name(payload.type),
- protolayer_grp_name(manager->grp),
+ kr_proto_name(manager->grp),
(direction == PROTOLAYER_UNWRAP) ? "unwrap" : "wrap",
layer_ix, layer_name(manager->grp, layer_ix));
};
for (size_t i = 0; i < manager->num_layers; i++) {
- if (kr_fails_assert(ctx->manager->grp < PROTOLAYER_GRP_COUNT))
+ if (kr_fails_assert(ctx->manager->grp < KR_PROTO_COUNT))
return kr_error(EFAULT);
- enum protolayer_protocol p = protolayer_grps[manager->grp][i];
+ enum protolayer_type p = protolayer_grps[manager->grp][i];
struct protolayer_globals *globals = &protolayer_globals[p];
struct protolayer_data *iter_data = protolayer_iter_data_get(ctx, i);
if (iter_data) {
return protolayer_step(ctx);
}
-static void *get_init_param(enum protolayer_protocol p,
+static void *get_init_param(enum protolayer_type p,
struct protolayer_data_param *layer_param,
size_t layer_param_count)
{
/** Allocates and initializes a new manager. */
static struct protolayer_manager *protolayer_manager_new(
struct session2 *s,
- enum protolayer_grp grp,
+ enum kr_proto grp,
struct protolayer_data_param *layer_param,
size_t layer_param_count)
{
size_t manager_size = sizeof(struct protolayer_manager);
size_t cb_ctx_size = sizeof(struct protolayer_iter_ctx);
- const enum protolayer_protocol *protocols = protolayer_grps[grp];
+ const enum protolayer_type *protocols = protolayer_grps[grp];
if (kr_fails_assert(protocols))
return NULL;
- const enum protolayer_protocol *p = protocols;
+ const enum protolayer_type *p = protocols;
/* Space for offset index */
for (; *p; p++)
if (!m) return;
for (size_t i = 0; i < m->num_layers; i++) {
- enum protolayer_protocol p = protolayer_grps[m->grp][i];
+ enum protolayer_type p = protolayer_grps[m->grp][i];
struct protolayer_globals *globals = &protolayer_globals[p];
if (globals->sess_deinit) {
struct protolayer_data *sess_data = protolayer_sess_data_get(m, i);
struct session2 *session2_new(enum session2_transport_type transport_type,
- enum protolayer_grp layer_grp,
+ enum kr_proto layer_grp,
struct protolayer_data_param *layer_param,
size_t layer_param_count,
bool outgoing)
payload, comm, cb, baton);
}
-int session2_unwrap_after(struct session2 *s, enum protolayer_protocol protocol,
+int session2_unwrap_after(struct session2 *s, enum protolayer_type protocol,
struct protolayer_payload payload,
const struct comm_info *comm,
protolayer_finished_cb cb, void *baton)
payload, comm, cb, baton);
}
-int session2_wrap_after(struct session2 *s, enum protolayer_protocol protocol,
+int session2_wrap_after(struct session2 *s, enum protolayer_type protocol,
struct protolayer_payload payload,
const struct comm_info *comm,
protolayer_finished_cb cb, void *baton)
bool cont;
struct protolayer_manager *m = s->layers;
for (ssize_t i = m->num_layers - 1; i >= 0; i--) {
- enum protolayer_protocol p = protolayer_grps[s->layers->grp][i];
+ enum protolayer_type p = protolayer_grps[s->layers->grp][i];
struct protolayer_globals *globals = &protolayer_globals[p];
if (globals->event_wrap) {
struct protolayer_data *sess_data = protolayer_sess_data_get(m, i);
bool cont;
struct protolayer_manager *m = s->layers;
for (ssize_t i = start_ix; i < m->num_layers; i++) {
- enum protolayer_protocol p = protolayer_grps[s->layers->grp][i];
+ enum protolayer_type p = protolayer_grps[s->layers->grp][i];
struct protolayer_globals *globals = &protolayer_globals[p];
if (globals->event_unwrap) {
struct protolayer_data *sess_data = protolayer_sess_data_get(m, i);
session2_event_unwrap(s, 0, event, baton);
}
-void session2_event_after(struct session2 *s, enum protolayer_protocol protocol,
+void session2_event_after(struct session2 *s, enum protolayer_type protocol,
enum protolayer_event_type event, void *baton)
{
ssize_t start_ix = protolayer_manager_get_protocol(s->layers, protocol);
{
struct protolayer_manager *m = s->layers;
for (ssize_t i = 0; i < m->num_layers; i++) {
- enum protolayer_protocol p = protolayer_grps[s->layers->grp][i];
+ enum protolayer_type p = protolayer_grps[s->layers->grp][i];
struct protolayer_globals *globals = &protolayer_globals[p];
if (globals->request_init) {
struct protolayer_data *sess_data = protolayer_sess_data_get(m, i);
*/
/* HINT: If you are looking to implement support for a new transport protocol,
- * start with the doc comment of the `PROTOLAYER_PROTOCOL_MAP` macro and
+ * start with the doc comment of the `PROTOLAYER_TYPE_MAP` macro and
* continue from there. */
/* GLOSSARY:
#include "contrib/mempattern.h"
#include "lib/generic/queue.h"
#include "lib/generic/trie.h"
+#include "lib/proto.h"
#include "lib/utils.h"
/* Forward declarations */
/** Protocol layer types map - an enumeration of individual protocol layer
* implementations
*
- * This macro is used to generate `enum protolayer_protocol` as well as other
+ * This macro is used to generate `enum protolayer_type` as well as other
* additional data on protocols, e.g. name string constants.
*
* To define a new protocol, add a new identifier to this macro, and, within
*
* To use protocols within sessions, protocol layer groups also need to be
* defined, to indicate the order in which individual protocols are to be
- * processed. See `PROTOLAYER_GRP_MAP` below for more details. */
-#define PROTOLAYER_PROTOCOL_MAP(XX) \
+ * processed. See `KR_PROTO_MAP` below for more details. */
+#define PROTOLAYER_TYPE_MAP(XX) \
/* General transport protocols */\
XX(UDP)\
XX(TCP)\
* stream (may span multiple (un)wraps). */
/** The identifiers of protocol layer types. */
-enum protolayer_protocol {
- PROTOLAYER_PROTOCOL_NULL = 0,
-#define XX(cid) PROTOLAYER_PROTOCOL_ ## cid,
- PROTOLAYER_PROTOCOL_MAP(XX)
+enum protolayer_type {
+ PROTOLAYER_TYPE_NULL = 0,
+#define XX(cid) PROTOLAYER_TYPE_ ## cid,
+ PROTOLAYER_TYPE_MAP(XX)
#undef XX
- PROTOLAYER_PROTOCOL_COUNT /* must be the last! */
+ PROTOLAYER_TYPE_COUNT /* must be the last! */
};
/** Gets the constant string name of the specified protocol. */
-const char *protolayer_protocol_name(enum protolayer_protocol p);
-
-/** Protocol layer group map
- *
- * This macro is used to generate `enum protolayer_grp` as well as other
- * additional data on protocol layer groups, e.g. name string constants.
- *
- * Each group represents a sequence of layers in the unwrap direction (wrap
- * direction being the opposite). The sequence dictates the order in which
- * individual layers are processed. This macro is used to generate global data
- * about groups.
- *
- * For defining new groups, see the docs of `protolayer_grps[]` in
- * `daemon/session2.h`.
- *
- * TODO: probably unify enum protolayer_grp with enum kr_proto.
- *
- * Parameters for XX are:
- * 1. Constant name (for e.g. PROTOLAYER_GRP_* enum value identifiers)
- * 2. Variable name (for e.g. protolayer_grp_* array identifiers - defined in
- * `session2.c`)
- * 3. Human-readable name for logging */
-#define PROTOLAYER_GRP_MAP(XX) \
- XX(DOUDP, doudp, "DNS UDP") \
- XX(DOTCP, dotcp, "DNS TCP") \
- XX(DOTLS, dot, "DNS-over-TLS") \
- XX(DOHTTPS, doh, "DNS-over-HTTPS")
-
-/** The identifiers of pre-defined protocol layer sequences. */
-enum protolayer_grp {
- PROTOLAYER_GRP_NULL = 0,
-#define XX(cid, vid, name) PROTOLAYER_GRP_ ## cid,
- PROTOLAYER_GRP_MAP(XX)
-#undef XX
- PROTOLAYER_GRP_COUNT
-};
-
-/** Gets the constant string name of the specified protocol layer group. */
-const char *protolayer_grp_name(enum protolayer_grp g);
+const char *protolayer_layer_name(enum protolayer_type p);
/** Flow control indicators for protocol layer `wrap` and `unwrap` callbacks.
* Use via `protolayer_continue`, `protolayer_break`, and `protolayer_push`
* `grp`), which define how the data processed by the session is to be
* interpreted. */
struct protolayer_manager {
- enum protolayer_grp grp;
+ enum kr_proto grp;
struct wire_buf wire_buf;
size_t wire_buf_max_length;
struct session2 *session;
/** Initialization parameters for protocol layer session data. */
struct protolayer_data_param {
- enum protolayer_protocol protocol; /**< Which protocol these parameters
+ enum protolayer_type protocol; /**< Which protocol these parameters
* are meant for. */
void *param; /**< Pointer to protolayer-related initialization
* parameters. Only needs to be valid during session
protolayer_request_cb request_init;
};
-/** Global data about layered protocols. Mapped by `enum protolayer_protocol`.
+/** Global data about layered protocols. Mapped by `enum protolayer_type`.
* Individual protocols are to be initialized during resolver startup. */
-extern struct protolayer_globals protolayer_globals[PROTOLAYER_PROTOCOL_COUNT];
+extern struct protolayer_globals protolayer_globals[PROTOLAYER_TYPE_COUNT];
/** *Layer sequence return function* - signalizes the protolayer manager to
* individual layer implementations to determine the lifetime of the data
* pointed to by the parameters. */
struct session2 *session2_new(enum session2_transport_type transport_type,
- enum protolayer_grp layer_grp,
+ enum kr_proto layer_grp,
struct protolayer_data_param *layer_param,
size_t layer_param_count,
bool outgoing);
/** Allocates and initializes a new session with the specified protocol layer
* group, using a *libuv handle* as its transport. */
static inline struct session2 *session2_new_io(uv_handle_t *handle,
- enum protolayer_grp layer_grp,
+ enum kr_proto layer_grp,
struct protolayer_data_param *layer_param,
size_t layer_param_count,
bool outgoing)
/** Allocates and initializes a new session with the specified protocol layer
* group, using a *parent session* as its transport. */
static inline struct session2 *session2_new_child(struct session2 *parent,
- enum protolayer_grp layer_grp,
+ enum kr_proto layer_grp,
struct protolayer_data_param *layer_param,
size_t layer_param_count,
bool outgoing)
*
* Layers may use this to generate their own data to send in the sequence, e.g.
* for protocol-specific ceremony. */
-int session2_unwrap_after(struct session2 *s, enum protolayer_protocol protocol,
+int session2_unwrap_after(struct session2 *s, enum protolayer_type protocol,
struct protolayer_payload payload,
const struct comm_info *comm,
protolayer_finished_cb cb, void *baton);
*
* Layers may use this to generate their own data to send in the sequence, e.g.
* for protocol-specific ceremony. */
-int session2_wrap_after(struct session2 *s, enum protolayer_protocol protocol,
+int session2_wrap_after(struct session2 *s, enum protolayer_type protocol,
struct protolayer_payload payload,
const struct comm_info *comm,
protolayer_finished_cb cb, void *baton);
* NOTE: The bounced iteration does not exclude any layers - the layer
* specified by `protocol` and those before it are only skipped in the
* `_UNWRAP` direction! */
-void session2_event_after(struct session2 *s, enum protolayer_protocol protocol,
+void session2_event_after(struct session2 *s, enum protolayer_type protocol,
enum protolayer_event_type event, void *baton);
/** Sends a `PROTOLAYER_EVENT_CLOSE` event to be processed by the protocol
#define EPHEMERAL_CERT_EXPIRATION_SECONDS_RENEW_BEFORE ((time_t)60*60*24*7)
#define GNUTLS_PIN_MIN_VERSION 0x030400
#define UNWRAP_BUF_SIZE 131072
-#define TLS_CHUNK_SIZE (16 * 1024)
+#define TLS_CHUNK_SIZE ((size_t)16 * 1024)
#define VERBOSE_MSG(cl_side, ...)\
if (cl_side) \
else \
kr_log_debug(TLS, __VA_ARGS__);
-static const gnutls_datum_t tls_grp_alpn[PROTOLAYER_GRP_COUNT] = {
- [PROTOLAYER_GRP_DOTLS] = { (uint8_t *)"dot", 3 },
- [PROTOLAYER_GRP_DOHTTPS] = { (uint8_t *)"h2", 2 },
+static const gnutls_datum_t tls_grp_alpn[KR_PROTO_COUNT] = {
+ [KR_PROTO_DOT] = { (uint8_t *)"dot", 3 },
+ [KR_PROTO_DOH] = { (uint8_t *)"h2", 2 },
};
typedef enum tls_client_hs_state {
push_ctx->sess_data = tls;
memcpy(push_ctx->iov, iov, sizeof(struct iovec[iovcnt]));
- session2_wrap_after(tls->h.session, PROTOLAYER_PROTOCOL_TLS,
+ session2_wrap_after(tls->h.session, PROTOLAYER_TYPE_TLS,
protolayer_iovec(push_ctx->iov, iovcnt, true), NULL,
kres_gnutls_push_finished, push_ctx);
}
}
if (!tls->first_handshake_done) {
- session2_event_after(session, PROTOLAYER_PROTOCOL_TLS,
+ session2_event_after(session, PROTOLAYER_TYPE_TLS,
PROTOLAYER_EVENT_CONNECT, NULL);
tls->first_handshake_done = true;
}
void tls_protolayers_init(void)
{
- protolayer_globals[PROTOLAYER_PROTOCOL_TLS] = (struct protolayer_globals){
+ protolayer_globals[PROTOLAYER_TYPE_TLS] = (struct protolayer_globals){
.sess_size = sizeof(struct pl_tls_sess_data),
.sess_deinit = pl_tls_sess_deinit,
.wire_buf_overhead = TLS_CHUNK_SIZE,
/*! @internal Create a UDP/TCP handle for an outgoing AF_INET* connection.
* socktype is SOCK_* */
static struct session2 *ioreq_spawn(int socktype, sa_family_t family,
- enum protolayer_grp grp,
+ enum kr_proto grp,
struct protolayer_data_param *layer_param,
size_t layer_param_count)
{
return ret;
struct session2 *session = ioreq_spawn(SOCK_DGRAM, choice->sin6_family,
- PROTOLAYER_GRP_DOUDP, NULL, 0);
+ KR_PROTO_UDP53, NULL, 0);
if (!session)
return kr_error(EINVAL);
bool has_tls = tls_entry;
if (has_tls) {
struct protolayer_data_param param = {
- .protocol = PROTOLAYER_PROTOCOL_TLS,
+ .protocol = PROTOLAYER_TYPE_TLS,
.param = tls_entry
};
session = ioreq_spawn(SOCK_STREAM, addr->sa_family,
- PROTOLAYER_GRP_DOTLS, ¶m, 1);
+ KR_PROTO_DOT, ¶m, 1);
} else {
session = ioreq_spawn(SOCK_STREAM, addr->sa_family,
- PROTOLAYER_GRP_DOTCP, NULL, 0);
+ KR_PROTO_TCP53, NULL, 0);
}
if (!session) {
free(conn);
kr_bindings_register(the_engine->L); // TODO move
/* DNS protocol layers */
- protolayer_globals[PROTOLAYER_PROTOCOL_DNS_DGRAM] = (struct protolayer_globals){
+ protolayer_globals[PROTOLAYER_TYPE_DNS_DGRAM] = (struct protolayer_globals){
.wire_buf_overhead_cb = pl_dns_dgram_wire_buf_overhead,
.wire_buf_max_overhead = KNOT_WIRE_MAX_PKTSIZE,
.unwrap = pl_dns_dgram_unwrap,
.event_unwrap = pl_dns_dgram_event_unwrap
};
- protolayer_globals[PROTOLAYER_PROTOCOL_DNS_UNSIZED_STREAM] = (struct protolayer_globals){
+ protolayer_globals[PROTOLAYER_TYPE_DNS_UNSIZED_STREAM] = (struct protolayer_globals){
.sess_size = sizeof(struct pl_dns_stream_sess_data),
.wire_buf_overhead = KNOT_WIRE_MAX_PKTSIZE,
.sess_init = pl_dns_stream_sess_init,
.event_unwrap = pl_dns_stream_event_unwrap,
.request_init = pl_dns_stream_request_init
};
- protolayer_globals[PROTOLAYER_PROTOCOL_DNS_MULTI_STREAM] = stream_common;
- protolayer_globals[PROTOLAYER_PROTOCOL_DNS_MULTI_STREAM].sess_init = pl_dns_stream_sess_init;
- protolayer_globals[PROTOLAYER_PROTOCOL_DNS_SINGLE_STREAM] = stream_common;
- protolayer_globals[PROTOLAYER_PROTOCOL_DNS_SINGLE_STREAM].sess_init = pl_dns_single_stream_sess_init;
+ protolayer_globals[PROTOLAYER_TYPE_DNS_MULTI_STREAM] = stream_common;
+ protolayer_globals[PROTOLAYER_TYPE_DNS_MULTI_STREAM].sess_init = pl_dns_stream_sess_init;
+ protolayer_globals[PROTOLAYER_TYPE_DNS_SINGLE_STREAM] = stream_common;
+ protolayer_globals[PROTOLAYER_TYPE_DNS_SINGLE_STREAM].sess_init = pl_dns_single_stream_sess_init;
/* Create main worker. */
the_worker = &the_worker_value;
'layer/iterate.c',
'layer/validate.c',
'log.c',
+ 'proto.c',
'rules/api.c',
'rules/defaults.c',
'rules/forward.c',
'layer/iterate.h',
'log.h',
'module.h',
+ 'proto.h',
'resolve.h',
'resolve-impl.h',
'rplan.h',
--- /dev/null
+/* Copyright (C) CZ.NIC, z.s.p.o. <knot-resolver@labs.nic.cz>
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#include "lib/proto.h"
+
+const char *kr_proto_name(enum kr_proto p)
+{
+ switch (p) {
+ case KR_PROTO_INTERNAL:
+ return "INTERNAL";
+#define XX(cid, vid, name) case KR_PROTO_##cid: \
+ return (name);
+ KR_PROTO_MAP(XX)
+#undef XX
+ default:
+ return "(default)";
+ }
+}
--- /dev/null
+/* Copyright (C) CZ.NIC, z.s.p.o. <knot-resolver@labs.nic.cz>
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+#pragma once
+
+#include <stdint.h>
+
+#include "lib/defines.h"
+
+/** DNS transport protocol map
+ *
+ * This macro is used to generate `enum kr_proto` as well as other additional
+ * data on protocols, like name string constants.
+ *
+ * It defines DNS transport protocols for use by `session2` (to define sequences
+ * of protocol layers) and `rules` (to filter requests based on them). To find
+ * out more, see the individual usages.
+ *
+ * Parameters for XX are:
+ * 1. Constant name (for e.g. KR_PROTO_* enum value identifiers)
+ * 2. Variable name (for e.g. kr_proto_* array identifiers, like those defined
+ * in `session2.c`)
+ * 3. Human-readable name for logging */
+#define KR_PROTO_MAP(XX) \
+ XX(UDP53, udp53, "DNS UDP") \
+ XX(TCP53, tcp53, "DNS TCP") \
+ XX(DOT, dot, "DNS-over-TLS") \
+ XX(DOH, doh, "DNS-over-HTTPS") \
+ XX(DOQ, doq, "DNS-over-QUIC") /* unused for now */ \
+ //
+
+/** DNS protocol set - mutually exclusive options, contrary to
+ * kr_request_qsource_flags
+ *
+ * The XDP flag is not discerned here, as it could apply to any protocol. (Not
+ * right now, but libknot does support it for TCP, so that would complete
+ * everything)
+ */
+enum kr_proto {
+ KR_PROTO_INTERNAL = 0, /// no protocol, e.g. useful to mark internal requests
+#define XX(cid, vid, name) KR_PROTO_ ## cid,
+ KR_PROTO_MAP(XX)
+#undef XX
+ KR_PROTO_COUNT,
+};
+
+/** Gets the constant string name of the specified transport protocol. */
+KR_EXPORT
+const char *kr_proto_name(enum kr_proto p);
+
+/** Bitmap of enum kr_proto options. */
+typedef uint8_t kr_proto_set;
+static_assert(sizeof(kr_proto_set) * 8 >= KR_PROTO_COUNT, "bad combination of type sizes");
#pragma once
#include "lib/defines.h"
+#include "lib/proto.h"
struct kr_query;
struct kr_request;
struct knot_pkt;
/// Tags "capacity", i.e. numbered from 0 to _CAP - 1.
#define KR_RULE_TAGS_CAP (sizeof(kr_rule_tags_t) * 8)
-/** DNS protocol set - mutually exclusive options, contrary to kr_request_qsource_flags
- *
- * The XDP flag is not discerned here, as it could apply to any protocol.
- * (not right now, but libknot does support it for TCP, so that would complete everything)
- *
- * TODO: probably unify with enum protolayer_grp.
- */
-enum kr_proto {
- KR_PROTO_INTERNAL = 0, /// no protocol, e.g. useful to mark internal requests
- KR_PROTO_UDP53,
- KR_PROTO_TCP53,
- KR_PROTO_DOT,
- KR_PROTO_DOH,
- KR_PROTO_DOQ, /// unused for now
- KR_PROTO_COUNT,
-};
-/** Bitmap of enum kr_proto options. */
-typedef uint8_t kr_proto_set;
-static_assert(sizeof(kr_proto_set) * 8 >= KR_PROTO_COUNT, "bad combination of type sizes");
-
-
/** Open the rule DB.
*
* You can call this to override the path or size (NULL/0 -> default).