From 870dc3a703b88943502fc772109c4ac5543d3fe8 Mon Sep 17 00:00:00 2001 From: Oto Šťáva Date: Tue, 21 May 2024 11:09:47 +0200 Subject: daemon, lib: unify protolayer_grp and kr_proto enums --- lib/meson.build | 2 ++ lib/proto.c | 19 +++++++++++++++++++ lib/proto.h | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/rules/api.h | 22 +--------------------- 4 files changed, 75 insertions(+), 21 deletions(-) create mode 100644 lib/proto.c create mode 100644 lib/proto.h (limited to 'lib') diff --git a/lib/meson.build b/lib/meson.build index d8cbf1fa..60988f02 100644 --- a/lib/meson.build +++ b/lib/meson.build @@ -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 index 00000000..cf12e94e --- /dev/null +++ b/lib/proto.c @@ -0,0 +1,19 @@ +/* Copyright (C) CZ.NIC, z.s.p.o. + * 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 index 00000000..875fe8e3 --- /dev/null +++ b/lib/proto.h @@ -0,0 +1,53 @@ +/* Copyright (C) CZ.NIC, z.s.p.o. + * SPDX-License-Identifier: GPL-3.0-or-later + */ +#pragma once + +#include + +#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"); diff --git a/lib/rules/api.h b/lib/rules/api.h index bf51e4d5..1069ef4d 100644 --- a/lib/rules/api.h +++ b/lib/rules/api.h @@ -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). -- cgit v1.2.3