ENH: typo
[cmake.git] / Utilities / cmcurl / hostasyn.c
blobfccecb04378b5a2465d1ac12f39690e03d752e1d
1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2006, 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.2 2007-03-15 19:22:13 andy 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_TYPES_H
32 #include <sys/types.h>
33 #endif
34 #ifdef HAVE_SYS_SOCKET_H
35 #include <sys/socket.h>
36 #endif
37 #ifdef HAVE_NETINET_IN_H
38 #include <netinet/in.h>
39 #endif
40 #ifdef HAVE_NETDB_H
41 #include <netdb.h>
42 #endif
43 #ifdef HAVE_ARPA_INET_H
44 #include <arpa/inet.h>
45 #endif
46 #ifdef HAVE_STDLIB_H
47 #include <stdlib.h> /* required for free() prototypes */
48 #endif
49 #ifdef HAVE_UNISTD_H
50 #include <unistd.h> /* for the close() proto */
51 #endif
52 #ifdef VMS
53 #include <in.h>
54 #include <inet.h>
55 #include <stdlib.h>
56 #endif
58 #ifdef HAVE_SETJMP_H
59 #include <setjmp.h>
60 #endif
62 #ifdef HAVE_PROCESS_H
63 #include <process.h>
64 #endif
66 #include "urldata.h"
67 #include "sendf.h"
68 #include "hostip.h"
69 #include "hash.h"
70 #include "share.h"
71 #include "strerror.h"
72 #include "url.h"
74 #define _MPRINTF_REPLACE /* use our functions only */
75 #include <curl/mprintf.h>
77 #if defined(HAVE_INET_NTOA_R) && !defined(HAVE_INET_NTOA_R_DECL)
78 #include "inet_ntoa_r.h"
79 #endif
81 #include "memory.h"
82 /* The last #include file should be: */
83 #include "memdebug.h"
85 /***********************************************************************
86 * Only for builds using asynchronous name resolves
87 **********************************************************************/
88 #ifdef CURLRES_ASYNCH
90 * addrinfo_callback() gets called by ares, gethostbyname_thread() or
91 * getaddrinfo_thread() when we got the name resolved (or not!).
93 * If the status argument is CURL_ASYNC_SUCCESS, we might need to copy the
94 * address field since it might be freed when this function returns. This
95 * operation stores the resolved data in the DNS cache.
97 * NOTE: for IPv6 operations, Curl_addrinfo_copy() returns the same
98 * pointer it is given as argument!
100 * The storage operation locks and unlocks the DNS cache.
102 static CURLcode addrinfo_callback(void *arg, /* "struct connectdata *" */
103 int status,
104 void *addr)
106 struct connectdata *conn = (struct connectdata *)arg;
107 struct Curl_dns_entry *dns = NULL;
108 CURLcode rc = CURLE_OK;
110 conn->async.status = status;
112 if(CURL_ASYNC_SUCCESS == status) {
115 * IPv4/ares: Curl_addrinfo_copy() copies the address and returns an
116 * allocated version.
118 * IPv6: Curl_addrinfo_copy() returns the input pointer!
120 Curl_addrinfo *ai = Curl_addrinfo_copy(addr, conn->async.port);
121 if(ai) {
122 struct SessionHandle *data = conn->data;
124 if(data->share)
125 Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
127 dns = Curl_cache_addr(data, ai,
128 conn->async.hostname,
129 conn->async.port);
130 if(!dns) {
131 /* failed to store, cleanup and return error */
132 Curl_freeaddrinfo(ai);
133 rc = CURLE_OUT_OF_MEMORY;
136 if(data->share)
137 Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
139 else
140 rc = CURLE_OUT_OF_MEMORY;
143 conn->async.dns = dns;
145 /* Set async.done TRUE last in this function since it may be used multi-
146 threaded and once this is TRUE the other thread may read fields from the
147 async struct */
148 conn->async.done = TRUE;
150 /* ipv4: The input hostent struct will be freed by ares when we return from
151 this function */
152 return rc;
155 CURLcode Curl_addrinfo4_callback(void *arg, /* "struct connectdata *" */
156 int status,
157 struct hostent *hostent)
159 return addrinfo_callback(arg, status, hostent);
162 #ifdef CURLRES_IPV6
163 CURLcode Curl_addrinfo6_callback(void *arg, /* "struct connectdata *" */
164 int status,
165 struct addrinfo *ai)
167 /* NOTE: for CURLRES_ARES, the 'ai' argument is really a
168 * 'struct hostent' pointer.
170 return addrinfo_callback(arg, status, ai);
172 #endif
174 #endif /* CURLRES_ASYNC */