| 1 | /*************************************************************************** |
| 2 | * _ _ ____ _ |
| 3 | * Project ___| | | | _ \| | |
| 4 | * / __| | | | |_) | | |
| 5 | * | (__| |_| | _ <| |___ |
| 6 | * \___|\___/|_| \_\_____| |
| 7 | * |
| 8 | * Copyright (C) 2019 - 2021, 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.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 | #if !defined(HAVE_SOCKETPAIR) && !defined(CURL_DISABLE_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 | #ifdef HAVE_ARPA_INET_H |
| 44 | #include <arpa/inet.h> |
| 45 | #endif |
| 46 | #ifndef INADDR_LOOPBACK |
| 47 | #define INADDR_LOOPBACK 0x7f000001 |
| 48 | #endif /* !INADDR_LOOPBACK */ |
| 49 | #endif /* !WIN32 */ |
| 50 | |
| 51 | #include "nonblock.h" /* for curlx_nonblock */ |
| 52 | #include "timeval.h" /* needed before select.h */ |
| 53 | #include "select.h" /* for Curl_poll */ |
| 54 | |
| 55 | /* The last 3 #include files should be in this order */ |
| 56 | #include "curl_printf.h" |
| 57 | #include "curl_memory.h" |
| 58 | #include "memdebug.h" |
| 59 | |
| 60 | int Curl_socketpair(int domain, int type, int protocol, |
| 61 | curl_socket_t socks[2]) |
| 62 | { |
| 63 | union { |
| 64 | struct sockaddr_in inaddr; |
| 65 | struct sockaddr addr; |
| 66 | } a, a2; |
| 67 | curl_socket_t listener; |
| 68 | curl_socklen_t addrlen = sizeof(a.inaddr); |
| 69 | int reuse = 1; |
| 70 | struct pollfd pfd[1]; |
| 71 | (void)domain; |
| 72 | (void)type; |
| 73 | (void)protocol; |
| 74 | |
| 75 | listener = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
| 76 | if(listener == CURL_SOCKET_BAD) |
| 77 | return -1; |
| 78 | |
| 79 | memset(&a, 0, sizeof(a)); |
| 80 | a.inaddr.sin_family = AF_INET; |
| 81 | a.inaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); |
| 82 | a.inaddr.sin_port = 0; |
| 83 | |
| 84 | socks[0] = socks[1] = CURL_SOCKET_BAD; |
| 85 | |
| 86 | if(setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, |
| 87 | (char *)&reuse, (curl_socklen_t)sizeof(reuse)) == -1) |
| 88 | goto error; |
| 89 | if(bind(listener, &a.addr, sizeof(a.inaddr)) == -1) |
| 90 | goto error; |
| 91 | if(getsockname(listener, &a.addr, &addrlen) == -1 || |
| 92 | addrlen < (int)sizeof(a.inaddr)) |
| 93 | goto error; |
| 94 | if(listen(listener, 1) == -1) |
| 95 | goto error; |
| 96 | socks[0] = socket(AF_INET, SOCK_STREAM, 0); |
| 97 | if(socks[0] == CURL_SOCKET_BAD) |
| 98 | goto error; |
| 99 | if(connect(socks[0], &a.addr, sizeof(a.inaddr)) == -1) |
| 100 | goto error; |
| 101 | |
| 102 | /* use non-blocking accept to make sure we don't block forever */ |
| 103 | if(curlx_nonblock(listener, TRUE) < 0) |
| 104 | goto error; |
| 105 | pfd[0].fd = listener; |
| 106 | pfd[0].events = POLLIN; |
| 107 | pfd[0].revents = 0; |
| 108 | (void)Curl_poll(pfd, 1, 10*1000); /* 10 seconds */ |
| 109 | socks[1] = accept(listener, NULL, NULL); |
| 110 | if(socks[1] == CURL_SOCKET_BAD) |
| 111 | goto error; |
| 112 | |
| 113 | /* verify that nothing else connected */ |
| 114 | addrlen = sizeof(a.inaddr); |
| 115 | if(getsockname(socks[0], &a.addr, &addrlen) == -1 || |
| 116 | addrlen < (int)sizeof(a.inaddr)) |
| 117 | goto error; |
| 118 | addrlen = sizeof(a2.inaddr); |
| 119 | if(getpeername(socks[1], &a2.addr, &addrlen) == -1 || |
| 120 | addrlen < (int)sizeof(a2.inaddr)) |
| 121 | goto error; |
| 122 | if(a.inaddr.sin_family != a2.inaddr.sin_family || |
| 123 | a.inaddr.sin_addr.s_addr != a2.inaddr.sin_addr.s_addr || |
| 124 | a.inaddr.sin_port != a2.inaddr.sin_port) |
| 125 | goto error; |
| 126 | |
| 127 | sclose(listener); |
| 128 | return 0; |
| 129 | |
| 130 | error: |
| 131 | sclose(listener); |
| 132 | sclose(socks[0]); |
| 133 | sclose(socks[1]); |
| 134 | return -1; |
| 135 | } |
| 136 | |
| 137 | #endif /* ! HAVE_SOCKETPAIR */ |
| 138 | |