Skip to content

Commit 07d53ff

Browse files
committed
following-up on phpGH-12551: removing inet_ntoa usage
1 parent 1c8943b commit 07d53ff

File tree

9 files changed

+58
-47
lines changed

9 files changed

+58
-47
lines changed

configure.ac

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,6 @@ getgrnam_r \
610610
getpwuid_r \
611611
getwd \
612612
glob \
613-
inet_ntoa \
614613
inet_ntop \
615614
inet_pton \
616615
localtime_r \

ext/ftp/ftp.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@
5656
#include <openssl/err.h>
5757
#endif
5858

59+
#ifndef HAVE_INET_NTOP
60+
#error inet_ntop unsupported on this platform
61+
#endif
62+
5963
#include "ftp.h"
6064
#include "ext/standard/fsock.h"
6165

@@ -1685,13 +1689,16 @@ ftp_getdata(ftpbuf_t *ftp)
16851689

16861690
data->listener = fd;
16871691

1688-
#if defined(HAVE_IPV6) && defined(HAVE_INET_NTOP)
1692+
#if defined(HAVE_IPV6)
16891693
if (sa->sa_family == AF_INET6) {
16901694
/* need to use EPRT */
16911695
char eprtarg[INET6_ADDRSTRLEN + sizeof("|x||xxxxx|")];
16921696
char out[INET6_ADDRSTRLEN];
16931697
int eprtarg_len;
1694-
inet_ntop(AF_INET6, &((struct sockaddr_in6*) sa)->sin6_addr, out, sizeof(out));
1698+
if (!inet_ntop(AF_INET6, &((struct sockaddr_in6*) sa)->sin6_addr, out, sizeof(out))) {
1699+
goto bail;
1700+
}
1701+
16951702
eprtarg_len = snprintf(eprtarg, sizeof(eprtarg), "|2|%s|%hu|", out, ntohs(((struct sockaddr_in6 *) &addr)->sin6_port));
16961703

16971704
if (eprtarg_len < 0) {

ext/snmp/snmp.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@
5151
#endif
5252
#include <locale.h>
5353

54+
#ifndef HAVE_INET_NTOP
55+
#error inet_ntop unsupported on this platform
56+
#endif
57+
5458
#ifndef __P
5559
#ifdef __GNUC__
5660
#define __P(args) args
@@ -843,7 +847,7 @@ static bool netsnmp_session_init(php_snmp_session **session_p, int version, zend
843847
res = psal;
844848
while (n-- > 0) {
845849
pptr = session->peername;
846-
#if defined(HAVE_GETADDRINFO) && defined(HAVE_IPV6) && defined(HAVE_INET_NTOP)
850+
#if defined(HAVE_GETADDRINFO) && defined(HAVE_IPV6)
847851
if (force_ipv6 && (*res)->sa_family != AF_INET6) {
848852
res++;
849853
continue;
@@ -859,12 +863,6 @@ static bool netsnmp_session_init(php_snmp_session **session_p, int version, zend
859863
res++;
860864
continue;
861865
}
862-
#else
863-
if ((*res)->sa_family != AF_INET) {
864-
res++;
865-
continue;
866-
}
867-
strcat(pptr, inet_ntoa(((struct sockaddr_in*)(*res))->sin_addr));
868866
#endif
869867
break;
870868
}

ext/sockets/sockets.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -927,9 +927,7 @@ PHP_FUNCTION(socket_getsockname)
927927
#if HAVE_IPV6
928928
struct sockaddr_in6 *sin6;
929929
#endif
930-
#ifdef HAVE_INET_NTOP
931930
char addrbuf[INET6_ADDRSTRLEN];
932-
#endif
933931
struct sockaddr_un *s_un;
934932
const char *addr_string;
935933
socklen_t salen = sizeof(php_sockaddr_storage);
@@ -997,9 +995,7 @@ PHP_FUNCTION(socket_getpeername)
997995
#if HAVE_IPV6
998996
struct sockaddr_in6 *sin6;
999997
#endif
1000-
#ifdef HAVE_INET_NTOP
1001998
char addrbuf[INET6_ADDRSTRLEN];
1002-
#endif
1003999
struct sockaddr_un *s_un;
10041000
const char *addr_string;
10051001
socklen_t salen = sizeof(php_sockaddr_storage);

ext/standard/basic_functions.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ typedef struct yy_buffer_state *YY_BUFFER_STATE;
102102
# define INADDR_NONE ((zend_ulong) -1)
103103
#endif
104104

105+
#ifndef HAVE_INET_NTOP
106+
# error inet_ntop unsupported on this platform
107+
#endif
108+
105109
#include "zend_globals.h"
106110
#include "php_globals.h"
107111
#include "SAPI.h"
@@ -531,7 +535,6 @@ PHP_FUNCTION(constant)
531535
}
532536
/* }}} */
533537

534-
#ifdef HAVE_INET_NTOP
535538
/* {{{ Converts a packed inet address to a human readable IP address string */
536539
PHP_FUNCTION(inet_ntop)
537540
{
@@ -560,7 +563,6 @@ PHP_FUNCTION(inet_ntop)
560563
RETURN_STRING(buffer);
561564
}
562565
/* }}} */
563-
#endif /* HAVE_INET_NTOP */
564566

565567
#ifdef HAVE_INET_PTON
566568
/* {{{ Converts a human readable IP address to a packed binary string */
@@ -652,15 +654,11 @@ PHP_FUNCTION(long2ip)
652654
ip = (zend_ulong)sip;
653655

654656
myaddr.s_addr = htonl(ip);
655-
#ifdef HAVE_INET_PTON
656657
if (inet_ntop(AF_INET, &myaddr, str, sizeof(str))) {
657658
RETURN_STRING(str);
658659
} else {
659660
RETURN_FALSE;
660661
}
661-
#else
662-
RETURN_STRING(inet_ntoa(myaddr));
663-
#endif
664662
}
665663
/* }}} */
666664

ext/standard/dns.c

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ extern void __res_ndestroy(res_state statp);
5757
#endif
5858
#endif
5959

60+
#ifndef HAVE_INET_NTOP
61+
#error inet_ntop unsupported on this platform
62+
#endif
63+
6064
#ifndef MAXHOSTNAMELEN
6165
#define MAXHOSTNAMELEN 255
6266
#endif
@@ -221,6 +225,7 @@ PHP_FUNCTION(gethostbyname)
221225
{
222226
char *hostname;
223227
size_t hostname_len;
228+
zend_string *ipaddr;
224229

225230
ZEND_PARSE_PARAMETERS_START(1, 1)
226231
Z_PARAM_PATH(hostname, hostname_len)
@@ -232,7 +237,12 @@ PHP_FUNCTION(gethostbyname)
232237
RETURN_STRINGL(hostname, hostname_len);
233238
}
234239

235-
RETURN_STR(php_gethostbyname(hostname));
240+
if (!(ipaddr = php_gethostbyname(hostname))) {
241+
php_error_docref(NULL, E_WARNING, "Host name to ip failed %s", hostname);
242+
RETURN_STRINGL(hostname, hostname_len);
243+
} else {
244+
RETURN_STR(ipaddr);
245+
}
236246
}
237247
/* }}} */
238248

@@ -244,9 +254,7 @@ PHP_FUNCTION(gethostbynamel)
244254
struct hostent *hp;
245255
struct in_addr in;
246256
int i;
247-
#ifdef HAVE_INET_NTOP
248257
char addr4[INET_ADDRSTRLEN];
249-
#endif
250258

251259
ZEND_PARSE_PARAMETERS_START(1, 1)
252260
Z_PARAM_PATH(hostname, hostname_len)
@@ -267,18 +275,21 @@ PHP_FUNCTION(gethostbynamel)
267275

268276
for (i = 0;; i++) {
269277
/* On macos h_addr_list entries may be misaligned. */
278+
const char *ipaddr;
270279
struct in_addr *h_addr_entry; /* Don't call this h_addr, it's a macro! */
271280
memcpy(&h_addr_entry, &hp->h_addr_list[i], sizeof(struct in_addr *));
272281
if (!h_addr_entry) {
273282
return;
274283
}
275284

276285
in = *h_addr_entry;
277-
#ifdef HAVE_INET_NTOP
278-
add_next_index_string(return_value, inet_ntop(AF_INET, &in, addr4, INET_ADDRSTRLEN));
279-
#else
280-
add_next_index_string(return_value, inet_ntoa(in));
281-
#endif
286+
if (!(ipaddr = inet_ntop(AF_INET, &in, addr4, INET_ADDRSTRLEN))) {
287+
/* unlikely regarding (too) long hostname and protocols but checking still */
288+
php_error_docref(NULL, E_WARNING, "Host name to ip failed %s", hostname);
289+
continue;
290+
} else {
291+
add_next_index_string(return_value, ipaddr);
292+
}
282293
}
283294
}
284295
/* }}} */
@@ -289,9 +300,7 @@ static zend_string *php_gethostbyname(char *name)
289300
struct hostent *hp;
290301
struct in_addr *h_addr_0; /* Don't call this h_addr, it's a macro! */
291302
struct in_addr in;
292-
#ifdef HAVE_INET_NTOP
293303
char addr4[INET_ADDRSTRLEN];
294-
#endif
295304
const char *address;
296305

297306
hp = php_network_gethostbyname(name);
@@ -307,11 +316,10 @@ static zend_string *php_gethostbyname(char *name)
307316

308317
memcpy(&in.s_addr, h_addr_0, sizeof(in.s_addr));
309318

310-
#ifdef HAVE_INET_NTOP
311-
address = inet_ntop(AF_INET, &in, addr4, INET_ADDRSTRLEN);
312-
#else
313-
address = inet_ntoa(in);
314-
#endif
319+
if (!(address = inet_ntop(AF_INET, &in, addr4, INET_ADDRSTRLEN))) {
320+
return NULL;
321+
}
322+
315323
return zend_string_init(address, strlen(address), 0);
316324
}
317325
/* }}} */

ext/standard/dns_win32.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <windows.h>
2020
#include <Winbase.h >
2121
#include <Windns.h>
22+
#include <Ws2tcpip.h>
2223

2324
#include "php_dns.h"
2425

@@ -174,9 +175,14 @@ static void php_parserr(PDNS_RECORD pRec, int type_to_fetch, int store, bool raw
174175
switch (type) {
175176
case DNS_TYPE_A: {
176177
IN_ADDR ipaddr;
178+
char ip[INET_ADDRSTRLEN];
177179
ipaddr.S_un.S_addr = (pRec->Data.A.IpAddress);
178-
add_assoc_string(subarray, "type", "A");
179-
add_assoc_string(subarray, "ip", inet_ntoa(ipaddr));
180+
if (!inet_ntop(AF_INET, &ipaddr, ip, INET_ADDRSTRLEN)) {
181+
ZVAL_UNDEF(subarray);
182+
} else {
183+
add_assoc_string(subarray, "type", "A");
184+
add_assoc_string(subarray, "ip", ip);
185+
}
180186
break;
181187
}
182188

ext/standard/net.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,15 @@
4747
# include <netdb.h>
4848
#endif
4949

50+
#ifndef HAVE_INET_NTOP
51+
#error inet_ntop unsupported on this platform
52+
#endif
53+
5054
PHPAPI zend_string* php_inet_ntop(const struct sockaddr *addr) {
5155
socklen_t addrlen = sizeof(struct sockaddr_in);
5256

5357
if (!addr) { return NULL; }
5458

55-
/* Prefer inet_ntop() as it's more task-specific and doesn't have to be demangled */
56-
#ifdef HAVE_INET_NTOP
5759
switch (addr->sa_family) {
5860
#ifdef AF_INET6
5961
case AF_INET6: {
@@ -76,7 +78,6 @@ PHPAPI zend_string* php_inet_ntop(const struct sockaddr *addr) {
7678
break;
7779
}
7880
}
79-
#endif
8081

8182
/* Fallback on getnameinfo() */
8283
switch (addr->sa_family) {

main/network.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@
6060
#endif
6161
#endif
6262

63+
#ifndef HAVE_INET_NTOP
64+
#error inet_ntop unsupported on this platform
65+
#endif
66+
6367
#ifndef HAVE_INET_ATON
6468
int inet_aton(const char *, struct in_addr *);
6569
#endif
@@ -620,27 +624,21 @@ PHPAPI void php_network_populate_name_from_sockaddr(
620624
}
621625

622626
if (textaddr) {
623-
#ifdef HAVE_INET_NTOP
624627
char abuf[256];
625-
#endif
626628
const char *buf = NULL;
627629

628630
switch (sa->sa_family) {
629631
case AF_INET:
630632
/* generally not thread safe, but it *is* thread safe under win32 */
631-
#ifdef HAVE_INET_NTOP
632633
buf = inet_ntop(AF_INET, &((struct sockaddr_in*)sa)->sin_addr, (char *)&abuf, sizeof(abuf));
633-
#else
634-
buf = inet_ntoa(((struct sockaddr_in*)sa)->sin_addr);
635-
#endif
636634
if (buf) {
637635
*textaddr = strpprintf(0, "%s:%d",
638636
buf, ntohs(((struct sockaddr_in*)sa)->sin_port));
639637
}
640638

641639
break;
642640

643-
#if HAVE_IPV6 && HAVE_INET_NTOP
641+
#if HAVE_IPV6
644642
case AF_INET6:
645643
buf = (char*)inet_ntop(sa->sa_family, &((struct sockaddr_in6*)sa)->sin6_addr, (char *)&abuf, sizeof(abuf));
646644
if (buf) {

0 commit comments

Comments
 (0)