1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2010, Daniel Stenberg, <daniel@haxx.se>, et al.
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.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
25 #include <curl/curl.h>
27 #ifdef HAVE_SYS_SOCKET_H
28 # include <sys/socket.h>
30 #ifdef HAVE_NETINET_IN_H
31 # include <netinet/in.h>
36 #ifdef HAVE_ARPA_INET_H
37 # include <arpa/inet.h>
46 #if defined(NETWARE) && defined(__NOVELL_LIBC__)
48 # define in_addr_t unsigned long
51 #include "curl_addrinfo.h"
53 #define _MPRINTF_REPLACE /* use our functions only */
54 #include <curl/mprintf.h>
56 #include "curl_memory.h"
57 /* The last #include file should be: */
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.
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
79 Curl_freeaddrinfo(Curl_addrinfo *cahead)
81 Curl_addrinfo *vqualifier canext;
84 for(ca = cahead; ca != NULL; ca = canext) {
90 free(ca->ai_canonname);
99 #ifdef HAVE_GETADDRINFO
101 * Curl_getaddrinfo_ex()
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().
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.
115 Curl_getaddrinfo_ex(const char *nodename,
116 const char *servname,
117 const struct addrinfo *hints,
118 Curl_addrinfo **result)
120 const struct addrinfo *ainext;
121 const struct addrinfo *ai;
122 struct addrinfo *aihead;
123 Curl_addrinfo *cafirst = NULL;
124 Curl_addrinfo *calast = NULL;
128 *result = NULL; /* assume failure */
130 error = getaddrinfo(nodename, servname, hints, &aihead);
134 for(ai = aihead; ai != NULL; ai = ainext) {
136 if((ca = malloc(sizeof(Curl_addrinfo))) == NULL) {
141 /* copy each structure member individually, member ordering, */
142 /* size, or padding might be different for each structure. */
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;
150 ca->ai_canonname = NULL;
153 if((ai->ai_addrlen > 0) && (ai->ai_addr != NULL)) {
154 ca->ai_addrlen = ai->ai_addrlen;
155 if((ca->ai_addr = malloc(ca->ai_addrlen)) == NULL) {
160 memcpy(ca->ai_addr, ai->ai_addr, ca->ai_addrlen);
163 if(ai->ai_canonname != NULL) {
164 if((ca->ai_canonname = strdup(ai->ai_canonname)) == NULL) {
173 /* if the return list is empty, this becomes the first element */
177 /* add this element last in the return list */
179 calast->ai_next = ca;
182 /* fetch next element fom the addrinfo list */
183 ainext = ai->ai_next;
186 /* destroy the addrinfo list */
188 freeaddrinfo(aihead);
190 /* if we failed, also destroy the Curl_addrinfo list */
192 Curl_freeaddrinfo(cafirst);
198 /* This is not a CURLcode */
201 #endif /* HAVE_GETADDRINFO */
207 * This function returns a pointer to the first element of a newly allocated
208 * Curl_addrinfo struct linked list filled with the data of a given hostent.
209 * Curl_addrinfo is meant to work like the addrinfo struct does for a IPv6
210 * stack, but usable also for IPv4, all hosts and environments.
212 * The memory allocated by this function *MUST* be free'd later on calling
213 * Curl_freeaddrinfo(). For each successful call to this function there
214 * must be an associated call later to Curl_freeaddrinfo().
216 * Curl_addrinfo defined in "lib/curl_addrinfo.h"
218 * struct Curl_addrinfo {
223 * curl_socklen_t ai_addrlen; * Follow rfc3493 struct addrinfo *
224 * char *ai_canonname;
225 * struct sockaddr *ai_addr;
226 * struct Curl_addrinfo *ai_next;
228 * typedef struct Curl_addrinfo Curl_addrinfo;
230 * hostent defined in <netdb.h>
237 * char **h_addr_list;
240 * for backward compatibility:
242 * #define h_addr h_addr_list[0]
246 Curl_he2ai(const struct hostent *he, int port)
249 Curl_addrinfo *prevai = NULL;
250 Curl_addrinfo *firstai = NULL;
251 struct sockaddr_in *addr;
253 struct sockaddr_in6 *addr6;
255 CURLcode result = CURLE_OK;
260 /* no input == no output! */
263 DEBUGASSERT((he->h_name != NULL) && (he->h_addr_list != NULL));
265 for(i=0; (curr = he->h_addr_list[i]) != NULL; i++) {
269 if (he->h_addrtype == AF_INET6)
270 ss_size = sizeof (struct sockaddr_in6);
273 ss_size = sizeof (struct sockaddr_in);
275 if((ai = calloc(1, sizeof(Curl_addrinfo))) == NULL) {
276 result = CURLE_OUT_OF_MEMORY;
279 if((ai->ai_canonname = strdup(he->h_name)) == NULL) {
280 result = CURLE_OUT_OF_MEMORY;
284 if((ai->ai_addr = calloc(1, ss_size)) == NULL) {
285 result = CURLE_OUT_OF_MEMORY;
286 free(ai->ai_canonname);
292 /* store the pointer we want to return from this function */
296 /* make the previous entry point to this */
297 prevai->ai_next = ai;
299 ai->ai_family = he->h_addrtype;
301 /* we return all names as STREAM, so when using this address for TFTP
302 the type must be ignored and conn->socktype be used instead! */
303 ai->ai_socktype = SOCK_STREAM;
305 ai->ai_addrlen = (curl_socklen_t)ss_size;
307 /* leave the rest of the struct filled with zero */
309 switch (ai->ai_family) {
311 addr = (void *)ai->ai_addr; /* storage area for this info */
313 memcpy(&addr->sin_addr, curr, sizeof(struct in_addr));
314 addr->sin_family = (unsigned short)(he->h_addrtype);
315 addr->sin_port = htons((unsigned short)port);
320 addr6 = (void *)ai->ai_addr; /* storage area for this info */
322 memcpy(&addr6->sin6_addr, curr, sizeof(struct in6_addr));
323 addr6->sin6_family = (unsigned short)(he->h_addrtype);
324 addr6->sin6_port = htons((unsigned short)port);
332 if(result != CURLE_OK) {
333 Curl_freeaddrinfo(firstai);
342 struct hostent hostentry;
346 struct in6_addr ina6;
349 char *h_addr_list[2];
356 * This function takes an internet address, in binary form, as input parameter
357 * along with its address family and the string version of the address, and it
358 * returns a Curl_addrinfo chain filled in correctly with information for the
363 Curl_ip2addr(int af, const void *inaddr, const char *hostname, int port)
367 #if defined(__VMS) && \
368 defined(__INITIAL_POINTER_SIZE) && (__INITIAL_POINTER_SIZE == 64)
369 #pragma pointer_size save
370 #pragma pointer_size short
371 #pragma message disable PTRMISMATCH
375 struct namebuff *buf;
380 DEBUGASSERT(inaddr && hostname);
382 buf = malloc(sizeof(struct namebuff));
386 hoststr = strdup(hostname);
394 addrsize = sizeof(struct in_addr);
395 addrentry = (void *)&buf->addrentry.ina4;
396 memcpy(addrentry, inaddr, sizeof(struct in_addr));
400 addrsize = sizeof(struct in6_addr);
401 addrentry = (void *)&buf->addrentry.ina6;
402 memcpy(addrentry, inaddr, sizeof(struct in6_addr));
414 h->h_addrtype = (short)af;
415 h->h_length = (short)addrsize;
416 h->h_addr_list = &buf->h_addr_list[0];
417 h->h_addr_list[0] = addrentry;
418 h->h_addr_list[1] = NULL; /* terminate list of entries */
420 #if defined(__VMS) && \
421 defined(__INITIAL_POINTER_SIZE) && (__INITIAL_POINTER_SIZE == 64)
422 #pragma pointer_size restore
423 #pragma message enable PTRMISMATCH
426 ai = Curl_he2ai(h, port);
435 #if defined(CURLDEBUG) && defined(HAVE_FREEADDRINFO)
437 * curl_dofreeaddrinfo()
439 * This is strictly for memory tracing and are using the same style as the
440 * family otherwise present in memdebug.c. I put these ones here since they
441 * require a bunch of structs I didn't wanna include in memdebug.c
445 curl_dofreeaddrinfo(struct addrinfo *freethis,
446 int line, const char *source)
448 (freeaddrinfo)(freethis);
449 curl_memlog("ADDR %s:%d freeaddrinfo(%p)\n",
450 source, line, (void *)freethis);
452 #endif /* defined(CURLDEBUG) && defined(HAVE_FREEADDRINFO) */
455 #if defined(CURLDEBUG) && defined(HAVE_GETADDRINFO)
457 * curl_dogetaddrinfo()
459 * This is strictly for memory tracing and are using the same style as the
460 * family otherwise present in memdebug.c. I put these ones here since they
461 * require a bunch of structs I didn't wanna include in memdebug.c
465 curl_dogetaddrinfo(const char *hostname,
467 const struct addrinfo *hints,
468 struct addrinfo **result,
469 int line, const char *source)
471 int res=(getaddrinfo)(hostname, service, hints, result);
474 curl_memlog("ADDR %s:%d getaddrinfo() = %p\n",
475 source, line, (void *)*result);
477 curl_memlog("ADDR %s:%d getaddrinfo() failed\n",
481 #endif /* defined(CURLDEBUG) && defined(HAVE_GETADDRINFO) */