1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2005, 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.
22 ***************************************************************************/
31 #if defined(WIN32) && !defined(__GNUC__) || defined(__MINGW32__)
34 #ifdef HAVE_SYS_TYPES_H
35 #include <sys/types.h>
37 #ifdef HAVE_SYS_SOCKET_H
38 #include <sys/socket.h>
40 #ifdef HAVE_NETINET_IN_H
41 #include <netinet/in.h>
46 #ifdef HAVE_ARPA_INET_H
47 #include <arpa/inet.h>
50 #include <stdlib.h> /* required for free() prototypes */
53 #include <unistd.h> /* for the close() proto */
78 #define _MPRINTF_REPLACE /* use our functions only */
79 #include <curl/mprintf.h>
81 #if defined(HAVE_INET_NTOA_R) && !defined(HAVE_INET_NTOA_R_DECL)
82 #include "inet_ntoa_r.h"
86 /* The last #include file should be: */
89 /***********************************************************************
90 * Only for builds using asynchronous name resolves
91 **********************************************************************/
94 * addrinfo_callback() gets called by ares, gethostbyname_thread() or
95 * getaddrinfo_thread() when we got the name resolved (or not!).
97 * If the status argument is CURL_ASYNC_SUCCESS, we might need to copy the
98 * address field since it might be freed when this function returns. This
99 * operation stores the resolved data in the DNS cache.
101 * NOTE: for IPv6 operations, Curl_addrinfo_copy() returns the same
102 * pointer it is given as argument!
104 * The storage operation locks and unlocks the DNS cache.
106 static CURLcode addrinfo_callback(void *arg, /* "struct connectdata *" */
110 struct connectdata *conn = (struct connectdata *)arg;
111 struct Curl_dns_entry *dns = NULL;
112 CURLcode rc = CURLE_OK;
114 conn->async.status = status;
116 if(CURL_ASYNC_SUCCESS == status) {
119 * IPv4: Curl_addrinfo_copy() copies the address and returns an allocated
122 * IPv6: Curl_addrinfo_copy() returns the input pointer!
124 Curl_addrinfo *ai = Curl_addrinfo_copy(addr, conn->async.port);
126 struct SessionHandle *data = conn->data;
129 Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
131 dns = Curl_cache_addr(data, ai,
132 conn->async.hostname,
135 /* failed to store, cleanup and return error */
136 Curl_freeaddrinfo(ai);
137 rc = CURLE_OUT_OF_MEMORY;
141 Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
144 rc = CURLE_OUT_OF_MEMORY;
147 conn->async.dns = dns;
149 /* Set async.done TRUE last in this function since it may be used multi-
150 threaded and once this is TRUE the other thread may read fields from the
152 conn->async.done = TRUE;
154 /* ipv4: The input hostent struct will be freed by ares when we return from
159 CURLcode Curl_addrinfo4_callback(void *arg, /* "struct connectdata *" */
161 struct hostent *hostent)
163 return addrinfo_callback(arg, status, hostent);
167 CURLcode Curl_addrinfo6_callback(void *arg, /* "struct connectdata *" */
171 return addrinfo_callback(arg, status, ai);
175 #endif /* CURLRES_ASYNC */