diff --git a/NEWS b/NEWS index a5cd7f23a74..0876c1e2fe7 100644 --- a/NEWS +++ b/NEWS @@ -11,6 +11,10 @@ PHP NEWS . Fixed bug #64342 (ZipArchive::addFile() has to check for file existence). (Anatol) +- Streams: + . Fixed Windows x64 version of stream_socket_pair() and improved error handling + (Anatol Belski) + 11 Apr 2013, PHP 5.3.24 - Core diff --git a/ext/standard/streamsfuncs.c b/ext/standard/streamsfuncs.c index 4a86007e853..27042f6e5dd 100644 --- a/ext/standard/streamsfuncs.c +++ b/ext/standard/streamsfuncs.c @@ -51,7 +51,7 @@ PHP_FUNCTION(stream_socket_pair) { long domain, type, protocol; php_stream *s1, *s2; - int pair[2]; + php_socket_t pair[2]; if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lll", &domain, &type, &protocol)) { diff --git a/win32/sockets.c b/win32/sockets.c index d64280870f3..c8fddd6e525 100644 --- a/win32/sockets.c +++ b/win32/sockets.c @@ -39,33 +39,54 @@ PHPAPI int socketpair(int domain, int type, int protocol, SOCKET sock[2]) return -1; } + sock[0] = sock[1] = redirect = INVALID_SOCKET; - sock[0] = socket(domain, type, protocol); - address.sin_addr.s_addr = INADDR_ANY; - address.sin_family = AF_INET; - address.sin_port = 0; - bind(sock[0], (struct sockaddr*)&address, sizeof(address)); - - if(getsockname(sock[0], (struct sockaddr *)&address, &size) != 0) { + sock[0] = socket(domain, type, protocol); + if (INVALID_SOCKET == sock[0]) { + goto error; } - listen(sock[0], 2); - sock[1] = socket(domain, type, protocol); - address.sin_addr.s_addr = htonl(INADDR_LOOPBACK); + address.sin_addr.s_addr = INADDR_ANY; + address.sin_family = AF_INET; + address.sin_port = 0; + + if (bind(sock[0], (struct sockaddr*)&address, sizeof(address)) != 0) { + goto error; + } + + if(getsockname(sock[0], (struct sockaddr *)&address, &size) != 0) { + goto error; + } + + if (listen(sock[0], 2) != 0) { + goto error; + } + + sock[1] = socket(domain, type, protocol); + if (INVALID_SOCKET == sock[1]) { + goto error; + } + + address.sin_addr.s_addr = htonl(INADDR_LOOPBACK); + if(connect(sock[1], (struct sockaddr*)&address, sizeof(address)) != 0) { + goto error; + } - connect(sock[1], (struct sockaddr*)&address, sizeof(address)); redirect = accept(sock[0],(struct sockaddr*)&address, &size); + if (INVALID_SOCKET == redirect) { + goto error; + } closesocket(sock[0]); sock[0] = redirect; - if(sock[0] == INVALID_SOCKET ) { - closesocket(sock[0]); - closesocket(sock[1]); - WSASetLastError(WSAECONNABORTED); - return -1; - } - return 0; + +error: + closesocket(redirect); + closesocket(sock[0]); + closesocket(sock[1]); + WSASetLastError(WSAECONNABORTED); + return -1; }