From acbd5781e75619fb28149b3e8460428196abb518 Mon Sep 17 00:00:00 2001 From: Tomas Korbar Date: Mon, 23 Mar 2026 13:07:07 +0100 Subject: [PATCH] dns: Move domain_is_routing and domain_is_valid functions domain_is_routing and domain_is_valid functions could be used outside of nm-dns-manager thus move them to core utils. --- src/core/dns/nm-dns-manager.c | 51 --------------------------------- src/core/nm-core-utils.c | 54 +++++++++++++++++++++++++++++++++++ src/core/nm-core-utils.h | 6 ++++ 3 files changed, 60 insertions(+), 51 deletions(-) diff --git a/src/core/dns/nm-dns-manager.c b/src/core/dns/nm-dns-manager.c index 59ef4a7de5..ae3b29e690 100644 --- a/src/core/dns/nm-dns-manager.c +++ b/src/core/dns/nm-dns-manager.c @@ -177,57 +177,6 @@ NM_DEFINE_SINGLETON_GETTER(NMDnsManager, nm_dns_manager_get, NM_TYPE_DNS_MANAGER /*****************************************************************************/ -static gboolean -domain_is_valid(const char *domain, - gboolean reject_public_suffix, - gboolean assume_any_tld_is_public) -{ - if (*domain == '\0') - return FALSE; - - if (reject_public_suffix) { - int is_pub; - -#if !WITH_LIBPSL - /* Without libpsl, we cannot detect that the domain is a public suffix, we assume - * the domain is not and valid. */ - is_pub = FALSE; -#elif defined(PSL_TYPE_NO_STAR_RULE) - /* - * If we use PSL_TYPE_ANY, any TLD (top-level domain, i.e., domain - * with no dots) is considered *public* by the PSL library even if - * it is *not* on the official suffix list. This is the implicit - * behavior of the older API function psl_is_public_suffix(). - * To inhibit that and only deem TLDs explicitly listed in the PSL - * as public, we need to turn off the "prevailing star rule" with - * PSL_TYPE_NO_STAR_RULE. - * For documentation on psl_is_public_suffix2(), see: - * https://rockdaboot.github.io/libpsl/libpsl-Public-Suffix-List-functions.html#psl-is-public-suffix2 - * For more on the public suffix format, including wildcards: - * https://github.com/publicsuffix/list/wiki/Format#format - */ - is_pub = - psl_is_public_suffix2(psl_builtin(), - domain, - assume_any_tld_is_public ? PSL_TYPE_ANY : PSL_TYPE_NO_STAR_RULE); -#else - is_pub = psl_is_public_suffix(psl_builtin(), domain); -#endif - - if (is_pub) - return FALSE; - } - - return TRUE; -} - -static gboolean -domain_is_routing(const char *domain) -{ - return domain[0] == '~'; -} - -/*****************************************************************************/ static NM_UTILS_LOOKUP_STR_DEFINE( _rc_manager_to_string, diff --git a/src/core/nm-core-utils.c b/src/core/nm-core-utils.c index ee05bedd01..91d2466099 100644 --- a/src/core/nm-core-utils.c +++ b/src/core/nm-core-utils.c @@ -26,6 +26,10 @@ #include #include +#if WITH_LIBPSL +#include +#endif + #include "libnm-glib-aux/nm-uuid.h" #include "libnm-platform/nmp-base.h" #include "libnm-std-aux/unaligned.h" @@ -4455,6 +4459,56 @@ nm_utils_parse_dns_domain(const char *domain, gboolean *is_routing) return domain; } +inline gboolean +nm_domain_is_routing(const char *domain) +{ + return domain[0] == '~'; +} + +gboolean +nm_domain_is_valid(const char *domain, + gboolean reject_public_suffix, + gboolean assume_any_tld_is_public) +{ + if (*domain == '\0') + return FALSE; + + if (reject_public_suffix) { + int is_pub; + +#if !WITH_LIBPSL + /* Without libpsl, we cannot detect that the domain is a public suffix, we assume + * the domain is not and valid. */ + is_pub = FALSE; +#elif defined(PSL_TYPE_NO_STAR_RULE) + /* + * If we use PSL_TYPE_ANY, any TLD (top-level domain, i.e., domain + * with no dots) is considered *public* by the PSL library even if + * it is *not* on the official suffix list. This is the implicit + * behavior of the older API function psl_is_public_suffix(). + * To inhibit that and only deem TLDs explicitly listed in the PSL + * as public, we need to turn off the "prevailing star rule" with + * PSL_TYPE_NO_STAR_RULE. + * For documentation on psl_is_public_suffix2(), see: + * https://rockdaboot.github.io/libpsl/libpsl-Public-Suffix-List-functions.html#psl-is-public-suffix2 + * For more on the public suffix format, including wildcards: + * https://github.com/publicsuffix/list/wiki/Format#format + */ + is_pub = + psl_is_public_suffix2(psl_builtin(), + domain, + assume_any_tld_is_public ? PSL_TYPE_ANY : PSL_TYPE_NO_STAR_RULE); +#else + is_pub = psl_is_public_suffix(psl_builtin(), domain); +#endif + + if (is_pub) + return FALSE; + } + + return TRUE; +} + /*****************************************************************************/ static guint32 diff --git a/src/core/nm-core-utils.h b/src/core/nm-core-utils.h index a1892b9993..44fd5766a6 100644 --- a/src/core/nm-core-utils.h +++ b/src/core/nm-core-utils.h @@ -457,6 +457,12 @@ const char *nm_activation_type_to_string(NMActivationType activation_type); const char *nm_utils_parse_dns_domain(const char *domain, gboolean *is_routing); +gboolean nm_domain_is_routing(const char *domain); + +gboolean nm_domain_is_valid(const char *domain, + gboolean reject_public_suffix, + gboolean assume_any_tld_is_public); + /*****************************************************************************/ void nm_wifi_utils_parse_ies(const guint8 *bytes,