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.

491 lines
12 KiB

  1. // Copyright (c) 1997-2001 Microsoft Corporation, All Rights Reserved
  2. /*---------------------------------------------------------
  3. Filename: tsess.cpp
  4. Written By: B.Rajeev
  5. ----------------------------------------------------------*/
  6. #include "precomp.h"
  7. #include "common.h"
  8. #include "error.h"
  9. #include "encdec.h"
  10. #include "vblist.h"
  11. #include "sec.h"
  12. #include "pdu.h"
  13. #include "tsent.h"
  14. #include "transp.h"
  15. #include "tsess.h"
  16. #include "sync.h"
  17. #include "dummy.h"
  18. #include "flow.h"
  19. #include "frame.h"
  20. #include "timer.h"
  21. #include "message.h"
  22. #include "ssent.h"
  23. #include "idmap.h"
  24. #include "opreg.h"
  25. #include "session.h"
  26. TransportWindow::TransportWindow (
  27. SnmpImpTransport &owner_transport
  28. ) : owner ( owner_transport ) , m_Session ( NULL )
  29. {
  30. smiUINT32 t_MajorVersion = 1 ;
  31. smiUINT32 t_MinorVersion = 1 ;
  32. smiUINT32 t_Level = 2 ;
  33. smiUINT32 t_TranslateMode = SNMPAPI_UNTRANSLATED_V1 ;
  34. smiUINT32 t_RetransmitMode = SNMPAPI_OFF ;
  35. CriticalSectionLock t_CriticalSectionLock ( SnmpEncodeDecode :: s_CriticalSection ) ;
  36. t_CriticalSectionLock.GetLock ( INFINITE ) ;
  37. SNMPAPI_STATUS t_StartupStatus = SnmpStartup (
  38. &t_MajorVersion,
  39. &t_MinorVersion,
  40. &t_Level,
  41. &t_TranslateMode,
  42. &t_RetransmitMode
  43. );
  44. t_CriticalSectionLock.UnLock () ;
  45. if ( t_StartupStatus == SNMPAPI_FAILURE )
  46. {
  47. throw GeneralException (
  48. Snmp_Error,
  49. Snmp_Local_Error,
  50. __FILE__,
  51. __LINE__
  52. );
  53. }
  54. m_Session = SnmpOpen (
  55. GetWindowHandle (),
  56. Window :: g_MessageArrivalEvent
  57. ) ;
  58. if ( m_Session == SNMPAPI_FAILURE )
  59. {
  60. m_Session = NULL;
  61. DWORD t_LastError = SnmpGetLastError (0) ;
  62. throw GeneralException (
  63. Snmp_Error,
  64. Snmp_Local_Error,
  65. __FILE__,
  66. __LINE__
  67. );
  68. }
  69. }
  70. TransportWindow::~TransportWindow ()
  71. {
  72. HSNMP_SESSION t_Session = ( HSNMP_SESSION ) m_Session ;
  73. if ( t_Session )
  74. SnmpClose ( t_Session ) ;
  75. }
  76. // over-rides the HandleEvent method provided by the
  77. // WinSnmpSession. Receives the Pdu and passes it to
  78. // the owner (SnmpTransport)
  79. LONG_PTR TransportWindow::HandleEvent (
  80. HWND hWnd ,
  81. UINT message ,
  82. WPARAM wParam ,
  83. LPARAM lParam
  84. )
  85. {
  86. LONG rc = 0;
  87. try
  88. {
  89. // check if the message needs to be handled
  90. if ( message == Window :: g_MessageArrivalEvent )
  91. {
  92. // inform the owner of a successful message receipt
  93. SnmpPdu t_SnmpPdu ;
  94. if ( ReceivePdu ( t_SnmpPdu ) )
  95. {
  96. owner.TransportReceiveFrame ( t_SnmpPdu , t_SnmpPdu.GetErrorReport () ) ;
  97. delete & t_SnmpPdu.GetVarbindList () ;
  98. }
  99. else
  100. {
  101. }
  102. }
  103. else if ( message == Window :: g_SentFrameEvent )
  104. {
  105. // inform the owner of a sent frame event
  106. // the error report will be ignored in this case
  107. owner.HandleSentFrame( (TransportFrameId)wParam );
  108. }
  109. else
  110. {
  111. return Window::HandleEvent(hWnd, message, wParam, lParam);
  112. }
  113. }
  114. catch ( Heap_Exception e_He )
  115. {
  116. }
  117. catch ( GeneralException exception )
  118. {
  119. }
  120. return rc;
  121. }
  122. // sends the specified pdu. it decodes the SnmpPdu to extract
  123. // parameters needed for SnmpSendMsg. the return value denotes
  124. // success and failure in transmission
  125. // we return on encoutering an error from the winsnmp library call
  126. BOOL TransportWindow :: SendPdu (
  127. IN SnmpPdu &a_SnmpPdu
  128. )
  129. {
  130. WinSnmpVariables t_WinSnmpVariables ;
  131. BOOL t_Status ;
  132. try
  133. {
  134. t_Status = owner.session.GetSnmpEncodeDecode ().EncodeFrame ( a_SnmpPdu , &t_WinSnmpVariables ) ;
  135. if ( ! t_Status )
  136. {
  137. throw GeneralException (
  138. Snmp_Error,
  139. Snmp_Local_Error,
  140. __FILE__,
  141. __LINE__,
  142. SnmpGetLastError ((HSNMP_SESSION) m_Session)
  143. );
  144. }
  145. }
  146. catch ( Heap_Exception e_He )
  147. {
  148. throw ;
  149. }
  150. catch ( GeneralException exception )
  151. {
  152. throw ;
  153. }
  154. HSNMP_CONTEXT t_Context = t_WinSnmpVariables.m_Context ;
  155. HSNMP_PDU t_Pdu = t_WinSnmpVariables.m_Pdu ;
  156. HSNMP_VBL t_Vbl = t_WinSnmpVariables.m_Vbl ;
  157. char *t_DstAddress = owner.GetTransportAddress ().GetAddress () ;
  158. CriticalSectionLock t_CriticalSectionLock ( SnmpEncodeDecode :: s_CriticalSection ) ;
  159. t_CriticalSectionLock.GetLock ( INFINITE ) ;
  160. owner.session.GetSnmpEncodeDecode ().SetTranslateMode () ;
  161. HSNMP_ENTITY t_SrcEntity = SnmpStrToEntity (
  162. ( HSNMP_SESSION ) m_Session ,
  163. LOOPBACK_ADDRESS
  164. ) ;
  165. if ( t_SrcEntity == SNMPAPI_FAILURE )
  166. {
  167. SnmpFreeContext ( t_Context ) ;
  168. SnmpFreePdu ( t_Pdu ) ;
  169. SnmpFreeVbl ( t_Vbl ) ;
  170. throw GeneralException (
  171. Snmp_Error,
  172. Snmp_Local_Error,
  173. __FILE__,
  174. __LINE__,
  175. SnmpGetLastError ((HSNMP_SESSION) m_Session)
  176. );
  177. }
  178. HSNMP_ENTITY t_DstEntity = SnmpStrToEntity (
  179. ( HSNMP_SESSION ) m_Session ,
  180. t_DstAddress
  181. ) ;
  182. t_CriticalSectionLock.UnLock () ;
  183. if ( t_DstEntity == SNMPAPI_FAILURE )
  184. {
  185. SnmpFreeContext ( t_Context ) ;
  186. SnmpFreeEntity ( t_SrcEntity ) ;
  187. SnmpFreePdu ( t_Pdu ) ;
  188. SnmpFreeVbl ( t_Vbl ) ;
  189. throw GeneralException (
  190. Snmp_Error,
  191. Snmp_Local_Error,
  192. __FILE__,
  193. __LINE__,
  194. SnmpGetLastError ((HSNMP_SESSION) m_Session)
  195. );
  196. }
  197. DebugMacro4(
  198. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  199. __FILE__,__LINE__,
  200. L"Transport::SendPdu: sending %d\r\n\r\n" , t_WinSnmpVariables.m_RequestId
  201. ) ;
  202. smiOCTETS t_Octets ;
  203. SnmpSetRetransmitMode ( SNMPAPI_OFF ) ;
  204. if ( SnmpEncodeMsg (
  205. m_Session,
  206. t_SrcEntity,
  207. t_DstEntity,
  208. t_Context,
  209. t_Pdu ,
  210. & t_Octets
  211. ) != SNMPAPI_FAILURE )
  212. {
  213. ULONG t_Len = t_Octets.len ;
  214. UCHAR *t_Ptr = t_Octets.ptr ;
  215. ULONG t_RowLength = t_Len / 16 ;
  216. ULONG t_Remainder = t_Len % 16 ;
  217. ULONG t_Index = 0 ;
  218. for ( ULONG t_RowIndex = 0 ; t_RowIndex < t_RowLength ; t_RowIndex ++ )
  219. {
  220. ULONG t_StoredIndex = t_Index ;
  221. for ( ULONG t_ColumnIndex = 0 ; t_ColumnIndex < 16 ; t_ColumnIndex ++ )
  222. {
  223. SnmpDebugLog :: s_SnmpDebugLog->Write ( L"%2.2lx " , t_Ptr [ t_Index ++ ] ) ;
  224. }
  225. SnmpDebugLog :: s_SnmpDebugLog->Write ( L" " ) ;
  226. for ( t_ColumnIndex = 0 ; t_ColumnIndex < 16 ; t_ColumnIndex ++ )
  227. {
  228. if ( ( t_Ptr [ t_StoredIndex ] >= 0x20 ) && ( t_Ptr [ t_StoredIndex ] <= 0x7f ) )
  229. {
  230. SnmpDebugLog :: s_SnmpDebugLog->Write ( L"%c" , t_Ptr [ t_StoredIndex ] ) ;
  231. }
  232. else
  233. {
  234. SnmpDebugLog :: s_SnmpDebugLog->Write ( L"." ) ;
  235. }
  236. t_StoredIndex ++ ;
  237. }
  238. SnmpDebugLog :: s_SnmpDebugLog->Write ( L"\r\n" ) ;
  239. }
  240. ULONG t_StoredIndex = t_Index ;
  241. for ( ULONG t_ColumnIndex = 0 ; t_ColumnIndex < 16 ; t_ColumnIndex ++ )
  242. {
  243. if ( t_ColumnIndex < t_Remainder )
  244. {
  245. SnmpDebugLog :: s_SnmpDebugLog->Write ( L"%2.2lx " , t_Ptr [ t_Index ++ ] ) ;
  246. }
  247. else
  248. {
  249. SnmpDebugLog :: s_SnmpDebugLog->Write ( L" " ) ;
  250. }
  251. }
  252. SnmpDebugLog :: s_SnmpDebugLog->Write ( L" " ) ;
  253. for ( t_ColumnIndex = 0 ; t_ColumnIndex < 16 ; t_ColumnIndex ++ )
  254. {
  255. if ( t_ColumnIndex < t_Remainder )
  256. {
  257. if ( t_Ptr [ t_StoredIndex ] >= 0x20 && t_Ptr [ t_StoredIndex ] <= 0x7f )
  258. {
  259. SnmpDebugLog :: s_SnmpDebugLog->Write ( L"%c" , t_Ptr [ t_StoredIndex ] ) ;
  260. }
  261. else
  262. {
  263. SnmpDebugLog :: s_SnmpDebugLog->Write ( L"." ) ;
  264. }
  265. t_StoredIndex ++ ;
  266. }
  267. }
  268. SnmpDebugLog :: s_SnmpDebugLog->Write ( L"\r\n\r\n" ) ;
  269. SnmpFreeDescriptor (
  270. SNMP_SYNTAX_OCTETS ,
  271. & t_Octets
  272. ) ;
  273. }
  274. )
  275. SnmpSetRetransmitMode ( SNMPAPI_OFF ) ;
  276. // send message
  277. t_Status = SnmpSendMsg (
  278. m_Session,
  279. t_SrcEntity,
  280. t_DstEntity,
  281. t_Context,
  282. t_Pdu
  283. );
  284. if ( t_Status == SNMPAPI_FAILURE )
  285. {
  286. DebugMacro4(
  287. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  288. __FILE__,__LINE__,
  289. L"Transport::SendPdu: failed sending %d\r\n\r\n" , t_WinSnmpVariables.m_RequestId
  290. ) ;
  291. )
  292. SnmpFreeContext ( t_Context ) ;
  293. SnmpFreeEntity ( t_SrcEntity ) ;
  294. SnmpFreeEntity ( t_DstEntity ) ;
  295. SnmpFreePdu ( t_Pdu ) ;
  296. SnmpFreeVbl ( t_Vbl ) ;
  297. throw GeneralException (
  298. Snmp_Error,
  299. Snmp_Local_Error,
  300. __FILE__,
  301. __LINE__,
  302. SnmpGetLastError ((HSNMP_SESSION) m_Session)
  303. ) ;
  304. }
  305. SnmpFreeContext ( t_Context ) ;
  306. SnmpFreeEntity ( t_SrcEntity ) ;
  307. SnmpFreeEntity ( t_DstEntity ) ;
  308. SnmpFreePdu ( t_Pdu ) ;
  309. SnmpFreeVbl ( t_Vbl ) ;
  310. if ( t_Status == SNMPAPI_FAILURE )
  311. return FALSE;
  312. else
  313. return TRUE;
  314. }
  315. BOOL TransportWindow :: ReceivePdu (
  316. OUT SnmpPdu &a_SnmpPdu
  317. )
  318. {
  319. HSNMP_ENTITY t_SrcEntity = NULL;
  320. HSNMP_ENTITY t_DstEntity = NULL;
  321. HSNMP_CONTEXT t_Context;
  322. HSNMP_PDU t_Pdu;
  323. SNMPAPI_STATUS t_Status = SnmpRecvMsg (
  324. m_Session ,
  325. &t_SrcEntity,
  326. &t_DstEntity ,
  327. &t_Context,
  328. &t_Pdu
  329. );
  330. if ( t_SrcEntity )
  331. {
  332. SnmpFreeEntity ( t_SrcEntity ) ;
  333. }
  334. if ( t_DstEntity )
  335. {
  336. SnmpFreeEntity ( t_DstEntity ) ;
  337. }
  338. if ( t_Status == SNMPAPI_FAILURE )
  339. {
  340. if ( SnmpGetLastError ((HSNMP_SESSION) m_Session) == SNMPAPI_NOOP )
  341. {
  342. return FALSE ;
  343. }
  344. else
  345. {
  346. throw GeneralException (
  347. Snmp_Error,
  348. Snmp_Local_Error,
  349. __FILE__,
  350. __LINE__,
  351. SnmpGetLastError ((HSNMP_SESSION) m_Session)
  352. );
  353. }
  354. }
  355. CriticalSectionLock t_CriticalSectionLock ( SnmpEncodeDecode :: s_CriticalSection ) ;
  356. t_CriticalSectionLock.GetLock ( INFINITE ) ;
  357. owner.session.GetSnmpEncodeDecode ().SetTranslateMode () ;
  358. t_SrcEntity = SnmpStrToEntity ( (HSNMP_SESSION) m_Session, LOOPBACK_ADDRESS ) ;
  359. t_DstEntity = SnmpStrToEntity ( (HSNMP_SESSION) m_Session, LOOPBACK_ADDRESS ) ;
  360. t_CriticalSectionLock.UnLock () ;
  361. WinSnmpVariables t_WinSnmpVariables ;
  362. t_WinSnmpVariables.m_SrcEntity = t_SrcEntity ;
  363. t_WinSnmpVariables.m_DstEntity = t_DstEntity ;
  364. t_WinSnmpVariables.m_Context = t_Context ;
  365. t_WinSnmpVariables.m_Pdu = t_Pdu ;
  366. t_Status = owner.session.GetSnmpEncodeDecode ().DecodeFrame ( &t_WinSnmpVariables , a_SnmpPdu ) ;
  367. if ( ! t_Status ||
  368. SnmpEncodeDecode :: PduType :: RESPONSE != a_SnmpPdu.GetPduType() )
  369. {
  370. SnmpFreePdu ( t_Pdu ) ;
  371. SnmpFreeEntity ( t_SrcEntity ) ;
  372. SnmpFreeEntity ( t_DstEntity ) ;
  373. SnmpFreeContext ( t_Context ) ;
  374. throw GeneralException (
  375. Snmp_Error,
  376. SnmpEncodeDecode :: PduType :: GETBULK == a_SnmpPdu.GetPduType() ? Snmp_General_Abort : Snmp_Local_Error,
  377. __FILE__,
  378. __LINE__,
  379. SnmpGetLastError ((HSNMP_SESSION) m_Session)
  380. );
  381. }
  382. SnmpFreePdu ( t_Pdu ) ;
  383. SnmpFreeEntity ( t_SrcEntity ) ;
  384. SnmpFreeEntity ( t_DstEntity ) ;
  385. SnmpFreeContext ( t_Context ) ;
  386. return TRUE ;
  387. }