1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2021, 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.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>
54 #include "inet_pton.h"
56 /* The last 3 #include files should be in this order */
57 #include "curl_printf.h"
58 #include "curl_memory.h"
62 * Curl_ipvalid() checks what CURL_IPRESOLVE_* requirements that might've
63 * been set and returns TRUE if they are OK.
65 bool Curl_ipvalid(struct Curl_easy *data, struct connectdata *conn)
67 if(conn->ip_version == CURL_IPRESOLVE_V6)
68 return Curl_ipv6works(data);
73 #if defined(CURLRES_SYNCH)
76 static void dump_addrinfo(struct connectdata *conn,
77 const struct Curl_addrinfo *ai)
79 printf("dump_addrinfo:\n");
80 for(; ai; ai = ai->ai_next) {
81 char buf[INET6_ADDRSTRLEN];
82 printf(" fam %2d, CNAME %s, ",
83 ai->ai_family, ai->ai_canonname ? ai->ai_canonname : "<none>");
84 Curl_printable_address(ai, buf, sizeof(buf));
89 #define dump_addrinfo(x,y) Curl_nop_stmt
93 * Curl_getaddrinfo() when built IPv6-enabled (non-threading and
96 * Returns name information about the given hostname and port number. If
97 * successful, the 'addrinfo' is returned and the forth argument will point to
98 * memory we need to free after use. That memory *MUST* be freed with
99 * Curl_freeaddrinfo(), nothing else.
101 struct Curl_addrinfo *Curl_getaddrinfo(struct Curl_easy *data,
102 const char *hostname,
106 struct addrinfo hints;
107 struct Curl_addrinfo *res;
110 char *sbufptr = NULL;
111 #ifndef USE_RESOLVE_ON_IPS
116 *waitp = 0; /* synchronous response only */
118 if(Curl_ipv6works(data))
119 /* The stack seems to be IPv6-enabled */
122 memset(&hints, 0, sizeof(hints));
123 hints.ai_family = pf;
124 hints.ai_socktype = (data->conn->transport == TRNSPRT_TCP) ?
125 SOCK_STREAM : SOCK_DGRAM;
127 #ifndef USE_RESOLVE_ON_IPS
129 * The AI_NUMERICHOST must not be set to get synthesized IPv6 address from
130 * an IPv4 address on iOS and Mac OS X.
132 if((1 == Curl_inet_pton(AF_INET, hostname, addrbuf)) ||
133 (1 == Curl_inet_pton(AF_INET6, hostname, addrbuf))) {
134 /* the given address is numerical only, prevent a reverse lookup */
135 hints.ai_flags = AI_NUMERICHOST;
140 msnprintf(sbuf, sizeof(sbuf), "%d", port);
144 error = Curl_getaddrinfo_ex(hostname, sbufptr, &hints, &res);
146 infof(data, "getaddrinfo(3) failed for %s:%d", hostname, port);
151 Curl_addrinfo_set_port(res, port);
154 dump_addrinfo(conn, res);
158 #endif /* CURLRES_SYNCH */
160 #endif /* CURLRES_IPV6 */