get rid of inet_aton and inet_ntoa use inet_ntop iand inet_pton where available standardize buffer size

This commit is contained in:
Remi Collet authored and Remi Collet committed 2021-05-06 13:39:58 +02:00
1 parent 01b3fc03c3
commit f9547f2b47
4 files changed
+70 -18

No files matched your search

+4
View File
@@ -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))) {
+33 -15
View File
@@ -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) /* {{{ */
{
@@ -1072,10 +1074,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) {
@@ -1096,8 +1100,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));
@@ -1107,11 +1111,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) {
@@ -1144,10 +1151,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) {
@@ -1168,9 +1177,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));
@@ -1181,11 +1190,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) {
@@ -1517,12 +1529,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) {
@@ -1580,7 +1594,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");
@@ -1607,11 +1625,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
+15 -1
View File
@@ -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)
@@ -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
}
}
/* }}} */
@@ -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) {
@@ -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);
}
/* }}} */
+18 -2
View File
@@ -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;
@@ -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);
@@ -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));
@@ -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;
}