113498266Sopenharmony_ci/***************************************************************************
213498266Sopenharmony_ci *                                  _   _ ____  _
313498266Sopenharmony_ci *  Project                     ___| | | |  _ \| |
413498266Sopenharmony_ci *                             / __| | | | |_) | |
513498266Sopenharmony_ci *                            | (__| |_| |  _ <| |___
613498266Sopenharmony_ci *                             \___|\___/|_| \_\_____|
713498266Sopenharmony_ci *
813498266Sopenharmony_ci * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
913498266Sopenharmony_ci *
1013498266Sopenharmony_ci * This software is licensed as described in the file COPYING, which
1113498266Sopenharmony_ci * you should have received as part of this distribution. The terms
1213498266Sopenharmony_ci * are also available at https://curl.se/docs/copyright.html.
1313498266Sopenharmony_ci *
1413498266Sopenharmony_ci * You may opt to use, copy, modify, merge, publish, distribute and/or sell
1513498266Sopenharmony_ci * copies of the Software, and permit persons to whom the Software is
1613498266Sopenharmony_ci * furnished to do so, under the terms of the COPYING file.
1713498266Sopenharmony_ci *
1813498266Sopenharmony_ci * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
1913498266Sopenharmony_ci * KIND, either express or implied.
2013498266Sopenharmony_ci *
2113498266Sopenharmony_ci * SPDX-License-Identifier: curl
2213498266Sopenharmony_ci *
2313498266Sopenharmony_ci ***************************************************************************/
2413498266Sopenharmony_ci
2513498266Sopenharmony_ci#include "curl_setup.h"
2613498266Sopenharmony_ci
2713498266Sopenharmony_ci/***********************************************************************
2813498266Sopenharmony_ci * Only for builds using asynchronous name resolves
2913498266Sopenharmony_ci **********************************************************************/
3013498266Sopenharmony_ci#ifdef CURLRES_ASYNCH
3113498266Sopenharmony_ci
3213498266Sopenharmony_ci#ifdef HAVE_NETINET_IN_H
3313498266Sopenharmony_ci#include <netinet/in.h>
3413498266Sopenharmony_ci#endif
3513498266Sopenharmony_ci#ifdef HAVE_NETDB_H
3613498266Sopenharmony_ci#include <netdb.h>
3713498266Sopenharmony_ci#endif
3813498266Sopenharmony_ci#ifdef HAVE_ARPA_INET_H
3913498266Sopenharmony_ci#include <arpa/inet.h>
4013498266Sopenharmony_ci#endif
4113498266Sopenharmony_ci#ifdef __VMS
4213498266Sopenharmony_ci#include <in.h>
4313498266Sopenharmony_ci#include <inet.h>
4413498266Sopenharmony_ci#endif
4513498266Sopenharmony_ci
4613498266Sopenharmony_ci#include "urldata.h"
4713498266Sopenharmony_ci#include "sendf.h"
4813498266Sopenharmony_ci#include "hostip.h"
4913498266Sopenharmony_ci#include "hash.h"
5013498266Sopenharmony_ci#include "share.h"
5113498266Sopenharmony_ci#include "url.h"
5213498266Sopenharmony_ci#include "curl_memory.h"
5313498266Sopenharmony_ci/* The last #include file should be: */
5413498266Sopenharmony_ci#include "memdebug.h"
5513498266Sopenharmony_ci
5613498266Sopenharmony_ci/*
5713498266Sopenharmony_ci * Curl_addrinfo_callback() gets called by ares, gethostbyname_thread()
5813498266Sopenharmony_ci * or getaddrinfo_thread() when we got the name resolved (or not!).
5913498266Sopenharmony_ci *
6013498266Sopenharmony_ci * If the status argument is CURL_ASYNC_SUCCESS, this function takes
6113498266Sopenharmony_ci * ownership of the Curl_addrinfo passed, storing the resolved data
6213498266Sopenharmony_ci * in the DNS cache.
6313498266Sopenharmony_ci *
6413498266Sopenharmony_ci * The storage operation locks and unlocks the DNS cache.
6513498266Sopenharmony_ci */
6613498266Sopenharmony_ciCURLcode Curl_addrinfo_callback(struct Curl_easy *data,
6713498266Sopenharmony_ci                                int status,
6813498266Sopenharmony_ci                                struct Curl_addrinfo *ai)
6913498266Sopenharmony_ci{
7013498266Sopenharmony_ci  struct Curl_dns_entry *dns = NULL;
7113498266Sopenharmony_ci  CURLcode result = CURLE_OK;
7213498266Sopenharmony_ci
7313498266Sopenharmony_ci  data->state.async.status = status;
7413498266Sopenharmony_ci
7513498266Sopenharmony_ci  if(CURL_ASYNC_SUCCESS == status) {
7613498266Sopenharmony_ci    if(ai) {
7713498266Sopenharmony_ci      if(data->share)
7813498266Sopenharmony_ci        Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
7913498266Sopenharmony_ci
8013498266Sopenharmony_ci      dns = Curl_cache_addr(data, ai,
8113498266Sopenharmony_ci                            data->state.async.hostname, 0,
8213498266Sopenharmony_ci                            data->state.async.port);
8313498266Sopenharmony_ci      if(data->share)
8413498266Sopenharmony_ci        Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
8513498266Sopenharmony_ci
8613498266Sopenharmony_ci      if(!dns) {
8713498266Sopenharmony_ci        /* failed to store, cleanup and return error */
8813498266Sopenharmony_ci        Curl_freeaddrinfo(ai);
8913498266Sopenharmony_ci        result = CURLE_OUT_OF_MEMORY;
9013498266Sopenharmony_ci      }
9113498266Sopenharmony_ci    }
9213498266Sopenharmony_ci    else {
9313498266Sopenharmony_ci      result = CURLE_OUT_OF_MEMORY;
9413498266Sopenharmony_ci    }
9513498266Sopenharmony_ci  }
9613498266Sopenharmony_ci
9713498266Sopenharmony_ci  data->state.async.dns = dns;
9813498266Sopenharmony_ci
9913498266Sopenharmony_ci /* Set async.done TRUE last in this function since it may be used multi-
10013498266Sopenharmony_ci    threaded and once this is TRUE the other thread may read fields from the
10113498266Sopenharmony_ci    async struct */
10213498266Sopenharmony_ci  data->state.async.done = TRUE;
10313498266Sopenharmony_ci
10413498266Sopenharmony_ci  /* IPv4: The input hostent struct will be freed by ares when we return from
10513498266Sopenharmony_ci     this function */
10613498266Sopenharmony_ci  return result;
10713498266Sopenharmony_ci}
10813498266Sopenharmony_ci
10913498266Sopenharmony_ci/*
11013498266Sopenharmony_ci * Curl_getaddrinfo() is the generic low-level name resolve API within this
11113498266Sopenharmony_ci * source file. There are several versions of this function - for different
11213498266Sopenharmony_ci * name resolve layers (selected at build-time). They all take this same set
11313498266Sopenharmony_ci * of arguments
11413498266Sopenharmony_ci */
11513498266Sopenharmony_cistruct Curl_addrinfo *Curl_getaddrinfo(struct Curl_easy *data,
11613498266Sopenharmony_ci                                       const char *hostname,
11713498266Sopenharmony_ci                                       int port,
11813498266Sopenharmony_ci                                       int *waitp)
11913498266Sopenharmony_ci{
12013498266Sopenharmony_ci  return Curl_resolver_getaddrinfo(data, hostname, port, waitp);
12113498266Sopenharmony_ci}
12213498266Sopenharmony_ci
12313498266Sopenharmony_ci#endif /* CURLRES_ASYNCH */
124