socketpair.c 5.2 KB

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