1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2008, 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.
22 ***************************************************************************/
31 #ifdef HAVE_SYS_SOCKET_H
32 #include <sys/socket.h>
34 #ifdef HAVE_NETINET_IN_H
35 #include <netinet/in.h>
40 #ifdef HAVE_ARPA_INET_H
41 #include <arpa/inet.h>
44 #include <stdlib.h> /* required for free() prototypes */
47 #include <unistd.h> /* for the close() proto */
66 #include "inet_pton.h"
69 #define _MPRINTF_REPLACE /* use our functions only */
70 #include <curl/mprintf.h>
73 /* The last #include file should be: */
76 /***********************************************************************
77 * Only for ipv6-enabled builds
78 **********************************************************************/
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!
88 Curl_addrinfo *Curl_addrinfo_copy(const void *orig, int port)
91 return (Curl_addrinfo*)orig;
93 #endif /* CURLRES_ASYNCH */
94 #endif /* CURLRES_ARES */
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
101 int curl_dogetaddrinfo(const char *hostname, const char *service,
102 struct addrinfo *hints,
103 struct addrinfo **result,
104 int line, const char *source)
106 int res=(getaddrinfo)(hostname, service, hints, result);
110 fprintf(logfile, "ADDR %s:%d getaddrinfo() = %p\n",
111 source, line, (void *)*result);
115 fprintf(logfile, "ADDR %s:%d getaddrinfo() failed\n",
122 * For CURLRES_ARS, this should be written using ares_gethostbyaddr()
123 * (ignoring the fact c-ares doesn't return 'serv').
125 #ifdef HAVE_GETNAMEINFO
126 int curl_dogetnameinfo(GETNAMEINFO_QUAL_ARG1 GETNAMEINFO_TYPE_ARG1 sa,
127 GETNAMEINFO_TYPE_ARG2 salen,
128 char *host, GETNAMEINFO_TYPE_ARG46 hostlen,
129 char *serv, GETNAMEINFO_TYPE_ARG46 servlen,
130 GETNAMEINFO_TYPE_ARG7 flags,
131 int line, const char *source)
133 int res = (getnameinfo)(sa, salen,
140 fprintf(logfile, "GETNAME %s:%d getnameinfo()\n",
145 fprintf(logfile, "GETNAME %s:%d getnameinfo() failed = %d\n",
152 void curl_dofreeaddrinfo(struct addrinfo *freethis,
153 int line, const char *source)
155 (freeaddrinfo)(freethis);
157 fprintf(logfile, "ADDR %s:%d freeaddrinfo(%p)\n",
158 source, line, (void *)freethis);
160 #endif /* CURLDEBUG */
163 * Curl_ipvalid() checks what CURL_IPRESOLVE_* requirements that might've
164 * been set and returns TRUE if they are OK.
166 bool Curl_ipvalid(struct SessionHandle *data)
168 if(data->set.ip_version == CURL_IPRESOLVE_V6) {
169 /* see if we have an IPv6 stack */
170 curl_socket_t s = socket(PF_INET6, SOCK_DGRAM, 0);
171 if(s == CURL_SOCKET_BAD)
172 /* an ipv6 address was requested and we can't get/use one */
179 #if !defined(USE_THREADING_GETADDRINFO) && !defined(CURLRES_ARES)
181 #ifdef DEBUG_ADDRINFO
182 static void dump_addrinfo(struct connectdata *conn, const Curl_addrinfo *ai)
184 printf("dump_addrinfo:\n");
185 for ( ; ai; ai = ai->ai_next) {
186 char buf[INET6_ADDRSTRLEN];
188 printf(" fam %2d, CNAME %s, ",
189 ai->ai_family, ai->ai_canonname ? ai->ai_canonname : "<none>");
190 if(Curl_printable_address(ai, buf, sizeof(buf)))
193 printf("failed; %s\n", Curl_strerror(conn, SOCKERRNO));
197 #define dump_addrinfo(x,y)
201 * Curl_getaddrinfo() when built ipv6-enabled (non-threading and
204 * Returns name information about the given hostname and port number. If
205 * successful, the 'addrinfo' is returned and the forth argument will point to
206 * memory we need to free after use. That memory *MUST* be freed with
207 * Curl_freeaddrinfo(), nothing else.
209 Curl_addrinfo *Curl_getaddrinfo(struct connectdata *conn,
210 const char *hostname,
214 struct addrinfo hints;
217 char sbuf[NI_MAXSERV];
218 char *sbufptr = NULL;
221 struct SessionHandle *data = conn->data;
223 *waitp=0; /* don't wait, we have the response now */
226 * Check if a limited name resolve has been requested.
228 switch(data->set.ip_version) {
229 case CURL_IPRESOLVE_V4:
232 case CURL_IPRESOLVE_V6:
241 /* see if we have an IPv6 stack */
242 curl_socket_t s = socket(PF_INET6, SOCK_DGRAM, 0);
243 if(s == CURL_SOCKET_BAD) {
244 /* Some non-IPv6 stacks have been found to make very slow name resolves
245 * when PF_UNSPEC is used, so thus we switch to a mere PF_INET lookup if
246 * the stack seems to be a non-ipv6 one. */
251 /* This seems to be an IPv6-capable stack, use PF_UNSPEC for the widest
252 * possible checks. And close the socket again.
258 memset(&hints, 0, sizeof(hints));
259 hints.ai_family = pf;
260 hints.ai_socktype = conn->socktype;
262 if((1 == Curl_inet_pton(AF_INET, hostname, addrbuf)) ||
263 (1 == Curl_inet_pton(AF_INET6, hostname, addrbuf))) {
264 /* the given address is numerical only, prevent a reverse lookup */
265 hints.ai_flags = AI_NUMERICHOST;
268 if(conn->data->set.krb)
269 /* if krb is used, we (might) need the canonical host name */
270 hints.ai_flags |= AI_CANONNAME;
274 snprintf(sbuf, sizeof(sbuf), "%d", port);
277 error = Curl_getaddrinfo_ex(hostname, sbufptr, &hints, &res);
279 infof(data, "getaddrinfo(3) failed for %s:%d\n", hostname, port);
283 dump_addrinfo(conn, res);
287 #endif /* !USE_THREADING_GETADDRINFO && !CURLRES_ARES */