2 /* Copyright 1998 by the Massachusetts Institute of Technology.
3 * Copyright (C) 2004-2009 by Daniel Stenberg
5 * Permission to use, copy, modify, and distribute this
6 * software and its documentation for any purpose and without
7 * fee is hereby granted, provided that the above copyright
8 * notice appear in all copies and that both that copyright
9 * notice and this permission notice appear in supporting
10 * documentation, and that the name of M.I.T. not be used in
11 * advertising or publicity pertaining to distribution of the
12 * software without specific, written prior permission.
13 * M.I.T. makes no representations about the suitability of
14 * this software for any purpose. It is provided "as is"
15 * without express or implied warranty.
18 #include "ares_setup.h"
21 #include "ares_library_init.h"
22 #include "ares_private.h"
24 /* library-private global and unique instance vars */
27 fpGetNetworkParams_t ares_fpGetNetworkParams = ZERO_NULL;
28 fpSystemFunction036_t ares_fpSystemFunction036 = ZERO_NULL;
29 fpGetAdaptersAddresses_t ares_fpGetAdaptersAddresses = ZERO_NULL;
30 fpGetBestRoute2_t ares_fpGetBestRoute2 = ZERO_NULL;
33 #if defined(ANDROID) || defined(__ANDROID__)
34 #include "ares_android.h"
37 /* library-private global vars with source visibility restricted to this file */
39 static unsigned int ares_initialized;
40 static int ares_init_flags;
42 /* library-private global vars with visibility across the whole library */
44 /* We need indirections to handle Windows DLL rules. */
45 static void *default_malloc(size_t size) { return malloc(size); }
46 static void *default_realloc(void *p, size_t size) { return realloc(p, size); }
47 static void default_free(void *p) { free(p); }
49 # define default_malloc malloc
50 # define default_realloc realloc
51 # define default_free free
53 void *(*ares_malloc)(size_t size) = default_malloc;
54 void *(*ares_realloc)(void *ptr, size_t size) = default_realloc;
55 void (*ares_free)(void *ptr) = default_free;
58 static HMODULE hnd_iphlpapi;
59 static HMODULE hnd_advapi32;
63 static int ares_win32_init(void)
68 hnd_iphlpapi = LoadLibraryW(L"iphlpapi.dll");
70 return ARES_ELOADIPHLPAPI;
72 ares_fpGetNetworkParams = (fpGetNetworkParams_t)
73 GetProcAddress(hnd_iphlpapi, "GetNetworkParams");
74 if (!ares_fpGetNetworkParams)
76 FreeLibrary(hnd_iphlpapi);
77 return ARES_EADDRGETNETWORKPARAMS;
80 ares_fpGetAdaptersAddresses = (fpGetAdaptersAddresses_t)
81 GetProcAddress(hnd_iphlpapi, "GetAdaptersAddresses");
82 if (!ares_fpGetAdaptersAddresses)
84 /* This can happen on clients before WinXP, I don't
85 think it should be an error, unless we don't want to
86 support Windows 2000 anymore */
89 ares_fpGetBestRoute2 = (fpGetBestRoute2_t)
90 GetProcAddress(hnd_iphlpapi, "GetBestRoute2");
91 if (!ares_fpGetBestRoute2)
93 /* This can happen on clients before Vista, I don't
94 think it should be an error, unless we don't want to
95 support Windows XP anymore */
99 * When advapi32.dll is unavailable or advapi32.dll has no SystemFunction036,
100 * also known as RtlGenRandom, which is the case for Windows versions prior
101 * to WinXP then c-ares uses portable rand() function. Then don't error here.
105 hnd_advapi32 = LoadLibraryW(L"advapi32.dll");
108 ares_fpSystemFunction036 = (fpSystemFunction036_t)
109 GetProcAddress(hnd_advapi32, "SystemFunction036");
117 static void ares_win32_cleanup(void)
121 FreeLibrary(hnd_advapi32);
123 FreeLibrary(hnd_iphlpapi);
128 int ares_library_init(int flags)
132 if (ares_initialized)
139 if (flags & ARES_LIB_INIT_WIN32)
141 res = ares_win32_init();
142 if (res != ARES_SUCCESS)
143 return res; /* LCOV_EXCL_LINE: can't test Win32 init failure */
146 ares_init_flags = flags;
151 int ares_library_init_mem(int flags,
152 void *(*amalloc)(size_t size),
153 void (*afree)(void *ptr),
154 void *(*arealloc)(void *ptr, size_t size))
157 ares_malloc = amalloc;
159 ares_realloc = arealloc;
162 return ares_library_init(flags);
166 void ares_library_cleanup(void)
168 if (!ares_initialized)
171 if (ares_initialized)
174 if (ares_init_flags & ARES_LIB_INIT_WIN32)
175 ares_win32_cleanup();
177 #if defined(ANDROID) || defined(__ANDROID__)
178 ares_library_cleanup_android();
181 ares_init_flags = ARES_LIB_INIT_NONE;
182 ares_malloc = malloc;
183 ares_realloc = realloc;
188 int ares_library_initialized(void)
191 if (!ares_initialized)
192 return ARES_ENOTINITIALIZED;