From c14f2938a0e3c873ae2114914ab406871e35342d Mon Sep 17 00:00:00 2001 From: Alan Coopersmith Date: Sun, 12 Jan 2025 10:45:01 -0800 Subject: [PATCH] os: if getaddrinfo() is available, use it, even if IPv6 support is disabled Support for using getaddrinfo() was originally added to support IPv6, and only used if IPv6 support was enabled. Two decades later, support for getaddrinfo() is ubiquitous and OS'es have marked gethostbyname() as deprecated, so use the modern interface whenever we can now. Signed-off-by: Alan Coopersmith Part-of: (cherry picked from commit 2ffe0f8a356f5deea1e498441ab9e077602a69ba) --- include/meson.build | 1 + os/access.c | 17 ++++++++++++----- os/utils.c | 6 +++--- os/xdmcp.c | 36 +++++++++++++++++++++++------------- 4 files changed, 39 insertions(+), 21 deletions(-) diff --git a/include/meson.build b/include/meson.build index e9dc7174f..c92c89d16 100644 --- a/include/meson.build +++ b/include/meson.build @@ -146,6 +146,7 @@ conf_data.set('HAVE_GETUID', cc.has_function('getuid') ? '1' : false) conf_data.set('HAVE_GETEUID', cc.has_function('geteuid') ? '1' : false) conf_data.set('HAVE_ISASTREAM', cc.has_function('isastream') ? '1' : false) conf_data.set('HAVE_ISSETUGID', cc.has_function('issetugid') ? '1' : false) +conf_data.set('HAVE_GETADDRINFO', cc.has_function('getaddrinfo') ? '1' : false) conf_data.set('HAVE_GETIFADDRS', cc.has_function('getifaddrs') ? '1' : false) conf_data.set('HAVE_GETPEEREID', cc.has_function('getpeereid') ? '1' : false) conf_data.set('HAVE_GETPEERUCRED', cc.has_function('getpeerucred') ? '1' : false) diff --git a/os/access.c b/os/access.c index 7176aa26d..172d15560 100644 --- a/os/access.c +++ b/os/access.c @@ -964,8 +964,11 @@ ResetHosts(const char *display) else #if defined(TCPCONN) { +#if defined(HAVE_GETADDRINFO) + if ((family == FamilyInternet) || #if defined(IPv6) - if ((family == FamilyInternet) || (family == FamilyInternet6) || + (family == FamilyInternet6) || +#endif (family == FamilyWild)) { struct addrinfo *addresses; struct addrinfo *a; @@ -984,7 +987,7 @@ ResetHosts(const char *display) freeaddrinfo(addresses); } } -#else +#else /* HAVE_GETADDRINFO */ #ifdef XTHREADS_NEEDS_BYNAMEPARAMS _Xgethostbynameparams hparams; #endif @@ -1011,7 +1014,7 @@ ResetHosts(const char *display) #endif } } -#endif /* IPv6 */ +#endif /* HAVE_GETADDRINFO */ } #endif /* TCPCONN */ family = FamilyWild; @@ -1759,8 +1762,12 @@ siHostnameAddrMatch(int family, void *addr, int len, * support for other address families, such as DECnet, could be added if * desired. */ +#if defined(HAVE_GETADDRINFO) + if ((family == FamilyInternet) #if defined(IPv6) - if ((family == FamilyInternet) || (family == FamilyInternet6)) { + || (family == FamilyInternet6) +#endif + ) { char hostname[SI_HOSTNAME_MAXLEN]; struct addrinfo *addresses; struct addrinfo *a; @@ -1785,7 +1792,7 @@ siHostnameAddrMatch(int family, void *addr, int len, freeaddrinfo(addresses); } } -#else /* IPv6 not supported, use gethostbyname instead for IPv4 */ +#else /* getaddrinfo not supported, use gethostbyname instead for IPv4 */ if (family == FamilyInternet) { register struct hostent *hp; diff --git a/os/utils.c b/os/utils.c index 32d490ccd..469d6f124 100644 --- a/os/utils.c +++ b/os/utils.c @@ -1072,7 +1072,7 @@ set_font_authorizations(char **authorizations, int *authlen, void *client) char hname[1024], *hnameptr; unsigned int len; -#if defined(IPv6) +#if defined(HAVE_GETADDRINFO) struct addrinfo hints, *ai = NULL; #else struct hostent *host; @@ -1083,7 +1083,7 @@ set_font_authorizations(char **authorizations, int *authlen, void *client) #endif gethostname(hname, 1024); -#if defined(IPv6) +#if defined(HAVE_GETADDRINFO) memset(&hints, 0, sizeof(hints)); hints.ai_flags = AI_CANONNAME; if (getaddrinfo(hname, NULL, &hints, &ai) == 0) { @@ -1113,7 +1113,7 @@ set_font_authorizations(char **authorizations, int *authlen, void *client) p += sizeof(AUTHORIZATION_NAME); memcpy(p, hnameptr, len); p += len; -#if defined(IPv6) +#if defined(HAVE_GETADDRINFO) if (ai) { freeaddrinfo(ai); } diff --git a/os/xdmcp.c b/os/xdmcp.c index 992ed2b70..ce14a7069 100644 --- a/os/xdmcp.c +++ b/os/xdmcp.c @@ -86,10 +86,12 @@ static char *xdmAuthCookie; static XdmcpBuffer buffer; -#if defined(IPv6) - +#if defined(HAVE_GETADDRINFO) static struct addrinfo *mgrAddr; static struct addrinfo *mgrAddrFirst; +#endif + +#if defined(IPv6) #define SOCKADDR_TYPE struct sockaddr_storage #define SOCKADDR_FAMILY(s) ((struct sockaddr *)&(s))->sa_family @@ -833,15 +835,19 @@ timeout(void) return; } -#if defined(IPv6) +#if defined(HAVE_GETADDRINFO) if (state == XDM_COLLECT_QUERY || state == XDM_COLLECT_INDIRECT_QUERY) { /* Try next address */ for (mgrAddr = mgrAddr->ai_next;; mgrAddr = mgrAddr->ai_next) { if (mgrAddr == NULL) { mgrAddr = mgrAddrFirst; } - if (mgrAddr->ai_family == AF_INET || mgrAddr->ai_family == AF_INET6) + if (mgrAddr->ai_family == AF_INET) break; +#if defined(IPv6) + if (mgrAddr->ai_family == AF_INET6) + break; +#endif } #ifndef SIN6_LEN ManagerAddressLen = mgrAddr->ai_addrlen; @@ -1348,12 +1354,12 @@ get_addr_by_name(const char *argtype, const char *namestr, int port, int socktype, SOCKADDR_TYPE * addr, SOCKLEN_TYPE * addrlen -#if defined(IPv6) +#if defined(HAVE_GETADDRINFO) , struct addrinfo **aip, struct addrinfo **aifirstp #endif ) { -#if defined(IPv6) +#if defined(HAVE_GETADDRINFO) struct addrinfo *ai; struct addrinfo hints; char portstr[6]; @@ -1380,8 +1386,12 @@ get_addr_by_name(const char *argtype, if ((gaierr = getaddrinfo(namestr, pport, &hints, aifirstp)) == 0) { for (ai = *aifirstp; ai != NULL; ai = ai->ai_next) { - if (ai->ai_family == AF_INET || ai->ai_family == AF_INET6) + if (ai->ai_family == AF_INET) break; +#if defined(IPv6) + if (ai->ai_family == AF_INET6) + break; +#endif } if ((ai == NULL) || (ai->ai_addrlen > sizeof(SOCKADDR_TYPE))) { FatalError("Xserver: %s host %s not on supported network type\n", @@ -1397,7 +1407,7 @@ get_addr_by_name(const char *argtype, FatalError("Xserver: %s: %s %s\n", gai_strerror(gaierr), argtype, namestr); } -#else +#else /* HAVE_GETADDRINFO */ struct hostent *hep; #ifdef XTHREADS_NEEDS_BYNAMEPARAMS @@ -1419,7 +1429,7 @@ get_addr_by_name(const char *argtype, FatalError("Xserver: %s host on strange network %s\n", argtype, namestr); } -#endif +#endif /* HAVE_GETADDRINFO */ } static void @@ -1432,7 +1442,7 @@ get_manager_by_name(int argc, char **argv, int i) get_addr_by_name(argv[i], argv[i + 1], xdm_udp_port, SOCK_DGRAM, &ManagerAddress, &ManagerAddressLen -#if defined(IPv6) +#if defined(HAVE_GETADDRINFO) , &mgrAddr, &mgrAddrFirst #endif ); @@ -1441,7 +1451,7 @@ get_manager_by_name(int argc, char **argv, int i) static void get_fromaddr_by_name(int argc, char **argv, int i) { -#if defined(IPv6) +#if defined(HAVE_GETADDRINFO) struct addrinfo *ai = NULL; struct addrinfo *aifirst = NULL; #endif @@ -1449,11 +1459,11 @@ get_fromaddr_by_name(int argc, char **argv, int i) FatalError("Xserver: missing -from host name in command line\n"); } get_addr_by_name("-from", argv[i], 0, 0, &FromAddress, &FromAddressLen -#if defined(IPv6) +#if defined(HAVE_GETADDRINFO) , &ai, &aifirst #endif ); -#if defined(IPv6) +#if defined(HAVE_GETADDRINFO) if (aifirst != NULL) freeaddrinfo(aifirst); #endif