cataphract                               Sat, 11 Jun 2011 20:20:50 +0000

Revision: http://svn.php.net/viewvc?view=revision&revision=312069

Log:
- Fixed ext/sockets build on Mac OS X (hopefully).
- Improvements in the multicast tests.
- Very light refactoring in sockets.c.

Changed paths:
    U   php/php-src/branches/PHP_5_4/ext/sockets/multicast.c
    U   php/php-src/branches/PHP_5_4/ext/sockets/multicast.h
    U   php/php-src/branches/PHP_5_4/ext/sockets/sockets.c
    A   php/php-src/branches/PHP_5_4/ext/sockets/tests/mcast_helpers.php.inc
    U   php/php-src/branches/PHP_5_4/ext/sockets/tests/mcast_ipv4_recv.phpt
    U   php/php-src/branches/PHP_5_4/ext/sockets/tests/mcast_ipv6_recv.phpt
    U   
php/php-src/branches/PHP_5_4/ext/sockets/tests/mcast_ipv6_recv_limited.phpt
    U   php/php-src/trunk/ext/sockets/multicast.c
    U   php/php-src/trunk/ext/sockets/multicast.h
    U   php/php-src/trunk/ext/sockets/sockets.c
    A   php/php-src/trunk/ext/sockets/tests/mcast_helpers.php.inc
    U   php/php-src/trunk/ext/sockets/tests/mcast_ipv4_recv.phpt
    U   php/php-src/trunk/ext/sockets/tests/mcast_ipv6_recv.phpt
    U   php/php-src/trunk/ext/sockets/tests/mcast_ipv6_recv_limited.phpt

Modified: php/php-src/branches/PHP_5_4/ext/sockets/multicast.c
===================================================================
--- php/php-src/branches/PHP_5_4/ext/sockets/multicast.c	2011-06-11 20:00:42 UTC (rev 312068)
+++ php/php-src/branches/PHP_5_4/ext/sockets/multicast.c	2011-06-11 20:20:50 UTC (rev 312069)
@@ -53,12 +53,7 @@
 #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,
 	LEAVE_SOURCE,
@@ -67,10 +62,13 @@
 };

 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 @@
 	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,7 +141,9 @@
 {
 	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,
 	int level,
@@ -204,6 +205,7 @@
 #endif
 }

+#ifdef HAS_MCAST_EXT
 static int _php_mcast_source_op(
 	php_socket *sock,
 	int level,
@@ -318,6 +320,8 @@
 }
 #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)
 {

Modified: php/php-src/branches/PHP_5_4/ext/sockets/multicast.h
===================================================================
--- php/php-src/branches/PHP_5_4/ext/sockets/multicast.h	2011-06-11 20:00:42 UTC (rev 312068)
+++ php/php-src/branches/PHP_5_4/ext/sockets/multicast.h	2011-06-11 20:20:50 UTC (rev 312069)
@@ -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 @@
 	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 @@
 	struct sockaddr *source,
 	socklen_t source_len,
 	unsigned int if_index TSRMLS_DC);
+#endif

Modified: php/php-src/branches/PHP_5_4/ext/sockets/sockets.c
===================================================================
--- php/php-src/branches/PHP_5_4/ext/sockets/sockets.c	2011-06-11 20:00:42 UTC (rev 312068)
+++ php/php-src/branches/PHP_5_4/ext/sockets/sockets.c	2011-06-11 20:20:50 UTC (rev 312069)
@@ -806,21 +806,25 @@
 	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 @@
 {
 	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 @@

 	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 @@

 			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 @@

 			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 @@
 	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 @@
 			break;
 		}

+#ifdef HAS_MCAST_EXT
 	case MCAST_BLOCK_SOURCE:
 		mcast_sreq_fun = &php_mcast_block_source;
 		goto mcast_sreq_fun;
@@ -2103,6 +2111,7 @@
 					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 @@
 	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 {

Added: php/php-src/branches/PHP_5_4/ext/sockets/tests/mcast_helpers.php.inc
===================================================================
--- php/php-src/branches/PHP_5_4/ext/sockets/tests/mcast_helpers.php.inc	                        (rev 0)
+++ php/php-src/branches/PHP_5_4/ext/sockets/tests/mcast_helpers.php.inc	2011-06-11 20:20:50 UTC (rev 312069)
@@ -0,0 +1,8 @@
+<?php
+function checktimeout($sock, $limit) {
+	$readfs = array($sock);
+	$writefs = $exceptfs = array();
+	if (socket_select($readfs, $writefs, $exceptfs, 0, $limit*1000) != 1) {
+		die("Socket read timeout hit. Can be a bug, a test bug, or a firewall issue.");
+	}
+}

Modified: php/php-src/branches/PHP_5_4/ext/sockets/tests/mcast_ipv4_recv.phpt
===================================================================
--- php/php-src/branches/PHP_5_4/ext/sockets/tests/mcast_ipv4_recv.phpt	2011-06-11 20:00:42 UTC (rev 312068)
+++ php/php-src/branches/PHP_5_4/ext/sockets/tests/mcast_ipv4_recv.phpt	2011-06-11 20:20:50 UTC (rev 312069)
@@ -14,8 +14,12 @@
 if ($so === false) {
     die('skip interface \'lo\' is unavailable.');
 }
+if (!defined("MCAST_BLOCK_SOURCE")) {
+    die('skip source operations are unavailable');
+}
 --FILE--
 <?php
+include __DIR__."/mcast_helpers.php.inc";
 $domain = AF_INET;
 $level = IPPROTO_IP;
 $interface = "lo";
@@ -47,6 +51,7 @@
 var_dump($r);

 $i = 0;
+checktimeout($s, 500);
 while (($str = socket_read($s, 3000)) !== FALSE) {
 	$i++;
 	echo "$i> ", $str, "\n";
@@ -146,13 +151,13 @@
 }

 }
---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)

Modified: php/php-src/branches/PHP_5_4/ext/sockets/tests/mcast_ipv6_recv.phpt
===================================================================
--- php/php-src/branches/PHP_5_4/ext/sockets/tests/mcast_ipv6_recv.phpt	2011-06-11 20:00:42 UTC (rev 312068)
+++ php/php-src/branches/PHP_5_4/ext/sockets/tests/mcast_ipv6_recv.phpt	2011-06-11 20:20:50 UTC (rev 312069)
@@ -24,6 +24,10 @@
 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 @@

 --FILE--
 <?php
+include __DIR__."/mcast_helpers.php.inc";
 $domain = AF_INET6;
 $level = IPPROTO_IPV6;
 $interface = 0;
@@ -63,6 +68,7 @@

 $r = socket_sendto($sends1, $m = "testing packet", strlen($m), 0, $mcastaddr, 3000);
 var_dump($r);
+checktimeout($s, 500);
 $r = socket_recvfrom($s, $str, 2000, 0, $from, $fromPort);
 var_dump($r, $str, $from);
 $sblock = $from;
@@ -71,6 +77,7 @@
 var_dump($r);

 $i = 0;
+checktimeout($s, 500);
 while (($str = socket_read($s, 3000)) !== FALSE) {
 	$i++;
 	echo "$i> ", $str, "\n";
@@ -171,9 +178,9 @@
 }
 --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)

Modified: php/php-src/branches/PHP_5_4/ext/sockets/tests/mcast_ipv6_recv_limited.phpt
===================================================================
--- php/php-src/branches/PHP_5_4/ext/sockets/tests/mcast_ipv6_recv_limited.phpt	2011-06-11 20:00:42 UTC (rev 312068)
+++ php/php-src/branches/PHP_5_4/ext/sockets/tests/mcast_ipv6_recv_limited.phpt	2011-06-11 20:20:50 UTC (rev 312069)
@@ -28,17 +28,20 @@
 	"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--
 <?php
+include __DIR__."/mcast_helpers.php.inc";
 $domain = AF_INET6;
 $level = IPPROTO_IPV6;
 $interface = 0;
@@ -63,6 +66,7 @@

 $r = socket_sendto($sends1, $m = "testing packet", strlen($m), 0, $mcastaddr, 3000);
 var_dump($r);
+checktimeout($s, 500);
 $r = socket_recvfrom($s, $str, 2000, 0, $from, $fromPort);
 var_dump($r, $str, $from);
 $sblock = $from;
@@ -71,7 +75,8 @@
 var_dump($r);

 $i = 0;
-while (($str = socket_read($s, 3000)) !== FALSE) {
+checktimeout($s, 500);
+while (($str = socket_read($s, 3000, 500)) !== FALSE) {
 	$i++;
 	echo "$i> ", $str, "\n";

@@ -104,9 +109,9 @@
 }
 --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)

Modified: php/php-src/trunk/ext/sockets/multicast.c
===================================================================
--- php/php-src/trunk/ext/sockets/multicast.c	2011-06-11 20:00:42 UTC (rev 312068)
+++ php/php-src/trunk/ext/sockets/multicast.c	2011-06-11 20:20:50 UTC (rev 312069)
@@ -53,12 +53,7 @@
 #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,
 	LEAVE_SOURCE,
@@ -67,10 +62,13 @@
 };

 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 @@
 	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,7 +141,9 @@
 {
 	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,
 	int level,
@@ -204,6 +205,7 @@
 #endif
 }

+#ifdef HAS_MCAST_EXT
 static int _php_mcast_source_op(
 	php_socket *sock,
 	int level,
@@ -318,6 +320,8 @@
 }
 #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)
 {

Modified: php/php-src/trunk/ext/sockets/multicast.h
===================================================================
--- php/php-src/trunk/ext/sockets/multicast.h	2011-06-11 20:00:42 UTC (rev 312068)
+++ php/php-src/trunk/ext/sockets/multicast.h	2011-06-11 20:20:50 UTC (rev 312069)
@@ -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 @@
 	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 @@
 	struct sockaddr *source,
 	socklen_t source_len,
 	unsigned int if_index TSRMLS_DC);
+#endif

Modified: php/php-src/trunk/ext/sockets/sockets.c
===================================================================
--- php/php-src/trunk/ext/sockets/sockets.c	2011-06-11 20:00:42 UTC (rev 312068)
+++ php/php-src/trunk/ext/sockets/sockets.c	2011-06-11 20:20:50 UTC (rev 312069)
@@ -806,21 +806,25 @@
 	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 @@
 {
 	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 @@

 	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 @@

 			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 @@

 			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 @@
 	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 @@
 			break;
 		}

+#ifdef HAS_MCAST_EXT
 	case MCAST_BLOCK_SOURCE:
 		mcast_sreq_fun = &php_mcast_block_source;
 		goto mcast_sreq_fun;
@@ -2103,6 +2111,7 @@
 					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 @@
 	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 {

Added: php/php-src/trunk/ext/sockets/tests/mcast_helpers.php.inc
===================================================================
--- php/php-src/trunk/ext/sockets/tests/mcast_helpers.php.inc	                        (rev 0)
+++ php/php-src/trunk/ext/sockets/tests/mcast_helpers.php.inc	2011-06-11 20:20:50 UTC (rev 312069)
@@ -0,0 +1,8 @@
+<?php
+function checktimeout($sock, $limit) {
+	$readfs = array($sock);
+	$writefs = $exceptfs = array();
+	if (socket_select($readfs, $writefs, $exceptfs, 0, $limit*1000) != 1) {
+		die("Socket read timeout hit. Can be a bug, a test bug, or a firewall issue.");
+	}
+}

Modified: php/php-src/trunk/ext/sockets/tests/mcast_ipv4_recv.phpt
===================================================================
--- php/php-src/trunk/ext/sockets/tests/mcast_ipv4_recv.phpt	2011-06-11 20:00:42 UTC (rev 312068)
+++ php/php-src/trunk/ext/sockets/tests/mcast_ipv4_recv.phpt	2011-06-11 20:20:50 UTC (rev 312069)
@@ -14,8 +14,12 @@
 if ($so === false) {
     die('skip interface \'lo\' is unavailable.');
 }
+if (!defined("MCAST_BLOCK_SOURCE")) {
+    die('skip source operations are unavailable');
+}
 --FILE--
 <?php
+include __DIR__."/mcast_helpers.php.inc";
 $domain = AF_INET;
 $level = IPPROTO_IP;
 $interface = "lo";
@@ -47,6 +51,7 @@
 var_dump($r);

 $i = 0;
+checktimeout($s, 500);
 while (($str = socket_read($s, 3000)) !== FALSE) {
 	$i++;
 	echo "$i> ", $str, "\n";
@@ -146,13 +151,13 @@
 }

 }
---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)

Modified: php/php-src/trunk/ext/sockets/tests/mcast_ipv6_recv.phpt
===================================================================
--- php/php-src/trunk/ext/sockets/tests/mcast_ipv6_recv.phpt	2011-06-11 20:00:42 UTC (rev 312068)
+++ php/php-src/trunk/ext/sockets/tests/mcast_ipv6_recv.phpt	2011-06-11 20:20:50 UTC (rev 312069)
@@ -24,6 +24,10 @@
 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 @@

 --FILE--
 <?php
+include __DIR__."/mcast_helpers.php.inc";
 $domain = AF_INET6;
 $level = IPPROTO_IPV6;
 $interface = 0;
@@ -63,6 +68,7 @@

 $r = socket_sendto($sends1, $m = "testing packet", strlen($m), 0, $mcastaddr, 3000);
 var_dump($r);
+checktimeout($s, 500);
 $r = socket_recvfrom($s, $str, 2000, 0, $from, $fromPort);
 var_dump($r, $str, $from);
 $sblock = $from;
@@ -71,6 +77,7 @@
 var_dump($r);

 $i = 0;
+checktimeout($s, 500);
 while (($str = socket_read($s, 3000)) !== FALSE) {
 	$i++;
 	echo "$i> ", $str, "\n";
@@ -171,9 +178,9 @@
 }
 --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)

Modified: php/php-src/trunk/ext/sockets/tests/mcast_ipv6_recv_limited.phpt
===================================================================
--- php/php-src/trunk/ext/sockets/tests/mcast_ipv6_recv_limited.phpt	2011-06-11 20:00:42 UTC (rev 312068)
+++ php/php-src/trunk/ext/sockets/tests/mcast_ipv6_recv_limited.phpt	2011-06-11 20:20:50 UTC (rev 312069)
@@ -28,17 +28,20 @@
 	"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--
 <?php
+include __DIR__."/mcast_helpers.php.inc";
 $domain = AF_INET6;
 $level = IPPROTO_IPV6;
 $interface = 0;
@@ -63,6 +66,7 @@

 $r = socket_sendto($sends1, $m = "testing packet", strlen($m), 0, $mcastaddr, 3000);
 var_dump($r);
+checktimeout($s, 500);
 $r = socket_recvfrom($s, $str, 2000, 0, $from, $fromPort);
 var_dump($r, $str, $from);
 $sblock = $from;
@@ -71,7 +75,8 @@
 var_dump($r);

 $i = 0;
-while (($str = socket_read($s, 3000)) !== FALSE) {
+checktimeout($s, 500);
+while (($str = socket_read($s, 3000, 500)) !== FALSE) {
 	$i++;
 	echo "$i> ", $str, "\n";

@@ -104,9 +109,9 @@
 }
 --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)
-- 
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to