2009-05-17 19:10:41 +02:00
|
|
|
/* $Id$ */
|
|
|
|
|
2009-05-19 17:23:25 +02:00
|
|
|
/* Copyright 1998 by the Massachusetts Institute of Technology.
|
|
|
|
* Copyright (C) 2004-2009 by Daniel Stenberg
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this
|
|
|
|
* software and its documentation for any purpose and without
|
|
|
|
* fee is hereby granted, provided that the above copyright
|
|
|
|
* notice appear in all copies and that both that copyright
|
|
|
|
* notice and this permission notice appear in supporting
|
|
|
|
* documentation, and that the name of M.I.T. not be used in
|
|
|
|
* advertising or publicity pertaining to distribution of the
|
|
|
|
* software without specific, written prior permission.
|
|
|
|
* M.I.T. makes no representations about the suitability of
|
|
|
|
* this software for any purpose. It is provided "as is"
|
|
|
|
* without express or implied warranty.
|
|
|
|
*/
|
|
|
|
|
2009-11-02 12:55:53 +01:00
|
|
|
#include "ares_setup.h"
|
2009-05-17 19:10:41 +02:00
|
|
|
|
|
|
|
#include "ares.h"
|
|
|
|
#include "ares_library_init.h"
|
|
|
|
#include "ares_private.h"
|
|
|
|
|
|
|
|
/* library-private global and unique instance vars */
|
|
|
|
|
2009-11-09 13:57:26 +01:00
|
|
|
#ifdef USE_WINSOCK
|
2009-11-11 09:56:46 +01:00
|
|
|
fpGetNetworkParams_t ares_fpGetNetworkParams = ZERO_NULL;
|
|
|
|
fpSystemFunction036_t ares_fpSystemFunction036 = ZERO_NULL;
|
2009-05-17 19:10:41 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* library-private global vars with source visibility restricted to this file */
|
|
|
|
|
|
|
|
static unsigned int ares_initialized;
|
|
|
|
static int ares_init_flags;
|
|
|
|
|
2009-11-09 13:57:26 +01:00
|
|
|
#ifdef USE_WINSOCK
|
2009-05-17 19:10:41 +02:00
|
|
|
static HMODULE hnd_iphlpapi;
|
|
|
|
static HMODULE hnd_advapi32;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
static int ares_win32_init(void)
|
|
|
|
{
|
2009-11-09 13:57:26 +01:00
|
|
|
#ifdef USE_WINSOCK
|
2009-05-17 19:10:41 +02:00
|
|
|
|
|
|
|
hnd_iphlpapi = 0;
|
|
|
|
hnd_iphlpapi = LoadLibrary("iphlpapi.dll");
|
|
|
|
if (!hnd_iphlpapi)
|
|
|
|
return ARES_ELOADIPHLPAPI;
|
|
|
|
|
2009-11-11 09:56:46 +01:00
|
|
|
ares_fpGetNetworkParams = (fpGetNetworkParams_t)
|
2009-05-17 19:10:41 +02:00
|
|
|
GetProcAddress(hnd_iphlpapi, "GetNetworkParams");
|
2009-11-11 09:56:46 +01:00
|
|
|
if (!ares_fpGetNetworkParams)
|
2009-05-17 19:10:41 +02:00
|
|
|
{
|
|
|
|
FreeLibrary(hnd_iphlpapi);
|
2009-05-19 17:19:03 +02:00
|
|
|
return ARES_EADDRGETNETWORKPARAMS;
|
2009-05-17 19:10:41 +02:00
|
|
|
}
|
|
|
|
|
2009-05-18 17:49:32 +02:00
|
|
|
/*
|
|
|
|
* When advapi32.dll is unavailable or advapi32.dll has no SystemFunction036,
|
|
|
|
* also known as RtlGenRandom, which is the case for Windows versions prior
|
|
|
|
* to WinXP then c-ares uses portable rand() function. Then don't error here.
|
|
|
|
*/
|
|
|
|
|
2009-05-17 19:10:41 +02:00
|
|
|
hnd_advapi32 = 0;
|
|
|
|
hnd_advapi32 = LoadLibrary("advapi32.dll");
|
2009-05-18 17:49:32 +02:00
|
|
|
if (hnd_advapi32)
|
2009-05-17 19:10:41 +02:00
|
|
|
{
|
2009-11-11 09:56:46 +01:00
|
|
|
ares_fpSystemFunction036 = (fpSystemFunction036_t)
|
2009-05-18 17:49:32 +02:00
|
|
|
GetProcAddress(hnd_advapi32, "SystemFunction036");
|
2009-05-17 19:10:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
return ARES_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void ares_win32_cleanup(void)
|
|
|
|
{
|
2009-11-09 13:57:26 +01:00
|
|
|
#ifdef USE_WINSOCK
|
2009-05-17 19:10:41 +02:00
|
|
|
if (hnd_advapi32)
|
|
|
|
FreeLibrary(hnd_advapi32);
|
|
|
|
if (hnd_iphlpapi)
|
|
|
|
FreeLibrary(hnd_iphlpapi);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int ares_library_init(int flags)
|
|
|
|
{
|
|
|
|
int res;
|
|
|
|
|
|
|
|
if (ares_initialized)
|
|
|
|
return ARES_SUCCESS;
|
|
|
|
ares_initialized++;
|
|
|
|
|
|
|
|
if (flags & ARES_LIB_INIT_WIN32)
|
|
|
|
{
|
|
|
|
res = ares_win32_init();
|
|
|
|
if (res != ARES_SUCCESS)
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
ares_init_flags = flags;
|
|
|
|
|
|
|
|
return ARES_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ares_library_cleanup(void)
|
|
|
|
{
|
|
|
|
if (!ares_initialized)
|
|
|
|
return;
|
|
|
|
ares_initialized--;
|
|
|
|
|
|
|
|
if (ares_init_flags & ARES_LIB_INIT_WIN32)
|
|
|
|
ares_win32_cleanup();
|
|
|
|
|
|
|
|
ares_init_flags = ARES_LIB_INIT_NONE;
|
|
|
|
}
|
|
|
|
|
2009-05-26 20:00:14 +02:00
|
|
|
|
|
|
|
int ares_library_initialized(void)
|
|
|
|
{
|
2009-11-09 13:57:26 +01:00
|
|
|
#ifdef USE_WINSOCK
|
2009-05-26 20:00:14 +02:00
|
|
|
if (!ares_initialized)
|
|
|
|
return ARES_ENOTINITIALIZED;
|
|
|
|
#endif
|
|
|
|
return ARES_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|