Built motion from commit 6a09e18b.|2.6.11
[motion2.git] / legacy-libs / grpc / deps / grpc / third_party / cares / cares / ares_library_init.c
1
2 /* Copyright 1998 by the Massachusetts Institute of Technology.
3  * Copyright (C) 2004-2009 by Daniel Stenberg
4  *
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.
16  */
17
18 #include "ares_setup.h"
19
20 #include "ares.h"
21 #include "ares_library_init.h"
22 #include "ares_private.h"
23
24 /* library-private global and unique instance vars */
25
26 #ifdef USE_WINSOCK
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;
31 #endif
32
33 #if defined(ANDROID) || defined(__ANDROID__)
34 #include "ares_android.h"
35 #endif
36
37 /* library-private global vars with source visibility restricted to this file */
38
39 static unsigned int ares_initialized;
40 static int          ares_init_flags;
41
42 /* library-private global vars with visibility across the whole library */
43 #if defined(WIN32)
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); }
48 #else
49 # define default_malloc malloc
50 # define default_realloc realloc
51 # define default_free free
52 #endif
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;
56
57 #ifdef USE_WINSOCK
58 static HMODULE hnd_iphlpapi;
59 static HMODULE hnd_advapi32;
60 #endif
61
62
63 static int ares_win32_init(void)
64 {
65 #ifdef USE_WINSOCK
66
67   hnd_iphlpapi = 0;
68   hnd_iphlpapi = LoadLibraryW(L"iphlpapi.dll");
69   if (!hnd_iphlpapi)
70     return ARES_ELOADIPHLPAPI;
71
72   ares_fpGetNetworkParams = (fpGetNetworkParams_t)
73     GetProcAddress(hnd_iphlpapi, "GetNetworkParams");
74   if (!ares_fpGetNetworkParams)
75     {
76       FreeLibrary(hnd_iphlpapi);
77       return ARES_EADDRGETNETWORKPARAMS;
78     }
79
80   ares_fpGetAdaptersAddresses = (fpGetAdaptersAddresses_t)
81     GetProcAddress(hnd_iphlpapi, "GetAdaptersAddresses");
82   if (!ares_fpGetAdaptersAddresses)
83     {
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 */
87     }
88
89   ares_fpGetBestRoute2 = (fpGetBestRoute2_t)
90     GetProcAddress(hnd_iphlpapi, "GetBestRoute2");
91   if (!ares_fpGetBestRoute2)
92     {
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 */
96     }
97
98   /*
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.
102    */
103
104   hnd_advapi32 = 0;
105   hnd_advapi32 = LoadLibraryW(L"advapi32.dll");
106   if (hnd_advapi32)
107     {
108       ares_fpSystemFunction036 = (fpSystemFunction036_t)
109         GetProcAddress(hnd_advapi32, "SystemFunction036");
110     }
111
112 #endif
113   return ARES_SUCCESS;
114 }
115
116
117 static void ares_win32_cleanup(void)
118 {
119 #ifdef USE_WINSOCK
120   if (hnd_advapi32)
121     FreeLibrary(hnd_advapi32);
122   if (hnd_iphlpapi)
123     FreeLibrary(hnd_iphlpapi);
124 #endif
125 }
126
127
128 int ares_library_init(int flags)
129 {
130   int res;
131
132   if (ares_initialized)
133     {
134       ares_initialized++;
135       return ARES_SUCCESS;
136     }
137   ares_initialized++;
138
139   if (flags & ARES_LIB_INIT_WIN32)
140     {
141       res = ares_win32_init();
142       if (res != ARES_SUCCESS)
143         return res;  /* LCOV_EXCL_LINE: can't test Win32 init failure */
144     }
145
146   ares_init_flags = flags;
147
148   return ARES_SUCCESS;
149 }
150
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))
155 {
156   if (amalloc)
157     ares_malloc = amalloc;
158   if (arealloc)
159     ares_realloc = arealloc;
160   if (afree)
161     ares_free = afree;
162   return ares_library_init(flags);
163 }
164
165
166 void ares_library_cleanup(void)
167 {
168   if (!ares_initialized)
169     return;
170   ares_initialized--;
171   if (ares_initialized)
172     return;
173
174   if (ares_init_flags & ARES_LIB_INIT_WIN32)
175     ares_win32_cleanup();
176
177 #if defined(ANDROID) || defined(__ANDROID__)
178   ares_library_cleanup_android();
179 #endif
180
181   ares_init_flags = ARES_LIB_INIT_NONE;
182   ares_malloc = malloc;
183   ares_realloc = realloc;
184   ares_free = free;
185 }
186
187
188 int ares_library_initialized(void)
189 {
190 #ifdef USE_WINSOCK
191   if (!ares_initialized)
192     return ARES_ENOTINITIALIZED;
193 #endif
194   return ARES_SUCCESS;
195 }