1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2008, 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 * $Id: hostip4.c,v 1.47 2008-11-06 17:19:57 yangtse Exp $
22 ***************************************************************************/
23
24 #include "setup.h"
25
26 #include <string.h>
27 #include <errno.h>
28
29 #ifdef NEED_MALLOC_H
30 #include <malloc.h>
31 #endif
32 #ifdef HAVE_SYS_SOCKET_H
33 #include <sys/socket.h>
34 #endif
35 #ifdef HAVE_NETINET_IN_H
36 #include <netinet/in.h>
37 #endif
38 #ifdef HAVE_NETDB_H
39 #include <netdb.h>
40 #endif
41 #ifdef HAVE_ARPA_INET_H
42 #include <arpa/inet.h>
43 #endif
44 #ifdef HAVE_STDLIB_H
45 #include <stdlib.h> /* required for free() prototypes */
46 #endif
47 #ifdef HAVE_UNISTD_H
48 #include <unistd.h> /* for the close() proto */
49 #endif
50 #ifdef VMS
51 #include <in.h>
52 #include <inet.h>
53 #include <stdlib.h>
54 #endif
55
56 #ifdef HAVE_PROCESS_H
57 #include <process.h>
58 #endif
59
60 #include "urldata.h"
61 #include "sendf.h"
62 #include "hostip.h"
63 #include "hash.h"
64 #include "share.h"
65 #include "strerror.h"
66 #include "url.h"
67 #include "inet_pton.h"
68
69 #define _MPRINTF_REPLACE /* use our functions only */
70 #include <curl/mprintf.h>
71
72 #include "memory.h"
73 /* The last #include file should be: */
74 #include "memdebug.h"
75
76 /***********************************************************************
77 * Only for plain-ipv4 builds
78 **********************************************************************/
79 #ifdef CURLRES_IPV4 /* plain ipv4 code coming up */
80 /*
81 * Curl_ipvalid() checks what CURL_IPRESOLVE_* requirements that might've
82 * been set and returns TRUE if they are OK.
83 */
Curl_ipvalid(struct SessionHandle * data)84 bool Curl_ipvalid(struct SessionHandle *data)
85 {
86 if(data->set.ip_version == CURL_IPRESOLVE_V6)
87 /* an ipv6 address was requested and we can't get/use one */
88 return FALSE;
89
90 return TRUE; /* OK, proceed */
91 }
92
93 #ifdef CURLRES_SYNCH /* the functions below are for synchronous resolves */
94
95 /*
96 * Curl_getaddrinfo() - the ipv4 synchronous version.
97 *
98 * The original code to this function was from the Dancer source code, written
99 * by Bjorn Reese, it has since been patched and modified considerably.
100 *
101 * gethostbyname_r() is the thread-safe version of the gethostbyname()
102 * function. When we build for plain IPv4, we attempt to use this
103 * function. There are _three_ different gethostbyname_r() versions, and we
104 * detect which one this platform supports in the configure script and set up
105 * the HAVE_GETHOSTBYNAME_R_3, HAVE_GETHOSTBYNAME_R_5 or
106 * HAVE_GETHOSTBYNAME_R_6 defines accordingly. Note that HAVE_GETADDRBYNAME
107 * has the corresponding rules. This is primarily on *nix. Note that some unix
108 * flavours have thread-safe versions of the plain gethostbyname() etc.
109 *
110 */
Curl_getaddrinfo(struct connectdata * conn,const char * hostname,int port,int * waitp)111 Curl_addrinfo *Curl_getaddrinfo(struct connectdata *conn,
112 const char *hostname,
113 int port,
114 int *waitp)
115 {
116 #if defined(HAVE_GETHOSTBYNAME_R_3)
117 int res;
118 #endif
119 Curl_addrinfo *ai = NULL;
120 struct hostent *h = NULL;
121 struct in_addr in;
122 struct hostent *buf = NULL;
123
124 #ifdef CURL_DISABLE_VERBOSE_STRINGS
125 (void)conn;
126 #endif
127
128 *waitp = 0; /* don't wait, we act synchronously */
129
130 if(Curl_inet_pton(AF_INET, hostname, &in) > 0)
131 /* This is a dotted IP address 123.123.123.123-style */
132 return Curl_ip2addr(AF_INET, &in, hostname, port);
133
134 #if defined(HAVE_GETHOSTBYNAME_R)
135 /*
136 * gethostbyname_r() is the preferred resolve function for many platforms.
137 * Since there are three different versions of it, the following code is
138 * somewhat #ifdef-ridden.
139 */
140 else {
141 int h_errnop;
142
143 buf = calloc(CURL_HOSTENT_SIZE, 1);
144 if(!buf)
145 return NULL; /* major failure */
146 /*
147 * The clearing of the buffer is a workaround for a gethostbyname_r bug in
148 * qnx nto and it is also _required_ for some of these functions on some
149 * platforms.
150 */
151
152 #ifdef HAVE_GETHOSTBYNAME_R_5
153 /* Solaris, IRIX and more */
154 h = gethostbyname_r(hostname,
155 (struct hostent *)buf,
156 (char *)buf + sizeof(struct hostent),
157 CURL_HOSTENT_SIZE - sizeof(struct hostent),
158 &h_errnop);
159
160 /* If the buffer is too small, it returns NULL and sets errno to
161 * ERANGE. The errno is thread safe if this is compiled with
162 * -D_REENTRANT as then the 'errno' variable is a macro defined to get
163 * used properly for threads.
164 */
165
166 if(h) {
167 ;
168 }
169 else
170 #endif /* HAVE_GETHOSTBYNAME_R_5 */
171 #ifdef HAVE_GETHOSTBYNAME_R_6
172 /* Linux */
173
174 (void)gethostbyname_r(hostname,
175 (struct hostent *)buf,
176 (char *)buf + sizeof(struct hostent),
177 CURL_HOSTENT_SIZE - sizeof(struct hostent),
178 &h, /* DIFFERENCE */
179 &h_errnop);
180 /* Redhat 8, using glibc 2.2.93 changed the behavior. Now all of a
181 * sudden this function returns EAGAIN if the given buffer size is too
182 * small. Previous versions are known to return ERANGE for the same
183 * problem.
184 *
185 * This wouldn't be such a big problem if older versions wouldn't
186 * sometimes return EAGAIN on a common failure case. Alas, we can't
187 * assume that EAGAIN *or* ERANGE means ERANGE for any given version of
188 * glibc.
189 *
190 * For now, we do that and thus we may call the function repeatedly and
191 * fail for older glibc versions that return EAGAIN, until we run out of
192 * buffer size (step_size grows beyond CURL_HOSTENT_SIZE).
193 *
194 * If anyone has a better fix, please tell us!
195 *
196 * -------------------------------------------------------------------
197 *
198 * On October 23rd 2003, Dan C dug up more details on the mysteries of
199 * gethostbyname_r() in glibc:
200 *
201 * In glibc 2.2.5 the interface is different (this has also been
202 * discovered in glibc 2.1.1-6 as shipped by Redhat 6). What I can't
203 * explain, is that tests performed on glibc 2.2.4-34 and 2.2.4-32
204 * (shipped/upgraded by Redhat 7.2) don't show this behavior!
205 *
206 * In this "buggy" version, the return code is -1 on error and 'errno'
207 * is set to the ERANGE or EAGAIN code. Note that 'errno' is not a
208 * thread-safe variable.
209 */
210
211 if(!h) /* failure */
212 #endif/* HAVE_GETHOSTBYNAME_R_6 */
213 #ifdef HAVE_GETHOSTBYNAME_R_3
214 /* AIX, Digital Unix/Tru64, HPUX 10, more? */
215
216 /* For AIX 4.3 or later, we don't use gethostbyname_r() at all, because of
217 * the plain fact that it does not return unique full buffers on each
218 * call, but instead several of the pointers in the hostent structs will
219 * point to the same actual data! This have the unfortunate down-side that
220 * our caching system breaks down horribly. Luckily for us though, AIX 4.3
221 * and more recent versions have a "completely thread-safe"[*] libc where
222 * all the data is stored in thread-specific memory areas making calls to
223 * the plain old gethostbyname() work fine even for multi-threaded
224 * programs.
225 *
226 * This AIX 4.3 or later detection is all made in the configure script.
227 *
228 * Troels Walsted Hansen helped us work this out on March 3rd, 2003.
229 *
230 * [*] = much later we've found out that it isn't at all "completely
231 * thread-safe", but at least the gethostbyname() function is.
232 */
233
234 if(CURL_HOSTENT_SIZE >=
235 (sizeof(struct hostent)+sizeof(struct hostent_data))) {
236
237 /* August 22nd, 2000: Albert Chin-A-Young brought an updated version
238 * that should work! September 20: Richard Prescott worked on the buffer
239 * size dilemma.
240 */
241
242 res = gethostbyname_r(hostname,
243 (struct hostent *)buf,
244 (struct hostent_data *)((char *)buf +
245 sizeof(struct hostent)));
246 h_errnop = SOCKERRNO; /* we don't deal with this, but set it anyway */
247 }
248 else
249 res = -1; /* failure, too smallish buffer size */
250
251 if(!res) { /* success */
252
253 h = buf; /* result expected in h */
254
255 /* This is the worst kind of the different gethostbyname_r() interfaces.
256 * Since we don't know how big buffer this particular lookup required,
257 * we can't realloc down the huge alloc without doing closer analysis of
258 * the returned data. Thus, we always use CURL_HOSTENT_SIZE for every
259 * name lookup. Fixing this would require an extra malloc() and then
260 * calling Curl_addrinfo_copy() that subsequent realloc()s down the new
261 * memory area to the actually used amount.
262 */
263 }
264 else
265 #endif /* HAVE_GETHOSTBYNAME_R_3 */
266 {
267 infof(conn->data, "gethostbyname_r(2) failed for %s\n", hostname);
268 h = NULL; /* set return code to NULL */
269 free(buf);
270 }
271 #else /* HAVE_GETHOSTBYNAME_R */
272 /*
273 * Here is code for platforms that don't have gethostbyname_r() or for
274 * which the gethostbyname() is the preferred() function.
275 */
276 else {
277 #if (defined(NETWARE) && !defined(__NOVELL_LIBC__))
278 h = gethostbyname((char*)hostname);
279 #else
280 h = gethostbyname(hostname);
281 #endif
282 if(!h)
283 infof(conn->data, "gethostbyname(2) failed for %s\n", hostname);
284 #endif /*HAVE_GETHOSTBYNAME_R */
285 }
286
287 if(h) {
288 ai = Curl_he2ai(h, port);
289
290 if(buf) /* used a *_r() function */
291 free(buf);
292 }
293
294 return ai;
295 }
296
297 #endif /* CURLRES_SYNCH */
298 #endif /* CURLRES_IPV4 */
299
300