socketpair.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <[email protected]>, 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. #ifdef USE_EVENTFD
  29. #include <sys/eventfd.h>
  30. int Curl_eventfd(curl_socket_t socks[2], bool nonblocking)
  31. {
  32. int efd = eventfd(0, nonblocking ? EFD_CLOEXEC | EFD_NONBLOCK : EFD_CLOEXEC);
  33. if(efd == -1) {
  34. socks[0] = socks[1] = CURL_SOCKET_BAD;
  35. return -1;
  36. }
  37. socks[0] = socks[1] = efd;
  38. return 0;
  39. }
  40. #elif defined(HAVE_PIPE)
  41. #ifdef HAVE_FCNTL
  42. #include <fcntl.h>
  43. #endif
  44. int Curl_pipe(curl_socket_t socks[2], bool nonblocking)
  45. {
  46. #ifdef HAVE_PIPE2
  47. int flags = nonblocking ? O_NONBLOCK | O_CLOEXEC : O_CLOEXEC;
  48. if(pipe2(socks, flags))
  49. return -1;
  50. #else
  51. if(pipe(socks))
  52. return -1;
  53. #ifdef HAVE_FCNTL
  54. if(fcntl(socks[0], F_SETFD, FD_CLOEXEC) ||
  55. fcntl(socks[1], F_SETFD, FD_CLOEXEC)) {
  56. close(socks[0]);
  57. close(socks[1]);
  58. socks[0] = socks[1] = CURL_SOCKET_BAD;
  59. return -1;
  60. }
  61. #endif
  62. if(nonblocking) {
  63. if(curlx_nonblock(socks[0], TRUE) < 0 ||
  64. curlx_nonblock(socks[1], TRUE) < 0) {
  65. close(socks[0]);
  66. close(socks[1]);
  67. socks[0] = socks[1] = CURL_SOCKET_BAD;
  68. return -1;
  69. }
  70. }
  71. #endif
  72. return 0;
  73. }
  74. #endif /* USE_EVENTFD */
  75. #ifndef CURL_DISABLE_SOCKETPAIR
  76. #ifdef HAVE_SOCKETPAIR
  77. #ifdef USE_SOCKETPAIR
  78. int Curl_socketpair(int domain, int type, int protocol,
  79. curl_socket_t socks[2], bool nonblocking)
  80. {
  81. #ifdef SOCK_NONBLOCK
  82. type = nonblocking ? type | SOCK_NONBLOCK : type;
  83. #endif
  84. if(CURL_SOCKETPAIR(domain, type, protocol, socks))
  85. return -1;
  86. #ifndef SOCK_NONBLOCK
  87. if(nonblocking) {
  88. if(curlx_nonblock(socks[0], TRUE) < 0 ||
  89. curlx_nonblock(socks[1], TRUE) < 0) {
  90. close(socks[0]);
  91. close(socks[1]);
  92. return -1;
  93. }
  94. }
  95. #endif
  96. return 0;
  97. }
  98. #endif /* USE_SOCKETPAIR */
  99. #else /* !HAVE_SOCKETPAIR */
  100. #ifdef _WIN32
  101. /*
  102. * This is a socketpair() implementation for Windows.
  103. */
  104. #include <string.h>
  105. #ifdef HAVE_IO_H
  106. #include <io.h>
  107. #endif
  108. #else
  109. #ifdef HAVE_NETDB_H
  110. #include <netdb.h>
  111. #endif
  112. #ifdef HAVE_NETINET_IN_H
  113. #include <netinet/in.h> /* IPPROTO_TCP */
  114. #endif
  115. #ifdef HAVE_ARPA_INET_H
  116. #include <arpa/inet.h>
  117. #endif
  118. #ifndef INADDR_LOOPBACK
  119. #define INADDR_LOOPBACK 0x7f000001
  120. #endif /* !INADDR_LOOPBACK */
  121. #endif /* !_WIN32 */
  122. #include "curlx/nonblock.h" /* for curlx_nonblock */
  123. #include "curlx/timeval.h" /* needed before select.h */
  124. #include "select.h" /* for Curl_poll */
  125. /* The last 2 #include files should be in this order */
  126. #include "curl_memory.h"
  127. #include "memdebug.h"
  128. int Curl_socketpair(int domain, int type, int protocol,
  129. curl_socket_t socks[2], bool nonblocking)
  130. {
  131. union {
  132. struct sockaddr_in inaddr;
  133. struct sockaddr addr;
  134. } a;
  135. curl_socket_t listener;
  136. curl_socklen_t addrlen = sizeof(a.inaddr);
  137. int reuse = 1;
  138. struct pollfd pfd[1];
  139. (void)domain;
  140. (void)type;
  141. (void)protocol;
  142. listener = CURL_SOCKET(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  143. if(listener == CURL_SOCKET_BAD)
  144. return -1;
  145. memset(&a, 0, sizeof(a));
  146. a.inaddr.sin_family = AF_INET;
  147. a.inaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  148. a.inaddr.sin_port = 0;
  149. socks[0] = socks[1] = CURL_SOCKET_BAD;
  150. #if defined(_WIN32) || defined(__CYGWIN__)
  151. /* do not set SO_REUSEADDR on Windows */
  152. (void)reuse;
  153. #ifdef SO_EXCLUSIVEADDRUSE
  154. {
  155. int exclusive = 1;
  156. if(setsockopt(listener, SOL_SOCKET, SO_EXCLUSIVEADDRUSE,
  157. (char *)&exclusive, (curl_socklen_t)sizeof(exclusive)) == -1)
  158. goto error;
  159. }
  160. #endif
  161. #else
  162. if(setsockopt(listener, SOL_SOCKET, SO_REUSEADDR,
  163. (char *)&reuse, (curl_socklen_t)sizeof(reuse)) == -1)
  164. goto error;
  165. #endif
  166. if(bind(listener, &a.addr, sizeof(a.inaddr)) == -1)
  167. goto error;
  168. if(getsockname(listener, &a.addr, &addrlen) == -1 ||
  169. addrlen < (int)sizeof(a.inaddr))
  170. goto error;
  171. if(listen(listener, 1) == -1)
  172. goto error;
  173. socks[0] = CURL_SOCKET(AF_INET, SOCK_STREAM, 0);
  174. if(socks[0] == CURL_SOCKET_BAD)
  175. goto error;
  176. if(connect(socks[0], &a.addr, sizeof(a.inaddr)) == -1)
  177. goto error;
  178. /* use non-blocking accept to make sure we do not block forever */
  179. if(curlx_nonblock(listener, TRUE) < 0)
  180. goto error;
  181. pfd[0].fd = listener;
  182. pfd[0].events = POLLIN;
  183. pfd[0].revents = 0;
  184. (void)Curl_poll(pfd, 1, 1000); /* one second */
  185. socks[1] = CURL_ACCEPT(listener, NULL, NULL);
  186. if(socks[1] == CURL_SOCKET_BAD)
  187. goto error;
  188. else {
  189. struct curltime start = curlx_now();
  190. char rnd[9];
  191. char check[sizeof(rnd)];
  192. char *p = &check[0];
  193. size_t s = sizeof(check);
  194. if(Curl_rand(NULL, (unsigned char *)rnd, sizeof(rnd)))
  195. goto error;
  196. /* write data to the socket */
  197. swrite(socks[0], rnd, sizeof(rnd));
  198. /* verify that we read the correct data */
  199. do {
  200. ssize_t nread;
  201. pfd[0].fd = socks[1];
  202. pfd[0].events = POLLIN;
  203. pfd[0].revents = 0;
  204. (void)Curl_poll(pfd, 1, 1000); /* one second */
  205. nread = sread(socks[1], p, s);
  206. if(nread == -1) {
  207. int sockerr = SOCKERRNO;
  208. /* Do not block forever */
  209. if(curlx_timediff(curlx_now(), start) > (60 * 1000))
  210. goto error;
  211. if(
  212. #ifdef USE_WINSOCK
  213. /* This is how Windows does it */
  214. (SOCKEWOULDBLOCK == sockerr)
  215. #else
  216. /* errno may be EWOULDBLOCK or on some systems EAGAIN when it
  217. returned due to its inability to send off data without
  218. blocking. We therefore treat both error codes the same here */
  219. (SOCKEWOULDBLOCK == sockerr) || (EAGAIN == sockerr) ||
  220. (SOCKEINTR == sockerr) || (SOCKEINPROGRESS == sockerr)
  221. #endif
  222. ) {
  223. continue;
  224. }
  225. goto error;
  226. }
  227. s -= nread;
  228. if(s) {
  229. p += nread;
  230. continue;
  231. }
  232. if(memcmp(rnd, check, sizeof(check)))
  233. goto error;
  234. break;
  235. } while(1);
  236. }
  237. if(nonblocking)
  238. if(curlx_nonblock(socks[0], TRUE) < 0 ||
  239. curlx_nonblock(socks[1], TRUE) < 0)
  240. goto error;
  241. sclose(listener);
  242. return 0;
  243. error:
  244. sclose(listener);
  245. sclose(socks[0]);
  246. sclose(socks[1]);
  247. return -1;
  248. }
  249. #endif
  250. #endif /* !CURL_DISABLE_SOCKETPAIR */