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 | */ |
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 |
74 | static void dump_addrinfo(struct connectdata *conn, |
75 | const struct Curl_addrinfo *ai) |
76 | { |
77 | printf("dump_addrinfo:\n" ); |
78 | for(; ai; ai = ai->ai_next) { |
79 | char buf[INET6_ADDRSTRLEN]; |
80 | printf(" fam %2d, CNAME %s, " , |
81 | ai->ai_family, ai->ai_canonname ? ai->ai_canonname : "<none>" ); |
82 | Curl_printable_address(ai, buf, sizeof(buf)); |
83 | printf("%s\n" , buf); |
84 | } |
85 | } |
86 | #else |
87 | #define dump_addrinfo(x,y) Curl_nop_stmt |
88 | #endif |
89 | |
90 | /* |
91 | * Curl_getaddrinfo() when built IPv6-enabled (non-threading and |
92 | * non-ares version). |
93 | * |
94 | * Returns name information about the given hostname and port number. If |
95 | * successful, the 'addrinfo' is returned and the fourth argument will point |
96 | * to memory we need to free after use. That memory *MUST* be freed with |
97 | * Curl_freeaddrinfo(), nothing else. |
98 | */ |
99 | struct Curl_addrinfo *Curl_getaddrinfo(struct Curl_easy *data, |
100 | const char *hostname, |
101 | int port, |
102 | int *waitp) |
103 | { |
104 | struct addrinfo hints; |
105 | struct Curl_addrinfo *res; |
106 | int error; |
107 | char sbuf[12]; |
108 | char *sbufptr = NULL; |
109 | #ifndef USE_RESOLVE_ON_IPS |
110 | char addrbuf[128]; |
111 | #endif |
112 | int pf = PF_INET; |
113 | |
114 | *waitp = 0; /* synchronous response only */ |
115 | |
116 | if((data->conn->ip_version != CURL_IPRESOLVE_V4) && Curl_ipv6works(data)) |
117 | /* The stack seems to be IPv6-enabled */ |
118 | pf = PF_UNSPEC; |
119 | |
120 | memset(&hints, 0, sizeof(hints)); |
121 | hints.ai_family = pf; |
122 | hints.ai_socktype = (data->conn->transport == TRNSPRT_TCP) ? |
123 | SOCK_STREAM : SOCK_DGRAM; |
124 | |
125 | #ifndef USE_RESOLVE_ON_IPS |
126 | /* |
127 | * The AI_NUMERICHOST must not be set to get synthesized IPv6 address from |
128 | * an IPv4 address on iOS and Mac OS X. |
129 | */ |
130 | if((1 == Curl_inet_pton(AF_INET, hostname, addrbuf)) || |
131 | (1 == Curl_inet_pton(AF_INET6, hostname, addrbuf))) { |
132 | /* the given address is numerical only, prevent a reverse lookup */ |
133 | hints.ai_flags = AI_NUMERICHOST; |
134 | } |
135 | #endif |
136 | |
137 | if(port) { |
138 | msnprintf(sbuf, sizeof(sbuf), "%d" , port); |
139 | sbufptr = sbuf; |
140 | } |
141 | |
142 | error = Curl_getaddrinfo_ex(hostname, sbufptr, &hints, &res); |
143 | if(error) { |
144 | infof(data, "getaddrinfo(3) failed for %s:%d" , hostname, port); |
145 | return NULL; |
146 | } |
147 | |
148 | if(port) { |
149 | Curl_addrinfo_set_port(res, port); |
150 | } |
151 | |
152 | dump_addrinfo(conn, res); |
153 | |
154 | return res; |
155 | } |
156 | #endif /* CURLRES_SYNCH */ |
157 | |
158 | #endif /* CURLRES_IPV6 */ |
159 | |