Skip to content

ext/sockets: Merge setting mcast option code for IPv4 and 6 #18724

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
105 changes: 35 additions & 70 deletions ext/sockets/multicast.c
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,12 @@ int php_do_setsockopt_ip_mcast(php_socket *php_sock,
struct in_addr if_addr;
void *opt_ptr;
socklen_t optlen;
unsigned char ipv4_mcast_ttl_lback;
int ip_mcast_loop_back;
int ip_mcast_hops;
int retval;
#if IP_MULTICAST_LOOP != IPV6_MULTICAST_LOOP
unsigned char ip_mcast_ttl;
#endif

switch (optname) {
case PHP_MCAST_JOIN_GROUP:
Expand All @@ -274,101 +278,62 @@ int php_do_setsockopt_ip_mcast(php_socket *php_sock,
case PHP_MCAST_JOIN_SOURCE_GROUP:
case PHP_MCAST_LEAVE_SOURCE_GROUP:
#endif
if (php_do_mcast_opt(php_sock, level, optname, arg4) == FAILURE) {
return FAILURE;
} else {
return SUCCESS;
}
return php_do_mcast_opt(php_sock, level, optname, arg4);

case IP_MULTICAST_IF:
/* On MacOS and Windows those have the same values */
#if IP_MULTICAST_LOOP != IPV6_MULTICAST_LOOP
case IPV6_MULTICAST_IF:
Copy link
Member

Choose a reason for hiding this comment

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

yeah ... I m afraid you just can t do this that way. on mac those two has same values.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah and also Windows... Probably doable with some #if preprocessor directives tho.

#endif
if (php_get_if_index_from_zval(arg4, &if_index) == FAILURE) {
return FAILURE;
}

if (php_if_index_to_addr4(if_index, php_sock, &if_addr) == FAILURE) {
return FAILURE;
if (optname == IP_MULTICAST_IF) {
if (php_if_index_to_addr4(if_index, php_sock, &if_addr) == FAILURE) {
return FAILURE;
}
opt_ptr = &if_addr;
optlen = sizeof(if_addr);
} else {
opt_ptr = &if_index;
optlen = sizeof(if_index);
}
opt_ptr = &if_addr;
optlen = sizeof(if_addr);
goto dosockopt;

case IP_MULTICAST_LOOP:
ipv4_mcast_ttl_lback = (unsigned char) zval_is_true(arg4);
goto ipv4_loop_ttl;
/* On MacOS and Windows those have the same values */
#if IP_MULTICAST_LOOP != IPV6_MULTICAST_LOOP
case IPV6_MULTICAST_LOOP:
#endif
ip_mcast_loop_back = zval_is_true(arg4);
opt_ptr = &ip_mcast_loop_back;
optlen = sizeof(ip_mcast_loop_back);
goto dosockopt;

/* On MacOS and Windows those have the same values */
#if IP_MULTICAST_TTL != IPV6_MULTICAST_HOPS
case IP_MULTICAST_TTL:
convert_to_long(arg4);
if (Z_LVAL_P(arg4) < 0L || Z_LVAL_P(arg4) > 255L) {
zend_argument_value_error(4, "must be between 0 and 255");
return FAILURE;
}
ipv4_mcast_ttl_lback = (unsigned char) Z_LVAL_P(arg4);
ipv4_loop_ttl:
opt_ptr = &ipv4_mcast_ttl_lback;
optlen = sizeof(ipv4_mcast_ttl_lback);
ip_mcast_ttl = (unsigned char) Z_LVAL_P(arg4);
opt_ptr = &ip_mcast_ttl;
optlen = sizeof(ip_mcast_ttl);
goto dosockopt;
}

return 1;

dosockopt:
retval = setsockopt(php_sock->bsd_socket, level, optname, opt_ptr, optlen);
if (retval != 0) {
PHP_SOCKET_ERROR(php_sock, "Unable to set socket option", errno);
return FAILURE;
}

return SUCCESS;
}

int php_do_setsockopt_ipv6_mcast(php_socket *php_sock,
int level,
int optname,
zval *arg4)
{
unsigned int if_index;
void *opt_ptr;
socklen_t optlen;
int ov;
int retval;

switch (optname) {
case PHP_MCAST_JOIN_GROUP:
case PHP_MCAST_LEAVE_GROUP:
#ifdef HAS_MCAST_EXT
case PHP_MCAST_BLOCK_SOURCE:
case PHP_MCAST_UNBLOCK_SOURCE:
case PHP_MCAST_JOIN_SOURCE_GROUP:
case PHP_MCAST_LEAVE_SOURCE_GROUP:
#endif
if (php_do_mcast_opt(php_sock, level, optname, arg4) == FAILURE) {
return FAILURE;
} else {
return SUCCESS;
}

case IPV6_MULTICAST_IF:
if (php_get_if_index_from_zval(arg4, &if_index) == FAILURE) {
return FAILURE;
}

opt_ptr = &if_index;
optlen = sizeof(if_index);
goto dosockopt;

case IPV6_MULTICAST_LOOP:
ov = (int) zval_is_true(arg4);
goto ipv6_loop_hops;
case IPV6_MULTICAST_HOPS:
convert_to_long(arg4);
if (Z_LVAL_P(arg4) < -1L || Z_LVAL_P(arg4) > 255L) {
zend_argument_value_error(4, "must be between -1 and 255");
return FAILURE;
}
ov = (int) Z_LVAL_P(arg4);
ipv6_loop_hops:
opt_ptr = &ov;
optlen = sizeof(ov);
ip_mcast_hops = (int) Z_LVAL_P(arg4);
opt_ptr = &ip_mcast_hops;
optlen = sizeof(ip_mcast_hops);
goto dosockopt;
}

Expand Down
5 changes: 0 additions & 5 deletions ext/sockets/multicast.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,6 @@ int php_do_setsockopt_ip_mcast(php_socket *php_sock,
int optname,
zval *arg4);

int php_do_setsockopt_ipv6_mcast(php_socket *php_sock,
int level,
int optname,
zval *arg4);

zend_result php_if_index_to_addr4(
unsigned if_index,
php_socket *php_sock,
Expand Down
30 changes: 13 additions & 17 deletions ext/sockets/sockets.c
Original file line number Diff line number Diff line change
Expand Up @@ -2031,28 +2031,24 @@ PHP_FUNCTION(socket_set_option)

set_errno(0);

#define HANDLE_SUBCALL(res) \
do { \
if (res == 1) { goto default_case; } \
else if (res == SUCCESS) { RETURN_TRUE; } \
else { RETURN_FALSE; } \
} while (0)


if (level == IPPROTO_IP) {
if (
level == IPPROTO_IP
#ifdef HAVE_IPV6
|| level == IPPROTO_IPV6
#endif
) {
int res = php_do_setsockopt_ip_mcast(php_sock, level, optname, arg4);
HANDLE_SUBCALL(res);
}

#ifdef HAVE_IPV6
else if (level == IPPROTO_IPV6) {
int res = php_do_setsockopt_ipv6_mcast(php_sock, level, optname, arg4);
if (res == 1) {
/* If option is unknown for IPv6 */
if (level == IPPROTO_IPV6 && res == 1) {
res = php_do_setsockopt_ipv6_rfc3542(php_sock, level, optname, arg4);
}
HANDLE_SUBCALL(res);
}
#endif
if (res == 1) {
goto default_case;
}
RETURN_BOOL(res == SUCCESS);
}

if (level == IPPROTO_TCP) {
switch (optname) {
Expand Down
Loading