update for beta universally
[external/c-ares.git] / 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 #endif
30
31 /* library-private global vars with source visibility restricted to this file */
32
33 static unsigned int ares_initialized;
34 static int          ares_init_flags;
35
36 #ifdef USE_WINSOCK
37 static HMODULE hnd_iphlpapi;
38 static HMODULE hnd_advapi32;
39 #endif
40
41
42 static int ares_win32_init(void)
43 {
44 #ifdef USE_WINSOCK
45
46   hnd_iphlpapi = 0;
47   hnd_iphlpapi = LoadLibrary("iphlpapi.dll");
48   if (!hnd_iphlpapi)
49     return ARES_ELOADIPHLPAPI;
50
51   ares_fpGetNetworkParams = (fpGetNetworkParams_t)
52     GetProcAddress(hnd_iphlpapi, "GetNetworkParams");
53   if (!ares_fpGetNetworkParams)
54     {
55       FreeLibrary(hnd_iphlpapi);
56       return ARES_EADDRGETNETWORKPARAMS;
57     }
58
59   /*
60    * When advapi32.dll is unavailable or advapi32.dll has no SystemFunction036,
61    * also known as RtlGenRandom, which is the case for Windows versions prior
62    * to WinXP then c-ares uses portable rand() function. Then don't error here.
63    */
64
65   hnd_advapi32 = 0;
66   hnd_advapi32 = LoadLibrary("advapi32.dll");
67   if (hnd_advapi32)
68     {
69       ares_fpSystemFunction036 = (fpSystemFunction036_t)
70         GetProcAddress(hnd_advapi32, "SystemFunction036");
71     }
72
73 #endif
74   return ARES_SUCCESS;
75 }
76
77
78 static void ares_win32_cleanup(void)
79 {
80 #ifdef USE_WINSOCK
81   if (hnd_advapi32)
82     FreeLibrary(hnd_advapi32);
83   if (hnd_iphlpapi)
84     FreeLibrary(hnd_iphlpapi);
85 #endif
86 }
87
88
89 int ares_library_init(int flags)
90 {
91   int res;
92
93   if (ares_initialized)
94     return ARES_SUCCESS;
95   ares_initialized++;
96
97   if (flags & ARES_LIB_INIT_WIN32)
98     {
99       res = ares_win32_init();
100       if (res != ARES_SUCCESS)
101         return res;
102     }
103
104   ares_init_flags = flags;
105
106   return ARES_SUCCESS;
107 }
108
109
110 void ares_library_cleanup(void)
111 {
112   if (!ares_initialized)
113     return;
114   ares_initialized--;
115
116   if (ares_init_flags & ARES_LIB_INIT_WIN32)
117     ares_win32_cleanup();
118
119   ares_init_flags = ARES_LIB_INIT_NONE;
120 }
121
122
123 int ares_library_initialized(void)
124 {
125 #ifdef USE_WINSOCK
126   if (!ares_initialized)
127     return ARES_ENOTINITIALIZED;
128 #endif
129   return ARES_SUCCESS;
130 }
131
132