Newer
Older
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* Copyright (C) 1998 - 2008, 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
* are also available at http://curl.haxx.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.
***************************************************************************/
#ifdef NEED_MALLOC_H
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
#ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h>
#endif
#include <stdlib.h> /* required for free() prototypes */
Daniel Stenberg
committed
#ifdef HAVE_UNISTD_H
#include <unistd.h> /* for the close() proto */
#endif
#include <inet.h>
#include <stdlib.h>
#endif
Daniel Stenberg
committed
#ifdef HAVE_SETJMP_H
#include <setjmp.h>
#endif
#ifdef HAVE_SIGNAL_H
#include <signal.h>
#endif
Daniel Stenberg
committed
#include <process.h>
#endif
#include "hash.h"
#include "strerror.h"
Daniel Stenberg
committed
#include "url.h"
Daniel Stenberg
committed
#include "inet_ntop.h"
#define _MPRINTF_REPLACE /* use our functions only */
#include <curl/mprintf.h>
#include "memory.h"
/* The last #include file should be: */
#include "memdebug.h"
#if defined(HAVE_ALARM) && defined(SIGALRM) && defined(HAVE_SIGSETJMP) \
&& !defined(USE_ARES)
/* alarm-based timeouts can only be used with all the dependencies satisfied */
#define USE_ALARM_TIMEOUT
#endif
/*
* hostip.c explained
* ==================
*
* The main COMPILE-TIME DEFINES to keep in mind when reading the host*.c
* source file are these:
*
* CURLRES_IPV6 - this host has getaddrinfo() and family, and thus we use
* that. The host may not be able to resolve IPv6, but we don't really have to
* take that into account. Hosts that aren't IPv6-enabled have CURLRES_IPV4
* defined.
*
* CURLRES_ARES - is defined if libcurl is built to use c-ares for
* asynchronous name resolves. This can be Windows or *nix.
*
* CURLRES_THREADED - is defined if libcurl is built to run under (native)
* Windows, and then the name resolve will be done in a new thread, and the
* supported API will be the same as for ares-builds.
*
* If any of the two previous are defined, CURLRES_ASYNCH is defined too. If
* libcurl is not built to use an asynchronous resolver, CURLRES_SYNCH is
* defined.
*
* The host*.c sources files are split up like this:
*
* hostip.c - method-independent resolver functions and utility functions
* hostasyn.c - functions for asynchronous name resolves
* hostsyn.c - functions for synchronous name resolves
* hostares.c - functions for ares-using name resolves
* hostthre.c - functions for threaded name resolves
* hostip4.c - ipv4-specific functions
* hostip6.c - ipv6-specific functions
*
* The hostip.h is the united header file for all this. It defines the
* CURLRES_* defines based on the config*.h and setup.h defines.
*/
/* These two symbols are for the global DNS cache */
Daniel Stenberg
committed
static struct curl_hash hostname_cache;
static int host_cache_initialized;
static void freednsentry(void *freethis);
/*
* Curl_global_host_cache_init() initializes and sets up a global DNS cache.
* Global DNS cache is general badness. Do not use. This will be removed in
* a future version. Use the share interface instead!
*
Daniel Stenberg
committed
* Returns a struct curl_hash pointer on success, NULL on failure.
*/
Daniel Stenberg
committed
struct curl_hash *Curl_global_host_cache_init(void)
int rc = 0;
rc = Curl_hash_init(&hostname_cache, 7, Curl_hash_str,
Curl_str_key_compare, freednsentry);
if(!rc)
host_cache_initialized = 1;
Daniel Stenberg
committed
return rc?NULL:&hostname_cache;
/*
* Destroy and cleanup the global DNS cache
*/
void Curl_global_host_cache_dtor(void)
{
Daniel Stenberg
committed
Curl_hash_clean(&hostname_cache);
host_cache_initialized = 0;
}
}
Daniel Stenberg
committed
/*
* Return # of adresses in a Curl_addrinfo struct
*/
int Curl_num_addresses(const Curl_addrinfo *addr)
{
int i = 0;
while(addr) {
addr = addr->ai_next;
i++;
}
Daniel Stenberg
committed
return i;
Daniel Stenberg
committed
}
/*
* Curl_printable_address() returns a printable version of the 1st address
* given in the 'ip' argument. The result will be stored in the buf that is
* bufsize bytes big.
Daniel Stenberg
committed
*
* If the conversion fails, it returns NULL.
*/
Daniel Stenberg
committed
const char *Curl_printable_address(const Curl_addrinfo *ip,
Daniel Stenberg
committed
char *buf, size_t bufsize)
{
const void *ip4 = &((const struct sockaddr_in*)ip->ai_addr)->sin_addr;
int af = ip->ai_family;
Daniel Stenberg
committed
#ifdef CURLRES_IPV6
const void *ip6 = &((const struct sockaddr_in6*)ip->ai_addr)->sin6_addr;
Daniel Stenberg
committed
#else
Daniel Stenberg
committed
const void *ip6 = NULL;
Daniel Stenberg
committed
#endif
Daniel Stenberg
committed
return Curl_inet_ntop(af, af == AF_INET ? ip4 : ip6, buf, bufsize);
Daniel Stenberg
committed
}
/*
* Return a hostcache id string for the providing host + port, to be used by
* the DNS caching.
*/
create_hostcache_id(const char *server, int port)
/* create and return the new allocated entry */
return aprintf("%s:%d", server, port);
Daniel Stenberg
committed
struct hostcache_prune_data {
/*
* This function is set as a callback to be called for every entry in the DNS
* cache when we want to prune old unused entries.
*
* Returning non-zero means remove the entry, return 0 to keep it in the
* cache.
*/
hostcache_timestamp_remove(void *datap, void *hc)
Daniel Stenberg
committed
(struct hostcache_prune_data *) datap;
struct Curl_dns_entry *c = (struct Curl_dns_entry *) hc;
c->inuse) {
/* please don't remove */
/* fine, remove */
/*
* Prune the DNS cache. This assumes that a lock has already been taken.
*/
hostcache_prune(struct curl_hash *hostcache, long cache_timeout, time_t now)
Daniel Stenberg
committed
struct hostcache_prune_data user;
user.cache_timeout = cache_timeout;
user.now = now;
Curl_hash_clean_with_criterium(hostcache,
(void *) &user,
hostcache_timestamp_remove);
/*
* Library-wide function for pruning the DNS cache. This function takes and
* returns the appropriate locks.
*/
void Curl_hostcache_prune(struct SessionHandle *data)
{
time_t now;
Daniel Stenberg
committed
Daniel Stenberg
committed
if((data->set.dns_cache_timeout == -1) || !data->dns.hostcache)
Daniel Stenberg
committed
/* cache forever means never prune, and NULL hostcache means
we can't do it */
Daniel Stenberg
committed
return;
if(data->share)
Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
time(&now);
/* Remove outdated and unused entries from the hostcache */
Daniel Stenberg
committed
hostcache_prune(data->dns.hostcache,
data->set.dns_cache_timeout,
now);
if(data->share)
Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
}
/*
* Check if the entry should be pruned. Assumes a locked cache.
*/
Daniel Stenberg
committed
static int
remove_entry_if_stale(struct SessionHandle *data, struct Curl_dns_entry *dns)
{
struct hostcache_prune_data user;
Daniel Stenberg
committed
if( !dns || (data->set.dns_cache_timeout == -1) || !data->dns.hostcache)
Daniel Stenberg
committed
/* cache forever means never prune, and NULL hostcache means
we can't do it */
return 0;
time(&user.now);
user.cache_timeout = data->set.dns_cache_timeout;
Daniel Stenberg
committed
return 0;
Daniel Stenberg
committed
Curl_hash_clean_with_criterium(data->dns.hostcache,
Daniel Stenberg
committed
(void *) &user,
hostcache_timestamp_remove);
return 1;
}
Daniel Stenberg
committed
#ifdef HAVE_SIGSETJMP
/* Beware this is a global and unique instance. This is used to store the
return address that we can jump back to from inside a signal handler. This
is not thread-safe stuff. */
Daniel Stenberg
committed
sigjmp_buf curl_jmpenv;
#endif
/*
* Curl_cache_addr() stores a 'Curl_addrinfo' struct in the DNS cache.
*
* When calling Curl_resolv() has resulted in a response with a returned
* address, we call this function to store the information in the dns
* cache etc
*
* Returns the Curl_dns_entry entry pointer or NULL if the storage failed.
*/
struct Curl_dns_entry *
Curl_cache_addr(struct SessionHandle *data,
Curl_addrinfo *addr,
const char *hostname,
int port)
{
Daniel Stenberg
committed
char *entry_id;
Daniel Stenberg
committed
size_t entry_len;
Daniel Stenberg
committed
struct Curl_dns_entry *dns;
struct Curl_dns_entry *dns2;
Daniel Stenberg
committed
time_t now;
/* Create an entry id, based upon the hostname and port */
entry_id = create_hostcache_id(hostname, port);
Daniel Stenberg
committed
/* If we can't create the entry id, fail */
Daniel Stenberg
committed
return NULL;
entry_len = strlen(entry_id);
Daniel Stenberg
committed
/* Create a new cache entry */
dns = calloc(sizeof(struct Curl_dns_entry), 1);
Daniel Stenberg
committed
free(entry_id);
return NULL;
}
Daniel Stenberg
committed
dns->inuse = 0; /* init to not used */
dns->addr = addr; /* this is the address(es) */
/* Store the resolved data in our DNS cache. This function may return a
pointer to an existing struct already present in the hash, and it may
return the same argument we pass in. Make no assumptions. */
Daniel Stenberg
committed
dns2 = Curl_hash_add(data->dns.hostcache, entry_id, entry_len+1,
(void *)dns);
if(!dns2) {
/* Major badness, run away. */
free(dns);
Daniel Stenberg
committed
free(entry_id);
return NULL;
}
Daniel Stenberg
committed
time(&now);
dns = dns2;
Daniel Stenberg
committed
Daniel Stenberg
committed
dns->timestamp = now; /* used now */
Daniel Stenberg
committed
dns->inuse++; /* mark entry as in-use */
/* free the allocated entry_id again */
free(entry_id);
return dns;
}
/*
* Curl_resolv() is the main name resolve function within libcurl. It resolves
* a name and returns a pointer to the entry in the 'entry' argument (if one
* is provided). This function might return immediately if we're using asynch
* resolves. See the return codes.
*
* The cache entry we return will get its 'inuse' counter increased when this
* function is used. You MUST call Curl_resolv_unlock() later (when you're
* done using this struct) to decrease the counter again.
*
* Return codes:
*
Daniel Stenberg
committed
* CURLRESOLV_ERROR (-1) = error, no pointer
* CURLRESOLV_RESOLVED (0) = OK, pointer provided
* CURLRESOLV_PENDING (1) = waiting for response, no pointer
*/
Daniel Stenberg
committed
Daniel Stenberg
committed
int Curl_resolv(struct connectdata *conn,
const char *hostname,
Daniel Stenberg
committed
int port,
struct Curl_dns_entry **entry)
char *entry_id = NULL;
struct Curl_dns_entry *dns = NULL;
Daniel Stenberg
committed
size_t entry_len;
Daniel Stenberg
committed
struct SessionHandle *data = conn->data;
CURLcode result;
int rc = CURLRESOLV_ERROR; /* default to failure */
Sterling Hughes
committed
*entry = NULL;
Daniel Stenberg
committed
/* Create an entry id, based upon the hostname and port */
entry_id = create_hostcache_id(hostname, port);
/* If we can't create the entry id, fail */
entry_len = strlen(entry_id);
if(data->share)
Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
Sterling Hughes
committed
/* See if its already in our dns cache */
Daniel Stenberg
committed
dns = Curl_hash_pick(data->dns.hostcache, entry_id, entry_len+1);
/* See whether the returned entry is stale. Done before we release lock */
if( remove_entry_if_stale(data, dns) )
dns = NULL; /* the memory deallocation is being handled by the hash */
if(dns) {
dns->inuse++; /* we use it! */
rc = CURLRESOLV_RESOLVED;
}
Daniel Stenberg
committed
if(data->share)
Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
Sterling Hughes
committed
Daniel Stenberg
committed
/* free the allocated entry_id again */
free(entry_id);
Daniel Stenberg
committed
/* The entry was not in the cache. Resolve it to IP address */
Curl_addrinfo *addr;
int respwait;
/* Check what IP specifics the app has requested and if we can provide it.
* If not, bail out. */
if(!Curl_ipvalid(data))
Daniel Stenberg
committed
return CURLRESOLV_ERROR;
/* If Curl_getaddrinfo() returns NULL, 'respwait' might be set to a
non-zero value indicating that we need to wait for the response to the
resolve call */
addr = Curl_getaddrinfo(conn, hostname, port, &respwait);
if(respwait) {
/* the response to our resolve call will come asynchronously at
Daniel Stenberg
committed
a later time, good or bad */
Daniel Stenberg
committed
/* First, check that we haven't received the info by now */
result = Curl_is_resolved(conn, &dns);
if(result) /* error detected */
Daniel Stenberg
committed
return CURLRESOLV_ERROR;
Daniel Stenberg
committed
if(dns)
Daniel Stenberg
committed
rc = CURLRESOLV_RESOLVED; /* pointer provided */
Daniel Stenberg
committed
else
Daniel Stenberg
committed
rc = CURLRESOLV_PENDING; /* no info yet */
Daniel Stenberg
committed
}
}
else {
Daniel Stenberg
committed
if(data->share)
Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
Daniel Stenberg
committed
/* we got a response, store it in the cache */
dns = Curl_cache_addr(data, addr, hostname, port);
Daniel Stenberg
committed
if(data->share)
Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
if(!dns)
/* returned failure, bail out nicely */
Curl_freeaddrinfo(addr);
Daniel Stenberg
committed
rc = CURLRESOLV_RESOLVED;
Daniel Stenberg
committed
}
}
Sterling Hughes
committed
Daniel Stenberg
committed
*entry = dns;
Daniel Stenberg
committed
return rc;
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
#ifdef USE_ALARM_TIMEOUT
/*
* This signal handler jumps back into the main libcurl code and continues
* execution. This effectively causes the remainder of the application to run
* within a signal handler which is nonportable and could lead to problems.
*/
static
RETSIGTYPE alarmfunc(int sig)
{
/* this is for "-ansi -Wall -pedantic" to stop complaining! (rabe) */
(void)sig;
siglongjmp(curl_jmpenv, 1);
return;
}
#endif /* USE_ALARM_TIMEOUT */
/*
* Curl_resolv_timeout() is the same as Curl_resolv() but specifies a
* timeout. This function might return immediately if we're using asynch
* resolves. See the return codes.
*
* The cache entry we return will get its 'inuse' counter increased when this
* function is used. You MUST call Curl_resolv_unlock() later (when you're
* done using this struct) to decrease the counter again.
*
* If built with a synchronous resolver and use of signals is not
* disabled by the application, then a nonzero timeout will cause a
* timeout after the specified number of milliseconds. Otherwise, timeout
* is ignored.
*
* Return codes:
*
* CURLRESOLV_TIMEDOUT(-2) = warning, time too short or previous alarm expired
* CURLRESOLV_ERROR (-1) = error, no pointer
* CURLRESOLV_RESOLVED (0) = OK, pointer provided
* CURLRESOLV_PENDING (1) = waiting for response, no pointer
*/
int Curl_resolv_timeout(struct connectdata *conn,
const char *hostname,
int port,
struct Curl_dns_entry **entry,
Yang Tse
committed
long timeoutms)
{
#ifdef USE_ALARM_TIMEOUT
#ifdef HAVE_SIGACTION
struct sigaction keep_sigact; /* store the old struct here */
bool keep_copysig=FALSE; /* did copy it? */
struct sigaction sigact;
#else
#ifdef HAVE_SIGNAL
void (*keep_sigact)(int); /* store the old handler here */
#endif /* HAVE_SIGNAL */
#endif /* HAVE_SIGACTION */
Yang Tse
committed
volatile long timeout;
unsigned int prev_alarm=0;
struct SessionHandle *data = conn->data;
#endif /* USE_ALARM_TIMEOUT */
*entry = NULL;
#ifdef USE_ALARM_TIMEOUT
if (data->set.no_signal)
/* Ignore the timeout when signals are disabled */
timeout = 0;
Yang Tse
committed
else
timeout = timeoutms;
if(timeout && timeout < 1000)
/* The alarm() function only provides integer second resolution, so if
we want to wait less than one second we must bail out already now. */
return CURLRESOLV_TIMEDOUT;
if (timeout > 0) {
/* This allows us to time-out from the name resolver, as the timeout
will generate a signal and we will siglongjmp() from that here.
This technique has problems (see alarmfunc). */
if(sigsetjmp(curl_jmpenv, 1)) {
/* this is coming from a siglongjmp() after an alarm signal */
failf(data, "name lookup timed out");
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
}
/*************************************************************
* Set signal handler to catch SIGALRM
* Store the old value to be able to set it back later!
*************************************************************/
#ifdef HAVE_SIGACTION
sigaction(SIGALRM, NULL, &sigact);
keep_sigact = sigact;
keep_copysig = TRUE; /* yes, we have a copy */
sigact.sa_handler = alarmfunc;
#ifdef SA_RESTART
/* HPUX doesn't have SA_RESTART but defaults to that behaviour! */
sigact.sa_flags &= ~SA_RESTART;
#endif
/* now set the new struct */
sigaction(SIGALRM, &sigact, NULL);
#else /* HAVE_SIGACTION */
/* no sigaction(), revert to the much lamer signal() */
#ifdef HAVE_SIGNAL
keep_sigact = signal(SIGALRM, alarmfunc);
#endif
#endif /* HAVE_SIGACTION */
/* alarm() makes a signal get sent when the timeout fires off, and that
will abort system calls */
prev_alarm = alarm((unsigned int) (timeout ? timeout/1000L : timeout));
}
#else
#ifndef CURLRES_ASYNCH
infof(conn->data, "timeout on name lookup is not supported\n");
#else
(void)timeoutms; /* timeoutms not used with an async resolver */
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
#endif
#endif /* USE_ALARM_TIMEOUT */
/* Perform the actual name resolution. This might be interrupted by an
* alarm if it takes too long.
*/
rc = Curl_resolv(conn, hostname, port, entry);
#ifdef USE_ALARM_TIMEOUT
if (timeout > 0) {
#ifdef HAVE_SIGACTION
if(keep_copysig) {
/* we got a struct as it looked before, now put that one back nice
and clean */
sigaction(SIGALRM, &keep_sigact, NULL); /* put it back */
}
#else
#ifdef HAVE_SIGNAL
/* restore the previous SIGALRM handler */
signal(SIGALRM, keep_sigact);
#endif
#endif /* HAVE_SIGACTION */
/* switch back the alarm() to either zero or to what it was before minus
the time we spent until now! */
if(prev_alarm) {
/* there was an alarm() set before us, now put it back */
unsigned long elapsed_ms = Curl_tvdiff(Curl_tvnow(), conn->created);
/* the alarm period is counted in even number of seconds */
unsigned long alarm_set = prev_alarm - elapsed_ms/1000;
if(!alarm_set ||
((alarm_set >= 0x80000000) && (prev_alarm < 0x80000000)) ) {
/* if the alarm time-left reached zero or turned "negative" (counted
with unsigned values), we should fire off a SIGALRM here, but we
won't, and zero would be to switch it off so we never set it to
less than 1! */
alarm(1);
rc = CURLRESOLV_TIMEDOUT;
failf(data, "Previous alarm fired off!");
}
else
alarm((unsigned int)alarm_set);
}
else
alarm(0); /* just shut it off */
}
#endif /* USE_ALARM_TIMEOUT */
return rc;
}
/*
* Curl_resolv_unlock() unlocks the given cached DNS entry. When this has been
* made, the struct may be destroyed due to pruning. It is important that only
* one unlock is made for each Curl_resolv() call.
*/
void Curl_resolv_unlock(struct SessionHandle *data, struct Curl_dns_entry *dns)
{
DEBUGASSERT(dns && (dns->inuse>0));
if(data->share)
Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
if(data->share)
Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
}
Daniel Stenberg
committed
/*
* File-internal: free a cache dns entry.
Daniel Stenberg
committed
*/
static void freednsentry(void *freethis)
Daniel Stenberg
committed
{
struct Curl_dns_entry *p = (struct Curl_dns_entry *) freethis;
Curl_freeaddrinfo(p->addr);
Sterling Hughes
committed
free(p);
}
/*
* Curl_mk_dnscache() creates a new DNS cache and returns the handle for it.
*/
Daniel Stenberg
committed
struct curl_hash *Curl_mk_dnscache(void)
{
Daniel Stenberg
committed
return Curl_hash_alloc(7, Curl_hash_str, Curl_str_key_compare, freednsentry);
}
#ifdef CURLRES_ADDRINFO_COPY
/* align on even 64bit boundaries */
#define MEMALIGN(x) ((x)+(8-(((unsigned long)(x))&0x7)))
Daniel Stenberg
committed
/*
* Curl_addrinfo_copy() performs a "deep" copy of a hostent into a buffer and
* returns a pointer to the malloc()ed copy. You need to call free() on the
* returned buffer when you're done with it.
Daniel Stenberg
committed
*/
Curl_addrinfo *Curl_addrinfo_copy(const void *org, int port)
Daniel Stenberg
committed
{
const struct hostent *orig = org;
Daniel Stenberg
committed
Daniel Stenberg
committed
return Curl_he2ai(orig, port);
Daniel Stenberg
committed
}
#endif /* CURLRES_ADDRINFO_COPY */
/***********************************************************************
Daniel Stenberg
committed
* Only for plain-ipv4 and c-ares builds (NOTE: c-ares builds can be IPv6
* enabled)
**********************************************************************/
#if defined(CURLRES_IPV4) || defined(CURLRES_ARES)
/*
* This is a function for freeing name information in a protocol independent
* way.
*/
void Curl_freeaddrinfo(Curl_addrinfo *ai)
{
Curl_addrinfo *next;
/* walk over the list and free all entries */
while(ai) {
next = ai->ai_next;
if(ai->ai_addr)
free(ai->ai_addr);
if(ai->ai_canonname)
free(ai->ai_canonname);
memset(ai, 0, sizeof(Curl_addrinfo));
free(ai);
ai = next;
}
}
struct namebuf4 {
struct hostent hostentry;
struct in_addr addrentry;
char *h_addr_list[2];
};
/*
* Curl_ip2addr() takes a 32bit ipv4 internet address as input parameter
* together with a pointer to the string version of the address, and it
* returns a Curl_addrinfo chain filled in correctly with information for this
* address/host.
*
* The input parameters ARE NOT checked for validity but they are expected
* to have been checked already when this is called.
*/
Curl_addrinfo *Curl_ip2addr(in_addr_t num, const char *hostname, int port)
{
Curl_addrinfo *ai;
#if defined(VMS) && \
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 in_addr *addrentry;
struct namebuf4 *buf;
char *hoststr;
DEBUGASSERT(hostname);
buf = malloc(sizeof(struct namebuf4));
if(!buf)
return NULL;
hoststr = strdup(hostname);
if(!hoststr) {
free(buf);
return NULL;
}
addrentry = &buf->addrentry;
#ifdef _CRAYC
/* On UNICOS, s_addr is a bit field and for some reason assigning to it
* doesn't work. There must be a better fix than this ugly hack.
*/
memcpy(addrentry, &num, SIZEOF_in_addr);
#else
addrentry->s_addr = num;
h = &buf->hostentry;
h->h_name = hoststr;
h->h_aliases = NULL;
h->h_addrtype = AF_INET;
h->h_length = sizeof(struct in_addr);
h->h_addr_list = &buf->h_addr_list[0];
h->h_addr_list[0] = (char*)addrentry;
h->h_addr_list[1] = NULL; /* terminate list of entries */
#if defined(VMS) && \
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);
Daniel Stenberg
committed
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
/*
* Curl_he2ai() translates from a hostent struct to a Curl_addrinfo struct.
* The Curl_addrinfo is meant to work like the addrinfo struct does for IPv6
* stacks, but for all hosts and environments.
*
* Curl_addrinfo defined in "lib/hostip.h"
*
* struct Curl_addrinfo {
* int ai_flags;
* int ai_family;
* int ai_socktype;
* int ai_protocol;
* socklen_t ai_addrlen; * Follow rfc3493 struct addrinfo *
* char *ai_canonname;
* struct sockaddr *ai_addr;
* struct Curl_addrinfo *ai_next;
* };
*
* 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 CURLRES_IPV6
struct sockaddr_in6 *addr6;
#endif /* CURLRES_IPV6 */
CURLcode result = CURLE_OK;
Daniel Stenberg
committed
int i;
char *curr;
Daniel Stenberg
committed
if(!he)
/* no input == no output! */
return NULL;
for(i=0; (curr = he->h_addr_list[i]) != NULL; i++) {
Daniel Stenberg
committed
int ss_size;
#ifdef CURLRES_IPV6
if (he->h_addrtype == AF_INET6)
ss_size = sizeof (struct sockaddr_in6);
else
#endif /* CURLRES_IPV6 */
ss_size = sizeof (struct sockaddr_in);
if((ai = calloc(1, sizeof(Curl_addrinfo))) == NULL) {
result = CURLE_OUT_OF_MEMORY;
break;
}
if((ai->ai_canonname = strdup(he->h_name)) == NULL) {
result = CURLE_OUT_OF_MEMORY;
free(ai);
break;
}
if((ai->ai_addr = calloc(1, ss_size)) == NULL) {
result = CURLE_OUT_OF_MEMORY;
free(ai->ai_canonname);
free(ai);
Daniel Stenberg
committed
break;
Daniel Stenberg
committed
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;
ai->ai_addrlen = ss_size;
/* leave the rest of the struct filled with zero */
switch (ai->ai_family) {
case AF_INET:
addr = (struct sockaddr_in *)ai->ai_addr; /* storage area for this info */
memcpy(&addr->sin_addr, curr, sizeof(struct in_addr));
Daniel Stenberg
committed
addr->sin_family = (unsigned short)(he->h_addrtype);
addr->sin_port = htons((unsigned short)port);
break;
#ifdef CURLRES_IPV6
case AF_INET6:
addr6 = (struct sockaddr_in6 *)ai->ai_addr; /* storage area for this info */
memcpy(&addr6->sin6_addr, curr, sizeof(struct in6_addr));
Daniel Stenberg
committed
addr6->sin6_family = (unsigned short)(he->h_addrtype);
addr6->sin6_port = htons((unsigned short)port);
break;
#endif /* CURLRES_IPV6 */
}
prevai = ai;
}
Curl_freeaddrinfo(firstai);
Daniel Stenberg
committed
return firstai;
}
#endif /* CURLRES_IPV4 || CURLRES_ARES */