xref: /PHP-5.5/win32/sockets.c (revision 73c1be26)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 5                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1997-2015 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 /* $Id$ */
23 
24 /* Code originally from ext/sockets */
25 
26 #include <stdio.h>
27 #include <fcntl.h>
28 
29 #include "php.h"
30 
socketpair(int domain,int type,int protocol,SOCKET sock[2])31 PHPAPI int socketpair(int domain, int type, int protocol, SOCKET sock[2])
32 {
33 	struct sockaddr_in address;
34 	SOCKET redirect;
35 	int size = sizeof(address);
36 
37 	if(domain != AF_INET) {
38 		WSASetLastError(WSAENOPROTOOPT);
39 		return -1;
40 	}
41 
42 	sock[0] = sock[1] = redirect = INVALID_SOCKET;
43 
44 
45 	sock[0]	= socket(domain, type, protocol);
46 	if (INVALID_SOCKET == sock[0]) {
47 		goto error;
48 	}
49 
50 	address.sin_addr.s_addr	= INADDR_ANY;
51 	address.sin_family	= AF_INET;
52 	address.sin_port	= 0;
53 
54 	if (bind(sock[0], (struct sockaddr*)&address, sizeof(address)) != 0) {
55 		goto error;
56 	}
57 
58 	if(getsockname(sock[0], (struct sockaddr *)&address, &size) != 0) {
59 		goto error;
60 	}
61 
62 	if (listen(sock[0], 2) != 0) {
63 		goto error;
64 	}
65 
66 	sock[1] = socket(domain, type, protocol);
67 	if (INVALID_SOCKET == sock[1]) {
68 		goto error;
69 	}
70 
71 	address.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
72 	if(connect(sock[1], (struct sockaddr*)&address, sizeof(address)) != 0) {
73 		goto error;
74 	}
75 
76 	redirect = accept(sock[0],(struct sockaddr*)&address, &size);
77 	if (INVALID_SOCKET == redirect) {
78 		goto error;
79 	}
80 
81 	closesocket(sock[0]);
82 	sock[0] = redirect;
83 
84 	return 0;
85 
86 error:
87 	closesocket(redirect);
88 	closesocket(sock[0]);
89 	closesocket(sock[1]);
90 	WSASetLastError(WSAECONNABORTED);
91 	return -1;
92 }
93