18eda7f23231ac143548cee4e38f010d3c80ea2d
[shibboleth/cpp-sp.git] / oncrpc / nt.c
1 /*********************************************************************
2  * RPC for the Windows NT Operating System
3  * 1993 by Martin F. Gergeleit
4  *
5  * RPC for the Windows NT Operating System COMES WITH ABSOLUTELY NO 
6  * WARRANTY, NOR WILL I BE LIABLE FOR ANY DAMAGES INCURRED FROM THE 
7  * USE OF. USE ENTIRELY AT YOUR OWN RISK!!!
8  *********************************************************************/
9
10 #include <rpc/rpc.h>
11 #include <stdio.h>
12 #include <winsock.h>
13
14 WSADATA WSAData;
15
16 static int init = 0;
17
18 CRITICAL_SECTION __thr_mutex;
19
20 int rpc_nt_init(void)
21 {
22     if (init++)
23         return 0;
24         
25     if (WSAStartup(0x0101, &WSAData)) {
26         init = 0;
27         nt_rpc_report("WSAStartup failed\n");
28         WSACleanup();
29         return -1;
30     }
31
32     return 0;
33 }
34
35 int rpc_nt_exit(void)
36 {
37     if (init == 0 || --init > 0)
38         return 0;
39
40     return WSACleanup();
41 }
42
43 BOOL WINAPI DllMain(
44   HINSTANCE hinstDLL,
45   DWORD fdwReason,
46   LPVOID lpvReserved
47 )
48 {
49     switch (fdwReason) {
50         case DLL_PROCESS_ATTACH:
51             __thr_key = TlsAlloc();
52             InitializeCriticalSection(&__thr_mutex);
53             break;
54
55         case DLL_PROCESS_DETACH:
56             TlsFree(__thr_key);
57             DeleteCriticalSection(&__thr_mutex);
58             break;
59
60         case DLL_THREAD_DETACH: {
61             LPVOID ptr=TlsGetValue(__thr_key);
62             if (ptr)
63                 free(ptr);
64             }
65             break;
66     }
67     return TRUE;
68 }
69
70 VOID
71 nt_rpc_report(LPTSTR lpszMsg)
72 {
73     CHAR    chMsg[256];
74     HANDLE  hEventSource;
75     LPTSTR  lpszStrings[2];
76
77     // Use event logging to log the error.
78     //
79     hEventSource = RegisterEventSource(NULL,
80                             TEXT("rpc.dll"));
81
82     sprintf(chMsg, "sunrpc report: %d", GetLastError());
83     lpszStrings[0] = chMsg;
84     lpszStrings[1] = lpszMsg;
85
86     if (hEventSource != NULL) {
87         ReportEvent(hEventSource, // handle of event source
88             EVENTLOG_WARNING_TYPE, // event type
89             0,                    // event category
90             0,                    // event ID
91             NULL,                 // current user's SID
92             2,                    // strings in lpszStrings
93             0,                    // no bytes of raw data
94             lpszStrings,          // array of error strings
95             NULL);                // no raw data
96
97         (VOID) DeregisterEventSource(hEventSource);
98     }
99 }
100