From bc9c089f3a4c0034e613fc57c1c429d877dde157 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Gustavo=20Andr=C3=A9=20dos=20Santos=20Lopes?= Date: Sat, 11 Jun 2011 20:20:50 +0000 Subject: [PATCH] - Fixed ext/sockets build on Mac OS X (hopefully). - Improvements in the multicast tests. - Very light refactoring in sockets.c. --- ext/sockets/multicast.c | 18 ++++---- ext/sockets/multicast.h | 12 ++++++ ext/sockets/sockets.c | 41 ++++++++++++------- ext/sockets/tests/mcast_helpers.php.inc | 8 ++++ ext/sockets/tests/mcast_ipv4_recv.phpt | 9 +++- ext/sockets/tests/mcast_ipv6_recv.phpt | 11 ++++- .../tests/mcast_ipv6_recv_limited.phpt | 25 ++++++----- 7 files changed, 88 insertions(+), 36 deletions(-) create mode 100644 ext/sockets/tests/mcast_helpers.php.inc diff --git a/ext/sockets/multicast.c b/ext/sockets/multicast.c index 9a1ecd5371..49e9c5cf73 100644 --- a/ext/sockets/multicast.c +++ b/ext/sockets/multicast.c @@ -53,11 +53,6 @@ #include "multicast.h" #include "main/php_network.h" -#if defined(MCAST_JOIN_GROUP) && 1 &&\ - (!defined(PHP_WIN32) || (_WIN32_WINNT >= 0x600 && SOCKETS_ENABLE_VISTA_API)) -#define RFC3678_API 1 -#endif - enum source_op { JOIN_SOURCE, @@ -67,10 +62,13 @@ enum source_op { }; static int _php_mcast_join_leave(php_socket *sock, int level, struct sockaddr *group, socklen_t group_len, unsigned int if_index, int join TSRMLS_DC); +#ifdef HAS_MCAST_EXT static int _php_mcast_source_op(php_socket *sock, int level, struct sockaddr *group, socklen_t group_len, struct sockaddr *source, socklen_t source_len, unsigned int if_index, enum source_op sop TSRMLS_DC); -#if RFC3678_API +#endif + +#ifdef RFC3678_API static int _php_source_op_to_rfc3678_op(enum source_op sop); -#else +#elif HAS_MCAST_EXT static const char *_php_source_op_to_string(enum source_op sop); static int _php_source_op_to_ipv4_op(enum source_op sop); #endif @@ -95,6 +93,7 @@ int php_mcast_leave( return _php_mcast_join_leave(sock, level, group, group_len, if_index, 0 TSRMLS_CC); } +#ifdef HAS_MCAST_EXT int php_mcast_join_source( php_socket *sock, int level, @@ -142,6 +141,8 @@ int php_mcast_unblock_source( { return _php_mcast_source_op(sock, level, group, group_len, source, source_len, if_index, UNBLOCK_SOURCE TSRMLS_CC); } +#endif /* HAS_MCAST_EXT */ + static int _php_mcast_join_leave( php_socket *sock, @@ -204,6 +205,7 @@ static int _php_mcast_join_leave( #endif } +#ifdef HAS_MCAST_EXT static int _php_mcast_source_op( php_socket *sock, int level, @@ -318,6 +320,8 @@ static int _php_source_op_to_ipv4_op(enum source_op sop) } #endif +#endif /* HAS_MCAST_EXT */ + #if PHP_WIN32 int php_if_index_to_addr4(unsigned if_index, php_socket *php_sock, struct in_addr *out_addr TSRMLS_DC) { diff --git a/ext/sockets/multicast.h b/ext/sockets/multicast.h index 5b63cf0381..0b1a1422e7 100644 --- a/ext/sockets/multicast.h +++ b/ext/sockets/multicast.h @@ -18,6 +18,16 @@ /* $Id$ */ +#if defined(MCAST_JOIN_GROUP) && \ + (!defined(PHP_WIN32) || (_WIN32_WINNT >= 0x600 && SOCKETS_ENABLE_VISTA_API)) +#define RFC3678_API 1 +/* has block/unblock and source membership, in this case for both IPv4 and IPv6 */ +#define HAS_MCAST_EXT 1 +#elif defined(IP_ADD_SOURCE_MEMBERSHIP) +/* has block/unblock and source membership, but only for IPv4 */ +#define HAS_MCAST_EXT 1 +#endif + int php_if_index_to_addr4( unsigned if_index, php_socket *php_sock, @@ -42,6 +52,7 @@ int php_mcast_leave( socklen_t group_len, unsigned int if_index TSRMLS_DC); +#ifdef HAS_MCAST_EXT int php_mcast_join_source( php_socket *sock, int level, @@ -77,3 +88,4 @@ int php_mcast_unblock_source( struct sockaddr *source, socklen_t source_len, unsigned int if_index TSRMLS_DC); +#endif diff --git a/ext/sockets/sockets.c b/ext/sockets/sockets.c index 2af92005b8..456e89d087 100644 --- a/ext/sockets/sockets.c +++ b/ext/sockets/sockets.c @@ -806,21 +806,25 @@ PHP_MINIT_FUNCTION(sockets) REGISTER_LONG_CONSTANT("PHP_NORMAL_READ", PHP_NORMAL_READ, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("PHP_BINARY_READ", PHP_BINARY_READ, CONST_CS | CONST_PERSISTENT); -#ifndef MCAST_JOIN_GROUP +#ifndef RFC3678_API #define MCAST_JOIN_GROUP IP_ADD_MEMBERSHIP #define MCAST_LEAVE_GROUP IP_DROP_MEMBERSHIP +#ifdef HAS_MCAST_EXT #define MCAST_BLOCK_SOURCE IP_BLOCK_SOURCE #define MCAST_UNBLOCK_SOURCE IP_UNBLOCK_SOURCE #define MCAST_JOIN_SOURCE_GROUP IP_ADD_SOURCE_MEMBERSHIP #define MCAST_LEAVE_SOURCE_GROUP IP_DROP_SOURCE_MEMBERSHIP +#endif #endif REGISTER_LONG_CONSTANT("MCAST_JOIN_GROUP", MCAST_JOIN_GROUP, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("MCAST_LEAVE_GROUP", MCAST_LEAVE_GROUP, CONST_CS | CONST_PERSISTENT); +#ifdef HAS_MCAST_EXT REGISTER_LONG_CONSTANT("MCAST_BLOCK_SOURCE", MCAST_BLOCK_SOURCE, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("MCAST_UNBLOCK_SOURCE", MCAST_UNBLOCK_SOURCE, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("MCAST_JOIN_SOURCE_GROUP", MCAST_JOIN_SOURCE_GROUP, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("MCAST_LEAVE_SOURCE_GROUP", MCAST_LEAVE_SOURCE_GROUP, CONST_CS | CONST_PERSISTENT); +#endif REGISTER_LONG_CONSTANT("IP_MULTICAST_IF", IP_MULTICAST_IF, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("IP_MULTICAST_TTL", IP_MULTICAST_TTL, CONST_CS | CONST_PERSISTENT); @@ -1485,11 +1489,6 @@ PHP_FUNCTION(socket_connect) { zval *arg1; php_socket *php_sock; - struct sockaddr_in sin; -#if HAVE_IPV6 - struct sockaddr_in6 sin6; -#endif - struct sockaddr_un s_un; char *addr; int retval, addr_len; long port = 0; @@ -1503,7 +1502,9 @@ PHP_FUNCTION(socket_connect) switch(php_sock->type) { #if HAVE_IPV6 - case AF_INET6: + case AF_INET6: { + struct sockaddr_in6 sin6 = {0}; + if (argc != 3) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Socket of type AF_INET6 requires 3 arguments"); RETURN_FALSE; @@ -1520,15 +1521,16 @@ PHP_FUNCTION(socket_connect) retval = connect(php_sock->bsd_socket, (struct sockaddr *)&sin6, sizeof(struct sockaddr_in6)); break; + } #endif - case AF_INET: + case AF_INET: { + struct sockaddr_in sin = {0}; + if (argc != 3) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Socket of type AF_INET requires 3 arguments"); RETURN_FALSE; } - memset(&sin, 0, sizeof(struct sockaddr_in)); - sin.sin_family = AF_INET; sin.sin_port = htons((unsigned short int)port); @@ -1538,19 +1540,22 @@ PHP_FUNCTION(socket_connect) retval = connect(php_sock->bsd_socket, (struct sockaddr *)&sin, sizeof(struct sockaddr_in)); break; + } - case AF_UNIX: + case AF_UNIX: { + struct sockaddr_un s_un = {0}; + if (addr_len >= sizeof(s_un.sun_path)) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Path too long"); RETURN_FALSE; } - - memset(&s_un, 0, sizeof(struct sockaddr_un)); s_un.sun_family = AF_UNIX; memcpy(&s_un.sun_path, addr, addr_len); - retval = connect(php_sock->bsd_socket, (struct sockaddr *) &s_un, (socklen_t) XtOffsetOf(struct sockaddr_un, sun_path) + addr_len); + retval = connect(php_sock->bsd_socket, (struct sockaddr *) &s_un, + (socklen_t)(XtOffsetOf(struct sockaddr_un, sun_path) + addr_len)); break; + } default: php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unsupported socket type %d", php_sock->type); @@ -2034,8 +2039,10 @@ static int php_do_mcast_opt(php_socket *php_sock, int level, int optname, zval * int retval; int (*mcast_req_fun)(php_socket *, int, struct sockaddr *, socklen_t, unsigned TSRMLS_DC); +#ifdef HAS_MCAST_EXT int (*mcast_sreq_fun)(php_socket *, int, struct sockaddr *, socklen_t, struct sockaddr *, socklen_t, unsigned TSRMLS_DC); +#endif switch (optname) { case MCAST_JOIN_GROUP: @@ -2065,6 +2072,7 @@ mcast_req_fun: break; } +#ifdef HAS_MCAST_EXT case MCAST_BLOCK_SOURCE: mcast_sreq_fun = &php_mcast_block_source; goto mcast_sreq_fun; @@ -2103,6 +2111,7 @@ mcast_req_fun: glen, (struct sockaddr*)&source, slen, if_index TSRMLS_CC); break; } +#endif default: php_error_docref(NULL TSRMLS_CC, E_WARNING, "unexpected option in php_do_mcast_opt (level %d, option %d). " @@ -2154,11 +2163,13 @@ PHP_FUNCTION(socket_set_option) if (level == IPPROTO_IP) { switch (optname) { case MCAST_JOIN_GROUP: - case MCAST_LEAVE_GROUP: + case MCAST_LEAVE_GROUP: +#ifdef HAS_MCAST_EXT case MCAST_BLOCK_SOURCE: case MCAST_UNBLOCK_SOURCE: case MCAST_JOIN_SOURCE_GROUP: case MCAST_LEAVE_SOURCE_GROUP: +#endif if (php_do_mcast_opt(php_sock, level, optname, arg4 TSRMLS_CC) == FAILURE) { RETURN_FALSE; } else { diff --git a/ext/sockets/tests/mcast_helpers.php.inc b/ext/sockets/tests/mcast_helpers.php.inc new file mode 100644 index 0000000000..ad65a3f9d6 --- /dev/null +++ b/ext/sockets/tests/mcast_helpers.php.inc @@ -0,0 +1,8 @@ + ", $str, "\n"; @@ -146,13 +151,13 @@ if ($i == 8) { } } ---EXPECT-- +--EXPECTF-- creating send socket bound to 127.0.0.1 bool(true) creating unbound socket and hoping the routing table causes an interface other than lo to be used for sending messages to 224.0.0.23 bool(true) creating receive socket -resource(6) of type (Socket) +resource(%d) of type (Socket) bool(true) bool(true) int(14) diff --git a/ext/sockets/tests/mcast_ipv6_recv.phpt b/ext/sockets/tests/mcast_ipv6_recv.phpt index b33306da1d..38390c025c 100644 --- a/ext/sockets/tests/mcast_ipv6_recv.phpt +++ b/ext/sockets/tests/mcast_ipv6_recv.phpt @@ -24,6 +24,10 @@ $r = socket_sendto($s, $m = "testing packet", strlen($m), 0, 'ff01::114', 3000); if ($r === false) { die('skip unable to send multicast packet.'); } + +if (!defined("MCAST_JOIN_SOURCE_GROUP")) + die('skip source operations are unavailable'); + $so = socket_set_option($s, IPPROTO_IPV6, MCAST_LEAVE_GROUP, array( "group" => 'ff01::114', "interface" => 0, @@ -39,6 +43,7 @@ if ($so === false) { --FILE-- ", $str, "\n"; @@ -171,9 +178,9 @@ if ($i == 8) { } --EXPECTF-- creating send socket -resource(4) of type (Socket) +resource(%d) of type (Socket) creating receive socket -resource(5) of type (Socket) +resource(%d) of type (Socket) bool(true) bool(true) int(14) diff --git a/ext/sockets/tests/mcast_ipv6_recv_limited.phpt b/ext/sockets/tests/mcast_ipv6_recv_limited.phpt index 2afea569bc..fda099877d 100644 --- a/ext/sockets/tests/mcast_ipv6_recv_limited.phpt +++ b/ext/sockets/tests/mcast_ipv6_recv_limited.phpt @@ -28,17 +28,20 @@ $so = socket_set_option($s, IPPROTO_IPV6, MCAST_LEAVE_GROUP, array( "group" => 'ff01::114', "interface" => 0, )); -$so = socket_set_option($s, IPPROTO_IPV6, MCAST_JOIN_SOURCE_GROUP, array( - "group" => 'ff01::114', - "interface" => 0, - "source" => '2001::dead:beef', -)); -if ($so !== false) { - die('skip protocol independent multicast API is available.'); +if (defined("MCAST_JOIN_SOURCE_GROUP")) { + $so = socket_set_option($s, IPPROTO_IPV6, MCAST_JOIN_SOURCE_GROUP, array( + "group" => 'ff01::114', + "interface" => 0, + "source" => '2001::dead:beef', + )); + if ($so !== false) { + die('skip protocol independent multicast API is available.'); + } } --FILE-- ", $str, "\n"; @@ -104,9 +109,9 @@ if ($i == 3) { } --EXPECTF-- creating send socket -resource(4) of type (Socket) +resource(%d) of type (Socket) creating receive socket -resource(5) of type (Socket) +resource(%d) of type (Socket) bool(true) bool(true) int(14) -- 2.40.0