merge: branch 'dnsconfd-port-fix'

dns: Fix port handling in Dnsconfd plugin

Closes #1765

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/2211
This commit is contained in:
Beniamino Galvani
2025-05-27 08:49:59 +00:00
4 changed files with 47 additions and 41 deletions

View File

@@ -393,6 +393,12 @@ server_builder_append_base(GVariantBuilder *argument_builder,
"{sv}",
"name",
g_variant_new("s", dns_server.servername));
if (dns_server.port != NM_DNS_PORT_UNDEFINED) {
g_variant_builder_add(argument_builder,
"{sv}",
"port",
g_variant_new("i", dns_server.port));
}
if (routing_domains) {
g_variant_builder_add(argument_builder,
"{sv}",

View File

@@ -749,9 +749,7 @@ nm_dns_uri_parse(int addr_family, const char *str, NMDnsServer *dns)
if (!str)
return FALSE;
*dns = (NMDnsServer) {
.port = -1,
};
*dns = (NMDnsServer) {0};
if (NM_STR_HAS_PREFIX(str, "dns+tls://")) {
dns->scheme = NM_DNS_URI_SCHEME_TLS;
@@ -816,8 +814,8 @@ nm_dns_uri_parse(int addr_family, const char *str, NMDnsServer *dns)
end++;
if (*end == ':') {
end++;
dns->port = _nm_utils_ascii_str_to_int64(end, 10, 0, 65535, G_MAXINT32);
if (dns->port == G_MAXINT32)
dns->port = _nm_utils_ascii_str_to_int64(end, 10, 1, 65535, 0);
if (dns->port == 0)
return FALSE;
}
} else if (addr_family != AF_INET6) {
@@ -831,8 +829,8 @@ nm_dns_uri_parse(int addr_family, const char *str, NMDnsServer *dns)
if (port) {
addr = nm_strndup_a(100, addr_port, port - addr_port, &addr_heap);
port++;
dns->port = _nm_utils_ascii_str_to_int64(port, 10, 0, 65535, G_MAXINT32);
if (dns->port == G_MAXINT32)
dns->port = _nm_utils_ascii_str_to_int64(port, 10, 1, 65535, 0);
if (dns->port == 0)
return FALSE;
}
} else {
@@ -885,7 +883,7 @@ nm_dns_uri_parse_plain(int addr_family, const char *str, char *out_addrstr, NMIP
}
return TRUE;
case NM_DNS_URI_SCHEME_UDP:
if (dns.port != -1 && dns.port != 53)
if (dns.port != NM_DNS_PORT_UNDEFINED && dns.port != 53)
return FALSE;
if (dns.interface[0])
return FALSE;
@@ -929,9 +927,9 @@ nm_dns_uri_normalize(int addr_family, const char *str, char **out_free)
nm_inet_ntop(dns.addr_family, &dns.addr, addrstr);
if (dns.port != -1) {
nm_assert(dns.port >= 0 && dns.port <= 65535);
g_snprintf(portstr, sizeof(portstr), "%d", dns.port);
if (dns.port != NM_DNS_PORT_UNDEFINED) {
nm_assert(dns.port >= 1 && dns.port <= 65535);
g_snprintf(portstr, sizeof(portstr), "%" G_GUINT16_FORMAT, dns.port);
}
switch (dns.scheme) {
@@ -955,15 +953,15 @@ nm_dns_uri_normalize(int addr_family, const char *str, char **out_free)
ret = g_strconcat(addrstr, "#", dns.servername, NULL);
break;
case NM_DNS_URI_SCHEME_UDP:
if (dns.interface[0] || dns.port != -1) {
if (dns.interface[0] || dns.port != NM_DNS_PORT_UNDEFINED) {
ret = g_strdup_printf("dns+udp://%s%s%s%s%s%s%s",
dns.addr_family == AF_INET6 ? "[" : "",
addrstr,
dns.interface[0] ? "%" : "",
dns.interface[0] ? dns.interface : "",
dns.addr_family == AF_INET6 ? "]" : "",
dns.port != -1 ? ":" : "",
dns.port != -1 ? portstr : "");
dns.port != NM_DNS_PORT_UNDEFINED ? ":" : "",
dns.port != NM_DNS_PORT_UNDEFINED ? portstr : "");
break;
}
ret = g_strdup_printf("%s%s%s", addrstr, dns.servername ? "#" : "", dns.servername ?: "");
@@ -975,8 +973,8 @@ nm_dns_uri_normalize(int addr_family, const char *str, char **out_free)
dns.interface[0] ? "%%" : "",
dns.interface[0] ? dns.interface : "",
dns.addr_family == AF_INET6 ? "]" : "",
dns.port != -1 ? ":" : "",
dns.port != -1 ? portstr : "",
dns.port != NM_DNS_PORT_UNDEFINED ? ":" : "",
dns.port != NM_DNS_PORT_UNDEFINED ? portstr : "",
dns.servername ? "#" : "",
dns.servername ?: "");
break;

View File

@@ -319,13 +319,15 @@ typedef enum {
NM_DNS_URI_SCHEME_TLS,
} NMDnsUriScheme;
#define NM_DNS_PORT_UNDEFINED 0
typedef struct {
NMIPAddr addr;
const char *servername;
char interface[NM_IFNAMSIZ];
NMDnsUriScheme scheme;
int addr_family;
int port;
guint16 port;
} NMDnsServer;
gboolean nm_dns_uri_parse(int addr_family, const char *str, NMDnsServer *out_dns);

View File

@@ -11454,32 +11454,32 @@ static void
test_dns_uri_parse(void)
{
/* clang-format off */
t_dns_1("dns+tls://8.8.8.8", INET, TLS, "8.8.8.8", -1, NULL, NULL);
t_dns_1("dns+tls://8.8.8.8", INET, TLS, "8.8.8.8", -1, NULL, NULL);
t_dns_1("dns+tls://1.2.3.4#name", INET, TLS, "1.2.3.4", -1, "name", NULL);
t_dns_1("dns+tls://1.2.3.4#a.b.c", INET, TLS, "1.2.3.4", -1, "a.b.c", NULL);
t_dns_1("dns+tls://8.8.8.8", INET, TLS, "8.8.8.8", 0, NULL, NULL);
t_dns_1("dns+tls://8.8.8.8", INET, TLS, "8.8.8.8", 0, NULL, NULL);
t_dns_1("dns+tls://1.2.3.4#name", INET, TLS, "1.2.3.4", 0, "name", NULL);
t_dns_1("dns+tls://1.2.3.4#a.b.c", INET, TLS, "1.2.3.4", 0, "a.b.c", NULL);
t_dns_1("dns+tls://1.2.3.4:53", INET, TLS, "1.2.3.4", 53, NULL, NULL);
t_dns_1("dns+tls://1.2.3.4:53#foobar", INET, TLS, "1.2.3.4", 53, "foobar", NULL);
t_dns_1("dns+tls://192.168.120.250:99", INET, TLS, "192.168.120.250", 99, NULL, NULL);
t_dns_1("dns+udp://8.8.8.8:65535", INET, UDP, "8.8.8.8", 65535, NULL, NULL);
t_dns_1("dns+udp://[fd01::1]", INET6, UDP, "fd01::1", -1, NULL, NULL);
t_dns_1("dns+udp://[fd01::1]", INET6, UDP, "fd01::1", 0, NULL, NULL);
t_dns_1("dns+tls://[fd01::2]:5353", INET6, UDP, "fd01::2", 5353, NULL, NULL);
t_dns_1("dns+tls://[::1]#name", INET6, UDP, "::1", -1, "name", NULL);
t_dns_1("dns+tls://[::1]#name", INET6, UDP, "::1", 0, "name", NULL);
t_dns_1("dns+tls://[::2]:65535#name", INET6, UDP, "::2", 65535, "name", NULL);
t_dns_1("dns+udp://[::ffff:1.2.3.4]", INET6, UDP, "::ffff:1.2.3.4", -1, NULL, NULL);
t_dns_1("dns+tls://[fe80::1%eth0]", INET6, UDP, "fe80::1", -1, NULL, "eth0");
t_dns_1("dns+udp://[::ffff:1.2.3.4]", INET6, UDP, "::ffff:1.2.3.4", 0, NULL, NULL);
t_dns_1("dns+tls://[fe80::1%eth0]", INET6, UDP, "fe80::1", 0, NULL, "eth0");
t_dns_1("dns+tls://[fe80::2%en1]:53#a", INET6, UDP, "fe80::2", 53, "a", "en1");
t_dns_1("dns+tls://[fe80::1%en3456789012345]", INET6, UDP, "fe80::1", -1, NULL, "en3456789012345");
t_dns_1("dns+tls://[fe80::1%en3456789012345]", INET6, UDP, "fe80::1", 0, NULL, "en3456789012345");
t_dns_1("1.2.3.4", INET, NONE, "1.2.3.4", -1, NULL, NULL);
t_dns_1("1.2.3.4#foo", INET, NONE, "1.2.3.4", -1, "foo", NULL);
t_dns_1("1::#x", INET6, NONE, "1::", -1, "x", NULL);
t_dns_1("1::0#x", INET6, NONE, "1::", -1, "x", NULL);
t_dns_1("192.168.0.1", INET, NONE, "192.168.0.1", -1, NULL, NULL);
t_dns_1("192.168.0.1#tst.com", INET, NONE, "192.168.0.1", -1, "tst.com", NULL);
t_dns_1("fe80::18", INET6, NONE, "fe80::18", -1, NULL, NULL);
t_dns_1("fe80::18#foo.com", INET6, NONE, "fe80::18", -1, "foo.com", NULL);
t_dns_1("1.2.3.4", INET, NONE, "1.2.3.4", 0, NULL, NULL);
t_dns_1("1.2.3.4#foo", INET, NONE, "1.2.3.4", 0, "foo", NULL);
t_dns_1("1::#x", INET6, NONE, "1::", 0, "x", NULL);
t_dns_1("1::0#x", INET6, NONE, "1::", 0, "x", NULL);
t_dns_1("192.168.0.1", INET, NONE, "192.168.0.1", 0, NULL, NULL);
t_dns_1("192.168.0.1#tst.com", INET, NONE, "192.168.0.1", 0, "tst.com", NULL);
t_dns_1("fe80::18", INET6, NONE, "fe80::18", 0, NULL, NULL);
t_dns_1("fe80::18#foo.com", INET6, NONE, "fe80::18", 0, "foo.com", NULL);
/* clang-format on */
t_dns_0("http://8.8.8.8"); /* unsupported schema */