diff --git a/src/libnm-core-aux-intern/nm-libnm-core-utils.c b/src/libnm-core-aux-intern/nm-libnm-core-utils.c index e13a8381f..5e7b90a49 100644 --- a/src/libnm-core-aux-intern/nm-libnm-core-utils.c +++ b/src/libnm-core-aux-intern/nm-libnm-core-utils.c @@ -700,116 +700,6 @@ nm_mptcp_flags_normalize(NMMptcpFlags flags) /*****************************************************************************/ -gboolean -nm_utils_dnsname_parse(int addr_family, - const char *dns, - int *out_addr_family, - gpointer /* (NMIPAddr **) */ out_addr, - const char **out_servername) -{ - gs_free char *dns_heap = NULL; - const char *s; - NMIPAddr addr; - - nm_assert_addr_family_or_unspec(addr_family); - nm_assert(!out_addr || out_addr_family || NM_IN_SET(addr_family, AF_INET, AF_INET6)); - - if (!dns) - return FALSE; - - s = strchr(dns, '#'); - - if (s) { - dns = nm_strndup_a(200, dns, s - dns, &dns_heap); - s++; - } - - if (s && s[0] == '\0') { - /* "ADDR#" empty DoT SNI name is not allowed. */ - return FALSE; - } - - if (!nm_inet_parse_bin(addr_family, dns, &addr_family, out_addr ? &addr : NULL)) - return FALSE; - - NM_SET_OUT(out_addr_family, addr_family); - if (out_addr) - nm_ip_addr_set(addr_family, out_addr, &addr); - NM_SET_OUT(out_servername, s); - return TRUE; -} - -const char * -nm_utils_dnsname_construct(int addr_family, - gconstpointer /* (const NMIPAddr *) */ addr, - const char *server_name, - char *result, - gsize result_len) -{ - char sbuf[NM_INET_ADDRSTRLEN]; - gsize l; - int d; - - nm_assert_addr_family(addr_family); - nm_assert(addr); - nm_assert(!server_name || !nm_str_is_empty(server_name)); - - nm_inet_ntop(addr_family, addr, sbuf); - - if (!server_name) { - l = g_strlcpy(result, sbuf, result_len); - } else { - d = g_snprintf(result, result_len, "%s#%s", sbuf, server_name); - nm_assert(d >= 0); - l = (gsize) d; - } - - return l < result_len ? result : NULL; -} - -const char * -nm_utils_dnsname_normalize(int addr_family, const char *dns, char **out_free) -{ - char sbuf[NM_INET_ADDRSTRLEN]; - const char *server_name; - char *s; - NMIPAddr a; - gsize l; - - nm_assert_addr_family_or_unspec(addr_family); - nm_assert(dns); - nm_assert(out_free && !*out_free); - - if (!nm_utils_dnsname_parse(addr_family, dns, &addr_family, &a, &server_name)) - return NULL; - - nm_inet_ntop(addr_family, &a, sbuf); - - l = strlen(sbuf); - - /* In the vast majority of cases, the name is in fact normalized. Check - * whether it is, and don't duplicate the string. */ - if (strncmp(dns, sbuf, l) == 0) { - if (server_name) { - if (dns[l] == '#' && nm_streq(&dns[l + 1], server_name)) - return dns; - } else { - if (dns[l] == '\0') - return dns; - } - } - - if (!server_name) - s = g_strdup(sbuf); - else - s = g_strconcat(sbuf, "#", server_name, NULL); - - *out_free = s; - return s; -} - -/*****************************************************************************/ - /* * nm_dns_uri_parse: * @addr_family: the address family, or AF_UNSPEC to autodetect it diff --git a/src/libnm-core-aux-intern/nm-libnm-core-utils.h b/src/libnm-core-aux-intern/nm-libnm-core-utils.h index 18c42bf78..9d1637a2a 100644 --- a/src/libnm-core-aux-intern/nm-libnm-core-utils.h +++ b/src/libnm-core-aux-intern/nm-libnm-core-utils.h @@ -312,35 +312,6 @@ NMMptcpFlags nm_mptcp_flags_normalize(NMMptcpFlags flags); /*****************************************************************************/ -gboolean nm_utils_dnsname_parse(int addr_family, - const char *dns, - int *out_addr_family, - gpointer /* (NMIPAddr **) */ out_addr, - const char **out_servername); - -#define nm_utils_dnsname_parse_assert(addr_family, dns, out_addr_family, out_addr, out_servername) \ - ({ \ - gboolean _good; \ - \ - _good = nm_utils_dnsname_parse((addr_family), \ - (dns), \ - (out_addr_family), \ - (out_addr), \ - (out_servername)); \ - nm_assert(_good); \ - _good; \ - }) - -const char *nm_utils_dnsname_construct(int addr_family, - gconstpointer /* (const NMIPAddr *) */ addr, - const char *server_name, - char *result, - gsize result_len); - -const char *nm_utils_dnsname_normalize(int addr_family, const char *dns, char **out_free); - -/*****************************************************************************/ - typedef enum { NM_DNS_URI_SCHEME_UNKNOWN, NM_DNS_URI_SCHEME_NONE, diff --git a/src/libnm-core-impl/tests/test-general.c b/src/libnm-core-impl/tests/test-general.c index 7e104cee4..3113fc462 100644 --- a/src/libnm-core-impl/tests/test-general.c +++ b/src/libnm-core-impl/tests/test-general.c @@ -11396,181 +11396,6 @@ test_connection_path(void) /*****************************************************************************/ -static void -_t_dnsname_1(const char *str, const char *exp_addr, const char *exp_server_name) -{ - int addr_family; - NMIPAddr exp_addr_bin; - gboolean addr_family_request; - gboolean r; - int detect_addr_family; - NMIPAddr detect_addr; - const char *detect_server_name; - int *p_detect_addr_family = &detect_addr_family; - NMIPAddr *p_detect_addr = &detect_addr; - const char **p_detect_server_name = &detect_server_name; - char str_construct_buf[100]; - char str_construct_buf2[100]; - const char *str_construct; - const char *str_construct2; - gsize l; - const char *str_normalized; - gs_free char *str_normalized_alloc = NULL; - - g_assert(str); - g_assert(exp_addr); - - r = nm_inet_parse_bin(AF_UNSPEC, exp_addr, &addr_family, &exp_addr_bin); - g_assert(r); - g_assert(NM_IN_SET(addr_family, AF_INET, AF_INET6)); - - addr_family_request = nmtst_get_rand_bool(); - if (nmtst_get_rand_bool()) - p_detect_addr = NULL; - if ((addr_family_request || !p_detect_addr) && nmtst_get_rand_bool()) - p_detect_addr_family = NULL; - if (nmtst_get_rand_bool()) - p_detect_server_name = NULL; - - r = nm_utils_dnsname_parse(addr_family_request ? addr_family : AF_UNSPEC, - str, - p_detect_addr_family, - p_detect_addr, - p_detect_server_name); - g_assert(r); - - if (p_detect_addr_family) - g_assert_cmpint(addr_family, ==, detect_addr_family); - if (p_detect_addr) - g_assert_cmpstr(nmtst_inet_to_string(addr_family, &detect_addr), ==, exp_addr); - if (p_detect_server_name) - g_assert_cmpstr(detect_server_name, ==, exp_server_name); - - r = nm_utils_dnsname_parse(addr_family == AF_INET ? AF_INET6 : AF_INET, - str, - p_detect_addr_family, - p_detect_addr, - p_detect_server_name); - g_assert(!r); - - /* Construct the expected value. */ - str_construct = nm_utils_dnsname_construct(addr_family, - &exp_addr_bin, - exp_server_name, - str_construct_buf, - sizeof(str_construct_buf)); - g_assert(str_construct); - g_assert(str_construct == str_construct_buf); - g_assert(strlen(str_construct) < sizeof(str_construct_buf)); - - /* Check that a too short buffer causes truncation. */ - l = nmtst_get_rand_uint32() % (strlen(str_construct) + 10); - str_construct2 = nm_utils_dnsname_construct(addr_family, - &exp_addr_bin, - exp_server_name, - str_construct_buf2, - l); - if (str_construct2) { - g_assert(str_construct2 == str_construct_buf2); - g_assert_cmpstr(str_construct2, ==, str_construct); - g_assert(l > strlen(str_construct)); - } else - g_assert(l <= strlen(str_construct)); - - if (!nm_streq(str_construct, str)) { - _t_dnsname_1(str_construct, exp_addr, exp_server_name); - } - - str_normalized = nm_utils_dnsname_normalize(nmtst_get_rand_bool() ? addr_family : AF_UNSPEC, - str, - &str_normalized_alloc); - g_assert(str_normalized); - if (str_normalized_alloc) { - g_assert(str_normalized == str_normalized_alloc); - g_assert_cmpstr(str_normalized, !=, str); - } else { - g_assert(str == str_normalized); - } - g_assert_cmpstr(str_normalized, ==, str_construct); - - nm_clear_g_free(&str_normalized_alloc); - str_normalized = nm_utils_dnsname_normalize(addr_family == AF_INET ? AF_INET6 : AF_INET, - str, - &str_normalized_alloc); - g_assert(!str_normalized); - g_assert(!str_normalized_alloc); -} - -static void -_t_dnsname_0(const char *str) -{ - gboolean addr_family_request; - int detect_addr_family; - NMIPAddr detect_addr; - const char *detect_server_name; - int *p_detect_addr_family = &detect_addr_family; - NMIPAddr *p_detect_addr = &detect_addr; - const char **p_detect_server_name = &detect_server_name; - const char *str_normalized; - gs_free char *str_normalized_alloc = NULL; - gboolean r; - - g_assert(str); - - addr_family_request = nmtst_get_rand_bool(); - if (nmtst_get_rand_bool()) - p_detect_addr = NULL; - if ((addr_family_request || !p_detect_addr) && nmtst_get_rand_bool()) - p_detect_addr_family = NULL; - if (nmtst_get_rand_bool()) - p_detect_server_name = NULL; - - r = nm_utils_dnsname_parse(addr_family_request ? nmtst_rand_select(AF_INET, AF_INET6) - : AF_UNSPEC, - str, - p_detect_addr_family, - p_detect_addr, - p_detect_server_name); - g_assert(!r); - - str_normalized = nm_utils_dnsname_normalize(nmtst_rand_select(AF_UNSPEC, AF_INET, AF_INET6), - str, - &str_normalized_alloc); - g_assert(!str_normalized); - g_assert(!str_normalized_alloc); -} - -static void -test_dnsname(void) -{ - _t_dnsname_1("1.2.3.4", "1.2.3.4", NULL); - _t_dnsname_1("1.2.3.4#foo", "1.2.3.4", "foo"); - _t_dnsname_1("1::#x", "1::", "x"); - _t_dnsname_1("1::0#x", "1::", "x"); - _t_dnsname_1("192.168.0.1", "192.168.0.1", NULL); - _t_dnsname_1("192.168.0.1#test.com", "192.168.0.1", "test.com"); - _t_dnsname_1("fe80::18", "fe80::18", NULL); - _t_dnsname_1("fe80::18#hoge.com", "fe80::18", "hoge.com"); - - _t_dnsname_0("1.2.3.4#"); - _t_dnsname_0("1::0#"); - _t_dnsname_0("192.168.0.1:53"); - _t_dnsname_0("192.168.0.1:53#example.com"); - _t_dnsname_0("fe80::18%19"); - _t_dnsname_0("fe80::18%lo"); - _t_dnsname_0("[fe80::18]:53"); - _t_dnsname_0("[fe80::18]:53%19"); - _t_dnsname_0("[fe80::18]:53%lo"); - _t_dnsname_0("fe80::18%19#hoge.com"); - _t_dnsname_0("[fe80::18]:53#hoge.com"); - _t_dnsname_0("[fe80::18]:53%19"); - _t_dnsname_0("[fe80::18]:53%19#hoge.com"); - _t_dnsname_0("[fe80::18]:53%lo"); - _t_dnsname_0("[fe80::18]:53%lo#hoge.com"); -} - -/*****************************************************************************/ - static void t_dns_0(const char *str) { @@ -12123,7 +11948,6 @@ main(int argc, char **argv) g_test_add_func("/core/general/test_system_encodings", test_system_encodings); g_test_add_func("/core/general/test_direct_string_is_refstr", test_direct_string_is_refstr); g_test_add_func("/core/general/test_connection_path", test_connection_path); - g_test_add_func("/core/general/test_dnsname", test_dnsname); g_test_add_func("/core/general/test_dns_uri_parse", test_dns_uri_parse); g_test_add_func("/core/general/test_dns_uri_get_legacy", test_dns_uri_parse_plain); g_test_add_func("/core/general/test_dns_uri_normalize", test_dns_uri_normalize);