2018-05-23 21:26:19 +08:00
|
|
|
/* IDNA functions, forwarding to implementations in libidn2.
|
2024-01-02 02:12:26 +08:00
|
|
|
Copyright (C) 2018-2024 Free Software Foundation, Inc.
|
2018-05-23 21:26:19 +08:00
|
|
|
This file is part of the GNU C Library.
|
|
|
|
|
|
|
|
The GNU C Library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
The GNU C Library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with the GNU C Library; if not, see
|
Prefer https to http for gnu.org and fsf.org URLs
Also, change sources.redhat.com to sourceware.org.
This patch was automatically generated by running the following shell
script, which uses GNU sed, and which avoids modifying files imported
from upstream:
sed -ri '
s,(http|ftp)(://(.*\.)?(gnu|fsf|sourceware)\.org($|[^.]|\.[^a-z])),https\2,g
s,(http|ftp)(://(.*\.)?)sources\.redhat\.com($|[^.]|\.[^a-z]),https\2sourceware.org\4,g
' \
$(find $(git ls-files) -prune -type f \
! -name '*.po' \
! -name 'ChangeLog*' \
! -path COPYING ! -path COPYING.LIB \
! -path manual/fdl-1.3.texi ! -path manual/lgpl-2.1.texi \
! -path manual/texinfo.tex ! -path scripts/config.guess \
! -path scripts/config.sub ! -path scripts/install-sh \
! -path scripts/mkinstalldirs ! -path scripts/move-if-change \
! -path INSTALL ! -path locale/programs/charmap-kw.h \
! -path po/libc.pot ! -path sysdeps/gnu/errlist.c \
! '(' -name configure \
-execdir test -f configure.ac -o -f configure.in ';' ')' \
! '(' -name preconfigure \
-execdir test -f preconfigure.ac ';' ')' \
-print)
and then by running 'make dist-prepare' to regenerate files built
from the altered files, and then executing the following to cleanup:
chmod a+x sysdeps/unix/sysv/linux/riscv/configure
# Omit irrelevant whitespace and comment-only changes,
# perhaps from a slightly-different Autoconf version.
git checkout -f \
sysdeps/csky/configure \
sysdeps/hppa/configure \
sysdeps/riscv/configure \
sysdeps/unix/sysv/linux/csky/configure
# Omit changes that caused a pre-commit check to fail like this:
# remote: *** error: sysdeps/powerpc/powerpc64/ppc-mcount.S: trailing lines
git checkout -f \
sysdeps/powerpc/powerpc64/ppc-mcount.S \
sysdeps/unix/sysv/linux/s390/s390-64/syscall.S
# Omit change that caused a pre-commit check to fail like this:
# remote: *** error: sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S: last line does not end in newline
git checkout -f sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S
2019-09-07 13:40:42 +08:00
|
|
|
<https://www.gnu.org/licenses/>. */
|
2018-05-23 21:26:19 +08:00
|
|
|
|
|
|
|
#include <allocate_once.h>
|
|
|
|
#include <dlfcn.h>
|
|
|
|
#include <inet/net-internal.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <stdbool.h>
|
2022-10-18 23:00:07 +08:00
|
|
|
#include <pointer_guard.h>
|
2018-05-23 21:26:19 +08:00
|
|
|
|
|
|
|
/* Use the soname and version to locate libidn2, to ensure a
|
|
|
|
compatible ABI. */
|
|
|
|
#define LIBIDN2_SONAME "libidn2.so.0"
|
|
|
|
#define LIBIDN2_VERSION "IDN2_0.0.0"
|
|
|
|
|
|
|
|
/* Return codes from libidn2. */
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
IDN2_OK = 0,
|
|
|
|
IDN2_MALLOC = -100,
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Functions from libidn2. */
|
|
|
|
struct functions
|
|
|
|
{
|
|
|
|
void *handle;
|
|
|
|
int (*lookup_ul) (const char *src, char **result, int flags);
|
|
|
|
int (*to_unicode_lzlz) (const char *name, char **result, int flags);
|
|
|
|
};
|
|
|
|
|
|
|
|
static void *
|
|
|
|
functions_allocate (void *closure)
|
|
|
|
{
|
|
|
|
struct functions *result = malloc (sizeof (*result));
|
|
|
|
if (result == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
void *handle = __libc_dlopen (LIBIDN2_SONAME);
|
|
|
|
if (handle == NULL)
|
|
|
|
/* Do not cache open failures. The library may appear
|
|
|
|
later. */
|
|
|
|
{
|
|
|
|
free (result);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *ptr_lookup_ul
|
|
|
|
= __libc_dlvsym (handle, "idn2_lookup_ul", LIBIDN2_VERSION);
|
|
|
|
void *ptr_to_unicode_lzlz
|
|
|
|
= __libc_dlvsym (handle, "idn2_to_unicode_lzlz", LIBIDN2_VERSION);
|
|
|
|
if (ptr_lookup_ul == NULL || ptr_to_unicode_lzlz == NULL)
|
|
|
|
{
|
|
|
|
__libc_dlclose (handle);
|
|
|
|
free (result);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
result->handle = handle;
|
|
|
|
result->lookup_ul = ptr_lookup_ul;
|
|
|
|
result->to_unicode_lzlz = ptr_to_unicode_lzlz;
|
|
|
|
PTR_MANGLE (result->lookup_ul);
|
|
|
|
PTR_MANGLE (result->to_unicode_lzlz);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
functions_deallocate (void *closure, void *ptr)
|
|
|
|
{
|
|
|
|
struct functions *functions = ptr;
|
|
|
|
__libc_dlclose (functions->handle);
|
|
|
|
free (functions);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Ensure that *functions is initialized and return the value of the
|
|
|
|
pointer. If the library cannot be loaded, return NULL. */
|
|
|
|
static inline struct functions *
|
|
|
|
get_functions (void)
|
|
|
|
{
|
|
|
|
static void *functions;
|
|
|
|
return allocate_once (&functions, functions_allocate, functions_deallocate,
|
|
|
|
NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* strdup with an EAI_* error code. */
|
|
|
|
static int
|
|
|
|
gai_strdup (const char *name, char **result)
|
|
|
|
{
|
|
|
|
char *ptr = __strdup (name);
|
|
|
|
if (ptr == NULL)
|
|
|
|
return EAI_MEMORY;
|
|
|
|
*result = ptr;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
__idna_to_dns_encoding (const char *name, char **result)
|
|
|
|
{
|
|
|
|
switch (__idna_name_classify (name))
|
|
|
|
{
|
|
|
|
case idna_name_ascii:
|
|
|
|
/* Nothing to convert. */
|
|
|
|
return gai_strdup (name, result);
|
|
|
|
case idna_name_nonascii:
|
|
|
|
/* Encoding needed. Handled below. */
|
|
|
|
break;
|
|
|
|
case idna_name_nonascii_backslash:
|
|
|
|
case idna_name_encoding_error:
|
|
|
|
return EAI_IDN_ENCODE;
|
|
|
|
case idna_name_memory_error:
|
|
|
|
return EAI_MEMORY;
|
|
|
|
case idna_name_error:
|
|
|
|
return EAI_SYSTEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct functions *functions = get_functions ();
|
|
|
|
if (functions == NULL)
|
|
|
|
/* We report this as an encoding error (assuming that libidn2 is
|
|
|
|
not installed), although the root cause may be a temporary
|
|
|
|
error condition due to resource shortage. */
|
|
|
|
return EAI_IDN_ENCODE;
|
|
|
|
char *ptr = NULL;
|
|
|
|
__typeof__ (functions->lookup_ul) fptr = functions->lookup_ul;
|
|
|
|
PTR_DEMANGLE (fptr);
|
|
|
|
int ret = fptr (name, &ptr, 0);
|
|
|
|
if (ret == 0)
|
|
|
|
{
|
|
|
|
/* Assume that idn2_free is equivalent to free. */
|
|
|
|
*result = ptr;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else if (ret == IDN2_MALLOC)
|
|
|
|
return EAI_MEMORY;
|
|
|
|
else
|
|
|
|
return EAI_IDN_ENCODE;
|
|
|
|
}
|
|
|
|
libc_hidden_def (__idna_to_dns_encoding)
|
|
|
|
|
|
|
|
int
|
|
|
|
__idna_from_dns_encoding (const char *name, char **result)
|
|
|
|
{
|
|
|
|
struct functions *functions = get_functions ();
|
|
|
|
if (functions == NULL)
|
|
|
|
/* Simply use the encoded name, assuming that it is not punycode
|
|
|
|
(but even a punycode name would be syntactically valid). */
|
|
|
|
return gai_strdup (name, result);
|
|
|
|
char *ptr = NULL;
|
|
|
|
__typeof__ (functions->to_unicode_lzlz) fptr = functions->to_unicode_lzlz;
|
|
|
|
PTR_DEMANGLE (fptr);
|
|
|
|
int ret = fptr (name, &ptr, 0);
|
|
|
|
if (ret == 0)
|
|
|
|
{
|
|
|
|
/* Assume that idn2_free is equivalent to free. */
|
|
|
|
*result = ptr;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else if (ret == IDN2_MALLOC)
|
|
|
|
return EAI_MEMORY;
|
|
|
|
else
|
|
|
|
return EAI_IDN_ENCODE;
|
|
|
|
}
|
|
|
|
libc_hidden_def (__idna_from_dns_encoding)
|