113498266Sopenharmony_ci/*************************************************************************** 213498266Sopenharmony_ci * _ _ ____ _ 313498266Sopenharmony_ci * Project ___| | | | _ \| | 413498266Sopenharmony_ci * / __| | | | |_) | | 513498266Sopenharmony_ci * | (__| |_| | _ <| |___ 613498266Sopenharmony_ci * \___|\___/|_| \_\_____| 713498266Sopenharmony_ci * 813498266Sopenharmony_ci * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. 913498266Sopenharmony_ci * 1013498266Sopenharmony_ci * This software is licensed as described in the file COPYING, which 1113498266Sopenharmony_ci * you should have received as part of this distribution. The terms 1213498266Sopenharmony_ci * are also available at https://curl.se/docs/copyright.html. 1313498266Sopenharmony_ci * 1413498266Sopenharmony_ci * You may opt to use, copy, modify, merge, publish, distribute and/or sell 1513498266Sopenharmony_ci * copies of the Software, and permit persons to whom the Software is 1613498266Sopenharmony_ci * furnished to do so, under the terms of the COPYING file. 1713498266Sopenharmony_ci * 1813498266Sopenharmony_ci * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY 1913498266Sopenharmony_ci * KIND, either express or implied. 2013498266Sopenharmony_ci * 2113498266Sopenharmony_ci * SPDX-License-Identifier: curl 2213498266Sopenharmony_ci * 2313498266Sopenharmony_ci ***************************************************************************/ 2413498266Sopenharmony_ci 2513498266Sopenharmony_ci#include "curl_setup.h" 2613498266Sopenharmony_ci#include "socketpair.h" 2713498266Sopenharmony_ci#include "urldata.h" 2813498266Sopenharmony_ci#include "rand.h" 2913498266Sopenharmony_ci 3013498266Sopenharmony_ci#if !defined(HAVE_SOCKETPAIR) && !defined(CURL_DISABLE_SOCKETPAIR) 3113498266Sopenharmony_ci#ifdef _WIN32 3213498266Sopenharmony_ci/* 3313498266Sopenharmony_ci * This is a socketpair() implementation for Windows. 3413498266Sopenharmony_ci */ 3513498266Sopenharmony_ci#include <string.h> 3613498266Sopenharmony_ci#include <io.h> 3713498266Sopenharmony_ci#else 3813498266Sopenharmony_ci#ifdef HAVE_NETDB_H 3913498266Sopenharmony_ci#include <netdb.h> 4013498266Sopenharmony_ci#endif 4113498266Sopenharmony_ci#ifdef HAVE_NETINET_IN_H 4213498266Sopenharmony_ci#include <netinet/in.h> /* IPPROTO_TCP */ 4313498266Sopenharmony_ci#endif 4413498266Sopenharmony_ci#ifdef HAVE_ARPA_INET_H 4513498266Sopenharmony_ci#include <arpa/inet.h> 4613498266Sopenharmony_ci#endif 4713498266Sopenharmony_ci#ifndef INADDR_LOOPBACK 4813498266Sopenharmony_ci#define INADDR_LOOPBACK 0x7f000001 4913498266Sopenharmony_ci#endif /* !INADDR_LOOPBACK */ 5013498266Sopenharmony_ci#endif /* !_WIN32 */ 5113498266Sopenharmony_ci 5213498266Sopenharmony_ci#include "nonblock.h" /* for curlx_nonblock */ 5313498266Sopenharmony_ci#include "timeval.h" /* needed before select.h */ 5413498266Sopenharmony_ci#include "select.h" /* for Curl_poll */ 5513498266Sopenharmony_ci 5613498266Sopenharmony_ci/* The last 3 #include files should be in this order */ 5713498266Sopenharmony_ci#include "curl_printf.h" 5813498266Sopenharmony_ci#include "curl_memory.h" 5913498266Sopenharmony_ci#include "memdebug.h" 6013498266Sopenharmony_ci 6113498266Sopenharmony_ciint Curl_socketpair(int domain, int type, int protocol, 6213498266Sopenharmony_ci curl_socket_t socks[2]) 6313498266Sopenharmony_ci{ 6413498266Sopenharmony_ci union { 6513498266Sopenharmony_ci struct sockaddr_in inaddr; 6613498266Sopenharmony_ci struct sockaddr addr; 6713498266Sopenharmony_ci } a; 6813498266Sopenharmony_ci curl_socket_t listener; 6913498266Sopenharmony_ci curl_socklen_t addrlen = sizeof(a.inaddr); 7013498266Sopenharmony_ci int reuse = 1; 7113498266Sopenharmony_ci struct pollfd pfd[1]; 7213498266Sopenharmony_ci (void)domain; 7313498266Sopenharmony_ci (void)type; 7413498266Sopenharmony_ci (void)protocol; 7513498266Sopenharmony_ci 7613498266Sopenharmony_ci listener = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); 7713498266Sopenharmony_ci if(listener == CURL_SOCKET_BAD) 7813498266Sopenharmony_ci return -1; 7913498266Sopenharmony_ci 8013498266Sopenharmony_ci memset(&a, 0, sizeof(a)); 8113498266Sopenharmony_ci a.inaddr.sin_family = AF_INET; 8213498266Sopenharmony_ci a.inaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); 8313498266Sopenharmony_ci a.inaddr.sin_port = 0; 8413498266Sopenharmony_ci 8513498266Sopenharmony_ci socks[0] = socks[1] = CURL_SOCKET_BAD; 8613498266Sopenharmony_ci 8713498266Sopenharmony_ci#if defined(_WIN32) || defined(__CYGWIN__) 8813498266Sopenharmony_ci /* don't set SO_REUSEADDR on Windows */ 8913498266Sopenharmony_ci (void)reuse; 9013498266Sopenharmony_ci#ifdef SO_EXCLUSIVEADDRUSE 9113498266Sopenharmony_ci { 9213498266Sopenharmony_ci int exclusive = 1; 9313498266Sopenharmony_ci if(setsockopt(listener, SOL_SOCKET, SO_EXCLUSIVEADDRUSE, 9413498266Sopenharmony_ci (char *)&exclusive, (curl_socklen_t)sizeof(exclusive)) == -1) 9513498266Sopenharmony_ci goto error; 9613498266Sopenharmony_ci } 9713498266Sopenharmony_ci#endif 9813498266Sopenharmony_ci#else 9913498266Sopenharmony_ci if(setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, 10013498266Sopenharmony_ci (char *)&reuse, (curl_socklen_t)sizeof(reuse)) == -1) 10113498266Sopenharmony_ci goto error; 10213498266Sopenharmony_ci#endif 10313498266Sopenharmony_ci if(bind(listener, &a.addr, sizeof(a.inaddr)) == -1) 10413498266Sopenharmony_ci goto error; 10513498266Sopenharmony_ci if(getsockname(listener, &a.addr, &addrlen) == -1 || 10613498266Sopenharmony_ci addrlen < (int)sizeof(a.inaddr)) 10713498266Sopenharmony_ci goto error; 10813498266Sopenharmony_ci if(listen(listener, 1) == -1) 10913498266Sopenharmony_ci goto error; 11013498266Sopenharmony_ci socks[0] = socket(AF_INET, SOCK_STREAM, 0); 11113498266Sopenharmony_ci if(socks[0] == CURL_SOCKET_BAD) 11213498266Sopenharmony_ci goto error; 11313498266Sopenharmony_ci if(connect(socks[0], &a.addr, sizeof(a.inaddr)) == -1) 11413498266Sopenharmony_ci goto error; 11513498266Sopenharmony_ci 11613498266Sopenharmony_ci /* use non-blocking accept to make sure we don't block forever */ 11713498266Sopenharmony_ci if(curlx_nonblock(listener, TRUE) < 0) 11813498266Sopenharmony_ci goto error; 11913498266Sopenharmony_ci pfd[0].fd = listener; 12013498266Sopenharmony_ci pfd[0].events = POLLIN; 12113498266Sopenharmony_ci pfd[0].revents = 0; 12213498266Sopenharmony_ci (void)Curl_poll(pfd, 1, 1000); /* one second */ 12313498266Sopenharmony_ci socks[1] = accept(listener, NULL, NULL); 12413498266Sopenharmony_ci if(socks[1] == CURL_SOCKET_BAD) 12513498266Sopenharmony_ci goto error; 12613498266Sopenharmony_ci else { 12713498266Sopenharmony_ci struct curltime start = Curl_now(); 12813498266Sopenharmony_ci char rnd[9]; 12913498266Sopenharmony_ci char check[sizeof(rnd)]; 13013498266Sopenharmony_ci char *p = &check[0]; 13113498266Sopenharmony_ci size_t s = sizeof(check); 13213498266Sopenharmony_ci 13313498266Sopenharmony_ci if(Curl_rand(NULL, (unsigned char *)rnd, sizeof(rnd))) 13413498266Sopenharmony_ci goto error; 13513498266Sopenharmony_ci 13613498266Sopenharmony_ci /* write data to the socket */ 13713498266Sopenharmony_ci swrite(socks[0], rnd, sizeof(rnd)); 13813498266Sopenharmony_ci /* verify that we read the correct data */ 13913498266Sopenharmony_ci do { 14013498266Sopenharmony_ci ssize_t nread; 14113498266Sopenharmony_ci 14213498266Sopenharmony_ci pfd[0].fd = socks[1]; 14313498266Sopenharmony_ci pfd[0].events = POLLIN; 14413498266Sopenharmony_ci pfd[0].revents = 0; 14513498266Sopenharmony_ci (void)Curl_poll(pfd, 1, 1000); /* one second */ 14613498266Sopenharmony_ci 14713498266Sopenharmony_ci nread = sread(socks[1], p, s); 14813498266Sopenharmony_ci if(nread == -1) { 14913498266Sopenharmony_ci int sockerr = SOCKERRNO; 15013498266Sopenharmony_ci /* Don't block forever */ 15113498266Sopenharmony_ci if(Curl_timediff(Curl_now(), start) > (60 * 1000)) 15213498266Sopenharmony_ci goto error; 15313498266Sopenharmony_ci if( 15413498266Sopenharmony_ci#ifdef WSAEWOULDBLOCK 15513498266Sopenharmony_ci /* This is how Windows does it */ 15613498266Sopenharmony_ci (WSAEWOULDBLOCK == sockerr) 15713498266Sopenharmony_ci#else 15813498266Sopenharmony_ci /* errno may be EWOULDBLOCK or on some systems EAGAIN when it 15913498266Sopenharmony_ci returned due to its inability to send off data without 16013498266Sopenharmony_ci blocking. We therefore treat both error codes the same here */ 16113498266Sopenharmony_ci (EWOULDBLOCK == sockerr) || (EAGAIN == sockerr) || 16213498266Sopenharmony_ci (EINTR == sockerr) || (EINPROGRESS == sockerr) 16313498266Sopenharmony_ci#endif 16413498266Sopenharmony_ci ) { 16513498266Sopenharmony_ci continue; 16613498266Sopenharmony_ci } 16713498266Sopenharmony_ci goto error; 16813498266Sopenharmony_ci } 16913498266Sopenharmony_ci s -= nread; 17013498266Sopenharmony_ci if(s) { 17113498266Sopenharmony_ci p += nread; 17213498266Sopenharmony_ci continue; 17313498266Sopenharmony_ci } 17413498266Sopenharmony_ci if(memcmp(rnd, check, sizeof(check))) 17513498266Sopenharmony_ci goto error; 17613498266Sopenharmony_ci break; 17713498266Sopenharmony_ci } while(1); 17813498266Sopenharmony_ci } 17913498266Sopenharmony_ci 18013498266Sopenharmony_ci sclose(listener); 18113498266Sopenharmony_ci return 0; 18213498266Sopenharmony_ci 18313498266Sopenharmony_cierror: 18413498266Sopenharmony_ci sclose(listener); 18513498266Sopenharmony_ci sclose(socks[0]); 18613498266Sopenharmony_ci sclose(socks[1]); 18713498266Sopenharmony_ci return -1; 18813498266Sopenharmony_ci} 18913498266Sopenharmony_ci 19013498266Sopenharmony_ci#endif /* ! HAVE_SOCKETPAIR */ 191