hostares.c 8.7 KB

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