2008-10-30 09:45:25 -04:00
|
|
|
/***************************************************************************
|
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2018-02-07 16:33:57 -05:00
|
|
|
* Copyright (C) 1998 - 2018, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2008-10-30 09:45:25 -04:00
|
|
|
*
|
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
2016-02-02 18:19:02 -05:00
|
|
|
* are also available at https://curl.haxx.se/docs/copyright.html.
|
2008-10-30 09:45:25 -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"
|
2008-10-30 09:45:25 -04:00
|
|
|
|
|
|
|
#include <curl/curl.h>
|
|
|
|
|
|
|
|
#ifdef HAVE_NETINET_IN_H
|
|
|
|
# include <netinet/in.h>
|
|
|
|
#endif
|
2017-12-05 11:41:27 -05:00
|
|
|
#ifdef HAVE_NETINET_IN6_H
|
|
|
|
# include <netinet/in6.h>
|
|
|
|
#endif
|
2008-10-30 09:45:25 -04:00
|
|
|
#ifdef HAVE_NETDB_H
|
|
|
|
# include <netdb.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_ARPA_INET_H
|
|
|
|
# include <arpa/inet.h>
|
|
|
|
#endif
|
2014-11-27 17:59:25 -05:00
|
|
|
#ifdef HAVE_SYS_UN_H
|
|
|
|
# include <sys/un.h>
|
|
|
|
#endif
|
2008-10-30 09:45:25 -04:00
|
|
|
|
2009-12-30 12:59:56 -05:00
|
|
|
#ifdef __VMS
|
2008-10-30 09:45:25 -04:00
|
|
|
# include <in.h>
|
|
|
|
# include <inet.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(NETWARE) && defined(__NOVELL_LIBC__)
|
|
|
|
# undef in_addr_t
|
|
|
|
# define in_addr_t unsigned long
|
|
|
|
#endif
|
|
|
|
|
2018-02-07 16:33:57 -05:00
|
|
|
#if defined(WIN32) && defined(USE_UNIX_SOCKETS)
|
|
|
|
#include <afunix.h>
|
|
|
|
#endif
|
|
|
|
|
2017-01-08 17:51:08 -05:00
|
|
|
#include <stddef.h>
|
|
|
|
|
2008-10-30 09:45:25 -04:00
|
|
|
#include "curl_addrinfo.h"
|
2013-01-03 20:50:28 -05:00
|
|
|
#include "inet_pton.h"
|
|
|
|
#include "warnless.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"
|
2008-10-30 09:45:25 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Curl_freeaddrinfo()
|
|
|
|
*
|
|
|
|
* This is used to free a linked list of Curl_addrinfo structs along
|
|
|
|
* with all its associated allocated storage. This function should be
|
|
|
|
* called once for each successful call to Curl_getaddrinfo_ex() or to
|
|
|
|
* any function call which actually allocates a Curl_addrinfo struct.
|
|
|
|
*/
|
|
|
|
|
2009-04-23 07:09:20 -04:00
|
|
|
#if defined(__INTEL_COMPILER) && (__INTEL_COMPILER == 910) && \
|
2009-04-25 06:24:11 -04:00
|
|
|
defined(__OPTIMIZE__) && defined(__unix__) && defined(__i386__)
|
2009-04-23 07:09:20 -04:00
|
|
|
/* workaround icc 9.1 optimizer issue */
|
2009-04-24 06:38:12 -04:00
|
|
|
# define vqualifier volatile
|
2009-04-23 07:09:20 -04:00
|
|
|
#else
|
2009-04-24 06:38:12 -04:00
|
|
|
# define vqualifier
|
2009-04-23 07:09:20 -04:00
|
|
|
#endif
|
2008-10-30 09:45:25 -04:00
|
|
|
|
2009-04-24 06:38:12 -04:00
|
|
|
void
|
|
|
|
Curl_freeaddrinfo(Curl_addrinfo *cahead)
|
|
|
|
{
|
|
|
|
Curl_addrinfo *vqualifier canext;
|
|
|
|
Curl_addrinfo *ca;
|
|
|
|
|
2008-10-30 09:45:25 -04:00
|
|
|
for(ca = cahead; ca != NULL; ca = canext) {
|
2015-03-11 12:41:01 -04:00
|
|
|
free(ca->ai_addr);
|
|
|
|
free(ca->ai_canonname);
|
2008-10-30 09:45:25 -04:00
|
|
|
canext = ca->ai_next;
|
|
|
|
|
|
|
|
free(ca);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_GETADDRINFO
|
|
|
|
/*
|
|
|
|
* Curl_getaddrinfo_ex()
|
|
|
|
*
|
2008-11-01 10:51:37 -04:00
|
|
|
* This is a wrapper function around system's getaddrinfo(), with
|
2008-10-30 09:45:25 -04:00
|
|
|
* the only difference that instead of returning a linked list of
|
|
|
|
* addrinfo structs this one returns a linked list of Curl_addrinfo
|
|
|
|
* ones. The memory allocated by this function *MUST* be free'd with
|
|
|
|
* Curl_freeaddrinfo(). For each successful call to this function
|
|
|
|
* there must be an associated call later to Curl_freeaddrinfo().
|
|
|
|
*
|
|
|
|
* There should be no single call to system's getaddrinfo() in the
|
|
|
|
* whole library, any such call should be 'routed' through this one.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
|
|
|
Curl_getaddrinfo_ex(const char *nodename,
|
|
|
|
const char *servname,
|
|
|
|
const struct addrinfo *hints,
|
|
|
|
Curl_addrinfo **result)
|
|
|
|
{
|
|
|
|
const struct addrinfo *ai;
|
|
|
|
struct addrinfo *aihead;
|
|
|
|
Curl_addrinfo *cafirst = NULL;
|
|
|
|
Curl_addrinfo *calast = NULL;
|
|
|
|
Curl_addrinfo *ca;
|
2010-11-23 15:38:20 -05:00
|
|
|
size_t ss_size;
|
2008-10-30 09:45:25 -04:00
|
|
|
int error;
|
|
|
|
|
|
|
|
*result = NULL; /* assume failure */
|
|
|
|
|
|
|
|
error = getaddrinfo(nodename, servname, hints, &aihead);
|
|
|
|
if(error)
|
|
|
|
return error;
|
|
|
|
|
2010-11-23 15:38:20 -05:00
|
|
|
/* traverse the addrinfo list */
|
|
|
|
|
|
|
|
for(ai = aihead; ai != NULL; ai = ai->ai_next) {
|
|
|
|
|
|
|
|
/* ignore elements with unsupported address family, */
|
|
|
|
/* settle family-specific sockaddr structure size. */
|
|
|
|
if(ai->ai_family == AF_INET)
|
|
|
|
ss_size = sizeof(struct sockaddr_in);
|
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
else if(ai->ai_family == AF_INET6)
|
|
|
|
ss_size = sizeof(struct sockaddr_in6);
|
|
|
|
#endif
|
|
|
|
else
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* ignore elements without required address info */
|
|
|
|
if((ai->ai_addr == NULL) || !(ai->ai_addrlen > 0))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* ignore elements with bogus address size */
|
|
|
|
if((size_t)ai->ai_addrlen < ss_size)
|
|
|
|
continue;
|
2008-10-30 09:45:25 -04:00
|
|
|
|
2016-12-13 19:29:44 -05:00
|
|
|
ca = malloc(sizeof(Curl_addrinfo));
|
|
|
|
if(!ca) {
|
2008-10-30 09:45:25 -04:00
|
|
|
error = EAI_MEMORY;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* copy each structure member individually, member ordering, */
|
2010-11-23 15:38:20 -05:00
|
|
|
/* size, or padding might be different for each platform. */
|
2008-10-30 09:45:25 -04:00
|
|
|
|
|
|
|
ca->ai_flags = ai->ai_flags;
|
|
|
|
ca->ai_family = ai->ai_family;
|
|
|
|
ca->ai_socktype = ai->ai_socktype;
|
|
|
|
ca->ai_protocol = ai->ai_protocol;
|
2010-11-23 15:38:20 -05:00
|
|
|
ca->ai_addrlen = (curl_socklen_t)ss_size;
|
2008-10-30 09:45:25 -04:00
|
|
|
ca->ai_addr = NULL;
|
|
|
|
ca->ai_canonname = NULL;
|
|
|
|
ca->ai_next = NULL;
|
|
|
|
|
2016-12-13 19:29:44 -05:00
|
|
|
ca->ai_addr = malloc(ss_size);
|
|
|
|
if(!ca->ai_addr) {
|
2010-11-23 15:38:20 -05:00
|
|
|
error = EAI_MEMORY;
|
|
|
|
free(ca);
|
|
|
|
break;
|
2008-10-30 09:45:25 -04:00
|
|
|
}
|
2010-11-23 15:38:20 -05:00
|
|
|
memcpy(ca->ai_addr, ai->ai_addr, ss_size);
|
2008-10-30 09:45:25 -04:00
|
|
|
|
|
|
|
if(ai->ai_canonname != NULL) {
|
2016-12-13 19:29:44 -05:00
|
|
|
ca->ai_canonname = strdup(ai->ai_canonname);
|
|
|
|
if(!ca->ai_canonname) {
|
2008-10-30 09:45:25 -04:00
|
|
|
error = EAI_MEMORY;
|
2010-11-23 15:38:20 -05:00
|
|
|
free(ca->ai_addr);
|
2008-10-30 09:45:25 -04:00
|
|
|
free(ca);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if the return list is empty, this becomes the first element */
|
|
|
|
if(!cafirst)
|
|
|
|
cafirst = ca;
|
|
|
|
|
|
|
|
/* add this element last in the return list */
|
|
|
|
if(calast)
|
|
|
|
calast->ai_next = ca;
|
|
|
|
calast = ca;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* destroy the addrinfo list */
|
|
|
|
if(aihead)
|
|
|
|
freeaddrinfo(aihead);
|
|
|
|
|
|
|
|
/* if we failed, also destroy the Curl_addrinfo list */
|
|
|
|
if(error) {
|
|
|
|
Curl_freeaddrinfo(cafirst);
|
|
|
|
cafirst = NULL;
|
|
|
|
}
|
2010-11-23 22:36:57 -05:00
|
|
|
else if(!cafirst) {
|
|
|
|
#ifdef EAI_NONAME
|
|
|
|
/* rfc3493 conformant */
|
|
|
|
error = EAI_NONAME;
|
|
|
|
#else
|
|
|
|
/* rfc3493 obsoleted */
|
|
|
|
error = EAI_NODATA;
|
|
|
|
#endif
|
|
|
|
#ifdef USE_WINSOCK
|
|
|
|
SET_SOCKERRNO(error);
|
|
|
|
#endif
|
|
|
|
}
|
2008-10-30 09:45:25 -04:00
|
|
|
|
|
|
|
*result = cafirst;
|
|
|
|
|
2008-11-06 12:19:56 -05:00
|
|
|
/* This is not a CURLcode */
|
|
|
|
return error;
|
2008-10-30 09:45:25 -04:00
|
|
|
}
|
|
|
|
#endif /* HAVE_GETADDRINFO */
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Curl_he2ai()
|
|
|
|
*
|
|
|
|
* This function returns a pointer to the first element of a newly allocated
|
|
|
|
* Curl_addrinfo struct linked list filled with the data of a given hostent.
|
|
|
|
* Curl_addrinfo is meant to work like the addrinfo struct does for a IPv6
|
|
|
|
* stack, but usable also for IPv4, all hosts and environments.
|
|
|
|
*
|
|
|
|
* The memory allocated by this function *MUST* be free'd later on calling
|
|
|
|
* Curl_freeaddrinfo(). For each successful call to this function there
|
|
|
|
* must be an associated call later to Curl_freeaddrinfo().
|
|
|
|
*
|
|
|
|
* Curl_addrinfo defined in "lib/curl_addrinfo.h"
|
|
|
|
*
|
|
|
|
* struct Curl_addrinfo {
|
|
|
|
* int ai_flags;
|
|
|
|
* int ai_family;
|
|
|
|
* int ai_socktype;
|
|
|
|
* int ai_protocol;
|
2009-05-01 22:37:32 -04:00
|
|
|
* curl_socklen_t ai_addrlen; * Follow rfc3493 struct addrinfo *
|
2008-10-30 09:45:25 -04:00
|
|
|
* char *ai_canonname;
|
|
|
|
* struct sockaddr *ai_addr;
|
|
|
|
* struct Curl_addrinfo *ai_next;
|
|
|
|
* };
|
|
|
|
* typedef struct Curl_addrinfo Curl_addrinfo;
|
|
|
|
*
|
|
|
|
* hostent defined in <netdb.h>
|
|
|
|
*
|
|
|
|
* struct hostent {
|
|
|
|
* char *h_name;
|
|
|
|
* char **h_aliases;
|
|
|
|
* int h_addrtype;
|
|
|
|
* int h_length;
|
|
|
|
* char **h_addr_list;
|
|
|
|
* };
|
|
|
|
*
|
|
|
|
* for backward compatibility:
|
|
|
|
*
|
|
|
|
* #define h_addr h_addr_list[0]
|
|
|
|
*/
|
|
|
|
|
|
|
|
Curl_addrinfo *
|
|
|
|
Curl_he2ai(const struct hostent *he, int port)
|
|
|
|
{
|
|
|
|
Curl_addrinfo *ai;
|
|
|
|
Curl_addrinfo *prevai = NULL;
|
|
|
|
Curl_addrinfo *firstai = NULL;
|
|
|
|
struct sockaddr_in *addr;
|
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
struct sockaddr_in6 *addr6;
|
|
|
|
#endif
|
|
|
|
CURLcode result = CURLE_OK;
|
|
|
|
int i;
|
|
|
|
char *curr;
|
|
|
|
|
|
|
|
if(!he)
|
|
|
|
/* no input == no output! */
|
|
|
|
return NULL;
|
|
|
|
|
2008-11-06 12:19:56 -05:00
|
|
|
DEBUGASSERT((he->h_name != NULL) && (he->h_addr_list != NULL));
|
|
|
|
|
2017-09-09 17:09:06 -04:00
|
|
|
for(i = 0; (curr = he->h_addr_list[i]) != NULL; i++) {
|
2008-10-30 09:45:25 -04:00
|
|
|
|
2009-04-14 08:53:53 -04:00
|
|
|
size_t ss_size;
|
2008-10-30 09:45:25 -04:00
|
|
|
#ifdef ENABLE_IPV6
|
2011-04-20 09:17:42 -04:00
|
|
|
if(he->h_addrtype == AF_INET6)
|
2016-12-13 17:34:59 -05:00
|
|
|
ss_size = sizeof(struct sockaddr_in6);
|
2008-10-30 09:45:25 -04:00
|
|
|
else
|
|
|
|
#endif
|
2016-12-13 17:34:59 -05:00
|
|
|
ss_size = sizeof(struct sockaddr_in);
|
2008-10-30 09:45:25 -04:00
|
|
|
|
2016-12-13 19:29:44 -05:00
|
|
|
ai = calloc(1, sizeof(Curl_addrinfo));
|
|
|
|
if(!ai) {
|
2008-10-30 09:45:25 -04:00
|
|
|
result = CURLE_OUT_OF_MEMORY;
|
|
|
|
break;
|
|
|
|
}
|
2016-12-13 19:29:44 -05:00
|
|
|
ai->ai_canonname = strdup(he->h_name);
|
|
|
|
if(!ai->ai_canonname) {
|
2008-10-30 09:45:25 -04:00
|
|
|
result = CURLE_OUT_OF_MEMORY;
|
|
|
|
free(ai);
|
|
|
|
break;
|
|
|
|
}
|
2016-12-13 19:29:44 -05:00
|
|
|
ai->ai_addr = calloc(1, ss_size);
|
|
|
|
if(!ai->ai_addr) {
|
2008-10-30 09:45:25 -04:00
|
|
|
result = CURLE_OUT_OF_MEMORY;
|
|
|
|
free(ai->ai_canonname);
|
|
|
|
free(ai);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!firstai)
|
|
|
|
/* store the pointer we want to return from this function */
|
|
|
|
firstai = ai;
|
|
|
|
|
|
|
|
if(prevai)
|
|
|
|
/* make the previous entry point to this */
|
|
|
|
prevai->ai_next = ai;
|
|
|
|
|
|
|
|
ai->ai_family = he->h_addrtype;
|
|
|
|
|
|
|
|
/* we return all names as STREAM, so when using this address for TFTP
|
|
|
|
the type must be ignored and conn->socktype be used instead! */
|
|
|
|
ai->ai_socktype = SOCK_STREAM;
|
|
|
|
|
2009-05-10 06:24:53 -04:00
|
|
|
ai->ai_addrlen = (curl_socklen_t)ss_size;
|
2008-10-30 09:45:25 -04:00
|
|
|
|
|
|
|
/* leave the rest of the struct filled with zero */
|
|
|
|
|
2016-12-13 17:34:59 -05:00
|
|
|
switch(ai->ai_family) {
|
2008-10-30 09:45:25 -04:00
|
|
|
case AF_INET:
|
2008-11-06 12:19:56 -05:00
|
|
|
addr = (void *)ai->ai_addr; /* storage area for this info */
|
2008-10-30 09:45:25 -04:00
|
|
|
|
|
|
|
memcpy(&addr->sin_addr, curr, sizeof(struct in_addr));
|
2018-04-06 09:26:04 -04:00
|
|
|
addr->sin_family = (CURL_SA_FAMILY_T)(he->h_addrtype);
|
2008-10-30 09:45:25 -04:00
|
|
|
addr->sin_port = htons((unsigned short)port);
|
|
|
|
break;
|
|
|
|
|
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
case AF_INET6:
|
2008-11-06 12:19:56 -05:00
|
|
|
addr6 = (void *)ai->ai_addr; /* storage area for this info */
|
2008-10-30 09:45:25 -04:00
|
|
|
|
|
|
|
memcpy(&addr6->sin6_addr, curr, sizeof(struct in6_addr));
|
2018-04-06 09:26:04 -04:00
|
|
|
addr6->sin6_family = (CURL_SA_FAMILY_T)(he->h_addrtype);
|
2008-10-30 09:45:25 -04:00
|
|
|
addr6->sin6_port = htons((unsigned short)port);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
prevai = ai;
|
|
|
|
}
|
|
|
|
|
2014-10-23 16:56:35 -04:00
|
|
|
if(result) {
|
2008-10-30 09:45:25 -04:00
|
|
|
Curl_freeaddrinfo(firstai);
|
|
|
|
firstai = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return firstai;
|
|
|
|
}
|
|
|
|
|
2008-10-30 15:02:23 -04:00
|
|
|
|
2008-11-06 12:19:56 -05:00
|
|
|
struct namebuff {
|
|
|
|
struct hostent hostentry;
|
|
|
|
union {
|
|
|
|
struct in_addr ina4;
|
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
struct in6_addr ina6;
|
|
|
|
#endif
|
|
|
|
} addrentry;
|
|
|
|
char *h_addr_list[2];
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Curl_ip2addr()
|
|
|
|
*
|
|
|
|
* This function takes an internet address, in binary form, as input parameter
|
|
|
|
* along with its address family and the string version of the address, and it
|
|
|
|
* returns a Curl_addrinfo chain filled in correctly with information for the
|
|
|
|
* given address/host
|
|
|
|
*/
|
|
|
|
|
|
|
|
Curl_addrinfo *
|
|
|
|
Curl_ip2addr(int af, const void *inaddr, const char *hostname, int port)
|
|
|
|
{
|
|
|
|
Curl_addrinfo *ai;
|
|
|
|
|
2009-12-30 12:59:56 -05:00
|
|
|
#if defined(__VMS) && \
|
2008-11-06 12:19:56 -05:00
|
|
|
defined(__INITIAL_POINTER_SIZE) && (__INITIAL_POINTER_SIZE == 64)
|
|
|
|
#pragma pointer_size save
|
|
|
|
#pragma pointer_size short
|
|
|
|
#pragma message disable PTRMISMATCH
|
|
|
|
#endif
|
|
|
|
|
|
|
|
struct hostent *h;
|
|
|
|
struct namebuff *buf;
|
|
|
|
char *addrentry;
|
|
|
|
char *hoststr;
|
2009-04-13 03:18:39 -04:00
|
|
|
size_t addrsize;
|
2008-11-06 12:19:56 -05:00
|
|
|
|
|
|
|
DEBUGASSERT(inaddr && hostname);
|
|
|
|
|
|
|
|
buf = malloc(sizeof(struct namebuff));
|
|
|
|
if(!buf)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
hoststr = strdup(hostname);
|
|
|
|
if(!hoststr) {
|
|
|
|
free(buf);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(af) {
|
|
|
|
case AF_INET:
|
|
|
|
addrsize = sizeof(struct in_addr);
|
|
|
|
addrentry = (void *)&buf->addrentry.ina4;
|
|
|
|
memcpy(addrentry, inaddr, sizeof(struct in_addr));
|
|
|
|
break;
|
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
case AF_INET6:
|
|
|
|
addrsize = sizeof(struct in6_addr);
|
|
|
|
addrentry = (void *)&buf->addrentry.ina6;
|
|
|
|
memcpy(addrentry, inaddr, sizeof(struct in6_addr));
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
free(hoststr);
|
|
|
|
free(buf);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
h = &buf->hostentry;
|
|
|
|
h->h_name = hoststr;
|
|
|
|
h->h_aliases = NULL;
|
|
|
|
h->h_addrtype = (short)af;
|
|
|
|
h->h_length = (short)addrsize;
|
|
|
|
h->h_addr_list = &buf->h_addr_list[0];
|
|
|
|
h->h_addr_list[0] = addrentry;
|
|
|
|
h->h_addr_list[1] = NULL; /* terminate list of entries */
|
|
|
|
|
2009-12-30 12:59:56 -05:00
|
|
|
#if defined(__VMS) && \
|
2008-11-06 12:19:56 -05:00
|
|
|
defined(__INITIAL_POINTER_SIZE) && (__INITIAL_POINTER_SIZE == 64)
|
|
|
|
#pragma pointer_size restore
|
|
|
|
#pragma message enable PTRMISMATCH
|
|
|
|
#endif
|
|
|
|
|
|
|
|
ai = Curl_he2ai(h, port);
|
|
|
|
|
|
|
|
free(hoststr);
|
|
|
|
free(buf);
|
|
|
|
|
|
|
|
return ai;
|
|
|
|
}
|
|
|
|
|
2010-11-05 17:31:40 -04:00
|
|
|
/*
|
|
|
|
* Given an IPv4 or IPv6 dotted string address, this converts it to a proper
|
|
|
|
* allocated Curl_addrinfo struct and returns it.
|
|
|
|
*/
|
|
|
|
Curl_addrinfo *Curl_str2addr(char *address, int port)
|
|
|
|
{
|
|
|
|
struct in_addr in;
|
|
|
|
if(Curl_inet_pton(AF_INET, address, &in) > 0)
|
|
|
|
/* This is a dotted IP address 123.123.123.123-style */
|
|
|
|
return Curl_ip2addr(AF_INET, &in, address, port);
|
|
|
|
#ifdef ENABLE_IPV6
|
2017-03-10 08:28:37 -05:00
|
|
|
{
|
2010-11-05 17:31:40 -04:00
|
|
|
struct in6_addr in6;
|
|
|
|
if(Curl_inet_pton(AF_INET6, address, &in6) > 0)
|
|
|
|
/* This is a dotted IPv6 address ::1-style */
|
|
|
|
return Curl_ip2addr(AF_INET6, &in6, address, port);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return NULL; /* bad input format */
|
|
|
|
}
|
2008-11-06 12:19:56 -05:00
|
|
|
|
2014-11-27 17:59:25 -05:00
|
|
|
#ifdef USE_UNIX_SOCKETS
|
|
|
|
/**
|
2014-12-26 15:45:21 -05:00
|
|
|
* Given a path to a Unix domain socket, return a newly allocated Curl_addrinfo
|
2014-11-27 17:59:25 -05:00
|
|
|
* struct initialized with this path.
|
2016-12-13 19:29:44 -05:00
|
|
|
* Set '*longpath' to TRUE if the error is a too long path.
|
2014-11-27 17:59:25 -05:00
|
|
|
*/
|
2017-01-08 17:51:08 -05:00
|
|
|
Curl_addrinfo *Curl_unix2addr(const char *path, bool *longpath, bool abstract)
|
2014-11-27 17:59:25 -05:00
|
|
|
{
|
|
|
|
Curl_addrinfo *ai;
|
2014-12-10 07:12:26 -05:00
|
|
|
struct sockaddr_un *sa_un;
|
2014-11-27 17:59:25 -05:00
|
|
|
size_t path_len;
|
|
|
|
|
2017-01-08 17:51:08 -05:00
|
|
|
*longpath = FALSE;
|
|
|
|
|
2014-11-27 17:59:25 -05:00
|
|
|
ai = calloc(1, sizeof(Curl_addrinfo));
|
|
|
|
if(!ai)
|
|
|
|
return NULL;
|
2016-12-13 19:29:44 -05:00
|
|
|
ai->ai_addr = calloc(1, sizeof(struct sockaddr_un));
|
|
|
|
if(!ai->ai_addr) {
|
2014-11-27 17:59:25 -05:00
|
|
|
free(ai);
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-01-08 17:51:08 -05:00
|
|
|
|
|
|
|
sa_un = (void *) ai->ai_addr;
|
|
|
|
sa_un->sun_family = AF_UNIX;
|
|
|
|
|
2014-11-27 17:59:25 -05:00
|
|
|
/* sun_path must be able to store the NUL-terminated path */
|
2017-01-08 17:51:08 -05:00
|
|
|
path_len = strlen(path) + 1;
|
|
|
|
if(path_len > sizeof(sa_un->sun_path)) {
|
2014-11-27 17:59:25 -05:00
|
|
|
free(ai->ai_addr);
|
|
|
|
free(ai);
|
2016-12-13 19:29:44 -05:00
|
|
|
*longpath = TRUE;
|
2014-11-27 17:59:25 -05:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ai->ai_family = AF_UNIX;
|
|
|
|
ai->ai_socktype = SOCK_STREAM; /* assume reliable transport for HTTP */
|
2017-01-13 17:58:22 -05:00
|
|
|
ai->ai_addrlen = (curl_socklen_t)
|
|
|
|
((offsetof(struct sockaddr_un, sun_path) + path_len) & 0x7FFFFFFF);
|
2017-01-08 17:51:08 -05:00
|
|
|
|
|
|
|
/* Abstract Unix domain socket have NULL prefix instead of suffix */
|
|
|
|
if(abstract)
|
|
|
|
memcpy(sa_un->sun_path + 1, path, path_len - 1);
|
|
|
|
else
|
|
|
|
memcpy(sa_un->sun_path, path, path_len); /* copy NUL byte */
|
|
|
|
|
2014-11-27 17:59:25 -05:00
|
|
|
return ai;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-06-01 08:40:35 -04:00
|
|
|
#if defined(CURLDEBUG) && defined(HAVE_GETADDRINFO) && \
|
|
|
|
defined(HAVE_FREEADDRINFO)
|
2008-10-30 15:02:23 -04:00
|
|
|
/*
|
|
|
|
* curl_dofreeaddrinfo()
|
|
|
|
*
|
2008-11-01 10:51:37 -04:00
|
|
|
* This is strictly for memory tracing and are using the same style as the
|
2013-01-03 20:50:28 -05:00
|
|
|
* family otherwise present in memdebug.c. I put these ones here since they
|
|
|
|
* require a bunch of structs I didn't want to include in memdebug.c
|
2008-10-30 15:02:23 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
|
|
|
curl_dofreeaddrinfo(struct addrinfo *freethis,
|
|
|
|
int line, const char *source)
|
|
|
|
{
|
2015-12-07 14:27:29 -05:00
|
|
|
#ifdef USE_LWIPSOCK
|
|
|
|
lwip_freeaddrinfo(freethis);
|
|
|
|
#else
|
2008-10-30 15:02:23 -04:00
|
|
|
(freeaddrinfo)(freethis);
|
2015-12-07 14:27:29 -05:00
|
|
|
#endif
|
2010-01-18 15:22:04 -05:00
|
|
|
curl_memlog("ADDR %s:%d freeaddrinfo(%p)\n",
|
|
|
|
source, line, (void *)freethis);
|
2008-10-30 15:02:23 -04:00
|
|
|
}
|
|
|
|
#endif /* defined(CURLDEBUG) && defined(HAVE_FREEADDRINFO) */
|
|
|
|
|
|
|
|
|
|
|
|
#if defined(CURLDEBUG) && defined(HAVE_GETADDRINFO)
|
|
|
|
/*
|
|
|
|
* curl_dogetaddrinfo()
|
|
|
|
*
|
2008-11-01 10:51:37 -04:00
|
|
|
* This is strictly for memory tracing and are using the same style as the
|
2013-01-03 20:50:28 -05:00
|
|
|
* family otherwise present in memdebug.c. I put these ones here since they
|
|
|
|
* require a bunch of structs I didn't want to include in memdebug.c
|
2008-10-30 15:02:23 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
|
|
|
curl_dogetaddrinfo(const char *hostname,
|
|
|
|
const char *service,
|
|
|
|
const struct addrinfo *hints,
|
|
|
|
struct addrinfo **result,
|
|
|
|
int line, const char *source)
|
|
|
|
{
|
2015-12-07 14:27:29 -05:00
|
|
|
#ifdef USE_LWIPSOCK
|
2017-09-09 17:09:06 -04:00
|
|
|
int res = lwip_getaddrinfo(hostname, service, hints, result);
|
2015-12-07 14:27:29 -05:00
|
|
|
#else
|
2017-09-09 17:09:06 -04:00
|
|
|
int res = (getaddrinfo)(hostname, service, hints, result);
|
2015-12-07 14:27:29 -05:00
|
|
|
#endif
|
2010-01-18 15:22:04 -05:00
|
|
|
if(0 == res)
|
2008-10-30 15:02:23 -04:00
|
|
|
/* success */
|
2010-01-18 15:22:04 -05:00
|
|
|
curl_memlog("ADDR %s:%d getaddrinfo() = %p\n",
|
|
|
|
source, line, (void *)*result);
|
|
|
|
else
|
|
|
|
curl_memlog("ADDR %s:%d getaddrinfo() failed\n",
|
|
|
|
source, line);
|
2008-10-30 15:02:23 -04:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
#endif /* defined(CURLDEBUG) && defined(HAVE_GETADDRINFO) */
|
|
|
|
|
2016-06-07 06:11:37 -04:00
|
|
|
#if defined(HAVE_GETADDRINFO) && defined(USE_RESOLVE_ON_IPS)
|
|
|
|
/*
|
|
|
|
* Work-arounds the sin6_port is always zero bug on iOS 9.3.2 and Mac OS X
|
|
|
|
* 10.11.5.
|
|
|
|
*/
|
|
|
|
void Curl_addrinfo_set_port(Curl_addrinfo *addrinfo, int port)
|
|
|
|
{
|
|
|
|
Curl_addrinfo *ca;
|
|
|
|
struct sockaddr_in *addr;
|
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
struct sockaddr_in6 *addr6;
|
|
|
|
#endif
|
|
|
|
for(ca = addrinfo; ca != NULL; ca = ca->ai_next) {
|
2016-12-13 17:34:59 -05:00
|
|
|
switch(ca->ai_family) {
|
2016-06-07 06:11:37 -04:00
|
|
|
case AF_INET:
|
|
|
|
addr = (void *)ca->ai_addr; /* storage area for this info */
|
|
|
|
addr->sin_port = htons((unsigned short)port);
|
|
|
|
break;
|
|
|
|
|
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
case AF_INET6:
|
|
|
|
addr6 = (void *)ca->ai_addr; /* storage area for this info */
|
|
|
|
addr6->sin6_port = htons((unsigned short)port);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|