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