1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
|
/* 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.
*/
#include "ares_setup.h"
#include "ares.h"
#include "ares_library_init.h"
#include "ares_private.h"
/* library-private global and unique instance vars */
#ifdef USE_WINSOCK
fpGetNetworkParams_t ares_fpGetNetworkParams = ZERO_NULL;
fpSystemFunction036_t ares_fpSystemFunction036 = ZERO_NULL;
fpGetAdaptersAddresses_t ares_fpGetAdaptersAddresses = ZERO_NULL;
#endif
/* library-private global vars with source visibility restricted to this file */
static unsigned int ares_initialized;
static int ares_init_flags;
/* library-private global vars with visibility across the whole library */
void *(*ares_malloc)(size_t size) = malloc;
void *(*ares_realloc)(void *ptr, size_t size) = realloc;
void (*ares_free)(void *ptr) = free;
#ifdef USE_WINSOCK
static HMODULE hnd_iphlpapi;
static HMODULE hnd_advapi32;
#endif
static int ares_win32_init(void)
{
#ifdef USE_WINSOCK
hnd_iphlpapi = 0;
hnd_iphlpapi = LoadLibraryW(L"iphlpapi.dll");
if (!hnd_iphlpapi)
return ARES_ELOADIPHLPAPI;
ares_fpGetNetworkParams = (fpGetNetworkParams_t)
GetProcAddress(hnd_iphlpapi, "GetNetworkParams");
if (!ares_fpGetNetworkParams)
{
FreeLibrary(hnd_iphlpapi);
return ARES_EADDRGETNETWORKPARAMS;
}
ares_fpGetAdaptersAddresses = (fpGetAdaptersAddresses_t)
GetProcAddress(hnd_iphlpapi, "GetAdaptersAddresses");
if (!ares_fpGetAdaptersAddresses)
{
/* This can happen on clients before WinXP, I don't
think it should be an error, unless we don't want to
support Windows 2000 anymore */
}
/*
* 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.
*/
hnd_advapi32 = 0;
hnd_advapi32 = LoadLibraryW(L"advapi32.dll");
if (hnd_advapi32)
{
ares_fpSystemFunction036 = (fpSystemFunction036_t)
GetProcAddress(hnd_advapi32, "SystemFunction036");
}
#endif
return ARES_SUCCESS;
}
static void ares_win32_cleanup(void)
{
#ifdef USE_WINSOCK
if (hnd_advapi32)
FreeLibrary(hnd_advapi32);
if (hnd_iphlpapi)
FreeLibrary(hnd_iphlpapi);
#endif
}
int ares_library_init(int flags)
{
int res;
if (ares_initialized)
{
ares_initialized++;
return ARES_SUCCESS;
}
ares_initialized++;
if (flags & ARES_LIB_INIT_WIN32)
{
res = ares_win32_init();
if (res != ARES_SUCCESS)
return res; /* LCOV_EXCL_LINE: can't test Win32 init failure */
}
ares_init_flags = flags;
return ARES_SUCCESS;
}
int ares_library_init_mem(int flags,
void *(*amalloc)(size_t size),
void (*afree)(void *ptr),
void *(*arealloc)(void *ptr, size_t size))
{
if (amalloc)
ares_malloc = amalloc;
if (arealloc)
ares_realloc = arealloc;
if (afree)
ares_free = afree;
return ares_library_init(flags);
}
void ares_library_cleanup(void)
{
if (!ares_initialized)
return;
ares_initialized--;
if (ares_initialized)
return;
if (ares_init_flags & ARES_LIB_INIT_WIN32)
ares_win32_cleanup();
ares_init_flags = ARES_LIB_INIT_NONE;
ares_malloc = malloc;
ares_realloc = realloc;
ares_free = free;
}
int ares_library_initialized(void)
{
#ifdef USE_WINSOCK
if (!ares_initialized)
return ARES_ENOTINITIALIZED;
#endif
return ARES_SUCCESS;
}
|