| 1 | /*************************************************************************** | 
|---|
| 2 | *                                  _   _ ____  _ | 
|---|
| 3 | *  Project                     ___| | | |  _ \| | | 
|---|
| 4 | *                             / __| | | | |_) | | | 
|---|
| 5 | *                            | (__| |_| |  _ <| |___ | 
|---|
| 6 | *                             \___|\___/|_| \_\_____| | 
|---|
| 7 | * | 
|---|
| 8 | * Copyright (C) 2019, Daniel Stenberg, <daniel@haxx.se>, et al. | 
|---|
| 9 | * | 
|---|
| 10 | * This software is licensed as described in the file COPYING, which | 
|---|
| 11 | * you should have received as part of this distribution. The terms | 
|---|
| 12 | * are also available at https://curl.haxx.se/docs/copyright.html. | 
|---|
| 13 | * | 
|---|
| 14 | * You may opt to use, copy, modify, merge, publish, distribute and/or sell | 
|---|
| 15 | * copies of the Software, and permit persons to whom the Software is | 
|---|
| 16 | * furnished to do so, under the terms of the COPYING file. | 
|---|
| 17 | * | 
|---|
| 18 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | 
|---|
| 19 | * KIND, either express or implied. | 
|---|
| 20 | * | 
|---|
| 21 | ***************************************************************************/ | 
|---|
| 22 |  | 
|---|
| 23 | #include "curl_setup.h" | 
|---|
| 24 | #include "socketpair.h" | 
|---|
| 25 |  | 
|---|
| 26 | #ifndef HAVE_SOCKETPAIR | 
|---|
| 27 | #ifdef WIN32 | 
|---|
| 28 | /* | 
|---|
| 29 | * This is a socketpair() implementation for Windows. | 
|---|
| 30 | */ | 
|---|
| 31 | #include <string.h> | 
|---|
| 32 | #include <winsock2.h> | 
|---|
| 33 | #include <ws2tcpip.h> | 
|---|
| 34 | #include <windows.h> | 
|---|
| 35 | #include <io.h> | 
|---|
| 36 | #else | 
|---|
| 37 | #ifdef HAVE_NETDB_H | 
|---|
| 38 | #include <netdb.h> | 
|---|
| 39 | #endif | 
|---|
| 40 | #ifdef HAVE_NETINET_IN_H | 
|---|
| 41 | #include <netinet/in.h> /* IPPROTO_TCP */ | 
|---|
| 42 | #endif | 
|---|
| 43 | #ifndef INADDR_LOOPBACK | 
|---|
| 44 | #define INADDR_LOOPBACK 0x7f000001 | 
|---|
| 45 | #endif /* !INADDR_LOOPBACK */ | 
|---|
| 46 | #endif /* !WIN32 */ | 
|---|
| 47 |  | 
|---|
| 48 | /* The last 3 #include files should be in this order */ | 
|---|
| 49 | #include "curl_printf.h" | 
|---|
| 50 | #include "curl_memory.h" | 
|---|
| 51 | #include "memdebug.h" | 
|---|
| 52 |  | 
|---|
| 53 | int Curl_socketpair(int domain, int type, int protocol, | 
|---|
| 54 | curl_socket_t socks[2]) | 
|---|
| 55 | { | 
|---|
| 56 | union { | 
|---|
| 57 | struct sockaddr_in inaddr; | 
|---|
| 58 | struct sockaddr addr; | 
|---|
| 59 | } a; | 
|---|
| 60 | curl_socket_t listener; | 
|---|
| 61 | curl_socklen_t addrlen = sizeof(a.inaddr); | 
|---|
| 62 | int reuse = 1; | 
|---|
| 63 | char data[2][12]; | 
|---|
| 64 | ssize_t dlen; | 
|---|
| 65 | (void)domain; | 
|---|
| 66 | (void)type; | 
|---|
| 67 | (void)protocol; | 
|---|
| 68 |  | 
|---|
| 69 | listener = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); | 
|---|
| 70 | if(listener == CURL_SOCKET_BAD) | 
|---|
| 71 | return -1; | 
|---|
| 72 |  | 
|---|
| 73 | memset(&a, 0, sizeof(a)); | 
|---|
| 74 | a.inaddr.sin_family = AF_INET; | 
|---|
| 75 | a.inaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); | 
|---|
| 76 | a.inaddr.sin_port = 0; | 
|---|
| 77 |  | 
|---|
| 78 | socks[0] = socks[1] = CURL_SOCKET_BAD; | 
|---|
| 79 |  | 
|---|
| 80 | if(setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, | 
|---|
| 81 | (char *)&reuse, (curl_socklen_t)sizeof(reuse)) == -1) | 
|---|
| 82 | goto error; | 
|---|
| 83 | if(bind(listener, &a.addr, sizeof(a.inaddr)) == -1) | 
|---|
| 84 | goto error; | 
|---|
| 85 | if(getsockname(listener, &a.addr, &addrlen) == -1) | 
|---|
| 86 | goto error; | 
|---|
| 87 | if(listen(listener, 1) == -1) | 
|---|
| 88 | goto error; | 
|---|
| 89 | socks[0] = socket(AF_INET, SOCK_STREAM, 0); | 
|---|
| 90 | if(socks[0] == CURL_SOCKET_BAD) | 
|---|
| 91 | goto error; | 
|---|
| 92 | if(connect(socks[0], &a.addr, sizeof(a.inaddr)) == -1) | 
|---|
| 93 | goto error; | 
|---|
| 94 | socks[1] = accept(listener, NULL, NULL); | 
|---|
| 95 | if(socks[1] == CURL_SOCKET_BAD) | 
|---|
| 96 | goto error; | 
|---|
| 97 |  | 
|---|
| 98 | /* verify that nothing else connected */ | 
|---|
| 99 | msnprintf(data[0], sizeof(data[0]), "%p", socks); | 
|---|
| 100 | dlen = strlen(data[0]); | 
|---|
| 101 | if(swrite(socks[0], data[0], dlen) != dlen) | 
|---|
| 102 | goto error; | 
|---|
| 103 | if(sread(socks[1], data[1], sizeof(data[1])) != dlen) | 
|---|
| 104 | goto error; | 
|---|
| 105 | if(memcmp(data[0], data[1], dlen)) | 
|---|
| 106 | goto error; | 
|---|
| 107 |  | 
|---|
| 108 | sclose(listener); | 
|---|
| 109 | return 0; | 
|---|
| 110 |  | 
|---|
| 111 | error: | 
|---|
| 112 | sclose(listener); | 
|---|
| 113 | sclose(socks[0]); | 
|---|
| 114 | sclose(socks[1]); | 
|---|
| 115 | return -1; | 
|---|
| 116 | } | 
|---|
| 117 |  | 
|---|
| 118 | #endif /* ! HAVE_SOCKETPAIR */ | 
|---|
| 119 |  | 
|---|