]> granicus.if.org Git - php/commitdiff
Add stream_socket_pair(), a streams based version of socketpair().
authorWez Furlong <wez@php.net>
Sun, 12 Dec 2004 16:10:35 +0000 (16:10 +0000)
committerWez Furlong <wez@php.net>
Sun, 12 Dec 2004 16:10:35 +0000 (16:10 +0000)
Modified patch from Vincent [six at t0x dot net]

configure.in
ext/standard/basic_functions.c
ext/standard/file.c
ext/standard/streamsfuncs.c
ext/standard/streamsfuncs.h

index c995a5b3d853a1ccc5c5bc008d72e06ba6647c76..ac5a36230735a3c8f572c8e778b1981c53214b32 100644 (file)
@@ -322,6 +322,7 @@ dnl are usually in libnsl
 dnl Also, uClibc will bark at linking with glibc's libnsl.
 
 PHP_CHECK_FUNC(socket, socket)
+PHP_CHECK_FUNC(socketpair, socket)
 PHP_CHECK_FUNC(htonl, socket)
 PHP_CHECK_FUNC(gethostname, nsl)
 PHP_CHECK_FUNC(gethostbyaddr, nsl)
index fc108d9abe591ffbc9013f4e80448dfd9c0817c1..0f00612db216e71d1708107a33a149c1bb2d2cb3 100644 (file)
@@ -602,6 +602,9 @@ function_entry basic_functions[] = {
        PHP_FE(stream_socket_recvfrom,                                                  fourth_arg_force_ref)
        PHP_FE(stream_socket_sendto,                                                                                    NULL)
        PHP_FE(stream_socket_enable_crypto,                                                                             NULL)
+#if HAVE_SOCKETPAIR
+       PHP_FE(stream_socket_pair,                                                                                              NULL)
+#endif
        PHP_FE(stream_copy_to_stream,                                                                                   NULL)
        PHP_FE(stream_get_contents,                                                                                             NULL)
        PHP_FE(fgetcsv,                                                                                                                 NULL)
index a6bdddd3211a414a2f0014ca7762048f62c200c2..adb8b2fde0de3b39954ffc11e11fbcfc9654aea1 100644 (file)
@@ -230,6 +230,17 @@ PHP_MINIT_FUNCTION(file)
        REGISTER_LONG_CONSTANT("STREAM_CRYPTO_METHOD_SSLv23_SERVER",    STREAM_CRYPTO_METHOD_SSLv23_SERVER,     CONST_CS|CONST_PERSISTENT);
        REGISTER_LONG_CONSTANT("STREAM_CRYPTO_METHOD_TLS_SERVER",               STREAM_CRYPTO_METHOD_TLS_SERVER,        CONST_CS|CONST_PERSISTENT);
 
+       REGISTER_LONG_CONSTANT("STREAM_SOCK_STREAM", SOCK_STREAM, CONST_CS|CONST_PERSISTENT);
+       REGISTER_LONG_CONSTANT("STREAM_SOCK_DGRAM", SOCK_DGRAM, CONST_CS|CONST_PERSISTENT);
+#ifdef SOCK_RAW
+       REGISTER_LONG_CONSTANT("STREAM_SOCK_RAW", SOCK_RAW, CONST_CS|CONST_PERSISTENT);
+#endif
+#ifdef SOCK_SEQPACKET
+       REGISTER_LONG_CONSTANT("STREAM_SOCK_SEQPACKET", SOCK_SEQPACKET, CONST_CS|CONST_PERSISTENT);
+#endif
+#ifdef SOCK_RDM
+       REGISTER_LONG_CONSTANT("STREAM_SOCK_RDM", SOCK_RDM, CONST_CS|CONST_PERSISTENT);
+#endif
        REGISTER_LONG_CONSTANT("STREAM_PEEK", STREAM_PEEK, CONST_CS | CONST_PERSISTENT);
        REGISTER_LONG_CONSTANT("STREAM_OOB",  STREAM_OOB, CONST_CS | CONST_PERSISTENT);
 
index 2853ac1ea99f2b06823c1854423c53d6888be5e5..b9970c4d384bc9502f2a1a48d8985313b36805dc 100644 (file)
@@ -41,6 +41,38 @@ static php_stream_context *decode_context_param(zval *contextresource TSRMLS_DC)
 
 /* Streams based network functions */
 
+#if HAVE_SOCKETPAIR
+/* {{{ proto array stream_socket_pair(int domain, int type, int protocol)
+   Creates a pair of indistinguishable socket streams */
+PHP_FUNCTION(stream_socket_pair)
+{
+       long domain, type, protocol;
+       php_stream *s1, *s2;
+       zval *zs1, *sz2;
+       int pair[2];
+
+       if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lll",
+                       &domain, &type, &protocol)) {
+               RETURN_FALSE;
+       }
+
+       if (0 != socketpair(domain, type, protocol, pair)) {
+               php_error_docref(NULL TSRMLS_CC, E_WARNING, "failed to create sockets: [%d]: %s",
+                       php_socket_errno(), php_socket_strerror(php_socket_errno()));
+               RETURN_FALSE;
+       }
+
+       array_init(return_value);
+
+       s1 = php_stream_sock_open_from_socket(pair[0], 0);
+       s2 = php_stream_sock_open_from_socket(pair[1], 0);
+
+       add_next_index_zval(return_value, php_stream_get_resource_id(s1));
+       add_next_index_zval(return_value, php_stream_get_resource_id(s2));
+}
+/* }}} */
+#endif
+
 /* {{{ proto resource stream_socket_client(string remoteaddress [, long &errcode, string &errstring, double timeout, long flags, resource context])
    Open a client connection to a remote address */
 PHP_FUNCTION(stream_socket_client)
index afe36c384e9c94bfe0ff41788dbf007e325dc437..41ad2fe2c6c58ed7578a0a1971faf798f30d8935 100644 (file)
@@ -54,6 +54,7 @@ PHP_FUNCTION(stream_filter_prepend);
 PHP_FUNCTION(stream_filter_append);
 PHP_FUNCTION(stream_filter_remove);
 PHP_FUNCTION(stream_socket_enable_crypto);
+PHP_FUNCTION(stream_socket_pair);
 
 /*
  * Local variables: