• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2015, 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  ***************************************************************************/
22 
23 #include "curl_setup.h"
24 
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 
39 #if defined(USE_THREADS_POSIX)
40 #  ifdef HAVE_PTHREAD_H
41 #    include <pthread.h>
42 #  endif
43 #elif defined(USE_THREADS_WIN32)
44 #  ifdef HAVE_PROCESS_H
45 #    include <process.h>
46 #  endif
47 #endif
48 
49 #if (defined(NETWARE) && defined(__NOVELL_LIBC__))
50 #undef in_addr_t
51 #define in_addr_t unsigned long
52 #endif
53 
54 #ifdef HAVE_GETADDRINFO
55 #  define RESOLVER_ENOMEM  EAI_MEMORY
56 #else
57 #  define RESOLVER_ENOMEM  ENOMEM
58 #endif
59 
60 #include "urldata.h"
61 #include "sendf.h"
62 #include "hostip.h"
63 #include "hash.h"
64 #include "share.h"
65 #include "strerror.h"
66 #include "url.h"
67 #include "multiif.h"
68 #include "inet_pton.h"
69 #include "inet_ntop.h"
70 #include "curl_threads.h"
71 #include "connect.h"
72 #include "curl_printf.h"
73 #include "curl_memory.h"
74 
75 /* The last #include file should be: */
76 #include "memdebug.h"
77 
78 /***********************************************************************
79  * Only for threaded name resolves builds
80  **********************************************************************/
81 #ifdef CURLRES_THREADED
82 
83 /*
84  * Curl_resolver_global_init()
85  * Called from curl_global_init() to initialize global resolver environment.
86  * Does nothing here.
87  */
Curl_resolver_global_init(void)88 int Curl_resolver_global_init(void)
89 {
90   return CURLE_OK;
91 }
92 
93 /*
94  * Curl_resolver_global_cleanup()
95  * Called from curl_global_cleanup() to destroy global resolver environment.
96  * Does nothing here.
97  */
Curl_resolver_global_cleanup(void)98 void Curl_resolver_global_cleanup(void)
99 {
100 }
101 
102 /*
103  * Curl_resolver_init()
104  * Called from curl_easy_init() -> Curl_open() to initialize resolver
105  * URL-state specific environment ('resolver' member of the UrlState
106  * structure).  Does nothing here.
107  */
Curl_resolver_init(void ** resolver)108 CURLcode Curl_resolver_init(void **resolver)
109 {
110   (void)resolver;
111   return CURLE_OK;
112 }
113 
114 /*
115  * Curl_resolver_cleanup()
116  * Called from curl_easy_cleanup() -> Curl_close() to cleanup resolver
117  * URL-state specific environment ('resolver' member of the UrlState
118  * structure).  Does nothing here.
119  */
Curl_resolver_cleanup(void * resolver)120 void Curl_resolver_cleanup(void *resolver)
121 {
122   (void)resolver;
123 }
124 
125 /*
126  * Curl_resolver_duphandle()
127  * Called from curl_easy_duphandle() to duplicate resolver URL state-specific
128  * environment ('resolver' member of the UrlState structure).  Does nothing
129  * here.
130  */
Curl_resolver_duphandle(void ** to,void * from)131 int Curl_resolver_duphandle(void **to, void *from)
132 {
133   (void)to;
134   (void)from;
135   return CURLE_OK;
136 }
137 
138 static void destroy_async_data(struct Curl_async *);
139 
140 /*
141  * Cancel all possibly still on-going resolves for this connection.
142  */
Curl_resolver_cancel(struct connectdata * conn)143 void Curl_resolver_cancel(struct connectdata *conn)
144 {
145   destroy_async_data(&conn->async);
146 }
147 
148 /* This function is used to init a threaded resolve */
149 static bool init_resolve_thread(struct connectdata *conn,
150                                 const char *hostname, int port,
151                                 const struct addrinfo *hints);
152 
153 
154 /* Data for synchronization between resolver thread and its parent */
155 struct thread_sync_data {
156   curl_mutex_t * mtx;
157   int done;
158 
159   char * hostname;        /* hostname to resolve, Curl_async.hostname
160                              duplicate */
161   int port;
162   int sock_error;
163   Curl_addrinfo *res;
164 #ifdef HAVE_GETADDRINFO
165   struct addrinfo hints;
166 #endif
167   struct thread_data *td; /* for thread-self cleanup */
168 };
169 
170 struct thread_data {
171   curl_thread_t thread_hnd;
172   unsigned int poll_interval;
173   long interval_end;
174   struct thread_sync_data tsd;
175 };
176 
conn_thread_sync_data(struct connectdata * conn)177 static struct thread_sync_data *conn_thread_sync_data(struct connectdata *conn)
178 {
179   return &(((struct thread_data *)conn->async.os_specific)->tsd);
180 }
181 
182 #define CONN_THREAD_SYNC_DATA(conn) &(((conn)->async.os_specific)->tsd);
183 
184 /* Destroy resolver thread synchronization data */
185 static
destroy_thread_sync_data(struct thread_sync_data * tsd)186 void destroy_thread_sync_data(struct thread_sync_data * tsd)
187 {
188   if(tsd->mtx) {
189     Curl_mutex_destroy(tsd->mtx);
190     free(tsd->mtx);
191   }
192 
193   free(tsd->hostname);
194 
195   if(tsd->res)
196     Curl_freeaddrinfo(tsd->res);
197 
198   memset(tsd, 0, sizeof(*tsd));
199 }
200 
201 /* Initialize resolver thread synchronization data */
202 static
init_thread_sync_data(struct thread_data * td,const char * hostname,int port,const struct addrinfo * hints)203 int init_thread_sync_data(struct thread_data * td,
204                            const char * hostname,
205                            int port,
206                            const struct addrinfo *hints)
207 {
208   struct thread_sync_data *tsd = &td->tsd;
209 
210   memset(tsd, 0, sizeof(*tsd));
211 
212   tsd->td = td;
213   tsd->port = port;
214 #ifdef HAVE_GETADDRINFO
215   DEBUGASSERT(hints);
216   tsd->hints = *hints;
217 #else
218   (void) hints;
219 #endif
220 
221   tsd->mtx = malloc(sizeof(curl_mutex_t));
222   if(tsd->mtx == NULL)
223     goto err_exit;
224 
225   Curl_mutex_init(tsd->mtx);
226 
227   tsd->sock_error = CURL_ASYNC_SUCCESS;
228 
229   /* Copying hostname string because original can be destroyed by parent
230    * thread during gethostbyname execution.
231    */
232   tsd->hostname = strdup(hostname);
233   if(!tsd->hostname)
234     goto err_exit;
235 
236   return 1;
237 
238  err_exit:
239   /* Memory allocation failed */
240   destroy_thread_sync_data(tsd);
241   return 0;
242 }
243 
getaddrinfo_complete(struct connectdata * conn)244 static int getaddrinfo_complete(struct connectdata *conn)
245 {
246   struct thread_sync_data *tsd = conn_thread_sync_data(conn);
247   int rc;
248 
249   rc = Curl_addrinfo_callback(conn, tsd->sock_error, tsd->res);
250   /* The tsd->res structure has been copied to async.dns and perhaps the DNS
251      cache.  Set our copy to NULL so destroy_thread_sync_data doesn't free it.
252   */
253   tsd->res = NULL;
254 
255   return rc;
256 }
257 
258 
259 #ifdef HAVE_GETADDRINFO
260 
261 /*
262  * getaddrinfo_thread() resolves a name and then exits.
263  *
264  * For builds without ARES, but with ENABLE_IPV6, create a resolver thread
265  * and wait on it.
266  */
getaddrinfo_thread(void * arg)267 static unsigned int CURL_STDCALL getaddrinfo_thread (void *arg)
268 {
269   struct thread_sync_data *tsd = (struct thread_sync_data*)arg;
270   struct thread_data *td = tsd->td;
271   char service[12];
272   int rc;
273 
274   snprintf(service, sizeof(service), "%d", tsd->port);
275 
276   rc = Curl_getaddrinfo_ex(tsd->hostname, service, &tsd->hints, &tsd->res);
277 
278   if(rc != 0) {
279     tsd->sock_error = SOCKERRNO?SOCKERRNO:rc;
280     if(tsd->sock_error == 0)
281       tsd->sock_error = RESOLVER_ENOMEM;
282   }
283 
284   Curl_mutex_acquire(tsd->mtx);
285   if(tsd->done) {
286     /* too late, gotta clean up the mess */
287     Curl_mutex_release(tsd->mtx);
288     destroy_thread_sync_data(tsd);
289     free(td);
290   }
291   else {
292     tsd->done = 1;
293     Curl_mutex_release(tsd->mtx);
294   }
295 
296   return 0;
297 }
298 
299 #else /* HAVE_GETADDRINFO */
300 
301 /*
302  * gethostbyname_thread() resolves a name and then exits.
303  */
gethostbyname_thread(void * arg)304 static unsigned int CURL_STDCALL gethostbyname_thread (void *arg)
305 {
306   struct thread_sync_data *tsd = (struct thread_sync_data *)arg;
307   struct thread_data *td = tsd->td;
308 
309   tsd->res = Curl_ipv4_resolve_r(tsd->hostname, tsd->port);
310 
311   if(!tsd->res) {
312     tsd->sock_error = SOCKERRNO;
313     if(tsd->sock_error == 0)
314       tsd->sock_error = RESOLVER_ENOMEM;
315   }
316 
317   Curl_mutex_acquire(tsd->mtx);
318   if(tsd->done) {
319     /* too late, gotta clean up the mess */
320     Curl_mutex_release(tsd->mtx);
321     destroy_thread_sync_data(tsd);
322     free(td);
323   }
324   else {
325     tsd->done = 1;
326     Curl_mutex_release(tsd->mtx);
327   }
328 
329   return 0;
330 }
331 
332 #endif /* HAVE_GETADDRINFO */
333 
334 /*
335  * destroy_async_data() cleans up async resolver data and thread handle.
336  */
destroy_async_data(struct Curl_async * async)337 static void destroy_async_data (struct Curl_async *async)
338 {
339   if(async->os_specific) {
340     struct thread_data *td = (struct thread_data*) async->os_specific;
341     int done;
342 
343     /*
344      * if the thread is still blocking in the resolve syscall, detach it and
345      * let the thread do the cleanup...
346      */
347     Curl_mutex_acquire(td->tsd.mtx);
348     done = td->tsd.done;
349     td->tsd.done = 1;
350     Curl_mutex_release(td->tsd.mtx);
351 
352     if(!done) {
353       Curl_thread_destroy(td->thread_hnd);
354     }
355     else {
356       if(td->thread_hnd != curl_thread_t_null)
357         Curl_thread_join(&td->thread_hnd);
358 
359       destroy_thread_sync_data(&td->tsd);
360 
361       free(async->os_specific);
362     }
363   }
364   async->os_specific = NULL;
365 
366   free(async->hostname);
367   async->hostname = NULL;
368 }
369 
370 /*
371  * init_resolve_thread() starts a new thread that performs the actual
372  * resolve. This function returns before the resolve is done.
373  *
374  * Returns FALSE in case of failure, otherwise TRUE.
375  */
init_resolve_thread(struct connectdata * conn,const char * hostname,int port,const struct addrinfo * hints)376 static bool init_resolve_thread (struct connectdata *conn,
377                                  const char *hostname, int port,
378                                  const struct addrinfo *hints)
379 {
380   struct thread_data *td = calloc(1, sizeof(struct thread_data));
381   int err = RESOLVER_ENOMEM;
382 
383   conn->async.os_specific = (void*) td;
384   if(!td)
385     goto err_exit;
386 
387   conn->async.port = port;
388   conn->async.done = FALSE;
389   conn->async.status = 0;
390   conn->async.dns = NULL;
391   td->thread_hnd = curl_thread_t_null;
392 
393   if(!init_thread_sync_data(td, hostname, port, hints))
394     goto err_exit;
395 
396   free(conn->async.hostname);
397   conn->async.hostname = strdup(hostname);
398   if(!conn->async.hostname)
399     goto err_exit;
400 
401 #ifdef HAVE_GETADDRINFO
402   td->thread_hnd = Curl_thread_create(getaddrinfo_thread, &td->tsd);
403 #else
404   td->thread_hnd = Curl_thread_create(gethostbyname_thread, &td->tsd);
405 #endif
406 
407   if(!td->thread_hnd) {
408 #ifndef _WIN32_WCE
409     err = errno;
410 #endif
411     goto err_exit;
412   }
413 
414   return TRUE;
415 
416  err_exit:
417   destroy_async_data(&conn->async);
418 
419   SET_ERRNO(err);
420 
421   return FALSE;
422 }
423 
424 /*
425  * resolver_error() calls failf() with the appropriate message after a resolve
426  * error
427  */
428 
resolver_error(struct connectdata * conn)429 static CURLcode resolver_error(struct connectdata *conn)
430 {
431   const char *host_or_proxy;
432   CURLcode result;
433 
434   if(conn->bits.httpproxy) {
435     host_or_proxy = "proxy";
436     result = CURLE_COULDNT_RESOLVE_PROXY;
437   }
438   else {
439     host_or_proxy = "host";
440     result = CURLE_COULDNT_RESOLVE_HOST;
441   }
442 
443   failf(conn->data, "Could not resolve %s: %s", host_or_proxy,
444         conn->async.hostname);
445 
446   return result;
447 }
448 
449 /*
450  * Curl_resolver_wait_resolv()
451  *
452  * waits for a resolve to finish. This function should be avoided since using
453  * this risk getting the multi interface to "hang".
454  *
455  * If 'entry' is non-NULL, make it point to the resolved dns entry
456  *
457  * This is the version for resolves-in-a-thread.
458  */
Curl_resolver_wait_resolv(struct connectdata * conn,struct Curl_dns_entry ** entry)459 CURLcode Curl_resolver_wait_resolv(struct connectdata *conn,
460                                    struct Curl_dns_entry **entry)
461 {
462   struct thread_data   *td = (struct thread_data*) conn->async.os_specific;
463   CURLcode result = CURLE_OK;
464 
465   DEBUGASSERT(conn && td);
466 
467   /* wait for the thread to resolve the name */
468   if(Curl_thread_join(&td->thread_hnd))
469     result = getaddrinfo_complete(conn);
470   else
471     DEBUGASSERT(0);
472 
473   conn->async.done = TRUE;
474 
475   if(entry)
476     *entry = conn->async.dns;
477 
478   if(!conn->async.dns)
479     /* a name was not resolved, report error */
480     result = resolver_error(conn);
481 
482   destroy_async_data(&conn->async);
483 
484   if(!conn->async.dns)
485     connclose(conn, "asynch resolve failed");
486 
487   return result;
488 }
489 
490 /*
491  * Curl_resolver_is_resolved() is called repeatedly to check if a previous
492  * name resolve request has completed. It should also make sure to time-out if
493  * the operation seems to take too long.
494  */
Curl_resolver_is_resolved(struct connectdata * conn,struct Curl_dns_entry ** entry)495 CURLcode Curl_resolver_is_resolved(struct connectdata *conn,
496                                    struct Curl_dns_entry **entry)
497 {
498   struct SessionHandle *data = conn->data;
499   struct thread_data   *td = (struct thread_data*) conn->async.os_specific;
500   int done = 0;
501 
502   *entry = NULL;
503 
504   if(!td) {
505     DEBUGASSERT(td);
506     return CURLE_COULDNT_RESOLVE_HOST;
507   }
508 
509   Curl_mutex_acquire(td->tsd.mtx);
510   done = td->tsd.done;
511   Curl_mutex_release(td->tsd.mtx);
512 
513   if(done) {
514     getaddrinfo_complete(conn);
515 
516     if(!conn->async.dns) {
517       CURLcode result = resolver_error(conn);
518       destroy_async_data(&conn->async);
519       return result;
520     }
521     destroy_async_data(&conn->async);
522     *entry = conn->async.dns;
523   }
524   else {
525     /* poll for name lookup done with exponential backoff up to 250ms */
526     long elapsed = Curl_tvdiff(Curl_tvnow(), data->progress.t_startsingle);
527     if(elapsed < 0)
528       elapsed = 0;
529 
530     if(td->poll_interval == 0)
531       /* Start at 1ms poll interval */
532       td->poll_interval = 1;
533     else if(elapsed >= td->interval_end)
534       /* Back-off exponentially if last interval expired  */
535       td->poll_interval *= 2;
536 
537     if(td->poll_interval > 250)
538       td->poll_interval = 250;
539 
540     td->interval_end = elapsed + td->poll_interval;
541     Curl_expire(conn->data, td->poll_interval);
542   }
543 
544   return CURLE_OK;
545 }
546 
Curl_resolver_getsock(struct connectdata * conn,curl_socket_t * socks,int numsocks)547 int Curl_resolver_getsock(struct connectdata *conn,
548                           curl_socket_t *socks,
549                           int numsocks)
550 {
551   (void)conn;
552   (void)socks;
553   (void)numsocks;
554   return 0;
555 }
556 
557 #ifndef HAVE_GETADDRINFO
558 /*
559  * Curl_getaddrinfo() - for platforms without getaddrinfo
560  */
Curl_resolver_getaddrinfo(struct connectdata * conn,const char * hostname,int port,int * waitp)561 Curl_addrinfo *Curl_resolver_getaddrinfo(struct connectdata *conn,
562                                          const char *hostname,
563                                          int port,
564                                          int *waitp)
565 {
566   struct in_addr in;
567 
568   *waitp = 0; /* default to synchronous response */
569 
570   if(Curl_inet_pton(AF_INET, hostname, &in) > 0)
571     /* This is a dotted IP address 123.123.123.123-style */
572     return Curl_ip2addr(AF_INET, &in, hostname, port);
573 
574   /* fire up a new resolver thread! */
575   if(init_resolve_thread(conn, hostname, port, NULL)) {
576     *waitp = 1; /* expect asynchronous response */
577     return NULL;
578   }
579 
580   /* fall-back to blocking version */
581   return Curl_ipv4_resolve_r(hostname, port);
582 }
583 
584 #else /* !HAVE_GETADDRINFO */
585 
586 /*
587  * Curl_resolver_getaddrinfo() - for getaddrinfo
588  */
Curl_resolver_getaddrinfo(struct connectdata * conn,const char * hostname,int port,int * waitp)589 Curl_addrinfo *Curl_resolver_getaddrinfo(struct connectdata *conn,
590                                          const char *hostname,
591                                          int port,
592                                          int *waitp)
593 {
594   struct addrinfo hints;
595   struct in_addr in;
596   Curl_addrinfo *res;
597   int error;
598   char sbuf[12];
599   int pf = PF_INET;
600 #ifdef CURLRES_IPV6
601   struct in6_addr in6;
602 #endif /* CURLRES_IPV6 */
603 
604   *waitp = 0; /* default to synchronous response */
605 
606   /* First check if this is an IPv4 address string */
607   if(Curl_inet_pton(AF_INET, hostname, &in) > 0)
608     /* This is a dotted IP address 123.123.123.123-style */
609     return Curl_ip2addr(AF_INET, &in, hostname, port);
610 
611 #ifdef CURLRES_IPV6
612   /* check if this is an IPv6 address string */
613   if(Curl_inet_pton (AF_INET6, hostname, &in6) > 0)
614     /* This is an IPv6 address literal */
615     return Curl_ip2addr(AF_INET6, &in6, hostname, port);
616 
617   /*
618    * Check if a limited name resolve has been requested.
619    */
620   switch(conn->ip_version) {
621   case CURL_IPRESOLVE_V4:
622     pf = PF_INET;
623     break;
624   case CURL_IPRESOLVE_V6:
625     pf = PF_INET6;
626     break;
627   default:
628     pf = PF_UNSPEC;
629     break;
630   }
631 
632   if((pf != PF_INET) && !Curl_ipv6works())
633     /* The stack seems to be a non-IPv6 one */
634     pf = PF_INET;
635 
636 #endif /* CURLRES_IPV6 */
637 
638   memset(&hints, 0, sizeof(hints));
639   hints.ai_family = pf;
640   hints.ai_socktype = conn->socktype;
641 
642   snprintf(sbuf, sizeof(sbuf), "%d", port);
643 
644   /* fire up a new resolver thread! */
645   if(init_resolve_thread(conn, hostname, port, &hints)) {
646     *waitp = 1; /* expect asynchronous response */
647     return NULL;
648   }
649 
650   /* fall-back to blocking version */
651   infof(conn->data, "init_resolve_thread() failed for %s; %s\n",
652         hostname, Curl_strerror(conn, ERRNO));
653 
654   error = Curl_getaddrinfo_ex(hostname, sbuf, &hints, &res);
655   if(error) {
656     infof(conn->data, "getaddrinfo() failed for %s:%d; %s\n",
657           hostname, port, Curl_strerror(conn, SOCKERRNO));
658     return NULL;
659   }
660   return res;
661 }
662 
663 #endif /* !HAVE_GETADDRINFO */
664 
Curl_set_dns_servers(struct SessionHandle * data,char * servers)665 CURLcode Curl_set_dns_servers(struct SessionHandle *data,
666                               char *servers)
667 {
668   (void)data;
669   (void)servers;
670   return CURLE_NOT_BUILT_IN;
671 
672 }
673 
Curl_set_dns_interface(struct SessionHandle * data,const char * interf)674 CURLcode Curl_set_dns_interface(struct SessionHandle *data,
675                                 const char *interf)
676 {
677   (void)data;
678   (void)interf;
679   return CURLE_NOT_BUILT_IN;
680 }
681 
Curl_set_dns_local_ip4(struct SessionHandle * data,const char * local_ip4)682 CURLcode Curl_set_dns_local_ip4(struct SessionHandle *data,
683                                 const char *local_ip4)
684 {
685   (void)data;
686   (void)local_ip4;
687   return CURLE_NOT_BUILT_IN;
688 }
689 
Curl_set_dns_local_ip6(struct SessionHandle * data,const char * local_ip6)690 CURLcode Curl_set_dns_local_ip6(struct SessionHandle *data,
691                                 const char *local_ip6)
692 {
693   (void)data;
694   (void)local_ip6;
695   return CURLE_NOT_BUILT_IN;
696 }
697 
698 #endif /* CURLRES_THREADED */
699