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