Newer
Older
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) 1998 - 2004, 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.
***************************************************************************/
#if defined(WIN32) && !defined(__GNUC__) || defined(__MINGW32__)
#else
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
#ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h> /* required for free() prototypes */
#endif
#include <inet.h>
#include <stdlib.h>
#endif
Daniel Stenberg
committed
#ifdef HAVE_SETJMP_H
#include <setjmp.h>
#endif
#if (defined(NETWARE) && defined(__NOVELL_LIBC__))
#undef in_addr_t
#define in_addr_t unsigned long
#endif
#include "hash.h"
Daniel Stenberg
committed
#include "url.h"
#define _MPRINTF_REPLACE /* use our functions only */
#include <curl/mprintf.h>
#if defined(HAVE_INET_NTOA_R) && !defined(HAVE_INET_NTOA_R_DECL)
/* The last #include file should be: */
#ifdef CURLDEBUG
Daniel Stenberg
committed
#ifndef ARES_SUCCESS
#define ARES_SUCCESS CURLE_OK
#endif
static curl_hash hostname_cache;
static int host_cache_initialized;
Daniel Stenberg
committed
static Curl_addrinfo *my_getaddrinfo(struct connectdata *conn,
char *hostname,
int port,
int *waitp);
Daniel Stenberg
committed
#ifndef ENABLE_IPV6
Daniel Stenberg
committed
#if !defined(HAVE_GETHOSTBYNAME_R) || defined(USE_ARES) || \
defined(USE_THREADING_GETHOSTBYNAME)
Daniel Stenberg
committed
static struct hostent* pack_hostent(char** buf, struct hostent* orig);
#endif
Daniel Stenberg
committed
#endif
Daniel Stenberg
committed
#ifdef USE_THREADING_GETHOSTBYNAME
#ifdef DEBUG_THREADING_GETHOSTBYNAME
/* If this is defined, provide tracing */
Daniel Stenberg
committed
#define TRACE(args) \
do { trace_it("%u: ", __LINE__); trace_it args; } while (0)
static void trace_it (const char *fmt, ...);
#else
#define TRACE(x)
#endif
Daniel Stenberg
committed
static struct hostent* pack_hostent (char** buf, struct hostent* orig);
static bool init_gethostbyname_thread (struct connectdata *conn,
const char *hostname, int port);
struct thread_data {
HANDLE thread_hnd;
DWORD thread_id;
DWORD thread_status;
};
#endif
void Curl_global_host_cache_init(void)
if (!host_cache_initialized) {
Daniel Stenberg
committed
Curl_hash_init(&hostname_cache, 7, Curl_freednsinfo);
host_cache_initialized = 1;
}
curl_hash *Curl_global_host_cache_get(void)
return &hostname_cache;
void Curl_global_host_cache_dtor(void)
{
if (host_cache_initialized) {
Daniel Stenberg
committed
Curl_hash_clean(&hostname_cache);
host_cache_initialized = 0;
}
}
/* count the number of characters that an integer takes up */
static int _num_chars(int i)
{
int chars = 0;
/* While the number divided by 10 is greater than one,
* re-divide the number by 10, and increment the number of
* characters by 1.
*
* this relies on the fact that for every multiple of 10,
* a new digit is added onto every number
*/
do {
chars++;
i = (int) i / 10;
} while (i >= 1);
return chars;
}
/* Create a hostcache id */
Daniel Stenberg
committed
create_hostcache_id(char *server, int port, size_t *entry_len)
{
char *id = NULL;
/* Get the length of the new entry id */
Daniel Stenberg
committed
*entry_len = strlen(server) + /* Hostname length */
1 + /* ':' seperator */
_num_chars(port); /* number of characters the port will take up */
/* Allocate the new entry id */
Daniel Stenberg
committed
id = malloc(*entry_len + 1); /* 1 extra for the zero terminator */
Daniel Stenberg
committed
if (!id)
return NULL;
/* Create the new entry */
Daniel Stenberg
committed
sprintf(id, "%s:%d", server, port);
Daniel Stenberg
committed
return id; /* return pointer to the string */
Daniel Stenberg
committed
struct hostcache_prune_data {
hostcache_timestamp_remove(void *datap, void *hc)
Daniel Stenberg
committed
struct hostcache_prune_data *data =
(struct hostcache_prune_data *) datap;
struct Curl_dns_entry *c = (struct Curl_dns_entry *) hc;
Loading
Loading full blame…