1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2008, 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.
22 ***************************************************************************/
26 #include <curl/curl.h>
31 #ifdef HAVE_SYS_SOCKET_H
32 # include <sys/socket.h>
34 #ifdef HAVE_NETINET_IN_H
35 # include <netinet/in.h>
40 #ifdef HAVE_ARPA_INET_H
41 # include <arpa/inet.h>
50 #if defined(NETWARE) && defined(__NOVELL_LIBC__)
52 # define in_addr_t unsigned long
55 #include "curl_addrinfo.h"
57 #define _MPRINTF_REPLACE /* use our functions only */
58 #include <curl/mprintf.h>
61 /* The last #include file should be: */
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.
75 Curl_freeaddrinfo(Curl_addrinfo *cahead)
77 Curl_addrinfo *ca, *canext;
79 for(ca = cahead; ca != NULL; ca = canext) {
85 free(ca->ai_canonname);
94 #ifdef HAVE_GETADDRINFO
96 * Curl_getaddrinfo_ex()
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().
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.
110 Curl_getaddrinfo_ex(const char *nodename,
111 const char *servname,
112 const struct addrinfo *hints,
113 Curl_addrinfo **result)
115 const struct addrinfo *ainext;
116 const struct addrinfo *ai;
117 struct addrinfo *aihead;
118 Curl_addrinfo *cafirst = NULL;
119 Curl_addrinfo *calast = NULL;
123 *result = NULL; /* assume failure */
125 error = getaddrinfo(nodename, servname, hints, &aihead);
129 for(ai = aihead; ai != NULL; ai = ainext) {
131 if((ca = malloc(sizeof(Curl_addrinfo))) == NULL) {
136 /* copy each structure member individually, member ordering, */
137 /* size, or padding might be different for each structure. */
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;
145 ca->ai_canonname = NULL;
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) {
155 memcpy(ca->ai_addr, ai->ai_addr, ca->ai_addrlen);
158 if(ai->ai_canonname != NULL) {
159 if((ca->ai_canonname = strdup(ai->ai_canonname)) == NULL) {
168 /* if the return list is empty, this becomes the first element */
172 /* add this element last in the return list */
174 calast->ai_next = ca;
177 /* fetch next element fom the addrinfo list */
178 ainext = ai->ai_next;
181 /* destroy the addrinfo list */
183 freeaddrinfo(aihead);
185 /* if we failed, also destroy the Curl_addrinfo list */
187 Curl_freeaddrinfo(cafirst);
193 return error; /* This is not a CURLcode */
195 #endif /* HAVE_GETADDRINFO */
201 * This function returns a pointer to the first element of a newly allocated
202 * Curl_addrinfo struct linked list filled with the data of a given hostent.
203 * Curl_addrinfo is meant to work like the addrinfo struct does for a IPv6
204 * stack, but usable also for IPv4, all hosts and environments.
206 * The memory allocated by this function *MUST* be free'd later on calling
207 * Curl_freeaddrinfo(). For each successful call to this function there
208 * must be an associated call later to Curl_freeaddrinfo().
210 * Curl_addrinfo defined in "lib/curl_addrinfo.h"
212 * struct Curl_addrinfo {
217 * socklen_t ai_addrlen; * Follow rfc3493 struct addrinfo *
218 * char *ai_canonname;
219 * struct sockaddr *ai_addr;
220 * struct Curl_addrinfo *ai_next;
222 * typedef struct Curl_addrinfo Curl_addrinfo;
224 * hostent defined in <netdb.h>
231 * char **h_addr_list;
234 * for backward compatibility:
236 * #define h_addr h_addr_list[0]
240 Curl_he2ai(const struct hostent *he, int port)
243 Curl_addrinfo *prevai = NULL;
244 Curl_addrinfo *firstai = NULL;
245 struct sockaddr_in *addr;
247 struct sockaddr_in6 *addr6;
249 CURLcode result = CURLE_OK;
254 /* no input == no output! */
257 for(i=0; (curr = he->h_addr_list[i]) != NULL; i++) {
261 if (he->h_addrtype == AF_INET6)
262 ss_size = sizeof (struct sockaddr_in6);
265 ss_size = sizeof (struct sockaddr_in);
267 if((ai = calloc(1, sizeof(Curl_addrinfo))) == NULL) {
268 result = CURLE_OUT_OF_MEMORY;
271 if((ai->ai_canonname = strdup(he->h_name)) == NULL) {
272 result = CURLE_OUT_OF_MEMORY;
276 if((ai->ai_addr = calloc(1, ss_size)) == NULL) {
277 result = CURLE_OUT_OF_MEMORY;
278 free(ai->ai_canonname);
284 /* store the pointer we want to return from this function */
288 /* make the previous entry point to this */
289 prevai->ai_next = ai;
291 ai->ai_family = he->h_addrtype;
293 /* we return all names as STREAM, so when using this address for TFTP
294 the type must be ignored and conn->socktype be used instead! */
295 ai->ai_socktype = SOCK_STREAM;
297 ai->ai_addrlen = ss_size;
299 /* leave the rest of the struct filled with zero */
301 switch (ai->ai_family) {
303 addr = (struct sockaddr_in *)ai->ai_addr; /* storage area for this info */
305 memcpy(&addr->sin_addr, curr, sizeof(struct in_addr));
306 addr->sin_family = (unsigned short)(he->h_addrtype);
307 addr->sin_port = htons((unsigned short)port);
312 addr6 = (struct sockaddr_in6 *)ai->ai_addr; /* storage area for this info */
314 memcpy(&addr6->sin6_addr, curr, sizeof(struct in6_addr));
315 addr6->sin6_family = (unsigned short)(he->h_addrtype);
316 addr6->sin6_port = htons((unsigned short)port);
324 if(result != CURLE_OK) {
325 Curl_freeaddrinfo(firstai);
333 #if defined(CURLDEBUG) && defined(HAVE_FREEADDRINFO)
335 * curl_dofreeaddrinfo()
337 * This is strictly for memory tracing and are using the same style as the
338 * family otherwise present in memdebug.c. I put these ones here since they
339 * require a bunch of structs I didn't wanna include in memdebug.c
343 curl_dofreeaddrinfo(struct addrinfo *freethis,
344 int line, const char *source)
346 (freeaddrinfo)(freethis);
348 fprintf(logfile, "ADDR %s:%d freeaddrinfo(%p)\n",
349 source, line, (void *)freethis);
351 #endif /* defined(CURLDEBUG) && defined(HAVE_FREEADDRINFO) */
354 #if defined(CURLDEBUG) && defined(HAVE_GETADDRINFO)
356 * curl_dogetaddrinfo()
358 * This is strictly for memory tracing and are using the same style as the
359 * family otherwise present in memdebug.c. I put these ones here since they
360 * require a bunch of structs I didn't wanna include in memdebug.c
364 curl_dogetaddrinfo(const char *hostname,
366 const struct addrinfo *hints,
367 struct addrinfo **result,
368 int line, const char *source)
370 int res=(getaddrinfo)(hostname, service, hints, result);
374 fprintf(logfile, "ADDR %s:%d getaddrinfo() = %p\n",
375 source, line, (void *)*result);
379 fprintf(logfile, "ADDR %s:%d getaddrinfo() failed\n",
384 #endif /* defined(CURLDEBUG) && defined(HAVE_GETADDRINFO) */