socketpair.c 5.5 KB

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