Coverage Report

Created: 2026-03-12 06:35

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_trc.h"
48
#include "hostip.h"
49
#include "url.h"
50
#include "curlx/inet_pton.h"
51
#include "connect.h"
52
53
#ifdef CURLRES_SYNCH
54
55
#ifdef DEBUG_ADDRINFO
56
static void dump_addrinfo(const struct Curl_addrinfo *ai)
57
{
58
  curl_mprintf("dump_addrinfo:\n");
59
  for(; ai; ai = ai->ai_next) {
60
    char buf[INET6_ADDRSTRLEN];
61
    curl_mprintf("    fam %2d, CNAME %s, ",
62
                 ai->ai_family,
63
                 ai->ai_canonname ? ai->ai_canonname : "<none>");
64
    Curl_printable_address(ai, buf, sizeof(buf));
65
    curl_mprintf("%s\n", buf);
66
  }
67
}
68
#else
69
0
#define dump_addrinfo(x) Curl_nop_stmt
70
#endif
71
72
/*
73
 * Curl_sync_getaddrinfo() when built IPv6-enabled (non-threading and
74
 * non-ares version).
75
 *
76
 * Returns name information about the given hostname and port number. If
77
 * successful, the 'addrinfo' is returned and the fourth argument will point
78
 * to memory we need to free after use. That memory *MUST* be freed with
79
 * Curl_freeaddrinfo(), nothing else.
80
 */
81
struct Curl_addrinfo *Curl_sync_getaddrinfo(struct Curl_easy *data,
82
                                            const char *hostname,
83
                                            int port,
84
                                            int ip_version)
85
0
{
86
0
  struct addrinfo hints;
87
0
  struct Curl_addrinfo *res;
88
0
  int error;
89
0
  char sbuf[12];
90
0
  char *sbufptr = NULL;
91
0
#ifndef USE_RESOLVE_ON_IPS
92
0
  char addrbuf[128];
93
0
#endif
94
0
  int pf = PF_INET;
95
96
0
  if((ip_version != CURL_IPRESOLVE_V4) && Curl_ipv6works(data))
97
    /* The stack seems to be IPv6-enabled */
98
0
    pf = PF_UNSPEC;
99
100
0
  memset(&hints, 0, sizeof(hints));
101
0
  hints.ai_family = pf;
102
0
  hints.ai_socktype =
103
0
    (Curl_conn_get_transport(data, data->conn) == TRNSPRT_TCP) ?
104
0
    SOCK_STREAM : SOCK_DGRAM;
105
106
0
#ifndef USE_RESOLVE_ON_IPS
107
  /*
108
   * The AI_NUMERICHOST must not be set to get synthesized IPv6 address from
109
   * an IPv4 address on iOS and macOS.
110
   */
111
0
  if((curlx_inet_pton(AF_INET, hostname, addrbuf) == 1) ||
112
0
     (curlx_inet_pton(AF_INET6, hostname, addrbuf) == 1)) {
113
    /* the given address is numerical only, prevent a reverse lookup */
114
0
    hints.ai_flags = AI_NUMERICHOST;
115
0
  }
116
0
#endif
117
118
0
  if(port) {
119
0
    curl_msnprintf(sbuf, sizeof(sbuf), "%d", port);
120
0
    sbufptr = sbuf;
121
0
  }
122
123
0
  error = Curl_getaddrinfo_ex(hostname, sbufptr, &hints, &res);
124
0
  if(error) {
125
0
    infof(data, "getaddrinfo(3) failed for %s:%d", hostname, port);
126
0
    return NULL;
127
0
  }
128
129
0
  if(port) {
130
0
    Curl_addrinfo_set_port(res, port);
131
0
  }
132
133
0
  dump_addrinfo(res);
134
135
0
  return res;
136
0
}
137
#endif /* CURLRES_SYNCH */
138
139
#endif /* CURLRES_IPV6 */