Skip to content

following-up on GH-12551: removing inet_ntoa usage #12554

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,6 @@ getgrnam_r \
getpwuid_r \
getwd \
glob \
inet_ntoa \
inet_ntop \
inet_pton \
localtime_r \
Expand Down
11 changes: 9 additions & 2 deletions ext/ftp/ftp.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@
#include <openssl/err.h>
#endif

#ifndef HAVE_INET_NTOP
#error inet_ntop unsupported on this platform
#endif

#include "ftp.h"
#include "ext/standard/fsock.h"

Expand Down Expand Up @@ -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) {
Expand Down
12 changes: 5 additions & 7 deletions ext/snmp/snmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@
#endif
#include <locale.h>

#ifndef HAVE_INET_NTOP
#error inet_ntop unsupported on this platform
#endif

#ifndef __P
#ifdef __GNUC__
#define __P(args) args
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down
4 changes: 0 additions & 4 deletions ext/sockets/sockets.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
10 changes: 4 additions & 6 deletions ext/standard/basic_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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;
}
Comment on lines 657 to 661
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this actually fail? because otherwise the call to this in dns.c looks fishy?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would need to recheck some of them indeed.

#else
RETURN_STRING(inet_ntoa(myaddr));
#endif
}
/* }}} */

Expand Down
38 changes: 23 additions & 15 deletions ext/standard/dns.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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);
}
}
/* }}} */

Expand All @@ -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)
Expand All @@ -267,18 +275,21 @@ 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) {
return;
}

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);
}
}
}
/* }}} */
Expand All @@ -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);
Expand All @@ -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);
}
/* }}} */
Expand Down
10 changes: 8 additions & 2 deletions ext/standard/dns_win32.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <windows.h>
#include <Winbase.h >
#include <Windns.h>
#include <Ws2tcpip.h>

#include "php_dns.h"

Expand Down Expand Up @@ -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;
}

Expand Down
7 changes: 4 additions & 3 deletions ext/standard/net.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,15 @@
# include <netdb.h>
#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: {
Expand All @@ -76,7 +78,6 @@ PHPAPI zend_string* php_inet_ntop(const struct sockaddr *addr) {
break;
}
}
#endif

/* Fallback on getnameinfo() */
switch (addr->sa_family) {
Expand Down
12 changes: 5 additions & 7 deletions main/network.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -620,27 +624,21 @@ 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));
}

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) {
Expand Down