2
0

socketpair.c 6.8 KB

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