2004-04-26 09:20:11 +02:00
|
|
|
/***************************************************************************
|
2004-08-19 08:44:19 +02:00
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
2004-04-26 09:20:11 +02:00
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2011-03-23 11:10:55 +01:00
|
|
|
* Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2004-04-26 09:20:11 +02:00
|
|
|
*
|
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
|
|
|
* are also available at http://curl.haxx.se/docs/copyright.html.
|
2004-08-19 08:44:19 +02:00
|
|
|
*
|
2004-04-26 09:20:11 +02:00
|
|
|
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
|
|
* copies of the Software, and permit persons to whom the Software is
|
|
|
|
* furnished to do so, under the terms of the COPYING file.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#include "setup.h"
|
|
|
|
|
|
|
|
#ifdef HAVE_SYS_SOCKET_H
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_NETINET_IN_H
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_NETDB_H
|
|
|
|
#include <netdb.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_ARPA_INET_H
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h> /* for the close() proto */
|
|
|
|
#endif
|
2009-12-30 18:59:56 +01:00
|
|
|
#ifdef __VMS
|
2004-04-26 09:20:11 +02:00
|
|
|
#include <in.h>
|
|
|
|
#include <inet.h>
|
|
|
|
#endif
|
|
|
|
|
2006-04-26 19:23:28 +02:00
|
|
|
#ifdef HAVE_PROCESS_H
|
2004-04-26 09:20:11 +02:00
|
|
|
#include <process.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "urldata.h"
|
|
|
|
#include "sendf.h"
|
|
|
|
#include "hostip.h"
|
|
|
|
#include "hash.h"
|
|
|
|
#include "share.h"
|
|
|
|
#include "strerror.h"
|
|
|
|
#include "url.h"
|
2005-03-17 20:12:15 +01:00
|
|
|
#include "inet_pton.h"
|
2005-10-20 21:40:02 +02:00
|
|
|
#include "connect.h"
|
2004-04-26 09:20:11 +02:00
|
|
|
|
|
|
|
#define _MPRINTF_REPLACE /* use our functions only */
|
|
|
|
#include <curl/mprintf.h>
|
|
|
|
|
2009-04-21 13:46:16 +02:00
|
|
|
#include "curl_memory.h"
|
2004-04-26 09:20:11 +02:00
|
|
|
/* The last #include file should be: */
|
|
|
|
#include "memdebug.h"
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* Only for ipv6-enabled builds
|
|
|
|
**********************************************************************/
|
|
|
|
#ifdef CURLRES_IPV6
|
|
|
|
|
|
|
|
|
2008-10-30 20:02:23 +01:00
|
|
|
#if defined(CURLDEBUG) && defined(HAVE_GETNAMEINFO)
|
2004-04-26 09:20:11 +02:00
|
|
|
/* These are strictly for memory tracing and are using the same style as the
|
|
|
|
* family otherwise present in memdebug.c. I put these ones here since they
|
2011-04-19 15:55:37 +02:00
|
|
|
* require a bunch of structs I didn't want to include in memdebug.c
|
2004-04-26 09:20:11 +02:00
|
|
|
*/
|
|
|
|
|
2006-07-24 17:58:33 +02:00
|
|
|
/*
|
|
|
|
* For CURLRES_ARS, this should be written using ares_gethostbyaddr()
|
|
|
|
* (ignoring the fact c-ares doesn't return 'serv').
|
|
|
|
*/
|
2008-10-30 20:02:23 +01:00
|
|
|
|
2006-07-03 20:38:03 +02:00
|
|
|
int 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,
|
2004-04-26 09:20:11 +02:00
|
|
|
int line, const char *source)
|
|
|
|
{
|
2006-04-26 19:23:28 +02:00
|
|
|
int res = (getnameinfo)(sa, salen,
|
|
|
|
host, hostlen,
|
|
|
|
serv, servlen,
|
|
|
|
flags);
|
2010-01-18 21:22:04 +01:00
|
|
|
if(0 == res)
|
2004-04-26 09:20:11 +02:00
|
|
|
/* success */
|
2010-01-18 21:22:04 +01:00
|
|
|
curl_memlog("GETNAME %s:%d getnameinfo()\n",
|
|
|
|
source, line);
|
|
|
|
else
|
|
|
|
curl_memlog("GETNAME %s:%d getnameinfo() failed = %d\n",
|
|
|
|
source, line, res);
|
2004-04-26 09:20:11 +02:00
|
|
|
return res;
|
|
|
|
}
|
2008-10-30 20:02:23 +01:00
|
|
|
#endif /* defined(CURLDEBUG) && defined(HAVE_GETNAMEINFO) */
|
2004-04-26 09:20:11 +02:00
|
|
|
|
|
|
|
/*
|
2011-03-23 11:10:55 +01:00
|
|
|
* Curl_ipv6works() returns TRUE if ipv6 seems to work.
|
2004-04-26 09:20:11 +02:00
|
|
|
*/
|
2011-03-23 11:10:55 +01:00
|
|
|
bool Curl_ipv6works(void)
|
2004-04-26 09:20:11 +02:00
|
|
|
{
|
2011-03-23 11:10:55 +01:00
|
|
|
/* the nature of most system is that IPv6 status doesn't come and go
|
|
|
|
during a program's lifetime so we only probe the first time and then we
|
|
|
|
have the info kept for fast re-use */
|
|
|
|
static int ipv6_works = -1;
|
|
|
|
if(-1 == ipv6_works) {
|
|
|
|
/* probe to see if we have a working IPv6 stack */
|
2004-04-26 09:20:11 +02:00
|
|
|
curl_socket_t s = socket(PF_INET6, SOCK_DGRAM, 0);
|
2007-11-07 10:21:35 +01:00
|
|
|
if(s == CURL_SOCKET_BAD)
|
2011-03-23 11:10:55 +01:00
|
|
|
/* an ipv6 address was requested but we can't get/use one */
|
|
|
|
ipv6_works = 0;
|
|
|
|
else {
|
|
|
|
ipv6_works = 1;
|
2011-05-16 23:46:43 +02:00
|
|
|
Curl_closesocket(NULL, s);
|
2011-03-23 11:10:55 +01:00
|
|
|
}
|
2004-04-26 09:20:11 +02:00
|
|
|
}
|
2011-03-23 11:10:55 +01:00
|
|
|
return (ipv6_works>0)?TRUE:FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Curl_ipvalid() checks what CURL_IPRESOLVE_* requirements that might've
|
|
|
|
* been set and returns TRUE if they are OK.
|
|
|
|
*/
|
|
|
|
bool Curl_ipvalid(struct connectdata *conn)
|
|
|
|
{
|
|
|
|
if(conn->ip_version == CURL_IPRESOLVE_V6)
|
|
|
|
return Curl_ipv6works();
|
2004-04-26 09:20:11 +02:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2010-01-26 00:50:13 +01:00
|
|
|
#if defined(CURLRES_SYNCH)
|
2005-10-20 21:40:02 +02:00
|
|
|
|
|
|
|
#ifdef DEBUG_ADDRINFO
|
2008-10-30 14:45:25 +01:00
|
|
|
static void dump_addrinfo(struct connectdata *conn, const Curl_addrinfo *ai)
|
2005-10-20 21:40:02 +02:00
|
|
|
{
|
|
|
|
printf("dump_addrinfo:\n");
|
2011-04-20 15:17:42 +02:00
|
|
|
for(; ai; ai = ai->ai_next) {
|
2005-10-20 21:40:02 +02:00
|
|
|
char buf[INET6_ADDRSTRLEN];
|
|
|
|
|
|
|
|
printf(" fam %2d, CNAME %s, ",
|
|
|
|
ai->ai_family, ai->ai_canonname ? ai->ai_canonname : "<none>");
|
2007-11-07 10:21:35 +01:00
|
|
|
if(Curl_printable_address(ai, buf, sizeof(buf)))
|
2005-10-20 21:40:02 +02:00
|
|
|
printf("%s\n", buf);
|
|
|
|
else
|
2007-02-16 19:19:35 +01:00
|
|
|
printf("failed; %s\n", Curl_strerror(conn, SOCKERRNO));
|
2005-10-20 21:40:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define dump_addrinfo(x,y)
|
|
|
|
#endif
|
|
|
|
|
2004-04-26 09:20:11 +02:00
|
|
|
/*
|
2006-07-21 06:22:44 +02:00
|
|
|
* Curl_getaddrinfo() when built ipv6-enabled (non-threading and
|
|
|
|
* non-ares version).
|
2004-04-26 09:20:11 +02:00
|
|
|
*
|
|
|
|
* Returns name information about the given hostname and port number. If
|
|
|
|
* successful, the 'addrinfo' is returned and the forth argument will point to
|
|
|
|
* memory we need to free after use. That memory *MUST* be freed with
|
|
|
|
* Curl_freeaddrinfo(), nothing else.
|
|
|
|
*/
|
|
|
|
Curl_addrinfo *Curl_getaddrinfo(struct connectdata *conn,
|
2006-07-21 07:51:12 +02:00
|
|
|
const char *hostname,
|
2004-04-26 09:20:11 +02:00
|
|
|
int port,
|
|
|
|
int *waitp)
|
|
|
|
{
|
2008-10-30 14:45:25 +01:00
|
|
|
struct addrinfo hints;
|
|
|
|
Curl_addrinfo *res;
|
2004-04-26 09:20:11 +02:00
|
|
|
int error;
|
|
|
|
char sbuf[NI_MAXSERV];
|
2005-10-10 20:28:05 +02:00
|
|
|
char *sbufptr = NULL;
|
2004-08-19 08:44:19 +02:00
|
|
|
char addrbuf[128];
|
2004-04-26 09:20:11 +02:00
|
|
|
int pf;
|
|
|
|
struct SessionHandle *data = conn->data;
|
|
|
|
|
2010-01-26 00:50:13 +01:00
|
|
|
*waitp = 0; /* synchronous response only */
|
2004-04-26 09:20:11 +02:00
|
|
|
|
2008-07-27 04:20:34 +02:00
|
|
|
/*
|
|
|
|
* Check if a limited name resolve has been requested.
|
|
|
|
*/
|
2010-11-11 14:51:39 +01:00
|
|
|
switch(conn->ip_version) {
|
2008-07-27 04:20:34 +02:00
|
|
|
case CURL_IPRESOLVE_V4:
|
2004-04-26 09:20:11 +02:00
|
|
|
pf = PF_INET;
|
2008-07-27 04:20:34 +02:00
|
|
|
break;
|
|
|
|
case CURL_IPRESOLVE_V6:
|
|
|
|
pf = PF_INET6;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
pf = PF_UNSPEC;
|
|
|
|
break;
|
2004-04-26 09:20:11 +02:00
|
|
|
}
|
|
|
|
|
2011-03-23 11:10:55 +01:00
|
|
|
if((pf != PF_INET) && !Curl_ipv6works())
|
|
|
|
/* the stack seems to be a non-ipv6 one */
|
|
|
|
pf = PF_INET;
|
2004-08-19 08:44:19 +02:00
|
|
|
|
2005-11-08 15:45:58 +01:00
|
|
|
memset(&hints, 0, sizeof(hints));
|
|
|
|
hints.ai_family = pf;
|
|
|
|
hints.ai_socktype = conn->socktype;
|
|
|
|
|
2005-03-17 08:40:15 +01:00
|
|
|
if((1 == Curl_inet_pton(AF_INET, hostname, addrbuf)) ||
|
|
|
|
(1 == Curl_inet_pton(AF_INET6, hostname, addrbuf))) {
|
2004-08-19 08:44:19 +02:00
|
|
|
/* the given address is numerical only, prevent a reverse lookup */
|
2005-11-08 15:45:58 +01:00
|
|
|
hints.ai_flags = AI_NUMERICHOST;
|
2004-08-19 08:44:19 +02:00
|
|
|
}
|
2005-09-02 17:11:08 +02:00
|
|
|
|
2005-10-10 20:28:05 +02:00
|
|
|
if(port) {
|
|
|
|
snprintf(sbuf, sizeof(sbuf), "%d", port);
|
|
|
|
sbufptr=sbuf;
|
|
|
|
}
|
2008-10-30 14:45:25 +01:00
|
|
|
error = Curl_getaddrinfo_ex(hostname, sbufptr, &hints, &res);
|
2007-11-07 10:21:35 +01:00
|
|
|
if(error) {
|
2004-08-19 08:44:19 +02:00
|
|
|
infof(data, "getaddrinfo(3) failed for %s:%d\n", hostname, port);
|
2004-04-26 09:20:11 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2005-10-20 21:40:02 +02:00
|
|
|
dump_addrinfo(conn, res);
|
|
|
|
|
2004-04-26 09:20:11 +02:00
|
|
|
return res;
|
|
|
|
}
|
2010-01-26 00:50:13 +01:00
|
|
|
#endif /* CURLRES_SYNCH */
|
2010-01-22 07:36:52 +01:00
|
|
|
#endif /* CURLRES_IPV6 */
|
2004-04-26 09:20:11 +02:00
|
|
|
|