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
25 #include "curl_setup.h"
26
27 /***********************************************************************
28 * Only for IPv6-enabled builds
29 **********************************************************************/
30 #ifdef CURLRES_IPV6
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 #include "urldata.h"
47 #include "sendf.h"
48 #include "hostip.h"
49 #include "hash.h"
50 #include "share.h"
51 #include "url.h"
52 #include "inet_pton.h"
53 #include "connect.h"
54 /* The last 3 #include files should be in this order */
55 #include "curl_printf.h"
56 #include "curl_memory.h"
57 #include "memdebug.h"
58
59 /*
60 * Curl_ipvalid() checks what CURL_IPRESOLVE_* requirements that might've
61 * been set and returns TRUE if they are OK.
62 */
Curl_ipvalid(struct Curl_easy * data,struct connectdata * conn)63 bool Curl_ipvalid(struct Curl_easy *data, struct connectdata *conn)
64 {
65 if(conn->ip_version == CURL_IPRESOLVE_V6)
66 return Curl_ipv6works(data);
67
68 return TRUE;
69 }
70
71 #if defined(CURLRES_SYNCH)
72
73 #ifdef DEBUG_ADDRINFO
dump_addrinfo(const struct Curl_addrinfo * ai)74 static void dump_addrinfo(const struct Curl_addrinfo *ai)
75 {
76 printf("dump_addrinfo:\n");
77 for(; ai; ai = ai->ai_next) {
78 char buf[INET6_ADDRSTRLEN];
79 printf(" fam %2d, CNAME %s, ",
80 ai->ai_family, ai->ai_canonname ? ai->ai_canonname : "<none>");
81 Curl_printable_address(ai, buf, sizeof(buf));
82 printf("%s\n", buf);
83 }
84 }
85 #else
86 #define dump_addrinfo(x) Curl_nop_stmt
87 #endif
88
89 /*
90 * Curl_getaddrinfo() when built IPv6-enabled (non-threading and
91 * non-ares version).
92 *
93 * Returns name information about the given hostname and port number. If
94 * successful, the 'addrinfo' is returned and the fourth argument will point
95 * to memory we need to free after use. That memory *MUST* be freed with
96 * Curl_freeaddrinfo(), nothing else.
97 */
Curl_getaddrinfo(struct Curl_easy * data,const char * hostname,int port,int * waitp)98 struct Curl_addrinfo *Curl_getaddrinfo(struct Curl_easy *data,
99 const char *hostname,
100 int port,
101 int *waitp)
102 {
103 struct addrinfo hints;
104 struct Curl_addrinfo *res;
105 int error;
106 char sbuf[12];
107 char *sbufptr = NULL;
108 #ifndef USE_RESOLVE_ON_IPS
109 char addrbuf[128];
110 #endif
111 int pf = PF_INET;
112
113 *waitp = 0; /* synchronous response only */
114
115 if((data->conn->ip_version != CURL_IPRESOLVE_V4) && Curl_ipv6works(data))
116 /* The stack seems to be IPv6-enabled */
117 pf = PF_UNSPEC;
118
119 memset(&hints, 0, sizeof(hints));
120 hints.ai_family = pf;
121 hints.ai_socktype = (data->conn->transport == TRNSPRT_TCP) ?
122 SOCK_STREAM : SOCK_DGRAM;
123
124 #ifndef USE_RESOLVE_ON_IPS
125 /*
126 * The AI_NUMERICHOST must not be set to get synthesized IPv6 address from
127 * an IPv4 address on iOS and macOS.
128 */
129 if((1 == Curl_inet_pton(AF_INET, hostname, addrbuf)) ||
130 (1 == Curl_inet_pton(AF_INET6, hostname, addrbuf))) {
131 /* the given address is numerical only, prevent a reverse lookup */
132 hints.ai_flags = AI_NUMERICHOST;
133 }
134 #endif
135
136 if(port) {
137 msnprintf(sbuf, sizeof(sbuf), "%d", port);
138 sbufptr = sbuf;
139 }
140
141 error = Curl_getaddrinfo_ex(hostname, sbufptr, &hints, &res);
142 if(error) {
143 infof(data, "getaddrinfo(3) failed for %s:%d", hostname, port);
144 return NULL;
145 }
146
147 if(port) {
148 Curl_addrinfo_set_port(res, port);
149 }
150
151 dump_addrinfo(res);
152
153 return res;
154 }
155 #endif /* CURLRES_SYNCH */
156
157 #endif /* CURLRES_IPV6 */
158