Skip to content

Commit 4dc8b3c

Browse files
committed
get rid of inet_aton and inet_ntoa
use inet_ntop iand inet_pton where available standardize buffer size
1 parent cfc8071 commit 4dc8b3c

File tree

4 files changed

+70
-18
lines changed

4 files changed

+70
-18
lines changed

ext/sockets/sockaddr_conv.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,11 @@ int php_set_inet_addr(struct sockaddr_in *sin, char *string, php_socket *php_soc
8787
struct in_addr tmp;
8888
struct hostent *host_entry;
8989

90+
#ifdef HAVE_INET_PTON
91+
if (inet_pton(AF_INET, string, &tmp)) {
92+
#else
9093
if (inet_aton(string, &tmp)) {
94+
#endif
9195
sin->sin_addr.s_addr = tmp.s_addr;
9296
} else {
9397
if (strlen(string) > MAXFQDNLEN || ! (host_entry = php_network_gethostbyname(string))) {

ext/sockets/sockets.c

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,10 @@ zend_module_entry sockets_module_entry = {
220220
ZEND_GET_MODULE(sockets)
221221
#endif
222222

223+
#ifndef HAVE_INET_NTOP
223224
/* inet_ntop should be used instead of inet_ntoa */
224225
int inet_ntoa_lock = 0;
226+
#endif
225227

226228
static int php_open_listen_sock(php_socket *sock, int port, int backlog) /* {{{ */
227229
{
@@ -1082,10 +1084,12 @@ PHP_FUNCTION(socket_getsockname)
10821084
struct sockaddr_in *sin;
10831085
#if HAVE_IPV6
10841086
struct sockaddr_in6 *sin6;
1085-
char addr6[INET6_ADDRSTRLEN+1];
1087+
#endif
1088+
#ifdef HAVE_INET_NTOP
1089+
char addrbuf[INET6_ADDRSTRLEN];
10861090
#endif
10871091
struct sockaddr_un *s_un;
1088-
char *addr_string;
1092+
const char *addr_string;
10891093
socklen_t salen = sizeof(php_sockaddr_storage);
10901094

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

11121116
if (port != NULL) {
11131117
ZEND_TRY_ASSIGN_REF_LONG(port, htons(sin6->sin6_port));
@@ -1117,11 +1121,14 @@ PHP_FUNCTION(socket_getsockname)
11171121
#endif
11181122
case AF_INET:
11191123
sin = (struct sockaddr_in *) sa;
1124+
#ifdef HAVE_INET_NTOP
1125+
addr_string = inet_ntop(AF_INET, &sin->sin_addr, addrbuf, sizeof(addrbuf));
1126+
#else
11201127
while (inet_ntoa_lock == 1);
11211128
inet_ntoa_lock = 1;
11221129
addr_string = inet_ntoa(sin->sin_addr);
11231130
inet_ntoa_lock = 0;
1124-
1131+
#endif
11251132
ZEND_TRY_ASSIGN_REF_STRING(addr, addr_string);
11261133

11271134
if (port != NULL) {
@@ -1154,10 +1161,12 @@ PHP_FUNCTION(socket_getpeername)
11541161
struct sockaddr_in *sin;
11551162
#if HAVE_IPV6
11561163
struct sockaddr_in6 *sin6;
1157-
char addr6[INET6_ADDRSTRLEN+1];
1164+
#endif
1165+
#ifdef HAVE_INET_NTOP
1166+
char addrbuf[INET6_ADDRSTRLEN];
11581167
#endif
11591168
struct sockaddr_un *s_un;
1160-
char *addr_string;
1169+
const char *addr_string;
11611170
socklen_t salen = sizeof(php_sockaddr_storage);
11621171

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

1183-
ZEND_TRY_ASSIGN_REF_STRING(arg2, addr6);
1192+
ZEND_TRY_ASSIGN_REF_STRING(arg2, addrbuf);
11841193

11851194
if (arg3 != NULL) {
11861195
ZEND_TRY_ASSIGN_REF_LONG(arg3, htons(sin6->sin6_port));
@@ -1191,11 +1200,14 @@ PHP_FUNCTION(socket_getpeername)
11911200
#endif
11921201
case AF_INET:
11931202
sin = (struct sockaddr_in *) sa;
1203+
#ifdef HAVE_INET_NTOP
1204+
addr_string = inet_ntop(AF_INET, &sin->sin_addr, addrbuf, sizeof(addrbuf));
1205+
#else
11941206
while (inet_ntoa_lock == 1);
11951207
inet_ntoa_lock = 1;
11961208
addr_string = inet_ntoa(sin->sin_addr);
11971209
inet_ntoa_lock = 0;
1198-
1210+
#endif
11991211
ZEND_TRY_ASSIGN_REF_STRING(arg2, addr_string);
12001212

12011213
if (arg3 != NULL) {
@@ -1527,12 +1539,14 @@ PHP_FUNCTION(socket_recvfrom)
15271539
struct sockaddr_in sin;
15281540
#if HAVE_IPV6
15291541
struct sockaddr_in6 sin6;
1530-
char addr6[INET6_ADDRSTRLEN];
1542+
#endif
1543+
#ifdef HAVE_INET_NTOP
1544+
char addrbuf[INET6_ADDRSTRLEN];
15311545
#endif
15321546
socklen_t slen;
15331547
int retval;
15341548
zend_long arg3, arg4;
1535-
char *address;
1549+
const char *address;
15361550
zend_string *recv_buf;
15371551

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

1607+
#ifdef HAVE_INET_NTOP
1608+
address = inet_ntop(AF_INET, &sin.sin_addr, addrbuf, sizeof(addrbuf));
1609+
#else
15931610
address = inet_ntoa(sin.sin_addr);
1611+
#endif
15941612

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

1620-
memset(addr6, 0, INET6_ADDRSTRLEN);
1621-
inet_ntop(AF_INET6, &sin6.sin6_addr, addr6, INET6_ADDRSTRLEN);
1638+
memset(addrbuf, 0, INET6_ADDRSTRLEN);
1639+
inet_ntop(AF_INET6, &sin6.sin6_addr, addrbuf, sizeof(addrbuf));
16221640

16231641
ZEND_TRY_ASSIGN_REF_NEW_STR(arg2, recv_buf);
1624-
ZEND_TRY_ASSIGN_REF_STRING(arg5, addr6[0] ? addr6 : "::");
1642+
ZEND_TRY_ASSIGN_REF_STRING(arg5, addrbuf[0] ? addrbuf : "::");
16251643
ZEND_TRY_ASSIGN_REF_LONG(arg6, ntohs(sin6.sin6_port));
16261644
break;
16271645
#endif

ext/standard/dns.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,9 @@ PHP_FUNCTION(gethostbynamel)
228228
struct hostent *hp;
229229
struct in_addr in;
230230
int i;
231+
#ifdef HAVE_INET_NTOP
232+
char addr4[INET_ADDRSTRLEN];
233+
#endif
231234

232235
ZEND_PARSE_PARAMETERS_START(1, 1)
233236
Z_PARAM_PATH(hostname, hostname_len)
@@ -255,7 +258,11 @@ PHP_FUNCTION(gethostbynamel)
255258
}
256259

257260
in = *h_addr_entry;
261+
#ifdef HAVE_INET_NTOP
262+
add_next_index_string(return_value, inet_ntop(AF_INET, &in, addr4, INET_ADDRSTRLEN));
263+
#else
258264
add_next_index_string(return_value, inet_ntoa(in));
265+
#endif
259266
}
260267
}
261268
/* }}} */
@@ -266,7 +273,10 @@ static zend_string *php_gethostbyname(char *name)
266273
struct hostent *hp;
267274
struct in_addr *h_addr_0; /* Don't call this h_addr, it's a macro! */
268275
struct in_addr in;
269-
char *address;
276+
#ifdef HAVE_INET_NTOP
277+
char addr4[INET_ADDRSTRLEN];
278+
#endif
279+
const char *address;
270280

271281
hp = php_network_gethostbyname(name);
272282
if (!hp) {
@@ -281,7 +291,11 @@ static zend_string *php_gethostbyname(char *name)
281291

282292
memcpy(&in.s_addr, h_addr_0, sizeof(in.s_addr));
283293

294+
#ifdef HAVE_INET_NTOP
295+
address = inet_ntop(AF_INET, &in, addr4, INET_ADDRSTRLEN);
296+
#else
284297
address = inet_ntoa(in);
298+
#endif
285299
return zend_string_init(address, strlen(address), 0);
286300
}
287301
/* }}} */

main/network.c

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,12 @@ PHPAPI int php_network_getaddresses(const char *host, int socktype, struct socka
236236
} while ((sai = sai->ai_next) != NULL);
237237

238238
freeaddrinfo(res);
239+
#else
240+
#ifdef HAVE_INET_PTON
241+
if (!inet_pton(AF_INET, host, &in)) {
239242
#else
240243
if (!inet_aton(host, &in)) {
244+
#endif
241245
if(strlen(host) > MAXFQDNLEN) {
242246
host_info = NULL;
243247
errno = E2BIG;
@@ -555,7 +559,11 @@ PHPAPI int php_network_parse_network_address_with_port(const char *addr, zend_lo
555559
goto out;
556560
}
557561
#endif
562+
#ifdef HAVE_INET_PTON
563+
if (inet_pton(AF_INET, tmp, &in4->sin_addr) > 0) {
564+
#else
558565
if (inet_aton(tmp, &in4->sin_addr) > 0) {
566+
#endif
559567
in4->sin_port = htons(port);
560568
in4->sin_family = AF_INET;
561569
*sl = sizeof(struct sockaddr_in);
@@ -617,15 +625,19 @@ PHPAPI void php_network_populate_name_from_sockaddr(
617625
}
618626

619627
if (textaddr) {
620-
#if HAVE_IPV6 && HAVE_INET_NTOP
628+
#ifdef HAVE_INET_NTOP
621629
char abuf[256];
622630
#endif
623-
char *buf = NULL;
631+
const char *buf = NULL;
624632

625633
switch (sa->sa_family) {
626634
case AF_INET:
627635
/* generally not thread safe, but it *is* thread safe under win32 */
636+
#ifdef HAVE_INET_NTOP
637+
buf = inet_ntop(AF_INET, &((struct sockaddr_in*)sa)->sin_addr, (char *)&abuf, sizeof(abuf));
638+
#else
628639
buf = inet_ntoa(((struct sockaddr_in*)sa)->sin_addr);
640+
#endif
629641
if (buf) {
630642
*textaddr = strpprintf(0, "%s:%d",
631643
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
862874

863875
in4->sin_family = sa->sa_family;
864876
in4->sin_port = htons(bindport);
877+
#ifdef HAVE_INET_PTON
878+
if (!inet_pton(AF_INET, bindto, &in4->sin_addr)) {
879+
#else
865880
if (!inet_aton(bindto, &in4->sin_addr)) {
881+
#endif
866882
php_error_docref(NULL, E_WARNING, "Invalid IP Address: %s", bindto);
867883
goto skip_bind;
868884
}

0 commit comments

Comments
 (0)