1 /* NFSv4.1 client for Windows
2 * Copyright � 2012 The Regents of the University of Michigan
3 *
4 * Olga Kornievskaia <aglo@umich.edu>
5 * Casey Bodley <cbodley@umich.edu>
6 *
7 * This library is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation; either version 2.1 of the License, or (at
10 * your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful, but
13 * without any warranty; without even the implied warranty of merchantability
14 * or fitness for a particular purpose. See the GNU Lesser General Public
15 * License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this library; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 */
21
22 #include <wintirpc.h>
23 #include <rpc/rpc.h>
24 #include <stdio.h>
25 #ifndef __REACTOS__
26 #include <winsock.h>
27 #else
28 #include <winsock2.h>
29 #endif
30
31 WSADATA WSAData;
32
33 static int init = 0;
34 static DWORD dwTlsIndex;
35
36 extern void multithread_init(void);
37
38 VOID
tirpc_report(LPTSTR lpszMsg)39 tirpc_report(LPTSTR lpszMsg)
40 {
41 WCHAR chMsg[256];
42 HANDLE hEventSource;
43 LPCWSTR lpszStrings[2];
44
45 // Use event logging to log the error.
46 //
47 hEventSource = RegisterEventSource(NULL,
48 TEXT("tirpc.dll"));
49
50 #ifndef __REACTOS__
51 swprintf_s(chMsg, sizeof(chMsg), L"tirpc report: %d", GetLastError());
52 #else
53 _snwprintf(chMsg, sizeof(chMsg) / sizeof(WCHAR), L"tirpc report: %d", GetLastError());
54 #endif
55 lpszStrings[0] = (LPCWSTR)chMsg;
56 lpszStrings[1] = lpszMsg;
57
58 if (hEventSource != NULL) {
59 ReportEvent(hEventSource, // handle of event source
60 EVENTLOG_WARNING_TYPE, // event type
61 0, // event category
62 0, // event ID
63 NULL, // current user's SID
64 2, // strings in lpszStrings
65 0, // no bytes of raw data
66 lpszStrings, // array of error strings
67 NULL); // no raw data
68
69 (VOID) DeregisterEventSource(hEventSource);
70 }
71 }
72
tirpc_criticalsection_init(void)73 void tirpc_criticalsection_init(void) {
74 multithread_init();
75 }
76
winsock_init(void)77 BOOL winsock_init(void)
78 {
79 int err;
80 err = WSAStartup(MAKEWORD( 3, 3 ), &WSAData); // XXX THIS SHOULD BE FAILING!!!!!!!!!!!!!!!!!
81 if (err != 0) {
82 init = 0;
83 tirpc_report(L"WSAStartup failed!\n");
84 WSACleanup();
85 return FALSE;
86 }
87 return TRUE;
88 }
89
winsock_fini(void)90 BOOL winsock_fini(void)
91 {
92 WSACleanup();
93 return TRUE;
94 }
95
96 #ifdef __REACTOS__
97 char NETCONFIG[MAX_PATH] = "";
98 #endif
DllMain(HINSTANCE hinstDLL,DWORD fdwReason,LPVOID lpvReserved)99 BOOL WINAPI DllMain/*tirpc_main*/(HINSTANCE hinstDLL, // DLL module handle
100 DWORD fdwReason, // reason called
101 LPVOID lpvReserved) // reserved
102 {
103 LPVOID lpvData;
104 BOOL fIgnore;
105
106 // if (init++)
107 // return TRUE;
108
109 // Deal with Thread Local Storage initialization!!
110 switch (fdwReason)
111 {
112 // The DLL is loading due to process
113 // initialization or a call to LoadLibrary.
114 case DLL_PROCESS_ATTACH:
115 #ifdef __REACTOS__
116 if (!GetSystemDirectoryA(NETCONFIG, ARRAYSIZE(NETCONFIG)))
117 return FALSE;
118
119 lstrcatA(NETCONFIG, "\\drivers\\etc\\netconfig");
120 #endif
121
122 // Initialize socket library
123 if (winsock_init() == FALSE)
124 return FALSE;
125
126 // Initialize CriticalSections
127 tirpc_criticalsection_init();
128
129 // Allocate a TLS index.
130 if ((dwTlsIndex = TlsAlloc()) == TLS_OUT_OF_INDEXES)
131 return FALSE;
132
133 // No break: Initialize the index for first thread.
134
135 // The attached process creates a new thread.
136 case DLL_THREAD_ATTACH:
137
138 // Initialize the TLS index for this thread
139 lpvData = (LPVOID) LocalAlloc(LPTR, 256);
140 if (lpvData != NULL)
141 fIgnore = TlsSetValue(dwTlsIndex, lpvData);
142
143 break;
144
145 // The thread of the attached process terminates.
146 case DLL_THREAD_DETACH:
147
148 // Release the allocated memory for this thread.
149 lpvData = TlsGetValue(dwTlsIndex);
150 if (lpvData != NULL)
151 LocalFree((HLOCAL) lpvData);
152
153 break;
154
155 // DLL unload due to process termination or FreeLibrary.
156 case DLL_PROCESS_DETACH:
157
158 // Release the allocated memory for this thread.
159 lpvData = TlsGetValue(dwTlsIndex);
160 if (lpvData != NULL)
161 LocalFree((HLOCAL) lpvData);
162
163 // Release the TLS index.
164 TlsFree(dwTlsIndex);
165
166 // Clean up winsock stuff
167 winsock_fini();
168
169 break;
170
171 default:
172 break;
173 }
174
175
176 return TRUE;
177 }
178
tirpc_exit(void)179 int tirpc_exit(void)
180 {
181 if (init == 0 || --init > 0)
182 return 0;
183
184 return WSACleanup();
185 }
186
187
wintirpc_debug(char * fmt,...)188 void wintirpc_debug(char *fmt, ...)
189 {
190 #ifdef _DEBUG
191 char buffer[2048];
192 #else
193 static int triedToOpen = 0;
194 static FILE *dbgFile = NULL;
195 #endif
196
197 va_list vargs;
198 va_start(vargs, fmt);
199
200 #ifdef _DEBUG
201 vsprintf(buffer, fmt, vargs);
202 OutputDebugStringA(buffer);
203 #else
204 if (dbgFile == NULL && triedToOpen == 0) {
205 triedToOpen = 1;
206 dbgFile = fopen("c:\\etc\\rpcsec_gss_debug.txt", "w");
207 }
208 if (dbgFile != NULL) {
209 vfprintf(dbgFile, fmt, vargs);
210 fflush(dbgFile);
211 }
212 #endif
213
214 va_end(vargs);
215 }
216