1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2008, 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: hostip6.c,v 1.47 2008-10-30 19:02:23 yangtse Exp $
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_PROCESS_H
56 #include <process.h>
57 #endif
58
59 #include "urldata.h"
60 #include "sendf.h"
61 #include "hostip.h"
62 #include "hash.h"
63 #include "share.h"
64 #include "strerror.h"
65 #include "url.h"
66 #include "inet_pton.h"
67 #include "connect.h"
68
69 #define _MPRINTF_REPLACE /* use our functions only */
70 #include <curl/mprintf.h>
71
72 #include "memory.h"
73 /* The last #include file should be: */
74 #include "memdebug.h"
75
76 /***********************************************************************
77 * Only for ipv6-enabled builds
78 **********************************************************************/
79 #ifdef CURLRES_IPV6
80
81 #ifndef CURLRES_ARES
82 #ifdef CURLRES_ASYNCH
83 /*
84 * Curl_addrinfo_copy() is used by the asynch callback to copy a given
85 * address. But this is an ipv6 build and then we don't copy the address, we
86 * just return the same pointer!
87 */
Curl_addrinfo_copy(const void * orig,int port)88 Curl_addrinfo *Curl_addrinfo_copy(const void *orig, int port)
89 {
90 (void) port;
91 return (Curl_addrinfo*)orig;
92 }
93 #endif /* CURLRES_ASYNCH */
94 #endif /* CURLRES_ARES */
95
96 #if defined(CURLDEBUG) && defined(HAVE_GETNAMEINFO)
97 /* These are strictly for memory tracing and are using the same style as the
98 * family otherwise present in memdebug.c. I put these ones here since they
99 * require a bunch of structs I didn't wanna include in memdebug.c
100 */
101
102 /*
103 * For CURLRES_ARS, this should be written using ares_gethostbyaddr()
104 * (ignoring the fact c-ares doesn't return 'serv').
105 */
106
curl_dogetnameinfo(GETNAMEINFO_QUAL_ARG1 GETNAMEINFO_TYPE_ARG1 sa,GETNAMEINFO_TYPE_ARG2 salen,char * host,GETNAMEINFO_TYPE_ARG46 hostlen,char * serv,GETNAMEINFO_TYPE_ARG46 servlen,GETNAMEINFO_TYPE_ARG7 flags,int line,const char * source)107 int curl_dogetnameinfo(GETNAMEINFO_QUAL_ARG1 GETNAMEINFO_TYPE_ARG1 sa,
108 GETNAMEINFO_TYPE_ARG2 salen,
109 char *host, GETNAMEINFO_TYPE_ARG46 hostlen,
110 char *serv, GETNAMEINFO_TYPE_ARG46 servlen,
111 GETNAMEINFO_TYPE_ARG7 flags,
112 int line, const char *source)
113 {
114 int res = (getnameinfo)(sa, salen,
115 host, hostlen,
116 serv, servlen,
117 flags);
118 if(0 == res) {
119 /* success */
120 if(logfile)
121 fprintf(logfile, "GETNAME %s:%d getnameinfo()\n",
122 source, line);
123 }
124 else {
125 if(logfile)
126 fprintf(logfile, "GETNAME %s:%d getnameinfo() failed = %d\n",
127 source, line, res);
128 }
129 return res;
130 }
131 #endif /* defined(CURLDEBUG) && defined(HAVE_GETNAMEINFO) */
132
133 /*
134 * Curl_ipvalid() checks what CURL_IPRESOLVE_* requirements that might've
135 * been set and returns TRUE if they are OK.
136 */
Curl_ipvalid(struct SessionHandle * data)137 bool Curl_ipvalid(struct SessionHandle *data)
138 {
139 if(data->set.ip_version == CURL_IPRESOLVE_V6) {
140 /* see if we have an IPv6 stack */
141 curl_socket_t s = socket(PF_INET6, SOCK_DGRAM, 0);
142 if(s == CURL_SOCKET_BAD)
143 /* an ipv6 address was requested and we can't get/use one */
144 return FALSE;
145 sclose(s);
146 }
147 return TRUE;
148 }
149
150 #if !defined(USE_THREADING_GETADDRINFO) && !defined(CURLRES_ARES)
151
152 #ifdef DEBUG_ADDRINFO
dump_addrinfo(struct connectdata * conn,const Curl_addrinfo * ai)153 static void dump_addrinfo(struct connectdata *conn, const Curl_addrinfo *ai)
154 {
155 printf("dump_addrinfo:\n");
156 for ( ; ai; ai = ai->ai_next) {
157 char buf[INET6_ADDRSTRLEN];
158
159 printf(" fam %2d, CNAME %s, ",
160 ai->ai_family, ai->ai_canonname ? ai->ai_canonname : "<none>");
161 if(Curl_printable_address(ai, buf, sizeof(buf)))
162 printf("%s\n", buf);
163 else
164 printf("failed; %s\n", Curl_strerror(conn, SOCKERRNO));
165 }
166 }
167 #else
168 #define dump_addrinfo(x,y)
169 #endif
170
171 /*
172 * Curl_getaddrinfo() when built ipv6-enabled (non-threading and
173 * non-ares version).
174 *
175 * Returns name information about the given hostname and port number. If
176 * successful, the 'addrinfo' is returned and the forth argument will point to
177 * memory we need to free after use. That memory *MUST* be freed with
178 * Curl_freeaddrinfo(), nothing else.
179 */
Curl_getaddrinfo(struct connectdata * conn,const char * hostname,int port,int * waitp)180 Curl_addrinfo *Curl_getaddrinfo(struct connectdata *conn,
181 const char *hostname,
182 int port,
183 int *waitp)
184 {
185 struct addrinfo hints;
186 Curl_addrinfo *res;
187 int error;
188 char sbuf[NI_MAXSERV];
189 char *sbufptr = NULL;
190 char addrbuf[128];
191 int pf;
192 struct SessionHandle *data = conn->data;
193
194 *waitp=0; /* don't wait, we have the response now */
195
196 /*
197 * Check if a limited name resolve has been requested.
198 */
199 switch(data->set.ip_version) {
200 case CURL_IPRESOLVE_V4:
201 pf = PF_INET;
202 break;
203 case CURL_IPRESOLVE_V6:
204 pf = PF_INET6;
205 break;
206 default:
207 pf = PF_UNSPEC;
208 break;
209 }
210
211 if (pf != PF_INET) {
212 /* see if we have an IPv6 stack */
213 curl_socket_t s = socket(PF_INET6, SOCK_DGRAM, 0);
214 if(s == CURL_SOCKET_BAD) {
215 /* Some non-IPv6 stacks have been found to make very slow name resolves
216 * when PF_UNSPEC is used, so thus we switch to a mere PF_INET lookup if
217 * the stack seems to be a non-ipv6 one. */
218
219 pf = PF_INET;
220 }
221 else {
222 /* This seems to be an IPv6-capable stack, use PF_UNSPEC for the widest
223 * possible checks. And close the socket again.
224 */
225 sclose(s);
226 }
227 }
228
229 memset(&hints, 0, sizeof(hints));
230 hints.ai_family = pf;
231 hints.ai_socktype = conn->socktype;
232
233 if((1 == Curl_inet_pton(AF_INET, hostname, addrbuf)) ||
234 (1 == Curl_inet_pton(AF_INET6, hostname, addrbuf))) {
235 /* the given address is numerical only, prevent a reverse lookup */
236 hints.ai_flags = AI_NUMERICHOST;
237 }
238 #ifdef HAVE_GSSAPI
239 if(conn->data->set.krb)
240 /* if krb is used, we (might) need the canonical host name */
241 hints.ai_flags |= AI_CANONNAME;
242 #endif
243
244 if(port) {
245 snprintf(sbuf, sizeof(sbuf), "%d", port);
246 sbufptr=sbuf;
247 }
248 error = Curl_getaddrinfo_ex(hostname, sbufptr, &hints, &res);
249 if(error) {
250 infof(data, "getaddrinfo(3) failed for %s:%d\n", hostname, port);
251 return NULL;
252 }
253
254 dump_addrinfo(conn, res);
255
256 return res;
257 }
258 #endif /* !USE_THREADING_GETADDRINFO && !CURLRES_ARES */
259 #endif /* ipv6 */
260
261