asyn-base.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. #ifdef HAVE_NETINET_IN_H
  26. #include <netinet/in.h>
  27. #endif
  28. #ifdef HAVE_NETDB_H
  29. #include <netdb.h>
  30. #endif
  31. #ifdef HAVE_ARPA_INET_H
  32. #include <arpa/inet.h>
  33. #endif
  34. #ifdef __VMS
  35. #include <in.h>
  36. #include <inet.h>
  37. #endif
  38. #ifdef USE_ARES
  39. #include <ares.h>
  40. #include <ares_version.h> /* really old c-ares did not include this by
  41. itself */
  42. #endif
  43. #include "urldata.h"
  44. #include "asyn.h"
  45. #include "sendf.h"
  46. #include "hostip.h"
  47. #include "hash.h"
  48. #include "multiif.h"
  49. #include "select.h"
  50. #include "share.h"
  51. #include "url.h"
  52. #include "curl_memory.h"
  53. /* The last #include file should be: */
  54. #include "memdebug.h"
  55. /***********************************************************************
  56. * Only for builds using asynchronous name resolves
  57. **********************************************************************/
  58. #ifdef CURLRES_ASYNCH
  59. #ifdef USE_ARES
  60. #if ARES_VERSION < 0x010600
  61. #error "requires c-ares 1.6.0 or newer"
  62. #endif
  63. /*
  64. * Curl_ares_pollset() is called when the outside world (using
  65. * curl_multi_fdset()) wants to get our fd_set setup and we are talking with
  66. * ares. The caller must make sure that this function is only called when we
  67. * have a working ares channel.
  68. *
  69. * Returns: sockets-in-use-bitmap
  70. */
  71. CURLcode Curl_ares_pollset(struct Curl_easy *data,
  72. ares_channel channel,
  73. struct easy_pollset *ps)
  74. {
  75. struct timeval maxtime = { CURL_TIMEOUT_RESOLVE, 0 };
  76. struct timeval timebuf;
  77. curl_socket_t sockets[16]; /* ARES documented limit */
  78. unsigned int bitmap, i;
  79. struct timeval *timeout;
  80. timediff_t milli;
  81. CURLcode result = CURLE_OK;
  82. DEBUGASSERT(channel);
  83. if(!channel)
  84. return CURLE_FAILED_INIT;
  85. bitmap = ares_getsock(channel, (ares_socket_t *)sockets,
  86. CURL_ARRAYSIZE(sockets));
  87. for(i = 0; i < CURL_ARRAYSIZE(sockets); ++i) {
  88. int flags = 0;
  89. if(ARES_GETSOCK_READABLE(bitmap, i))
  90. flags |= CURL_POLL_IN;
  91. if(ARES_GETSOCK_WRITABLE(bitmap, i))
  92. flags |= CURL_POLL_OUT;
  93. if(!flags)
  94. break;
  95. result = Curl_pollset_change(data, ps, sockets[i], flags, 0);
  96. if(result)
  97. return result;
  98. }
  99. timeout = ares_timeout(channel, &maxtime, &timebuf);
  100. if(!timeout)
  101. timeout = &maxtime;
  102. milli = curlx_tvtoms(timeout);
  103. Curl_expire(data, milli, EXPIRE_ASYNC_NAME);
  104. return result;
  105. }
  106. /*
  107. * Curl_ares_perform()
  108. *
  109. * 1) Ask ares what sockets it currently plays with, then
  110. * 2) wait for the timeout period to check for action on ares' sockets.
  111. * 3) tell ares to act on all the sockets marked as "with action"
  112. *
  113. * return number of sockets it worked on, or -1 on error
  114. */
  115. int Curl_ares_perform(ares_channel channel,
  116. timediff_t timeout_ms)
  117. {
  118. int nfds;
  119. int bitmask;
  120. ares_socket_t socks[ARES_GETSOCK_MAXNUM];
  121. struct pollfd pfd[ARES_GETSOCK_MAXNUM];
  122. int i;
  123. int num = 0;
  124. if(!channel)
  125. return 0;
  126. bitmask = ares_getsock(channel, socks, ARES_GETSOCK_MAXNUM);
  127. for(i = 0; i < ARES_GETSOCK_MAXNUM; i++) {
  128. pfd[i].events = 0;
  129. pfd[i].revents = 0;
  130. if(ARES_GETSOCK_READABLE(bitmask, i)) {
  131. pfd[i].fd = socks[i];
  132. pfd[i].events |= POLLRDNORM|POLLIN;
  133. }
  134. if(ARES_GETSOCK_WRITABLE(bitmask, i)) {
  135. pfd[i].fd = socks[i];
  136. pfd[i].events |= POLLWRNORM|POLLOUT;
  137. }
  138. if(pfd[i].events)
  139. num++;
  140. else
  141. break;
  142. }
  143. if(num) {
  144. nfds = Curl_poll(pfd, (unsigned int)num, timeout_ms);
  145. if(nfds < 0)
  146. return -1;
  147. }
  148. else
  149. nfds = 0;
  150. if(!nfds)
  151. /* Call ares_process() unconditionally here, even if we simply timed out
  152. above, as otherwise the ares name resolve will not timeout! */
  153. ares_process_fd(channel, ARES_SOCKET_BAD, ARES_SOCKET_BAD);
  154. else {
  155. /* move through the descriptors and ask for processing on them */
  156. for(i = 0; i < num; i++)
  157. ares_process_fd(channel,
  158. (pfd[i].revents & (POLLRDNORM|POLLIN)) ?
  159. pfd[i].fd : ARES_SOCKET_BAD,
  160. (pfd[i].revents & (POLLWRNORM|POLLOUT)) ?
  161. pfd[i].fd : ARES_SOCKET_BAD);
  162. }
  163. return nfds;
  164. }
  165. #endif
  166. #endif /* CURLRES_ASYNCH */
  167. #ifdef USE_CURL_ASYNC
  168. #include "doh.h"
  169. void Curl_async_shutdown(struct Curl_easy *data)
  170. {
  171. #ifdef CURLRES_ARES
  172. Curl_async_ares_shutdown(data);
  173. #endif
  174. #ifdef CURLRES_THREADED
  175. Curl_async_thrdd_shutdown(data);
  176. #endif
  177. #ifndef CURL_DISABLE_DOH
  178. Curl_doh_cleanup(data);
  179. #endif
  180. Curl_safefree(data->state.async.hostname);
  181. }
  182. void Curl_async_destroy(struct Curl_easy *data)
  183. {
  184. #ifdef CURLRES_ARES
  185. Curl_async_ares_destroy(data);
  186. #endif
  187. #ifdef CURLRES_THREADED
  188. Curl_async_thrdd_destroy(data);
  189. #endif
  190. #ifndef CURL_DISABLE_DOH
  191. Curl_doh_cleanup(data);
  192. #endif
  193. Curl_safefree(data->state.async.hostname);
  194. }
  195. #endif /* USE_CURL_ASYNC */