Simplify check for NEED_MALLOC_H, and make more explicit that NEED_MALLOC_H shall...
[platform/upstream/curl.git] / lib / hostip6.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2005, 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
28 #ifdef NEED_MALLOC_H
29 #include <malloc.h>
30 #endif
31 #ifdef HAVE_SYS_TYPES_H
32 #include <sys/types.h>
33 #endif
34 #ifdef HAVE_SYS_SOCKET_H
35 #include <sys/socket.h>
36 #endif
37 #ifdef HAVE_NETINET_IN_H
38 #include <netinet/in.h>
39 #endif
40 #ifdef HAVE_NETDB_H
41 #include <netdb.h>
42 #endif
43 #ifdef HAVE_ARPA_INET_H
44 #include <arpa/inet.h>
45 #endif
46 #ifdef HAVE_STDLIB_H
47 #include <stdlib.h>     /* required for free() prototypes */
48 #endif
49 #ifdef HAVE_UNISTD_H
50 #include <unistd.h>     /* for the close() proto */
51 #endif
52 #ifdef  VMS
53 #include <in.h>
54 #include <inet.h>
55 #include <stdlib.h>
56 #endif
57
58 #ifdef HAVE_SETJMP_H
59 #include <setjmp.h>
60 #endif
61
62 #ifdef HAVE_PROCESS_H
63 #include <process.h>
64 #endif
65
66 #include "urldata.h"
67 #include "sendf.h"
68 #include "hostip.h"
69 #include "hash.h"
70 #include "share.h"
71 #include "strerror.h"
72 #include "url.h"
73 #include "inet_pton.h"
74 #include "connect.h"
75
76 #define _MPRINTF_REPLACE /* use our functions only */
77 #include <curl/mprintf.h>
78
79 #if defined(HAVE_INET_NTOA_R) && !defined(HAVE_INET_NTOA_R_DECL)
80 #include "inet_ntoa_r.h"
81 #endif
82
83 #include "memory.h"
84 /* The last #include file should be: */
85 #include "memdebug.h"
86
87 /***********************************************************************
88  * Only for ipv6-enabled builds
89  **********************************************************************/
90 #ifdef CURLRES_IPV6
91 #ifndef CURLRES_ARES
92 /*
93  * This is a wrapper function for freeing name information in a protocol
94  * independent way. This takes care of using the appropriate underlaying
95  * function.
96  */
97 void Curl_freeaddrinfo(Curl_addrinfo *p)
98 {
99   freeaddrinfo(p);
100 }
101
102 #ifdef CURLRES_ASYNCH
103 /*
104  * Curl_addrinfo_copy() is used by the asynch callback to copy a given
105  * address. But this is an ipv6 build and then we don't copy the address, we
106  * just return the same pointer!
107  */
108 Curl_addrinfo *Curl_addrinfo_copy(const void *orig, int port)
109 {
110   (void) port;
111   return (Curl_addrinfo*)orig;
112 }
113 #endif  /* CURLRES_ASYNCH */
114 #endif  /* CURLRES_ARES */
115
116 #ifdef CURLDEBUG
117 /* These are strictly for memory tracing and are using the same style as the
118  * family otherwise present in memdebug.c. I put these ones here since they
119  * require a bunch of structs I didn't wanna include in memdebug.c
120  */
121 int curl_dogetaddrinfo(const char *hostname, const char *service,
122                        struct addrinfo *hints,
123                        struct addrinfo **result,
124                        int line, const char *source)
125 {
126   int res=(getaddrinfo)(hostname, service, hints, result);
127   if(0 == res) {
128     /* success */
129     if(logfile)
130       fprintf(logfile, "ADDR %s:%d getaddrinfo() = %p\n",
131               source, line, (void *)*result);
132   }
133   else {
134     if(logfile)
135       fprintf(logfile, "ADDR %s:%d getaddrinfo() failed\n",
136               source, line);
137   }
138   return res;
139 }
140
141 /*
142  * For CURLRES_ARS, this should be written using ares_gethostbyaddr()
143  * (ignoring the fact c-ares doesn't return 'serv').
144  */
145 #ifdef HAVE_GETNAMEINFO
146 int curl_dogetnameinfo(GETNAMEINFO_QUAL_ARG1 GETNAMEINFO_TYPE_ARG1 sa,
147                        GETNAMEINFO_TYPE_ARG2 salen,
148                        char *host, GETNAMEINFO_TYPE_ARG46 hostlen,
149                        char *serv, GETNAMEINFO_TYPE_ARG46 servlen,
150                        GETNAMEINFO_TYPE_ARG7 flags,
151                        int line, const char *source)
152 {
153   int res = (getnameinfo)(sa, salen,
154                           host, hostlen,
155                           serv, servlen,
156                           flags);
157   if(0 == res) {
158     /* success */
159     if(logfile)
160       fprintf(logfile, "GETNAME %s:%d getnameinfo()\n",
161               source, line);
162   }
163   else {
164     if(logfile)
165       fprintf(logfile, "GETNAME %s:%d getnameinfo() failed = %d\n",
166               source, line, res);
167   }
168   return res;
169 }
170 #endif
171
172 void curl_dofreeaddrinfo(struct addrinfo *freethis,
173                          int line, const char *source)
174 {
175   (freeaddrinfo)(freethis);
176   if(logfile)
177     fprintf(logfile, "ADDR %s:%d freeaddrinfo(%p)\n",
178             source, line, (void *)freethis);
179 }
180 #endif  /* CURLDEBUG */
181
182 /*
183  * Curl_ipvalid() checks what CURL_IPRESOLVE_* requirements that might've
184  * been set and returns TRUE if they are OK.
185  */
186 bool Curl_ipvalid(struct SessionHandle *data)
187 {
188   if(data->set.ip_version == CURL_IPRESOLVE_V6) {
189     /* see if we have an IPv6 stack */
190     curl_socket_t s = socket(PF_INET6, SOCK_DGRAM, 0);
191     if (s == CURL_SOCKET_BAD)
192       /* an ipv6 address was requested and we can't get/use one */
193       return FALSE;
194     sclose(s);
195   }
196   return TRUE;
197 }
198
199 #if !defined(USE_THREADING_GETADDRINFO) && !defined(CURLRES_ARES)
200
201 #ifdef DEBUG_ADDRINFO
202 static void dump_addrinfo(struct connectdata *conn, const struct addrinfo *ai)
203 {
204   printf("dump_addrinfo:\n");
205   for ( ; ai; ai = ai->ai_next) {
206     char  buf[INET6_ADDRSTRLEN];
207
208     printf("    fam %2d, CNAME %s, ",
209            ai->ai_family, ai->ai_canonname ? ai->ai_canonname : "<none>");
210     if (Curl_printable_address(ai, buf, sizeof(buf)))
211       printf("%s\n", buf);
212     else
213       printf("failed; %s\n", Curl_strerror(conn, Curl_sockerrno()));
214   }
215 }
216 #else
217 #define dump_addrinfo(x,y)
218 #endif
219
220 /*
221  * Curl_getaddrinfo() when built ipv6-enabled (non-threading and
222  * non-ares version).
223  *
224  * Returns name information about the given hostname and port number. If
225  * successful, the 'addrinfo' is returned and the forth argument will point to
226  * memory we need to free after use. That memory *MUST* be freed with
227  * Curl_freeaddrinfo(), nothing else.
228  */
229 Curl_addrinfo *Curl_getaddrinfo(struct connectdata *conn,
230                                 const char *hostname,
231                                 int port,
232                                 int *waitp)
233 {
234   struct addrinfo hints, *res;
235   int error;
236   char sbuf[NI_MAXSERV];
237   char *sbufptr = NULL;
238   char addrbuf[128];
239   curl_socket_t s;
240   int pf;
241   struct SessionHandle *data = conn->data;
242
243   *waitp=0; /* don't wait, we have the response now */
244
245   /* see if we have an IPv6 stack */
246   s = socket(PF_INET6, SOCK_DGRAM, 0);
247   if (s != CURL_SOCKET_BAD) {
248     /* Some non-IPv6 stacks have been found to make very slow name resolves
249      * when PF_UNSPEC is used, so thus we switch to a mere PF_INET lookup if
250      * the stack seems to be a non-ipv6 one. */
251
252     pf = PF_INET;
253   }
254   else {
255     /* This seems to be an IPv6-capable stack, use PF_UNSPEC for the widest
256      * possible checks. And close the socket again.
257      */
258     sclose(s);
259
260     /*
261      * Check if a more limited name resolve has been requested.
262      */
263     switch(data->set.ip_version) {
264     case CURL_IPRESOLVE_V4:
265       pf = PF_INET;
266       break;
267     case CURL_IPRESOLVE_V6:
268       pf = PF_INET6;
269       break;
270     default:
271       pf = PF_UNSPEC;
272       break;
273     }
274   }
275
276   memset(&hints, 0, sizeof(hints));
277   hints.ai_family = pf;
278   hints.ai_socktype = conn->socktype;
279
280   if((1 == Curl_inet_pton(AF_INET, hostname, addrbuf)) ||
281      (1 == Curl_inet_pton(AF_INET6, hostname, addrbuf))) {
282     /* the given address is numerical only, prevent a reverse lookup */
283     hints.ai_flags = AI_NUMERICHOST;
284   }
285 #if 0 /* removed nov 8 2005 before 7.15.1 */
286   else
287     hints.ai_flags = AI_CANONNAME;
288 #endif
289
290   if(port) {
291     snprintf(sbuf, sizeof(sbuf), "%d", port);
292     sbufptr=sbuf;
293   }
294   error = getaddrinfo(hostname, sbufptr, &hints, &res);
295   if (error) {
296     infof(data, "getaddrinfo(3) failed for %s:%d\n", hostname, port);
297     return NULL;
298   }
299
300   dump_addrinfo(conn, res);
301
302   return res;
303 }
304 #endif /* !USE_THREADING_GETADDRINFO && !CURLRES_ARES */
305 #endif /* ipv6 */
306