xref: /PHP-7.4/win32/sockets.c (revision 92ac598a)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) The PHP Group                                          |
6    +----------------------------------------------------------------------+
7    | This source file is subject to version 3.01 of the PHP license,      |
8    | that is bundled with this package in the file LICENSE, and is        |
9    | available through the world-wide-web at the following url:           |
10    | http://www.php.net/license/3_01.txt                                  |
11    | If you did not receive a copy of the PHP license and are unable to   |
12    | obtain it through the world-wide-web, please send a note to          |
13    | license@php.net so we can mail you a copy immediately.               |
14    +----------------------------------------------------------------------+
15    | Authors: Chris Vandomelen <chrisv@b0rked.dhs.org>                    |
16    |          Sterling Hughes  <sterling@php.net>                         |
17    |                                                                      |
18    | WinSock: Daniel Beulshausen <daniel@php4win.de>                      |
19    +----------------------------------------------------------------------+
20  */
21 
22 /* Code originally from ext/sockets */
23 
24 #include <stdio.h>
25 #include <fcntl.h>
26 
27 #include "php.h"
28 
socketpair(int domain,int type,int protocol,SOCKET sock[2])29 PHPAPI int socketpair(int domain, int type, int protocol, SOCKET sock[2])
30 {
31 	struct sockaddr_in address;
32 	SOCKET redirect;
33 	int size = sizeof(address);
34 
35 	if(domain != AF_INET) {
36 		WSASetLastError(WSAENOPROTOOPT);
37 		return -1;
38 	}
39 
40 	sock[0] = sock[1] = redirect = INVALID_SOCKET;
41 
42 
43 	sock[0]	= socket(domain, type, protocol);
44 	if (INVALID_SOCKET == sock[0]) {
45 		goto error;
46 	}
47 
48 	address.sin_addr.s_addr	= INADDR_ANY;
49 	address.sin_family	= AF_INET;
50 	address.sin_port	= 0;
51 
52 	if (bind(sock[0], (struct sockaddr*)&address, sizeof(address)) != 0) {
53 		goto error;
54 	}
55 
56 	if(getsockname(sock[0], (struct sockaddr *)&address, &size) != 0) {
57 		goto error;
58 	}
59 
60 	if (listen(sock[0], 2) != 0) {
61 		goto error;
62 	}
63 
64 	sock[1] = socket(domain, type, protocol);
65 	if (INVALID_SOCKET == sock[1]) {
66 		goto error;
67 	}
68 
69 	address.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
70 	if(connect(sock[1], (struct sockaddr*)&address, sizeof(address)) != 0) {
71 		goto error;
72 	}
73 
74 	redirect = accept(sock[0],(struct sockaddr*)&address, &size);
75 	if (INVALID_SOCKET == redirect) {
76 		goto error;
77 	}
78 
79 	closesocket(sock[0]);
80 	sock[0] = redirect;
81 
82 	return 0;
83 
84 error:
85 	closesocket(redirect);
86 	closesocket(sock[0]);
87 	closesocket(sock[1]);
88 	WSASetLastError(WSAECONNABORTED);
89 	return -1;
90 }
91