From b0afb8fe0ff1d87165af9df61501197a06240dda Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Sun, 28 Dec 2025 12:13:40 +0100 Subject: Sync with Gnulib stable-202507 code (a8ac9f9ce5) --- gl/getaddrinfo.c | 74 ++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 59 insertions(+), 15 deletions(-) (limited to 'gl/getaddrinfo.c') diff --git a/gl/getaddrinfo.c b/gl/getaddrinfo.c index bf5d61f3..a8c45c21 100644 --- a/gl/getaddrinfo.c +++ b/gl/getaddrinfo.c @@ -1,5 +1,5 @@ /* Get address information (partial implementation). - Copyright (C) 1997, 2001-2002, 2004-2024 Free Software Foundation, Inc. + Copyright (C) 1997, 2001-2002, 2004-2025 Free Software Foundation, Inc. Contributed by Simon Josefsson . This file is free software: you can redistribute it and/or modify @@ -40,8 +40,8 @@ #include #include "gettext.h" -#define _(String) gettext (String) -#define N_(String) String +#define _(msgid) dgettext ("gnulib", msgid) +#define N_(msgid) msgid /* BeOS has AF_INET, but not PF_INET. */ #ifndef PF_INET @@ -54,7 +54,7 @@ #if HAVE_GETADDRINFO -/* Override with cdecl calling convention. */ +/* Override with cdecl calling convention and mingw fix. */ int getaddrinfo (const char *restrict nodename, @@ -63,6 +63,10 @@ getaddrinfo (const char *restrict nodename, struct addrinfo **restrict res) # undef getaddrinfo { + if (hints && (hints->ai_flags & AI_NUMERICSERV) != 0 + && servname && !(*servname >= '0' && *servname <= '9')) + return EAI_NONAME; + return getaddrinfo (nodename, servname, hints, res); } @@ -169,16 +173,43 @@ validate_family (int family) { /* FIXME: Support more families. */ # if HAVE_IPV4 - if (family == PF_INET) - return true; + if (family == PF_INET) + return true; # endif # if HAVE_IPV6 - if (family == PF_INET6) - return true; + if (family == PF_INET6) + return true; # endif - if (family == PF_UNSPEC) - return true; - return false; + if (family == PF_UNSPEC) + return true; + return false; +} + +static bool +is_numeric_host (const char *host, int family) +{ +# if HAVE_IPV4 + if (family == PF_INET || family == PF_UNSPEC) + { + /* glibc supports IPv4 addresses in numbers-and-dots notation, that is, + also hexadecimal and octal number formats and formats that don't + require all four bytes to be explicitly written, via inet_aton(). + But POSIX doesn't require support for these legacy formats. Therefore + we are free to use inet_pton() instead of inet_aton(). */ + struct in_addr addr; + if (inet_pton (AF_INET, host, &addr)) + return true; + } +# endif +# if HAVE_IPV6 + if (family == PF_INET6 || family == PF_UNSPEC) + { + struct in6_addr addr; + if (inet_pton (AF_INET6, host, &addr)) + return true; + } +# endif + return false; } /* Translate name of a service location and/or a service name to set of @@ -210,10 +241,17 @@ getaddrinfo (const char *restrict nodename, # ifdef WINDOWS_NATIVE if (use_win32_p ()) - return getaddrinfo_ptr (nodename, servname, hints, res); + { + if (hints && (hints->ai_flags & AI_NUMERICSERV) != 0 + && servname && !(*servname >= '0' && *servname <= '9')) + return EAI_NONAME; + return getaddrinfo_ptr (nodename, servname, hints, res); + } # endif - if (hints && (hints->ai_flags & ~(AI_CANONNAME|AI_PASSIVE))) + if (hints + && (hints->ai_flags + & ~(AI_CANONNAME | AI_PASSIVE | AI_NUMERICHOST | AI_NUMERICSERV))) /* FIXME: Support more flags. */ return EAI_BADFLAGS; @@ -225,12 +263,18 @@ getaddrinfo (const char *restrict nodename, /* FIXME: Support other socktype. */ return EAI_SOCKTYPE; /* FIXME: Better return code? */ - if (!nodename) + if (nodename != NULL) + { + if (hints && (hints->ai_flags & AI_NUMERICHOST) != 0 + && !is_numeric_host (nodename, hints->ai_family)) + return EAI_NONAME; + } + else { if (!(hints->ai_flags & AI_PASSIVE)) return EAI_NONAME; -# ifdef HAVE_IPV6 +# if HAVE_IPV6 nodename = (hints->ai_family == AF_INET6) ? "::" : "0.0.0.0"; # else nodename = "0.0.0.0"; -- cgit v1.2.3-74-g34f1