1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2020, Daniel Stenberg, <daniel@haxx.se>, et al.
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.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
23 #include "curl_setup.h"
25 /***********************************************************************
26 * Only for IPv6-enabled builds
27 **********************************************************************/
30 #ifdef HAVE_NETINET_IN_H
31 #include <netinet/in.h>
36 #ifdef HAVE_ARPA_INET_H
37 #include <arpa/inet.h>
55 #include "inet_pton.h"
57 /* The last 3 #include files should be in this order */
58 #include "curl_printf.h"
59 #include "curl_memory.h"
63 * Curl_ipv6works() returns TRUE if IPv6 seems to work.
65 bool Curl_ipv6works(struct connectdata *conn)
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 */
72 DEBUGASSERT(conn->data);
73 DEBUGASSERT(conn->data->multi);
74 return conn->data->multi->ipv6_works;
78 /* probe to see if we have a working IPv6 stack */
79 curl_socket_t s = socket(PF_INET6, SOCK_DGRAM, 0);
80 if(s == CURL_SOCKET_BAD)
81 /* an IPv6 address was requested but we can't get/use one */
85 Curl_closesocket(NULL, s);
87 return (ipv6_works>0)?TRUE:FALSE;
92 * Curl_ipvalid() checks what CURL_IPRESOLVE_* requirements that might've
93 * been set and returns TRUE if they are OK.
95 bool Curl_ipvalid(struct connectdata *conn)
97 if(conn->ip_version == CURL_IPRESOLVE_V6)
98 return Curl_ipv6works(conn);
103 #if defined(CURLRES_SYNCH)
105 #ifdef DEBUG_ADDRINFO
106 static void dump_addrinfo(struct connectdata *conn,
107 const struct Curl_addrinfo *ai)
109 printf("dump_addrinfo:\n");
110 for(; ai; ai = ai->ai_next) {
111 char buf[INET6_ADDRSTRLEN];
112 printf(" fam %2d, CNAME %s, ",
113 ai->ai_family, ai->ai_canonname ? ai->ai_canonname : "<none>");
114 Curl_printable_address(ai, buf, sizeof(buf));
119 #define dump_addrinfo(x,y) Curl_nop_stmt
123 * Curl_getaddrinfo() when built IPv6-enabled (non-threading and
126 * Returns name information about the given hostname and port number. If
127 * successful, the 'addrinfo' is returned and the forth argument will point to
128 * memory we need to free after use. That memory *MUST* be freed with
129 * Curl_freeaddrinfo(), nothing else.
131 struct Curl_addrinfo *Curl_getaddrinfo(struct connectdata *conn,
132 const char *hostname,
136 struct addrinfo hints;
137 struct Curl_addrinfo *res;
140 char *sbufptr = NULL;
141 #ifndef USE_RESOLVE_ON_IPS
145 #if !defined(CURL_DISABLE_VERBOSE_STRINGS)
146 struct Curl_easy *data = conn->data;
149 *waitp = 0; /* synchronous response only */
151 /* Check if a limited name resolve has been requested */
152 switch(conn->ip_version) {
153 case CURL_IPRESOLVE_V4:
156 case CURL_IPRESOLVE_V6:
164 if((pf != PF_INET) && !Curl_ipv6works(conn))
165 /* The stack seems to be a non-IPv6 one */
168 memset(&hints, 0, sizeof(hints));
169 hints.ai_family = pf;
170 hints.ai_socktype = (conn->transport == TRNSPRT_TCP) ?
171 SOCK_STREAM : SOCK_DGRAM;
173 #ifndef USE_RESOLVE_ON_IPS
175 * The AI_NUMERICHOST must not be set to get synthesized IPv6 address from
176 * an IPv4 address on iOS and Mac OS X.
178 if((1 == Curl_inet_pton(AF_INET, hostname, addrbuf)) ||
179 (1 == Curl_inet_pton(AF_INET6, hostname, addrbuf))) {
180 /* the given address is numerical only, prevent a reverse lookup */
181 hints.ai_flags = AI_NUMERICHOST;
186 msnprintf(sbuf, sizeof(sbuf), "%d", port);
190 error = Curl_getaddrinfo_ex(hostname, sbufptr, &hints, &res);
192 infof(data, "getaddrinfo(3) failed for %s:%d\n", hostname, port);
197 Curl_addrinfo_set_port(res, port);
200 dump_addrinfo(conn, res);
204 #endif /* CURLRES_SYNCH */
206 #endif /* CURLRES_IPV6 */