1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2008, 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: hostasyn.c,v 1.22 2008-10-30 13:45:25 yangtse Exp $
22  ***************************************************************************/
23 
24 #include "setup.h"
25 
26 #include <string.h>
27 
28 #ifdef NEED_MALLOC_H
29 #include <malloc.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 
55 #ifdef HAVE_PROCESS_H
56 #include <process.h>
57 #endif
58 
59 #include "urldata.h"
60 #include "sendf.h"
61 #include "hostip.h"
62 #include "hash.h"
63 #include "share.h"
64 #include "strerror.h"
65 #include "url.h"
66 
67 #define _MPRINTF_REPLACE /* use our functions only */
68 #include <curl/mprintf.h>
69 
70 #include "memory.h"
71 /* The last #include file should be: */
72 #include "memdebug.h"
73 
74 /***********************************************************************
75  * Only for builds using asynchronous name resolves
76  **********************************************************************/
77 #ifdef CURLRES_ASYNCH
78 /*
79  * addrinfo_callback() gets called by ares, gethostbyname_thread() or
80  * getaddrinfo_thread() when we got the name resolved (or not!).
81  *
82  * If the status argument is CURL_ASYNC_SUCCESS, we might need to copy the
83  * address field since it might be freed when this function returns. This
84  * operation stores the resolved data in the DNS cache.
85  *
86  * NOTE: for IPv6 operations, Curl_addrinfo_copy() returns the same
87  * pointer it is given as argument!
88  *
89  * The storage operation locks and unlocks the DNS cache.
90  */
addrinfo_callback(void * arg,int status,void * addr)91 static CURLcode addrinfo_callback(void *arg, /* "struct connectdata *" */
92                                   int status,
93                                   void *addr)
94 {
95   struct connectdata *conn = (struct connectdata *)arg;
96   struct Curl_dns_entry *dns = NULL;
97   CURLcode rc = CURLE_OK;
98 
99   conn->async.status = status;
100 
101   if(CURL_ASYNC_SUCCESS == status) {
102 
103     /*
104      * IPv4/ares: Curl_addrinfo_copy() copies the address and returns an
105      * allocated version.
106      *
107      * IPv6: Curl_addrinfo_copy() returns the input pointer!
108      */
109     Curl_addrinfo *ai = Curl_addrinfo_copy(addr, conn->async.port);
110     if(ai) {
111       struct SessionHandle *data = conn->data;
112 
113       if(data->share)
114         Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
115 
116       dns = Curl_cache_addr(data, ai,
117                             conn->async.hostname,
118                             conn->async.port);
119       if(!dns) {
120         /* failed to store, cleanup and return error */
121         Curl_freeaddrinfo(ai);
122         rc = CURLE_OUT_OF_MEMORY;
123       }
124 
125       if(data->share)
126         Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
127     }
128     else
129       rc = CURLE_OUT_OF_MEMORY;
130   }
131 
132   conn->async.dns = dns;
133 
134  /* Set async.done TRUE last in this function since it may be used multi-
135     threaded and once this is TRUE the other thread may read fields from the
136     async struct */
137   conn->async.done = TRUE;
138 
139   /* ipv4: The input hostent struct will be freed by ares when we return from
140      this function */
141   return rc;
142 }
143 
Curl_addrinfo4_callback(void * arg,int status,int timeouts,struct hostent * hostent)144 CURLcode Curl_addrinfo4_callback(void *arg, /* "struct connectdata *" */
145                                  int status,
146 #ifdef HAVE_CARES_CALLBACK_TIMEOUTS
147                                  int timeouts,
148 #endif
149                                  struct hostent *hostent)
150 {
151 #ifdef HAVE_CARES_CALLBACK_TIMEOUTS
152   (void)timeouts; /* ignored */
153 #endif
154   return addrinfo_callback(arg, status, hostent);
155 }
156 
157 #ifdef CURLRES_IPV6
Curl_addrinfo6_callback(void * arg,int status,int timeouts,Curl_addrinfo * ai)158 CURLcode Curl_addrinfo6_callback(void *arg, /* "struct connectdata *" */
159                                  int status,
160 #ifdef HAVE_CARES_CALLBACK_TIMEOUTS
161                                  int timeouts,
162 #endif
163                                  Curl_addrinfo *ai)
164 {
165  /* NOTE: for CURLRES_ARES, the 'ai' argument is really a
166   * 'struct hostent' pointer.
167   */
168 #ifdef HAVE_CARES_CALLBACK_TIMEOUTS
169   (void)timeouts; /* ignored */
170 #endif
171   return addrinfo_callback(arg, status, ai);
172 }
173 #endif
174 
175 #endif /* CURLRES_ASYNC */
176