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