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