Debian Codegraph @

libmowgli-2/src/libmowgli/platform/win32/socketpair.c

raw
1 /* socketpair.c
2 * Copyright 2007, 2010 by Nathan C. Myers <[email protected]>
3 * This code is Free Software. It may be copied freely, in original or
4 * modified form, subject only to the restrictions that (1) the author is
5 * relieved from all responsibilities for any use for any purpose, and (2)
6 * this copyright notice must be retained, unchanged, in its entirety. If
7 * for any reason the author might be held responsible for any consequences
8 * of copying or use, license is withheld.
9 */
10
11 /* Changes:
12 * 2010-03-31:
13 * set addr to 127.0.0.1 because win32 getsockname does not always set it.
14 * 2010-02-25:
15 * set SO_REUSEADDR option to avoid leaking some windows resource.
16 * Windows System Error 10049, "Event ID 4226 TCP/IP has reached
17 * the security limit imposed on the number of concurrent TCP connect
18 * attempts." Bleah.
19 * 2007-04-25:
20 * preserve value of WSAGetLastError() on all error returns.
21 * 2007-04-22: (Thanks to Matthew Gregan <[email protected]>)
22 * s/EINVAL/WSAEINVAL/ fix trivial compile failure
23 * s/socket/WSASocket/ enable creation of sockets suitable as stdin/stdout
24 * of a child process.
25 * add argument make_overlapped
26 */
27
28 #include "mowgli.h"
29
30 #ifdef _WIN32
31
32 /* dumb_socketpair:
33 * If make_overlapped is nonzero, both sockets created will be usable for
34 * "overlapped" operations via WSASend etc. If make_overlapped is zero,
35 * socks[0] (only) will be usable with regular ReadFile etc., and thus
36 * suitable for use as stdin or stdout of a child process. Note that the
37 * sockets must be closed with closesocket() regardless.
38 */
39
40 int
41 socketpair(int domain, int type, int protocol, int socks[2])
42 {
43 union
44 {
45 struct sockaddr_in inaddr;
46
47 struct sockaddr addr;
48 } a;
49
50 SOCKET listener;
51 int e;
52 socklen_t addrlen = sizeof(a.inaddr);
53 int make_overlapped = 0;
54 DWORD flags = (make_overlapped ? WSA_FLAG_OVERLAPPED : 0);
55 int reuse = 1;
56
57 if (socks == NULL)
58 {
59 WSASetLastError(WSAEINVAL);
60 return SOCKET_ERROR;
61 }
62
63 listener = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
64
65 if (listener == INVALID_SOCKET)
66 return SOCKET_ERROR;
67
68 memset(&a, 0, sizeof(a));
69 a.inaddr.sin_family = AF_INET;
70 a.inaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
71 a.inaddr.sin_port = 0;
72
73 socks[0] = socks[1] = INVALID_SOCKET;
74
75 do
76 {
77 if (setsockopt(listener, SOL_SOCKET, SO_REUSEADDR,
78 (char *) &reuse, (socklen_t) sizeof(reuse)) == -1)
79 break;
80
81 if (bind(listener, &a.addr, sizeof(a.inaddr)) == SOCKET_ERROR)
82 break;
83
84 memset(&a, 0, sizeof(a));
85
86 if (getsockname(listener, &a.addr, &addrlen) == SOCKET_ERROR)
87 break;
88
89 // win32 getsockname may only set the port number, p=0.0005.
90 // ( http://msdn.microsoft.com/library/ms738543.aspx ):
91 a.inaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
92 a.inaddr.sin_family = AF_INET;
93
94 if (listen(listener, 1) == SOCKET_ERROR)
95 break;
96
97 socks[0] = WSASocket(AF_INET, SOCK_STREAM, 0, NULL, 0, flags);
98
99 if (socks[0] == (int) INVALID_SOCKET)
100 break;
101
102 if (connect(socks[0], &a.addr, sizeof(a.inaddr)) == SOCKET_ERROR)
103 break;
104
105 socks[1] = accept(listener, NULL, NULL);
106
107 if (socks[1] == (int) INVALID_SOCKET)
108 break;
109
110 closesocket(listener);
111 return 0;
112 } while (0);
113
114 e = WSAGetLastError();
115 closesocket(listener);
116 closesocket(socks[0]);
117 closesocket(socks[1]);
118 WSASetLastError(e);
119 return SOCKET_ERROR;
120 }
121
122 #endif