Skip to content

get rid of inet_aton and inet_ntoa #6944

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 2 commits 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: 1 addition & 0 deletions ext/pdo_firebird/tests/CONFLICTS
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
firebird
4 changes: 4 additions & 0 deletions ext/sockets/sockaddr_conv.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,11 @@ int php_set_inet_addr(struct sockaddr_in *sin, char *string, php_socket *php_soc
struct in_addr tmp;
struct hostent *host_entry;

#ifdef HAVE_INET_PTON
if (inet_pton(AF_INET, string, &tmp)) {
#else
if (inet_aton(string, &tmp)) {
#endif
sin->sin_addr.s_addr = tmp.s_addr;
} else {
if (strlen(string) > MAXFQDNLEN || ! (host_entry = php_network_gethostbyname(string))) {
Expand Down
48 changes: 33 additions & 15 deletions ext/sockets/sockets.c
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,10 @@ zend_module_entry sockets_module_entry = {
ZEND_GET_MODULE(sockets)
#endif

#ifndef HAVE_INET_NTOP
/* inet_ntop should be used instead of inet_ntoa */
int inet_ntoa_lock = 0;
#endif

static int php_open_listen_sock(php_socket *sock, int port, int backlog) /* {{{ */
{
Expand Down Expand Up @@ -1082,10 +1084,12 @@ PHP_FUNCTION(socket_getsockname)
struct sockaddr_in *sin;
#if HAVE_IPV6
struct sockaddr_in6 *sin6;
char addr6[INET6_ADDRSTRLEN+1];
#endif
#ifdef HAVE_INET_NTOP
char addrbuf[INET6_ADDRSTRLEN];
#endif
struct sockaddr_un *s_un;
char *addr_string;
const char *addr_string;
socklen_t salen = sizeof(php_sockaddr_storage);

if (zend_parse_parameters(ZEND_NUM_ARGS(), "Oz|z", &arg1, socket_ce, &addr, &port) == FAILURE) {
Expand All @@ -1106,8 +1110,8 @@ PHP_FUNCTION(socket_getsockname)
#if HAVE_IPV6
case AF_INET6:
sin6 = (struct sockaddr_in6 *) sa;
inet_ntop(AF_INET6, &sin6->sin6_addr, addr6, INET6_ADDRSTRLEN);
ZEND_TRY_ASSIGN_REF_STRING(addr, addr6);
inet_ntop(AF_INET6, &sin6->sin6_addr, addrbuf, sizeof(addrbuf));
ZEND_TRY_ASSIGN_REF_STRING(addr, addrbuf);

if (port != NULL) {
ZEND_TRY_ASSIGN_REF_LONG(port, htons(sin6->sin6_port));
Expand All @@ -1117,11 +1121,14 @@ PHP_FUNCTION(socket_getsockname)
#endif
case AF_INET:
sin = (struct sockaddr_in *) sa;
#ifdef HAVE_INET_NTOP
addr_string = inet_ntop(AF_INET, &sin->sin_addr, addrbuf, sizeof(addrbuf));
#else
while (inet_ntoa_lock == 1);
inet_ntoa_lock = 1;
addr_string = inet_ntoa(sin->sin_addr);
inet_ntoa_lock = 0;

#endif
ZEND_TRY_ASSIGN_REF_STRING(addr, addr_string);

if (port != NULL) {
Expand Down Expand Up @@ -1154,10 +1161,12 @@ PHP_FUNCTION(socket_getpeername)
struct sockaddr_in *sin;
#if HAVE_IPV6
struct sockaddr_in6 *sin6;
char addr6[INET6_ADDRSTRLEN+1];
#endif
#ifdef HAVE_INET_NTOP
char addrbuf[INET6_ADDRSTRLEN];
#endif
struct sockaddr_un *s_un;
char *addr_string;
const char *addr_string;
socklen_t salen = sizeof(php_sockaddr_storage);

if (zend_parse_parameters(ZEND_NUM_ARGS(), "Oz|z", &arg1, socket_ce, &arg2, &arg3) == FAILURE) {
Expand All @@ -1178,9 +1187,9 @@ PHP_FUNCTION(socket_getpeername)
#if HAVE_IPV6
case AF_INET6:
sin6 = (struct sockaddr_in6 *) sa;
inet_ntop(AF_INET6, &sin6->sin6_addr, addr6, INET6_ADDRSTRLEN);
inet_ntop(AF_INET6, &sin6->sin6_addr, addrbuf, sizeof(addrbuf));

ZEND_TRY_ASSIGN_REF_STRING(arg2, addr6);
ZEND_TRY_ASSIGN_REF_STRING(arg2, addrbuf);

if (arg3 != NULL) {
ZEND_TRY_ASSIGN_REF_LONG(arg3, htons(sin6->sin6_port));
Expand All @@ -1191,11 +1200,14 @@ PHP_FUNCTION(socket_getpeername)
#endif
case AF_INET:
sin = (struct sockaddr_in *) sa;
#ifdef HAVE_INET_NTOP
addr_string = inet_ntop(AF_INET, &sin->sin_addr, addrbuf, sizeof(addrbuf));
#else
while (inet_ntoa_lock == 1);
inet_ntoa_lock = 1;
addr_string = inet_ntoa(sin->sin_addr);
inet_ntoa_lock = 0;

#endif
ZEND_TRY_ASSIGN_REF_STRING(arg2, addr_string);

if (arg3 != NULL) {
Expand Down Expand Up @@ -1527,12 +1539,14 @@ PHP_FUNCTION(socket_recvfrom)
struct sockaddr_in sin;
#if HAVE_IPV6
struct sockaddr_in6 sin6;
char addr6[INET6_ADDRSTRLEN];
#endif
#ifdef HAVE_INET_NTOP
char addrbuf[INET6_ADDRSTRLEN];
#endif
socklen_t slen;
int retval;
zend_long arg3, arg4;
char *address;
const char *address;
zend_string *recv_buf;

if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ozllz|z", &arg1, socket_ce, &arg2, &arg3, &arg4, &arg5, &arg6) == FAILURE) {
Expand Down Expand Up @@ -1590,7 +1604,11 @@ PHP_FUNCTION(socket_recvfrom)
ZSTR_LEN(recv_buf) = retval;
ZSTR_VAL(recv_buf)[ZSTR_LEN(recv_buf)] = '\0';

#ifdef HAVE_INET_NTOP
address = inet_ntop(AF_INET, &sin.sin_addr, addrbuf, sizeof(addrbuf));
#else
address = inet_ntoa(sin.sin_addr);
#endif

ZEND_TRY_ASSIGN_REF_NEW_STR(arg2, recv_buf);
ZEND_TRY_ASSIGN_REF_STRING(arg5, address ? address : "0.0.0.0");
Expand All @@ -1617,11 +1635,11 @@ PHP_FUNCTION(socket_recvfrom)
ZSTR_LEN(recv_buf) = retval;
ZSTR_VAL(recv_buf)[ZSTR_LEN(recv_buf)] = '\0';

memset(addr6, 0, INET6_ADDRSTRLEN);
inet_ntop(AF_INET6, &sin6.sin6_addr, addr6, INET6_ADDRSTRLEN);
memset(addrbuf, 0, INET6_ADDRSTRLEN);
inet_ntop(AF_INET6, &sin6.sin6_addr, addrbuf, sizeof(addrbuf));

ZEND_TRY_ASSIGN_REF_NEW_STR(arg2, recv_buf);
ZEND_TRY_ASSIGN_REF_STRING(arg5, addr6[0] ? addr6 : "::");
ZEND_TRY_ASSIGN_REF_STRING(arg5, addrbuf[0] ? addrbuf : "::");
ZEND_TRY_ASSIGN_REF_LONG(arg6, ntohs(sin6.sin6_port));
break;
#endif
Expand Down
16 changes: 15 additions & 1 deletion ext/standard/dns.c
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,9 @@ 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 Down Expand Up @@ -255,7 +258,11 @@ 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
}
}
/* }}} */
Expand All @@ -266,7 +273,10 @@ 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;
char *address;
#ifdef HAVE_INET_NTOP
char addr4[INET_ADDRSTRLEN];
#endif
const char *address;

hp = php_network_gethostbyname(name);
if (!hp) {
Expand All @@ -281,7 +291,11 @@ 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
return zend_string_init(address, strlen(address), 0);
}
/* }}} */
Expand Down
20 changes: 18 additions & 2 deletions main/network.c
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,12 @@ PHPAPI int php_network_getaddresses(const char *host, int socktype, struct socka
} while ((sai = sai->ai_next) != NULL);

freeaddrinfo(res);
#else
#ifdef HAVE_INET_PTON
if (!inet_pton(AF_INET, host, &in)) {
#else
if (!inet_aton(host, &in)) {
#endif
if(strlen(host) > MAXFQDNLEN) {
host_info = NULL;
errno = E2BIG;
Expand Down Expand Up @@ -555,7 +559,11 @@ PHPAPI int php_network_parse_network_address_with_port(const char *addr, zend_lo
goto out;
}
#endif
#ifdef HAVE_INET_PTON
if (inet_pton(AF_INET, tmp, &in4->sin_addr) > 0) {
#else
if (inet_aton(tmp, &in4->sin_addr) > 0) {
#endif
in4->sin_port = htons(port);
in4->sin_family = AF_INET;
*sl = sizeof(struct sockaddr_in);
Expand Down Expand Up @@ -617,15 +625,19 @@ PHPAPI void php_network_populate_name_from_sockaddr(
}

if (textaddr) {
#if HAVE_IPV6 && HAVE_INET_NTOP
#ifdef HAVE_INET_NTOP
char abuf[256];
#endif
char *buf = NULL;
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));
Expand Down Expand Up @@ -862,7 +874,11 @@ php_socket_t php_network_connect_socket_to_host(const char *host, unsigned short

in4->sin_family = sa->sa_family;
in4->sin_port = htons(bindport);
#ifdef HAVE_INET_PTON
if (!inet_pton(AF_INET, bindto, &in4->sin_addr)) {
#else
if (!inet_aton(bindto, &in4->sin_addr)) {
#endif
php_error_docref(NULL, E_WARNING, "Invalid IP Address: %s", bindto);
goto skip_bind;
}
Expand Down