1ceb81
From 4dc8b3c0efaae25b08c8f59b068f17c97c59d0ae Mon Sep 17 00:00:00 2001
1ceb81
From: Remi Collet <remi@remirepo.net>
1ceb81
Date: Wed, 5 May 2021 15:41:00 +0200
1ceb81
Subject: [PATCH] get rid of inet_aton and inet_ntoa use inet_ntop iand
1ceb81
 inet_pton where available standardize buffer size
1ceb81
1ceb81
---
1ceb81
 ext/sockets/sockaddr_conv.c |  4 ++++
1ceb81
 ext/sockets/sockets.c       | 48 +++++++++++++++++++++++++------------
1ceb81
 ext/standard/dns.c          | 16 ++++++++++++-
1ceb81
 main/network.c              | 20 ++++++++++++++--
1ceb81
 4 files changed, 70 insertions(+), 18 deletions(-)
1ceb81
1ceb81
diff --git a/ext/sockets/sockaddr_conv.c b/ext/sockets/sockaddr_conv.c
1ceb81
index 57996612d2d7e..65c8418fb3a6f 100644
1ceb81
--- a/ext/sockets/sockaddr_conv.c
1ceb81
+++ b/ext/sockets/sockaddr_conv.c
1ceb81
@@ -87,7 +87,11 @@ int php_set_inet_addr(struct sockaddr_in *sin, char *string, php_socket *php_soc
1ceb81
 	struct in_addr tmp;
1ceb81
 	struct hostent *host_entry;
1ceb81
 
1ceb81
+#ifdef HAVE_INET_PTON
1ceb81
+	if (inet_pton(AF_INET, string, &tmp)) {
1ceb81
+#else
1ceb81
 	if (inet_aton(string, &tmp)) {
1ceb81
+#endif
1ceb81
 		sin->sin_addr.s_addr = tmp.s_addr;
1ceb81
 	} else {
1ceb81
 		if (strlen(string) > MAXFQDNLEN || ! (host_entry = php_network_gethostbyname(string))) {
1ceb81
diff --git a/ext/sockets/sockets.c b/ext/sockets/sockets.c
1ceb81
index 16ad3e8013a4c..85c938d1b97b1 100644
1ceb81
--- a/ext/sockets/sockets.c
1ceb81
+++ b/ext/sockets/sockets.c
1ceb81
@@ -220,8 +220,10 @@ zend_module_entry sockets_module_entry = {
1ceb81
 ZEND_GET_MODULE(sockets)
1ceb81
 #endif
1ceb81
 
1ceb81
+#ifndef HAVE_INET_NTOP
1ceb81
 /* inet_ntop should be used instead of inet_ntoa */
1ceb81
 int inet_ntoa_lock = 0;
1ceb81
+#endif
1ceb81
 
1ceb81
 static int php_open_listen_sock(php_socket *sock, int port, int backlog) /* {{{ */
1ceb81
 {
1ceb81
@@ -1082,10 +1084,12 @@ PHP_FUNCTION(socket_getsockname)
1ceb81
 	struct sockaddr_in		*sin;
1ceb81
 #if HAVE_IPV6
1ceb81
 	struct sockaddr_in6		*sin6;
1ceb81
-	char					addr6[INET6_ADDRSTRLEN+1];
1ceb81
+#endif
1ceb81
+#ifdef HAVE_INET_NTOP
1ceb81
+	char					addrbuf[INET6_ADDRSTRLEN];
1ceb81
 #endif
1ceb81
 	struct sockaddr_un		*s_un;
1ceb81
-	char					*addr_string;
1ceb81
+	const char				*addr_string;
1ceb81
 	socklen_t				salen = sizeof(php_sockaddr_storage);
1ceb81
 
1ceb81
 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "Oz|z", &arg1, socket_ce, &addr, &port) == FAILURE) {
1ceb81
@@ -1106,8 +1110,8 @@ PHP_FUNCTION(socket_getsockname)
1ceb81
 #if HAVE_IPV6
1ceb81
 		case AF_INET6:
1ceb81
 			sin6 = (struct sockaddr_in6 *) sa;
1ceb81
-			inet_ntop(AF_INET6, &sin6->sin6_addr, addr6, INET6_ADDRSTRLEN);
1ceb81
-			ZEND_TRY_ASSIGN_REF_STRING(addr, addr6);
1ceb81
+			inet_ntop(AF_INET6, &sin6->sin6_addr,  addrbuf, sizeof(addrbuf));
1ceb81
+			ZEND_TRY_ASSIGN_REF_STRING(addr, addrbuf);
1ceb81
 
1ceb81
 			if (port != NULL) {
1ceb81
 				ZEND_TRY_ASSIGN_REF_LONG(port, htons(sin6->sin6_port));
1ceb81
@@ -1117,11 +1121,14 @@ PHP_FUNCTION(socket_getsockname)
1ceb81
 #endif
1ceb81
 		case AF_INET:
1ceb81
 			sin = (struct sockaddr_in *) sa;
1ceb81
+#ifdef HAVE_INET_NTOP
1ceb81
+			addr_string = inet_ntop(AF_INET, &sin->sin_addr, addrbuf, sizeof(addrbuf));
1ceb81
+#else
1ceb81
 			while (inet_ntoa_lock == 1);
1ceb81
 			inet_ntoa_lock = 1;
1ceb81
 			addr_string = inet_ntoa(sin->sin_addr);
1ceb81
 			inet_ntoa_lock = 0;
1ceb81
-
1ceb81
+#endif
1ceb81
 			ZEND_TRY_ASSIGN_REF_STRING(addr, addr_string);
1ceb81
 
1ceb81
 			if (port != NULL) {
1ceb81
@@ -1154,10 +1161,12 @@ PHP_FUNCTION(socket_getpeername)
1ceb81
 	struct sockaddr_in		*sin;
1ceb81
 #if HAVE_IPV6
1ceb81
 	struct sockaddr_in6		*sin6;
1ceb81
-	char					addr6[INET6_ADDRSTRLEN+1];
1ceb81
+#endif
1ceb81
+#ifdef HAVE_INET_NTOP
1ceb81
+	char					addrbuf[INET6_ADDRSTRLEN];
1ceb81
 #endif
1ceb81
 	struct sockaddr_un		*s_un;
1ceb81
-	char					*addr_string;
1ceb81
+	const char				*addr_string;
1ceb81
 	socklen_t				salen = sizeof(php_sockaddr_storage);
1ceb81
 
1ceb81
 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "Oz|z", &arg1, socket_ce, &arg2, &arg3) == FAILURE) {
1ceb81
@@ -1178,9 +1187,9 @@ PHP_FUNCTION(socket_getpeername)
1ceb81
 #if HAVE_IPV6
1ceb81
 		case AF_INET6:
1ceb81
 			sin6 = (struct sockaddr_in6 *) sa;
1ceb81
-			inet_ntop(AF_INET6, &sin6->sin6_addr, addr6, INET6_ADDRSTRLEN);
1ceb81
+			inet_ntop(AF_INET6, &sin6->sin6_addr, addrbuf, sizeof(addrbuf));
1ceb81
 
1ceb81
-			ZEND_TRY_ASSIGN_REF_STRING(arg2, addr6);
1ceb81
+			ZEND_TRY_ASSIGN_REF_STRING(arg2, addrbuf);
1ceb81
 
1ceb81
 			if (arg3 != NULL) {
1ceb81
 				ZEND_TRY_ASSIGN_REF_LONG(arg3, htons(sin6->sin6_port));
1ceb81
@@ -1191,11 +1200,14 @@ PHP_FUNCTION(socket_getpeername)
1ceb81
 #endif
1ceb81
 		case AF_INET:
1ceb81
 			sin = (struct sockaddr_in *) sa;
1ceb81
+#ifdef HAVE_INET_NTOP
1ceb81
+			addr_string = inet_ntop(AF_INET, &sin->sin_addr, addrbuf, sizeof(addrbuf));
1ceb81
+#else
1ceb81
 			while (inet_ntoa_lock == 1);
1ceb81
 			inet_ntoa_lock = 1;
1ceb81
 			addr_string = inet_ntoa(sin->sin_addr);
1ceb81
 			inet_ntoa_lock = 0;
1ceb81
-
1ceb81
+#endif
1ceb81
 			ZEND_TRY_ASSIGN_REF_STRING(arg2, addr_string);
1ceb81
 
1ceb81
 			if (arg3 != NULL) {
1ceb81
@@ -1527,12 +1539,14 @@ PHP_FUNCTION(socket_recvfrom)
1ceb81
 	struct sockaddr_in	sin;
1ceb81
 #if HAVE_IPV6
1ceb81
 	struct sockaddr_in6	sin6;
1ceb81
-	char				addr6[INET6_ADDRSTRLEN];
1ceb81
+#endif
1ceb81
+#ifdef HAVE_INET_NTOP
1ceb81
+	char				addrbuf[INET6_ADDRSTRLEN];
1ceb81
 #endif
1ceb81
 	socklen_t			slen;
1ceb81
 	int					retval;
1ceb81
 	zend_long				arg3, arg4;
1ceb81
-	char				*address;
1ceb81
+	const char			*address;
1ceb81
 	zend_string			*recv_buf;
1ceb81
 
1ceb81
 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ozllz|z", &arg1, socket_ce, &arg2, &arg3, &arg4, &arg5, &arg6) == FAILURE) {
1ceb81
@@ -1590,7 +1604,11 @@ PHP_FUNCTION(socket_recvfrom)
1ceb81
 			ZSTR_LEN(recv_buf) = retval;
1ceb81
 			ZSTR_VAL(recv_buf)[ZSTR_LEN(recv_buf)] = '\0';
1ceb81
 
1ceb81
+#ifdef HAVE_INET_NTOP
1ceb81
+			address = inet_ntop(AF_INET, &sin.sin_addr, addrbuf, sizeof(addrbuf));
1ceb81
+#else
1ceb81
 			address = inet_ntoa(sin.sin_addr);
1ceb81
+#endif
1ceb81
 
1ceb81
 			ZEND_TRY_ASSIGN_REF_NEW_STR(arg2, recv_buf);
1ceb81
 			ZEND_TRY_ASSIGN_REF_STRING(arg5, address ? address : "0.0.0.0");
1ceb81
@@ -1617,11 +1635,11 @@ PHP_FUNCTION(socket_recvfrom)
1ceb81
 			ZSTR_LEN(recv_buf) = retval;
1ceb81
 			ZSTR_VAL(recv_buf)[ZSTR_LEN(recv_buf)] = '\0';
1ceb81
 
1ceb81
-			memset(addr6, 0, INET6_ADDRSTRLEN);
1ceb81
-			inet_ntop(AF_INET6, &sin6.sin6_addr, addr6, INET6_ADDRSTRLEN);
1ceb81
+			memset(addrbuf, 0, INET6_ADDRSTRLEN);
1ceb81
+			inet_ntop(AF_INET6, &sin6.sin6_addr,  addrbuf, sizeof(addrbuf));
1ceb81
 
1ceb81
 			ZEND_TRY_ASSIGN_REF_NEW_STR(arg2, recv_buf);
1ceb81
-			ZEND_TRY_ASSIGN_REF_STRING(arg5, addr6[0] ? addr6 : "::");
1ceb81
+			ZEND_TRY_ASSIGN_REF_STRING(arg5, addrbuf[0] ? addrbuf : "::");
1ceb81
 			ZEND_TRY_ASSIGN_REF_LONG(arg6, ntohs(sin6.sin6_port));
1ceb81
 			break;
1ceb81
 #endif
1ceb81
diff --git a/ext/standard/dns.c b/ext/standard/dns.c
1ceb81
index 41b98424edb60..6efdbbe894b46 100644
1ceb81
--- a/ext/standard/dns.c
1ceb81
+++ b/ext/standard/dns.c
1ceb81
@@ -228,6 +228,9 @@ PHP_FUNCTION(gethostbynamel)
1ceb81
 	struct hostent *hp;
1ceb81
 	struct in_addr in;
1ceb81
 	int i;
1ceb81
+#ifdef HAVE_INET_NTOP
1ceb81
+	char addr4[INET_ADDRSTRLEN];
1ceb81
+#endif
1ceb81
 
1ceb81
 	ZEND_PARSE_PARAMETERS_START(1, 1)
1ceb81
 		Z_PARAM_PATH(hostname, hostname_len)
1ceb81
@@ -255,7 +258,11 @@ PHP_FUNCTION(gethostbynamel)
1ceb81
 		}
1ceb81
 
1ceb81
 		in = *h_addr_entry;
1ceb81
+#ifdef HAVE_INET_NTOP
1ceb81
+		add_next_index_string(return_value, inet_ntop(AF_INET, &in, addr4, INET_ADDRSTRLEN));
1ceb81
+#else
1ceb81
 		add_next_index_string(return_value, inet_ntoa(in));
1ceb81
+#endif
1ceb81
 	}
1ceb81
 }
1ceb81
 /* }}} */
1ceb81
@@ -266,7 +273,10 @@ static zend_string *php_gethostbyname(char *name)
1ceb81
 	struct hostent *hp;
1ceb81
 	struct in_addr *h_addr_0; /* Don't call this h_addr, it's a macro! */
1ceb81
 	struct in_addr in;
1ceb81
-	char *address;
1ceb81
+#ifdef HAVE_INET_NTOP
1ceb81
+	char addr4[INET_ADDRSTRLEN];
1ceb81
+#endif
1ceb81
+	const char *address;
1ceb81
 
1ceb81
 	hp = php_network_gethostbyname(name);
1ceb81
 	if (!hp) {
1ceb81
@@ -281,7 +291,11 @@ static zend_string *php_gethostbyname(char *name)
1ceb81
 
1ceb81
 	memcpy(&in.s_addr, h_addr_0, sizeof(in.s_addr));
1ceb81
 
1ceb81
+#ifdef HAVE_INET_NTOP
1ceb81
+	address = inet_ntop(AF_INET, &in, addr4, INET_ADDRSTRLEN);
1ceb81
+#else
1ceb81
 	address = inet_ntoa(in);
1ceb81
+#endif
1ceb81
 	return zend_string_init(address, strlen(address), 0);
1ceb81
 }
1ceb81
 /* }}} */
1ceb81
diff --git a/main/network.c b/main/network.c
1ceb81
index 2c504952b2dd1..7f2f714ec42df 100644
1ceb81
--- a/main/network.c
1ceb81
+++ b/main/network.c
1ceb81
@@ -236,8 +236,12 @@ PHPAPI int php_network_getaddresses(const char *host, int socktype, struct socka
1ceb81
 	} while ((sai = sai->ai_next) != NULL);
1ceb81
 
1ceb81
 	freeaddrinfo(res);
1ceb81
+#else
1ceb81
+#ifdef HAVE_INET_PTON
1ceb81
+	if (!inet_pton(AF_INET, host, &in)) {
1ceb81
 #else
1ceb81
 	if (!inet_aton(host, &in)) {
1ceb81
+#endif
1ceb81
 		if(strlen(host) > MAXFQDNLEN) {
1ceb81
 			host_info = NULL;
1ceb81
 			errno = E2BIG;
1ceb81
@@ -555,7 +559,11 @@ PHPAPI int php_network_parse_network_address_with_port(const char *addr, zend_lo
1ceb81
 		goto out;
1ceb81
 	}
1ceb81
 #endif
1ceb81
+#ifdef HAVE_INET_PTON
1ceb81
+	if (inet_pton(AF_INET, tmp, &in4->sin_addr) > 0) {
1ceb81
+#else
1ceb81
 	if (inet_aton(tmp, &in4->sin_addr) > 0) {
1ceb81
+#endif
1ceb81
 		in4->sin_port = htons(port);
1ceb81
 		in4->sin_family = AF_INET;
1ceb81
 		*sl = sizeof(struct sockaddr_in);
1ceb81
@@ -617,15 +625,19 @@ PHPAPI void php_network_populate_name_from_sockaddr(
1ceb81
 	}
1ceb81
 
1ceb81
 	if (textaddr) {
1ceb81
-#if HAVE_IPV6 && HAVE_INET_NTOP
1ceb81
+#ifdef HAVE_INET_NTOP
1ceb81
 		char abuf[256];
1ceb81
 #endif
1ceb81
-		char *buf = NULL;
1ceb81
+		const char *buf = NULL;
1ceb81
 
1ceb81
 		switch (sa->sa_family) {
1ceb81
 			case AF_INET:
1ceb81
 				/* generally not thread safe, but it *is* thread safe under win32 */
1ceb81
+#ifdef HAVE_INET_NTOP
1ceb81
+				buf = inet_ntop(AF_INET, &((struct sockaddr_in*)sa)->sin_addr, (char *)&abuf, sizeof(abuf));
1ceb81
+#else
1ceb81
 				buf = inet_ntoa(((struct sockaddr_in*)sa)->sin_addr);
1ceb81
+#endif
1ceb81
 				if (buf) {
1ceb81
 					*textaddr = strpprintf(0, "%s:%d",
1ceb81
 						buf, ntohs(((struct sockaddr_in*)sa)->sin_port));
1ceb81
@@ -862,7 +874,11 @@ php_socket_t php_network_connect_socket_to_host(const char *host, unsigned short
1ceb81
 
1ceb81
 					in4->sin_family = sa->sa_family;
1ceb81
 					in4->sin_port = htons(bindport);
1ceb81
+#ifdef HAVE_INET_PTON
1ceb81
+					if (!inet_pton(AF_INET, bindto, &in4->sin_addr)) {
1ceb81
+#else
1ceb81
 					if (!inet_aton(bindto, &in4->sin_addr)) {
1ceb81
+#endif
1ceb81
 						php_error_docref(NULL, E_WARNING, "Invalid IP Address: %s", bindto);
1ceb81
 						goto skip_bind;
1ceb81
 					}
1ceb81
From e5b6f43ec7813392d83ea586b7902e0396a1f792 Mon Sep 17 00:00:00 2001
1ceb81
From: Remi Collet <remi@remirepo.net>
1ceb81
Date: Thu, 6 May 2021 14:21:29 +0200
1ceb81
Subject: [PATCH] get rid of inet_addr usage
1ceb81
1ceb81
---
1ceb81
 main/fastcgi.c            | 4 ++++
1ceb81
 sapi/litespeed/lsapilib.c | 4 ++++
1ceb81
 2 files changed, 8 insertions(+)
1ceb81
1ceb81
diff --git a/main/fastcgi.c b/main/fastcgi.c
1ceb81
index 071f69d3a7f0..c936d42405de 100644
1ceb81
--- a/main/fastcgi.c
1ceb81
+++ b/main/fastcgi.c
1ceb81
@@ -688,8 +688,12 @@ int fcgi_listen(const char *path, int backlog)
1ceb81
 		if (!*host || !strncmp(host, "*", sizeof("*")-1)) {
1ceb81
 			sa.sa_inet.sin_addr.s_addr = htonl(INADDR_ANY);
1ceb81
 		} else {
1ceb81
+#ifdef HAVE_INET_PTON
1ceb81
+			if (!inet_pton(AF_INET, host, &sa.sa_inet.sin_addr)) {
1ceb81
+#else
1ceb81
 			sa.sa_inet.sin_addr.s_addr = inet_addr(host);
1ceb81
 			if (sa.sa_inet.sin_addr.s_addr == INADDR_NONE) {
1ceb81
+#endif
1ceb81
 				struct hostent *hep;
1ceb81
 
1ceb81
 				if(strlen(host) > MAXFQDNLEN) {
1ceb81
diff --git a/sapi/litespeed/lsapilib.c b/sapi/litespeed/lsapilib.c
1ceb81
index a72b5dc1b988..305f3326a682 100644
1ceb81
--- a/sapi/litespeed/lsapilib.c
1ceb81
+++ b/sapi/litespeed/lsapilib.c
1ceb81
@@ -2672,8 +2672,12 @@ int LSAPI_ParseSockAddr( const char * pBind, struct sockaddr * pAddr )
1ceb81
             ((struct sockaddr_in *)pAddr)->sin_addr.s_addr = htonl( INADDR_LOOPBACK );
1ceb81
         else
1ceb81
         {
1ceb81
+#ifdef HAVE_INET_PTON
1ceb81
+            if (!inet_pton(AF_INET, p, &((struct sockaddr_in *)pAddr)->sin_addr))
1ceb81
+#else
1ceb81
             ((struct sockaddr_in *)pAddr)->sin_addr.s_addr = inet_addr( p );
1ceb81
             if ( ((struct sockaddr_in *)pAddr)->sin_addr.s_addr == INADDR_BROADCAST)
1ceb81
+#endif
1ceb81
             {
1ceb81
                 doAddrInfo = 1;
1ceb81
             }
1ceb81
From 99d67d121acd4c324738509679d23acaf759d065 Mon Sep 17 00:00:00 2001
1ceb81
From: Remi Collet <remi@remirepo.net>
1ceb81
Date: Thu, 6 May 2021 16:35:48 +0200
1ceb81
Subject: [PATCH] use getnameinfo instead of gethostbyaddr
1ceb81
1ceb81
---
1ceb81
 ext/standard/dns.c | 34 ++++++++++++++++++++++------------
1ceb81
 1 file changed, 22 insertions(+), 12 deletions(-)
1ceb81
1ceb81
diff --git a/ext/standard/dns.c b/ext/standard/dns.c
1ceb81
index edd9a4549f5c..540c777faaba 100644
1ceb81
--- a/ext/standard/dns.c
1ceb81
+++ b/ext/standard/dns.c
1ceb81
@@ -169,20 +169,30 @@ PHP_FUNCTION(gethostbyaddr)
1ceb81
 static zend_string *php_gethostbyaddr(char *ip)
1ceb81
 {
1ceb81
 #if HAVE_IPV6 && HAVE_INET_PTON
1ceb81
-	struct in6_addr addr6;
1ceb81
-#endif
1ceb81
-	struct in_addr addr;
1ceb81
-	struct hostent *hp;
1ceb81
+	struct sockaddr_in sa4;
1ceb81
+	struct sockaddr_in6 sa6;
1ceb81
+	char out[NI_MAXHOST];
1ceb81
 
1ceb81
-#if HAVE_IPV6 && HAVE_INET_PTON
1ceb81
-	if (inet_pton(AF_INET6, ip, &addr6)) {
1ceb81
-		hp = gethostbyaddr((char *) &addr6, sizeof(addr6), AF_INET6);
1ceb81
-	} else if (inet_pton(AF_INET, ip, &addr)) {
1ceb81
-		hp = gethostbyaddr((char *) &addr, sizeof(addr), AF_INET);
1ceb81
-	} else {
1ceb81
-		return NULL;
1ceb81
+	if (inet_pton(AF_INET6, ip, &sa6.sin6_addr)) {
1ceb81
+		sa6.sin6_family = AF_INET6;
1ceb81
+
1ceb81
+		if (getnameinfo((struct sockaddr *)&sa6, sizeof(sa6), out, sizeof(out), NULL, 0, NI_NAMEREQD) < 0) {
1ceb81
+			return zend_string_init(ip, strlen(ip), 0);
1ceb81
+		}
1ceb81
+		return zend_string_init(out, strlen(out), 0);
1ceb81
+	} else if (inet_pton(AF_INET, ip, &sa4.sin_addr)) {
1ceb81
+		sa4.sin_family = AF_INET;
1ceb81
+
1ceb81
+		if (getnameinfo((struct sockaddr *)&sa4, sizeof(sa4), out, sizeof(out), NULL, 0, NI_NAMEREQD) < 0) {
1ceb81
+			return zend_string_init(ip, strlen(ip), 0);
1ceb81
+		}
1ceb81
+		return zend_string_init(out, strlen(out), 0);
1ceb81
 	}
1ceb81
+	return NULL; /* not a valid IP */
1ceb81
 #else
1ceb81
+	struct in_addr addr;
1ceb81
+	struct hostent *hp;
1ceb81
+
1ceb81
 	addr.s_addr = inet_addr(ip);
1ceb81
 
1ceb81
 	if (addr.s_addr == -1) {
1ceb81
@@ -190,13 +200,13 @@ static zend_string *php_gethostbyaddr(char *ip)
1ceb81
 	}
1ceb81
 
1ceb81
 	hp = gethostbyaddr((char *) &addr, sizeof(addr), AF_INET);
1ceb81
-#endif
1ceb81
 
1ceb81
 	if (!hp || hp->h_name == NULL || hp->h_name[0] == '\0') {
1ceb81
 		return zend_string_init(ip, strlen(ip), 0);
1ceb81
 	}
1ceb81
 
1ceb81
 	return zend_string_init(hp->h_name, strlen(hp->h_name), 0);
1ceb81
+#endif
1ceb81
 }
1ceb81
 /* }}} */
1ceb81