1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2010, 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 ***************************************************************************/
27 #ifdef HAVE_SYS_SOCKET_H
28 #include <sys/socket.h>
30 #ifdef HAVE_NETINET_IN_H
31 #include <netinet/in.h>
36 #ifdef HAVE_ARPA_INET_H
37 #include <arpa/inet.h>
40 #include <stdlib.h> /* required for free() prototypes */
43 #include <unistd.h> /* for the close() proto */
62 #include "inet_pton.h"
65 #define _MPRINTF_REPLACE /* use our functions only */
66 #include <curl/mprintf.h>
68 #include "curl_memory.h"
69 /* The last #include file should be: */
72 /***********************************************************************
73 * Only for ipv6-enabled builds
74 **********************************************************************/
78 #if defined(CURLDEBUG) && defined(HAVE_GETNAMEINFO)
79 /* These are strictly for memory tracing and are using the same style as the
80 * family otherwise present in memdebug.c. I put these ones here since they
81 * require a bunch of structs I didn't wanna include in memdebug.c
85 * For CURLRES_ARS, this should be written using ares_gethostbyaddr()
86 * (ignoring the fact c-ares doesn't return 'serv').
89 int curl_dogetnameinfo(GETNAMEINFO_QUAL_ARG1 GETNAMEINFO_TYPE_ARG1 sa,
90 GETNAMEINFO_TYPE_ARG2 salen,
91 char *host, GETNAMEINFO_TYPE_ARG46 hostlen,
92 char *serv, GETNAMEINFO_TYPE_ARG46 servlen,
93 GETNAMEINFO_TYPE_ARG7 flags,
94 int line, const char *source)
96 int res = (getnameinfo)(sa, salen,
102 curl_memlog("GETNAME %s:%d getnameinfo()\n",
105 curl_memlog("GETNAME %s:%d getnameinfo() failed = %d\n",
109 #endif /* defined(CURLDEBUG) && defined(HAVE_GETNAMEINFO) */
112 * Curl_ipvalid() checks what CURL_IPRESOLVE_* requirements that might've
113 * been set and returns TRUE if they are OK.
115 bool Curl_ipvalid(struct connectdata *conn)
117 if(conn->ip_version == CURL_IPRESOLVE_V6) {
118 /* see if we have an IPv6 stack */
119 curl_socket_t s = socket(PF_INET6, SOCK_DGRAM, 0);
120 if(s == CURL_SOCKET_BAD)
121 /* an ipv6 address was requested and we can't get/use one */
128 #if defined(CURLRES_SYNCH)
130 #ifdef DEBUG_ADDRINFO
131 static void dump_addrinfo(struct connectdata *conn, const Curl_addrinfo *ai)
133 printf("dump_addrinfo:\n");
134 for ( ; ai; ai = ai->ai_next) {
135 char buf[INET6_ADDRSTRLEN];
137 printf(" fam %2d, CNAME %s, ",
138 ai->ai_family, ai->ai_canonname ? ai->ai_canonname : "<none>");
139 if(Curl_printable_address(ai, buf, sizeof(buf)))
142 printf("failed; %s\n", Curl_strerror(conn, SOCKERRNO));
146 #define dump_addrinfo(x,y)
150 * Curl_getaddrinfo() when built ipv6-enabled (non-threading and
153 * Returns name information about the given hostname and port number. If
154 * successful, the 'addrinfo' is returned and the forth argument will point to
155 * memory we need to free after use. That memory *MUST* be freed with
156 * Curl_freeaddrinfo(), nothing else.
158 Curl_addrinfo *Curl_getaddrinfo(struct connectdata *conn,
159 const char *hostname,
163 struct addrinfo hints;
166 char sbuf[NI_MAXSERV];
167 char *sbufptr = NULL;
170 struct SessionHandle *data = conn->data;
172 *waitp = 0; /* synchronous response only */
175 * Check if a limited name resolve has been requested.
177 switch(conn->ip_version) {
178 case CURL_IPRESOLVE_V4:
181 case CURL_IPRESOLVE_V6:
190 /* see if we have an IPv6 stack */
191 curl_socket_t s = socket(PF_INET6, SOCK_DGRAM, 0);
192 if(s == CURL_SOCKET_BAD) {
193 /* Some non-IPv6 stacks have been found to make very slow name resolves
194 * when PF_UNSPEC is used, so thus we switch to a mere PF_INET lookup if
195 * the stack seems to be a non-ipv6 one. */
200 /* This seems to be an IPv6-capable stack, use PF_UNSPEC for the widest
201 * possible checks. And close the socket again.
207 memset(&hints, 0, sizeof(hints));
208 hints.ai_family = pf;
209 hints.ai_socktype = conn->socktype;
211 if((1 == Curl_inet_pton(AF_INET, hostname, addrbuf)) ||
212 (1 == Curl_inet_pton(AF_INET6, hostname, addrbuf))) {
213 /* the given address is numerical only, prevent a reverse lookup */
214 hints.ai_flags = AI_NUMERICHOST;
218 snprintf(sbuf, sizeof(sbuf), "%d", port);
221 error = Curl_getaddrinfo_ex(hostname, sbufptr, &hints, &res);
223 infof(data, "getaddrinfo(3) failed for %s:%d\n", hostname, port);
227 dump_addrinfo(conn, res);
231 #endif /* CURLRES_SYNCH */
232 #endif /* CURLRES_IPV6 */