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