Support socketpairs in proc_open()

Closes GH-5777.
This commit is contained in:
Martin Schröder 2020-07-12 20:56:47 +02:00 committed by Nikita Popov
parent 1a00d015be
commit 547d98b81d
10 changed files with 301 additions and 38 deletions

View file

@ -24,34 +24,33 @@
#include "php.h"
PHPAPI int socketpair(int domain, int type, int protocol, SOCKET sock[2])
PHPAPI int socketpair_win32(int domain, int type, int protocol, SOCKET sock[2], int overlapped)
{
struct sockaddr_in address;
SOCKET redirect;
int size = sizeof(address);
if(domain != AF_INET) {
if (domain != AF_INET) {
WSASetLastError(WSAENOPROTOOPT);
return -1;
}
sock[0] = sock[1] = redirect = INVALID_SOCKET;
sock[1] = redirect = INVALID_SOCKET;
sock[0] = socket(domain, type, protocol);
sock[0] = socket(domain, type, protocol);
if (INVALID_SOCKET == sock[0]) {
goto error;
}
address.sin_addr.s_addr = INADDR_ANY;
address.sin_family = AF_INET;
address.sin_port = 0;
address.sin_family = AF_INET;
address.sin_port = 0;
if (bind(sock[0], (struct sockaddr*)&address, sizeof(address)) != 0) {
if (bind(sock[0], (struct sockaddr *) &address, sizeof(address)) != 0) {
goto error;
}
if(getsockname(sock[0], (struct sockaddr *)&address, &size) != 0) {
if (getsockname(sock[0], (struct sockaddr *) &address, &size) != 0) {
goto error;
}
@ -59,17 +58,22 @@ PHPAPI int socketpair(int domain, int type, int protocol, SOCKET sock[2])
goto error;
}
sock[1] = socket(domain, type, protocol);
if (overlapped) {
sock[1] = socket(domain, type, protocol);
} else {
sock[1] = WSASocket(domain, type, protocol, NULL, 0, 0);
}
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) {
if (connect(sock[1], (struct sockaddr *) &address, sizeof(address)) != 0) {
goto error;
}
redirect = accept(sock[0],(struct sockaddr*)&address, &size);
redirect = accept(sock[0], (struct sockaddr *) &address, &size);
if (INVALID_SOCKET == redirect) {
goto error;
}
@ -86,3 +90,8 @@ error:
WSASetLastError(WSAECONNABORTED);
return -1;
}
PHPAPI int socketpair(int domain, int type, int protocol, SOCKET sock[2])
{
return socketpair_win32(domain, type, protocol, sock, 1);
}

View file

@ -22,6 +22,7 @@
#ifndef PHP_WIN32_SOCKETS_H
#define PHP_WIN32_SOCKETS_H
PHPAPI int socketpair_win32(int domain, int type, int protocol, SOCKET sock[2], int overlapped);
PHPAPI int socketpair(int domain, int type, int protocol, SOCKET sock[2]);
#endif