Skip to content

ext/sockets: php_set_inet*_addr using zend_string instead. #17845

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions ext/sockets/conversions.c
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ static void from_zval_write_sin_addr(const zval *zaddr_str, char *inaddr, ser_co
zend_string *addr_str, *tmp_addr_str;

addr_str = zval_get_tmp_string((zval *) zaddr_str, &tmp_addr_str);
res = php_set_inet_addr(&saddr, ZSTR_VAL(addr_str), ctx->sock);
res = php_set_inet_addr(&saddr, addr_str, ctx->sock);
if (res) {
memcpy(inaddr, &saddr.sin_addr, sizeof saddr.sin_addr);
} else {
Expand Down Expand Up @@ -605,7 +605,7 @@ static void from_zval_write_sin6_addr(const zval *zaddr_str, char *addr6, ser_co
zend_string *addr_str, *tmp_addr_str;

addr_str = zval_get_tmp_string((zval *) zaddr_str, &tmp_addr_str);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto: try API.

res = php_set_inet6_addr(&saddr6, ZSTR_VAL(addr_str), ctx->sock);
res = php_set_inet6_addr(&saddr6, addr_str, ctx->sock);
if (res) {
memcpy(addr6, &saddr6.sin6_addr, sizeof saddr6.sin6_addr);
} else {
Expand Down
2 changes: 1 addition & 1 deletion ext/sockets/multicast.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ static zend_result php_get_address_from_array(const HashTable *ht, const char *k
return FAILURE;
}
str = zval_get_tmp_string(val, &tmp_str);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Follow-up PR: but this should use the try API.

if (!php_set_inet46_addr(ss, ss_len, ZSTR_VAL(str), sock)) {
if (!php_set_inet46_addr(ss, ss_len, str, sock)) {
zend_tmp_string_release(tmp_str);
return FAILURE;
}
Expand Down
16 changes: 8 additions & 8 deletions ext/sockets/sockaddr_conv.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ extern zend_result php_string_to_if_index(const char *val, unsigned *out);

#ifdef HAVE_IPV6
/* Sets addr by hostname, or by ip in string form (AF_INET6) */
int php_set_inet6_addr(struct sockaddr_in6 *sin6, char *string, php_socket *php_sock) /* {{{ */
int php_set_inet6_addr(struct sockaddr_in6 *sin6, zend_string *string, php_socket *php_sock) /* {{{ */
{
struct in6_addr tmp;
#ifdef HAVE_GETADDRINFO
struct addrinfo hints;
struct addrinfo *addrinfo = NULL;
#endif
char *scope = strchr(string, '%');
char *scope = strchr(ZSTR_VAL(string), '%');

if (inet_pton(AF_INET6, string, &tmp)) {
if (inet_pton(AF_INET6, ZSTR_VAL(string), &tmp)) {
memcpy(&(sin6->sin6_addr.s6_addr), &(tmp.s6_addr), sizeof(struct in6_addr));
} else {
#ifdef HAVE_GETADDRINFO
Expand All @@ -34,7 +34,7 @@ int php_set_inet6_addr(struct sockaddr_in6 *sin6, char *string, php_socket *php_
#else
hints.ai_flags = AI_ADDRCONFIG;
#endif
getaddrinfo(string, NULL, &hints, &addrinfo);
getaddrinfo(ZSTR_VAL(string), NULL, &hints, &addrinfo);
if (!addrinfo) {
#ifdef PHP_WIN32
PHP_SOCKET_ERROR(php_sock, "Host lookup failed", WSAGetLastError());
Expand Down Expand Up @@ -84,15 +84,15 @@ int php_set_inet6_addr(struct sockaddr_in6 *sin6, char *string, php_socket *php_
#endif

/* Sets addr by hostname, or by ip in string form (AF_INET) */
int php_set_inet_addr(struct sockaddr_in *sin, char *string, php_socket *php_sock) /* {{{ */
int php_set_inet_addr(struct sockaddr_in *sin, zend_string *string, php_socket *php_sock) /* {{{ */
{
struct in_addr tmp;
struct hostent *host_entry;

if (inet_pton(AF_INET, string, &tmp)) {
if (inet_pton(AF_INET, ZSTR_VAL(string), &tmp)) {
sin->sin_addr.s_addr = tmp.s_addr;
} else {
if (strlen(string) > MAXFQDNLEN || ! (host_entry = php_network_gethostbyname(string))) {
if (ZSTR_LEN(string) > MAXFQDNLEN || ! (host_entry = php_network_gethostbyname(ZSTR_VAL(string)))) {
/* Note: < -10000 indicates a host lookup error */
#ifdef PHP_WIN32
PHP_SOCKET_ERROR(php_sock, "Host lookup failed", WSAGetLastError());
Expand All @@ -114,7 +114,7 @@ int php_set_inet_addr(struct sockaddr_in *sin, char *string, php_socket *php_soc

/* Sets addr by hostname or by ip in string form (AF_INET or AF_INET6,
* depending on the socket) */
int php_set_inet46_addr(php_sockaddr_storage *ss, socklen_t *ss_len, char *string, php_socket *php_sock) /* {{{ */
int php_set_inet46_addr(php_sockaddr_storage *ss, socklen_t *ss_len, zend_string *string, php_socket *php_sock) /* {{{ */
{
if (php_sock->type == AF_INET) {
struct sockaddr_in t = {0};
Expand Down
6 changes: 3 additions & 3 deletions ext/sockets/sockaddr_conv.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@
* The IPv6 literal can be a IPv4 mapped address (like ::ffff:127.0.0.1).
* If the hostname yields no IPv6 addresses, a mapped IPv4 address may be returned (AI_V4MAPPED)
*/
int php_set_inet6_addr(struct sockaddr_in6 *sin6, char *string, php_socket *php_sock);
int php_set_inet6_addr(struct sockaddr_in6 *sin6, zend_string *string, php_socket *php_sock);

/*
* Convert an IPv4 literal or a hostname into a sockaddr_in.
*/
int php_set_inet_addr(struct sockaddr_in *sin, char *string, php_socket *php_sock);
int php_set_inet_addr(struct sockaddr_in *sin, zend_string *string, php_socket *php_sock);

/*
* Calls either php_set_inet6_addr() or php_set_inet_addr(), depending on the type of the socket.
*/
int php_set_inet46_addr(php_sockaddr_storage *ss, socklen_t *ss_len, char *string, php_socket *php_sock);
int php_set_inet46_addr(php_sockaddr_storage *ss, socklen_t *ss_len, zend_string *string, php_socket *php_sock);

#endif
33 changes: 16 additions & 17 deletions ext/sockets/sockets.c
Original file line number Diff line number Diff line change
Expand Up @@ -1204,15 +1204,14 @@ PHP_FUNCTION(socket_connect)
{
zval *resource_socket;
php_socket *php_sock;
char *addr;
zend_string *addr;
int retval;
size_t addr_len;
zend_long port;
bool port_is_null = 1;

ZEND_PARSE_PARAMETERS_START(2, 3)
Z_PARAM_OBJECT_OF_CLASS(resource_socket, socket_ce)
Z_PARAM_STRING(addr, addr_len)
Z_PARAM_STR(addr)
Z_PARAM_OPTIONAL
Z_PARAM_LONG_OR_NULL(port, port_is_null)
ZEND_PARSE_PARAMETERS_END();
Expand Down Expand Up @@ -1265,15 +1264,15 @@ PHP_FUNCTION(socket_connect)
case AF_UNIX: {
struct sockaddr_un s_un = {0};

if (addr_len >= sizeof(s_un.sun_path)) {
if (ZSTR_LEN(addr) >= sizeof(s_un.sun_path)) {
zend_argument_value_error(2, "must be less than %d", sizeof(s_un.sun_path));
RETURN_THROWS();
}

s_un.sun_family = AF_UNIX;
memcpy(&s_un.sun_path, addr, addr_len);
memcpy(&s_un.sun_path, ZSTR_VAL(addr), ZSTR_LEN(addr));
retval = connect(php_sock->bsd_socket, (struct sockaddr *) &s_un,
(socklen_t)(XtOffsetOf(struct sockaddr_un, sun_path) + addr_len));
(socklen_t)(XtOffsetOf(struct sockaddr_un, sun_path) + ZSTR_LEN(addr)));
break;
}

Expand Down Expand Up @@ -1316,14 +1315,13 @@ PHP_FUNCTION(socket_bind)
php_sockaddr_storage sa_storage = {0};
struct sockaddr *sock_type = (struct sockaddr*) &sa_storage;
php_socket *php_sock;
char *addr;
size_t addr_len;
zend_string *addr;
zend_long objint = 0;
zend_long retval = 0;

ZEND_PARSE_PARAMETERS_START(2, 3)
Z_PARAM_OBJECT_OF_CLASS(arg1, socket_ce)
Z_PARAM_STRING(addr, addr_len)
Z_PARAM_STR(addr)
Z_PARAM_OPTIONAL
Z_PARAM_LONG(objint)
ZEND_PARSE_PARAMETERS_END();
Expand All @@ -1343,14 +1341,14 @@ PHP_FUNCTION(socket_bind)

sa->sun_family = AF_UNIX;

if (addr_len >= sizeof(sa->sun_path)) {
if (ZSTR_LEN(addr) >= sizeof(sa->sun_path)) {
zend_argument_value_error(2, "must be less than %d", sizeof(sa->sun_path));
RETURN_THROWS();
}
memcpy(&sa->sun_path, addr, addr_len);
memcpy(&sa->sun_path, ZSTR_VAL(addr), ZSTR_LEN(addr));

retval = bind(php_sock->bsd_socket, (struct sockaddr *) sa,
offsetof(struct sockaddr_un, sun_path) + addr_len);
offsetof(struct sockaddr_un, sun_path) + ZSTR_LEN(addr));
break;
}

Expand Down Expand Up @@ -1395,7 +1393,7 @@ PHP_FUNCTION(socket_bind)
RETURN_THROWS();
}

sa->sll_ifindex = if_nametoindex(addr);
sa->sll_ifindex = if_nametoindex(ZSTR_VAL(addr));

retval = bind(php_sock->bsd_socket, sock_type, sizeof(struct sockaddr_ll));
break;
Expand Down Expand Up @@ -1666,17 +1664,18 @@ PHP_FUNCTION(socket_sendto)
//struct sockaddr_ll sll;
#endif
int retval;
size_t buf_len, addr_len;
size_t buf_len;
zend_long len, flags, port = 0;
bool port_is_null = 1;
char *buf, *addr;
char *buf;
zend_string *addr;

ZEND_PARSE_PARAMETERS_START(5, 6)
Z_PARAM_OBJECT_OF_CLASS(arg1, socket_ce)
Z_PARAM_STRING(buf, buf_len)
Z_PARAM_LONG(len)
Z_PARAM_LONG(flags)
Z_PARAM_STRING(addr, addr_len)
Z_PARAM_STR(addr)
Z_PARAM_OPTIONAL
Z_PARAM_LONG_OR_NULL(port, port_is_null)
ZEND_PARSE_PARAMETERS_END();
Expand All @@ -1699,7 +1698,7 @@ PHP_FUNCTION(socket_sendto)
case AF_UNIX:
memset(&s_un, 0, sizeof(s_un));
s_un.sun_family = AF_UNIX;
snprintf(s_un.sun_path, sizeof(s_un.sun_path), "%s", addr);
snprintf(s_un.sun_path, sizeof(s_un.sun_path), "%s", ZSTR_VAL(addr));

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;
Expand Down
Loading