LRN loves slist: Use stack allocation for slist iterator
[oweals/gnunet.git] / src / util / winproc.c
1 /*
2      This file is part of GNUnet.
3      (C) 2001, 2002, 2003, 2004, 2005, 2006 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 2, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file util/winproc.c
23  * @brief Functions for MS Windows
24  * @author Nils Durner
25  */
26
27 #include "platform.h"
28 #include "gnunet_common.h"
29
30 #define DEBUG_WINPROC 0
31
32 #ifdef MINGW
33
34 static HINSTANCE hNTDLL, hIphlpapi, hAdvapi, hNetapi;
35
36 TNtQuerySystemInformation GNNtQuerySystemInformation;
37 TGetIfEntry GNGetIfEntry;
38 TGetIpAddrTable GNGetIpAddrTable;
39 TGetIfTable GNGetIfTable;
40 TOpenSCManager GNOpenSCManager;
41 TCreateService GNCreateService;
42 TCloseServiceHandle GNCloseServiceHandle;
43 TDeleteService GNDeleteService;
44 TRegisterServiceCtrlHandler GNRegisterServiceCtrlHandler;
45 TSetServiceStatus GNSetServiceStatus;
46 TStartServiceCtrlDispatcher GNStartServiceCtrlDispatcher;
47 TControlService GNControlService;
48 TOpenService GNOpenService;
49 TGetBestInterface GNGetBestInterface;
50 TGetAdaptersInfo GGetAdaptersInfo;
51 TNetUserAdd GNNetUserAdd;
52 TNetUserSetInfo GNNetUserSetInfo;
53 TLsaOpenPolicy GNLsaOpenPolicy;
54 TLsaAddAccountRights GNLsaAddAccountRights;
55 TLsaRemoveAccountRights GNLsaRemoveAccountRights;
56 TLsaClose GNLsaClose;
57 TLookupAccountName GNLookupAccountName;
58 TGetFileSecurity GNGetFileSecurity;
59 TInitializeSecurityDescriptor GNInitializeSecurityDescriptor;
60 TGetSecurityDescriptorDacl GNGetSecurityDescriptorDacl;
61 TGetAclInformation GNGetAclInformation;
62 TInitializeAcl GNInitializeAcl;
63 TGetAce GNGetAce;
64 TEqualSid GNEqualSid;
65 TAddAce GNAddAce;
66 TAddAccessAllowedAce GNAddAccessAllowedAce;
67 TSetNamedSecurityInfo GNSetNamedSecurityInfo;
68
69 /**
70  * Log (panic) messages from PlibC
71  */
72 void
73 plibc_panic (int err, char *msg)
74 {
75   LOG (((err ==
76          INT_MAX) ? GNUNET_ERROR_TYPE_DEBUG : GNUNET_ERROR_TYPE_ERROR),
77        "%s", msg);
78 }
79
80 /**
81  * @brief Initialize PlibC and set up Windows environment
82  * @param logging context, NULL means stderr
83  * @return Error code from winerror.h, ERROR_SUCCESS on success
84 */
85 int
86 GNInitWinEnv ()
87 {
88   int ret;
89
90   plibc_initialized ();
91   plibc_set_panic_proc (plibc_panic);
92   ret = plibc_init ("GNU", PACKAGE);
93
94   /* don't load other DLLs twice */
95   if (hNTDLL)
96     return ret;
97
98   hNTDLL = LoadLibrary ("ntdll.dll");
99
100   /* Function to get CPU usage under Win NT */
101   if (hNTDLL)
102     {
103       GNNtQuerySystemInformation =
104         (TNtQuerySystemInformation) GetProcAddress (hNTDLL,
105                                                     "NtQuerySystemInformation");
106     }
107   else
108     {
109       GNNtQuerySystemInformation = NULL;
110     }
111
112   /* Functions to get information about a network adapter */
113   hIphlpapi = LoadLibrary ("iphlpapi.dll");
114   if (hIphlpapi)
115     {
116       GNGetIfEntry = (TGetIfEntry) GetProcAddress (hIphlpapi, "GetIfEntry");
117       GNGetIpAddrTable =
118         (TGetIpAddrTable) GetProcAddress (hIphlpapi, "GetIpAddrTable");
119       GNGetIfTable = (TGetIfTable) GetProcAddress (hIphlpapi, "GetIfTable");
120       GNGetBestInterface =
121         (TGetBestInterface) GetProcAddress (hIphlpapi, "GetBestInterface");
122       GGetAdaptersInfo =
123         (TGetAdaptersInfo) GetProcAddress (hIphlpapi, "GetAdaptersInfo");
124     }
125   else
126     {
127       GNGetIfEntry = NULL;
128       GNGetIpAddrTable = NULL;
129       GNGetIfTable = NULL;
130       GNGetBestInterface = NULL;
131       GGetAdaptersInfo = NULL;
132     }
133
134   /* Service & Account functions */
135   hAdvapi = LoadLibrary ("advapi32.dll");
136   if (hAdvapi)
137     {
138       GNOpenSCManager =
139         (TOpenSCManager) GetProcAddress (hAdvapi, "OpenSCManagerA");
140       GNCreateService =
141         (TCreateService) GetProcAddress (hAdvapi, "CreateServiceA");
142       GNCloseServiceHandle =
143         (TCloseServiceHandle) GetProcAddress (hAdvapi, "CloseServiceHandle");
144       GNDeleteService =
145         (TDeleteService) GetProcAddress (hAdvapi, "DeleteService");
146       GNRegisterServiceCtrlHandler =
147         (TRegisterServiceCtrlHandler) GetProcAddress (hAdvapi,
148                                                       "RegisterServiceCtrlHandlerA");
149       GNSetServiceStatus =
150         (TSetServiceStatus) GetProcAddress (hAdvapi, "SetServiceStatus");
151       GNStartServiceCtrlDispatcher =
152         (TStartServiceCtrlDispatcher) GetProcAddress (hAdvapi,
153                                                       "StartServiceCtrlDispatcherA");
154       GNControlService =
155         (TControlService) GetProcAddress (hAdvapi, "ControlService");
156       GNOpenService = (TOpenService) GetProcAddress (hAdvapi, "OpenServiceA");
157
158       GNLsaOpenPolicy =
159         (TLsaOpenPolicy) GetProcAddress (hAdvapi, "LsaOpenPolicy");
160       GNLsaAddAccountRights =
161         (TLsaAddAccountRights) GetProcAddress (hAdvapi,
162                                                "LsaAddAccountRights");
163       GNLsaRemoveAccountRights =
164         (TLsaRemoveAccountRights) GetProcAddress (hAdvapi,
165                                                   "LsaRemoveAccountRights");
166       GNLsaClose = (TLsaClose) GetProcAddress (hAdvapi, "LsaClose");
167       GNLookupAccountName =
168         (TLookupAccountName) GetProcAddress (hAdvapi, "LookupAccountNameA");
169
170       GNGetFileSecurity =
171         (TGetFileSecurity) GetProcAddress (hAdvapi, "GetFileSecurityA");
172       GNInitializeSecurityDescriptor =
173         (TInitializeSecurityDescriptor) GetProcAddress (hAdvapi,
174                                                         "InitializeSecurityDescriptor");
175       GNGetSecurityDescriptorDacl =
176         (TGetSecurityDescriptorDacl) GetProcAddress (hAdvapi,
177                                                      "GetSecurityDescriptorDacl");
178       GNGetAclInformation =
179         (TGetAclInformation) GetProcAddress (hAdvapi, "GetAclInformation");
180       GNInitializeAcl =
181         (TInitializeAcl) GetProcAddress (hAdvapi, "InitializeAcl");
182       GNGetAce = (TGetAce) GetProcAddress (hAdvapi, "GetAce");
183       GNEqualSid = (TEqualSid) GetProcAddress (hAdvapi, "EqualSid");
184       GNAddAce = (TAddAce) GetProcAddress (hAdvapi, "AddAce");
185       GNAddAccessAllowedAce =
186         (TAddAccessAllowedAce) GetProcAddress (hAdvapi,
187                                                "AddAccessAllowedAce");
188       GNSetNamedSecurityInfo =
189         (TSetNamedSecurityInfo) GetProcAddress (hAdvapi,
190                                                 "SetNamedSecurityInfoA");
191     }
192   else
193     {
194       GNOpenSCManager = NULL;
195       GNCreateService = NULL;
196       GNCloseServiceHandle = NULL;
197       GNDeleteService = NULL;
198       GNRegisterServiceCtrlHandler = NULL;
199       GNSetServiceStatus = NULL;
200       GNStartServiceCtrlDispatcher = NULL;
201       GNControlService = NULL;
202       GNOpenService = NULL;
203
204       GNLsaOpenPolicy = NULL;
205       GNLsaAddAccountRights = NULL;
206       GNLsaRemoveAccountRights = NULL;
207       GNLsaClose = NULL;
208       GNLookupAccountName = NULL;
209
210       GNGetFileSecurity = NULL;
211       GNInitializeSecurityDescriptor = NULL;
212       GNGetSecurityDescriptorDacl = NULL;
213       GNGetAclInformation = NULL;
214       GNInitializeAcl = NULL;
215       GNGetAce = NULL;
216       GNEqualSid = NULL;
217       GNAddAce = NULL;
218       GNAddAccessAllowedAce = NULL;
219       GNSetNamedSecurityInfo = NULL;
220     }
221
222   /* Account function */
223   hNetapi = LoadLibrary ("netapi32.dll");
224   if (hNetapi)
225     {
226       GNNetUserAdd = (TNetUserAdd) GetProcAddress (hNetapi, "NetUserAdd");
227       GNNetUserSetInfo =
228         (TNetUserSetInfo) GetProcAddress (hNetapi, "NetUserSetInfo");
229     }
230   else
231     {
232       GNNetUserAdd = NULL;
233       GNNetUserSetInfo = NULL;
234     }
235
236   return ret;
237 }
238
239 /**
240  * Clean up Windows environment
241  */
242 void
243 GNShutdownWinEnv ()
244 {
245   plibc_shutdown ();
246
247   FreeLibrary (hNTDLL);
248   FreeLibrary (hIphlpapi);
249   FreeLibrary (hAdvapi);
250   FreeLibrary (hNetapi);
251
252   CoUninitialize ();
253 }
254
255 #endif /* MINGW */
256
257 #if !HAVE_ATOLL
258 long long
259 atoll (const char *nptr)
260 {
261   return atol (nptr);
262 }
263 #endif