socketpair.c 5.1 KB

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