Skip to content

Commit

Permalink
ext/sockets: inet family conversions internal changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
devnexen committed Dec 29, 2024
1 parent 9eb2284 commit ddcf16a
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 35 deletions.
8 changes: 2 additions & 6 deletions ext/sockets/conversions.c
Original file line number Diff line number Diff line change
Expand Up @@ -550,13 +550,11 @@ static void to_zval_read_uid_t(const char *data, zval *zv, res_context *ctx)
/* CONVERSIONS for sockaddr */
static void from_zval_write_sin_addr(const zval *zaddr_str, char *inaddr, ser_context *ctx)
{
int res;
struct sockaddr_in saddr = {0};
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);
if (res) {
if (php_set_inet_addr(&saddr, ZSTR_VAL(addr_str), ctx->sock) == SUCCESS) {
memcpy(inaddr, &saddr.sin_addr, sizeof saddr.sin_addr);
} else {
/* error already emitted, but let's emit another more relevant */
Expand Down Expand Up @@ -600,13 +598,11 @@ static void to_zval_read_sockaddr_in(const char *data, zval *zv, res_context *ct
#ifdef HAVE_IPV6
static void from_zval_write_sin6_addr(const zval *zaddr_str, char *addr6, ser_context *ctx)
{
int res;
struct sockaddr_in6 saddr6 = {0};
zend_string *addr_str, *tmp_addr_str;

addr_str = zval_get_tmp_string((zval *) zaddr_str, &tmp_addr_str);
res = php_set_inet6_addr(&saddr6, ZSTR_VAL(addr_str), ctx->sock);
if (res) {
if (php_set_inet6_addr(&saddr6, ZSTR_VAL(addr_str), ctx->sock) == SUCCESS) {
memcpy(addr6, &saddr6.sin6_addr, sizeof saddr6.sin6_addr);
} else {
/* error already emitted, but let's emit another more relevant */
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);
if (!php_set_inet46_addr(ss, ss_len, ZSTR_VAL(str), sock)) {
if (php_set_inet46_addr(ss, ss_len, ZSTR_VAL(str), sock) == FAILURE) {
zend_tmp_string_release(tmp_str);
return FAILURE;
}
Expand Down
45 changes: 26 additions & 19 deletions ext/sockets/sockaddr_conv.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ 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) /* {{{ */
zend_result php_set_inet6_addr(struct sockaddr_in6 *sin6, char *string, php_socket *php_sock) /* {{{ */
{
struct in6_addr tmp;
#ifdef HAVE_GETADDRINFO
Expand Down Expand Up @@ -41,12 +41,12 @@ int php_set_inet6_addr(struct sockaddr_in6 *sin6, char *string, php_socket *php_
#else
PHP_SOCKET_ERROR(php_sock, "Host lookup failed", (-10000 - h_errno));
#endif
return 0;
return FAILURE;
}
if (addrinfo->ai_family != PF_INET6 || addrinfo->ai_addrlen != sizeof(struct sockaddr_in6)) {
php_error_docref(NULL, E_WARNING, "Host lookup failed: Non AF_INET6 domain returned on AF_INET6 socket");
freeaddrinfo(addrinfo);
return 0;
return FAILURE;
}

memcpy(&(sin6->sin6_addr.s6_addr), ((struct sockaddr_in6*)(addrinfo->ai_addr))->sin6_addr.s6_addr, sizeof(struct in6_addr));
Expand All @@ -55,36 +55,43 @@ int php_set_inet6_addr(struct sockaddr_in6 *sin6, char *string, php_socket *php_
#else
/* No IPv6 specific hostname resolution is available on this system? */
php_error_docref(NULL, E_WARNING, "Host lookup failed: getaddrinfo() not available on this system");
return 0;
return FAILURE;
#endif

}

if (scope) {
zend_long lval = 0;
double dval = 0;
unsigned scope_id = 0;

uint32_t scope_id = 0;
scope++;

if (*scope == '\0') {
zend_value_error("scope cannot be empty");
return FAILURE;
}


if (IS_LONG == is_numeric_string(scope, strlen(scope), &lval, &dval, 0)) {
if (lval > 0 && (zend_ulong)lval <= UINT_MAX) {
scope_id = lval;
if (lval <= 0 || (zend_ulong)lval > UINT_MAX) {
zend_value_error("scope must be between 1 and %u", UINT_MAX);
return FAILURE;
}
scope_id = lval;
} else {
php_string_to_if_index(scope, &scope_id);
}

sin6->sin6_scope_id = scope_id;
}

return 1;
return SUCCESS;
}
/* }}} */
#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) /* {{{ */
zend_result php_set_inet_addr(struct sockaddr_in *sin, char *string, php_socket *php_sock) /* {{{ */
{
struct in_addr tmp;
struct hostent *host_entry;
Expand All @@ -99,46 +106,46 @@ int php_set_inet_addr(struct sockaddr_in *sin, char *string, php_socket *php_soc
#else
PHP_SOCKET_ERROR(php_sock, "Host lookup failed", (-10000 - h_errno));
#endif
return 0;
return FAILURE;
}
if (host_entry->h_addrtype != AF_INET) {
php_error_docref(NULL, E_WARNING, "Host lookup failed: Non AF_INET domain returned on AF_INET socket");
return 0;
return FAILURE;
}
memcpy(&(sin->sin_addr.s_addr), host_entry->h_addr_list[0], host_entry->h_length);
}

return 1;
return SUCCESS;
}
/* }}} */

/* 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) /* {{{ */
zend_result php_set_inet46_addr(php_sockaddr_storage *ss, socklen_t *ss_len, char *string, php_socket *php_sock) /* {{{ */
{
if (php_sock->type == AF_INET) {
struct sockaddr_in t = {0};
if (php_set_inet_addr(&t, string, php_sock)) {
if (php_set_inet_addr(&t, string, php_sock) == SUCCESS) {
memcpy(ss, &t, sizeof t);
ss->ss_family = AF_INET;
*ss_len = sizeof(t);
return 1;
return SUCCESS;
}
}
#ifdef HAVE_IPV6
else if (php_sock->type == AF_INET6) {
struct sockaddr_in6 t = {0};
if (php_set_inet6_addr(&t, string, php_sock)) {
if (php_set_inet6_addr(&t, string, php_sock) == SUCCESS) {
memcpy(ss, &t, sizeof t);
ss->ss_family = AF_INET6;
*ss_len = sizeof(t);
return 1;
return SUCCESS;
}
}
#endif
else {
php_error_docref(NULL, E_WARNING,
"IP address used in the context of an unexpected type of socket");
}
return 0;
return FAILURE;
}
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);
zend_result php_set_inet6_addr(struct sockaddr_in6 *sin6, char *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);
zend_result php_set_inet_addr(struct sockaddr_in *sin, char *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);
zend_result php_set_inet46_addr(php_sockaddr_storage *ss, socklen_t *ss_len, char *string, php_socket *php_sock);

#endif
12 changes: 6 additions & 6 deletions ext/sockets/sockets.c
Original file line number Diff line number Diff line change
Expand Up @@ -1192,7 +1192,7 @@ PHP_FUNCTION(socket_connect)
sin6.sin6_family = AF_INET6;
sin6.sin6_port = htons((unsigned short int)port);

if (! php_set_inet6_addr(&sin6, addr, php_sock)) {
if (php_set_inet6_addr(&sin6, addr, php_sock) == FAILURE) {
RETURN_FALSE;
}

Expand All @@ -1211,7 +1211,7 @@ PHP_FUNCTION(socket_connect)
sin.sin_family = AF_INET;
sin.sin_port = htons((unsigned short int)port);

if (! php_set_inet_addr(&sin, addr, php_sock)) {
if (php_set_inet_addr(&sin, addr, php_sock) == FAILURE) {
RETURN_FALSE;
}

Expand Down Expand Up @@ -1313,7 +1313,7 @@ PHP_FUNCTION(socket_bind)
sa->sin_family = AF_INET;
sa->sin_port = htons((unsigned short) port);

if (! php_set_inet_addr(sa, addr, php_sock)) {
if (php_set_inet_addr(sa, addr, php_sock) == FAILURE) {
RETURN_FALSE;
}

Expand All @@ -1328,7 +1328,7 @@ PHP_FUNCTION(socket_bind)
sa->sin6_family = AF_INET6;
sa->sin6_port = htons((unsigned short) port);

if (! php_set_inet6_addr(sa, addr, php_sock)) {
if (php_set_inet6_addr(sa, addr, php_sock) == FAILURE) {
RETURN_FALSE;
}

Expand Down Expand Up @@ -1605,7 +1605,7 @@ PHP_FUNCTION(socket_sendto)
sin.sin_family = AF_INET;
sin.sin_port = htons((unsigned short) port);

if (! php_set_inet_addr(&sin, addr, php_sock)) {
if (php_set_inet_addr(&sin, addr, php_sock) == FAILURE) {
RETURN_FALSE;
}

Expand All @@ -1622,7 +1622,7 @@ PHP_FUNCTION(socket_sendto)
sin6.sin6_family = AF_INET6;
sin6.sin6_port = htons((unsigned short) port);

if (! php_set_inet6_addr(&sin6, addr, php_sock)) {
if (php_set_inet6_addr(&sin6, addr, php_sock) == FAILURE) {
RETURN_FALSE;
}

Expand Down

0 comments on commit ddcf16a

Please sign in to comment.