From 2ffe0f8a356f5deea1e498441ab9e077602a69ba 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: --- 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 6d39bf603..67623f1e6 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 d3ad798cc..44d6c2430 100644 --- a/os/access.c +++ b/os/access.c @@ -970,8 +970,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; @@ -990,7 +993,7 @@ ResetHosts(const char *display) freeaddrinfo(addresses); } } -#else +#else /* HAVE_GETADDRINFO */ #ifdef XTHREADS_NEEDS_BYNAMEPARAMS _Xgethostbynameparams hparams; #endif @@ -1017,7 +1020,7 @@ ResetHosts(const char *display) #endif } } -#endif /* IPv6 */ +#endif /* HAVE_GETADDRINFO */ } #endif /* TCPCONN */ family = FamilyWild; @@ -1765,8 +1768,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; @@ -1791,7 +1798,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 b34145b1f..c6489bfe8 100644 --- a/os/utils.c +++ b/os/utils.c @@ -887,7 +887,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; @@ -898,7 +898,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) { @@ -928,7 +928,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 4a12cae5b..955cee1b4 100644 --- a/os/xdmcp.c +++ b/os/xdmcp.c @@ -89,10 +89,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 @@ -836,15 +838,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; @@ -1351,12 +1357,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]; @@ -1383,8 +1389,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", @@ -1400,7 +1410,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 @@ -1422,7 +1432,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 @@ -1435,7 +1445,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 ); @@ -1444,7 +1454,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 @@ -1452,11 +1462,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