• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 ENABLE_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 struct Curl_addrinfo *
Curl_he2ai(const struct hostent * he,int port)256 Curl_he2ai(const struct hostent *he, int port)
257 {
258   struct Curl_addrinfo *ai;
259   struct Curl_addrinfo *prevai = NULL;
260   struct Curl_addrinfo *firstai = NULL;
261   struct sockaddr_in *addr;
262 #ifdef ENABLE_IPV6
263   struct sockaddr_in6 *addr6;
264 #endif
265   CURLcode result = CURLE_OK;
266   int i;
267   char *curr;
268 
269   if(!he)
270     /* no input == no output! */
271     return NULL;
272 
273   DEBUGASSERT((he->h_name != NULL) && (he->h_addr_list != NULL));
274 
275   for(i = 0; (curr = he->h_addr_list[i]) != NULL; i++) {
276     size_t ss_size;
277     size_t namelen = strlen(he->h_name) + 1; /* include null-terminatior */
278 #ifdef ENABLE_IPV6
279     if(he->h_addrtype == AF_INET6)
280       ss_size = sizeof(struct sockaddr_in6);
281     else
282 #endif
283       ss_size = sizeof(struct sockaddr_in);
284 
285     /* allocate memory to hold the struct, the address and the name */
286     ai = calloc(1, sizeof(struct Curl_addrinfo) + ss_size + namelen);
287     if(!ai) {
288       result = CURLE_OUT_OF_MEMORY;
289       break;
290     }
291     /* put the address after the struct */
292     ai->ai_addr = (void *)((char *)ai + sizeof(struct Curl_addrinfo));
293     /* then put the name after the address */
294     ai->ai_canonname = (char *)ai->ai_addr + ss_size;
295     memcpy(ai->ai_canonname, he->h_name, namelen);
296 
297     if(!firstai)
298       /* store the pointer we want to return from this function */
299       firstai = ai;
300 
301     if(prevai)
302       /* make the previous entry point to this */
303       prevai->ai_next = ai;
304 
305     ai->ai_family = he->h_addrtype;
306 
307     /* we return all names as STREAM, so when using this address for TFTP
308        the type must be ignored and conn->socktype be used instead! */
309     ai->ai_socktype = SOCK_STREAM;
310 
311     ai->ai_addrlen = (curl_socklen_t)ss_size;
312 
313     /* leave the rest of the struct filled with zero */
314 
315     switch(ai->ai_family) {
316     case AF_INET:
317       addr = (void *)ai->ai_addr; /* storage area for this info */
318 
319       memcpy(&addr->sin_addr, curr, sizeof(struct in_addr));
320       addr->sin_family = (CURL_SA_FAMILY_T)(he->h_addrtype);
321       addr->sin_port = htons((unsigned short)port);
322       break;
323 
324 #ifdef ENABLE_IPV6
325     case AF_INET6:
326       addr6 = (void *)ai->ai_addr; /* storage area for this info */
327 
328       memcpy(&addr6->sin6_addr, curr, sizeof(struct in6_addr));
329       addr6->sin6_family = (CURL_SA_FAMILY_T)(he->h_addrtype);
330       addr6->sin6_port = htons((unsigned short)port);
331       break;
332 #endif
333     }
334 
335     prevai = ai;
336   }
337 
338   if(result) {
339     Curl_freeaddrinfo(firstai);
340     firstai = NULL;
341   }
342 
343   return firstai;
344 }
345 
346 
347 struct namebuff {
348   struct hostent hostentry;
349   union {
350     struct in_addr  ina4;
351 #ifdef ENABLE_IPV6
352     struct in6_addr ina6;
353 #endif
354   } addrentry;
355   char *h_addr_list[2];
356 };
357 
358 
359 /*
360  * Curl_ip2addr()
361  *
362  * This function takes an internet address, in binary form, as input parameter
363  * along with its address family and the string version of the address, and it
364  * returns a Curl_addrinfo chain filled in correctly with information for the
365  * given address/host
366  */
367 
368 struct Curl_addrinfo *
Curl_ip2addr(int af,const void * inaddr,const char * hostname,int port)369 Curl_ip2addr(int af, const void *inaddr, const char *hostname, int port)
370 {
371   struct Curl_addrinfo *ai;
372 
373 #if defined(__VMS) && \
374     defined(__INITIAL_POINTER_SIZE) && (__INITIAL_POINTER_SIZE == 64)
375 #pragma pointer_size save
376 #pragma pointer_size short
377 #pragma message disable PTRMISMATCH
378 #endif
379 
380   struct hostent  *h;
381   struct namebuff *buf;
382   char  *addrentry;
383   char  *hoststr;
384   size_t addrsize;
385 
386   DEBUGASSERT(inaddr && hostname);
387 
388   buf = malloc(sizeof(struct namebuff));
389   if(!buf)
390     return NULL;
391 
392   hoststr = strdup(hostname);
393   if(!hoststr) {
394     free(buf);
395     return NULL;
396   }
397 
398   switch(af) {
399   case AF_INET:
400     addrsize = sizeof(struct in_addr);
401     addrentry = (void *)&buf->addrentry.ina4;
402     memcpy(addrentry, inaddr, sizeof(struct in_addr));
403     break;
404 #ifdef ENABLE_IPV6
405   case AF_INET6:
406     addrsize = sizeof(struct in6_addr);
407     addrentry = (void *)&buf->addrentry.ina6;
408     memcpy(addrentry, inaddr, sizeof(struct in6_addr));
409     break;
410 #endif
411   default:
412     free(hoststr);
413     free(buf);
414     return NULL;
415   }
416 
417   h = &buf->hostentry;
418   h->h_name = hoststr;
419   h->h_aliases = NULL;
420   h->h_addrtype = (short)af;
421   h->h_length = (short)addrsize;
422   h->h_addr_list = &buf->h_addr_list[0];
423   h->h_addr_list[0] = addrentry;
424   h->h_addr_list[1] = NULL; /* terminate list of entries */
425 
426 #if defined(__VMS) && \
427     defined(__INITIAL_POINTER_SIZE) && (__INITIAL_POINTER_SIZE == 64)
428 #pragma pointer_size restore
429 #pragma message enable PTRMISMATCH
430 #endif
431 
432   ai = Curl_he2ai(h, port);
433 
434   free(hoststr);
435   free(buf);
436 
437   return ai;
438 }
439 
440 /*
441  * Given an IPv4 or IPv6 dotted string address, this converts it to a proper
442  * allocated Curl_addrinfo struct and returns it.
443  */
Curl_str2addr(char * address,int port)444 struct Curl_addrinfo *Curl_str2addr(char *address, int port)
445 {
446   struct in_addr in;
447   if(Curl_inet_pton(AF_INET, address, &in) > 0)
448     /* This is a dotted IP address 123.123.123.123-style */
449     return Curl_ip2addr(AF_INET, &in, address, port);
450 #ifdef ENABLE_IPV6
451   {
452     struct in6_addr in6;
453     if(Curl_inet_pton(AF_INET6, address, &in6) > 0)
454       /* This is a dotted IPv6 address ::1-style */
455       return Curl_ip2addr(AF_INET6, &in6, address, port);
456   }
457 #endif
458   return NULL; /* bad input format */
459 }
460 
461 #ifdef USE_UNIX_SOCKETS
462 /**
463  * Given a path to a Unix domain socket, return a newly allocated Curl_addrinfo
464  * struct initialized with this path.
465  * Set '*longpath' to TRUE if the error is a too long path.
466  */
Curl_unix2addr(const char * path,bool * longpath,bool abstract)467 struct Curl_addrinfo *Curl_unix2addr(const char *path, bool *longpath,
468                                      bool abstract)
469 {
470   struct Curl_addrinfo *ai;
471   struct sockaddr_un *sa_un;
472   size_t path_len;
473 
474   *longpath = FALSE;
475 
476   ai = calloc(1, sizeof(struct Curl_addrinfo) + sizeof(struct sockaddr_un));
477   if(!ai)
478     return NULL;
479   ai->ai_addr = (void *)((char *)ai + sizeof(struct Curl_addrinfo));
480 
481   sa_un = (void *) ai->ai_addr;
482   sa_un->sun_family = AF_UNIX;
483 
484   /* sun_path must be able to store the NUL-terminated path */
485   path_len = strlen(path) + 1;
486   if(path_len > sizeof(sa_un->sun_path)) {
487     free(ai);
488     *longpath = TRUE;
489     return NULL;
490   }
491 
492   ai->ai_family = AF_UNIX;
493   ai->ai_socktype = SOCK_STREAM; /* assume reliable transport for HTTP */
494   ai->ai_addrlen = (curl_socklen_t)
495     ((offsetof(struct sockaddr_un, sun_path) + path_len) & 0x7FFFFFFF);
496 
497   /* Abstract Unix domain socket have NULL prefix instead of suffix */
498   if(abstract)
499     memcpy(sa_un->sun_path + 1, path, path_len - 1);
500   else
501     memcpy(sa_un->sun_path, path, path_len); /* copy NUL byte */
502 
503   return ai;
504 }
505 #endif
506 
507 #if defined(CURLDEBUG) && defined(HAVE_GETADDRINFO) &&  \
508   defined(HAVE_FREEADDRINFO)
509 /*
510  * curl_dbg_freeaddrinfo()
511  *
512  * This is strictly for memory tracing and are using the same style as the
513  * family otherwise present in memdebug.c. I put these ones here since they
514  * require a bunch of structs I didn't want to include in memdebug.c
515  */
516 
517 void
curl_dbg_freeaddrinfo(struct addrinfo * freethis,int line,const char * source)518 curl_dbg_freeaddrinfo(struct addrinfo *freethis,
519                       int line, const char *source)
520 {
521   curl_dbg_log("ADDR %s:%d freeaddrinfo(%p)\n",
522                source, line, (void *)freethis);
523 #ifdef USE_LWIPSOCK
524   lwip_freeaddrinfo(freethis);
525 #else
526   (freeaddrinfo)(freethis);
527 #endif
528 }
529 #endif /* defined(CURLDEBUG) && defined(HAVE_FREEADDRINFO) */
530 
531 
532 #if defined(CURLDEBUG) && defined(HAVE_GETADDRINFO)
533 /*
534  * curl_dbg_getaddrinfo()
535  *
536  * This is strictly for memory tracing and are using the same style as the
537  * family otherwise present in memdebug.c. I put these ones here since they
538  * require a bunch of structs I didn't want to include in memdebug.c
539  */
540 
541 int
curl_dbg_getaddrinfo(const char * hostname,const char * service,const struct addrinfo * hints,struct addrinfo ** result,int line,const char * source)542 curl_dbg_getaddrinfo(const char *hostname,
543                     const char *service,
544                     const struct addrinfo *hints,
545                     struct addrinfo **result,
546                     int line, const char *source)
547 {
548 #ifdef USE_LWIPSOCK
549   int res = lwip_getaddrinfo(hostname, service, hints, result);
550 #else
551   int res = (getaddrinfo)(hostname, service, hints, result);
552 #endif
553   if(0 == res)
554     /* success */
555     curl_dbg_log("ADDR %s:%d getaddrinfo() = %p\n",
556                  source, line, (void *)*result);
557   else
558     curl_dbg_log("ADDR %s:%d getaddrinfo() failed\n",
559                  source, line);
560   return res;
561 }
562 #endif /* defined(CURLDEBUG) && defined(HAVE_GETADDRINFO) */
563 
564 #if defined(HAVE_GETADDRINFO) && defined(USE_RESOLVE_ON_IPS)
565 /*
566  * Work-arounds the sin6_port is always zero bug on iOS 9.3.2 and Mac OS X
567  * 10.11.5.
568  */
Curl_addrinfo_set_port(struct Curl_addrinfo * addrinfo,int port)569 void Curl_addrinfo_set_port(struct Curl_addrinfo *addrinfo, int port)
570 {
571   struct Curl_addrinfo *ca;
572   struct sockaddr_in *addr;
573 #ifdef ENABLE_IPV6
574   struct sockaddr_in6 *addr6;
575 #endif
576   for(ca = addrinfo; ca != NULL; ca = ca->ai_next) {
577     switch(ca->ai_family) {
578     case AF_INET:
579       addr = (void *)ca->ai_addr; /* storage area for this info */
580       addr->sin_port = htons((unsigned short)port);
581       break;
582 
583 #ifdef ENABLE_IPV6
584     case AF_INET6:
585       addr6 = (void *)ca->ai_addr; /* storage area for this info */
586       addr6->sin6_port = htons((unsigned short)port);
587       break;
588 #endif
589     }
590   }
591 }
592 #endif
593