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