2004-04-26 03:20:11 -04:00
|
|
|
/***************************************************************************
|
2004-08-19 02:44:19 -04:00
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
2004-04-26 03:20:11 -04:00
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2021-01-08 11:58:15 -05:00
|
|
|
* Copyright (C) 1998 - 2021, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2004-04-26 03:20:11 -04:00
|
|
|
*
|
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
2020-11-04 08:02:01 -05:00
|
|
|
* are also available at https://curl.se/docs/copyright.html.
|
2004-08-19 02:44:19 -04:00
|
|
|
*
|
2004-04-26 03:20:11 -04: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.
|
|
|
|
*
|
|
|
|
***************************************************************************/
|
|
|
|
|
2013-01-06 13:06:49 -05:00
|
|
|
#include "curl_setup.h"
|
2004-04-26 03:20:11 -04:00
|
|
|
|
2017-10-26 14:52:22 -04:00
|
|
|
/***********************************************************************
|
|
|
|
* Only for IPv6-enabled builds
|
|
|
|
**********************************************************************/
|
|
|
|
#ifdef CURLRES_IPV6
|
|
|
|
|
2004-04-26 03:20:11 -04:00
|
|
|
#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
|
2009-12-30 12:59:56 -05:00
|
|
|
#ifdef __VMS
|
2004-04-26 03:20:11 -04:00
|
|
|
#include <in.h>
|
|
|
|
#include <inet.h>
|
|
|
|
#endif
|
|
|
|
|
2006-04-26 13:23:28 -04:00
|
|
|
#ifdef HAVE_PROCESS_H
|
2004-04-26 03:20:11 -04:00
|
|
|
#include <process.h>
|
|
|
|
#endif
|
|
|
|
|
2013-01-03 20:50:28 -05:00
|
|
|
#include "urldata.h"
|
|
|
|
#include "sendf.h"
|
|
|
|
#include "hostip.h"
|
|
|
|
#include "hash.h"
|
|
|
|
#include "share.h"
|
|
|
|
#include "strerror.h"
|
|
|
|
#include "url.h"
|
|
|
|
#include "inet_pton.h"
|
|
|
|
#include "connect.h"
|
2016-04-29 09:46:40 -04:00
|
|
|
/* The last 3 #include files should be in this order */
|
2015-03-03 06:36:18 -05:00
|
|
|
#include "curl_printf.h"
|
2009-04-21 07:46:16 -04:00
|
|
|
#include "curl_memory.h"
|
2013-01-03 20:50:28 -05:00
|
|
|
#include "memdebug.h"
|
2004-04-26 03:20:11 -04:00
|
|
|
|
|
|
|
/*
|
2014-12-27 06:09:01 -05:00
|
|
|
* Curl_ipv6works() returns TRUE if IPv6 seems to work.
|
2004-04-26 03:20:11 -04:00
|
|
|
*/
|
2021-01-08 11:58:15 -05:00
|
|
|
bool Curl_ipv6works(struct Curl_easy *data)
|
2004-04-26 03:20:11 -04:00
|
|
|
{
|
2021-01-08 11:58:15 -05:00
|
|
|
if(data) {
|
2020-01-26 11:51:01 -05: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 */
|
2021-01-08 11:58:15 -05:00
|
|
|
DEBUGASSERT(data);
|
|
|
|
DEBUGASSERT(data->multi);
|
|
|
|
return data->multi->ipv6_works;
|
2020-01-26 11:51:01 -05:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
int ipv6_works = -1;
|
2011-03-23 06:10:55 -04:00
|
|
|
/* probe to see if we have a working IPv6 stack */
|
2004-04-26 03:20:11 -04:00
|
|
|
curl_socket_t s = socket(PF_INET6, SOCK_DGRAM, 0);
|
2007-11-07 04:21:35 -05:00
|
|
|
if(s == CURL_SOCKET_BAD)
|
2014-12-27 06:09:01 -05:00
|
|
|
/* an IPv6 address was requested but we can't get/use one */
|
2011-03-23 06:10:55 -04:00
|
|
|
ipv6_works = 0;
|
|
|
|
else {
|
|
|
|
ipv6_works = 1;
|
2021-01-08 11:58:15 -05:00
|
|
|
sclose(s);
|
2011-03-23 06:10:55 -04:00
|
|
|
}
|
2020-01-26 11:51:01 -05:00
|
|
|
return (ipv6_works>0)?TRUE:FALSE;
|
2004-04-26 03:20:11 -04:00
|
|
|
}
|
2011-03-23 06:10:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Curl_ipvalid() checks what CURL_IPRESOLVE_* requirements that might've
|
|
|
|
* been set and returns TRUE if they are OK.
|
|
|
|
*/
|
2021-01-08 11:58:15 -05:00
|
|
|
bool Curl_ipvalid(struct Curl_easy *data, struct connectdata *conn)
|
2011-03-23 06:10:55 -04:00
|
|
|
{
|
|
|
|
if(conn->ip_version == CURL_IPRESOLVE_V6)
|
2021-01-08 11:58:15 -05:00
|
|
|
return Curl_ipv6works(data);
|
2016-03-22 02:16:06 -04:00
|
|
|
|
2004-04-26 03:20:11 -04:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2010-01-25 18:50:13 -05:00
|
|
|
#if defined(CURLRES_SYNCH)
|
2005-10-20 15:40:02 -04:00
|
|
|
|
|
|
|
#ifdef DEBUG_ADDRINFO
|
2020-05-13 18:05:04 -04:00
|
|
|
static void dump_addrinfo(struct connectdata *conn,
|
|
|
|
const struct Curl_addrinfo *ai)
|
2005-10-20 15:40:02 -04:00
|
|
|
{
|
|
|
|
printf("dump_addrinfo:\n");
|
2011-04-20 09:17:42 -04:00
|
|
|
for(; ai; ai = ai->ai_next) {
|
2019-02-25 12:12:51 -05:00
|
|
|
char buf[INET6_ADDRSTRLEN];
|
2005-10-20 15:40:02 -04:00
|
|
|
printf(" fam %2d, CNAME %s, ",
|
|
|
|
ai->ai_family, ai->ai_canonname ? ai->ai_canonname : "<none>");
|
2020-05-18 12:41:20 -04:00
|
|
|
Curl_printable_address(ai, buf, sizeof(buf));
|
|
|
|
printf("%s\n", buf);
|
2005-10-20 15:40:02 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
2011-09-03 10:06:10 -04:00
|
|
|
#define dump_addrinfo(x,y) Curl_nop_stmt
|
2005-10-20 15:40:02 -04:00
|
|
|
#endif
|
|
|
|
|
2004-04-26 03:20:11 -04:00
|
|
|
/*
|
2014-12-27 06:09:01 -05:00
|
|
|
* Curl_getaddrinfo() when built IPv6-enabled (non-threading and
|
2006-07-21 00:22:44 -04:00
|
|
|
* non-ares version).
|
2004-04-26 03:20:11 -04: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.
|
|
|
|
*/
|
2020-05-13 18:05:04 -04:00
|
|
|
struct Curl_addrinfo *Curl_getaddrinfo(struct connectdata *conn,
|
|
|
|
const char *hostname,
|
|
|
|
int port,
|
|
|
|
int *waitp)
|
2004-04-26 03:20:11 -04:00
|
|
|
{
|
2008-10-30 09:45:25 -04:00
|
|
|
struct addrinfo hints;
|
2020-05-13 18:05:04 -04:00
|
|
|
struct Curl_addrinfo *res;
|
2004-04-26 03:20:11 -04:00
|
|
|
int error;
|
2013-09-10 17:18:43 -04:00
|
|
|
char sbuf[12];
|
2005-10-10 14:28:05 -04:00
|
|
|
char *sbufptr = NULL;
|
2016-06-07 06:11:37 -04:00
|
|
|
#ifndef USE_RESOLVE_ON_IPS
|
2004-08-19 02:44:19 -04:00
|
|
|
char addrbuf[128];
|
2016-06-07 06:11:37 -04:00
|
|
|
#endif
|
2004-04-26 03:20:11 -04:00
|
|
|
int pf;
|
2016-03-22 02:16:06 -04:00
|
|
|
#if !defined(CURL_DISABLE_VERBOSE_STRINGS)
|
2016-06-21 09:47:12 -04:00
|
|
|
struct Curl_easy *data = conn->data;
|
2016-03-22 02:16:06 -04:00
|
|
|
#endif
|
2004-04-26 03:20:11 -04:00
|
|
|
|
2010-01-25 18:50:13 -05:00
|
|
|
*waitp = 0; /* synchronous response only */
|
2004-04-26 03:20:11 -04:00
|
|
|
|
2016-03-22 02:16:06 -04:00
|
|
|
/* Check if a limited name resolve has been requested */
|
2010-11-11 08:51:39 -05:00
|
|
|
switch(conn->ip_version) {
|
2008-07-26 22:20:34 -04:00
|
|
|
case CURL_IPRESOLVE_V4:
|
2004-04-26 03:20:11 -04:00
|
|
|
pf = PF_INET;
|
2008-07-26 22:20:34 -04:00
|
|
|
break;
|
|
|
|
case CURL_IPRESOLVE_V6:
|
|
|
|
pf = PF_INET6;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
pf = PF_UNSPEC;
|
|
|
|
break;
|
2004-04-26 03:20:11 -04:00
|
|
|
}
|
|
|
|
|
2021-01-08 11:58:15 -05:00
|
|
|
if((pf != PF_INET) && !Curl_ipv6works(data))
|
2014-12-27 06:09:01 -05:00
|
|
|
/* The stack seems to be a non-IPv6 one */
|
2011-03-23 06:10:55 -04:00
|
|
|
pf = PF_INET;
|
2004-08-19 02:44:19 -04:00
|
|
|
|
2005-11-08 09:45:58 -05:00
|
|
|
memset(&hints, 0, sizeof(hints));
|
|
|
|
hints.ai_family = pf;
|
2019-07-21 17:48:58 -04:00
|
|
|
hints.ai_socktype = (conn->transport == TRNSPRT_TCP) ?
|
|
|
|
SOCK_STREAM : SOCK_DGRAM;
|
2005-11-08 09:45:58 -05:00
|
|
|
|
2016-06-07 06:11:37 -04:00
|
|
|
#ifndef USE_RESOLVE_ON_IPS
|
|
|
|
/*
|
|
|
|
* The AI_NUMERICHOST must not be set to get synthesized IPv6 address from
|
|
|
|
* an IPv4 address on iOS and Mac OS X.
|
|
|
|
*/
|
2005-03-17 02:40:15 -05:00
|
|
|
if((1 == Curl_inet_pton(AF_INET, hostname, addrbuf)) ||
|
|
|
|
(1 == Curl_inet_pton(AF_INET6, hostname, addrbuf))) {
|
2004-08-19 02:44:19 -04:00
|
|
|
/* the given address is numerical only, prevent a reverse lookup */
|
2005-11-08 09:45:58 -05:00
|
|
|
hints.ai_flags = AI_NUMERICHOST;
|
2004-08-19 02:44:19 -04:00
|
|
|
}
|
2016-06-07 06:11:37 -04:00
|
|
|
#endif
|
2005-09-02 11:11:08 -04:00
|
|
|
|
2005-10-10 14:28:05 -04:00
|
|
|
if(port) {
|
2018-11-22 03:01:24 -05:00
|
|
|
msnprintf(sbuf, sizeof(sbuf), "%d", port);
|
2017-09-09 17:09:06 -04:00
|
|
|
sbufptr = sbuf;
|
2005-10-10 14:28:05 -04:00
|
|
|
}
|
2016-03-22 02:16:06 -04:00
|
|
|
|
2008-10-30 09:45:25 -04:00
|
|
|
error = Curl_getaddrinfo_ex(hostname, sbufptr, &hints, &res);
|
2007-11-07 04:21:35 -05:00
|
|
|
if(error) {
|
2004-08-19 02:44:19 -04:00
|
|
|
infof(data, "getaddrinfo(3) failed for %s:%d\n", hostname, port);
|
2004-04-26 03:20:11 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-06-07 06:11:37 -04:00
|
|
|
if(port) {
|
|
|
|
Curl_addrinfo_set_port(res, port);
|
|
|
|
}
|
|
|
|
|
2005-10-20 15:40:02 -04:00
|
|
|
dump_addrinfo(conn, res);
|
|
|
|
|
2004-04-26 03:20:11 -04:00
|
|
|
return res;
|
|
|
|
}
|
2010-01-25 18:50:13 -05:00
|
|
|
#endif /* CURLRES_SYNCH */
|
2004-04-26 03:20:11 -04:00
|
|
|
|
2016-03-22 02:16:06 -04:00
|
|
|
#endif /* CURLRES_IPV6 */
|