161 lines
4.4 KiB
C
Raw Normal View History

/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
2021-09-14 00:13:48 +02:00
* Copyright (C) 1998 - 2021, Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
2021-09-14 00:13:48 +02:00
* are also available at https://curl.se/docs/copyright.html.
*
* 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.
*
***************************************************************************/
2015-04-27 22:25:09 +02:00
#include "curl_setup.h"
2018-04-23 21:13:27 +02:00
/***********************************************************************
* Only for IPv6-enabled builds
**********************************************************************/
#ifdef CURLRES_IPV6
#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
2015-04-27 22:25:09 +02:00
#ifdef __VMS
#include <in.h>
#include <inet.h>
#endif
#ifdef HAVE_PROCESS_H
#include <process.h>
#endif
#include "urldata.h"
#include "sendf.h"
#include "hostip.h"
#include "hash.h"
#include "share.h"
#include "url.h"
#include "inet_pton.h"
#include "connect.h"
2016-09-11 17:22:32 +02:00
/* The last 3 #include files should be in this order */
2015-11-17 17:22:37 +01:00
#include "curl_printf.h"
2015-04-27 22:25:09 +02:00
#include "curl_memory.h"
#include "memdebug.h"
/*
* Curl_ipvalid() checks what CURL_IPRESOLVE_* requirements that might've
* been set and returns TRUE if they are OK.
*/
2021-09-14 00:13:48 +02:00
bool Curl_ipvalid(struct Curl_easy *data, struct connectdata *conn)
{
2015-04-27 22:25:09 +02:00
if(conn->ip_version == CURL_IPRESOLVE_V6)
2021-09-14 00:13:48 +02:00
return Curl_ipv6works(data);
2016-09-11 17:22:32 +02:00
return TRUE;
}
2015-04-27 22:25:09 +02:00
#if defined(CURLRES_SYNCH)
#ifdef DEBUG_ADDRINFO
2020-08-30 11:54:41 +02:00
static void dump_addrinfo(struct connectdata *conn,
const struct Curl_addrinfo *ai)
{
printf("dump_addrinfo:\n");
2015-04-27 22:25:09 +02:00
for(; ai; ai = ai->ai_next) {
2019-11-11 23:01:05 +01:00
char buf[INET6_ADDRSTRLEN];
printf(" fam %2d, CNAME %s, ",
ai->ai_family, ai->ai_canonname ? ai->ai_canonname : "<none>");
2020-08-30 11:54:41 +02:00
Curl_printable_address(ai, buf, sizeof(buf));
printf("%s\n", buf);
}
}
#else
2015-04-27 22:25:09 +02:00
#define dump_addrinfo(x,y) Curl_nop_stmt
#endif
/*
2015-11-17 17:22:37 +01:00
* Curl_getaddrinfo() when built IPv6-enabled (non-threading and
* non-ares version).
*
* 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.
*/
2021-09-14 00:13:48 +02:00
struct Curl_addrinfo *Curl_getaddrinfo(struct Curl_easy *data,
2020-08-30 11:54:41 +02:00
const char *hostname,
int port,
int *waitp)
{
2015-04-27 22:25:09 +02:00
struct addrinfo hints;
2020-08-30 11:54:41 +02:00
struct Curl_addrinfo *res;
int error;
2015-04-27 22:25:09 +02:00
char sbuf[12];
char *sbufptr = NULL;
2016-09-11 17:22:32 +02:00
#ifndef USE_RESOLVE_ON_IPS
char addrbuf[128];
2016-09-11 17:22:32 +02:00
#endif
2021-09-14 00:13:48 +02:00
int pf = PF_INET;
2015-04-27 22:25:09 +02:00
*waitp = 0; /* synchronous response only */
2021-09-14 00:13:48 +02:00
if(Curl_ipv6works(data))
/* The stack seems to be IPv6-enabled */
2015-04-27 22:25:09 +02:00
pf = PF_UNSPEC;
memset(&hints, 0, sizeof(hints));
hints.ai_family = pf;
2021-09-14 00:13:48 +02:00
hints.ai_socktype = (data->conn->transport == TRNSPRT_TCP) ?
2020-08-30 11:54:41 +02:00
SOCK_STREAM : SOCK_DGRAM;
2016-09-11 17:22:32 +02: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.
*/
if((1 == Curl_inet_pton(AF_INET, hostname, addrbuf)) ||
(1 == Curl_inet_pton(AF_INET6, hostname, addrbuf))) {
/* the given address is numerical only, prevent a reverse lookup */
hints.ai_flags = AI_NUMERICHOST;
}
2016-09-11 17:22:32 +02:00
#endif
if(port) {
2019-11-11 23:01:05 +01:00
msnprintf(sbuf, sizeof(sbuf), "%d", port);
2018-01-26 17:06:56 +01:00
sbufptr = sbuf;
}
2016-09-11 17:22:32 +02:00
2015-04-27 22:25:09 +02:00
error = Curl_getaddrinfo_ex(hostname, sbufptr, &hints, &res);
if(error) {
2021-11-20 13:41:27 +01:00
infof(data, "getaddrinfo(3) failed for %s:%d", hostname, port);
return NULL;
}
2016-09-11 17:22:32 +02:00
if(port) {
Curl_addrinfo_set_port(res, port);
}
dump_addrinfo(conn, res);
return res;
}
2015-04-27 22:25:09 +02:00
#endif /* CURLRES_SYNCH */
2016-09-11 17:22:32 +02:00
#endif /* CURLRES_IPV6 */