From 07d53ff24dc1d65b1720336c93d376895bab1ce2 Mon Sep 17 00:00:00 2001 From: David Carlier Date: Sun, 29 Oct 2023 16:25:18 +0000 Subject: [PATCH] following-up on GH-12551: removing inet_ntoa usage --- configure.ac | 1 - ext/ftp/ftp.c | 11 ++++++++-- ext/snmp/snmp.c | 12 +++++------ ext/sockets/sockets.c | 4 ---- ext/standard/basic_functions.c | 10 ++++----- ext/standard/dns.c | 38 ++++++++++++++++++++-------------- ext/standard/dns_win32.c | 10 +++++++-- ext/standard/net.c | 7 ++++--- main/network.c | 12 +++++------ 9 files changed, 58 insertions(+), 47 deletions(-) diff --git a/configure.ac b/configure.ac index 8079d98d21d4e..923fd7a94c47f 100644 --- a/configure.ac +++ b/configure.ac @@ -610,7 +610,6 @@ getgrnam_r \ getpwuid_r \ getwd \ glob \ -inet_ntoa \ inet_ntop \ inet_pton \ localtime_r \ diff --git a/ext/ftp/ftp.c b/ext/ftp/ftp.c index ff3518944bbc2..1ac3cccbacb7f 100644 --- a/ext/ftp/ftp.c +++ b/ext/ftp/ftp.c @@ -56,6 +56,10 @@ #include #endif +#ifndef HAVE_INET_NTOP +#error inet_ntop unsupported on this platform +#endif + #include "ftp.h" #include "ext/standard/fsock.h" @@ -1685,13 +1689,16 @@ ftp_getdata(ftpbuf_t *ftp) data->listener = fd; -#if defined(HAVE_IPV6) && defined(HAVE_INET_NTOP) +#if defined(HAVE_IPV6) if (sa->sa_family == AF_INET6) { /* need to use EPRT */ char eprtarg[INET6_ADDRSTRLEN + sizeof("|x||xxxxx|")]; char out[INET6_ADDRSTRLEN]; int eprtarg_len; - inet_ntop(AF_INET6, &((struct sockaddr_in6*) sa)->sin6_addr, out, sizeof(out)); + if (!inet_ntop(AF_INET6, &((struct sockaddr_in6*) sa)->sin6_addr, out, sizeof(out))) { + goto bail; + } + eprtarg_len = snprintf(eprtarg, sizeof(eprtarg), "|2|%s|%hu|", out, ntohs(((struct sockaddr_in6 *) &addr)->sin6_port)); if (eprtarg_len < 0) { diff --git a/ext/snmp/snmp.c b/ext/snmp/snmp.c index 617d07ab7822a..a5968949e9aae 100644 --- a/ext/snmp/snmp.c +++ b/ext/snmp/snmp.c @@ -51,6 +51,10 @@ #endif #include +#ifndef HAVE_INET_NTOP +#error inet_ntop unsupported on this platform +#endif + #ifndef __P #ifdef __GNUC__ #define __P(args) args @@ -843,7 +847,7 @@ static bool netsnmp_session_init(php_snmp_session **session_p, int version, zend res = psal; while (n-- > 0) { pptr = session->peername; -#if defined(HAVE_GETADDRINFO) && defined(HAVE_IPV6) && defined(HAVE_INET_NTOP) +#if defined(HAVE_GETADDRINFO) && defined(HAVE_IPV6) if (force_ipv6 && (*res)->sa_family != AF_INET6) { res++; continue; @@ -859,12 +863,6 @@ static bool netsnmp_session_init(php_snmp_session **session_p, int version, zend res++; continue; } -#else - if ((*res)->sa_family != AF_INET) { - res++; - continue; - } - strcat(pptr, inet_ntoa(((struct sockaddr_in*)(*res))->sin_addr)); #endif break; } diff --git a/ext/sockets/sockets.c b/ext/sockets/sockets.c index 9172750031a54..e1cd5a6c29ac8 100644 --- a/ext/sockets/sockets.c +++ b/ext/sockets/sockets.c @@ -927,9 +927,7 @@ PHP_FUNCTION(socket_getsockname) #if HAVE_IPV6 struct sockaddr_in6 *sin6; #endif -#ifdef HAVE_INET_NTOP char addrbuf[INET6_ADDRSTRLEN]; -#endif struct sockaddr_un *s_un; const char *addr_string; socklen_t salen = sizeof(php_sockaddr_storage); @@ -997,9 +995,7 @@ PHP_FUNCTION(socket_getpeername) #if HAVE_IPV6 struct sockaddr_in6 *sin6; #endif -#ifdef HAVE_INET_NTOP char addrbuf[INET6_ADDRSTRLEN]; -#endif struct sockaddr_un *s_un; const char *addr_string; socklen_t salen = sizeof(php_sockaddr_storage); diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c index 60d484341a0aa..098f01bb7878a 100644 --- a/ext/standard/basic_functions.c +++ b/ext/standard/basic_functions.c @@ -102,6 +102,10 @@ typedef struct yy_buffer_state *YY_BUFFER_STATE; # define INADDR_NONE ((zend_ulong) -1) #endif +#ifndef HAVE_INET_NTOP +# error inet_ntop unsupported on this platform +#endif + #include "zend_globals.h" #include "php_globals.h" #include "SAPI.h" @@ -531,7 +535,6 @@ PHP_FUNCTION(constant) } /* }}} */ -#ifdef HAVE_INET_NTOP /* {{{ Converts a packed inet address to a human readable IP address string */ PHP_FUNCTION(inet_ntop) { @@ -560,7 +563,6 @@ PHP_FUNCTION(inet_ntop) RETURN_STRING(buffer); } /* }}} */ -#endif /* HAVE_INET_NTOP */ #ifdef HAVE_INET_PTON /* {{{ Converts a human readable IP address to a packed binary string */ @@ -652,15 +654,11 @@ PHP_FUNCTION(long2ip) ip = (zend_ulong)sip; myaddr.s_addr = htonl(ip); -#ifdef HAVE_INET_PTON if (inet_ntop(AF_INET, &myaddr, str, sizeof(str))) { RETURN_STRING(str); } else { RETURN_FALSE; } -#else - RETURN_STRING(inet_ntoa(myaddr)); -#endif } /* }}} */ diff --git a/ext/standard/dns.c b/ext/standard/dns.c index d229b998a4b6b..9c62288e95630 100644 --- a/ext/standard/dns.c +++ b/ext/standard/dns.c @@ -57,6 +57,10 @@ extern void __res_ndestroy(res_state statp); #endif #endif +#ifndef HAVE_INET_NTOP +#error inet_ntop unsupported on this platform +#endif + #ifndef MAXHOSTNAMELEN #define MAXHOSTNAMELEN 255 #endif @@ -221,6 +225,7 @@ PHP_FUNCTION(gethostbyname) { char *hostname; size_t hostname_len; + zend_string *ipaddr; ZEND_PARSE_PARAMETERS_START(1, 1) Z_PARAM_PATH(hostname, hostname_len) @@ -232,7 +237,12 @@ PHP_FUNCTION(gethostbyname) RETURN_STRINGL(hostname, hostname_len); } - RETURN_STR(php_gethostbyname(hostname)); + if (!(ipaddr = php_gethostbyname(hostname))) { + php_error_docref(NULL, E_WARNING, "Host name to ip failed %s", hostname); + RETURN_STRINGL(hostname, hostname_len); + } else { + RETURN_STR(ipaddr); + } } /* }}} */ @@ -244,9 +254,7 @@ PHP_FUNCTION(gethostbynamel) struct hostent *hp; struct in_addr in; int i; -#ifdef HAVE_INET_NTOP char addr4[INET_ADDRSTRLEN]; -#endif ZEND_PARSE_PARAMETERS_START(1, 1) Z_PARAM_PATH(hostname, hostname_len) @@ -267,6 +275,7 @@ PHP_FUNCTION(gethostbynamel) for (i = 0;; i++) { /* On macos h_addr_list entries may be misaligned. */ + const char *ipaddr; struct in_addr *h_addr_entry; /* Don't call this h_addr, it's a macro! */ memcpy(&h_addr_entry, &hp->h_addr_list[i], sizeof(struct in_addr *)); if (!h_addr_entry) { @@ -274,11 +283,13 @@ PHP_FUNCTION(gethostbynamel) } in = *h_addr_entry; -#ifdef HAVE_INET_NTOP - add_next_index_string(return_value, inet_ntop(AF_INET, &in, addr4, INET_ADDRSTRLEN)); -#else - add_next_index_string(return_value, inet_ntoa(in)); -#endif + if (!(ipaddr = inet_ntop(AF_INET, &in, addr4, INET_ADDRSTRLEN))) { + /* unlikely regarding (too) long hostname and protocols but checking still */ + php_error_docref(NULL, E_WARNING, "Host name to ip failed %s", hostname); + continue; + } else { + add_next_index_string(return_value, ipaddr); + } } } /* }}} */ @@ -289,9 +300,7 @@ static zend_string *php_gethostbyname(char *name) struct hostent *hp; struct in_addr *h_addr_0; /* Don't call this h_addr, it's a macro! */ struct in_addr in; -#ifdef HAVE_INET_NTOP char addr4[INET_ADDRSTRLEN]; -#endif const char *address; hp = php_network_gethostbyname(name); @@ -307,11 +316,10 @@ static zend_string *php_gethostbyname(char *name) memcpy(&in.s_addr, h_addr_0, sizeof(in.s_addr)); -#ifdef HAVE_INET_NTOP - address = inet_ntop(AF_INET, &in, addr4, INET_ADDRSTRLEN); -#else - address = inet_ntoa(in); -#endif + if (!(address = inet_ntop(AF_INET, &in, addr4, INET_ADDRSTRLEN))) { + return NULL; + } + return zend_string_init(address, strlen(address), 0); } /* }}} */ diff --git a/ext/standard/dns_win32.c b/ext/standard/dns_win32.c index 4d87d4c8210de..55c6381782baf 100644 --- a/ext/standard/dns_win32.c +++ b/ext/standard/dns_win32.c @@ -19,6 +19,7 @@ #include #include #include +#include #include "php_dns.h" @@ -174,9 +175,14 @@ static void php_parserr(PDNS_RECORD pRec, int type_to_fetch, int store, bool raw switch (type) { case DNS_TYPE_A: { IN_ADDR ipaddr; + char ip[INET_ADDRSTRLEN]; ipaddr.S_un.S_addr = (pRec->Data.A.IpAddress); - add_assoc_string(subarray, "type", "A"); - add_assoc_string(subarray, "ip", inet_ntoa(ipaddr)); + if (!inet_ntop(AF_INET, &ipaddr, ip, INET_ADDRSTRLEN)) { + ZVAL_UNDEF(subarray); + } else { + add_assoc_string(subarray, "type", "A"); + add_assoc_string(subarray, "ip", ip); + } break; } diff --git a/ext/standard/net.c b/ext/standard/net.c index b22f304c8eb33..beecf6c5ca727 100644 --- a/ext/standard/net.c +++ b/ext/standard/net.c @@ -47,13 +47,15 @@ # include #endif +#ifndef HAVE_INET_NTOP +#error inet_ntop unsupported on this platform +#endif + PHPAPI zend_string* php_inet_ntop(const struct sockaddr *addr) { socklen_t addrlen = sizeof(struct sockaddr_in); if (!addr) { return NULL; } - /* Prefer inet_ntop() as it's more task-specific and doesn't have to be demangled */ -#ifdef HAVE_INET_NTOP switch (addr->sa_family) { #ifdef AF_INET6 case AF_INET6: { @@ -76,7 +78,6 @@ PHPAPI zend_string* php_inet_ntop(const struct sockaddr *addr) { break; } } -#endif /* Fallback on getnameinfo() */ switch (addr->sa_family) { diff --git a/main/network.c b/main/network.c index ba1080e8fe10b..cb2c098592858 100644 --- a/main/network.c +++ b/main/network.c @@ -60,6 +60,10 @@ #endif #endif +#ifndef HAVE_INET_NTOP +#error inet_ntop unsupported on this platform +#endif + #ifndef HAVE_INET_ATON int inet_aton(const char *, struct in_addr *); #endif @@ -620,19 +624,13 @@ PHPAPI void php_network_populate_name_from_sockaddr( } if (textaddr) { -#ifdef HAVE_INET_NTOP char abuf[256]; -#endif const char *buf = NULL; switch (sa->sa_family) { case AF_INET: /* generally not thread safe, but it *is* thread safe under win32 */ -#ifdef HAVE_INET_NTOP buf = inet_ntop(AF_INET, &((struct sockaddr_in*)sa)->sin_addr, (char *)&abuf, sizeof(abuf)); -#else - buf = inet_ntoa(((struct sockaddr_in*)sa)->sin_addr); -#endif if (buf) { *textaddr = strpprintf(0, "%s:%d", buf, ntohs(((struct sockaddr_in*)sa)->sin_port)); @@ -640,7 +638,7 @@ PHPAPI void php_network_populate_name_from_sockaddr( break; -#if HAVE_IPV6 && HAVE_INET_NTOP +#if HAVE_IPV6 case AF_INET6: buf = (char*)inet_ntop(sa->sa_family, &((struct sockaddr_in6*)sa)->sin6_addr, (char *)&abuf, sizeof(abuf)); if (buf) {