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