1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2021, 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 ***************************************************************************/
22
23 #include "curl_setup.h"
24
25 /***********************************************************************
26 * Only for IPv6-enabled builds
27 **********************************************************************/
28 #ifdef CURLRES_IPV6
29
30 #ifdef HAVE_NETINET_IN_H
31 #include <netinet/in.h>
32 #endif
33 #ifdef HAVE_NETDB_H
34 #include <netdb.h>
35 #endif
36 #ifdef HAVE_ARPA_INET_H
37 #include <arpa/inet.h>
38 #endif
39 #ifdef __VMS
40 #include <in.h>
41 #include <inet.h>
42 #endif
43
44 #ifdef HAVE_PROCESS_H
45 #include <process.h>
46 #endif
47
48 #include "urldata.h"
49 #include "sendf.h"
50 #include "hostip.h"
51 #include "hash.h"
52 #include "share.h"
53 #include "strerror.h"
54 #include "url.h"
55 #include "inet_pton.h"
56 #include "connect.h"
57 /* The last 3 #include files should be in this order */
58 #include "curl_printf.h"
59 #include "curl_memory.h"
60 #include "memdebug.h"
61
62 /*
63 * Curl_ipv6works() returns TRUE if IPv6 seems to work.
64 */
Curl_ipv6works(struct Curl_easy * data)65 bool Curl_ipv6works(struct Curl_easy *data)
66 {
67 if(data) {
68 /* the nature of most system is that IPv6 status doesn't come and go
69 during a program's lifetime so we only probe the first time and then we
70 have the info kept for fast re-use */
71 DEBUGASSERT(data);
72 DEBUGASSERT(data->multi);
73 return data->multi->ipv6_works;
74 }
75 else {
76 int ipv6_works = -1;
77 /* probe to see if we have a working IPv6 stack */
78 curl_socket_t s = socket(PF_INET6, SOCK_DGRAM, 0);
79 if(s == CURL_SOCKET_BAD)
80 /* an IPv6 address was requested but we can't get/use one */
81 ipv6_works = 0;
82 else {
83 ipv6_works = 1;
84 sclose(s);
85 }
86 return (ipv6_works>0)?TRUE:FALSE;
87 }
88 }
89
90 /*
91 * Curl_ipvalid() checks what CURL_IPRESOLVE_* requirements that might've
92 * been set and returns TRUE if they are OK.
93 */
Curl_ipvalid(struct Curl_easy * data,struct connectdata * conn)94 bool Curl_ipvalid(struct Curl_easy *data, struct connectdata *conn)
95 {
96 if(conn->ip_version == CURL_IPRESOLVE_V6)
97 return Curl_ipv6works(data);
98
99 return TRUE;
100 }
101
102 #if defined(CURLRES_SYNCH)
103
104 #ifdef DEBUG_ADDRINFO
dump_addrinfo(struct connectdata * conn,const struct Curl_addrinfo * ai)105 static void dump_addrinfo(struct connectdata *conn,
106 const struct Curl_addrinfo *ai)
107 {
108 printf("dump_addrinfo:\n");
109 for(; ai; ai = ai->ai_next) {
110 char buf[INET6_ADDRSTRLEN];
111 printf(" fam %2d, CNAME %s, ",
112 ai->ai_family, ai->ai_canonname ? ai->ai_canonname : "<none>");
113 Curl_printable_address(ai, buf, sizeof(buf));
114 printf("%s\n", buf);
115 }
116 }
117 #else
118 #define dump_addrinfo(x,y) Curl_nop_stmt
119 #endif
120
121 /*
122 * Curl_getaddrinfo() when built IPv6-enabled (non-threading and
123 * non-ares version).
124 *
125 * Returns name information about the given hostname and port number. If
126 * successful, the 'addrinfo' is returned and the forth argument will point to
127 * memory we need to free after use. That memory *MUST* be freed with
128 * Curl_freeaddrinfo(), nothing else.
129 */
Curl_getaddrinfo(struct Curl_easy * data,const char * hostname,int port,int * waitp)130 struct Curl_addrinfo *Curl_getaddrinfo(struct Curl_easy *data,
131 const char *hostname,
132 int port,
133 int *waitp)
134 {
135 struct addrinfo hints;
136 struct Curl_addrinfo *res;
137 int error;
138 char sbuf[12];
139 char *sbufptr = NULL;
140 #ifndef USE_RESOLVE_ON_IPS
141 char addrbuf[128];
142 #endif
143 int pf = PF_INET;
144
145 *waitp = 0; /* synchronous response only */
146
147 if(Curl_ipv6works(data))
148 /* The stack seems to be IPv6-enabled */
149 pf = PF_UNSPEC;
150
151 memset(&hints, 0, sizeof(hints));
152 hints.ai_family = pf;
153 hints.ai_socktype = (data->conn->transport == TRNSPRT_TCP) ?
154 SOCK_STREAM : SOCK_DGRAM;
155
156 #ifndef USE_RESOLVE_ON_IPS
157 /*
158 * The AI_NUMERICHOST must not be set to get synthesized IPv6 address from
159 * an IPv4 address on iOS and Mac OS X.
160 */
161 if((1 == Curl_inet_pton(AF_INET, hostname, addrbuf)) ||
162 (1 == Curl_inet_pton(AF_INET6, hostname, addrbuf))) {
163 /* the given address is numerical only, prevent a reverse lookup */
164 hints.ai_flags = AI_NUMERICHOST;
165 }
166 #endif
167
168 if(port) {
169 msnprintf(sbuf, sizeof(sbuf), "%d", port);
170 sbufptr = sbuf;
171 }
172
173 error = Curl_getaddrinfo_ex(hostname, sbufptr, &hints, &res);
174 if(error) {
175 infof(data, "getaddrinfo(3) failed for %s:%d", hostname, port);
176 return NULL;
177 }
178
179 if(port) {
180 Curl_addrinfo_set_port(res, port);
181 }
182
183 dump_addrinfo(conn, res);
184
185 return res;
186 }
187 #endif /* CURLRES_SYNCH */
188
189 #endif /* CURLRES_IPV6 */
190