mirror of
https://github.com/php/php-src.git
synced 2026-03-24 08:12:21 +01:00
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.
This commit is contained in:
@@ -571,7 +571,6 @@ static void to_zval_read_sin_addr(const char *data, zval *zv, res_context *ctx)
|
||||
const struct in_addr *addr = (const struct in_addr *)data;
|
||||
socklen_t size = INET_ADDRSTRLEN;
|
||||
zend_string *str = zend_string_alloc(size - 1, 0);
|
||||
memset(ZSTR_VAL(str), '\0', size);
|
||||
|
||||
ZVAL_NEW_STR(zv, str);
|
||||
|
||||
@@ -581,7 +580,7 @@ static void to_zval_read_sin_addr(const char *data, zval *zv, res_context *ctx)
|
||||
return;
|
||||
}
|
||||
|
||||
Z_STRLEN_P(zv) = strlen(Z_STRVAL_P(zv));
|
||||
Z_STR_P(zv) = zend_string_truncate(Z_STR_P(zv), strlen(Z_STRVAL_P(zv)), 0);
|
||||
}
|
||||
static const field_descriptor descriptors_sockaddr_in[] = {
|
||||
{"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)
|
||||
socklen_t size = INET6_ADDRSTRLEN;
|
||||
zend_string *str = zend_string_alloc(size - 1, 0);
|
||||
|
||||
memset(ZSTR_VAL(str), '\0', size);
|
||||
|
||||
ZVAL_NEW_STR(zv, str);
|
||||
|
||||
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)
|
||||
return;
|
||||
}
|
||||
|
||||
Z_STRLEN_P(zv) = strlen(Z_STRVAL_P(zv));
|
||||
Z_STR_P(zv) = zend_string_truncate(Z_STR_P(zv), strlen(Z_STRVAL_P(zv)), 0);
|
||||
}
|
||||
static const field_descriptor descriptors_sockaddr_in6[] = {
|
||||
{"family", sizeof("family"), false, offsetof(struct sockaddr_in6, sin6_family), from_zval_write_sa_family, to_zval_read_sa_family},
|
||||
|
||||
@@ -244,18 +244,9 @@ ZEND_GET_MODULE(sockets)
|
||||
static bool php_open_listen_sock(php_socket *sock, unsigned short port, int backlog) /* {{{ */
|
||||
{
|
||||
struct sockaddr_in la = {0};
|
||||
struct hostent *hp;
|
||||
|
||||
#ifndef PHP_WIN32
|
||||
if ((hp = php_network_gethostbyname("0.0.0.0")) == NULL) {
|
||||
#else
|
||||
if ((hp = php_network_gethostbyname("localhost")) == NULL) {
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
|
||||
memcpy((char *) &la.sin_addr, hp->h_addr, hp->h_length);
|
||||
la.sin_family = hp->h_addrtype;
|
||||
la.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||
la.sin_family = AF_INET;
|
||||
la.sin_port = htons(port);
|
||||
|
||||
sock->bsd_socket = socket(PF_INET, SOCK_STREAM, 0);
|
||||
@@ -1249,8 +1240,6 @@ PHP_FUNCTION(socket_connect)
|
||||
RETURN_THROWS();
|
||||
}
|
||||
|
||||
memset(&sin6, 0, sizeof(struct sockaddr_in6));
|
||||
|
||||
sin6.sin6_family = AF_INET6;
|
||||
sin6.sin6_port = htons((unsigned short int)port);
|
||||
|
||||
@@ -1629,7 +1618,6 @@ PHP_FUNCTION(socket_recvfrom)
|
||||
ZSTR_LEN(recv_buf) = retval;
|
||||
ZSTR_VAL(recv_buf)[ZSTR_LEN(recv_buf)] = '\0';
|
||||
|
||||
memset(addrbuf, 0, INET6_ADDRSTRLEN);
|
||||
inet_ntop(AF_INET6, &sin6.sin6_addr, addrbuf, sizeof(addrbuf));
|
||||
|
||||
ZEND_TRY_ASSIGN_REF_NEW_STR(arg2, recv_buf);
|
||||
@@ -1732,7 +1720,7 @@ PHP_FUNCTION(socket_sendto)
|
||||
}
|
||||
|
||||
s_un.sun_family = AF_UNIX;
|
||||
snprintf(s_un.sun_path, sizeof(s_un.sun_path), "%s", ZSTR_VAL(addr));
|
||||
memcpy(s_un.sun_path, ZSTR_VAL(addr), ZSTR_LEN(addr) + 1);
|
||||
|
||||
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));
|
||||
break;
|
||||
|
||||
@@ -15,7 +15,7 @@ socket_getsockname($sock, $addr, $port);
|
||||
var_dump($addr, $port);
|
||||
?>
|
||||
--EXPECT--
|
||||
string(9) "127.0.0.1"
|
||||
string(7) "0.0.0.0"
|
||||
int(31338)
|
||||
--CREDITS--
|
||||
Till Klampaeckel, till@php.net
|
||||
|
||||
Reference in New Issue
Block a user