summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorOto Šťáva <oto.stava@nic.cz>2024-05-21 11:09:47 +0200
committerOto Šťáva <oto.stava@nic.cz>2024-06-04 13:04:59 +0200
commit870dc3a703b88943502fc772109c4ac5543d3fe8 (patch)
tree3d94df61a0809da85c0fa03240351b3722e9e256 /lib
parentdaemon/session2: documentation improvements (diff)
downloadknot-resolver-870dc3a703b88943502fc772109c4ac5543d3fe8.tar.xz
knot-resolver-870dc3a703b88943502fc772109c4ac5543d3fe8.zip
daemon, lib: unify protolayer_grp and kr_proto enums
Diffstat (limited to 'lib')
-rw-r--r--lib/meson.build2
-rw-r--r--lib/proto.c19
-rw-r--r--lib/proto.h53
-rw-r--r--lib/rules/api.h22
4 files changed, 75 insertions, 21 deletions
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. <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
index 00000000..875fe8e3
--- /dev/null
+++ b/lib/proto.h
@@ -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");
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).