Fixed stream_socket_pair() on Windows x64 and

improved errorhandling in the socketpair() implementation.
This commit is contained in:
Anatol Belski 2013-04-22 18:53:52 +02:00
parent 70b67f2e50
commit f082d6311b
3 changed files with 44 additions and 19 deletions

4
NEWS
View File

@ -11,6 +11,10 @@ PHP NEWS
. Fixed bug #64342 (ZipArchive::addFile() has to check for file existence). . Fixed bug #64342 (ZipArchive::addFile() has to check for file existence).
(Anatol) (Anatol)
- Streams:
. Fixed Windows x64 version of stream_socket_pair() and improved error handling
(Anatol Belski)
11 Apr 2013, PHP 5.3.24 11 Apr 2013, PHP 5.3.24
- Core - Core

View File

@ -51,7 +51,7 @@ PHP_FUNCTION(stream_socket_pair)
{ {
long domain, type, protocol; long domain, type, protocol;
php_stream *s1, *s2; php_stream *s1, *s2;
int pair[2]; php_socket_t pair[2];
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lll", if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lll",
&domain, &type, &protocol)) { &domain, &type, &protocol)) {

View File

@ -39,33 +39,54 @@ PHPAPI int socketpair(int domain, int type, int protocol, SOCKET sock[2])
return -1; 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)); sock[0] = socket(domain, type, protocol);
if (INVALID_SOCKET == sock[0]) {
if(getsockname(sock[0], (struct sockaddr *)&address, &size) != 0) { goto error;
} }
listen(sock[0], 2); address.sin_addr.s_addr = INADDR_ANY;
sock[1] = socket(domain, type, protocol); address.sin_family = AF_INET;
address.sin_addr.s_addr = htonl(INADDR_LOOPBACK); 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); redirect = accept(sock[0],(struct sockaddr*)&address, &size);
if (INVALID_SOCKET == redirect) {
goto error;
}
closesocket(sock[0]); closesocket(sock[0]);
sock[0] = redirect; sock[0] = redirect;
if(sock[0] == INVALID_SOCKET ) {
closesocket(sock[0]);
closesocket(sock[1]);
WSASetLastError(WSAECONNABORTED);
return -1;
}
return 0; return 0;
error:
closesocket(redirect);
closesocket(sock[0]);
closesocket(sock[1]);
WSASetLastError(WSAECONNABORTED);
return -1;
} }