hostares.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2004, 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 http://curl.haxx.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. * $Id$
  22. ***************************************************************************/
  23. #include "setup.h"
  24. #include <string.h>
  25. #include <errno.h>
  26. #define _REENTRANT
  27. #if defined(WIN32) && !defined(__GNUC__) || defined(__MINGW32__)
  28. #include <malloc.h>
  29. #else
  30. #ifdef HAVE_SYS_TYPES_H
  31. #include <sys/types.h>
  32. #endif
  33. #ifdef HAVE_SYS_SOCKET_H
  34. #include <sys/socket.h>
  35. #endif
  36. #ifdef HAVE_NETINET_IN_H
  37. #include <netinet/in.h>
  38. #endif
  39. #ifdef HAVE_NETDB_H
  40. #include <netdb.h>
  41. #endif
  42. #ifdef HAVE_ARPA_INET_H
  43. #include <arpa/inet.h>
  44. #endif
  45. #ifdef HAVE_STDLIB_H
  46. #include <stdlib.h> /* required for free() prototypes */
  47. #endif
  48. #ifdef HAVE_UNISTD_H
  49. #include <unistd.h> /* for the close() proto */
  50. #endif
  51. #ifdef VMS
  52. #include <in.h>
  53. #include <inet.h>
  54. #include <stdlib.h>
  55. #endif
  56. #endif
  57. #ifdef HAVE_SETJMP_H
  58. #include <setjmp.h>
  59. #endif
  60. #ifdef WIN32
  61. #include <process.h>
  62. #endif
  63. #if (defined(NETWARE) && defined(__NOVELL_LIBC__))
  64. #undef in_addr_t
  65. #define in_addr_t unsigned long
  66. #endif
  67. #include "urldata.h"
  68. #include "sendf.h"
  69. #include "hostip.h"
  70. #include "hash.h"
  71. #include "share.h"
  72. #include "strerror.h"
  73. #include "url.h"
  74. #define _MPRINTF_REPLACE /* use our functions only */
  75. #include <curl/mprintf.h>
  76. #if defined(HAVE_INET_NTOA_R) && !defined(HAVE_INET_NTOA_R_DECL)
  77. #include "inet_ntoa_r.h"
  78. #endif
  79. #include "curl_memory.h"
  80. /* The last #include file should be: */
  81. #include "memdebug.h"
  82. /***********************************************************************
  83. * Only for ares-enabled builds
  84. **********************************************************************/
  85. #ifdef CURLRES_ARES
  86. /*
  87. * Curl_fdset() is called when someone from the outside world (using
  88. * curl_multi_fdset()) wants to get our fd_set setup and we're talking with
  89. * ares. The caller must make sure that this function is only called when we
  90. * have a working ares channel.
  91. *
  92. * Returns: CURLE_OK always!
  93. */
  94. CURLcode Curl_fdset(struct connectdata *conn,
  95. fd_set *read_fd_set,
  96. fd_set *write_fd_set,
  97. int *max_fdp)
  98. {
  99. int max = ares_fds(conn->data->state.areschannel,
  100. read_fd_set, write_fd_set);
  101. *max_fdp = max;
  102. return CURLE_OK;
  103. }
  104. /*
  105. * Curl_is_resolved() is called repeatedly to check if a previous name resolve
  106. * request has completed. It should also make sure to time-out if the
  107. * operation seems to take too long.
  108. *
  109. * Returns normal CURLcode errors.
  110. */
  111. CURLcode Curl_is_resolved(struct connectdata *conn,
  112. struct Curl_dns_entry **dns)
  113. {
  114. fd_set read_fds, write_fds;
  115. struct timeval tv={0,0};
  116. struct SessionHandle *data = conn->data;
  117. int nfds;
  118. FD_ZERO(&read_fds);
  119. FD_ZERO(&write_fds);
  120. nfds = ares_fds(data->state.areschannel, &read_fds, &write_fds);
  121. (void)select(nfds, &read_fds, &write_fds, NULL,
  122. (struct timeval *)&tv);
  123. /* Call ares_process() unconditonally here, even if we simply timed out
  124. above, as otherwise the ares name resolve won't timeout! */
  125. ares_process(data->state.areschannel, &read_fds, &write_fds);
  126. *dns = NULL;
  127. if(conn->async.done) {
  128. /* we're done, kill the ares handle */
  129. if(!conn->async.dns) {
  130. failf(data, "Could not resolve host: %s (%s)", conn->host.dispname,
  131. ares_strerror(conn->async.status));
  132. return CURLE_COULDNT_RESOLVE_HOST;
  133. }
  134. *dns = conn->async.dns;
  135. }
  136. return CURLE_OK;
  137. }
  138. /*
  139. * Curl_wait_for_resolv() waits for a resolve to finish. This function should
  140. * be avoided since using this risk getting the multi interface to "hang".
  141. *
  142. * If 'entry' is non-NULL, make it point to the resolved dns entry
  143. *
  144. * Returns CURLE_COULDNT_RESOLVE_HOST if the host was not resolved, and
  145. * CURLE_OPERATION_TIMEDOUT if a time-out occurred.
  146. */
  147. CURLcode Curl_wait_for_resolv(struct connectdata *conn,
  148. struct Curl_dns_entry **entry)
  149. {
  150. CURLcode rc=CURLE_OK;
  151. struct SessionHandle *data = conn->data;
  152. long timeout = CURL_TIMEOUT_RESOLVE; /* default name resolve timeout */
  153. /* now, see if there's a connect timeout or a regular timeout to
  154. use instead of the default one */
  155. if(conn->data->set.connecttimeout)
  156. timeout = conn->data->set.connecttimeout;
  157. else if(conn->data->set.timeout)
  158. timeout = conn->data->set.timeout;
  159. /* We convert the number of seconds into number of milliseconds here: */
  160. if(timeout < 2147483)
  161. /* maximum amount of seconds that can be multiplied with 1000 and
  162. still fit within 31 bits */
  163. timeout *= 1000;
  164. else
  165. timeout = 0x7fffffff; /* ridiculous amount of time anyway */
  166. /* Wait for the name resolve query to complete. */
  167. while (1) {
  168. int nfds=0;
  169. fd_set read_fds, write_fds;
  170. struct timeval *tvp, tv, store;
  171. int count;
  172. struct timeval now = Curl_tvnow();
  173. long timediff;
  174. store.tv_sec = (int)timeout/1000;
  175. store.tv_usec = (timeout%1000)*1000;
  176. FD_ZERO(&read_fds);
  177. FD_ZERO(&write_fds);
  178. nfds = ares_fds(data->state.areschannel, &read_fds, &write_fds);
  179. if (nfds == 0)
  180. /* no file descriptors means we're done waiting */
  181. break;
  182. tvp = ares_timeout(data->state.areschannel, &store, &tv);
  183. count = select(nfds, &read_fds, &write_fds, NULL, tvp);
  184. if (count < 0 && errno != EINVAL)
  185. break;
  186. ares_process(data->state.areschannel, &read_fds, &write_fds);
  187. timediff = Curl_tvdiff(Curl_tvnow(), now); /* spent time */
  188. timeout -= timediff?timediff:1; /* always deduct at least 1 */
  189. if (timeout < 0) {
  190. /* our timeout, so we cancel the ares operation */
  191. ares_cancel(data->state.areschannel);
  192. break;
  193. }
  194. }
  195. /* Operation complete, if the lookup was successful we now have the entry
  196. in the cache. */
  197. if(entry)
  198. *entry = conn->async.dns;
  199. if(!conn->async.dns) {
  200. /* a name was not resolved */
  201. if((timeout < 0) || (conn->async.status == ARES_ETIMEOUT)) {
  202. failf(data, "Resolving host timed out: %s", conn->host.dispname);
  203. rc = CURLE_OPERATION_TIMEDOUT;
  204. }
  205. else if(conn->async.done) {
  206. failf(data, "Could not resolve host: %s (%s)", conn->host.dispname,
  207. ares_strerror(conn->async.status));
  208. rc = CURLE_COULDNT_RESOLVE_HOST;
  209. }
  210. else
  211. rc = CURLE_OPERATION_TIMEDOUT;
  212. /* close the connection, since we can't return failure here without
  213. cleaning up this connection properly */
  214. Curl_disconnect(conn);
  215. }
  216. return rc;
  217. }
  218. /*
  219. * Curl_getaddrinfo() - when using ares
  220. *
  221. * Returns name information about the given hostname and port number. If
  222. * successful, the 'hostent' is returned and the forth argument will point to
  223. * memory we need to free after use. That memory *MUST* be freed with
  224. * Curl_freeaddrinfo(), nothing else.
  225. */
  226. Curl_addrinfo *Curl_getaddrinfo(struct connectdata *conn,
  227. char *hostname,
  228. int port,
  229. int *waitp)
  230. {
  231. char *bufp;
  232. struct SessionHandle *data = conn->data;
  233. in_addr_t in = inet_addr(hostname);
  234. *waitp = FALSE;
  235. if (in != CURL_INADDR_NONE) {
  236. /* This is a dotted IP address 123.123.123.123-style */
  237. return Curl_ip2addr(in, hostname, port);
  238. }
  239. bufp = strdup(hostname);
  240. if(bufp) {
  241. Curl_safefree(conn->async.hostname);
  242. conn->async.hostname = bufp;
  243. conn->async.port = port;
  244. conn->async.done = FALSE; /* not done */
  245. conn->async.status = 0; /* clear */
  246. conn->async.dns = NULL; /* clear */
  247. /* areschannel is already setup in the Curl_open() function */
  248. ares_gethostbyname(data->state.areschannel, hostname, PF_INET,
  249. Curl_addrinfo4_callback, conn);
  250. *waitp = TRUE; /* please wait for the response */
  251. }
  252. return NULL; /* no struct yet */
  253. }
  254. #endif /* CURLRES_ARES */