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.

577 lines
13 KiB

  1. // Copyright (c) 1997-2001 Microsoft Corporation, All Rights Reserved
  2. #include "precomp.h"
  3. #include "common.h"
  4. #include "address.h"
  5. #include "timer.h"
  6. #include "sec.h"
  7. #include "dummy.h"
  8. #include "flow.h"
  9. #include "frame.h"
  10. #include "ssent.h"
  11. #include "idmap.h"
  12. #include "opreg.h"
  13. #include "session.h"
  14. #include "vblist.h"
  15. #include "ophelp.h"
  16. #include "window.h"
  17. #include "trap.h"
  18. #include "trapsess.h"
  19. SnmpTrapManager *SnmpTrapManager ::s_TrapMngrPtr = NULL ;
  20. SnmpTrapTaskObject::SnmpTrapTaskObject(SnmpTrapManager* managerPtr,
  21. SnmpWinSnmpTrapSession** pptrapsess)
  22. {
  23. DebugMacro4(
  24. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  25. __FILE__,__LINE__,
  26. L"SnmpTrapTaskObject::SnmpTrapTaskObject: Creating a new trap task object\n"
  27. ) ;
  28. )
  29. m_mptr = managerPtr;
  30. m_pptrapsess = pptrapsess;
  31. DebugMacro4(
  32. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  33. __FILE__,__LINE__,
  34. L"SnmpTrapTaskObject::SnmpTrapTaskObject: Created a new trap task object\n"
  35. ) ;
  36. )
  37. }
  38. void SnmpTrapTaskObject::Process ()
  39. {
  40. DebugMacro4(
  41. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  42. __FILE__,__LINE__,
  43. L"SnmpTrapTaskObject::Process: Entering...\n"
  44. ) ;
  45. )
  46. try
  47. {
  48. *m_pptrapsess = new SnmpWinSnmpTrapSession(m_mptr);
  49. if (NULL == (**m_pptrapsess)())
  50. {
  51. (*m_pptrapsess)->DestroySession();
  52. *m_pptrapsess = NULL;
  53. }
  54. }
  55. catch ( Heap_Exception e_HE )
  56. {
  57. *m_pptrapsess = NULL;
  58. }
  59. Complete();
  60. DebugMacro4(
  61. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  62. __FILE__,__LINE__,
  63. L"SnmpTrapTaskObject::Process: Exiting\n"
  64. ) ;
  65. )
  66. }
  67. SnmpTrapManager::SnmpTrapManager() : m_trapThread ( NULL ), m_trapSession ( NULL )
  68. {
  69. DebugMacro4(
  70. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  71. __FILE__,__LINE__,
  72. L"SnmpTrapManager::SnmpTrapManager: Entering...\n"
  73. ) ;
  74. )
  75. m_bListening = FALSE;
  76. m_trapThread = new SnmpClTrapThreadObject;
  77. m_trapThread->BeginThread();
  78. DebugMacro4(
  79. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  80. __FILE__,__LINE__,
  81. L"SnmpTrapManager::SnmpTrapManager: Exiting\n"
  82. ) ;
  83. )
  84. }
  85. SnmpTrapManager::~SnmpTrapManager()
  86. {
  87. DebugMacro4(
  88. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  89. __FILE__,__LINE__,
  90. L"SnmpTrapManager::~SnmpTrapManager: Entering...\n"
  91. ) ;
  92. )
  93. if (NULL != m_trapSession)
  94. {
  95. DebugMacro4(
  96. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  97. __FILE__,__LINE__,
  98. L"SnmpTrapManager::~SnmpTrapManager: Destroy the session\n"
  99. ) ;
  100. )
  101. m_trapSession->DestroySession();
  102. }
  103. if ( NULL != m_trapThread )
  104. {
  105. DebugMacro4(
  106. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  107. __FILE__,__LINE__,
  108. L"SnmpTrapManager::~SnmpTrapManager: Kill the thread\n"
  109. ) ;
  110. )
  111. m_trapThread->SignalThreadShutdown();
  112. }
  113. DebugMacro4(
  114. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  115. __FILE__,__LINE__,
  116. L"SnmpTrapManager::~SnmpTrapManager: Exiting\n"
  117. ) ;
  118. )
  119. }
  120. BOOL SnmpTrapManager::RegisterReceiver(SnmpTrapReceiver *trapRx)
  121. {
  122. DebugMacro4(
  123. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  124. __FILE__,__LINE__,
  125. L"SnmpTrapManager::RegisterReceiver: Entering...\n"
  126. ) ;
  127. )
  128. if (NULL == trapRx)
  129. {
  130. DebugMacro4(
  131. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  132. __FILE__,__LINE__,
  133. L"SnmpTrapManager::RegisterReceiver: Exiting with FALSE, invalid argument\n"
  134. ) ;
  135. )
  136. return FALSE;
  137. }
  138. if (!m_bListening)
  139. {
  140. SnmpTrapTaskObject trap_task = SnmpTrapTaskObject(this, &m_trapSession);
  141. m_trapThread->ScheduleTask(trap_task);
  142. trap_task.Exec();
  143. trap_task.Wait();
  144. m_trapThread->ReapTask(trap_task);
  145. if (NULL == m_trapSession)
  146. {
  147. DebugMacro4(
  148. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  149. __FILE__,__LINE__,
  150. L"SnmpTrapManager::RegisterReceiver: Exiting with FALSE, invalid trap session\n"
  151. ) ;
  152. )
  153. return FALSE;
  154. }
  155. m_bListening = TRUE;
  156. }
  157. BOOL bRet = m_receivers.Add(trapRx);
  158. DebugMacro4(
  159. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  160. __FILE__,__LINE__,
  161. L"SnmpTrapManager::RegisterReceiver: Exiting with %s\n" ,
  162. bRet ? L"TRUE" : L"FALSE, failed to add to store of receivers"
  163. ) ;
  164. )
  165. return bRet;
  166. }
  167. BOOL SnmpTrapManager::UnRegisterReceiver (SnmpTrapReceiver *trapRx)
  168. {
  169. DebugMacro4(
  170. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  171. __FILE__,__LINE__,
  172. L"SnmpTrapManager::UnRegisterReceiver: Entering...\n"
  173. ) ;
  174. )
  175. if (!m_bListening || (NULL == trapRx))
  176. {
  177. DebugMacro4(
  178. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  179. __FILE__,__LINE__,
  180. L"SnmpTrapManager::UnRegisterReceiver: Exiting with FALSE, invalid trap session\n"
  181. ) ;
  182. )
  183. return FALSE;
  184. }
  185. BOOL bRet = m_receivers.Delete(trapRx);
  186. if (bRet && m_receivers.IsEmpty())
  187. {
  188. DebugMacro4(
  189. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  190. __FILE__,__LINE__,
  191. L"SnmpTrapManager::UnRegisterReceiver: Destroy trap session, no more receivers\n"
  192. ) ;
  193. )
  194. m_trapSession->DestroySession();
  195. m_trapSession = NULL;
  196. m_bListening = FALSE;
  197. }
  198. DebugMacro4(
  199. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  200. __FILE__,__LINE__,
  201. L"SnmpTrapManager::UnRegisterReceiver: Exiting with %s\n" ,
  202. bRet ? L"TRUE" : L"FALSE, failed to remove from store of receivers"
  203. ) ;
  204. )
  205. return bRet;
  206. }
  207. SnmpTrapReceiver::SnmpTrapReceiver() : m_cRef ( 1 ) , m_bregistered ( false )
  208. {
  209. DebugMacro4(
  210. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  211. __FILE__,__LINE__,
  212. L"SnmpTrapReceiver::SnmpTrapReceiver: Creating a new SnmpTrapReceiver\n"
  213. ) ;
  214. )
  215. m_bregistered = SnmpTrapManager ::s_TrapMngrPtr->RegisterReceiver(this);
  216. DebugMacro4(
  217. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  218. __FILE__,__LINE__,
  219. L"SnmpTrapReceiver::SnmpTrapReceiver: %s this trap receiver\n" ,
  220. m_bregistered ? L"Succeessfully registered" : L"Failed to register"
  221. ) ;
  222. )
  223. }
  224. SnmpTrapReceiver::~SnmpTrapReceiver()
  225. {
  226. DebugMacro4(
  227. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  228. __FILE__,__LINE__,
  229. L"SnmpTrapReceiver::~SnmpTrapReceiver: deleted this trap receiver\n"
  230. ) ;
  231. )
  232. }
  233. BOOL SnmpTrapReceiver::DestroyReceiver()
  234. {
  235. if (0 != InterlockedDecrement(&m_cRef))
  236. {
  237. return FALSE;
  238. }
  239. if (m_bregistered)
  240. {
  241. SnmpTrapManager ::s_TrapMngrPtr->UnRegisterReceiver(this);
  242. m_bregistered = FALSE;
  243. }
  244. DebugMacro4(
  245. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  246. __FILE__,__LINE__,
  247. L"SnmpTrapReceiver::DestroyReceiver: Destroyed and unregistered this trap receiver\n"
  248. ) ;
  249. )
  250. delete this;
  251. return TRUE;
  252. }
  253. SnmpTrapReceiverStore::SnmpTrapReceiverStore() : m_HandledRxStack ( NULL ), m_UnHandledRxStack ( NULL )
  254. {
  255. DebugMacro4(
  256. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  257. __FILE__,__LINE__,
  258. L"SnmpTrapReceiverStore::SnmpTrapReceiverStore: Created store\n"
  259. ) ;
  260. )
  261. try {
  262. m_HandledRxStack = (void *)(new CList<SnmpTrapReceiver*, SnmpTrapReceiver*>);
  263. m_UnHandledRxStack = (void *)(new CList<SnmpTrapReceiver*, SnmpTrapReceiver*>);
  264. InitializeCriticalSection(&m_Lock);
  265. } catch( ... ) { //if there is any exception, wind back and don't leak.
  266. if (m_HandledRxStack)
  267. {
  268. delete ((CList<SnmpTrapReceiver*, SnmpTrapReceiver*>*)m_HandledRxStack);
  269. m_HandledRxStack = NULL;
  270. }
  271. if (m_UnHandledRxStack)
  272. {
  273. delete ((CList<SnmpTrapReceiver*, SnmpTrapReceiver*>*)m_UnHandledRxStack);
  274. m_UnHandledRxStack = NULL;
  275. }
  276. throw ;
  277. }
  278. }
  279. BOOL SnmpTrapReceiverStore::Add(SnmpTrapReceiver* receiver)
  280. {
  281. DebugMacro4(
  282. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  283. __FILE__,__LINE__,
  284. L"SnmpTrapReceiverStore::Add: Entering...\n"
  285. ) ;
  286. )
  287. Lock();
  288. ScopeGuard t_1 = MakeObjGuard ( *this , Unlock ) ;
  289. BOOL bRet = FALSE;
  290. if (((CList<SnmpTrapReceiver*, SnmpTrapReceiver*>*)m_HandledRxStack)->IsEmpty())
  291. {
  292. bRet = (NULL != ((CList<SnmpTrapReceiver*, SnmpTrapReceiver*>*)m_UnHandledRxStack)->AddTail(receiver));
  293. }
  294. else
  295. {
  296. bRet = (NULL != ((CList<SnmpTrapReceiver*, SnmpTrapReceiver*>*)m_HandledRxStack)->AddTail(receiver));
  297. }
  298. DebugMacro4(
  299. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  300. __FILE__,__LINE__,
  301. L"SnmpTrapReceiverStore::Add: Exiting with %s...\n",
  302. bRet ? L"TRUE" : L"FALSE"
  303. ) ;
  304. )
  305. return bRet;
  306. }
  307. BOOL SnmpTrapReceiverStore::Delete(SnmpTrapReceiver* receiver)
  308. {
  309. DebugMacro4(
  310. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  311. __FILE__,__LINE__,
  312. L"SnmpTrapReceiverStore::Delete: Entering...\n"
  313. ) ;
  314. )
  315. Lock();
  316. //first check the m_HandledRxStack
  317. POSITION pos = ((CList<SnmpTrapReceiver*, SnmpTrapReceiver*>*)m_HandledRxStack)->GetHeadPosition();
  318. while(NULL != pos)
  319. {
  320. POSITION delpos = pos;
  321. SnmpTrapReceiver* rx = ((CList<SnmpTrapReceiver*, SnmpTrapReceiver*>*)m_HandledRxStack)->GetNext(pos);
  322. if (rx == receiver)
  323. {
  324. ((CList<SnmpTrapReceiver*, SnmpTrapReceiver*>*)m_HandledRxStack)->RemoveAt(delpos);
  325. Unlock();
  326. DebugMacro4(
  327. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  328. __FILE__,__LINE__,
  329. L"SnmpTrapReceiverStore::Delete: Exiting with TRUE\n"
  330. ) ;
  331. )
  332. return TRUE;
  333. }
  334. }
  335. //now check the m_UnHandledRxStack
  336. pos = ((CList<SnmpTrapReceiver*, SnmpTrapReceiver*>*)m_UnHandledRxStack)->GetHeadPosition();
  337. while(NULL != pos)
  338. {
  339. POSITION delpos = pos;
  340. SnmpTrapReceiver* rx = ((CList<SnmpTrapReceiver*, SnmpTrapReceiver*>*)m_UnHandledRxStack)->GetNext(pos);
  341. if (rx == receiver)
  342. {
  343. ((CList<SnmpTrapReceiver*, SnmpTrapReceiver*>*)m_UnHandledRxStack)->RemoveAt(delpos);
  344. Unlock();
  345. DebugMacro4(
  346. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  347. __FILE__,__LINE__,
  348. L"SnmpTrapReceiverStore::Delete: Exiting with TRUE\n"
  349. ) ;
  350. )
  351. return TRUE;
  352. }
  353. }
  354. Unlock();
  355. DebugMacro4(
  356. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  357. __FILE__,__LINE__,
  358. L"SnmpTrapReceiverStore::Delete: Exiting with FALSE\n"
  359. ) ;
  360. )
  361. return FALSE;
  362. }
  363. void SnmpTrapReceiverStore::Lock()
  364. {
  365. EnterCriticalSection(&m_Lock);
  366. }
  367. void SnmpTrapReceiverStore::Unlock()
  368. {
  369. LeaveCriticalSection(&m_Lock);
  370. }
  371. BOOL SnmpTrapReceiverStore::IsEmpty()
  372. {
  373. Lock();
  374. BOOL ret = ((CList<SnmpTrapReceiver*, SnmpTrapReceiver*>*)m_HandledRxStack)->IsEmpty() && ((CList<SnmpTrapReceiver*, SnmpTrapReceiver*>*)m_UnHandledRxStack)->IsEmpty();
  375. Unlock();
  376. return ret;
  377. }
  378. SnmpTrapReceiver* SnmpTrapReceiverStore::GetNext()
  379. {
  380. SnmpTrapReceiver* ret = NULL;
  381. Lock();
  382. if (!((CList<SnmpTrapReceiver*, SnmpTrapReceiver*>*)m_UnHandledRxStack)->IsEmpty())
  383. {
  384. ret = ((CList<SnmpTrapReceiver*, SnmpTrapReceiver*>*)m_UnHandledRxStack)->RemoveHead();
  385. ((CList<SnmpTrapReceiver*, SnmpTrapReceiver*>*)m_HandledRxStack)->AddTail(ret);
  386. }
  387. else
  388. {
  389. CList<SnmpTrapReceiver*, SnmpTrapReceiver*> *tmp = ((CList<SnmpTrapReceiver*, SnmpTrapReceiver*>*)m_UnHandledRxStack);
  390. m_UnHandledRxStack = m_HandledRxStack;
  391. m_HandledRxStack = (void*)tmp;
  392. }
  393. Unlock();
  394. return ret;
  395. }
  396. SnmpTrapReceiverStore::~SnmpTrapReceiverStore()
  397. {
  398. DebugMacro4(
  399. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  400. __FILE__,__LINE__,
  401. L"SnmpTrapReceiverStore::~SnmpTrapReceiverStore: Deleting store\n"
  402. ) ;
  403. )
  404. DeleteCriticalSection(&m_Lock);
  405. if (m_HandledRxStack)
  406. {
  407. ((CList<SnmpTrapReceiver*, SnmpTrapReceiver*>*)m_HandledRxStack)->RemoveAll();
  408. delete ((CList<SnmpTrapReceiver*, SnmpTrapReceiver*>*)m_HandledRxStack);
  409. m_HandledRxStack = NULL;
  410. }
  411. if (m_UnHandledRxStack)
  412. {
  413. ((CList<SnmpTrapReceiver*, SnmpTrapReceiver*>*)m_UnHandledRxStack)->RemoveAll();
  414. delete ((CList<SnmpTrapReceiver*, SnmpTrapReceiver*>*)m_UnHandledRxStack);
  415. m_UnHandledRxStack = NULL;
  416. }
  417. }