From: Wez Furlong Date: Sun, 12 Dec 2004 16:10:35 +0000 (+0000) Subject: Add stream_socket_pair(), a streams based version of socketpair(). X-Git-Tag: RELEASE_0_2~533 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=d10b86f8f42aa3625c6e89f543bade10d0c6917b;p=php Add stream_socket_pair(), a streams based version of socketpair(). Modified patch from Vincent [six at t0x dot net] --- diff --git a/configure.in b/configure.in index c995a5b3d8..ac5a362307 100644 --- a/configure.in +++ b/configure.in @@ -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) diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c index fc108d9abe..0f00612db2 100644 --- a/ext/standard/basic_functions.c +++ b/ext/standard/basic_functions.c @@ -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) diff --git a/ext/standard/file.c b/ext/standard/file.c index a6bdddd321..adb8b2fde0 100644 --- a/ext/standard/file.c +++ b/ext/standard/file.c @@ -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); diff --git a/ext/standard/streamsfuncs.c b/ext/standard/streamsfuncs.c index 2853ac1ea9..b9970c4d38 100644 --- a/ext/standard/streamsfuncs.c +++ b/ext/standard/streamsfuncs.c @@ -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) diff --git a/ext/standard/streamsfuncs.h b/ext/standard/streamsfuncs.h index afe36c384e..41ad2fe2c6 100644 --- a/ext/standard/streamsfuncs.h +++ b/ext/standard/streamsfuncs.h @@ -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: