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