summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorOto Šťáva <oto.stava@nic.cz>2024-06-04 17:32:01 +0200
committerOto Šťáva <oto.stava@nic.cz>2024-06-04 17:32:01 +0200
commitbd5d48010c0a51327ef564ba5e0623f49a3f70ee (patch)
treec712f03dd4cc27bff3e717eb0db1f13700b30b4c /lib
parentMerge branch 'manager-configurable-meson' into 'master' (diff)
parentdaemon/session2.h: clarify `struct session2` docs (diff)
downloadknot-resolver-bd5d48010c0a51327ef564ba5e0623f49a3f70ee.tar.xz
knot-resolver-bd5d48010c0a51327ef564ba5e0623f49a3f70ee.zip
Merge branch 'nits' into 'master'
Protocol layers and other parts refactoring (+ nits) See merge request knot/knot-resolver!1546
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.c12
-rw-r--r--lib/rules/api.h22
-rw-r--r--lib/rules/local-addr.c2
-rw-r--r--lib/rules/zonefile.c3
7 files changed, 84 insertions, 29 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.c b/lib/rules/api.c
index c10cbb0b..8e908a7a 100644
--- a/lib/rules/api.c
+++ b/lib/rules/api.c
@@ -158,7 +158,7 @@ int kr_rules_init(const char *path, size_t maxsize)
// Later we might improve it to auto-resize in case of running out of space.
// Caveat: mdb_env_set_mapsize() can only be called without transactions open.
.maxsize = maxsize ? maxsize :
- (sizeof(size_t) > 4 ? 2048 : 500) * 1024*(size_t)1024,
+ (size_t)(sizeof(size_t) > 4 ? 2048 : 500) * 1024*1024,
};
int ret = the_rules->api->open(&the_rules->db, &the_rules->stats, &opts, NULL);
/* No persistence - we always refill from config for now.
@@ -848,8 +848,8 @@ static int subnet_encode(const struct sockaddr *addr, int sub_len, uint8_t buf[3
uint16_t x = a[i] * 85; // interleave by zero bits
uint8_t sub_mask = 255 >> (8 - MIN(sub_len, 8));
uint16_t r = x | (sub_mask * 85 * 2);
- buf[2*i] = r / 256;
- buf[2*i + 1] = r % 256;
+ buf[(ssize_t)2*i] = r / 256;
+ buf[(ssize_t)2*i + 1] = r % 256;
}
return i * 2;
}
@@ -870,9 +870,9 @@ bool subnet_is_prefix(uint8_t a, uint8_t b)
}
#define KEY_PREPEND(key, arr) do { \
- key.data -= sizeof(arr); \
- key.len += sizeof(arr); \
- memcpy(key.data, arr, sizeof(arr)); \
+ (key).data -= sizeof(arr); \
+ (key).len += sizeof(arr); \
+ memcpy((key).data, arr, sizeof(arr)); \
} while (false)
int kr_view_insert_action(const char *subnet, const char *dst_subnet,
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).
diff --git a/lib/rules/local-addr.c b/lib/rules/local-addr.c
index 787639df..cd5d456b 100644
--- a/lib/rules/local-addr.c
+++ b/lib/rules/local-addr.c
@@ -67,7 +67,7 @@ static const knot_dname_t * raw_addr2reverse(const uint8_t *raw_addr, int family
#undef REV_MAXLEN
if (family == AF_INET) {
- snprintf(reverse_addr, sizeof(reverse_addr),
+ (void)snprintf(reverse_addr, sizeof(reverse_addr),
"%d.%d.%d.%d.in-addr.arpa.",
raw_addr[3], raw_addr[2], raw_addr[1], raw_addr[0]);
} else if (family == AF_INET6) {
diff --git a/lib/rules/zonefile.c b/lib/rules/zonefile.c
index 74a7317d..d308f375 100644
--- a/lib/rules/zonefile.c
+++ b/lib/rules/zonefile.c
@@ -203,6 +203,7 @@ static void process_record(zs_scanner_t *s)
KR_RRTYPE_GET_STR(type_str, s->r_type);
kr_log_warning(RULES, "skipping unsupported RR type %s\n", type_str);
return;
+ default:; // Continue below
}
if (knot_rrtype_is_metatype(s->r_type))
goto unsupported_type;
@@ -245,7 +246,7 @@ int kr_rule_zonefile(const struct kr_rule_zonefile_config *c)
s_data_t s_data = { 0 };
s_data.c = c;
- s_data.pool = mm_ctx_mempool2(64 * 1024);
+ s_data.pool = mm_ctx_mempool2((size_t)64 * 1024);
s_data.rrs = trie_create(s_data.pool);
ret = zs_set_processing(s, process_record, NULL, &s_data);
if (kr_fails_assert(ret == 0))