Skip to content

Commit f830fa9

Browse files
authored
Sockets refactoring 2026 (#21365)
ext/sockets: internal refactorings. - remove redundant memsets and faster socket unix path copy. - simplify php_open_listen_sock. - use INADDR_ANY directly instead of resolving via gethostbyname. - remove redundant memsets in conversions.
1 parent f2d96c8 commit f830fa9

File tree

3 files changed

+6
-21
lines changed

3 files changed

+6
-21
lines changed

ext/sockets/conversions.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,6 @@ static void to_zval_read_sin_addr(const char *data, zval *zv, res_context *ctx)
571571
const struct in_addr *addr = (const struct in_addr *)data;
572572
socklen_t size = INET_ADDRSTRLEN;
573573
zend_string *str = zend_string_alloc(size - 1, 0);
574-
memset(ZSTR_VAL(str), '\0', size);
575574

576575
ZVAL_NEW_STR(zv, str);
577576

@@ -581,7 +580,7 @@ static void to_zval_read_sin_addr(const char *data, zval *zv, res_context *ctx)
581580
return;
582581
}
583582

584-
Z_STRLEN_P(zv) = strlen(Z_STRVAL_P(zv));
583+
Z_STR_P(zv) = zend_string_truncate(Z_STR_P(zv), strlen(Z_STRVAL_P(zv)), 0);
585584
}
586585
static const field_descriptor descriptors_sockaddr_in[] = {
587586
{"family", sizeof("family"), false, offsetof(struct sockaddr_in, sin_family), from_zval_write_sa_family, to_zval_read_sa_family},
@@ -622,8 +621,6 @@ static void to_zval_read_sin6_addr(const char *data, zval *zv, res_context *ctx)
622621
socklen_t size = INET6_ADDRSTRLEN;
623622
zend_string *str = zend_string_alloc(size - 1, 0);
624623

625-
memset(ZSTR_VAL(str), '\0', size);
626-
627624
ZVAL_NEW_STR(zv, str);
628625

629626
if (inet_ntop(AF_INET6, addr, Z_STRVAL_P(zv), size) == NULL) {
@@ -632,7 +629,7 @@ static void to_zval_read_sin6_addr(const char *data, zval *zv, res_context *ctx)
632629
return;
633630
}
634631

635-
Z_STRLEN_P(zv) = strlen(Z_STRVAL_P(zv));
632+
Z_STR_P(zv) = zend_string_truncate(Z_STR_P(zv), strlen(Z_STRVAL_P(zv)), 0);
636633
}
637634
static const field_descriptor descriptors_sockaddr_in6[] = {
638635
{"family", sizeof("family"), false, offsetof(struct sockaddr_in6, sin6_family), from_zval_write_sa_family, to_zval_read_sa_family},

ext/sockets/sockets.c

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -244,18 +244,9 @@ ZEND_GET_MODULE(sockets)
244244
static bool php_open_listen_sock(php_socket *sock, unsigned short port, int backlog) /* {{{ */
245245
{
246246
struct sockaddr_in la = {0};
247-
struct hostent *hp;
248247

249-
#ifndef PHP_WIN32
250-
if ((hp = php_network_gethostbyname("0.0.0.0")) == NULL) {
251-
#else
252-
if ((hp = php_network_gethostbyname("localhost")) == NULL) {
253-
#endif
254-
return false;
255-
}
256-
257-
memcpy((char *) &la.sin_addr, hp->h_addr, hp->h_length);
258-
la.sin_family = hp->h_addrtype;
248+
la.sin_addr.s_addr = htonl(INADDR_ANY);
249+
la.sin_family = AF_INET;
259250
la.sin_port = htons(port);
260251

261252
sock->bsd_socket = socket(PF_INET, SOCK_STREAM, 0);
@@ -1249,8 +1240,6 @@ PHP_FUNCTION(socket_connect)
12491240
RETURN_THROWS();
12501241
}
12511242

1252-
memset(&sin6, 0, sizeof(struct sockaddr_in6));
1253-
12541243
sin6.sin6_family = AF_INET6;
12551244
sin6.sin6_port = htons((unsigned short int)port);
12561245

@@ -1629,7 +1618,6 @@ PHP_FUNCTION(socket_recvfrom)
16291618
ZSTR_LEN(recv_buf) = retval;
16301619
ZSTR_VAL(recv_buf)[ZSTR_LEN(recv_buf)] = '\0';
16311620

1632-
memset(addrbuf, 0, INET6_ADDRSTRLEN);
16331621
inet_ntop(AF_INET6, &sin6.sin6_addr, addrbuf, sizeof(addrbuf));
16341622

16351623
ZEND_TRY_ASSIGN_REF_NEW_STR(arg2, recv_buf);
@@ -1732,7 +1720,7 @@ PHP_FUNCTION(socket_sendto)
17321720
}
17331721

17341722
s_un.sun_family = AF_UNIX;
1735-
snprintf(s_un.sun_path, sizeof(s_un.sun_path), "%s", ZSTR_VAL(addr));
1723+
memcpy(s_un.sun_path, ZSTR_VAL(addr), ZSTR_LEN(addr) + 1);
17361724

17371725
retval = sendto(php_sock->bsd_socket, buf, ((size_t)len > buf_len) ? buf_len : (size_t)len, flags, (struct sockaddr *) &s_un, SUN_LEN(&s_un));
17381726
break;

ext/sockets/tests/socket_create_listen-win32.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ socket_getsockname($sock, $addr, $port);
1515
var_dump($addr, $port);
1616
?>
1717
--EXPECT--
18-
string(9) "127.0.0.1"
18+
string(7) "0.0.0.0"
1919
int(31338)
2020
--CREDITS--
2121
Till Klampaeckel, till@php.net

0 commit comments

Comments
 (0)