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 "url.h" |
54 | #include "inet_pton.h" |
55 | #include "connect.h" |
56 | /* The last 3 #include files should be in this order */ |
57 | #include "curl_printf.h" |
58 | #include "curl_memory.h" |
59 | #include "memdebug.h" |
60 | |
61 | /* |
62 | * Curl_ipvalid() checks what CURL_IPRESOLVE_* requirements that might've |
63 | * been set and returns TRUE if they are OK. |
64 | */ |
65 | bool Curl_ipvalid(struct Curl_easy *data, struct connectdata *conn) |
66 | { |
67 | if(conn->ip_version == CURL_IPRESOLVE_V6) |
68 | return Curl_ipv6works(data); |
69 | |
70 | return TRUE; |
71 | } |
72 | |
73 | #if defined(CURLRES_SYNCH) |
74 | |
75 | #ifdef DEBUG_ADDRINFO |
76 | static void dump_addrinfo(struct connectdata *conn, |
77 | const struct Curl_addrinfo *ai) |
78 | { |
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)); |
85 | printf("%s\n" , buf); |
86 | } |
87 | } |
88 | #else |
89 | #define dump_addrinfo(x,y) Curl_nop_stmt |
90 | #endif |
91 | |
92 | /* |
93 | * Curl_getaddrinfo() when built IPv6-enabled (non-threading and |
94 | * non-ares version). |
95 | * |
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. |
100 | */ |
101 | struct Curl_addrinfo *Curl_getaddrinfo(struct Curl_easy *data, |
102 | const char *hostname, |
103 | int port, |
104 | int *waitp) |
105 | { |
106 | struct addrinfo hints; |
107 | struct Curl_addrinfo *res; |
108 | int error; |
109 | char sbuf[12]; |
110 | char *sbufptr = NULL; |
111 | #ifndef USE_RESOLVE_ON_IPS |
112 | char addrbuf[128]; |
113 | #endif |
114 | int pf = PF_INET; |
115 | |
116 | *waitp = 0; /* synchronous response only */ |
117 | |
118 | if(Curl_ipv6works(data)) |
119 | /* The stack seems to be IPv6-enabled */ |
120 | pf = PF_UNSPEC; |
121 | |
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; |
126 | |
127 | #ifndef USE_RESOLVE_ON_IPS |
128 | /* |
129 | * The AI_NUMERICHOST must not be set to get synthesized IPv6 address from |
130 | * an IPv4 address on iOS and Mac OS X. |
131 | */ |
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; |
136 | } |
137 | #endif |
138 | |
139 | if(port) { |
140 | msnprintf(sbuf, sizeof(sbuf), "%d" , port); |
141 | sbufptr = sbuf; |
142 | } |
143 | |
144 | error = Curl_getaddrinfo_ex(hostname, sbufptr, &hints, &res); |
145 | if(error) { |
146 | infof(data, "getaddrinfo(3) failed for %s:%d" , hostname, port); |
147 | return NULL; |
148 | } |
149 | |
150 | if(port) { |
151 | Curl_addrinfo_set_port(res, port); |
152 | } |
153 | |
154 | dump_addrinfo(conn, res); |
155 | |
156 | return res; |
157 | } |
158 | #endif /* CURLRES_SYNCH */ |
159 | |
160 | #endif /* CURLRES_IPV6 */ |
161 | |