Resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Utilities / cmcurl-7.19.0 / lib / hostasyn.c
blob2ebf0505b0f2aa4ca15ea8863f9049cea560c0b9
1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2007, Daniel Stenberg, <daniel@haxx.se>, et al.
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.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 * $Id: hostasyn.c,v 1.1.1.1 2008-09-23 16:32:05 hoffman Exp $
22 ***************************************************************************/
24 #include "setup.h"
26 #include <string.h>
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
55 #ifdef HAVE_SETJMP_H
56 #include <setjmp.h>
57 #endif
59 #ifdef HAVE_PROCESS_H
60 #include <process.h>
61 #endif
63 #include "urldata.h"
64 #include "sendf.h"
65 #include "hostip.h"
66 #include "hash.h"
67 #include "share.h"
68 #include "strerror.h"
69 #include "url.h"
71 #define _MPRINTF_REPLACE /* use our functions only */
72 #include <curl/mprintf.h>
74 #if defined(HAVE_INET_NTOA_R) && !defined(HAVE_INET_NTOA_R_DECL)
75 #include "inet_ntoa_r.h"
76 #endif
78 #include "memory.h"
79 /* The last #include file should be: */
80 #include "memdebug.h"
82 /***********************************************************************
83 * Only for builds using asynchronous name resolves
84 **********************************************************************/
85 #ifdef CURLRES_ASYNCH
87 * addrinfo_callback() gets called by ares, gethostbyname_thread() or
88 * getaddrinfo_thread() when we got the name resolved (or not!).
90 * If the status argument is CURL_ASYNC_SUCCESS, we might need to copy the
91 * address field since it might be freed when this function returns. This
92 * operation stores the resolved data in the DNS cache.
94 * NOTE: for IPv6 operations, Curl_addrinfo_copy() returns the same
95 * pointer it is given as argument!
97 * The storage operation locks and unlocks the DNS cache.
99 static CURLcode addrinfo_callback(void *arg, /* "struct connectdata *" */
100 int status,
101 void *addr)
103 struct connectdata *conn = (struct connectdata *)arg;
104 struct Curl_dns_entry *dns = NULL;
105 CURLcode rc = CURLE_OK;
107 conn->async.status = status;
109 if(CURL_ASYNC_SUCCESS == status) {
112 * IPv4/ares: Curl_addrinfo_copy() copies the address and returns an
113 * allocated version.
115 * IPv6: Curl_addrinfo_copy() returns the input pointer!
117 Curl_addrinfo *ai = Curl_addrinfo_copy(addr, conn->async.port);
118 if(ai) {
119 struct SessionHandle *data = conn->data;
121 if(data->share)
122 Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
124 dns = Curl_cache_addr(data, ai,
125 conn->async.hostname,
126 conn->async.port);
127 if(!dns) {
128 /* failed to store, cleanup and return error */
129 Curl_freeaddrinfo(ai);
130 rc = CURLE_OUT_OF_MEMORY;
133 if(data->share)
134 Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
136 else
137 rc = CURLE_OUT_OF_MEMORY;
140 conn->async.dns = dns;
142 /* Set async.done TRUE last in this function since it may be used multi-
143 threaded and once this is TRUE the other thread may read fields from the
144 async struct */
145 conn->async.done = TRUE;
147 /* ipv4: The input hostent struct will be freed by ares when we return from
148 this function */
149 return rc;
152 CURLcode Curl_addrinfo4_callback(void *arg, /* "struct connectdata *" */
153 int status,
154 #ifdef HAVE_CARES_CALLBACK_TIMEOUTS
155 int timeouts,
156 #endif
157 struct hostent *hostent)
159 #ifdef HAVE_CARES_CALLBACK_TIMEOUTS
160 (void)timeouts; /* ignored */
161 #endif
162 return addrinfo_callback(arg, status, hostent);
165 #ifdef CURLRES_IPV6
166 CURLcode Curl_addrinfo6_callback(void *arg, /* "struct connectdata *" */
167 int status,
168 #ifdef HAVE_CARES_CALLBACK_TIMEOUTS
169 int timeouts,
170 #endif
171 struct addrinfo *ai)
173 /* NOTE: for CURLRES_ARES, the 'ai' argument is really a
174 * 'struct hostent' pointer.
176 #ifdef HAVE_CARES_CALLBACK_TIMEOUTS
177 (void)timeouts; /* ignored */
178 #endif
179 return addrinfo_callback(arg, status, ai);
181 #endif
183 #endif /* CURLRES_ASYNC */