1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2021, 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 https://curl.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 /***********************************************************************
26 * Only for builds using asynchronous name resolves
27 **********************************************************************/
28 #ifdef CURLRES_ASYNCH
29
30 #ifdef HAVE_NETINET_IN_H
31 #include <netinet/in.h>
32 #endif
33 #ifdef HAVE_NETDB_H
34 #include <netdb.h>
35 #endif
36 #ifdef HAVE_ARPA_INET_H
37 #include <arpa/inet.h>
38 #endif
39 #ifdef __VMS
40 #include <in.h>
41 #include <inet.h>
42 #endif
43
44 #ifdef HAVE_PROCESS_H
45 #include <process.h>
46 #endif
47
48 #include "urldata.h"
49 #include "sendf.h"
50 #include "hostip.h"
51 #include "hash.h"
52 #include "share.h"
53 #include "url.h"
54 #include "curl_memory.h"
55 /* The last #include file should be: */
56 #include "memdebug.h"
57
58 /*
59 * Curl_addrinfo_callback() gets called by ares, gethostbyname_thread()
60 * or getaddrinfo_thread() when we got the name resolved (or not!).
61 *
62 * If the status argument is CURL_ASYNC_SUCCESS, this function takes
63 * ownership of the Curl_addrinfo passed, storing the resolved data
64 * in the DNS cache.
65 *
66 * The storage operation locks and unlocks the DNS cache.
67 */
Curl_addrinfo_callback(struct Curl_easy * data,int status,struct Curl_addrinfo * ai)68 CURLcode Curl_addrinfo_callback(struct Curl_easy *data,
69 int status,
70 struct Curl_addrinfo *ai)
71 {
72 struct Curl_dns_entry *dns = NULL;
73 CURLcode result = CURLE_OK;
74
75 data->state.async.status = status;
76
77 if(CURL_ASYNC_SUCCESS == status) {
78 if(ai) {
79 if(data->share)
80 Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
81
82 dns = Curl_cache_addr(data, ai,
83 data->state.async.hostname,
84 data->state.async.port);
85 if(data->share)
86 Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
87
88 if(!dns) {
89 /* failed to store, cleanup and return error */
90 Curl_freeaddrinfo(ai);
91 result = CURLE_OUT_OF_MEMORY;
92 }
93 }
94 else {
95 result = CURLE_OUT_OF_MEMORY;
96 }
97 }
98
99 data->state.async.dns = dns;
100
101 /* Set async.done TRUE last in this function since it may be used multi-
102 threaded and once this is TRUE the other thread may read fields from the
103 async struct */
104 data->state.async.done = TRUE;
105
106 /* IPv4: The input hostent struct will be freed by ares when we return from
107 this function */
108 return result;
109 }
110
111 /*
112 * Curl_getaddrinfo() is the generic low-level name resolve API within this
113 * source file. There are several versions of this function - for different
114 * name resolve layers (selected at build-time). They all take this same set
115 * of arguments
116 */
Curl_getaddrinfo(struct Curl_easy * data,const char * hostname,int port,int * waitp)117 struct Curl_addrinfo *Curl_getaddrinfo(struct Curl_easy *data,
118 const char *hostname,
119 int port,
120 int *waitp)
121 {
122 return Curl_resolver_getaddrinfo(data, hostname, port, waitp);
123 }
124
125 #endif /* CURLRES_ASYNCH */
126