Removed inclusion of <sys/types.h> and <sys/stat.h> in .c-files
[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 SessionHandle *data = conn->data;
131   struct hostent *buf = NULL;
132
133   (void)port; /* unused in IPv4 code */
134
135   *waitp = 0; /* don't wait, we act synchronously */
136
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);
140
141 #if defined(HAVE_GETHOSTBYNAME_R)
142   /*
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.
146    */
147   else {
148     int h_errnop;
149
150     buf = (struct hostent *)calloc(CURL_HOSTENT_SIZE, 1);
151     if(!buf)
152       return NULL; /* major failure */
153     /*
154      * The clearing of the buffer is a workaround for a gethostbyname_r bug in
155      * qnx nto and it is also _required_ for some of these functions on some
156      * platforms.
157      */
158
159 #ifdef HAVE_GETHOSTBYNAME_R_5
160     /* Solaris, IRIX and more */
161     h = gethostbyname_r(hostname,
162                         (struct hostent *)buf,
163                         (char *)buf + sizeof(struct hostent),
164                         CURL_HOSTENT_SIZE - sizeof(struct hostent),
165                         &h_errnop);
166
167     /* If the buffer is too small, it returns NULL and sets errno to
168      * ERANGE. The errno is thread safe if this is compiled with
169      * -D_REENTRANT as then the 'errno' variable is a macro defined to get
170      * used properly for threads.
171      */
172
173     if(h) {
174       ;
175     }
176     else
177 #endif /* HAVE_GETHOSTBYNAME_R_5 */
178 #ifdef HAVE_GETHOSTBYNAME_R_6
179     /* Linux */
180
181     (void)gethostbyname_r(hostname,
182                         (struct hostent *)buf,
183                         (char *)buf + sizeof(struct hostent),
184                         CURL_HOSTENT_SIZE - sizeof(struct hostent),
185                         &h, /* DIFFERENCE */
186                         &h_errnop);
187     /* Redhat 8, using glibc 2.2.93 changed the behavior. Now all of a
188      * sudden this function returns EAGAIN if the given buffer size is too
189      * small. Previous versions are known to return ERANGE for the same
190      * problem.
191      *
192      * This wouldn't be such a big problem if older versions wouldn't
193      * sometimes return EAGAIN on a common failure case. Alas, we can't
194      * assume that EAGAIN *or* ERANGE means ERANGE for any given version of
195      * glibc.
196      *
197      * For now, we do that and thus we may call the function repeatedly and
198      * fail for older glibc versions that return EAGAIN, until we run out of
199      * buffer size (step_size grows beyond CURL_HOSTENT_SIZE).
200      *
201      * If anyone has a better fix, please tell us!
202      *
203      * -------------------------------------------------------------------
204      *
205      * On October 23rd 2003, Dan C dug up more details on the mysteries of
206      * gethostbyname_r() in glibc:
207      *
208      * In glibc 2.2.5 the interface is different (this has also been
209      * discovered in glibc 2.1.1-6 as shipped by Redhat 6). What I can't
210      * explain, is that tests performed on glibc 2.2.4-34 and 2.2.4-32
211      * (shipped/upgraded by Redhat 7.2) don't show this behavior!
212      *
213      * In this "buggy" version, the return code is -1 on error and 'errno'
214      * is set to the ERANGE or EAGAIN code. Note that 'errno' is not a
215      * thread-safe variable.
216      */
217
218     if(!h) /* failure */
219 #endif/* HAVE_GETHOSTBYNAME_R_6 */
220 #ifdef HAVE_GETHOSTBYNAME_R_3
221     /* AIX, Digital Unix/Tru64, HPUX 10, more? */
222
223     /* For AIX 4.3 or later, we don't use gethostbyname_r() at all, because of
224      * the plain fact that it does not return unique full buffers on each
225      * call, but instead several of the pointers in the hostent structs will
226      * point to the same actual data! This have the unfortunate down-side that
227      * our caching system breaks down horribly. Luckily for us though, AIX 4.3
228      * and more recent versions have a "completely thread-safe"[*] libc where
229      * all the data is stored in thread-specific memory areas making calls to
230      * the plain old gethostbyname() work fine even for multi-threaded
231      * programs.
232      *
233      * This AIX 4.3 or later detection is all made in the configure script.
234      *
235      * Troels Walsted Hansen helped us work this out on March 3rd, 2003.
236      *
237      * [*] = much later we've found out that it isn't at all "completely
238      * thread-safe", but at least the gethostbyname() function is.
239      */
240
241     if(CURL_HOSTENT_SIZE >=
242        (sizeof(struct hostent)+sizeof(struct hostent_data))) {
243
244       /* August 22nd, 2000: Albert Chin-A-Young brought an updated version
245        * that should work! September 20: Richard Prescott worked on the buffer
246        * size dilemma.
247        */
248
249       res = gethostbyname_r(hostname,
250                             (struct hostent *)buf,
251                             (struct hostent_data *)((char *)buf +
252                                                     sizeof(struct hostent)));
253       h_errnop = SOCKERRNO; /* we don't deal with this, but set it anyway */
254     }
255     else
256       res = -1; /* failure, too smallish buffer size */
257
258     if(!res) { /* success */
259
260       h = buf; /* result expected in h */
261
262       /* This is the worst kind of the different gethostbyname_r() interfaces.
263        * Since we don't know how big buffer this particular lookup required,
264        * we can't realloc down the huge alloc without doing closer analysis of
265        * the returned data. Thus, we always use CURL_HOSTENT_SIZE for every
266        * name lookup. Fixing this would require an extra malloc() and then
267        * calling Curl_addrinfo_copy() that subsequent realloc()s down the new
268        * memory area to the actually used amount.
269        */
270     }
271     else
272 #endif /* HAVE_GETHOSTBYNAME_R_3 */
273       {
274       infof(data, "gethostbyname_r(2) failed for %s\n", hostname);
275       h = NULL; /* set return code to NULL */
276       free(buf);
277     }
278 #else /* HAVE_GETHOSTBYNAME_R */
279     /*
280      * Here is code for platforms that don't have gethostbyname_r() or for
281      * which the gethostbyname() is the preferred() function.
282      */
283   else {
284     h = gethostbyname(hostname);
285     if (!h)
286       infof(data, "gethostbyname(2) failed for %s\n", hostname);
287 #endif /*HAVE_GETHOSTBYNAME_R */
288   }
289
290   if(h) {
291     ai = Curl_he2ai(h, port);
292
293     if (buf) /* used a *_r() function */
294       free(buf);
295   }
296
297   return ai;
298 }
299
300 #endif /* CURLRES_SYNCH */
301 #endif /* CURLRES_IPV4 */
302
303 /*
304  * Curl_he2ai() translates from a hostent struct to a Curl_addrinfo struct.
305  * The Curl_addrinfo is meant to work like the addrinfo struct does for IPv6
306  * stacks, but for all hosts and environments.
307  *
308  *   Curl_addrinfo defined in "lib/hostip.h"
309  *
310  *     struct Curl_addrinfo {
311  *       int                   ai_flags;
312  *       int                   ai_family;
313  *       int                   ai_socktype;
314  *       int                   ai_protocol;
315  *       socklen_t             ai_addrlen;   * Follow rfc3493 struct addrinfo *
316  *       char                 *ai_canonname;
317  *       struct sockaddr      *ai_addr;
318  *       struct Curl_addrinfo *ai_next;
319  *     };
320  *
321  *   hostent defined in <netdb.h>
322  *
323  *     struct hostent {
324  *       char    *h_name;
325  *       char    **h_aliases;
326  *       int     h_addrtype;
327  *       int     h_length;
328  *       char    **h_addr_list;
329  *     };
330  *
331  *   for backward compatibility:
332  *
333  *     #define h_addr  h_addr_list[0]
334  */
335
336 Curl_addrinfo *Curl_he2ai(const struct hostent *he, int port)
337 {
338   Curl_addrinfo *ai;
339   Curl_addrinfo *prevai = NULL;
340   Curl_addrinfo *firstai = NULL;
341   struct sockaddr_in *addr;
342   int i;
343   struct in_addr *curr;
344
345   if(!he)
346     /* no input == no output! */
347     return NULL;
348
349   for(i=0; (curr = (struct in_addr *)he->h_addr_list[i]) != NULL; i++) {
350
351     ai = calloc(1, sizeof(Curl_addrinfo) + sizeof(struct sockaddr_in));
352
353     if(!ai)
354       break;
355
356     if(!firstai)
357       /* store the pointer we want to return from this function */
358       firstai = ai;
359
360     if(prevai)
361       /* make the previous entry point to this */
362       prevai->ai_next = ai;
363
364     ai->ai_family = AF_INET;              /* we only support this */
365
366     /* we return all names as STREAM, so when using this address for TFTP
367        the type must be ignored and conn->socktype be used instead! */
368     ai->ai_socktype = SOCK_STREAM;
369
370     ai->ai_addrlen = sizeof(struct sockaddr_in);
371     /* make the ai_addr point to the address immediately following this struct
372        and use that area to store the address */
373     ai->ai_addr = (struct sockaddr *) ((char*)ai + sizeof(Curl_addrinfo));
374
375     /* leave the rest of the struct filled with zero */
376
377     addr = (struct sockaddr_in *)ai->ai_addr; /* storage area for this info */
378
379     memcpy((char *)&(addr->sin_addr), curr, sizeof(struct in_addr));
380     addr->sin_family = (unsigned short)(he->h_addrtype);
381     addr->sin_port = htons((unsigned short)port);
382
383     prevai = ai;
384   }
385   return firstai;
386 }
387