/src/curl/lib/curl_addrinfo.c
Line | Count | Source |
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 | | #include "curl_setup.h" |
25 | | |
26 | | #ifdef HAVE_NETINET_IN_H |
27 | | # include <netinet/in.h> |
28 | | #endif |
29 | | #ifdef HAVE_NETINET_IN6_H |
30 | | # include <netinet/in6.h> |
31 | | #endif |
32 | | #ifdef HAVE_NETDB_H |
33 | | # include <netdb.h> |
34 | | #endif |
35 | | #ifdef HAVE_ARPA_INET_H |
36 | | # include <arpa/inet.h> |
37 | | #endif |
38 | | #ifdef HAVE_SYS_UN_H |
39 | | # include <sys/un.h> |
40 | | #endif |
41 | | |
42 | | #ifdef __VMS |
43 | | # include <in.h> |
44 | | # include <inet.h> |
45 | | #endif |
46 | | |
47 | | #include <stddef.h> /* for offsetof() */ |
48 | | |
49 | | #include "curl_addrinfo.h" |
50 | | #include "fake_addrinfo.h" |
51 | | #include "curlx/inet_pton.h" |
52 | | #include "curlx/strparse.h" |
53 | | |
54 | | /* |
55 | | * Curl_freeaddrinfo() |
56 | | * |
57 | | * This is used to free a linked list of Curl_addrinfo structs along |
58 | | * with all its associated allocated storage. This function should be |
59 | | * called once for each successful call to Curl_getaddrinfo_ex() or to |
60 | | * any function call which actually allocates a Curl_addrinfo struct. |
61 | | */ |
62 | | |
63 | | #if defined(__INTEL_COMPILER) && (__INTEL_COMPILER == 910) && \ |
64 | | defined(__OPTIMIZE__) && defined(__unix__) && defined(__i386__) |
65 | | /* workaround icc 9.1 optimizer issue */ |
66 | | # define vqualifier volatile |
67 | | #else |
68 | | # define vqualifier |
69 | | #endif |
70 | | |
71 | | void Curl_freeaddrinfo(struct Curl_addrinfo *cahead) |
72 | 0 | { |
73 | 0 | struct Curl_addrinfo *vqualifier canext; |
74 | 0 | struct Curl_addrinfo *ca; |
75 | |
|
76 | 0 | for(ca = cahead; ca; ca = canext) { |
77 | 0 | canext = ca->ai_next; |
78 | 0 | curlx_free(ca); |
79 | 0 | } |
80 | 0 | } |
81 | | |
82 | | #ifdef HAVE_GETADDRINFO |
83 | | /* |
84 | | * Curl_getaddrinfo_ex() |
85 | | * |
86 | | * This is a wrapper function around system's getaddrinfo(), with |
87 | | * the only difference that instead of returning a linked list of |
88 | | * addrinfo structs this one returns a linked list of Curl_addrinfo |
89 | | * ones. The memory allocated by this function *MUST* be free'd with |
90 | | * Curl_freeaddrinfo(). For each successful call to this function |
91 | | * there must be an associated call later to Curl_freeaddrinfo(). |
92 | | * |
93 | | * There should be no single call to system's getaddrinfo() in the |
94 | | * whole library, any such call should be 'routed' through this one. |
95 | | */ |
96 | | int Curl_getaddrinfo_ex(const char *nodename, |
97 | | const char *servname, |
98 | | const struct addrinfo *hints, |
99 | | struct Curl_addrinfo **result) |
100 | 0 | { |
101 | 0 | const struct addrinfo *ai; |
102 | 0 | struct addrinfo *aihead; |
103 | 0 | struct Curl_addrinfo *cafirst = NULL; |
104 | 0 | struct Curl_addrinfo *calast = NULL; |
105 | 0 | struct Curl_addrinfo *ca; |
106 | 0 | size_t ss_size; |
107 | 0 | int error; |
108 | |
|
109 | 0 | *result = NULL; /* assume failure */ |
110 | |
|
111 | 0 | error = CURL_GETADDRINFO(nodename, servname, hints, &aihead); |
112 | 0 | if(error) |
113 | 0 | return error; |
114 | | |
115 | | /* traverse the addrinfo list */ |
116 | | |
117 | 0 | for(ai = aihead; ai != NULL; ai = ai->ai_next) { |
118 | 0 | size_t namelen = ai->ai_canonname ? strlen(ai->ai_canonname) + 1 : 0; |
119 | | /* ignore elements with unsupported address family, |
120 | | settle family-specific sockaddr structure size. */ |
121 | 0 | if(ai->ai_family == AF_INET) |
122 | 0 | ss_size = sizeof(struct sockaddr_in); |
123 | 0 | #ifdef USE_IPV6 |
124 | 0 | else if(ai->ai_family == AF_INET6) |
125 | 0 | ss_size = sizeof(struct sockaddr_in6); |
126 | 0 | #endif |
127 | 0 | else |
128 | 0 | continue; |
129 | | |
130 | | /* ignore elements without required address info */ |
131 | 0 | if(!ai->ai_addr || !(ai->ai_addrlen > 0)) |
132 | 0 | continue; |
133 | | |
134 | | /* ignore elements with bogus address size */ |
135 | 0 | if((size_t)ai->ai_addrlen < ss_size) |
136 | 0 | continue; |
137 | | |
138 | 0 | ca = curlx_malloc(sizeof(struct Curl_addrinfo) + ss_size + namelen); |
139 | 0 | if(!ca) { |
140 | 0 | error = EAI_MEMORY; |
141 | 0 | break; |
142 | 0 | } |
143 | | |
144 | | /* copy each structure member individually, member ordering, |
145 | | size, or padding might be different for each platform. */ |
146 | | |
147 | 0 | ca->ai_flags = ai->ai_flags; |
148 | 0 | ca->ai_family = ai->ai_family; |
149 | 0 | ca->ai_socktype = ai->ai_socktype; |
150 | 0 | ca->ai_protocol = ai->ai_protocol; |
151 | 0 | ca->ai_addrlen = (curl_socklen_t)ss_size; |
152 | 0 | ca->ai_addr = NULL; |
153 | 0 | ca->ai_canonname = NULL; |
154 | 0 | ca->ai_next = NULL; |
155 | |
|
156 | 0 | ca->ai_addr = (void *)((char *)ca + sizeof(struct Curl_addrinfo)); |
157 | 0 | memcpy(ca->ai_addr, ai->ai_addr, ss_size); |
158 | |
|
159 | 0 | if(namelen) { |
160 | 0 | ca->ai_canonname = (void *)((char *)ca->ai_addr + ss_size); |
161 | 0 | memcpy(ca->ai_canonname, ai->ai_canonname, namelen); |
162 | 0 | } |
163 | | |
164 | | /* if the return list is empty, this becomes the first element */ |
165 | 0 | if(!cafirst) |
166 | 0 | cafirst = ca; |
167 | | |
168 | | /* add this element last in the return list */ |
169 | 0 | if(calast) |
170 | 0 | calast->ai_next = ca; |
171 | 0 | calast = ca; |
172 | 0 | } |
173 | | |
174 | | /* destroy the addrinfo list */ |
175 | 0 | if(aihead) |
176 | 0 | CURL_FREEADDRINFO(aihead); |
177 | | |
178 | | /* if we failed, also destroy the Curl_addrinfo list */ |
179 | 0 | if(error) { |
180 | 0 | Curl_freeaddrinfo(cafirst); |
181 | 0 | cafirst = NULL; |
182 | 0 | } |
183 | 0 | else if(!cafirst) { |
184 | 0 | #ifdef EAI_NONAME |
185 | | /* rfc3493 conformant */ |
186 | 0 | error = EAI_NONAME; |
187 | | #else |
188 | | /* rfc3493 obsoleted */ |
189 | | error = EAI_NODATA; |
190 | | #endif |
191 | | #ifdef USE_WINSOCK |
192 | | SET_SOCKERRNO(error); |
193 | | #endif |
194 | 0 | } |
195 | |
|
196 | 0 | *result = cafirst; |
197 | | |
198 | | /* This is not a CURLcode */ |
199 | 0 | return error; |
200 | 0 | } |
201 | | #endif /* HAVE_GETADDRINFO */ |
202 | | |
203 | | /* |
204 | | * Curl_he2ai() |
205 | | * |
206 | | * This function returns a pointer to the first element of a newly allocated |
207 | | * Curl_addrinfo struct linked list filled with the data of a given hostent. |
208 | | * Curl_addrinfo is meant to work like the addrinfo struct does for an IPv6 |
209 | | * stack, but usable also for IPv4, all hosts and environments. |
210 | | * |
211 | | * The memory allocated by this function *MUST* be free'd later on calling |
212 | | * Curl_freeaddrinfo(). For each successful call to this function there |
213 | | * must be an associated call later to Curl_freeaddrinfo(). |
214 | | * |
215 | | * Curl_addrinfo defined in "lib/curl_addrinfo.h" |
216 | | * |
217 | | * struct Curl_addrinfo { |
218 | | * int ai_flags; |
219 | | * int ai_family; |
220 | | * int ai_socktype; |
221 | | * int ai_protocol; |
222 | | * curl_socklen_t ai_addrlen; * Follow rfc3493 struct addrinfo * |
223 | | * char *ai_canonname; |
224 | | * struct sockaddr *ai_addr; |
225 | | * struct Curl_addrinfo *ai_next; |
226 | | * }; |
227 | | * |
228 | | * hostent defined in <netdb.h> |
229 | | * |
230 | | * struct hostent { |
231 | | * char *h_name; |
232 | | * char **h_aliases; |
233 | | * int h_addrtype; |
234 | | * int h_length; |
235 | | * char **h_addr_list; |
236 | | * }; |
237 | | * |
238 | | * for backward compatibility: |
239 | | * |
240 | | * #define h_addr h_addr_list[0] |
241 | | */ |
242 | | #if !(defined(HAVE_GETADDRINFO) && defined(HAVE_GETADDRINFO_THREADSAFE)) |
243 | | struct Curl_addrinfo *Curl_he2ai(const struct hostent *he, int port) |
244 | | { |
245 | | struct Curl_addrinfo *ai; |
246 | | struct Curl_addrinfo *prevai = NULL; |
247 | | struct Curl_addrinfo *firstai = NULL; |
248 | | struct sockaddr_in *addr; |
249 | | #ifdef USE_IPV6 |
250 | | struct sockaddr_in6 *addr6; |
251 | | #endif |
252 | | CURLcode result = CURLE_OK; |
253 | | int i; |
254 | | char *curr; |
255 | | |
256 | | if(!he) |
257 | | /* no input == no output! */ |
258 | | return NULL; |
259 | | |
260 | | DEBUGASSERT((he->h_name != NULL) && (he->h_addr_list != NULL)); |
261 | | |
262 | | for(i = 0; (curr = he->h_addr_list[i]) != NULL; i++) { |
263 | | size_t ss_size; |
264 | | size_t namelen = strlen(he->h_name) + 1; /* include null-terminator */ |
265 | | #ifdef USE_IPV6 |
266 | | if(he->h_addrtype == AF_INET6) |
267 | | ss_size = sizeof(struct sockaddr_in6); |
268 | | else |
269 | | #endif |
270 | | ss_size = sizeof(struct sockaddr_in); |
271 | | |
272 | | /* allocate memory to hold the struct, the address and the name */ |
273 | | ai = curlx_calloc(1, sizeof(struct Curl_addrinfo) + ss_size + namelen); |
274 | | if(!ai) { |
275 | | result = CURLE_OUT_OF_MEMORY; |
276 | | break; |
277 | | } |
278 | | /* put the address after the struct */ |
279 | | ai->ai_addr = (void *)((char *)ai + sizeof(struct Curl_addrinfo)); |
280 | | /* then put the name after the address */ |
281 | | ai->ai_canonname = (char *)ai->ai_addr + ss_size; |
282 | | memcpy(ai->ai_canonname, he->h_name, namelen); |
283 | | |
284 | | if(!firstai) |
285 | | /* store the pointer we want to return from this function */ |
286 | | firstai = ai; |
287 | | |
288 | | if(prevai) |
289 | | /* make the previous entry point to this */ |
290 | | prevai->ai_next = ai; |
291 | | |
292 | | ai->ai_family = he->h_addrtype; |
293 | | |
294 | | /* we return all names as STREAM, so when using this address for TFTP |
295 | | the type must be ignored and conn->socktype be used instead! */ |
296 | | ai->ai_socktype = SOCK_STREAM; |
297 | | |
298 | | ai->ai_addrlen = (curl_socklen_t)ss_size; |
299 | | |
300 | | /* leave the rest of the struct filled with zero */ |
301 | | |
302 | | switch(ai->ai_family) { |
303 | | case AF_INET: |
304 | | addr = (void *)ai->ai_addr; /* storage area for this info */ |
305 | | |
306 | | memcpy(&addr->sin_addr, curr, sizeof(struct in_addr)); |
307 | | addr->sin_family = (CURL_SA_FAMILY_T)(he->h_addrtype); |
308 | | addr->sin_port = htons((unsigned short)port); |
309 | | break; |
310 | | |
311 | | #ifdef USE_IPV6 |
312 | | case AF_INET6: |
313 | | addr6 = (void *)ai->ai_addr; /* storage area for this info */ |
314 | | |
315 | | memcpy(&addr6->sin6_addr, curr, sizeof(struct in6_addr)); |
316 | | addr6->sin6_family = (CURL_SA_FAMILY_T)(he->h_addrtype); |
317 | | addr6->sin6_port = htons((unsigned short)port); |
318 | | break; |
319 | | #endif |
320 | | } |
321 | | |
322 | | prevai = ai; |
323 | | } |
324 | | |
325 | | if(result) { |
326 | | Curl_freeaddrinfo(firstai); |
327 | | firstai = NULL; |
328 | | } |
329 | | |
330 | | return firstai; |
331 | | } |
332 | | #endif |
333 | | |
334 | | /* |
335 | | * ip2addr() |
336 | | * |
337 | | * This function takes an Internet address, in binary form, as input parameter |
338 | | * along with its address family and the string version of the address, and it |
339 | | * returns a Curl_addrinfo chain filled in correctly with information for the |
340 | | * given address/host |
341 | | */ |
342 | | static CURLcode ip2addr(struct Curl_addrinfo **addrp, int af, |
343 | | const void *inaddr, const char *hostname, int port) |
344 | 0 | { |
345 | 0 | struct Curl_addrinfo *ai; |
346 | 0 | size_t addrsize; |
347 | 0 | size_t namelen; |
348 | 0 | struct sockaddr_in *addr; |
349 | 0 | #ifdef USE_IPV6 |
350 | 0 | struct sockaddr_in6 *addr6; |
351 | 0 | #endif |
352 | |
|
353 | 0 | DEBUGASSERT(inaddr && hostname); |
354 | |
|
355 | 0 | namelen = strlen(hostname) + 1; |
356 | 0 | *addrp = NULL; |
357 | |
|
358 | 0 | if(af == AF_INET) |
359 | 0 | addrsize = sizeof(struct sockaddr_in); |
360 | 0 | #ifdef USE_IPV6 |
361 | 0 | else if(af == AF_INET6) |
362 | 0 | addrsize = sizeof(struct sockaddr_in6); |
363 | 0 | #endif |
364 | 0 | else |
365 | 0 | return CURLE_BAD_FUNCTION_ARGUMENT; |
366 | | |
367 | | /* allocate memory to hold the struct, the address and the name */ |
368 | 0 | ai = curlx_calloc(1, sizeof(struct Curl_addrinfo) + addrsize + namelen); |
369 | 0 | if(!ai) |
370 | 0 | return CURLE_OUT_OF_MEMORY; |
371 | | /* put the address after the struct */ |
372 | 0 | ai->ai_addr = (void *)((char *)ai + sizeof(struct Curl_addrinfo)); |
373 | | /* then put the name after the address */ |
374 | 0 | ai->ai_canonname = (char *)ai->ai_addr + addrsize; |
375 | 0 | memcpy(ai->ai_canonname, hostname, namelen); |
376 | 0 | ai->ai_family = af; |
377 | 0 | ai->ai_socktype = SOCK_STREAM; |
378 | 0 | ai->ai_addrlen = (curl_socklen_t)addrsize; |
379 | | /* leave the rest of the struct filled with zero */ |
380 | |
|
381 | 0 | switch(af) { |
382 | 0 | case AF_INET: |
383 | 0 | addr = (void *)ai->ai_addr; /* storage area for this info */ |
384 | |
|
385 | 0 | memcpy(&addr->sin_addr, inaddr, sizeof(struct in_addr)); |
386 | 0 | addr->sin_family = (CURL_SA_FAMILY_T)af; |
387 | 0 | addr->sin_port = htons((unsigned short)port); |
388 | 0 | break; |
389 | | |
390 | 0 | #ifdef USE_IPV6 |
391 | 0 | case AF_INET6: |
392 | 0 | addr6 = (void *)ai->ai_addr; /* storage area for this info */ |
393 | |
|
394 | 0 | memcpy(&addr6->sin6_addr, inaddr, sizeof(struct in6_addr)); |
395 | 0 | addr6->sin6_family = (CURL_SA_FAMILY_T)af; |
396 | 0 | addr6->sin6_port = htons((unsigned short)port); |
397 | 0 | break; |
398 | 0 | #endif |
399 | 0 | } |
400 | 0 | *addrp = ai; |
401 | 0 | return CURLE_OK; |
402 | 0 | } |
403 | | |
404 | | /* |
405 | | * Given an IPv4 or IPv6 dotted string address, this converts it to a proper |
406 | | * allocated Curl_addrinfo struct and returns it. |
407 | | */ |
408 | | CURLcode Curl_str2addr(const char *dotted, uint16_t port, |
409 | | struct Curl_addrinfo **addrp) |
410 | 0 | { |
411 | 0 | struct in_addr in; |
412 | 0 | if(curlx_inet_pton(AF_INET, dotted, &in) > 0) |
413 | | /* This is a dotted IP address 123.123.123.123-style */ |
414 | 0 | return ip2addr(addrp, AF_INET, &in, dotted, port); |
415 | 0 | #ifdef USE_IPV6 |
416 | 0 | { |
417 | 0 | struct in6_addr in6; |
418 | 0 | if(curlx_inet_pton(AF_INET6, dotted, &in6) > 0) |
419 | | /* This is a dotted IPv6 address ::1-style */ |
420 | 0 | return ip2addr(addrp, AF_INET6, &in6, dotted, port); |
421 | 0 | } |
422 | 0 | #endif |
423 | 0 | return CURLE_BAD_FUNCTION_ARGUMENT; /* bad input format */ |
424 | 0 | } |
425 | | |
426 | | bool Curl_is_ipv4addr(const char *address) |
427 | 0 | { |
428 | 0 | struct in_addr in; |
429 | 0 | return (curlx_inet_pton(AF_INET, address, &in) > 0); |
430 | 0 | } |
431 | | |
432 | | bool Curl_is_ipaddr(const char *address) |
433 | 0 | { |
434 | 0 | if(Curl_is_ipv4addr(address)) |
435 | 0 | return TRUE; |
436 | 0 | #ifdef USE_IPV6 |
437 | 0 | { |
438 | 0 | struct in6_addr in6; |
439 | 0 | if(curlx_inet_pton(AF_INET6, address, &in6) > 0) |
440 | | /* This is a dotted IPv6 address ::1-style */ |
441 | 0 | return TRUE; |
442 | 0 | } |
443 | 0 | #endif |
444 | 0 | return FALSE; |
445 | 0 | } |
446 | | |
447 | | bool Curl_looks_like_ipv6(const char *s, size_t len, bool maybe_url_encoded, |
448 | | struct Curl_str *host, struct Curl_str *zone) |
449 | 0 | { |
450 | 0 | const char *zonep = NULL; |
451 | 0 | size_t i = 0, hlen = 0, zlen = 0; |
452 | |
|
453 | 0 | if(host) |
454 | 0 | memset(host, 0, sizeof(*host)); |
455 | 0 | if(zone) |
456 | 0 | memset(zone, 0, sizeof(*zone)); |
457 | |
|
458 | 0 | for(i = 0; i < len; ++i, ++hlen) { |
459 | 0 | if(!s[i] || !(ISXDIGIT(s[i]) || (s[i] == ':') || (s[i] == '.'))) |
460 | 0 | break; |
461 | 0 | } |
462 | |
|
463 | 0 | if((i < len) && (s[i] == '%')) { /* address followed by a zone? */ |
464 | 0 | i += 1; |
465 | 0 | if(maybe_url_encoded && !strncmp("25", s + i, 2)) |
466 | 0 | i += 2; |
467 | 0 | zonep = s + i; |
468 | 0 | for(; i < len; ++i, ++zlen) { |
469 | | /* Allow unreserved characters as defined in RFC 3986 */ |
470 | 0 | if(!s[i] || !(ISALPHA(s[i]) || ISXDIGIT(s[i]) || (s[i] == '-') || |
471 | 0 | (s[i] == '.') || (s[i] == '_') || (s[i] == '~'))) |
472 | 0 | break; |
473 | 0 | } |
474 | 0 | } |
475 | |
|
476 | 0 | if(i != len) |
477 | 0 | return FALSE; /* invalid chars in zone */ |
478 | 0 | if(host && hlen) { |
479 | 0 | host->str = s; |
480 | 0 | host->len = hlen; |
481 | 0 | } |
482 | 0 | if(zone && zlen) { |
483 | 0 | zone->str = zonep; |
484 | 0 | zone->len = zlen; |
485 | 0 | } |
486 | 0 | return TRUE; |
487 | 0 | } |
488 | | |
489 | | #ifdef USE_UNIX_SOCKETS |
490 | | /** |
491 | | * Given a path to a Unix domain socket, return a newly allocated Curl_addrinfo |
492 | | * struct initialized with this path. |
493 | | * Returns CURLE_TOO_LARGE when path is too long. |
494 | | */ |
495 | | CURLcode Curl_unix2addr(const char *path, bool abstract, |
496 | | struct Curl_addrinfo **paddr) |
497 | 0 | { |
498 | 0 | struct Curl_addrinfo *ai; |
499 | 0 | struct sockaddr_un *sa_un; |
500 | 0 | size_t path_len; |
501 | |
|
502 | 0 | *paddr = NULL; |
503 | | |
504 | | /* sun_path must be able to store the null-terminated path */ |
505 | 0 | path_len = strlen(path) + 1; |
506 | 0 | if(path_len > sizeof(sa_un->sun_path)) |
507 | 0 | return CURLE_TOO_LARGE; |
508 | | |
509 | 0 | ai = curlx_calloc(1, |
510 | 0 | sizeof(struct Curl_addrinfo) + sizeof(struct sockaddr_un)); |
511 | 0 | if(!ai) |
512 | 0 | return CURLE_OUT_OF_MEMORY; |
513 | | |
514 | 0 | ai->ai_addr = (void *)((char *)ai + sizeof(struct Curl_addrinfo)); |
515 | 0 | sa_un = (void *)ai->ai_addr; |
516 | 0 | sa_un->sun_family = AF_UNIX; |
517 | |
|
518 | 0 | ai->ai_family = AF_UNIX; |
519 | 0 | ai->ai_socktype = SOCK_STREAM; /* assume reliable transport for HTTP */ |
520 | 0 | ai->ai_addrlen = (curl_socklen_t) |
521 | 0 | ((offsetof(struct sockaddr_un, sun_path) + path_len) & 0x7FFFFFFF); |
522 | | |
523 | | /* Abstract Unix domain socket have NULL prefix instead of suffix */ |
524 | 0 | if(abstract) |
525 | 0 | memcpy(sa_un->sun_path + 1, path, path_len - 1); |
526 | 0 | else |
527 | 0 | memcpy(sa_un->sun_path, path, path_len); /* copy NUL byte */ |
528 | |
|
529 | 0 | *paddr = ai; |
530 | 0 | return CURLE_OK; |
531 | 0 | } |
532 | | #endif |
533 | | |
534 | | #if defined(CURL_MEMDEBUG) && defined(HAVE_GETADDRINFO) && \ |
535 | | defined(HAVE_FREEADDRINFO) |
536 | | /* |
537 | | * curl_dbg_freeaddrinfo() |
538 | | * |
539 | | * This is strictly for memory tracing and are using the same style as the |
540 | | * family otherwise present in memdebug.c. I put these ones here since they |
541 | | * require a bunch of structs I did not want to include in memdebug.c |
542 | | */ |
543 | | void curl_dbg_freeaddrinfo(struct addrinfo *freethis, |
544 | | int line, const char *source) |
545 | 0 | { |
546 | 0 | curl_dbg_log("ADDR %s:%d freeaddrinfo(%p)\n", |
547 | 0 | source, line, (void *)freethis); |
548 | | #ifdef USE_LWIPSOCK |
549 | | lwip_freeaddrinfo(freethis); |
550 | | #elif defined(USE_FAKE_GETADDRINFO) |
551 | | { |
552 | | const char *env = getenv("CURL_DNS_SERVER"); |
553 | | if(env) |
554 | | r_freeaddrinfo(freethis); |
555 | | else |
556 | | /* !checksrc! disable BANNEDFUNC 1 */ |
557 | | freeaddrinfo(freethis); |
558 | | } |
559 | | #else |
560 | | /* !checksrc! disable BANNEDFUNC 1 */ |
561 | 0 | freeaddrinfo(freethis); |
562 | 0 | #endif |
563 | 0 | } |
564 | | #endif /* CURL_MEMDEBUG && HAVE_FREEADDRINFO */ |
565 | | |
566 | | #if defined(CURL_MEMDEBUG) && defined(HAVE_GETADDRINFO) |
567 | | /* |
568 | | * curl_dbg_getaddrinfo() |
569 | | * |
570 | | * This is strictly for memory tracing and are using the same style as the |
571 | | * family otherwise present in memdebug.c. I put these ones here since they |
572 | | * require a bunch of structs I did not want to include in memdebug.c |
573 | | */ |
574 | | int curl_dbg_getaddrinfo(const char *hostname, |
575 | | const char *service, |
576 | | const struct addrinfo *hints, |
577 | | struct addrinfo **result, |
578 | | int line, const char *source) |
579 | 0 | { |
580 | | #ifdef USE_LWIPSOCK |
581 | | int res = lwip_getaddrinfo(hostname, service, hints, result); |
582 | | #elif defined(USE_FAKE_GETADDRINFO) |
583 | | int res; |
584 | | const char *env = getenv("CURL_DNS_SERVER"); |
585 | | if(env) |
586 | | res = r_getaddrinfo(hostname, service, hints, result); |
587 | | else |
588 | | /* !checksrc! disable BANNEDFUNC 1 */ |
589 | | res = getaddrinfo(hostname, service, hints, result); |
590 | | #else |
591 | | /* !checksrc! disable BANNEDFUNC 1 */ |
592 | 0 | int res = getaddrinfo(hostname, service, hints, result); |
593 | 0 | #endif |
594 | 0 | if(res == 0) |
595 | | /* success */ |
596 | 0 | curl_dbg_log("ADDR %s:%d getaddrinfo() = %p\n", source, line, |
597 | 0 | (void *)*result); |
598 | 0 | else |
599 | 0 | curl_dbg_log("ADDR %s:%d getaddrinfo() failed\n", source, line); |
600 | 0 | return res; |
601 | 0 | } |
602 | | #endif /* CURL_MEMDEBUG && HAVE_GETADDRINFO */ |
603 | | |
604 | | #if defined(HAVE_GETADDRINFO) && defined(USE_RESOLVE_ON_IPS) |
605 | | /* |
606 | | * Work-arounds the sin6_port is always zero bug on iOS 9.3.2 and macOS |
607 | | * 10.11.5. |
608 | | */ |
609 | | void Curl_addrinfo_set_port(struct Curl_addrinfo *addrinfo, int port) |
610 | | { |
611 | | struct Curl_addrinfo *ca; |
612 | | struct sockaddr_in *addr; |
613 | | #ifdef USE_IPV6 |
614 | | struct sockaddr_in6 *addr6; |
615 | | #endif |
616 | | for(ca = addrinfo; ca != NULL; ca = ca->ai_next) { |
617 | | switch(ca->ai_family) { |
618 | | case AF_INET: |
619 | | addr = (void *)ca->ai_addr; /* storage area for this info */ |
620 | | addr->sin_port = htons((unsigned short)port); |
621 | | break; |
622 | | |
623 | | #ifdef USE_IPV6 |
624 | | case AF_INET6: |
625 | | addr6 = (void *)ca->ai_addr; /* storage area for this info */ |
626 | | addr6->sin6_port = htons((unsigned short)port); |
627 | | break; |
628 | | #endif |
629 | | } |
630 | | } |
631 | | } |
632 | | #endif |