Resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Utilities / cmcurl-7.19.0 / lib / hostip6.c
blob94340dc2d6f2b75e1be09368e4af2f3a5c7dac93
1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2007, Daniel Stenberg, <daniel@haxx.se>, et al.
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.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 * $Id: hostip6.c,v 1.1.1.1 2008-09-23 16:32:05 hoffman Exp $
22 ***************************************************************************/
24 #include "setup.h"
26 #include <string.h>
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
55 #ifdef HAVE_SETJMP_H
56 #include <setjmp.h>
57 #endif
59 #ifdef HAVE_PROCESS_H
60 #include <process.h>
61 #endif
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"
73 #define _MPRINTF_REPLACE /* use our functions only */
74 #include <curl/mprintf.h>
76 #if defined(HAVE_INET_NTOA_R) && !defined(HAVE_INET_NTOA_R_DECL)
77 #include "inet_ntoa_r.h"
78 #endif
80 #include "memory.h"
81 /* The last #include file should be: */
82 #include "memdebug.h"
84 /***********************************************************************
85 * Only for ipv6-enabled builds
86 **********************************************************************/
87 #ifdef CURLRES_IPV6
88 #ifndef CURLRES_ARES
90 * This is a wrapper function for freeing name information in a protocol
91 * independent way. This takes care of using the appropriate underlaying
92 * function.
94 void Curl_freeaddrinfo(Curl_addrinfo *p)
96 freeaddrinfo(p);
99 #ifdef CURLRES_ASYNCH
101 * Curl_addrinfo_copy() is used by the asynch callback to copy a given
102 * address. But this is an ipv6 build and then we don't copy the address, we
103 * just return the same pointer!
105 Curl_addrinfo *Curl_addrinfo_copy(const void *orig, int port)
107 (void) port;
108 return (Curl_addrinfo*)orig;
110 #endif /* CURLRES_ASYNCH */
111 #endif /* CURLRES_ARES */
113 #ifdef CURLDEBUG
114 /* These are strictly for memory tracing and are using the same style as the
115 * family otherwise present in memdebug.c. I put these ones here since they
116 * require a bunch of structs I didn't wanna include in memdebug.c
118 int curl_dogetaddrinfo(const char *hostname, const char *service,
119 struct addrinfo *hints,
120 struct addrinfo **result,
121 int line, const char *source)
123 int res=(getaddrinfo)(hostname, service, hints, result);
124 if(0 == res) {
125 /* success */
126 if(logfile)
127 fprintf(logfile, "ADDR %s:%d getaddrinfo() = %p\n",
128 source, line, (void *)*result);
130 else {
131 if(logfile)
132 fprintf(logfile, "ADDR %s:%d getaddrinfo() failed\n",
133 source, line);
135 return res;
139 * For CURLRES_ARS, this should be written using ares_gethostbyaddr()
140 * (ignoring the fact c-ares doesn't return 'serv').
142 #ifdef HAVE_GETNAMEINFO
143 int curl_dogetnameinfo(GETNAMEINFO_QUAL_ARG1 GETNAMEINFO_TYPE_ARG1 sa,
144 GETNAMEINFO_TYPE_ARG2 salen,
145 char *host, GETNAMEINFO_TYPE_ARG46 hostlen,
146 char *serv, GETNAMEINFO_TYPE_ARG46 servlen,
147 GETNAMEINFO_TYPE_ARG7 flags,
148 int line, const char *source)
150 int res = (getnameinfo)(sa, salen,
151 host, hostlen,
152 serv, servlen,
153 flags);
154 if(0 == res) {
155 /* success */
156 if(logfile)
157 fprintf(logfile, "GETNAME %s:%d getnameinfo()\n",
158 source, line);
160 else {
161 if(logfile)
162 fprintf(logfile, "GETNAME %s:%d getnameinfo() failed = %d\n",
163 source, line, res);
165 return res;
167 #endif
169 void curl_dofreeaddrinfo(struct addrinfo *freethis,
170 int line, const char *source)
172 (freeaddrinfo)(freethis);
173 if(logfile)
174 fprintf(logfile, "ADDR %s:%d freeaddrinfo(%p)\n",
175 source, line, (void *)freethis);
177 #endif /* CURLDEBUG */
180 * Curl_ipvalid() checks what CURL_IPRESOLVE_* requirements that might've
181 * been set and returns TRUE if they are OK.
183 bool Curl_ipvalid(struct SessionHandle *data)
185 if(data->set.ip_version == CURL_IPRESOLVE_V6) {
186 /* see if we have an IPv6 stack */
187 curl_socket_t s = socket(PF_INET6, SOCK_DGRAM, 0);
188 if(s == CURL_SOCKET_BAD)
189 /* an ipv6 address was requested and we can't get/use one */
190 return FALSE;
191 sclose(s);
193 return TRUE;
196 #if !defined(USE_THREADING_GETADDRINFO) && !defined(CURLRES_ARES)
198 #ifdef DEBUG_ADDRINFO
199 static void dump_addrinfo(struct connectdata *conn, const struct addrinfo *ai)
201 printf("dump_addrinfo:\n");
202 for ( ; ai; ai = ai->ai_next) {
203 char buf[INET6_ADDRSTRLEN];
205 printf(" fam %2d, CNAME %s, ",
206 ai->ai_family, ai->ai_canonname ? ai->ai_canonname : "<none>");
207 if(Curl_printable_address(ai, buf, sizeof(buf)))
208 printf("%s\n", buf);
209 else
210 printf("failed; %s\n", Curl_strerror(conn, SOCKERRNO));
213 #else
214 #define dump_addrinfo(x,y)
215 #endif
218 * Curl_getaddrinfo() when built ipv6-enabled (non-threading and
219 * non-ares version).
221 * Returns name information about the given hostname and port number. If
222 * successful, the 'addrinfo' is returned and the forth argument will point to
223 * memory we need to free after use. That memory *MUST* be freed with
224 * Curl_freeaddrinfo(), nothing else.
226 Curl_addrinfo *Curl_getaddrinfo(struct connectdata *conn,
227 const char *hostname,
228 int port,
229 int *waitp)
231 struct addrinfo hints, *res;
232 int error;
233 char sbuf[NI_MAXSERV];
234 char *sbufptr = NULL;
235 char addrbuf[128];
236 int pf;
237 struct SessionHandle *data = conn->data;
239 *waitp=0; /* don't wait, we have the response now */
242 * Check if a limited name resolve has been requested.
244 switch(data->set.ip_version) {
245 case CURL_IPRESOLVE_V4:
246 pf = PF_INET;
247 break;
248 case CURL_IPRESOLVE_V6:
249 pf = PF_INET6;
250 break;
251 default:
252 pf = PF_UNSPEC;
253 break;
256 if (pf != PF_INET) {
257 /* see if we have an IPv6 stack */
258 curl_socket_t s = socket(PF_INET6, SOCK_DGRAM, 0);
259 if(s == CURL_SOCKET_BAD) {
260 /* Some non-IPv6 stacks have been found to make very slow name resolves
261 * when PF_UNSPEC is used, so thus we switch to a mere PF_INET lookup if
262 * the stack seems to be a non-ipv6 one. */
264 pf = PF_INET;
266 else {
267 /* This seems to be an IPv6-capable stack, use PF_UNSPEC for the widest
268 * possible checks. And close the socket again.
270 sclose(s);
274 memset(&hints, 0, sizeof(hints));
275 hints.ai_family = pf;
276 hints.ai_socktype = conn->socktype;
278 if((1 == Curl_inet_pton(AF_INET, hostname, addrbuf)) ||
279 (1 == Curl_inet_pton(AF_INET6, hostname, addrbuf))) {
280 /* the given address is numerical only, prevent a reverse lookup */
281 hints.ai_flags = AI_NUMERICHOST;
283 #ifdef HAVE_GSSAPI
284 if(conn->data->set.krb)
285 /* if krb is used, we (might) need the canonical host name */
286 hints.ai_flags |= AI_CANONNAME;
287 #endif
289 if(port) {
290 snprintf(sbuf, sizeof(sbuf), "%d", port);
291 sbufptr=sbuf;
293 error = getaddrinfo(hostname, sbufptr, &hints, &res);
294 if(error) {
295 infof(data, "getaddrinfo(3) failed for %s:%d\n", hostname, port);
296 return NULL;
299 dump_addrinfo(conn, res);
301 return res;
303 #endif /* !USE_THREADING_GETADDRINFO && !CURLRES_ARES */
304 #endif /* ipv6 */