dns: refactor domain_is_valid() to combine #if blocks

This commit is contained in:
Thomas Haller 2023-05-02 11:08:19 +02:00
parent 4ddbf32f1b
commit db3da65c6c
No known key found for this signature in database
GPG key ID: 29C2366E4DFC5728

View file

@ -177,40 +177,42 @@ domain_is_valid(const char *domain,
gboolean reject_public_suffix,
gboolean assume_any_tld_is_public)
{
#if WITH_LIBPSL
/* ifdef to fall back to old API function on platforms with older LIBPSL */
#ifdef PSL_TYPE_NO_STAR_RULE
int type = PSL_TYPE_ANY;
#endif
#endif
if (*domain == '\0')
return FALSE;
#if WITH_LIBPSL
/* ifdef to fall back to old API function on platforms with older LIBPSL */
#ifdef 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
*/
if (!assume_any_tld_is_public)
type = PSL_TYPE_NO_STAR_RULE;
if (reject_public_suffix && psl_is_public_suffix2(psl_builtin(), domain, type))
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
if (reject_public_suffix && psl_is_public_suffix(psl_builtin(), domain))
return FALSE;
#endif
is_pub = psl_is_public_suffix(psl_builtin(), domain);
#endif
if (is_pub)
return FALSE;
}
return TRUE;
}