socketpair.c 6.6 KB

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