asyn-base.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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_getsock() 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. int Curl_ares_getsock(struct Curl_easy *data,
  72. ares_channel channel,
  73. curl_socket_t *socks)
  74. {
  75. struct timeval maxtime = { CURL_TIMEOUT_RESOLVE, 0 };
  76. struct timeval timebuf;
  77. int max = ares_getsock(channel,
  78. (ares_socket_t *)socks, MAX_SOCKSPEREASYHANDLE);
  79. struct timeval *timeout = ares_timeout(channel, &maxtime, &timebuf);
  80. timediff_t milli = curlx_tvtoms(timeout);
  81. Curl_expire(data, milli, EXPIRE_ASYNC_NAME);
  82. return max;
  83. }
  84. /*
  85. * Curl_ares_perform()
  86. *
  87. * 1) Ask ares what sockets it currently plays with, then
  88. * 2) wait for the timeout period to check for action on ares' sockets.
  89. * 3) tell ares to act on all the sockets marked as "with action"
  90. *
  91. * return number of sockets it worked on, or -1 on error
  92. */
  93. int Curl_ares_perform(ares_channel channel,
  94. timediff_t timeout_ms)
  95. {
  96. int nfds;
  97. int bitmask;
  98. ares_socket_t socks[ARES_GETSOCK_MAXNUM];
  99. struct pollfd pfd[ARES_GETSOCK_MAXNUM];
  100. int i;
  101. int num = 0;
  102. if(!channel)
  103. return 0;
  104. bitmask = ares_getsock(channel, socks, ARES_GETSOCK_MAXNUM);
  105. for(i = 0; i < ARES_GETSOCK_MAXNUM; i++) {
  106. pfd[i].events = 0;
  107. pfd[i].revents = 0;
  108. if(ARES_GETSOCK_READABLE(bitmask, i)) {
  109. pfd[i].fd = socks[i];
  110. pfd[i].events |= POLLRDNORM|POLLIN;
  111. }
  112. if(ARES_GETSOCK_WRITABLE(bitmask, i)) {
  113. pfd[i].fd = socks[i];
  114. pfd[i].events |= POLLWRNORM|POLLOUT;
  115. }
  116. if(pfd[i].events)
  117. num++;
  118. else
  119. break;
  120. }
  121. if(num) {
  122. nfds = Curl_poll(pfd, (unsigned int)num, timeout_ms);
  123. if(nfds < 0)
  124. return -1;
  125. }
  126. else
  127. nfds = 0;
  128. if(!nfds)
  129. /* Call ares_process() unconditionally here, even if we simply timed out
  130. above, as otherwise the ares name resolve will not timeout! */
  131. ares_process_fd(channel, ARES_SOCKET_BAD, ARES_SOCKET_BAD);
  132. else {
  133. /* move through the descriptors and ask for processing on them */
  134. for(i = 0; i < num; i++)
  135. ares_process_fd(channel,
  136. (pfd[i].revents & (POLLRDNORM|POLLIN)) ?
  137. pfd[i].fd : ARES_SOCKET_BAD,
  138. (pfd[i].revents & (POLLWRNORM|POLLOUT)) ?
  139. pfd[i].fd : ARES_SOCKET_BAD);
  140. }
  141. return nfds;
  142. }
  143. #endif
  144. #endif /* CURLRES_ASYNCH */
  145. #ifdef USE_CURL_ASYNC
  146. #include "doh.h"
  147. void Curl_async_shutdown(struct Curl_easy *data)
  148. {
  149. #ifdef CURLRES_ARES
  150. Curl_async_ares_shutdown(data);
  151. #endif
  152. #ifdef CURLRES_THREADED
  153. Curl_async_thrdd_shutdown(data);
  154. #endif
  155. #ifndef CURL_DISABLE_DOH
  156. Curl_doh_cleanup(data);
  157. #endif
  158. Curl_safefree(data->state.async.hostname);
  159. }
  160. void Curl_async_destroy(struct Curl_easy *data)
  161. {
  162. #ifdef CURLRES_ARES
  163. Curl_async_ares_destroy(data);
  164. #endif
  165. #ifdef CURLRES_THREADED
  166. Curl_async_thrdd_destroy(data);
  167. #endif
  168. #ifndef CURL_DISABLE_DOH
  169. Curl_doh_cleanup(data);
  170. #endif
  171. Curl_safefree(data->state.async.hostname);
  172. }
  173. #endif /* USE_CURL_ASYNC */