1 | /***************************************************************************
|
---|
2 | * _ _ ____ _
|
---|
3 | * Project ___| | | | _ \| |
|
---|
4 | * / __| | | | |_) | |
|
---|
5 | * | (__| |_| | _ <| |___
|
---|
6 | * \___|\___/|_| \_\_____|
|
---|
7 | *
|
---|
8 | * Copyright (C) 1998 - 2018, Daniel Stenberg, <[email protected]>, 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.haxx.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 | */
|
---|
65 | bool Curl_ipv6works(void)
|
---|
66 | {
|
---|
67 | /* the nature of most system is that IPv6 status doesn't come and go
|
---|
68 | during a program's lifetime so we only probe the first time and then we
|
---|
69 | have the info kept for fast re-use */
|
---|
70 | static int ipv6_works = -1;
|
---|
71 | if(-1 == ipv6_works) {
|
---|
72 | /* probe to see if we have a working IPv6 stack */
|
---|
73 | curl_socket_t s = socket(PF_INET6, SOCK_DGRAM, 0);
|
---|
74 | if(s == CURL_SOCKET_BAD)
|
---|
75 | /* an IPv6 address was requested but we can't get/use one */
|
---|
76 | ipv6_works = 0;
|
---|
77 | else {
|
---|
78 | ipv6_works = 1;
|
---|
79 | Curl_closesocket(NULL, s);
|
---|
80 | }
|
---|
81 | }
|
---|
82 | return (ipv6_works>0)?TRUE:FALSE;
|
---|
83 | }
|
---|
84 |
|
---|
85 | /*
|
---|
86 | * Curl_ipvalid() checks what CURL_IPRESOLVE_* requirements that might've
|
---|
87 | * been set and returns TRUE if they are OK.
|
---|
88 | */
|
---|
89 | bool Curl_ipvalid(struct connectdata *conn)
|
---|
90 | {
|
---|
91 | if(conn->ip_version == CURL_IPRESOLVE_V6)
|
---|
92 | return Curl_ipv6works();
|
---|
93 |
|
---|
94 | return TRUE;
|
---|
95 | }
|
---|
96 |
|
---|
97 | #if defined(CURLRES_SYNCH)
|
---|
98 |
|
---|
99 | #ifdef DEBUG_ADDRINFO
|
---|
100 | static void dump_addrinfo(struct connectdata *conn, const Curl_addrinfo *ai)
|
---|
101 | {
|
---|
102 | printf("dump_addrinfo:\n");
|
---|
103 | for(; ai; ai = ai->ai_next) {
|
---|
104 | char buf[INET6_ADDRSTRLEN];
|
---|
105 |
|
---|
106 | printf(" fam %2d, CNAME %s, ",
|
---|
107 | ai->ai_family, ai->ai_canonname ? ai->ai_canonname : "<none>");
|
---|
108 | if(Curl_printable_address(ai, buf, sizeof(buf)))
|
---|
109 | printf("%s\n", buf);
|
---|
110 | else
|
---|
111 | printf("failed; %s\n", Curl_strerror(conn, SOCKERRNO));
|
---|
112 | }
|
---|
113 | }
|
---|
114 | #else
|
---|
115 | #define dump_addrinfo(x,y) Curl_nop_stmt
|
---|
116 | #endif
|
---|
117 |
|
---|
118 | /*
|
---|
119 | * Curl_getaddrinfo() when built IPv6-enabled (non-threading and
|
---|
120 | * non-ares version).
|
---|
121 | *
|
---|
122 | * Returns name information about the given hostname and port number. If
|
---|
123 | * successful, the 'addrinfo' is returned and the forth argument will point to
|
---|
124 | * memory we need to free after use. That memory *MUST* be freed with
|
---|
125 | * Curl_freeaddrinfo(), nothing else.
|
---|
126 | */
|
---|
127 | Curl_addrinfo *Curl_getaddrinfo(struct connectdata *conn,
|
---|
128 | const char *hostname,
|
---|
129 | int port,
|
---|
130 | int *waitp)
|
---|
131 | {
|
---|
132 | struct addrinfo hints;
|
---|
133 | Curl_addrinfo *res;
|
---|
134 | int error;
|
---|
135 | char sbuf[12];
|
---|
136 | char *sbufptr = NULL;
|
---|
137 | #ifndef USE_RESOLVE_ON_IPS
|
---|
138 | char addrbuf[128];
|
---|
139 | #endif
|
---|
140 | int pf;
|
---|
141 | #if !defined(CURL_DISABLE_VERBOSE_STRINGS)
|
---|
142 | struct Curl_easy *data = conn->data;
|
---|
143 | #endif
|
---|
144 |
|
---|
145 | *waitp = 0; /* synchronous response only */
|
---|
146 |
|
---|
147 | /* Check if a limited name resolve has been requested */
|
---|
148 | switch(conn->ip_version) {
|
---|
149 | case CURL_IPRESOLVE_V4:
|
---|
150 | pf = PF_INET;
|
---|
151 | break;
|
---|
152 | case CURL_IPRESOLVE_V6:
|
---|
153 | pf = PF_INET6;
|
---|
154 | break;
|
---|
155 | default:
|
---|
156 | pf = PF_UNSPEC;
|
---|
157 | break;
|
---|
158 | }
|
---|
159 |
|
---|
160 | if((pf != PF_INET) && !Curl_ipv6works())
|
---|
161 | /* The stack seems to be a non-IPv6 one */
|
---|
162 | pf = PF_INET;
|
---|
163 |
|
---|
164 | memset(&hints, 0, sizeof(hints));
|
---|
165 | hints.ai_family = pf;
|
---|
166 | hints.ai_socktype = conn->socktype;
|
---|
167 |
|
---|
168 | #ifndef USE_RESOLVE_ON_IPS
|
---|
169 | /*
|
---|
170 | * The AI_NUMERICHOST must not be set to get synthesized IPv6 address from
|
---|
171 | * an IPv4 address on iOS and Mac OS X.
|
---|
172 | */
|
---|
173 | if((1 == Curl_inet_pton(AF_INET, hostname, addrbuf)) ||
|
---|
174 | (1 == Curl_inet_pton(AF_INET6, hostname, addrbuf))) {
|
---|
175 | /* the given address is numerical only, prevent a reverse lookup */
|
---|
176 | hints.ai_flags = AI_NUMERICHOST;
|
---|
177 | }
|
---|
178 | #endif
|
---|
179 |
|
---|
180 | if(port) {
|
---|
181 | msnprintf(sbuf, sizeof(sbuf), "%d", port);
|
---|
182 | sbufptr = sbuf;
|
---|
183 | }
|
---|
184 |
|
---|
185 | error = Curl_getaddrinfo_ex(hostname, sbufptr, &hints, &res);
|
---|
186 | if(error) {
|
---|
187 | infof(data, "getaddrinfo(3) failed for %s:%d\n", hostname, port);
|
---|
188 | return NULL;
|
---|
189 | }
|
---|
190 |
|
---|
191 | if(port) {
|
---|
192 | Curl_addrinfo_set_port(res, port);
|
---|
193 | }
|
---|
194 |
|
---|
195 | dump_addrinfo(conn, res);
|
---|
196 |
|
---|
197 | return res;
|
---|
198 | }
|
---|
199 | #endif /* CURLRES_SYNCH */
|
---|
200 |
|
---|
201 | #endif /* CURLRES_IPV6 */
|
---|