]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
daemon, lib: unify protolayer_grp and kr_proto enums
authorOto Šťáva <oto.stava@nic.cz>
Tue, 21 May 2024 09:09:47 +0000 (11:09 +0200)
committerOto Šťáva <oto.stava@nic.cz>
Tue, 4 Jun 2024 11:04:59 +0000 (13:04 +0200)
daemon/http.c
daemon/io.c
daemon/io.h
daemon/session2.c
daemon/session2.h
daemon/tls.c
daemon/worker.c
lib/meson.build
lib/proto.c [new file with mode: 0644]
lib/proto.h [new file with mode: 0644]
lib/rules/api.h

index 425b81c8701317fe34ee402676124b2965316095..411fe22218f40012e2c30ea27fc2996f2c83885c 100644 (file)
@@ -298,7 +298,7 @@ static int http_send_response(struct pl_http_sess_data *http, int32_t stream_id,
                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));
@@ -389,7 +389,7 @@ static ssize_t send_callback(nghttp2_session *h2, const uint8_t *data, size_t le
        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);
 
@@ -505,7 +505,7 @@ static int send_data_callback(nghttp2_session *h2, nghttp2_frame *frame, const u
                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);
 
@@ -732,7 +732,7 @@ static int submit_to_wirebuffer(struct pl_http_sess_data *ctx)
        }
 
        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);
@@ -1032,7 +1032,7 @@ static void pl_http_request_init(struct protolayer_manager *manager,
 
 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,
index 3adb0deca250dd7f0c196b4fd417c2817ea75362..d8aee074299f0c450feb5e436917fb6b26705f6d 100644 (file)
@@ -357,13 +357,13 @@ static enum protolayer_event_cb_result pl_tcp_event_wrap(
 
 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,
@@ -458,7 +458,7 @@ int io_listen_udp(uv_loop_t *loop, uv_udp_t *handle, int fd)
        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);
@@ -515,7 +515,7 @@ static void tcp_recv(uv_stream_t *handle, ssize_t nread, const uv_buf_t *buf)
                        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;
@@ -576,18 +576,18 @@ static void _tcp_accept(uv_stream_t *master, int status, enum protolayer_grp grp
 
 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
 
@@ -1041,7 +1041,7 @@ int io_listen_xdp(uv_loop_t *loop, struct endpoint *ep, const char *ifname)
                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
@@ -1053,7 +1053,7 @@ int io_listen_xdp(uv_loop_t *loop, struct endpoint *ep, const char *ifname)
 #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)
 {
index 305f26cfe19a35b6074ed87409b2c91a82164a5e..b03c6aae50b36d89af8ed7abb0cb08c7dcbb4f0a 100644 (file)
@@ -45,7 +45,7 @@ void tcp_timeout_trigger(uv_timer_t *timer);
  * \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);
index 566ff0c0ac1a0cfadd5dfe4e85532a62cebe43db..dc9911a0b1688e3c289453ff9afeac9c58b822ee 100644 (file)
 
 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)";
@@ -334,10 +330,10 @@ static inline struct protolayer_data *protolayer_iter_data_get(
 }
 
 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;
        }
@@ -361,12 +357,12 @@ static inline void protolayer_iter_ctx_next(struct protolayer_iter_ctx *ctx)
                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)
@@ -388,12 +384,12 @@ static int protolayer_iter_ctx_finish(struct protolayer_iter_ctx *ctx, int ret)
 
        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)
@@ -470,10 +466,10 @@ static void protolayer_ensure_long_lived(struct protolayer_iter_ctx *ctx)
 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;
@@ -561,7 +557,7 @@ static int protolayer_manager_submit(
        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));
 
@@ -576,10 +572,10 @@ static int protolayer_manager_submit(
        };
 
        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) {
@@ -594,7 +590,7 @@ static int protolayer_manager_submit(
        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)
 {
@@ -610,7 +606,7 @@ static void *get_init_param(enum protolayer_protocol p,
 /** 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)
 {
@@ -621,10 +617,10 @@ static struct protolayer_manager *protolayer_manager_new(
        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++)
@@ -697,7 +693,7 @@ static void protolayer_manager_free(struct protolayer_manager *m)
        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);
@@ -809,7 +805,7 @@ int wire_buf_reset(struct wire_buf *wb)
 
 
 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)
@@ -1181,7 +1177,7 @@ int session2_unwrap(struct session2 *s, struct protolayer_payload payload,
                        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)
@@ -1202,7 +1198,7 @@ int session2_wrap(struct session2 *s, struct protolayer_payload payload,
                        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)
@@ -1219,7 +1215,7 @@ static void session2_event_wrap(struct session2 *s, enum protolayer_event_type e
        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);
@@ -1240,7 +1236,7 @@ void session2_event_unwrap(struct session2 *s, ssize_t start_ix, enum protolayer
        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);
@@ -1267,7 +1263,7 @@ void session2_event(struct session2 *s, enum protolayer_event_type event, void *
        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);
@@ -1280,7 +1276,7 @@ void session2_init_request(struct session2 *s, struct kr_request *req)
 {
        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);
index 5fb20dc744f0d828bd93ed4ec6e6609518013874..cda0bd5a16e7586adf9263dfb6dbfcc96b9487ef 100644 (file)
@@ -3,7 +3,7 @@
  */
 
 /* 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:
@@ -61,6 +61,7 @@
 #include "contrib/mempattern.h"
 #include "lib/generic/queue.h"
 #include "lib/generic/trie.h"
+#include "lib/proto.h"
 #include "lib/utils.h"
 
 /* Forward declarations */
@@ -191,7 +192,7 @@ static inline size_t wire_buf_free_space_length(const struct wire_buf *wb)
 /** 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
@@ -203,8 +204,8 @@ static inline size_t wire_buf_free_space_length(const struct wire_buf *wb)
  *
  * 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)\
@@ -223,54 +224,16 @@ static inline size_t wire_buf_free_space_length(const struct wire_buf *wb)
                               * 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`
@@ -617,7 +580,7 @@ typedef void (*protolayer_request_cb)(struct protolayer_manager *manager,
  * `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;
@@ -651,7 +614,7 @@ struct protolayer_manager {
 
 /** 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
@@ -743,9 +706,9 @@ struct protolayer_globals {
        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
@@ -880,7 +843,7 @@ struct session2 {
  * 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);
@@ -888,7 +851,7 @@ struct session2 *session2_new(enum session2_transport_type transport_type,
 /** 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)
@@ -904,7 +867,7 @@ static inline struct session2 *session2_new_io(uv_handle_t *handle,
 /** 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)
@@ -1030,7 +993,7 @@ int session2_unwrap(struct session2 *s, struct protolayer_payload payload,
  *
  * 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);
@@ -1059,7 +1022,7 @@ int session2_wrap(struct session2 *s, struct protolayer_payload payload,
  *
  * 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);
@@ -1077,7 +1040,7 @@ void session2_event(struct session2 *s, enum protolayer_event_type event, void *
  * 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
index 09c99508447733756c025bd0481925b90818a128..377355f7a4eb97142ae812b2bb97643117a139ee 100644 (file)
@@ -27,7 +27,7 @@
 #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) \
@@ -35,9 +35,9 @@
        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 {
@@ -228,7 +228,7 @@ static ssize_t kres_gnutls_vec_push(gnutls_transport_ptr_t h, const giovec_t * i
        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);
 
@@ -260,7 +260,7 @@ static void tls_handshake_success(struct pl_tls_sess_data *tls,
                }
        }
        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;
        }
@@ -1330,7 +1330,7 @@ static void pl_tls_request_init(struct protolayer_manager *manager,
 
 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,
index 2e9f4523641ffa64bb19bca76f7e4111aca03436..1ac52266184da04762635908ca89a7ac092d7e2e 100644 (file)
@@ -128,7 +128,7 @@ struct worker_ctx *the_worker = NULL;
 /*! @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)
 {
@@ -832,7 +832,7 @@ static int transmit(struct qr_task *task)
                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);
 
@@ -1088,14 +1088,14 @@ static int tcp_task_make_connection(struct qr_task *task, const struct sockaddr
        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, &param, 1);
+                               KR_PROTO_DOT, &param, 1);
        } else {
                session = ioreq_spawn(SOCK_STREAM, addr->sa_family,
-                               PROTOLAYER_GRP_DOTCP, NULL, 0);
+                               KR_PROTO_TCP53, NULL, 0);
        }
        if (!session) {
                free(conn);
@@ -2310,13 +2310,13 @@ int worker_init(void)
        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,
@@ -2335,10 +2335,10 @@ int worker_init(void)
                .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;
index d8cbf1fad77123a9439811e1956611f639048c75..60988f02e6c1910d95110e0184851b32f50f90ea 100644 (file)
@@ -23,6 +23,7 @@ libkres_src = files([
   'layer/iterate.c',
   'layer/validate.c',
   'log.c',
+  'proto.c',
   'rules/api.c',
   'rules/defaults.c',
   'rules/forward.c',
@@ -60,6 +61,7 @@ libkres_headers = files([
   'layer/iterate.h',
   'log.h',
   'module.h',
+  'proto.h',
   'resolve.h',
   'resolve-impl.h',
   'rplan.h',
diff --git a/lib/proto.c b/lib/proto.c
new file mode 100644 (file)
index 0000000..cf12e94
--- /dev/null
@@ -0,0 +1,19 @@
+/*  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)";
+       }
+}
diff --git a/lib/proto.h b/lib/proto.h
new file mode 100644 (file)
index 0000000..875fe8e
--- /dev/null
@@ -0,0 +1,53 @@
+/*  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");
index bf51e4d523781569a61039c61f2871308b25d3d4..1069ef4d4ab96b49d6aa77473208e6ace0f8469a 100644 (file)
@@ -4,6 +4,7 @@
 #pragma once
 
 #include "lib/defines.h"
+#include "lib/proto.h"
 struct kr_query;
 struct kr_request;
 struct knot_pkt;
@@ -16,27 +17,6 @@ typedef uint64_t kr_rule_tags_t;
 /// 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).