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.

472 lines
14 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. SnmpWinSnmpTrapSession::SnmpWinSnmpTrapSession(SnmpTrapManager* managerPtr)
  20. {
  21. DebugMacro4(
  22. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  23. __FILE__,__LINE__,
  24. L"SnmpWinSnmpTrapSession::SnmpWinSnmpTrapSession: Creating a new trap session\n"
  25. ) ;
  26. )
  27. m_bValid = FALSE;
  28. m_bDestroy = FALSE;
  29. m_cRef = 1;
  30. if ( (NULL == Window::operator()()) || (NULL == managerPtr) || !RegisterForAllTraps())
  31. {
  32. DebugMacro4(
  33. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  34. __FILE__,__LINE__,
  35. L"SnmpWinSnmpTrapSession::SnmpWinSnmpTrapSession: Invalid trap session created\n"
  36. ) ;
  37. )
  38. return;
  39. }
  40. m_managerPtr = managerPtr;
  41. m_bValid = TRUE;
  42. DebugMacro4(
  43. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  44. __FILE__,__LINE__,
  45. L"SnmpWinSnmpTrapSession::SnmpWinSnmpTrapSession: Valid trap session created\n"
  46. ) ;
  47. )
  48. }
  49. SnmpWinSnmpTrapSession::~SnmpWinSnmpTrapSession()
  50. {
  51. //Deregister for all traps...
  52. if (m_bValid)
  53. {
  54. SnmpRegister(m_session_handle,
  55. 0, //manager
  56. 0, //agent
  57. 0, //context
  58. 0, //trap_oid
  59. SNMPAPI_OFF);
  60. SnmpClose(m_session_handle);
  61. DebugMacro4(
  62. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  63. __FILE__,__LINE__,
  64. L"SnmpWinSnmpTrapSession::~SnmpWinSnmpTrapSession: Unregistered for traps and closed winsnmp session\n"
  65. ) ;
  66. )
  67. }
  68. DebugMacro4(
  69. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  70. __FILE__,__LINE__,
  71. L"SnmpWinSnmpTrapSession::~SnmpWinSnmpTrapSession: Trap session destroyed\n"
  72. ) ;
  73. )
  74. }
  75. BOOL SnmpWinSnmpTrapSession::RegisterForAllTraps()
  76. {
  77. DebugMacro4(
  78. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  79. __FILE__,__LINE__,
  80. L"SnmpWinSnmpTrapSession::RegisterForAllTraps: Registering for all traps...\n"
  81. ) ;
  82. )
  83. smiUINT32 nMajorVersion = 1;
  84. smiUINT32 nMinorVersion = 1;
  85. smiUINT32 nLevel = 2;
  86. smiUINT32 nTranslateMode = SNMPAPI_UNTRANSLATED_V1;
  87. smiUINT32 nRetransmitMode = SNMPAPI_OFF;
  88. SNMPAPI_STATUS apiStatus = SnmpStartup(&nMajorVersion,
  89. &nMinorVersion,
  90. &nLevel,
  91. &nTranslateMode,
  92. &nRetransmitMode);
  93. if (SNMPAPI_FAILURE == apiStatus)
  94. {
  95. DebugMacro4(
  96. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  97. __FILE__,__LINE__,
  98. L"SnmpWinSnmpTrapSession::RegisterForAllTraps: Failed to start up winsnmp\n"
  99. ) ;
  100. )
  101. return FALSE;
  102. }
  103. m_session_handle = SnmpOpen(GetWindowHandle(), TRAP_EVENT);
  104. if (SNMPAPI_FAILURE == m_session_handle)
  105. {
  106. DWORD t_LastError = SnmpGetLastError ( 0 ) ;
  107. DebugMacro4(
  108. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  109. __FILE__,__LINE__,
  110. L"SnmpWinSnmpTrapSession::RegisterForAllTraps: Failed to open a winsnmp session, error code (%d)\n" ,
  111. t_LastError
  112. ) ;
  113. )
  114. return FALSE;
  115. }
  116. apiStatus = SnmpRegister(m_session_handle,
  117. 0, //manager
  118. 0, //agent
  119. 0, //context
  120. 0, //trap_oid
  121. SNMPAPI_ON);
  122. if (SNMPAPI_FAILURE == apiStatus)
  123. {
  124. DebugMacro4(
  125. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  126. __FILE__,__LINE__,
  127. L"SnmpWinSnmpTrapSession::RegisterForAllTraps: Failed to register for all traps\n"
  128. ) ;
  129. )
  130. SnmpClose(m_session_handle);
  131. return FALSE;
  132. }
  133. DebugMacro4(
  134. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  135. __FILE__,__LINE__,
  136. L"SnmpWinSnmpTrapSession::RegisterForAllTraps: Registered for all traps!\n"
  137. ) ;
  138. )
  139. return TRUE;
  140. }
  141. BOOL SnmpWinSnmpTrapSession::PostMessage(UINT user_msg_id, WPARAM wParam, LPARAM lParam)
  142. {
  143. //call the global PostMessage...
  144. return ::WaitPostMessage(GetWindowHandle(), user_msg_id, wParam, lParam);
  145. }
  146. BOOL SnmpWinSnmpTrapSession::DestroySession()
  147. {
  148. if (0 != InterlockedDecrement(&m_cRef))
  149. {
  150. return FALSE;
  151. }
  152. delete this;
  153. return TRUE;
  154. }
  155. LONG_PTR SnmpWinSnmpTrapSession::HandleEvent(HWND hWnd, UINT message,
  156. WPARAM wParam,LPARAM lParam)
  157. {
  158. InterlockedIncrement(&m_cRef);
  159. LONG_PTR ret = 0;
  160. ScopeGuard t_1 = MakeObjGuard ( *this , SnmpWinSnmpTrapSession::DestroySession ) ;
  161. if (TRAP_EVENT == message)
  162. {
  163. DebugMacro4(
  164. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  165. __FILE__,__LINE__,
  166. L"SnmpWinSnmpTrapSession::HandleEvent: Received Trap\n"
  167. ) ;
  168. )
  169. /*
  170. never the case 'cos if the list is empty, this session
  171. would not have been created by the TrapMnager!
  172. if (m_managerPtr->m_receivers->IsEmpty())
  173. {
  174. return 0;
  175. }
  176. */
  177. HSNMP_ENTITY hsrc;
  178. HSNMP_CONTEXT hctxt;
  179. HSNMP_VBL hvbl;
  180. HSNMP_PDU hpdu;
  181. SNMPAPI_STATUS status = SnmpRecvMsg (m_session_handle,
  182. &hsrc, NULL, &hctxt, &hpdu);
  183. if (SNMPAPI_FAILURE != status)
  184. {
  185. SnmpSecurity *ctxt = OperationHelper::GetSecurityContext(hctxt);
  186. SnmpFreeContext(hctxt);
  187. if (NULL == ctxt)
  188. {
  189. //conversion failed clean up and return
  190. SnmpFreePdu(hpdu);
  191. SnmpFreeEntity(hsrc);
  192. DebugMacro4(
  193. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  194. __FILE__,__LINE__,
  195. L"SnmpWinSnmpTrapSession::HandleEvent: Discarding Trap due to security context decode failure\n"
  196. ) ;
  197. )
  198. }
  199. else
  200. {
  201. SnmpTransportAddress *src = OperationHelper::GetTransportAddress(hsrc);
  202. SnmpFreeEntity(hsrc);
  203. if (NULL == src)
  204. {
  205. //conversion failed clean up and return
  206. delete ctxt;
  207. SnmpFreePdu(hpdu);
  208. DebugMacro4(
  209. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  210. __FILE__,__LINE__,
  211. L"SnmpWinSnmpTrapSession::HandleEvent: Discarding Trap due to source address decode failure\n"
  212. ) ;
  213. )
  214. }
  215. else
  216. {
  217. smiINT t_PduType ;
  218. status = SnmpGetPduData(hpdu, &t_PduType, NULL, NULL, NULL, &hvbl);
  219. //got the vblist, don't need the pdu
  220. SnmpFreePdu(hpdu);
  221. if (SNMPAPI_FAILURE == status)
  222. {
  223. //failed to get varbinds clean up and return
  224. delete ctxt;
  225. delete src;
  226. DebugMacro4(
  227. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  228. __FILE__,__LINE__,
  229. L"SnmpWinSnmpTrapSession::HandleEvent: Discarding Trap due to PDU decode failure\n"
  230. ) ;
  231. )
  232. }
  233. else
  234. {
  235. if ( !(( SNMP_PDU_TRAP == t_PduType ) ||
  236. ( SNMP_PDU_V1TRAP == t_PduType) ||
  237. ( SNMP_PDU_INFORM == t_PduType)))
  238. {
  239. delete ctxt;
  240. delete src;
  241. SnmpFreeVbl(hvbl);
  242. DestroySession();
  243. return ret;
  244. }
  245. UINT vbcount = SnmpCountVbl(hvbl);
  246. if (SNMPAPI_FAILURE == vbcount)
  247. {
  248. delete ctxt;
  249. delete src;
  250. SnmpFreeVbl(hvbl);
  251. DebugMacro4(
  252. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  253. __FILE__,__LINE__,
  254. L"SnmpWinSnmpTrapSession::HandleEvent: Discarding Trap due to VarBind count decode failure\n"
  255. ) ;
  256. )
  257. }
  258. else
  259. {
  260. SnmpVarBindList vbl;
  261. for (UINT i = 1; i <= vbcount; i++)
  262. {
  263. smiOID vbname;
  264. smiVALUE vbvalue;
  265. status = SnmpGetVb(hvbl, i, &vbname, &vbvalue);
  266. if (SNMPAPI_FAILURE == status)
  267. {
  268. delete ctxt;
  269. delete src;
  270. SnmpFreeVbl(hvbl);
  271. hvbl = NULL;
  272. DebugMacro4(
  273. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  274. __FILE__,__LINE__,
  275. L"SnmpWinSnmpTrapSession::HandleEvent: Discarding Trap due to complete VarBind decode failure\n"
  276. ) ;
  277. )
  278. break;
  279. }
  280. else
  281. {
  282. SnmpVarBind* vb = OperationHelper::GetVarBind(vbname, vbvalue);
  283. if ( ! vb )
  284. {
  285. delete ctxt;
  286. delete src;
  287. SnmpFreeVbl(hvbl);
  288. hvbl = NULL;
  289. SnmpFreeDescriptor(SNMP_SYNTAX_OID, (smiOCTETS *)&vbname);
  290. DebugMacro4(
  291. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  292. __FILE__,__LINE__,
  293. L"SnmpWinSnmpTrapSession::HandleEvent: Discarding Trap due to (%d)th VarBind decode failure\n",
  294. i
  295. ) ;
  296. )
  297. break;
  298. }
  299. SnmpFreeDescriptor(SNMP_SYNTAX_OID, (smiOCTETS *)&vbname);
  300. switch (vbvalue.syntax)
  301. {
  302. case SNMP_SYNTAX_OCTETS :
  303. case SNMP_SYNTAX_BITS :
  304. case SNMP_SYNTAX_OPAQUE :
  305. case SNMP_SYNTAX_IPADDR :
  306. case SNMP_SYNTAX_NSAPADDR :
  307. {
  308. SnmpFreeDescriptor(SNMP_SYNTAX_OCTETS, &vbvalue.value.string);
  309. }
  310. break ;
  311. case SNMP_SYNTAX_OID :
  312. {
  313. SnmpFreeDescriptor(SNMP_SYNTAX_OID,
  314. (smiOCTETS *)(&vbvalue.value.oid));
  315. }
  316. break ;
  317. default:
  318. {
  319. }
  320. }
  321. vbl.AddNoReallocate (*vb);
  322. }
  323. }
  324. if (hvbl != NULL)
  325. {
  326. SnmpFreeVbl(hvbl);
  327. SnmpTrapReceiver * rx = m_managerPtr->m_receivers.GetNext();
  328. while (NULL != rx)
  329. {
  330. InterlockedIncrement(&(rx->m_cRef));
  331. rx->Receive(*src, *ctxt, vbl);
  332. rx->DestroyReceiver();
  333. rx = m_managerPtr->m_receivers.GetNext();
  334. }
  335. delete ctxt;
  336. delete src;
  337. }
  338. else
  339. {
  340. DebugMacro4(
  341. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  342. __FILE__,__LINE__,
  343. L"SnmpWinSnmpTrapSession::HandleEvent: Discarding Trap due to receive failure\n"
  344. ) ;
  345. )
  346. }
  347. }
  348. }
  349. }
  350. }
  351. }
  352. else
  353. {
  354. DebugMacro4(
  355. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  356. __FILE__,__LINE__,
  357. L"SnmpWinSnmpTrapSession::HandleEvent: Discarding Trap due to decode error\n"
  358. ) ;
  359. )
  360. }
  361. }
  362. else
  363. {
  364. ret = Window::HandleEvent(hWnd, message, wParam, lParam);
  365. }
  366. return ret;
  367. }