3 /* Copyright 1998 by the Massachusetts Institute of Technology.
4 * Copyright (C) 2004-2009 by Daniel Stenberg
6 * Permission to use, copy, modify, and distribute this
7 * software and its documentation for any purpose and without
8 * fee is hereby granted, provided that the above copyright
9 * notice appear in all copies and that both that copyright
10 * notice and this permission notice appear in supporting
11 * documentation, and that the name of M.I.T. not be used in
12 * advertising or publicity pertaining to distribution of the
13 * software without specific, written prior permission.
14 * M.I.T. makes no representations about the suitability of
15 * this software for any purpose. It is provided "as is"
16 * without express or implied warranty.
22 #include "ares_library_init.h"
23 #include "ares_private.h"
25 /* library-private global and unique instance vars */
28 fpGetNetworkParams_t fpGetNetworkParams = ZERO_NULL;
29 fpSystemFunction036_t fpSystemFunction036 = ZERO_NULL;
32 /* library-private global vars with source visibility restricted to this file */
34 static unsigned int ares_initialized;
35 static int ares_init_flags;
38 static HMODULE hnd_iphlpapi;
39 static HMODULE hnd_advapi32;
43 static int ares_win32_init(void)
48 hnd_iphlpapi = LoadLibrary("iphlpapi.dll");
50 return ARES_ELOADIPHLPAPI;
52 fpGetNetworkParams = (fpGetNetworkParams_t)
53 GetProcAddress(hnd_iphlpapi, "GetNetworkParams");
54 if (!fpGetNetworkParams)
56 FreeLibrary(hnd_iphlpapi);
57 return ARES_EADDRGETNETWORKPARAMS;
61 * When advapi32.dll is unavailable or advapi32.dll has no SystemFunction036,
62 * also known as RtlGenRandom, which is the case for Windows versions prior
63 * to WinXP then c-ares uses portable rand() function. Then don't error here.
67 hnd_advapi32 = LoadLibrary("advapi32.dll");
70 fpSystemFunction036 = (fpSystemFunction036_t)
71 GetProcAddress(hnd_advapi32, "SystemFunction036");
79 static void ares_win32_cleanup(void)
83 FreeLibrary(hnd_advapi32);
85 FreeLibrary(hnd_iphlpapi);
90 int ares_library_init(int flags)
98 if (flags & ARES_LIB_INIT_WIN32)
100 res = ares_win32_init();
101 if (res != ARES_SUCCESS)
105 ares_init_flags = flags;
111 void ares_library_cleanup(void)
113 if (!ares_initialized)
117 if (ares_init_flags & ARES_LIB_INIT_WIN32)
118 ares_win32_cleanup();
120 ares_init_flags = ARES_LIB_INIT_NONE;
124 int ares_library_initialized(void)
127 if (!ares_initialized)
128 return ARES_ENOTINITIALIZED;