Make ares_init(), ares_dup() and ares_init_options() return ARES_ENOTINITIALIZED
[platform/upstream/c-ares.git] / ares_library_init.c
1 /* $Id$ */
2
3 /* Copyright 1998 by the Massachusetts Institute of Technology.
4  * Copyright (C) 2004-2009 by Daniel Stenberg
5  *
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.
17  */
18
19 #include "setup.h"
20
21 #include "ares.h"
22 #include "ares_library_init.h"
23 #include "ares_private.h"
24
25 /* library-private global and unique instance vars */
26
27 #ifdef WIN32
28 fpGetNetworkParams_t fpGetNetworkParams = ZERO_NULL;
29 fpSystemFunction036_t fpSystemFunction036 = ZERO_NULL;
30 #endif
31
32 /* library-private global vars with source visibility restricted to this file */
33
34 static unsigned int ares_initialized;
35 static int          ares_init_flags;
36
37 #ifdef WIN32
38 static HMODULE hnd_iphlpapi;
39 static HMODULE hnd_advapi32;
40 #endif
41
42
43 static int ares_win32_init(void)
44 {
45 #ifdef WIN32
46
47   hnd_iphlpapi = 0;
48   hnd_iphlpapi = LoadLibrary("iphlpapi.dll");
49   if (!hnd_iphlpapi)
50     return ARES_ELOADIPHLPAPI;
51
52   fpGetNetworkParams = (fpGetNetworkParams_t)
53     GetProcAddress(hnd_iphlpapi, "GetNetworkParams");
54   if (!fpGetNetworkParams)
55     {
56       FreeLibrary(hnd_iphlpapi);
57       return ARES_EADDRGETNETWORKPARAMS;
58     }
59
60   /*
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.
64    */
65
66   hnd_advapi32 = 0;
67   hnd_advapi32 = LoadLibrary("advapi32.dll");
68   if (hnd_advapi32)
69     {
70       fpSystemFunction036 = (fpSystemFunction036_t)
71         GetProcAddress(hnd_advapi32, "SystemFunction036");
72     }
73
74 #endif
75   return ARES_SUCCESS;
76 }
77
78
79 static void ares_win32_cleanup(void)
80 {
81 #ifdef WIN32
82   if (hnd_advapi32)
83     FreeLibrary(hnd_advapi32);
84   if (hnd_iphlpapi)
85     FreeLibrary(hnd_iphlpapi);
86 #endif
87 }
88
89
90 int ares_library_init(int flags)
91 {
92   int res;
93
94   if (ares_initialized)
95     return ARES_SUCCESS;
96   ares_initialized++;
97
98   if (flags & ARES_LIB_INIT_WIN32)
99     {
100       res = ares_win32_init();
101       if (res != ARES_SUCCESS)
102         return res;
103     }
104
105   ares_init_flags = flags;
106
107   return ARES_SUCCESS;
108 }
109
110
111 void ares_library_cleanup(void)
112 {
113   if (!ares_initialized)
114     return;
115   ares_initialized--;
116
117   if (ares_init_flags & ARES_LIB_INIT_WIN32)
118     ares_win32_cleanup();
119
120   ares_init_flags = ARES_LIB_INIT_NONE;
121 }
122
123
124 int ares_library_initialized(void)
125 {
126 #ifdef WIN32
127   if (!ares_initialized)
128     return ARES_ENOTINITIALIZED;
129 #endif
130   return ARES_SUCCESS;
131 }
132
133