Curl_getaddrinfo_ex: sanitize function results.
[platform/upstream/curl.git] / lib / curl_addrinfo.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2010, 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 http://curl.haxx.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 "setup.h"
24
25 #include <curl/curl.h>
26
27 #ifdef HAVE_SYS_SOCKET_H
28 #  include <sys/socket.h>
29 #endif
30 #ifdef HAVE_NETINET_IN_H
31 #  include <netinet/in.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
40 #ifdef __VMS
41 #  include <in.h>
42 #  include <inet.h>
43 #  include <stdlib.h>
44 #endif
45
46 #if defined(NETWARE) && defined(__NOVELL_LIBC__)
47 #  undef  in_addr_t
48 #  define in_addr_t unsigned long
49 #endif
50
51 #include "curl_addrinfo.h"
52 #include "inet_pton.h"
53
54 #define _MPRINTF_REPLACE /* use our functions only */
55 #include <curl/mprintf.h>
56
57 #include "curl_memory.h"
58 /* The last #include file should be: */
59 #include "memdebug.h"
60
61
62 /*
63  * Curl_freeaddrinfo()
64  *
65  * This is used to free a linked list of Curl_addrinfo structs along
66  * with all its associated allocated storage. This function should be
67  * called once for each successful call to Curl_getaddrinfo_ex() or to
68  * any function call which actually allocates a Curl_addrinfo struct.
69  */
70
71 #if defined(__INTEL_COMPILER) && (__INTEL_COMPILER == 910) && \
72     defined(__OPTIMIZE__) && defined(__unix__) &&  defined(__i386__)
73   /* workaround icc 9.1 optimizer issue */
74 # define vqualifier volatile
75 #else
76 # define vqualifier
77 #endif
78
79 void
80 Curl_freeaddrinfo(Curl_addrinfo *cahead)
81 {
82   Curl_addrinfo *vqualifier canext;
83   Curl_addrinfo *ca;
84
85   for(ca = cahead; ca != NULL; ca = canext) {
86
87     if(ca->ai_addr)
88       free(ca->ai_addr);
89
90     if(ca->ai_canonname)
91       free(ca->ai_canonname);
92
93     canext = ca->ai_next;
94
95     free(ca);
96   }
97 }
98
99
100 #ifdef HAVE_GETADDRINFO
101 /*
102  * Curl_getaddrinfo_ex()
103  *
104  * This is a wrapper function around system's getaddrinfo(), with
105  * the only difference that instead of returning a linked list of
106  * addrinfo structs this one returns a linked list of Curl_addrinfo
107  * ones. The memory allocated by this function *MUST* be free'd with
108  * Curl_freeaddrinfo().  For each successful call to this function
109  * there must be an associated call later to Curl_freeaddrinfo().
110  *
111  * There should be no single call to system's getaddrinfo() in the
112  * whole library, any such call should be 'routed' through this one.
113  */
114
115 int
116 Curl_getaddrinfo_ex(const char *nodename,
117                     const char *servname,
118                     const struct addrinfo *hints,
119                     Curl_addrinfo **result)
120 {
121   const struct addrinfo *ai;
122   struct addrinfo *aihead;
123   Curl_addrinfo *cafirst = NULL;
124   Curl_addrinfo *calast = NULL;
125   Curl_addrinfo *ca;
126   size_t ss_size;
127   int error;
128
129   *result = NULL; /* assume failure */
130
131   error = getaddrinfo(nodename, servname, hints, &aihead);
132   if(error)
133     return error;
134
135   /* traverse the addrinfo list */
136
137   for(ai = aihead; ai != NULL; ai = ai->ai_next) {
138
139     /* ignore elements with unsupported address family, */
140     /* settle family-specific sockaddr structure size.  */
141     if(ai->ai_family == AF_INET)
142       ss_size = sizeof(struct sockaddr_in);
143 #ifdef ENABLE_IPV6
144     else if(ai->ai_family == AF_INET6)
145       ss_size = sizeof(struct sockaddr_in6);
146 #endif
147     else
148       continue;
149
150     /* ignore elements without required address info */
151     if((ai->ai_addr == NULL) || !(ai->ai_addrlen > 0))
152       continue;
153
154     /* ignore elements with bogus address size */
155     if((size_t)ai->ai_addrlen < ss_size)
156       continue;
157
158     if((ca = malloc(sizeof(Curl_addrinfo))) == NULL) {
159       error = EAI_MEMORY;
160       break;
161     }
162
163     /* copy each structure member individually, member ordering, */
164     /* size, or padding might be different for each platform.    */
165
166     ca->ai_flags     = ai->ai_flags;
167     ca->ai_family    = ai->ai_family;
168     ca->ai_socktype  = ai->ai_socktype;
169     ca->ai_protocol  = ai->ai_protocol;
170     ca->ai_addrlen   = (curl_socklen_t)ss_size;
171     ca->ai_addr      = NULL;
172     ca->ai_canonname = NULL;
173     ca->ai_next      = NULL;
174
175     if((ca->ai_addr = malloc(ss_size)) == NULL) {
176       error = EAI_MEMORY;
177       free(ca);
178       break;
179     }
180     memcpy(ca->ai_addr, ai->ai_addr, ss_size);
181
182     if(ai->ai_canonname != NULL) {
183       if((ca->ai_canonname = strdup(ai->ai_canonname)) == NULL) {
184         error = EAI_MEMORY;
185         free(ca->ai_addr);
186         free(ca);
187         break;
188       }
189     }
190
191     /* if the return list is empty, this becomes the first element */
192     if(!cafirst)
193       cafirst = ca;
194
195     /* add this element last in the return list */
196     if(calast)
197       calast->ai_next = ca;
198     calast = ca;
199
200   }
201
202   /* destroy the addrinfo list */
203   if(aihead)
204     freeaddrinfo(aihead);
205
206   /* if we failed, also destroy the Curl_addrinfo list */
207   if(error) {
208     Curl_freeaddrinfo(cafirst);
209     cafirst = NULL;
210   }
211
212   *result = cafirst;
213
214   /* This is not a CURLcode */
215   return error;
216 }
217 #endif /* HAVE_GETADDRINFO */
218
219
220 /*
221  * Curl_he2ai()
222  *
223  * This function returns a pointer to the first element of a newly allocated
224  * Curl_addrinfo struct linked list filled with the data of a given hostent.
225  * Curl_addrinfo is meant to work like the addrinfo struct does for a IPv6
226  * stack, but usable also for IPv4, all hosts and environments.
227  *
228  * The memory allocated by this function *MUST* be free'd later on calling
229  * Curl_freeaddrinfo().  For each successful call to this function there
230  * must be an associated call later to Curl_freeaddrinfo().
231  *
232  *   Curl_addrinfo defined in "lib/curl_addrinfo.h"
233  *
234  *     struct Curl_addrinfo {
235  *       int                   ai_flags;
236  *       int                   ai_family;
237  *       int                   ai_socktype;
238  *       int                   ai_protocol;
239  *       curl_socklen_t        ai_addrlen;   * Follow rfc3493 struct addrinfo *
240  *       char                 *ai_canonname;
241  *       struct sockaddr      *ai_addr;
242  *       struct Curl_addrinfo *ai_next;
243  *     };
244  *     typedef struct Curl_addrinfo Curl_addrinfo;
245  *
246  *   hostent defined in <netdb.h>
247  *
248  *     struct hostent {
249  *       char    *h_name;
250  *       char    **h_aliases;
251  *       int     h_addrtype;
252  *       int     h_length;
253  *       char    **h_addr_list;
254  *     };
255  *
256  *   for backward compatibility:
257  *
258  *     #define h_addr  h_addr_list[0]
259  */
260
261 Curl_addrinfo *
262 Curl_he2ai(const struct hostent *he, int port)
263 {
264   Curl_addrinfo *ai;
265   Curl_addrinfo *prevai = NULL;
266   Curl_addrinfo *firstai = NULL;
267   struct sockaddr_in *addr;
268 #ifdef ENABLE_IPV6
269   struct sockaddr_in6 *addr6;
270 #endif
271   CURLcode result = CURLE_OK;
272   int i;
273   char *curr;
274
275   if(!he)
276     /* no input == no output! */
277     return NULL;
278
279   DEBUGASSERT((he->h_name != NULL) && (he->h_addr_list != NULL));
280
281   for(i=0; (curr = he->h_addr_list[i]) != NULL; i++) {
282
283     size_t ss_size;
284 #ifdef ENABLE_IPV6
285     if (he->h_addrtype == AF_INET6)
286       ss_size = sizeof (struct sockaddr_in6);
287     else
288 #endif
289       ss_size = sizeof (struct sockaddr_in);
290
291     if((ai = calloc(1, sizeof(Curl_addrinfo))) == NULL) {
292       result = CURLE_OUT_OF_MEMORY;
293       break;
294     }
295     if((ai->ai_canonname = strdup(he->h_name)) == NULL) {
296       result = CURLE_OUT_OF_MEMORY;
297       free(ai);
298       break;
299     }
300     if((ai->ai_addr = calloc(1, ss_size)) == NULL) {
301       result = CURLE_OUT_OF_MEMORY;
302       free(ai->ai_canonname);
303       free(ai);
304       break;
305     }
306
307     if(!firstai)
308       /* store the pointer we want to return from this function */
309       firstai = ai;
310
311     if(prevai)
312       /* make the previous entry point to this */
313       prevai->ai_next = ai;
314
315     ai->ai_family = he->h_addrtype;
316
317     /* we return all names as STREAM, so when using this address for TFTP
318        the type must be ignored and conn->socktype be used instead! */
319     ai->ai_socktype = SOCK_STREAM;
320
321     ai->ai_addrlen = (curl_socklen_t)ss_size;
322
323     /* leave the rest of the struct filled with zero */
324
325     switch (ai->ai_family) {
326     case AF_INET:
327       addr = (void *)ai->ai_addr; /* storage area for this info */
328
329       memcpy(&addr->sin_addr, curr, sizeof(struct in_addr));
330       addr->sin_family = (unsigned short)(he->h_addrtype);
331       addr->sin_port = htons((unsigned short)port);
332       break;
333
334 #ifdef ENABLE_IPV6
335     case AF_INET6:
336       addr6 = (void *)ai->ai_addr; /* storage area for this info */
337
338       memcpy(&addr6->sin6_addr, curr, sizeof(struct in6_addr));
339       addr6->sin6_family = (unsigned short)(he->h_addrtype);
340       addr6->sin6_port = htons((unsigned short)port);
341       break;
342 #endif
343     }
344
345     prevai = ai;
346   }
347
348   if(result != CURLE_OK) {
349     Curl_freeaddrinfo(firstai);
350     firstai = NULL;
351   }
352
353   return firstai;
354 }
355
356
357 struct namebuff {
358   struct hostent hostentry;
359   union {
360     struct in_addr  ina4;
361 #ifdef ENABLE_IPV6
362     struct in6_addr ina6;
363 #endif
364   } addrentry;
365   char *h_addr_list[2];
366 };
367
368
369 /*
370  * Curl_ip2addr()
371  *
372  * This function takes an internet address, in binary form, as input parameter
373  * along with its address family and the string version of the address, and it
374  * returns a Curl_addrinfo chain filled in correctly with information for the
375  * given address/host
376  */
377
378 Curl_addrinfo *
379 Curl_ip2addr(int af, const void *inaddr, const char *hostname, int port)
380 {
381   Curl_addrinfo *ai;
382
383 #if defined(__VMS) && \
384     defined(__INITIAL_POINTER_SIZE) && (__INITIAL_POINTER_SIZE == 64)
385 #pragma pointer_size save
386 #pragma pointer_size short
387 #pragma message disable PTRMISMATCH
388 #endif
389
390   struct hostent  *h;
391   struct namebuff *buf;
392   char  *addrentry;
393   char  *hoststr;
394   size_t addrsize;
395
396   DEBUGASSERT(inaddr && hostname);
397
398   buf = malloc(sizeof(struct namebuff));
399   if(!buf)
400     return NULL;
401
402   hoststr = strdup(hostname);
403   if(!hoststr) {
404     free(buf);
405     return NULL;
406   }
407
408   switch(af) {
409   case AF_INET:
410     addrsize = sizeof(struct in_addr);
411     addrentry = (void *)&buf->addrentry.ina4;
412     memcpy(addrentry, inaddr, sizeof(struct in_addr));
413     break;
414 #ifdef ENABLE_IPV6
415   case AF_INET6:
416     addrsize = sizeof(struct in6_addr);
417     addrentry = (void *)&buf->addrentry.ina6;
418     memcpy(addrentry, inaddr, sizeof(struct in6_addr));
419     break;
420 #endif
421   default:
422     free(hoststr);
423     free(buf);
424     return NULL;
425   }
426
427   h = &buf->hostentry;
428   h->h_name = hoststr;
429   h->h_aliases = NULL;
430   h->h_addrtype = (short)af;
431   h->h_length = (short)addrsize;
432   h->h_addr_list = &buf->h_addr_list[0];
433   h->h_addr_list[0] = addrentry;
434   h->h_addr_list[1] = NULL; /* terminate list of entries */
435
436 #if defined(__VMS) && \
437     defined(__INITIAL_POINTER_SIZE) && (__INITIAL_POINTER_SIZE == 64)
438 #pragma pointer_size restore
439 #pragma message enable PTRMISMATCH
440 #endif
441
442   ai = Curl_he2ai(h, port);
443
444   free(hoststr);
445   free(buf);
446
447   return ai;
448 }
449
450 /*
451  * Given an IPv4 or IPv6 dotted string address, this converts it to a proper
452  * allocated Curl_addrinfo struct and returns it.
453  */
454 Curl_addrinfo *Curl_str2addr(char *address, int port)
455 {
456   struct in_addr in;
457   if(Curl_inet_pton(AF_INET, address, &in) > 0)
458     /* This is a dotted IP address 123.123.123.123-style */
459     return Curl_ip2addr(AF_INET, &in, address, port);
460 #ifdef ENABLE_IPV6
461   else {
462     struct in6_addr in6;
463     if(Curl_inet_pton(AF_INET6, address, &in6) > 0)
464       /* This is a dotted IPv6 address ::1-style */
465       return Curl_ip2addr(AF_INET6, &in6, address, port);
466   }
467 #endif
468   return NULL; /* bad input format */
469 }
470
471 #if defined(CURLDEBUG) && defined(HAVE_FREEADDRINFO)
472 /*
473  * curl_dofreeaddrinfo()
474  *
475  * This is strictly for memory tracing and are using the same style as the
476  * family otherwise present in memdebug.c. I put these ones here since they
477  * require a bunch of structs I didn't wanna include in memdebug.c
478  */
479
480 void
481 curl_dofreeaddrinfo(struct addrinfo *freethis,
482                     int line, const char *source)
483 {
484   (freeaddrinfo)(freethis);
485   curl_memlog("ADDR %s:%d freeaddrinfo(%p)\n",
486               source, line, (void *)freethis);
487 }
488 #endif /* defined(CURLDEBUG) && defined(HAVE_FREEADDRINFO) */
489
490
491 #if defined(CURLDEBUG) && defined(HAVE_GETADDRINFO)
492 /*
493  * curl_dogetaddrinfo()
494  *
495  * This is strictly for memory tracing and are using the same style as the
496  * family otherwise present in memdebug.c. I put these ones here since they
497  * require a bunch of structs I didn't wanna include in memdebug.c
498  */
499
500 int
501 curl_dogetaddrinfo(const char *hostname,
502                    const char *service,
503                    const struct addrinfo *hints,
504                    struct addrinfo **result,
505                    int line, const char *source)
506 {
507   int res=(getaddrinfo)(hostname, service, hints, result);
508   if(0 == res)
509     /* success */
510     curl_memlog("ADDR %s:%d getaddrinfo() = %p\n",
511                 source, line, (void *)*result);
512   else
513     curl_memlog("ADDR %s:%d getaddrinfo() failed\n",
514                 source, line);
515   return res;
516 }
517 #endif /* defined(CURLDEBUG) && defined(HAVE_GETADDRINFO) */
518