ENH: do not error when sunpro or mipspro fortran used
[cmake.git] / Utilities / cmcurl / hostip4.c
blob090867a294696197c3383d6d9bd8f4f0a317f37a
1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2006, 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 * $Id: hostip4.c,v 1.2 2007-03-15 19:22:13 andy Exp $
22 ***************************************************************************/
24 #include "setup.h"
26 #include <string.h>
27 #include <errno.h>
29 #ifdef NEED_MALLOC_H
30 #include <malloc.h>
31 #endif
32 #ifdef HAVE_SYS_TYPES_H
33 #include <sys/types.h>
34 #endif
35 #ifdef HAVE_SYS_SOCKET_H
36 #include <sys/socket.h>
37 #endif
38 #ifdef HAVE_NETINET_IN_H
39 #include <netinet/in.h>
40 #endif
41 #ifdef HAVE_NETDB_H
42 #include <netdb.h>
43 #endif
44 #ifdef HAVE_ARPA_INET_H
45 #include <arpa/inet.h>
46 #endif
47 #ifdef HAVE_STDLIB_H
48 #include <stdlib.h> /* required for free() prototypes */
49 #endif
50 #ifdef HAVE_UNISTD_H
51 #include <unistd.h> /* for the close() proto */
52 #endif
53 #ifdef VMS
54 #include <in.h>
55 #include <inet.h>
56 #include <stdlib.h>
57 #endif
59 #ifdef HAVE_SETJMP_H
60 #include <setjmp.h>
61 #endif
63 #ifdef HAVE_PROCESS_H
64 #include <process.h>
65 #endif
67 #include "urldata.h"
68 #include "sendf.h"
69 #include "hostip.h"
70 #include "hash.h"
71 #include "share.h"
72 #include "strerror.h"
73 #include "url.h"
74 #include "inet_pton.h"
76 #define _MPRINTF_REPLACE /* use our functions only */
77 #include <curl/mprintf.h>
79 #if defined(HAVE_INET_NTOA_R) && !defined(HAVE_INET_NTOA_R_DECL)
80 #include "inet_ntoa_r.h"
81 #endif
83 #include "memory.h"
84 /* The last #include file should be: */
85 #include "memdebug.h"
87 /***********************************************************************
88 * Only for plain-ipv4 builds
89 **********************************************************************/
90 #ifdef CURLRES_IPV4 /* plain ipv4 code coming up */
92 * Curl_ipvalid() checks what CURL_IPRESOLVE_* requirements that might've
93 * been set and returns TRUE if they are OK.
95 bool Curl_ipvalid(struct SessionHandle *data)
97 if(data->set.ip_version == CURL_IPRESOLVE_V6)
98 /* an ipv6 address was requested and we can't get/use one */
99 return FALSE;
101 return TRUE; /* OK, proceed */
104 #ifdef CURLRES_SYNCH /* the functions below are for synchronous resolves */
107 * Curl_getaddrinfo() - the ipv4 synchronous version.
109 * The original code to this function was from the Dancer source code, written
110 * by Bjorn Reese, it has since been patched and modified considerably.
112 * gethostbyname_r() is the thread-safe version of the gethostbyname()
113 * function. When we build for plain IPv4, we attempt to use this
114 * function. There are _three_ different gethostbyname_r() versions, and we
115 * detect which one this platform supports in the configure script and set up
116 * the HAVE_GETHOSTBYNAME_R_3, HAVE_GETHOSTBYNAME_R_5 or
117 * HAVE_GETHOSTBYNAME_R_6 defines accordingly. Note that HAVE_GETADDRBYNAME
118 * has the corresponding rules. This is primarily on *nix. Note that some unix
119 * flavours have thread-safe versions of the plain gethostbyname() etc.
122 Curl_addrinfo *Curl_getaddrinfo(struct connectdata *conn,
123 const char *hostname,
124 int port,
125 int *waitp)
127 Curl_addrinfo *ai = NULL;
128 struct hostent *h = NULL;
129 in_addr_t in;
130 struct SessionHandle *data = conn->data;
131 struct hostent *buf = NULL;
133 (void)port; /* unused in IPv4 code */
135 *waitp = 0; /* don't wait, we act synchronously */
137 if(1 == Curl_inet_pton(AF_INET, hostname, &in))
138 /* This is a dotted IP address 123.123.123.123-style */
139 return Curl_ip2addr(in, hostname, port);
141 #if defined(HAVE_GETHOSTBYNAME_R)
143 * gethostbyname_r() is the preferred resolve function for many platforms.
144 * Since there are three different versions of it, the following code is
145 * somewhat #ifdef-ridden.
147 else {
148 int h_errnop;
149 int res=ERANGE;
151 buf = (struct hostent *)calloc(CURL_HOSTENT_SIZE, 1);
152 if(!buf)
153 return NULL; /* major failure */
155 * The clearing of the buffer is a workaround for a gethostbyname_r bug in
156 * qnx nto and it is also _required_ for some of these functions on some
157 * platforms.
160 #ifdef HAVE_GETHOSTBYNAME_R_5
161 /* Solaris, IRIX and more */
162 (void)res; /* prevent compiler warning */
163 h = gethostbyname_r(hostname,
164 (struct hostent *)buf,
165 (char *)buf + sizeof(struct hostent),
166 CURL_HOSTENT_SIZE - sizeof(struct hostent),
167 &h_errnop);
169 /* If the buffer is too small, it returns NULL and sets errno to
170 * ERANGE. The errno is thread safe if this is compiled with
171 * -D_REENTRANT as then the 'errno' variable is a macro defined to get
172 * used properly for threads.
175 if(h) {
178 else
179 #endif /* HAVE_GETHOSTBYNAME_R_5 */
180 #ifdef HAVE_GETHOSTBYNAME_R_6
181 /* Linux */
183 res=gethostbyname_r(hostname,
184 (struct hostent *)buf,
185 (char *)buf + sizeof(struct hostent),
186 CURL_HOSTENT_SIZE - sizeof(struct hostent),
187 &h, /* DIFFERENCE */
188 &h_errnop);
189 /* Redhat 8, using glibc 2.2.93 changed the behavior. Now all of a
190 * sudden this function returns EAGAIN if the given buffer size is too
191 * small. Previous versions are known to return ERANGE for the same
192 * problem.
194 * This wouldn't be such a big problem if older versions wouldn't
195 * sometimes return EAGAIN on a common failure case. Alas, we can't
196 * assume that EAGAIN *or* ERANGE means ERANGE for any given version of
197 * glibc.
199 * For now, we do that and thus we may call the function repeatedly and
200 * fail for older glibc versions that return EAGAIN, until we run out of
201 * buffer size (step_size grows beyond CURL_HOSTENT_SIZE).
203 * If anyone has a better fix, please tell us!
205 * -------------------------------------------------------------------
207 * On October 23rd 2003, Dan C dug up more details on the mysteries of
208 * gethostbyname_r() in glibc:
210 * In glibc 2.2.5 the interface is different (this has also been
211 * discovered in glibc 2.1.1-6 as shipped by Redhat 6). What I can't
212 * explain, is that tests performed on glibc 2.2.4-34 and 2.2.4-32
213 * (shipped/upgraded by Redhat 7.2) don't show this behavior!
215 * In this "buggy" version, the return code is -1 on error and 'errno'
216 * is set to the ERANGE or EAGAIN code. Note that 'errno' is not a
217 * thread-safe variable.
220 if(!h) /* failure */
221 #endif/* HAVE_GETHOSTBYNAME_R_6 */
222 #ifdef HAVE_GETHOSTBYNAME_R_3
223 /* AIX, Digital Unix/Tru64, HPUX 10, more? */
225 /* For AIX 4.3 or later, we don't use gethostbyname_r() at all, because of
226 * the plain fact that it does not return unique full buffers on each
227 * call, but instead several of the pointers in the hostent structs will
228 * point to the same actual data! This have the unfortunate down-side that
229 * our caching system breaks down horribly. Luckily for us though, AIX 4.3
230 * and more recent versions have a "completely thread-safe"[*] libc where
231 * all the data is stored in thread-specific memory areas making calls to
232 * the plain old gethostbyname() work fine even for multi-threaded
233 * programs.
235 * This AIX 4.3 or later detection is all made in the configure script.
237 * Troels Walsted Hansen helped us work this out on March 3rd, 2003.
239 * [*] = much later we've found out that it isn't at all "completely
240 * thread-safe", but at least the gethostbyname() function is.
243 if(CURL_HOSTENT_SIZE >=
244 (sizeof(struct hostent)+sizeof(struct hostent_data))) {
246 /* August 22nd, 2000: Albert Chin-A-Young brought an updated version
247 * that should work! September 20: Richard Prescott worked on the buffer
248 * size dilemma.
251 res = gethostbyname_r(hostname,
252 (struct hostent *)buf,
253 (struct hostent_data *)((char *)buf +
254 sizeof(struct hostent)));
255 h_errnop= errno; /* we don't deal with this, but set it anyway */
257 else
258 res = -1; /* failure, too smallish buffer size */
260 if(!res) { /* success */
262 h = buf; /* result expected in h */
264 /* This is the worst kind of the different gethostbyname_r() interfaces.
265 * Since we don't know how big buffer this particular lookup required,
266 * we can't realloc down the huge alloc without doing closer analysis of
267 * the returned data. Thus, we always use CURL_HOSTENT_SIZE for every
268 * name lookup. Fixing this would require an extra malloc() and then
269 * calling Curl_addrinfo_copy() that subsequent realloc()s down the new
270 * memory area to the actually used amount.
273 else
274 #endif /* HAVE_GETHOSTBYNAME_R_3 */
276 infof(data, "gethostbyname_r(2) failed for %s\n", hostname);
277 h = NULL; /* set return code to NULL */
278 free(buf);
280 #else /* HAVE_GETHOSTBYNAME_R */
282 * Here is code for platforms that don't have gethostbyname_r() or for
283 * which the gethostbyname() is the preferred() function.
285 else {
286 h = gethostbyname(hostname);
287 if (!h)
288 infof(data, "gethostbyname(2) failed for %s\n", hostname);
289 #endif /*HAVE_GETHOSTBYNAME_R */
292 if(h) {
293 ai = Curl_he2ai(h, port);
295 if (buf) /* used a *_r() function */
296 free(buf);
299 return ai;
302 #endif /* CURLRES_SYNCH */
303 #endif /* CURLRES_IPV4 */
306 * Curl_he2ai() translates from a hostent struct to a Curl_addrinfo struct.
307 * The Curl_addrinfo is meant to work like the addrinfo struct does for IPv6
308 * stacks, but for all hosts and environments.
310 * Curl_addrinfo defined in "lib/hostip.h"
312 * struct Curl_addrinfo {
313 * int ai_flags;
314 * int ai_family;
315 * int ai_socktype;
316 * int ai_protocol;
317 * socklen_t ai_addrlen; * Follow rfc3493 struct addrinfo *
318 * char *ai_canonname;
319 * struct sockaddr *ai_addr;
320 * struct Curl_addrinfo *ai_next;
321 * };
323 * hostent defined in <netdb.h>
325 * struct hostent {
326 * char *h_name;
327 * char **h_aliases;
328 * int h_addrtype;
329 * int h_length;
330 * char **h_addr_list;
331 * };
333 * for backward compatibility:
335 * #define h_addr h_addr_list[0]
338 Curl_addrinfo *Curl_he2ai(const struct hostent *he, int port)
340 Curl_addrinfo *ai;
341 Curl_addrinfo *prevai = NULL;
342 Curl_addrinfo *firstai = NULL;
343 struct sockaddr_in *addr;
344 int i;
345 struct in_addr *curr;
347 if(!he)
348 /* no input == no output! */
349 return NULL;
351 for(i=0; (curr = (struct in_addr *)he->h_addr_list[i]) != NULL; i++) {
353 ai = calloc(1, sizeof(Curl_addrinfo) + sizeof(struct sockaddr_in));
355 if(!ai)
356 break;
358 if(!firstai)
359 /* store the pointer we want to return from this function */
360 firstai = ai;
362 if(prevai)
363 /* make the previous entry point to this */
364 prevai->ai_next = ai;
366 ai->ai_family = AF_INET; /* we only support this */
368 /* we return all names as STREAM, so when using this address for TFTP
369 the type must be ignored and conn->socktype be used instead! */
370 ai->ai_socktype = SOCK_STREAM;
372 ai->ai_addrlen = sizeof(struct sockaddr_in);
373 /* make the ai_addr point to the address immediately following this struct
374 and use that area to store the address */
375 ai->ai_addr = (struct sockaddr *) ((char*)ai + sizeof(Curl_addrinfo));
377 /* leave the rest of the struct filled with zero */
379 addr = (struct sockaddr_in *)ai->ai_addr; /* storage area for this info */
381 memcpy((char *)&(addr->sin_addr), curr, sizeof(struct in_addr));
382 addr->sin_family = he->h_addrtype;
383 addr->sin_port = htons((unsigned short)port);
385 prevai = ai;
387 return firstai;