socketpair.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 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. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "curl_setup.h"
  25. #include "socketpair.h"
  26. #include "urldata.h"
  27. #include "rand.h"
  28. #if !defined(HAVE_SOCKETPAIR) && !defined(CURL_DISABLE_SOCKETPAIR)
  29. #ifdef _WIN32
  30. /*
  31. * This is a socketpair() implementation for Windows.
  32. */
  33. #include <string.h>
  34. #include <io.h>
  35. #else
  36. #ifdef HAVE_NETDB_H
  37. #include <netdb.h>
  38. #endif
  39. #ifdef HAVE_NETINET_IN_H
  40. #include <netinet/in.h> /* IPPROTO_TCP */
  41. #endif
  42. #ifdef HAVE_ARPA_INET_H
  43. #include <arpa/inet.h>
  44. #endif
  45. #ifndef INADDR_LOOPBACK
  46. #define INADDR_LOOPBACK 0x7f000001
  47. #endif /* !INADDR_LOOPBACK */
  48. #endif /* !_WIN32 */
  49. #include "nonblock.h" /* for curlx_nonblock */
  50. #include "timeval.h" /* needed before select.h */
  51. #include "select.h" /* for Curl_poll */
  52. /* The last 3 #include files should be in this order */
  53. #include "curl_printf.h"
  54. #include "curl_memory.h"
  55. #include "memdebug.h"
  56. int Curl_socketpair(int domain, int type, int protocol,
  57. curl_socket_t socks[2])
  58. {
  59. union {
  60. struct sockaddr_in inaddr;
  61. struct sockaddr addr;
  62. } a;
  63. curl_socket_t listener;
  64. curl_socklen_t addrlen = sizeof(a.inaddr);
  65. int reuse = 1;
  66. struct pollfd pfd[1];
  67. (void)domain;
  68. (void)type;
  69. (void)protocol;
  70. listener = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  71. if(listener == CURL_SOCKET_BAD)
  72. return -1;
  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. socks[0] = socks[1] = CURL_SOCKET_BAD;
  78. #if defined(_WIN32) || defined(__CYGWIN__)
  79. /* don't set SO_REUSEADDR on Windows */
  80. (void)reuse;
  81. #ifdef SO_EXCLUSIVEADDRUSE
  82. {
  83. int exclusive = 1;
  84. if(setsockopt(listener, SOL_SOCKET, SO_EXCLUSIVEADDRUSE,
  85. (char *)&exclusive, (curl_socklen_t)sizeof(exclusive)) == -1)
  86. goto error;
  87. }
  88. #endif
  89. #else
  90. if(setsockopt(listener, SOL_SOCKET, SO_REUSEADDR,
  91. (char *)&reuse, (curl_socklen_t)sizeof(reuse)) == -1)
  92. goto error;
  93. #endif
  94. if(bind(listener, &a.addr, sizeof(a.inaddr)) == -1)
  95. goto error;
  96. if(getsockname(listener, &a.addr, &addrlen) == -1 ||
  97. addrlen < (int)sizeof(a.inaddr))
  98. goto error;
  99. if(listen(listener, 1) == -1)
  100. goto error;
  101. socks[0] = socket(AF_INET, SOCK_STREAM, 0);
  102. if(socks[0] == CURL_SOCKET_BAD)
  103. goto error;
  104. if(connect(socks[0], &a.addr, sizeof(a.inaddr)) == -1)
  105. goto error;
  106. /* use non-blocking accept to make sure we don't block forever */
  107. if(curlx_nonblock(listener, TRUE) < 0)
  108. goto error;
  109. pfd[0].fd = listener;
  110. pfd[0].events = POLLIN;
  111. pfd[0].revents = 0;
  112. (void)Curl_poll(pfd, 1, 1000); /* one second */
  113. socks[1] = accept(listener, NULL, NULL);
  114. if(socks[1] == CURL_SOCKET_BAD)
  115. goto error;
  116. else {
  117. struct curltime start = Curl_now();
  118. char rnd[9];
  119. char check[sizeof(rnd)];
  120. char *p = &check[0];
  121. size_t s = sizeof(check);
  122. if(Curl_rand(NULL, (unsigned char *)rnd, sizeof(rnd)))
  123. goto error;
  124. /* write data to the socket */
  125. swrite(socks[0], rnd, sizeof(rnd));
  126. /* verify that we read the correct data */
  127. do {
  128. ssize_t nread;
  129. pfd[0].fd = socks[1];
  130. pfd[0].events = POLLIN;
  131. pfd[0].revents = 0;
  132. (void)Curl_poll(pfd, 1, 1000); /* one second */
  133. nread = sread(socks[1], p, s);
  134. if(nread == -1) {
  135. int sockerr = SOCKERRNO;
  136. /* Don't block forever */
  137. if(Curl_timediff(Curl_now(), start) > (60 * 1000))
  138. goto error;
  139. if(
  140. #ifdef WSAEWOULDBLOCK
  141. /* This is how Windows does it */
  142. (WSAEWOULDBLOCK == sockerr)
  143. #else
  144. /* errno may be EWOULDBLOCK or on some systems EAGAIN when it
  145. returned due to its inability to send off data without
  146. blocking. We therefore treat both error codes the same here */
  147. (EWOULDBLOCK == sockerr) || (EAGAIN == sockerr) ||
  148. (EINTR == sockerr) || (EINPROGRESS == sockerr)
  149. #endif
  150. ) {
  151. continue;
  152. }
  153. goto error;
  154. }
  155. s -= nread;
  156. if(s) {
  157. p += nread;
  158. continue;
  159. }
  160. if(memcmp(rnd, check, sizeof(check)))
  161. goto error;
  162. break;
  163. } while(1);
  164. }
  165. sclose(listener);
  166. return 0;
  167. error:
  168. sclose(listener);
  169. sclose(socks[0]);
  170. sclose(socks[1]);
  171. return -1;
  172. }
  173. #endif /* ! HAVE_SOCKETPAIR */