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