hostares.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2009, 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 HAVE_LIMITS_H
  26. #include <limits.h>
  27. #endif
  28. #ifdef HAVE_SYS_SOCKET_H
  29. #include <sys/socket.h>
  30. #endif
  31. #ifdef HAVE_NETINET_IN_H
  32. #include <netinet/in.h>
  33. #endif
  34. #ifdef HAVE_NETDB_H
  35. #include <netdb.h>
  36. #endif
  37. #ifdef HAVE_ARPA_INET_H
  38. #include <arpa/inet.h>
  39. #endif
  40. #ifdef HAVE_STDLIB_H
  41. #include <stdlib.h> /* required for free() prototypes */
  42. #endif
  43. #ifdef HAVE_UNISTD_H
  44. #include <unistd.h> /* for the close() proto */
  45. #endif
  46. #ifdef VMS
  47. #include <in.h>
  48. #include <inet.h>
  49. #include <stdlib.h>
  50. #endif
  51. #ifdef HAVE_PROCESS_H
  52. #include <process.h>
  53. #endif
  54. #if (defined(NETWARE) && defined(__NOVELL_LIBC__))
  55. #undef in_addr_t
  56. #define in_addr_t unsigned long
  57. #endif
  58. #include "urldata.h"
  59. #include "sendf.h"
  60. #include "hostip.h"
  61. #include "hash.h"
  62. #include "share.h"
  63. #include "strerror.h"
  64. #include "url.h"
  65. #include "multiif.h"
  66. #include "inet_pton.h"
  67. #include "connect.h"
  68. #include "select.h"
  69. #define _MPRINTF_REPLACE /* use our functions only */
  70. #include <curl/mprintf.h>
  71. #include "curl_memory.h"
  72. /* The last #include file should be: */
  73. #include "memdebug.h"
  74. /***********************************************************************
  75. * Only for ares-enabled builds
  76. **********************************************************************/
  77. #ifdef CURLRES_ARES
  78. /*
  79. * Curl_resolv_fdset() is called when someone from the outside world (using
  80. * curl_multi_fdset()) wants to get our fd_set setup and we're talking with
  81. * ares. The caller must make sure that this function is only called when we
  82. * have a working ares channel.
  83. *
  84. * Returns: CURLE_OK always!
  85. */
  86. int Curl_resolv_getsock(struct connectdata *conn,
  87. curl_socket_t *socks,
  88. int numsocks)
  89. {
  90. struct timeval maxtime;
  91. struct timeval timebuf;
  92. struct timeval *timeout;
  93. int max = ares_getsock(conn->data->state.areschannel,
  94. (int *)socks, numsocks);
  95. maxtime.tv_sec = CURL_TIMEOUT_RESOLVE;
  96. maxtime.tv_usec = 0;
  97. timeout = ares_timeout(conn->data->state.areschannel, &maxtime, &timebuf);
  98. Curl_expire(conn->data,
  99. (timeout->tv_sec * 1000) + (timeout->tv_usec/1000));
  100. return max;
  101. }
  102. /*
  103. * ares_waitperform()
  104. *
  105. * 1) Ask ares what sockets it currently plays with, then
  106. * 2) wait for the timeout period to check for action on ares' sockets.
  107. * 3) tell ares to act on all the sockets marked as "with action"
  108. *
  109. * return number of sockets it worked on
  110. */
  111. static int ares_waitperform(struct connectdata *conn, int timeout_ms)
  112. {
  113. struct SessionHandle *data = conn->data;
  114. int nfds;
  115. int bitmask;
  116. int socks[ARES_GETSOCK_MAXNUM];
  117. struct pollfd pfd[ARES_GETSOCK_MAXNUM];
  118. int i;
  119. int num = 0;
  120. bitmask = ares_getsock(data->state.areschannel, socks, ARES_GETSOCK_MAXNUM);
  121. for(i=0; i < ARES_GETSOCK_MAXNUM; i++) {
  122. pfd[i].events = 0;
  123. pfd[i].revents = 0;
  124. if(ARES_GETSOCK_READABLE(bitmask, i)) {
  125. pfd[i].fd = socks[i];
  126. pfd[i].events |= POLLRDNORM|POLLIN;
  127. }
  128. if(ARES_GETSOCK_WRITABLE(bitmask, i)) {
  129. pfd[i].fd = socks[i];
  130. pfd[i].events |= POLLWRNORM|POLLOUT;
  131. }
  132. if(pfd[i].events != 0)
  133. num++;
  134. else
  135. break;
  136. }
  137. if(num)
  138. nfds = Curl_poll(pfd, num, timeout_ms);
  139. else
  140. nfds = 0;
  141. if(!nfds)
  142. /* Call ares_process() unconditonally here, even if we simply timed out
  143. above, as otherwise the ares name resolve won't timeout! */
  144. ares_process_fd(data->state.areschannel, ARES_SOCKET_BAD, ARES_SOCKET_BAD);
  145. else {
  146. /* move through the descriptors and ask for processing on them */
  147. for(i=0; i < num; i++)
  148. ares_process_fd(data->state.areschannel,
  149. pfd[i].revents & (POLLRDNORM|POLLIN)?
  150. pfd[i].fd:ARES_SOCKET_BAD,
  151. pfd[i].revents & (POLLWRNORM|POLLOUT)?
  152. pfd[i].fd:ARES_SOCKET_BAD);
  153. }
  154. return nfds;
  155. }
  156. /*
  157. * Curl_is_resolved() is called repeatedly to check if a previous name resolve
  158. * request has completed. It should also make sure to time-out if the
  159. * operation seems to take too long.
  160. *
  161. * Returns normal CURLcode errors.
  162. */
  163. CURLcode Curl_is_resolved(struct connectdata *conn,
  164. struct Curl_dns_entry **dns)
  165. {
  166. struct SessionHandle *data = conn->data;
  167. *dns = NULL;
  168. ares_waitperform(conn, 0);
  169. if(conn->async.done) {
  170. /* we're done, kill the ares handle */
  171. if(!conn->async.dns) {
  172. failf(data, "Could not resolve host: %s (%s)", conn->host.dispname,
  173. ares_strerror(conn->async.status));
  174. return CURLE_COULDNT_RESOLVE_HOST;
  175. }
  176. *dns = conn->async.dns;
  177. }
  178. return CURLE_OK;
  179. }
  180. /*
  181. * Curl_wait_for_resolv() waits for a resolve to finish. This function should
  182. * be avoided since using this risk getting the multi interface to "hang".
  183. *
  184. * If 'entry' is non-NULL, make it point to the resolved dns entry
  185. *
  186. * Returns CURLE_COULDNT_RESOLVE_HOST if the host was not resolved, and
  187. * CURLE_OPERATION_TIMEDOUT if a time-out occurred.
  188. */
  189. CURLcode Curl_wait_for_resolv(struct connectdata *conn,
  190. struct Curl_dns_entry **entry)
  191. {
  192. CURLcode rc=CURLE_OK;
  193. struct SessionHandle *data = conn->data;
  194. long timeout;
  195. struct timeval now = Curl_tvnow();
  196. /* now, see if there's a connect timeout or a regular timeout to
  197. use instead of the default one */
  198. if(conn->data->set.connecttimeout)
  199. timeout = conn->data->set.connecttimeout;
  200. else if(conn->data->set.timeout)
  201. timeout = conn->data->set.timeout;
  202. else
  203. timeout = CURL_TIMEOUT_RESOLVE * 1000; /* default name resolve timeout */
  204. /* Wait for the name resolve query to complete. */
  205. while(1) {
  206. struct timeval *tvp, tv, store;
  207. long timediff;
  208. int itimeout;
  209. itimeout = (timeout > (long)INT_MAX) ? INT_MAX : (int)timeout;
  210. store.tv_sec = itimeout/1000;
  211. store.tv_usec = (itimeout%1000)*1000;
  212. tvp = ares_timeout(data->state.areschannel, &store, &tv);
  213. /* use the timeout period ares returned to us above */
  214. ares_waitperform(conn, (int)(tvp->tv_sec * 1000 + tvp->tv_usec/1000));
  215. if(conn->async.done)
  216. break;
  217. timediff = Curl_tvdiff(Curl_tvnow(), now); /* spent time */
  218. timeout -= timediff?timediff:1; /* always deduct at least 1 */
  219. if(timeout < 0) {
  220. /* our timeout, so we cancel the ares operation */
  221. ares_cancel(data->state.areschannel);
  222. break;
  223. }
  224. }
  225. /* Operation complete, if the lookup was successful we now have the entry
  226. in the cache. */
  227. if(entry)
  228. *entry = conn->async.dns;
  229. if(!conn->async.dns) {
  230. /* a name was not resolved */
  231. if((timeout < 0) || (conn->async.status == ARES_ETIMEOUT)) {
  232. failf(data, "Resolving host timed out: %s", conn->host.dispname);
  233. rc = CURLE_COULDNT_RESOLVE_HOST;
  234. }
  235. else if(conn->async.done) {
  236. failf(data, "Could not resolve host: %s (%s)", conn->host.dispname,
  237. ares_strerror(conn->async.status));
  238. rc = CURLE_COULDNT_RESOLVE_HOST;
  239. }
  240. else
  241. rc = CURLE_OPERATION_TIMEDOUT;
  242. /* close the connection, since we can't return failure here without
  243. cleaning up this connection properly */
  244. conn->bits.close = TRUE;
  245. }
  246. return rc;
  247. }
  248. /*
  249. * Curl_getaddrinfo() - when using ares
  250. *
  251. * Returns name information about the given hostname and port number. If
  252. * successful, the 'hostent' is returned and the forth argument will point to
  253. * memory we need to free after use. That memory *MUST* be freed with
  254. * Curl_freeaddrinfo(), nothing else.
  255. */
  256. Curl_addrinfo *Curl_getaddrinfo(struct connectdata *conn,
  257. const char *hostname,
  258. int port,
  259. int *waitp)
  260. {
  261. char *bufp;
  262. struct SessionHandle *data = conn->data;
  263. struct in_addr in;
  264. int family = PF_INET;
  265. #ifdef ENABLE_IPV6 /* CURLRES_IPV6 */
  266. struct in6_addr in6;
  267. #endif /* CURLRES_IPV6 */
  268. *waitp = FALSE;
  269. /* First check if this is an IPv4 address string */
  270. if(Curl_inet_pton(AF_INET, hostname, &in) > 0) {
  271. /* This is a dotted IP address 123.123.123.123-style */
  272. return Curl_ip2addr(AF_INET, &in, hostname, port);
  273. }
  274. #ifdef ENABLE_IPV6 /* CURLRES_IPV6 */
  275. /* Otherwise, check if this is an IPv6 address string */
  276. if (Curl_inet_pton (AF_INET6, hostname, &in6) > 0) {
  277. /* This must be an IPv6 address literal. */
  278. return Curl_ip2addr(AF_INET6, &in6, hostname, port);
  279. }
  280. switch(data->set.ip_version) {
  281. default:
  282. #if ARES_VERSION >= 0x010601
  283. family = PF_UNSPEC; /* supported by c-ares since 1.6.1, so for older
  284. c-ares versions this just falls through and defaults
  285. to PF_INET */
  286. break;
  287. #endif
  288. case CURL_IPRESOLVE_V4:
  289. family = PF_INET;
  290. break;
  291. case CURL_IPRESOLVE_V6:
  292. family = PF_INET6;
  293. break;
  294. }
  295. #endif /* CURLRES_IPV6 */
  296. bufp = strdup(hostname);
  297. if(bufp) {
  298. Curl_safefree(conn->async.hostname);
  299. conn->async.hostname = bufp;
  300. conn->async.port = port;
  301. conn->async.done = FALSE; /* not done */
  302. conn->async.status = 0; /* clear */
  303. conn->async.dns = NULL; /* clear */
  304. /* areschannel is already setup in the Curl_open() function */
  305. ares_gethostbyname(data->state.areschannel, hostname, family,
  306. (ares_host_callback)Curl_addrinfo4_callback, conn);
  307. *waitp = TRUE; /* please wait for the response */
  308. }
  309. return NULL; /* no struct yet */
  310. }
  311. #endif /* CURLRES_ARES */