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 addr->sin_family = (CURL_SA_FAMILY_T)(he->h_addrtype);
322 addr->sin_port = htons((unsigned short)port);
323 break;
324
325 #ifdef USE_IPV6
326 case AF_INET6:
327 addr6 = (void *)ai->ai_addr; /* storage area for this info */
328
329 memcpy(&addr6->sin6_addr, curr, sizeof(struct in6_addr));
330 addr6->sin6_family = (CURL_SA_FAMILY_T)(he->h_addrtype);
331 addr6->sin6_port = htons((unsigned short)port);
332 break;
333 #endif
334 }
335
336 prevai = ai;
337 }
338
339 if(result) {
340 Curl_freeaddrinfo(firstai);
341 firstai = NULL;
342 }
343
344 return firstai;
345 }
346 #endif
347
348 /*
349 * Curl_ip2addr()
350 *
351 * This function takes an Internet address, in binary form, as input parameter
352 * along with its address family and the string version of the address, and it
353 * returns a Curl_addrinfo chain filled in correctly with information for the
354 * given address/host
355 */
356
357 struct Curl_addrinfo *
Curl_ip2addr(int af,const void * inaddr,const char * hostname,int port)358 Curl_ip2addr(int af, const void *inaddr, const char *hostname, int port)
359 {
360 struct Curl_addrinfo *ai;
361 size_t addrsize;
362 size_t namelen;
363 struct sockaddr_in *addr;
364 #ifdef USE_IPV6
365 struct sockaddr_in6 *addr6;
366 #endif
367
368 DEBUGASSERT(inaddr && hostname);
369
370 namelen = strlen(hostname) + 1;
371
372 if(af == AF_INET)
373 addrsize = sizeof(struct sockaddr_in);
374 #ifdef USE_IPV6
375 else if(af == AF_INET6)
376 addrsize = sizeof(struct sockaddr_in6);
377 #endif
378 else
379 return NULL;
380
381 /* allocate memory to hold the struct, the address and the name */
382 ai = calloc(1, sizeof(struct Curl_addrinfo) + addrsize + namelen);
383 if(!ai)
384 return NULL;
385 /* put the address after the struct */
386 ai->ai_addr = (void *)((char *)ai + sizeof(struct Curl_addrinfo));
387 /* then put the name after the address */
388 ai->ai_canonname = (char *)ai->ai_addr + addrsize;
389 memcpy(ai->ai_canonname, hostname, namelen);
390 ai->ai_family = af;
391 ai->ai_socktype = SOCK_STREAM;
392 ai->ai_addrlen = (curl_socklen_t)addrsize;
393 /* leave the rest of the struct filled with zero */
394
395 switch(af) {
396 case AF_INET:
397 addr = (void *)ai->ai_addr; /* storage area for this info */
398
399 memcpy(&addr->sin_addr, inaddr, sizeof(struct in_addr));
400 #ifdef __MINGW32__
401 addr->sin_family = (short)af;
402 #else
403 addr->sin_family = (CURL_SA_FAMILY_T)af;
404 #endif
405 addr->sin_port = htons((unsigned short)port);
406 break;
407
408 #ifdef USE_IPV6
409 case AF_INET6:
410 addr6 = (void *)ai->ai_addr; /* storage area for this info */
411
412 memcpy(&addr6->sin6_addr, inaddr, sizeof(struct in6_addr));
413 #ifdef __MINGW32__
414 addr6->sin6_family = (short)af;
415 #else
416 addr6->sin6_family = (CURL_SA_FAMILY_T)af;
417 #endif
418 addr6->sin6_port = htons((unsigned short)port);
419 break;
420 #endif
421 }
422
423 return ai;
424 }
425
426 /*
427 * Given an IPv4 or IPv6 dotted string address, this converts it to a proper
428 * allocated Curl_addrinfo struct and returns it.
429 */
Curl_str2addr(char * address,int port)430 struct Curl_addrinfo *Curl_str2addr(char *address, int port)
431 {
432 struct in_addr in;
433 if(Curl_inet_pton(AF_INET, address, &in) > 0)
434 /* This is a dotted IP address 123.123.123.123-style */
435 return Curl_ip2addr(AF_INET, &in, address, port);
436 #ifdef USE_IPV6
437 {
438 struct in6_addr in6;
439 if(Curl_inet_pton(AF_INET6, address, &in6) > 0)
440 /* This is a dotted IPv6 address ::1-style */
441 return Curl_ip2addr(AF_INET6, &in6, address, port);
442 }
443 #endif
444 return NULL; /* bad input format */
445 }
446
447 #ifdef USE_UNIX_SOCKETS
448 /**
449 * Given a path to a Unix domain socket, return a newly allocated Curl_addrinfo
450 * struct initialized with this path.
451 * Set '*longpath' to TRUE if the error is a too long path.
452 */
Curl_unix2addr(const char * path,bool * longpath,bool abstract)453 struct Curl_addrinfo *Curl_unix2addr(const char *path, bool *longpath,
454 bool abstract)
455 {
456 struct Curl_addrinfo *ai;
457 struct sockaddr_un *sa_un;
458 size_t path_len;
459
460 *longpath = FALSE;
461
462 ai = calloc(1, sizeof(struct Curl_addrinfo) + sizeof(struct sockaddr_un));
463 if(!ai)
464 return NULL;
465 ai->ai_addr = (void *)((char *)ai + sizeof(struct Curl_addrinfo));
466
467 sa_un = (void *) ai->ai_addr;
468 sa_un->sun_family = AF_UNIX;
469
470 /* sun_path must be able to store the NUL-terminated path */
471 path_len = strlen(path) + 1;
472 if(path_len > sizeof(sa_un->sun_path)) {
473 free(ai);
474 *longpath = TRUE;
475 return NULL;
476 }
477
478 ai->ai_family = AF_UNIX;
479 ai->ai_socktype = SOCK_STREAM; /* assume reliable transport for HTTP */
480 ai->ai_addrlen = (curl_socklen_t)
481 ((offsetof(struct sockaddr_un, sun_path) + path_len) & 0x7FFFFFFF);
482
483 /* Abstract Unix domain socket have NULL prefix instead of suffix */
484 if(abstract)
485 memcpy(sa_un->sun_path + 1, path, path_len - 1);
486 else
487 memcpy(sa_un->sun_path, path, path_len); /* copy NUL byte */
488
489 return ai;
490 }
491 #endif
492
493 #if defined(CURLDEBUG) && defined(HAVE_GETADDRINFO) && \
494 defined(HAVE_FREEADDRINFO)
495 /*
496 * curl_dbg_freeaddrinfo()
497 *
498 * This is strictly for memory tracing and are using the same style as the
499 * family otherwise present in memdebug.c. I put these ones here since they
500 * require a bunch of structs I did not want to include in memdebug.c
501 */
502
503 void
curl_dbg_freeaddrinfo(struct addrinfo * freethis,int line,const char * source)504 curl_dbg_freeaddrinfo(struct addrinfo *freethis,
505 int line, const char *source)
506 {
507 curl_dbg_log("ADDR %s:%d freeaddrinfo(%p)\n",
508 source, line, (void *)freethis);
509 #ifdef USE_LWIPSOCK
510 lwip_freeaddrinfo(freethis);
511 #else
512 (freeaddrinfo)(freethis);
513 #endif
514 }
515 #endif /* defined(CURLDEBUG) && defined(HAVE_FREEADDRINFO) */
516
517
518 #if defined(CURLDEBUG) && defined(HAVE_GETADDRINFO)
519 /*
520 * curl_dbg_getaddrinfo()
521 *
522 * This is strictly for memory tracing and are using the same style as the
523 * family otherwise present in memdebug.c. I put these ones here since they
524 * require a bunch of structs I did not want to include in memdebug.c
525 */
526
527 int
curl_dbg_getaddrinfo(const char * hostname,const char * service,const struct addrinfo * hints,struct addrinfo ** result,int line,const char * source)528 curl_dbg_getaddrinfo(const char *hostname,
529 const char *service,
530 const struct addrinfo *hints,
531 struct addrinfo **result,
532 int line, const char *source)
533 {
534 #ifdef USE_LWIPSOCK
535 int res = lwip_getaddrinfo(hostname, service, hints, result);
536 #else
537 int res = (getaddrinfo)(hostname, service, hints, result);
538 #endif
539 if(0 == res)
540 /* success */
541 curl_dbg_log("ADDR %s:%d getaddrinfo() = %p\n",
542 source, line, (void *)*result);
543 else
544 curl_dbg_log("ADDR %s:%d getaddrinfo() failed\n",
545 source, line);
546 return res;
547 }
548 #endif /* defined(CURLDEBUG) && defined(HAVE_GETADDRINFO) */
549
550 #if defined(HAVE_GETADDRINFO) && defined(USE_RESOLVE_ON_IPS)
551 /*
552 * Work-arounds the sin6_port is always zero bug on iOS 9.3.2 and macOS
553 * 10.11.5.
554 */
Curl_addrinfo_set_port(struct Curl_addrinfo * addrinfo,int port)555 void Curl_addrinfo_set_port(struct Curl_addrinfo *addrinfo, int port)
556 {
557 struct Curl_addrinfo *ca;
558 struct sockaddr_in *addr;
559 #ifdef USE_IPV6
560 struct sockaddr_in6 *addr6;
561 #endif
562 for(ca = addrinfo; ca != NULL; ca = ca->ai_next) {
563 switch(ca->ai_family) {
564 case AF_INET:
565 addr = (void *)ca->ai_addr; /* storage area for this info */
566 addr->sin_port = htons((unsigned short)port);
567 break;
568
569 #ifdef USE_IPV6
570 case AF_INET6:
571 addr6 = (void *)ca->ai_addr; /* storage area for this info */
572 addr6->sin6_port = htons((unsigned short)port);
573 break;
574 #endif
575 }
576 }
577 }
578 #endif
579