Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

295 lines
7.0 KiB

  1. // Copyright (c) 1997-2001 Microsoft Corporation, All Rights Reserved
  2. #include "precomp.h"
  3. #include <provexpt.h>
  4. #include <snmpstd.h>
  5. #include <snmptempl.h>
  6. #include <snmpmt.h>
  7. #include <typeinfo.h>
  8. #include <process.h>
  9. #include <objbase.h>
  10. #include <stdio.h>
  11. #include <winsock.h>
  12. #include <snmpcont.h>
  13. #include <snmpevt.h>
  14. #include <snmpthrd.h>
  15. #include <snmplog.h>
  16. #include "common.h"
  17. #include "startup.h"
  18. #include "sync.h"
  19. #include "dummy.h"
  20. #include "flow.h"
  21. #include "reg.h"
  22. #include "frame.h"
  23. #include "timer.h"
  24. #include "message.h"
  25. #include "tsent.h"
  26. #include "transp.h"
  27. #include "vblist.h"
  28. #include "sec.h"
  29. #include "pdu.h"
  30. #include "ssent.h"
  31. #include "idmap.h"
  32. #include "opreg.h"
  33. #include "session.h"
  34. #include "pseudo.h"
  35. #include "fs_reg.h"
  36. #include "ophelp.h"
  37. #include "op.h"
  38. #include <winsock.h>
  39. #include "trap.h"
  40. CRITICAL_SECTION s_CriticalSection ;
  41. LONG SnmpClassLibrary :: s_ReferenceCount = 0 ;
  42. BOOL SnmpClassLibrary :: Startup ()
  43. {
  44. EnterCriticalSection ( & s_CriticalSection ) ;
  45. DebugMacro4(
  46. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  47. __FILE__,__LINE__,
  48. L"SnmpClassLibrary :: Startup, s_ReferenceCount = %lx\n" , s_ReferenceCount
  49. ) ;
  50. )
  51. BOOL status = TRUE ;
  52. s_ReferenceCount ++ ;
  53. if ( s_ReferenceCount == 1 )
  54. {
  55. DebugMacro4(
  56. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  57. __FILE__,__LINE__,
  58. L"Creating library resources\n"
  59. ) ;
  60. )
  61. status = FALSE ;
  62. SnmpThreadObject :: Startup () ;
  63. try
  64. {
  65. SnmpDebugLog :: Startup () ;
  66. }
  67. catch ( ... )
  68. {
  69. // we do not want to left s_Reference count up
  70. SnmpThreadObject :: Closedown () ;
  71. s_ReferenceCount -- ;
  72. LeaveCriticalSection ( & s_CriticalSection ) ;
  73. throw;
  74. }
  75. try
  76. {
  77. Timer::g_timerThread = new SnmpClThreadObject ;
  78. }
  79. catch ( ... )
  80. {
  81. // we do not want to left s_Reference count up
  82. SnmpDebugLog :: Closedown () ;
  83. SnmpThreadObject :: Closedown () ;
  84. s_ReferenceCount -- ;
  85. LeaveCriticalSection ( & s_CriticalSection ) ;
  86. throw;
  87. }
  88. try
  89. {
  90. Timer::g_timerThread->BeginThread () ;
  91. Timer::g_timerThread->WaitForStartup () ;
  92. WORD wVersionRequested;
  93. WSADATA wsaData;
  94. wVersionRequested = MAKEWORD(1, 1);
  95. status = ( WSAStartup ( wVersionRequested , &wsaData ) == 0 ) ;
  96. if ( status )
  97. {
  98. BOOL WindowStatus = FALSE ;
  99. BOOL TimerStatus = FALSE ;
  100. BOOL EncodeStatus = FALSE ;
  101. try
  102. {
  103. WindowStatus = Window::InitializeStaticComponents () ;
  104. TimerStatus = Timer::InitializeStaticComponents () ;
  105. EncodeStatus = SnmpEncodeDecode :: InitializeStaticComponents () ;
  106. status = WindowStatus & TimerStatus & EncodeStatus ;
  107. if ( status )
  108. {
  109. SnmpTrapManager ::s_TrapMngrPtr = NULL ;
  110. try
  111. {
  112. SnmpTrapManager ::s_TrapMngrPtr = new SnmpTrapManager();
  113. status = SnmpTrapManager ::s_TrapMngrPtr ? TRUE : FALSE ;
  114. }
  115. catch ( ... )
  116. {
  117. delete SnmpTrapManager ::s_TrapMngrPtr ;
  118. SnmpTrapManager ::s_TrapMngrPtr = NULL ;
  119. status = FALSE ;
  120. }
  121. }
  122. }
  123. catch ( ... )
  124. {
  125. status = FALSE ;
  126. }
  127. if ( ! status )
  128. {
  129. if ( TimerStatus )
  130. {
  131. Timer::DestroyStaticComponents();
  132. }
  133. if ( WindowStatus )
  134. {
  135. Window::DestroyStaticComponents();
  136. }
  137. if ( EncodeStatus )
  138. {
  139. SnmpEncodeDecode :: DestroyStaticComponents() ;
  140. }
  141. WSACleanup () ;
  142. }
  143. }
  144. }
  145. catch ( ... )
  146. {
  147. status = FALSE ;
  148. }
  149. if ( ! status )
  150. {
  151. Timer::g_timerThread->SignalThreadShutdown () ;
  152. Timer::g_timerThread = NULL ;
  153. }
  154. if ( ! status )
  155. {
  156. SnmpDebugLog :: Closedown () ;
  157. SnmpThreadObject :: Closedown () ;
  158. s_ReferenceCount -- ;
  159. }
  160. }
  161. LeaveCriticalSection ( & s_CriticalSection ) ;
  162. return status ;
  163. }
  164. void SnmpClassLibrary :: Closedown ()
  165. {
  166. EnterCriticalSection ( & s_CriticalSection ) ;
  167. DebugMacro4(
  168. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  169. __FILE__,__LINE__,
  170. L"SnmpClassLibrary :: Closedown, s_ReferenceCount = %lx\n" , s_ReferenceCount
  171. ) ;
  172. )
  173. if ( InterlockedDecrement ( & s_ReferenceCount ) <= 0 )
  174. {
  175. if ( Timer::g_timerThread )
  176. {
  177. Timer::g_timerThread->SignalThreadShutdown () ;
  178. Timer::g_timerThread = NULL ;
  179. }
  180. if ( SnmpTrapManager ::s_TrapMngrPtr )
  181. {
  182. delete SnmpTrapManager ::s_TrapMngrPtr ;
  183. SnmpTrapManager ::s_TrapMngrPtr = NULL;
  184. }
  185. SnmpDebugLog :: Closedown () ;
  186. SnmpThreadObject :: Closedown () ;
  187. Timer::DestroyStaticComponents();
  188. Window::DestroyStaticComponents();
  189. SnmpEncodeDecode :: DestroyStaticComponents() ;
  190. SnmpCleanup();
  191. WSACleanup () ;
  192. }
  193. LeaveCriticalSection ( & s_CriticalSection ) ;
  194. }
  195. HINSTANCE g_hInst = NULL ;
  196. //***************************************************************************
  197. //
  198. // LibMain32
  199. //
  200. // Purpose: Entry point for DLL. Good place for initialization.
  201. // Return: TRUE if OK.
  202. //***************************************************************************
  203. BOOL APIENTRY DllMain (
  204. HINSTANCE hInstance,
  205. ULONG ulReason ,
  206. LPVOID pvReserved
  207. )
  208. {
  209. g_hInst=hInstance;
  210. BOOL status = TRUE ;
  211. SetStructuredExceptionHandler seh;
  212. try
  213. {
  214. if ( DLL_PROCESS_DETACH == ulReason )
  215. {
  216. DeleteCriticalSection ( & s_CriticalSection ) ;
  217. }
  218. else if ( DLL_PROCESS_ATTACH == ulReason )
  219. {
  220. InitializeCriticalSection ( & s_CriticalSection ) ;
  221. DisableThreadLibraryCalls(hInstance); // 158024
  222. }
  223. else if ( DLL_THREAD_DETACH == ulReason )
  224. {
  225. }
  226. else if ( DLL_THREAD_ATTACH == ulReason )
  227. {
  228. }
  229. }
  230. catch(Structured_Exception e_SE)
  231. {
  232. status = FALSE;
  233. }
  234. catch(Heap_Exception e_HE)
  235. {
  236. status = FALSE;
  237. }
  238. return status ;
  239. }