Coverage Report

Created: 2026-04-29 07:01

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/CMake/Utilities/cmcurl/lib/hostip6.c
Line
Count
Source
1
/***************************************************************************
2
 *                                  _   _ ____  _
3
 *  Project                     ___| | | |  _ \| |
4
 *                             / __| | | | |_) | |
5
 *                            | (__| |_| |  _ <| |___
6
 *                             \___|\___/|_| \_\_____|
7
 *
8
 * Copyright (C) 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
 * SPDX-License-Identifier: curl
22
 *
23
 ***************************************************************************/
24
#include "curl_setup.h"
25
26
/***********************************************************************
27
 * Only for IPv6-enabled builds
28
 **********************************************************************/
29
#ifdef CURLRES_IPV6
30
31
#ifdef HAVE_NETINET_IN_H
32
#include <netinet/in.h>
33
#endif
34
#ifdef HAVE_NETDB_H
35
#include <netdb.h>
36
#endif
37
#ifdef HAVE_ARPA_INET_H
38
#include <arpa/inet.h>
39
#endif
40
#ifdef __VMS
41
#include <in.h>
42
#include <inet.h>
43
#endif
44
45
#include "urldata.h"
46
#include "cfilters.h"
47
#include "curl_addrinfo.h"
48
#include "curl_trc.h"
49
#include "hostip.h"
50
#include "url.h"
51
#include "curlx/inet_pton.h"
52
#include "connect.h"
53
54
#ifdef CURLRES_SYNCH
55
56
/*
57
 * Curl_sync_getaddrinfo() when built IPv6-enabled (non-threading and
58
 * non-ares version).
59
 *
60
 * Returns name information about the given hostname and port number. If
61
 * successful, the 'addrinfo' is returned and the fourth argument will point
62
 * to memory we need to free after use. That memory *MUST* be freed with
63
 * Curl_freeaddrinfo(), nothing else.
64
 */
65
struct Curl_addrinfo *Curl_sync_getaddrinfo(struct Curl_easy *data,
66
                                            const char *hostname,
67
                                            int port,
68
                                            int ip_version)
69
0
{
70
0
  struct addrinfo hints;
71
0
  struct Curl_addrinfo *res;
72
0
  int error;
73
0
  char sbuf[12];
74
0
  char *sbufptr = NULL;
75
0
#ifndef USE_RESOLVE_ON_IPS
76
0
  char addrbuf[128];
77
0
#endif
78
0
  int pf = PF_INET;
79
80
0
  if((ip_version != CURL_IPRESOLVE_V4) && Curl_ipv6works(data))
81
    /* The stack seems to be IPv6-enabled */
82
0
    pf = PF_UNSPEC;
83
84
0
  memset(&hints, 0, sizeof(hints));
85
0
  hints.ai_family = pf;
86
0
  hints.ai_socktype =
87
0
    (Curl_conn_get_transport(data, data->conn) == TRNSPRT_TCP) ?
88
0
    SOCK_STREAM : SOCK_DGRAM;
89
90
0
#ifndef USE_RESOLVE_ON_IPS
91
  /*
92
   * The AI_NUMERICHOST must not be set to get synthesized IPv6 address from
93
   * an IPv4 address on iOS and macOS.
94
   */
95
0
  if((curlx_inet_pton(AF_INET, hostname, addrbuf) == 1) ||
96
0
     (curlx_inet_pton(AF_INET6, hostname, addrbuf) == 1)) {
97
    /* the given address is numerical only, prevent a reverse lookup */
98
0
    hints.ai_flags = AI_NUMERICHOST;
99
0
  }
100
0
#endif
101
102
0
  if(port) {
103
0
    curl_msnprintf(sbuf, sizeof(sbuf), "%d", port);
104
0
    sbufptr = sbuf;
105
0
  }
106
107
0
  error = Curl_getaddrinfo_ex(hostname, sbufptr, &hints, &res);
108
0
  if(error) {
109
0
    infof(data, "getaddrinfo(3) failed for %s:%d", hostname, port);
110
0
    return NULL;
111
0
  }
112
113
0
  if(port) {
114
0
    Curl_addrinfo_set_port(res, port);
115
0
  }
116
117
0
  return res;
118
0
}
119
#endif /* CURLRES_SYNCH */
120
121
#endif /* CURLRES_IPV6 */