1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2013, 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 ***************************************************************************/
23 #include "curl_setup.h"
25 #ifdef HAVE_NETINET_IN_H
26 #include <netinet/in.h>
31 #ifdef HAVE_ARPA_INET_H
32 #include <arpa/inet.h>
50 #include "inet_pton.h"
52 #define _MPRINTF_REPLACE /* use our functions only */
53 #include <curl/mprintf.h>
55 #include "curl_memory.h"
56 /* The last #include file should be: */
59 /***********************************************************************
60 * Only for plain-ipv4 builds
61 **********************************************************************/
62 #ifdef CURLRES_IPV4 /* plain ipv4 code coming up */
64 * Curl_ipvalid() checks what CURL_IPRESOLVE_* requirements that might've
65 * been set and returns TRUE if they are OK.
67 bool Curl_ipvalid(struct connectdata *conn)
69 if(conn->ip_version == CURL_IPRESOLVE_V6)
70 /* an ipv6 address was requested and we can't get/use one */
73 return TRUE; /* OK, proceed */
79 * Curl_getaddrinfo() - the ipv4 synchronous version.
81 * The original code to this function was from the Dancer source code, written
82 * by Bjorn Reese, it has since been patched and modified considerably.
84 * gethostbyname_r() is the thread-safe version of the gethostbyname()
85 * function. When we build for plain IPv4, we attempt to use this
86 * function. There are _three_ different gethostbyname_r() versions, and we
87 * detect which one this platform supports in the configure script and set up
88 * the HAVE_GETHOSTBYNAME_R_3, HAVE_GETHOSTBYNAME_R_5 or
89 * HAVE_GETHOSTBYNAME_R_6 defines accordingly. Note that HAVE_GETADDRBYNAME
90 * has the corresponding rules. This is primarily on *nix. Note that some unix
91 * flavours have thread-safe versions of the plain gethostbyname() etc.
94 Curl_addrinfo *Curl_getaddrinfo(struct connectdata *conn,
99 Curl_addrinfo *ai = NULL;
101 #ifdef CURL_DISABLE_VERBOSE_STRINGS
105 *waitp = 0; /* synchronous response only */
107 ai = Curl_ipv4_resolve_r(hostname, port);
109 infof(conn->data, "Curl_ipv4_resolve_r failed for %s\n", hostname);
113 #endif /* CURLRES_SYNCH */
114 #endif /* CURLRES_IPV4 */
116 #if defined(CURLRES_IPV4) && !defined(CURLRES_ARES)
119 * Curl_ipv4_resolve_r() - ipv4 threadsafe resolver function.
121 * This is used for both synchronous and asynchronous resolver builds,
122 * implying that only threadsafe code and function calls may be used.
125 Curl_addrinfo *Curl_ipv4_resolve_r(const char *hostname,
128 #if !defined(HAVE_GETADDRINFO_THREADSAFE) && defined(HAVE_GETHOSTBYNAME_R_3)
131 Curl_addrinfo *ai = NULL;
132 struct hostent *h = NULL;
134 struct hostent *buf = NULL;
136 if(Curl_inet_pton(AF_INET, hostname, &in) > 0)
137 /* This is a dotted IP address 123.123.123.123-style */
138 return Curl_ip2addr(AF_INET, &in, hostname, port);
140 #if defined(HAVE_GETADDRINFO_THREADSAFE)
142 struct addrinfo hints;
144 char *sbufptr = NULL;
146 memset(&hints, 0, sizeof(hints));
147 hints.ai_family = PF_INET;
148 hints.ai_socktype = SOCK_STREAM;
150 snprintf(sbuf, sizeof(sbuf), "%d", port);
154 (void)Curl_getaddrinfo_ex(hostname, sbufptr, &hints, &ai);
156 #elif defined(HAVE_GETHOSTBYNAME_R)
158 * gethostbyname_r() is the preferred resolve function for many platforms.
159 * Since there are three different versions of it, the following code is
160 * somewhat #ifdef-ridden.
165 buf = calloc(1, CURL_HOSTENT_SIZE);
167 return NULL; /* major failure */
169 * The clearing of the buffer is a workaround for a gethostbyname_r bug in
170 * qnx nto and it is also _required_ for some of these functions on some
174 #if defined(HAVE_GETHOSTBYNAME_R_5)
175 /* Solaris, IRIX and more */
176 h = gethostbyname_r(hostname,
177 (struct hostent *)buf,
178 (char *)buf + sizeof(struct hostent),
179 CURL_HOSTENT_SIZE - sizeof(struct hostent),
182 /* If the buffer is too small, it returns NULL and sets errno to
183 * ERANGE. The errno is thread safe if this is compiled with
184 * -D_REENTRANT as then the 'errno' variable is a macro defined to get
185 * used properly for threads.
192 #elif defined(HAVE_GETHOSTBYNAME_R_6)
195 (void)gethostbyname_r(hostname,
196 (struct hostent *)buf,
197 (char *)buf + sizeof(struct hostent),
198 CURL_HOSTENT_SIZE - sizeof(struct hostent),
201 /* Redhat 8, using glibc 2.2.93 changed the behavior. Now all of a
202 * sudden this function returns EAGAIN if the given buffer size is too
203 * small. Previous versions are known to return ERANGE for the same
206 * This wouldn't be such a big problem if older versions wouldn't
207 * sometimes return EAGAIN on a common failure case. Alas, we can't
208 * assume that EAGAIN *or* ERANGE means ERANGE for any given version of
211 * For now, we do that and thus we may call the function repeatedly and
212 * fail for older glibc versions that return EAGAIN, until we run out of
213 * buffer size (step_size grows beyond CURL_HOSTENT_SIZE).
215 * If anyone has a better fix, please tell us!
217 * -------------------------------------------------------------------
219 * On October 23rd 2003, Dan C dug up more details on the mysteries of
220 * gethostbyname_r() in glibc:
222 * In glibc 2.2.5 the interface is different (this has also been
223 * discovered in glibc 2.1.1-6 as shipped by Redhat 6). What I can't
224 * explain, is that tests performed on glibc 2.2.4-34 and 2.2.4-32
225 * (shipped/upgraded by Redhat 7.2) don't show this behavior!
227 * In this "buggy" version, the return code is -1 on error and 'errno'
228 * is set to the ERANGE or EAGAIN code. Note that 'errno' is not a
229 * thread-safe variable.
233 #elif defined(HAVE_GETHOSTBYNAME_R_3)
234 /* AIX, Digital Unix/Tru64, HPUX 10, more? */
236 /* For AIX 4.3 or later, we don't use gethostbyname_r() at all, because of
237 * the plain fact that it does not return unique full buffers on each
238 * call, but instead several of the pointers in the hostent structs will
239 * point to the same actual data! This have the unfortunate down-side that
240 * our caching system breaks down horribly. Luckily for us though, AIX 4.3
241 * and more recent versions have a "completely thread-safe"[*] libc where
242 * all the data is stored in thread-specific memory areas making calls to
243 * the plain old gethostbyname() work fine even for multi-threaded
246 * This AIX 4.3 or later detection is all made in the configure script.
248 * Troels Walsted Hansen helped us work this out on March 3rd, 2003.
250 * [*] = much later we've found out that it isn't at all "completely
251 * thread-safe", but at least the gethostbyname() function is.
254 if(CURL_HOSTENT_SIZE >=
255 (sizeof(struct hostent)+sizeof(struct hostent_data))) {
257 /* August 22nd, 2000: Albert Chin-A-Young brought an updated version
258 * that should work! September 20: Richard Prescott worked on the buffer
262 res = gethostbyname_r(hostname,
263 (struct hostent *)buf,
264 (struct hostent_data *)((char *)buf +
265 sizeof(struct hostent)));
266 h_errnop = SOCKERRNO; /* we don't deal with this, but set it anyway */
269 res = -1; /* failure, too smallish buffer size */
271 if(!res) { /* success */
273 h = buf; /* result expected in h */
275 /* This is the worst kind of the different gethostbyname_r() interfaces.
276 * Since we don't know how big buffer this particular lookup required,
277 * we can't realloc down the huge alloc without doing closer analysis of
278 * the returned data. Thus, we always use CURL_HOSTENT_SIZE for every
279 * name lookup. Fixing this would require an extra malloc() and then
280 * calling Curl_addrinfo_copy() that subsequent realloc()s down the new
281 * memory area to the actually used amount.
285 #endif /* HAVE_...BYNAME_R_5 || HAVE_...BYNAME_R_6 || HAVE_...BYNAME_R_3 */
287 h = NULL; /* set return code to NULL */
290 #else /* HAVE_GETADDRINFO_THREADSAFE || HAVE_GETHOSTBYNAME_R */
292 * Here is code for platforms that don't have a thread safe
293 * getaddrinfo() nor gethostbyname_r() function or for which
294 * gethostbyname() is the preferred one.
297 h = gethostbyname((void*)hostname);
298 #endif /* HAVE_GETADDRINFO_THREADSAFE || HAVE_GETHOSTBYNAME_R */
302 ai = Curl_he2ai(h, port);
304 if(buf) /* used a *_r() function */
310 #endif /* defined(CURLRES_IPV4) && !defined(CURLRES_ARES) */