blob: 7228832eef760da673dcd0a1e3d7d82bf46f4784 [file] [log] [blame]
Roger Meier84e4a3c2011-09-16 20:58:44 +00001/* socketpair.c
2 * Copyright 2007 by Nathan C. Myers <ncm@cantrip.org>; some rights reserved.
Roger Meierb69d24d2012-10-04 18:02:15 +00003 * This code is Free Software. It may be copied freely, in original or
Roger Meier84e4a3c2011-09-16 20:58:44 +00004 * 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
Roger Meierb69d24d2012-10-04 18:02:15 +00008 * of copying or use, license is withheld.
Roger Meier84e4a3c2011-09-16 20:58:44 +00009 */
10
11/*
12 * Licensed to the Apache Software Foundation (ASF) under one
13 * or more contributor license agreements. See the NOTICE file
14 * distributed with this work for additional information
15 * regarding copyright ownership. The ASF licenses this file
16 * to you under the Apache License, Version 2.0 (the
17 * "License"); you may not use this file except in compliance
18 * with the License. You may obtain a copy of the License at
19 *
20 * http://www.apache.org/licenses/LICENSE-2.0
21 *
22 * Unless required by applicable law or agreed to in writing,
23 * software distributed under the License is distributed on an
24 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
25 * KIND, either express or implied. See the License for the
26 * specific language governing permissions and limitations
27 * under the License.
28 */
29
Roger Meier4285ba22013-06-10 21:17:23 +020030#include <thrift/windows/SocketPair.h>
Roger Meierd051ca02013-08-15 01:35:11 +020031#include <thrift/Thrift.h>
Roger Meier84e4a3c2011-09-16 20:58:44 +000032
33// stl
34#include <string.h>
35
36// Win32
Roger Meier84e4a3c2011-09-16 20:58:44 +000037#include <WS2tcpip.h>
38
Konrad Grochowski16a23a62014-11-13 15:33:38 +010039int thrift_socketpair(int d, int type, int protocol, THRIFT_SOCKET sv[2]) {
40 THRIFT_UNUSED_VARIABLE(protocol);
41 THRIFT_UNUSED_VARIABLE(type);
42 THRIFT_UNUSED_VARIABLE(d);
Roger Meierd051ca02013-08-15 01:35:11 +020043
Konrad Grochowski16a23a62014-11-13 15:33:38 +010044 union {
45 struct sockaddr_in inaddr;
46 struct sockaddr addr;
47 } a;
48 THRIFT_SOCKET listener;
49 int e;
50 socklen_t addrlen = sizeof(a.inaddr);
51 DWORD flags = 0;
52 int reuse = 1;
Roger Meier84e4a3c2011-09-16 20:58:44 +000053
Konrad Grochowski16a23a62014-11-13 15:33:38 +010054 if (sv == 0) {
55 WSASetLastError(WSAEINVAL);
Konrad Grochowski240120c2014-11-18 11:33:31 +010056 return SOCKET_ERROR;
Konrad Grochowski16a23a62014-11-13 15:33:38 +010057 }
58
59 listener = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
60 if (listener == INVALID_SOCKET)
61 return SOCKET_ERROR;
62
63 memset(&a, 0, sizeof(a));
64 a.inaddr.sin_family = AF_INET;
65 a.inaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
66 a.inaddr.sin_port = 0;
67
68 sv[0] = sv[1] = INVALID_SOCKET;
69 do {
70 // ignore errors coming out of this setsockopt. This is because
71 // SO_EXCLUSIVEADDRUSE requires admin privileges on WinXP, but we don't
72 // want to force socket pairs to be an admin.
73 setsockopt(listener, SOL_SOCKET, SO_EXCLUSIVEADDRUSE, (char*)&reuse, (socklen_t)sizeof(reuse));
74 if (bind(listener, &a.addr, sizeof(a.inaddr)) == SOCKET_ERROR)
75 break;
76 if (getsockname(listener, &a.addr, &addrlen) == SOCKET_ERROR)
77 break;
78 if (listen(listener, 1) == SOCKET_ERROR)
79 break;
80 sv[0] = WSASocket(AF_INET, SOCK_STREAM, 0, NULL, 0, flags);
81 if (sv[0] == INVALID_SOCKET)
82 break;
83 if (connect(sv[0], &a.addr, sizeof(a.inaddr)) == SOCKET_ERROR)
84 break;
85 sv[1] = accept(listener, NULL, NULL);
86 if (sv[1] == INVALID_SOCKET)
87 break;
88
89 closesocket(listener);
90 return 0;
91
92 } while (0);
93
94 e = WSAGetLastError();
95 closesocket(listener);
96 closesocket(sv[0]);
97 closesocket(sv[1]);
98 WSASetLastError(e);
99 return SOCKET_ERROR;
Roger Meier84e4a3c2011-09-16 20:58:44 +0000100}