Skip to content

Commit f9547f2

Browse files
committed
get rid of inet_aton and inet_ntoa use inet_ntop iand inet_pton where available standardize buffer size
1 parent 01b3fc0 commit f9547f2

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
{
@@ -1072,10 +1074,12 @@ PHP_FUNCTION(socket_getsockname)
10721074
struct sockaddr_in *sin;
10731075
#if HAVE_IPV6
10741076
struct sockaddr_in6 *sin6;
1075-
char addr6[INET6_ADDRSTRLEN+1];
1077+
#endif
1078+
#ifdef HAVE_INET_NTOP
1079+
char addrbuf[INET6_ADDRSTRLEN];
10761080
#endif
10771081
struct sockaddr_un *s_un;
1078-
char *addr_string;
1082+
const char *addr_string;
10791083
socklen_t salen = sizeof(php_sockaddr_storage);
10801084

10811085
if (zend_parse_parameters(ZEND_NUM_ARGS(), "Oz|z", &arg1, socket_ce, &addr, &port) == FAILURE) {
@@ -1096,8 +1100,8 @@ PHP_FUNCTION(socket_getsockname)
10961100
#if HAVE_IPV6
10971101
case AF_INET6:
10981102
sin6 = (struct sockaddr_in6 *) sa;
1099-
inet_ntop(AF_INET6, &sin6->sin6_addr, addr6, INET6_ADDRSTRLEN);
1100-
ZEND_TRY_ASSIGN_REF_STRING(addr, addr6);
1103+
inet_ntop(AF_INET6, &sin6->sin6_addr, addrbuf, sizeof(addrbuf));
1104+
ZEND_TRY_ASSIGN_REF_STRING(addr, addrbuf);
11011105

11021106
if (port != NULL) {
11031107
ZEND_TRY_ASSIGN_REF_LONG(port, htons(sin6->sin6_port));
@@ -1107,11 +1111,14 @@ PHP_FUNCTION(socket_getsockname)
11071111
#endif
11081112
case AF_INET:
11091113
sin = (struct sockaddr_in *) sa;
1114+
#ifdef HAVE_INET_NTOP
1115+
addr_string = inet_ntop(AF_INET, &sin->sin_addr, addrbuf, sizeof(addrbuf));
1116+
#else
11101117
while (inet_ntoa_lock == 1);
11111118
inet_ntoa_lock = 1;
11121119
addr_string = inet_ntoa(sin->sin_addr);
11131120
inet_ntoa_lock = 0;
1114-
1121+
#endif
11151122
ZEND_TRY_ASSIGN_REF_STRING(addr, addr_string);
11161123

11171124
if (port != NULL) {
@@ -1144,10 +1151,12 @@ PHP_FUNCTION(socket_getpeername)
11441151
struct sockaddr_in *sin;
11451152
#if HAVE_IPV6
11461153
struct sockaddr_in6 *sin6;
1147-
char addr6[INET6_ADDRSTRLEN+1];
1154+
#endif
1155+
#ifdef HAVE_INET_NTOP
1156+
char addrbuf[INET6_ADDRSTRLEN];
11481157
#endif
11491158
struct sockaddr_un *s_un;
1150-
char *addr_string;
1159+
const char *addr_string;
11511160
socklen_t salen = sizeof(php_sockaddr_storage);
11521161

11531162
if (zend_parse_parameters(ZEND_NUM_ARGS(), "Oz|z", &arg1, socket_ce, &arg2, &arg3) == FAILURE) {
@@ -1168,9 +1177,9 @@ PHP_FUNCTION(socket_getpeername)
11681177
#if HAVE_IPV6
11691178
case AF_INET6:
11701179
sin6 = (struct sockaddr_in6 *) sa;
1171-
inet_ntop(AF_INET6, &sin6->sin6_addr, addr6, INET6_ADDRSTRLEN);
1180+
inet_ntop(AF_INET6, &sin6->sin6_addr, addrbuf, sizeof(addrbuf));
11721181

1173-
ZEND_TRY_ASSIGN_REF_STRING(arg2, addr6);
1182+
ZEND_TRY_ASSIGN_REF_STRING(arg2, addrbuf);
11741183

11751184
if (arg3 != NULL) {
11761185
ZEND_TRY_ASSIGN_REF_LONG(arg3, htons(sin6->sin6_port));
@@ -1181,11 +1190,14 @@ PHP_FUNCTION(socket_getpeername)
11811190
#endif
11821191
case AF_INET:
11831192
sin = (struct sockaddr_in *) sa;
1193+
#ifdef HAVE_INET_NTOP
1194+
addr_string = inet_ntop(AF_INET, &sin->sin_addr, addrbuf, sizeof(addrbuf));
1195+
#else
11841196
while (inet_ntoa_lock == 1);
11851197
inet_ntoa_lock = 1;
11861198
addr_string = inet_ntoa(sin->sin_addr);
11871199
inet_ntoa_lock = 0;
1188-
1200+
#endif
11891201
ZEND_TRY_ASSIGN_REF_STRING(arg2, addr_string);
11901202

11911203
if (arg3 != NULL) {
@@ -1517,12 +1529,14 @@ PHP_FUNCTION(socket_recvfrom)
15171529
struct sockaddr_in sin;
15181530
#if HAVE_IPV6
15191531
struct sockaddr_in6 sin6;
1520-
char addr6[INET6_ADDRSTRLEN];
1532+
#endif
1533+
#ifdef HAVE_INET_NTOP
1534+
char addrbuf[INET6_ADDRSTRLEN];
15211535
#endif
15221536
socklen_t slen;
15231537
int retval;
15241538
zend_long arg3, arg4;
1525-
char *address;
1539+
const char *address;
15261540
zend_string *recv_buf;
15271541

15281542
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)
15801594
ZSTR_LEN(recv_buf) = retval;
15811595
ZSTR_VAL(recv_buf)[ZSTR_LEN(recv_buf)] = '\0';
15821596

1597+
#ifdef HAVE_INET_NTOP
1598+
address = inet_ntop(AF_INET, &sin.sin_addr, addrbuf, sizeof(addrbuf));
1599+
#else
15831600
address = inet_ntoa(sin.sin_addr);
1601+
#endif
15841602

15851603
ZEND_TRY_ASSIGN_REF_NEW_STR(arg2, recv_buf);
15861604
ZEND_TRY_ASSIGN_REF_STRING(arg5, address ? address : "0.0.0.0");
@@ -1607,11 +1625,11 @@ PHP_FUNCTION(socket_recvfrom)
16071625
ZSTR_LEN(recv_buf) = retval;
16081626
ZSTR_VAL(recv_buf)[ZSTR_LEN(recv_buf)] = '\0';
16091627

1610-
memset(addr6, 0, INET6_ADDRSTRLEN);
1611-
inet_ntop(AF_INET6, &sin6.sin6_addr, addr6, INET6_ADDRSTRLEN);
1628+
memset(addrbuf, 0, INET6_ADDRSTRLEN);
1629+
inet_ntop(AF_INET6, &sin6.sin6_addr, addrbuf, sizeof(addrbuf));
16121630

16131631
ZEND_TRY_ASSIGN_REF_NEW_STR(arg2, recv_buf);
1614-
ZEND_TRY_ASSIGN_REF_STRING(arg5, addr6[0] ? addr6 : "::");
1632+
ZEND_TRY_ASSIGN_REF_STRING(arg5, addrbuf[0] ? addrbuf : "::");
16151633
ZEND_TRY_ASSIGN_REF_LONG(arg6, ntohs(sin6.sin6_port));
16161634
break;
16171635
#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)