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.

1390 lines
34 KiB

  1. //***************************************************************************
  2. //
  3. // File:
  4. //
  5. // Module: MS SNMP Provider
  6. //
  7. // Purpose:
  8. //
  9. // Copyright (c) 1997-2001 Microsoft Corporation, All Rights Reserved
  10. //
  11. //***************************************************************************
  12. /*---------------------------------------------------------
  13. Filename: encdec.cpp
  14. Written By: S.Menzies
  15. ----------------------------------------------------------*/
  16. #include "precomp.h"
  17. #include <winsock2.h>
  18. #include "common.h"
  19. #include "sync.h"
  20. #include "encap.h"
  21. #include "value.h"
  22. #include "vblist.h"
  23. #include "vbl.h"
  24. #include "fs_reg.h"
  25. #include "error.h"
  26. #include "encdec.h"
  27. #include "sec.h"
  28. #include "pdu.h"
  29. #include "pseudo.h"
  30. #include "dummy.h"
  31. #include "flow.h"
  32. #include "frame.h"
  33. #include "timer.h"
  34. #include "message.h"
  35. #include "ssent.h"
  36. #include "idmap.h"
  37. #include "opreg.h"
  38. #include "session.h"
  39. #include "ophelp.h"
  40. #include "op.h"
  41. #include "tsess.h"
  42. const WinSnmpInteger winsnmp_pdu_type[] = {SNMP_PDU_GET, SNMP_PDU_GETNEXT, SNMP_PDU_SET};
  43. const ULONG num_pdus = sizeof(winsnmp_pdu_type)/sizeof(WinSnmpInteger);
  44. CriticalSection SnmpEncodeDecode :: s_CriticalSection;
  45. void FreeDescriptor ( smiVALUE &a_Value )
  46. {
  47. switch ( a_Value.syntax )
  48. {
  49. case SNMP_SYNTAX_OCTETS :
  50. case SNMP_SYNTAX_BITS :
  51. case SNMP_SYNTAX_OPAQUE :
  52. case SNMP_SYNTAX_IPADDR :
  53. case SNMP_SYNTAX_NSAPADDR :
  54. {
  55. SnmpFreeDescriptor (
  56. SNMP_SYNTAX_OCTETS ,
  57. & a_Value.value.string
  58. ) ;
  59. }
  60. break ;
  61. case SNMP_SYNTAX_OID :
  62. {
  63. SnmpFreeDescriptor (
  64. SNMP_SYNTAX_OID,
  65. (smiOCTETS *)(&a_Value.value.oid)
  66. );
  67. }
  68. break ;
  69. default:
  70. {
  71. }
  72. break ;
  73. }
  74. }
  75. // returns an SnmpVarBind containing an SnmpObjectIdentifier and an
  76. // SnmpValue created using the instance(OID) and the value(VALUE)
  77. SnmpVarBind *GetVarBind (
  78. IN smiOID &instance,
  79. IN smiVALUE &value
  80. )
  81. {
  82. // create an SnmpObjectIdentifier using the instance value
  83. SnmpObjectIdentifier id(instance.ptr, instance.len);
  84. SnmpValue *snmp_value = NULL;
  85. // for each possible value for value.syntax, create the
  86. // corresponding SnmpValue
  87. switch(value.syntax)
  88. {
  89. case SNMP_SYNTAX_NULL: // null value
  90. {
  91. snmp_value = new SnmpNull();
  92. }
  93. break;
  94. case SNMP_SYNTAX_INT: // integer *(has same value as SNMP_SYNTAX_INT32)*
  95. {
  96. snmp_value = new SnmpInteger(value.value.sNumber);
  97. }
  98. break;
  99. case SNMP_SYNTAX_UINT32: // integer *(has same value as SNMP_SYNTAX_GAUGE)*
  100. {
  101. snmp_value = new SnmpUInteger32(value.value.uNumber);
  102. }
  103. break;
  104. case SNMP_SYNTAX_CNTR32: // counter32
  105. {
  106. snmp_value = new SnmpCounter (value.value.uNumber);
  107. }
  108. break;
  109. case SNMP_SYNTAX_GAUGE32: // gauge
  110. {
  111. snmp_value = new SnmpGauge(value.value.uNumber);
  112. }
  113. break;
  114. case SNMP_SYNTAX_TIMETICKS: // time ticks
  115. {
  116. snmp_value = new SnmpTimeTicks(value.value.uNumber);
  117. }
  118. break;
  119. case SNMP_SYNTAX_OCTETS: // octets
  120. {
  121. snmp_value = new SnmpOctetString(value.value.string.ptr,
  122. value.value.string.len);
  123. }
  124. break;
  125. case SNMP_SYNTAX_OPAQUE: // opaque value
  126. {
  127. snmp_value = new SnmpOpaque(value.value.string.ptr,
  128. value.value.string.len);
  129. }
  130. break;
  131. case SNMP_SYNTAX_OID: // object identifier
  132. {
  133. snmp_value = new SnmpObjectIdentifier(value.value.oid.ptr,
  134. value.value.oid.len);
  135. }
  136. break;
  137. case SNMP_SYNTAX_IPADDR: // ip address value
  138. {
  139. if ( value.value.string.ptr )
  140. {
  141. snmp_value = new SnmpIpAddress(ntohl(*((ULONG *)value.value.string.ptr)));
  142. }
  143. else
  144. {
  145. snmp_value = new SnmpNull();
  146. }
  147. }
  148. break;
  149. case SNMP_SYNTAX_CNTR64: // counter64
  150. {
  151. snmp_value = new SnmpCounter64 (value.value.hNumber.lopart , value.value.hNumber.hipart );
  152. }
  153. break;
  154. case SNMP_SYNTAX_NOSUCHOBJECT:
  155. {
  156. snmp_value = new SnmpNoSuchObject ;
  157. }
  158. break ;
  159. case SNMP_SYNTAX_NOSUCHINSTANCE:
  160. {
  161. snmp_value = new SnmpNoSuchInstance ;
  162. }
  163. break ;
  164. case SNMP_SYNTAX_ENDOFMIBVIEW:
  165. {
  166. snmp_value = new SnmpEndOfMibView ;
  167. }
  168. break ;
  169. default:
  170. {
  171. // it must be an unsupported type
  172. // return an SnmpNullValue by default
  173. snmp_value = new SnmpNull();
  174. }
  175. break;
  176. };
  177. SnmpVarBind *var_bind = NULL ;
  178. if ( snmp_value )
  179. {
  180. CProvDeleteMe<SnmpValue> sc(snmp_value);
  181. var_bind = new SnmpVarBind(id, *snmp_value);
  182. }
  183. return var_bind;
  184. }
  185. void GetOID(OUT smiOID &oid, IN SnmpObjectIdentifier &instance)
  186. {
  187. // determine length
  188. oid.len = instance.GetValueLength();
  189. // allocate space
  190. oid.ptr = new smiUINT32[oid.len];
  191. // copy the identifier values
  192. ULONG *value = instance.GetValue();
  193. for(UINT i=0; i < oid.len; i++)
  194. oid.ptr[i] = value[i];
  195. }
  196. // returns a winsnmp VALUE in the value OUT parameter corresponding
  197. // to the specified snmp_value. makes use of run-time type information
  198. // for the purpose
  199. void GetValue(OUT smiVALUE &value, IN SnmpValue &snmp_value)
  200. {
  201. // for each SnmpValue type, check if it is a pointer
  202. // to the derived type. If so, fill in the smiValue
  203. // and return
  204. SnmpNull *null_value = dynamic_cast<SnmpNull *>(&snmp_value);
  205. if ( null_value != NULL )
  206. {
  207. value.syntax = SNMP_SYNTAX_NULL;
  208. return;
  209. }
  210. SnmpInteger *integer_value = dynamic_cast<SnmpInteger *>(&snmp_value);
  211. if ( integer_value != NULL )
  212. {
  213. value.syntax = SNMP_SYNTAX_INT;
  214. value.value.sNumber = integer_value->GetValue();
  215. return;
  216. }
  217. SnmpGauge *gauge_value = dynamic_cast<SnmpGauge *>(&snmp_value);
  218. if ( gauge_value != NULL )
  219. {
  220. value.syntax = SNMP_SYNTAX_GAUGE32;
  221. value.value.uNumber = gauge_value->GetValue();
  222. return;
  223. }
  224. SnmpCounter *counter_value = dynamic_cast<SnmpCounter *>(&snmp_value);
  225. if ( counter_value != NULL )
  226. {
  227. value.syntax = SNMP_SYNTAX_CNTR32;
  228. value.value.uNumber = counter_value->GetValue();
  229. return;
  230. }
  231. SnmpTimeTicks *timeTicks_value = dynamic_cast<SnmpTimeTicks *>(&snmp_value);
  232. if ( timeTicks_value != NULL )
  233. {
  234. value.syntax = SNMP_SYNTAX_TIMETICKS;
  235. value.value.uNumber = timeTicks_value->GetValue();
  236. return;
  237. }
  238. SnmpOpaque *opaque_value = dynamic_cast<SnmpOpaque *>(&snmp_value);
  239. if ( opaque_value != NULL )
  240. {
  241. value.syntax = SNMP_SYNTAX_OPAQUE;
  242. value.value.string.len = opaque_value->GetValueLength();
  243. value.value.string.ptr = new smiBYTE[value.value.string.len];
  244. UCHAR *source = opaque_value->GetValue();
  245. for(UINT i=0; i < value.value.string.len; i++)
  246. value.value.string.ptr[i] = source[i];
  247. return;
  248. }
  249. SnmpOctetString *string_value = dynamic_cast<SnmpOctetString *>(&snmp_value);
  250. if ( string_value != NULL )
  251. {
  252. value.syntax = SNMP_SYNTAX_OCTETS;
  253. value.value.string.len = string_value->GetValueLength();
  254. value.value.string.ptr = new smiBYTE[value.value.string.len];
  255. UCHAR *source = string_value->GetValue();
  256. for(UINT i=0; i < value.value.string.len; i++)
  257. value.value.string.ptr[i] = source[i];
  258. return;
  259. }
  260. SnmpObjectIdentifier *oid_value = dynamic_cast<SnmpObjectIdentifier *>(&snmp_value);
  261. if ( oid_value != NULL )
  262. {
  263. value.syntax = SNMP_SYNTAX_OID;
  264. GetOID(value.value.oid, *oid_value);
  265. return;
  266. }
  267. SnmpIpAddress *ip_address_value = dynamic_cast<SnmpIpAddress *>(&snmp_value);
  268. if ( ip_address_value != NULL )
  269. {
  270. value.syntax = SNMP_SYNTAX_IPADDR;
  271. value.value.string.len = IP_ADDR_LEN;
  272. value.value.string.ptr = new smiBYTE[IP_ADDR_LEN];
  273. ULONG address = htonl ( ip_address_value->GetValue() ) ;
  274. UCHAR *t_Address = ( UCHAR * ) & address ;
  275. for(int i=IP_ADDR_LEN-1; i >= 0; i--)
  276. {
  277. value.value.string.ptr[i] = t_Address [ i ] ;
  278. }
  279. return;
  280. }
  281. SnmpUInteger32 *uinteger32_value = dynamic_cast<SnmpUInteger32 *>(&snmp_value);
  282. if ( uinteger32_value != NULL )
  283. {
  284. value.syntax = SNMP_SYNTAX_UINT32;
  285. value.value.uNumber = uinteger32_value->GetValue();
  286. return;
  287. }
  288. SnmpCounter64 *counter64_value = dynamic_cast<SnmpCounter64 *>(&snmp_value);
  289. if ( counter64_value != NULL )
  290. {
  291. value.syntax = SNMP_SYNTAX_CNTR64;
  292. value.value.hNumber.lopart = counter64_value->GetLowValue();
  293. value.value.hNumber.hipart = counter64_value->GetHighValue();
  294. return;
  295. }
  296. SnmpEndOfMibView *endofmibview_value = dynamic_cast<SnmpEndOfMibView *>(&snmp_value);
  297. if ( endofmibview_value != NULL )
  298. {
  299. value.syntax = SNMP_SYNTAX_ENDOFMIBVIEW;
  300. return;
  301. }
  302. SnmpNoSuchObject *nosuchobject_value = dynamic_cast<SnmpNoSuchObject *>(&snmp_value);
  303. if ( nosuchobject_value != NULL )
  304. {
  305. value.syntax = SNMP_SYNTAX_NOSUCHOBJECT;
  306. return;
  307. }
  308. SnmpNoSuchInstance *nosuchinstance_value = dynamic_cast<SnmpNoSuchInstance *>(&snmp_value);
  309. if ( nosuchinstance_value != NULL )
  310. {
  311. value.syntax = SNMP_SYNTAX_NOSUCHINSTANCE;
  312. return;
  313. }
  314. // we should not have come here
  315. // did we check all supported types?
  316. throw GeneralException(Snmp_Error, Snmp_Local_Error,__FILE__,__LINE__);
  317. }
  318. void LocalFreeVb(IN smiOID &oid, IN smiVALUE &value)
  319. {
  320. if ( oid.len > 0 )
  321. delete[] oid.ptr;
  322. switch( value.syntax )
  323. {
  324. case SNMP_SYNTAX_OCTETS: // octets
  325. case SNMP_SYNTAX_OPAQUE: // opaque value
  326. case SNMP_SYNTAX_IPADDR: // ip address value
  327. {
  328. delete[] value.value.string.ptr;
  329. break;
  330. }
  331. case SNMP_SYNTAX_OID: // object identifier
  332. {
  333. delete[] value.value.oid.ptr;
  334. break;
  335. }
  336. default:
  337. break;
  338. };
  339. }
  340. void SetWinSnmpVbl(SnmpVarBindList &var_bind_list, HSNMP_VBL vbl)
  341. {
  342. // reset the list
  343. var_bind_list.Reset();
  344. // for each var_bind, create a pair <oid, value> and
  345. // insert it into the vbl
  346. while( var_bind_list.Next() )
  347. {
  348. const SnmpVarBind *var_bind = var_bind_list.Get();
  349. smiOID instance;
  350. GetOID(instance, var_bind->GetInstance());
  351. smiVALUE value;
  352. GetValue(value, var_bind->GetValue());
  353. // insert a new var bind
  354. SnmpSetVb(vbl, 0, &instance, &value);
  355. LocalFreeVb(instance, value);
  356. }
  357. }
  358. BOOL DecodeVarBindList (
  359. HSNMP_VBL a_Vbl ,
  360. SnmpVarBindList &a_SnmpVarBindList
  361. )
  362. {
  363. smiINT t_VblCount = SnmpCountVbl ( a_Vbl ) ;
  364. for ( smiINT t_Count = 1 ; t_Count <= t_VblCount ; t_Count ++ )
  365. {
  366. smiOID t_Instance;
  367. smiVALUE t_Value;
  368. SNMPAPI_STATUS t_Status = SnmpGetVb (
  369. a_Vbl,
  370. t_Count,
  371. & t_Instance,
  372. & t_Value
  373. ) ;
  374. if ( t_Status == SNMPAPI_FAILURE )
  375. {
  376. return FALSE ;
  377. }
  378. SnmpVarBind *t_VarBind = GetVarBind ( t_Instance , t_Value ) ;
  379. if ( ! t_VarBind )
  380. {
  381. SnmpFreeDescriptor (
  382. SNMP_SYNTAX_OID,
  383. (smiOCTETS *) & t_Instance
  384. ) ;
  385. return FALSE ;
  386. }
  387. a_SnmpVarBindList.AddNoReallocate ( *t_VarBind ) ;
  388. SnmpFreeDescriptor (
  389. SNMP_SYNTAX_OID,
  390. (smiOCTETS *) & t_Instance
  391. ) ;
  392. FreeDescriptor ( t_Value ) ;
  393. }
  394. return TRUE ;
  395. }
  396. BOOL SnmpEncodeDecode :: DestroyStaticComponents ()
  397. {
  398. return TRUE ;
  399. }
  400. BOOL SnmpEncodeDecode :: InitializeStaticComponents ()
  401. {
  402. return TRUE ;
  403. }
  404. SnmpEncodeDecode :: SnmpEncodeDecode () : m_IsValid ( FALSE ) , m_Session ( NULL ) , m_Window ( NULL )
  405. {
  406. Window *t_Window = new Window ;
  407. m_Window = t_Window ;
  408. }
  409. SnmpEncodeDecode :: ~SnmpEncodeDecode ()
  410. {
  411. if ( m_IsValid )
  412. {
  413. HSNMP_SESSION t_Session = ( HSNMP_SESSION ) m_Session ;
  414. SnmpClose ( t_Session ) ;
  415. }
  416. Window *t_Window = ( Window * ) m_Window ;
  417. delete t_Window ;
  418. }
  419. BOOL SnmpEncodeDecode :: EncodeFrame (
  420. OUT SnmpPdu &a_SnmpPdu ,
  421. IN RequestId a_RequestId,
  422. IN PduType a_PduType,
  423. IN SnmpErrorReport &a_SnmpErrorReport ,
  424. IN SnmpVarBindList &a_SnmpVarBindList,
  425. IN SnmpCommunityBasedSecurity *&a_SnmpCommunityBasedSecurity ,
  426. IN SnmpTransportAddress *&a_SrcTransportAddress ,
  427. IN SnmpTransportAddress *&a_DstTransportAddress
  428. )
  429. {
  430. a_SnmpPdu.SetRequestId ( a_RequestId ) ;
  431. a_SnmpPdu.SetPduType ( a_PduType ) ;
  432. a_SnmpPdu.SetErrorReport ( a_SnmpErrorReport ) ;
  433. a_SnmpPdu.SetVarBindList ( a_SnmpVarBindList ) ;
  434. if ( a_SnmpCommunityBasedSecurity )
  435. a_SnmpPdu.SetCommunityName ( *a_SnmpCommunityBasedSecurity ) ;
  436. if ( a_SrcTransportAddress )
  437. a_SnmpPdu.SetSourceAddress ( *a_SrcTransportAddress ) ;
  438. if ( a_DstTransportAddress )
  439. a_SnmpPdu.SetDestinationAddress ( *a_DstTransportAddress ) ;
  440. return TRUE ;
  441. }
  442. BOOL SnmpEncodeDecode :: DecodeFrame (
  443. IN SnmpPdu &a_SnmpPdu ,
  444. OUT RequestId a_RequestId,
  445. OUT PduType a_PduType ,
  446. OUT SnmpErrorReport &a_SnmpErrorReport ,
  447. OUT SnmpVarBindList *&a_SnmpVarBindList ,
  448. OUT SnmpCommunityBasedSecurity *&a_SnmpCommunityBasedSecurity ,
  449. OUT SnmpTransportAddress *&a_SrcTransportAddress ,
  450. OUT SnmpTransportAddress *&a_DstTransportAddress
  451. )
  452. {
  453. a_SrcTransportAddress = & a_SnmpPdu.GetSourceAddress ();
  454. a_DstTransportAddress = & a_SnmpPdu.GetDestinationAddress () ;
  455. a_PduType = a_SnmpPdu.GetPduType () ;
  456. a_RequestId = a_SnmpPdu.GetRequestId () ;
  457. a_SnmpVarBindList = & a_SnmpPdu.GetVarbindList () ;
  458. a_SnmpCommunityBasedSecurity = & a_SnmpPdu.GetCommunityName () ;
  459. a_SnmpErrorReport = a_SnmpPdu.GetErrorReport () ;
  460. return TRUE ;
  461. }
  462. BOOL SnmpEncodeDecode :: EncodeFrame (
  463. IN SnmpPdu &a_SnmpPdu ,
  464. OUT void *a_ImplementationEncoding
  465. )
  466. {
  467. WinSnmpVariables *t_WinSnmpVariables = ( WinSnmpVariables * ) a_ImplementationEncoding ;
  468. RequestId t_SnmpRequestId = a_SnmpPdu.GetRequestId () ;
  469. PduType t_SnmpPduType = a_SnmpPdu.GetPduType () ;
  470. SnmpErrorReport &t_SnmpErrorReport = a_SnmpPdu.GetErrorReport () ;
  471. SnmpCommunityBasedSecurity &t_SnmpCommunityBasedSecurity = a_SnmpPdu.GetCommunityName () ;
  472. SnmpVarBindList &t_SnmpVarBindList = a_SnmpPdu.GetVarbindList () ;
  473. SnmpTransportAddress &t_SrcTransportAddress = a_SnmpPdu.GetSourceAddress () ;
  474. SnmpTransportAddress &t_DstTransportAddress = a_SnmpPdu.GetDestinationAddress () ;
  475. if ( ! m_IsValid )
  476. {
  477. throw GeneralException (
  478. Snmp_Error,
  479. Snmp_Local_Error,
  480. __FILE__,
  481. __LINE__,
  482. SnmpGetLastError (( HSNMP_SESSION)m_Session)
  483. );
  484. }
  485. HSNMP_SESSION t_Session = ( HSNMP_SESSION ) m_Session ;
  486. HSNMP_VBL t_Vbl = SnmpCreateVbl ( t_Session , NULL , NULL ) ;
  487. if ( t_Vbl == SNMPAPI_FAILURE )
  488. {
  489. throw GeneralException (
  490. Snmp_Error,
  491. Snmp_Local_Error,
  492. __FILE__,
  493. __LINE__,
  494. SnmpGetLastError ( ( HSNMP_SESSION ) m_Session )
  495. ) ;
  496. }
  497. SetWinSnmpVbl ( t_SnmpVarBindList , t_Vbl ) ;
  498. smiINT t_RequestId = t_SnmpRequestId ;
  499. smiINT t_PduType = winsnmp_pdu_type[t_SnmpPduType] ;
  500. smiINT t_ErrorStatus = t_SnmpErrorReport.GetStatus () ;
  501. smiINT t_ErrorIndex = t_SnmpErrorReport.GetIndex () ;
  502. HSNMP_PDU t_Pdu ;
  503. t_Pdu = SnmpCreatePdu (
  504. t_Session,
  505. t_PduType,
  506. t_RequestId,
  507. t_ErrorStatus,
  508. t_ErrorIndex ,
  509. t_Vbl
  510. );
  511. if ( t_Pdu == SNMPAPI_FAILURE )
  512. {
  513. SnmpFreeVbl ( t_Vbl ) ;
  514. throw GeneralException (
  515. Snmp_Error,
  516. Snmp_Local_Error,
  517. __FILE__,
  518. __LINE__,
  519. SnmpGetLastError ( ( HSNMP_SESSION ) m_Session )
  520. ) ;
  521. }
  522. CriticalSectionLock t_CriticalSectionLock ( s_CriticalSection ) ;
  523. SnmpOctetString t_OctetString ( NULL , 0 ) ;
  524. t_SnmpCommunityBasedSecurity.GetCommunityName ( t_OctetString ) ;
  525. smiOCTETS t_Name;
  526. t_Name.len = t_OctetString.GetValueLength () ;
  527. t_Name.ptr = t_OctetString.GetValue () ;
  528. HSNMP_CONTEXT t_Context ;
  529. t_CriticalSectionLock.GetLock ( INFINITE ) ;
  530. SetTranslateMode () ;
  531. t_Context = SnmpStrToContext (
  532. t_Session,
  533. &t_Name
  534. ) ;
  535. t_CriticalSectionLock.UnLock () ;
  536. if ( t_Context == SNMPAPI_FAILURE )
  537. {
  538. SnmpFreePdu ( t_Pdu ) ;
  539. SnmpFreeVbl ( t_Vbl ) ;
  540. throw GeneralException (
  541. Snmp_Error,
  542. Snmp_Local_Error,
  543. __FILE__,
  544. __LINE__,
  545. SnmpGetLastError (t_Session)
  546. );
  547. }
  548. t_WinSnmpVariables->m_RequestId = t_RequestId;
  549. t_WinSnmpVariables->m_Pdu = t_Pdu ;
  550. t_WinSnmpVariables->m_Vbl = t_Vbl ;
  551. t_WinSnmpVariables->m_SrcEntity = NULL ;
  552. t_WinSnmpVariables->m_DstEntity = NULL ;
  553. t_WinSnmpVariables->m_Context = t_Context ;
  554. return TRUE ;
  555. }
  556. BOOL SnmpEncodeDecode :: DecodeFrame (
  557. IN void *a_ImplementationEncoding ,
  558. OUT SnmpPdu &a_SnmpPdu
  559. )
  560. {
  561. WinSnmpVariables *t_WinSnmpVariables = ( WinSnmpVariables * ) a_ImplementationEncoding ;
  562. RequestId t_SnmpRequestId ;
  563. PduType t_SnmpPduType ;
  564. SnmpErrorReport t_SnmpErrorReport ;
  565. SnmpCommunityBasedSecurity *t_SnmpCommunityBasedSecurity ;
  566. SnmpVarBindList *t_SnmpVarBindList ;
  567. SnmpTransportAddress *t_SrcTransportAddress ;
  568. SnmpTransportAddress *t_DstTransportAddress ;
  569. if ( ! m_IsValid )
  570. {
  571. throw GeneralException (
  572. Snmp_Error,
  573. Snmp_Local_Error,
  574. __FILE__,
  575. __LINE__,
  576. SnmpGetLastError (( HSNMP_SESSION)m_Session)
  577. );
  578. }
  579. HSNMP_ENTITY t_SrcEntity = t_WinSnmpVariables->m_SrcEntity ;
  580. HSNMP_ENTITY t_DstEntity = t_WinSnmpVariables->m_DstEntity ;
  581. HSNMP_CONTEXT t_Context = t_WinSnmpVariables->m_Context ;
  582. HSNMP_PDU t_Pdu = t_WinSnmpVariables->m_Pdu ;
  583. smiOCTETS t_Community ;
  584. SNMPAPI_STATUS t_Status = SnmpContextToStr (
  585. t_Context ,
  586. &t_Community
  587. );
  588. if (SNMPAPI_FAILURE == t_Status)
  589. {
  590. throw GeneralException (
  591. Snmp_Error,
  592. Snmp_Local_Error,
  593. __FILE__,
  594. __LINE__,
  595. SnmpGetLastError (( HSNMP_SESSION)m_Session)
  596. );
  597. }
  598. t_SnmpCommunityBasedSecurity = new SnmpCommunityBasedSecurity ;
  599. SnmpOctetString t_SnmpOctetString ( t_Community.ptr , t_Community.len ) ;
  600. SnmpFreeDescriptor ( SNMP_SYNTAX_OCTETS, &t_Community );
  601. t_SnmpCommunityBasedSecurity->SetCommunityName ( t_SnmpOctetString ) ;
  602. if ( t_SrcEntity )
  603. {
  604. char buff[MAX_ADDRESS_LEN];
  605. t_Status = SnmpEntityToStr(t_SrcEntity, MAX_ADDRESS_LEN, (LPSTR)buff);
  606. if (SNMPAPI_FAILURE == t_Status)
  607. {
  608. throw GeneralException (
  609. Snmp_Error,
  610. Snmp_Local_Error,
  611. __FILE__,
  612. __LINE__,
  613. SnmpGetLastError (( HSNMP_SESSION)m_Session)
  614. );
  615. }
  616. SnmpTransportIpAddress *t_SrcIpAddress = new SnmpTransportIpAddress (buff, SNMP_ADDRESS_RESOLVE_VALUE);
  617. if (t_SrcIpAddress->IsValid())
  618. {
  619. t_SrcTransportAddress = t_SrcIpAddress ;
  620. }
  621. else
  622. {
  623. delete t_SrcIpAddress ;
  624. SnmpTransportIpxAddress *t_SrcIpxAddress = new SnmpTransportIpxAddress (buff);
  625. if (t_SrcIpxAddress->IsValid())
  626. {
  627. t_SrcTransportAddress = t_SrcIpxAddress ;
  628. }
  629. else
  630. {
  631. delete t_SrcIpxAddress ;
  632. throw GeneralException (
  633. Snmp_Error,
  634. Snmp_Local_Error,
  635. __FILE__,
  636. __LINE__,
  637. SnmpGetLastError (( HSNMP_SESSION)m_Session)
  638. );
  639. }
  640. }
  641. }
  642. if ( t_DstEntity )
  643. {
  644. char buff[MAX_ADDRESS_LEN];
  645. t_Status = SnmpEntityToStr(t_DstEntity, MAX_ADDRESS_LEN, (LPSTR)buff);
  646. if (SNMPAPI_FAILURE == t_Status)
  647. {
  648. throw GeneralException (
  649. Snmp_Error,
  650. Snmp_Local_Error,
  651. __FILE__,
  652. __LINE__,
  653. SnmpGetLastError (( HSNMP_SESSION)m_Session)
  654. );
  655. }
  656. SnmpTransportIpAddress *t_DstIpAddress = new SnmpTransportIpAddress (buff, SNMP_ADDRESS_RESOLVE_VALUE);
  657. if (t_DstIpAddress->IsValid())
  658. {
  659. t_DstTransportAddress = t_DstIpAddress ;
  660. }
  661. else
  662. {
  663. delete t_DstIpAddress ;
  664. SnmpTransportIpxAddress *t_DstIpxAddress = new SnmpTransportIpxAddress (buff);
  665. if (t_DstIpxAddress->IsValid())
  666. {
  667. t_DstTransportAddress = t_DstIpxAddress ;
  668. }
  669. else
  670. {
  671. delete t_DstIpxAddress ;
  672. throw GeneralException (
  673. Snmp_Error,
  674. Snmp_Local_Error,
  675. __FILE__,
  676. __LINE__,
  677. SnmpGetLastError (( HSNMP_SESSION)m_Session)
  678. );
  679. }
  680. }
  681. }
  682. HSNMP_VBL t_Vbl ;
  683. smiINT t_RequestId ;
  684. smiINT t_PduType ;
  685. smiINT t_ErrorStatus ;
  686. smiINT t_ErrorIndex ;
  687. t_Status = SnmpGetPduData (
  688. t_Pdu,
  689. &t_PduType,
  690. &t_RequestId,
  691. &t_ErrorStatus,
  692. &t_ErrorIndex,
  693. &t_Vbl
  694. );
  695. if ( t_Status == SNMPAPI_FAILURE )
  696. {
  697. throw GeneralException (
  698. Snmp_Error,
  699. Snmp_Local_Error,
  700. __FILE__,
  701. __LINE__,
  702. SnmpGetLastError (( HSNMP_SESSION)m_Session)
  703. ) ;
  704. }
  705. if ( t_PduType == SNMP_PDU_GET )
  706. {
  707. t_SnmpPduType = GET ;
  708. }
  709. else if ( t_PduType == SNMP_PDU_SET )
  710. {
  711. t_SnmpPduType = SET ;
  712. }
  713. else if ( t_PduType == SNMP_PDU_GETNEXT )
  714. {
  715. t_SnmpPduType = GETNEXT ;
  716. }
  717. else if ( t_PduType == SNMP_PDU_RESPONSE )
  718. {
  719. t_SnmpPduType = RESPONSE ;
  720. }
  721. else if (t_PduType == SNMP_PDU_GETBULK)
  722. {
  723. t_SnmpPduType = GETBULK;
  724. }
  725. else if (t_PduType == SNMP_PDU_TRAP)
  726. {
  727. t_SnmpPduType = TRAP;
  728. }
  729. else if (t_PduType == SNMP_PDU_V1TRAP)
  730. {
  731. t_SnmpPduType = V1TRAP;
  732. }
  733. else if (t_PduType == SNMP_PDU_INFORM)
  734. {
  735. t_SnmpPduType = INFORM ;
  736. }
  737. else
  738. {
  739. t_SnmpPduType = UNKNOWN;
  740. }
  741. a_SnmpPdu.SetPduType(t_SnmpPduType);
  742. t_SnmpRequestId = t_RequestId ;
  743. t_SnmpErrorReport.SetStatus ( ( SnmpStatus ) t_ErrorStatus ) ;
  744. t_SnmpErrorReport.SetIndex ( t_ErrorIndex ) ;
  745. t_SnmpErrorReport.SetError ( t_ErrorStatus == SNMP_ERROR_NOERROR ? Snmp_Success : Snmp_Error ) ;
  746. t_SnmpVarBindList = new SnmpVarBindList ;
  747. if ( ! DecodeVarBindList ( t_Vbl , *t_SnmpVarBindList ) )
  748. {
  749. throw GeneralException (
  750. Snmp_Error,
  751. Snmp_Local_Error,
  752. __FILE__,
  753. __LINE__,
  754. SnmpGetLastError (( HSNMP_SESSION)m_Session)
  755. ) ;
  756. }
  757. a_SnmpPdu.SetRequestId ( t_SnmpRequestId ) ;
  758. a_SnmpPdu.SetErrorReport ( t_SnmpErrorReport ) ;
  759. a_SnmpPdu.SetSourceAddress ( *t_SrcTransportAddress ) ;
  760. a_SnmpPdu.SetDestinationAddress ( *t_DstTransportAddress ) ;
  761. a_SnmpPdu.SetVarBindList ( *t_SnmpVarBindList ) ;
  762. a_SnmpPdu.SetCommunityName ( *t_SnmpCommunityBasedSecurity ) ;
  763. DebugMacro4(
  764. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  765. __FILE__,__LINE__,
  766. L"SnmpEncodeDecode :: DecodeFrame- received frame_id (%d) \r\n\r\n" , t_RequestId
  767. ) ;
  768. smiOCTETS t_Octets ;
  769. if ( SnmpEncodeMsg (
  770. m_Session,
  771. t_SrcEntity,
  772. t_DstEntity,
  773. t_Context,
  774. t_Pdu ,
  775. & t_Octets
  776. ) != SNMPAPI_FAILURE )
  777. {
  778. ULONG t_Len = t_Octets.len ;
  779. UCHAR *t_Ptr = t_Octets.ptr ;
  780. ULONG t_RowLength = t_Len / 16 ;
  781. ULONG t_Remainder = t_Len % 16 ;
  782. ULONG t_Index = 0 ;
  783. for ( ULONG t_RowIndex = 0 ; t_RowIndex < t_RowLength ; t_RowIndex ++ )
  784. {
  785. ULONG t_StoredIndex = t_Index ;
  786. for ( ULONG t_ColumnIndex = 0 ; t_ColumnIndex < 16 ; t_ColumnIndex ++ )
  787. {
  788. SnmpDebugLog :: s_SnmpDebugLog->Write ( L"%2.2lx " , t_Ptr [ t_Index ++ ] ) ;
  789. }
  790. SnmpDebugLog :: s_SnmpDebugLog->Write ( L" " ) ;
  791. for ( t_ColumnIndex = 0 ; t_ColumnIndex < 16 ; t_ColumnIndex ++ )
  792. {
  793. if ( ( t_Ptr [ t_StoredIndex ] >= 0x20 ) && ( t_Ptr [ t_StoredIndex ] <= 0x7f ) )
  794. {
  795. SnmpDebugLog :: s_SnmpDebugLog->Write ( L"%c" , t_Ptr [ t_StoredIndex ] ) ;
  796. }
  797. else
  798. {
  799. SnmpDebugLog :: s_SnmpDebugLog->Write ( L"." ) ;
  800. }
  801. t_StoredIndex ++ ;
  802. }
  803. SnmpDebugLog :: s_SnmpDebugLog->Write ( L"\r\n" ) ;
  804. }
  805. ULONG t_StoredIndex = t_Index ;
  806. for ( ULONG t_ColumnIndex = 0 ; t_ColumnIndex < 16 ; t_ColumnIndex ++ )
  807. {
  808. if ( t_ColumnIndex < t_Remainder )
  809. {
  810. SnmpDebugLog :: s_SnmpDebugLog->Write ( L"%2.2lx " , t_Ptr [ t_Index ++ ] ) ;
  811. }
  812. else
  813. {
  814. SnmpDebugLog :: s_SnmpDebugLog->Write ( L" " ) ;
  815. }
  816. }
  817. SnmpDebugLog :: s_SnmpDebugLog->Write ( L" " ) ;
  818. for ( t_ColumnIndex = 0 ; t_ColumnIndex < 16 ; t_ColumnIndex ++ )
  819. {
  820. if ( t_ColumnIndex < t_Remainder )
  821. {
  822. if ( t_Ptr [ t_StoredIndex ] >= 0x20 && t_Ptr [ t_StoredIndex ] <= 0x7f )
  823. {
  824. SnmpDebugLog :: s_SnmpDebugLog->Write ( L"%c" , t_Ptr [ t_StoredIndex ] ) ;
  825. }
  826. else
  827. {
  828. SnmpDebugLog :: s_SnmpDebugLog->Write ( L"." ) ;
  829. }
  830. t_StoredIndex ++ ;
  831. }
  832. }
  833. SnmpDebugLog :: s_SnmpDebugLog->Write ( L"\r\n\r\n" ) ;
  834. SnmpFreeDescriptor (
  835. SNMP_SYNTAX_OCTETS ,
  836. & t_Octets
  837. ) ;
  838. }
  839. else
  840. {
  841. DWORD t_LastError = SnmpGetLastError ((HSNMP_SESSION) m_Session) ;
  842. SnmpDebugLog :: s_SnmpDebugLog->Write ( L"Encode Failure\r\n\r\n" ) ;
  843. }
  844. )
  845. SnmpFreeVbl ( t_Vbl ) ;
  846. return TRUE ;
  847. }
  848. BOOL SnmpEncodeDecode :: SetRequestId (
  849. IN OUT SnmpPdu &a_SnmpPdu ,
  850. IN RequestId a_RequestId
  851. )
  852. {
  853. return a_SnmpPdu.SetRequestId ( a_RequestId ) ;
  854. }
  855. BOOL SnmpEncodeDecode :: SetVarBindList (
  856. IN SnmpPdu &a_SnmpPdu ,
  857. OUT SnmpVarBindList &a_SnmpVarBindList
  858. )
  859. {
  860. return a_SnmpPdu.SetVarBindList ( a_SnmpVarBindList ) ;
  861. }
  862. BOOL SnmpEncodeDecode :: SetCommunityName (
  863. IN SnmpPdu &a_SnmpPdu ,
  864. IN SnmpCommunityBasedSecurity &a_SnmpCommunityBasedSecurity
  865. )
  866. {
  867. return a_SnmpPdu.SetCommunityName ( a_SnmpCommunityBasedSecurity ) ;
  868. }
  869. BOOL SnmpEncodeDecode :: SetErrorReport (
  870. IN SnmpPdu &a_SnmpPdu ,
  871. OUT SnmpErrorReport &a_SnmpErrorReport
  872. )
  873. {
  874. return a_SnmpPdu.SetErrorReport ( a_SnmpErrorReport ) ;
  875. }
  876. BOOL SnmpEncodeDecode :: SetPduType (
  877. IN SnmpPdu &a_SnmpPdu ,
  878. OUT PduType a_PduType
  879. )
  880. {
  881. return a_SnmpPdu.SetPduType ( a_PduType ) ;
  882. }
  883. BOOL SnmpEncodeDecode :: SetSourceAddress (
  884. IN OUT SnmpPdu &a_SnmpPdu ,
  885. IN SnmpTransportAddress &a_TransportAddress
  886. )
  887. {
  888. return a_SnmpPdu.SetSourceAddress ( a_TransportAddress ) ;
  889. }
  890. BOOL SnmpEncodeDecode :: SetDestinationAddress (
  891. IN OUT SnmpPdu &a_SnmpPdu ,
  892. IN SnmpTransportAddress &a_TransportAddress
  893. )
  894. {
  895. return a_SnmpPdu.SetDestinationAddress ( a_TransportAddress ) ;
  896. }
  897. BOOL SnmpEncodeDecode :: GetSourceAddress (
  898. IN SnmpPdu &a_SnmpPdu ,
  899. SnmpTransportAddress *&a_TransportAddress
  900. )
  901. {
  902. a_TransportAddress = a_SnmpPdu.GetSourceAddress ().Copy () ;
  903. return TRUE ;
  904. }
  905. BOOL SnmpEncodeDecode :: GetDestinationAddress (
  906. IN SnmpPdu &a_SnmpPdu ,
  907. SnmpTransportAddress *&a_TransportAddress
  908. )
  909. {
  910. a_TransportAddress = a_SnmpPdu.GetDestinationAddress ().Copy () ;
  911. return TRUE ;
  912. }
  913. BOOL SnmpEncodeDecode :: GetPduType (
  914. IN SnmpPdu &a_SnmpPdu ,
  915. OUT PduType &a_PduType
  916. )
  917. {
  918. a_PduType = a_SnmpPdu.GetPduType () ;
  919. return TRUE ;
  920. }
  921. BOOL SnmpEncodeDecode :: GetRequestId (
  922. IN SnmpPdu &a_SnmpPdu ,
  923. RequestId &a_RequestId
  924. )
  925. {
  926. a_RequestId = a_SnmpPdu.GetRequestId () ;
  927. return TRUE ;
  928. }
  929. BOOL SnmpEncodeDecode :: GetErrorReport (
  930. IN SnmpPdu &a_SnmpPdu ,
  931. OUT SnmpErrorReport &a_SnmpErrorReport
  932. )
  933. {
  934. a_SnmpErrorReport = a_SnmpPdu.GetErrorReport () ;
  935. return TRUE ;
  936. }
  937. BOOL SnmpEncodeDecode :: GetVarbindList (
  938. IN SnmpPdu &a_SnmpPdu ,
  939. OUT SnmpVarBindList &a_SnmpVarBindList
  940. )
  941. {
  942. a_SnmpVarBindList = a_SnmpPdu.GetVarbindList () ;
  943. return TRUE ;
  944. }
  945. BOOL SnmpEncodeDecode :: GetCommunityName (
  946. IN SnmpPdu &a_SnmpPdu ,
  947. OUT SnmpCommunityBasedSecurity &a_SnmpCommunityBasedSecurity
  948. )
  949. {
  950. a_SnmpCommunityBasedSecurity = a_SnmpPdu.GetCommunityName () ;
  951. return TRUE;
  952. }
  953. SnmpV1EncodeDecode::SnmpV1EncodeDecode ()
  954. {
  955. try
  956. {
  957. InitializeVariables();
  958. }
  959. catch ( Heap_Exception e_He )
  960. {
  961. }
  962. catch(GeneralException exception)
  963. {
  964. }
  965. }
  966. SnmpV1EncodeDecode::~SnmpV1EncodeDecode(void)
  967. {
  968. }
  969. void SnmpV1EncodeDecode::InitializeVariables()
  970. {
  971. m_IsValid = FALSE;
  972. smiUINT32 t_MajorVersion = 1 ;
  973. smiUINT32 t_MinorVersion = 1 ;
  974. smiUINT32 t_Level = 2 ;
  975. smiUINT32 t_TranslateMode = SNMPAPI_UNTRANSLATED_V1 ;
  976. smiUINT32 t_RetransmitMode = SNMPAPI_OFF ;
  977. SNMPAPI_STATUS t_StartupStatus = SnmpStartup (
  978. &t_MajorVersion,
  979. &t_MinorVersion,
  980. &t_Level,
  981. &t_TranslateMode,
  982. &t_RetransmitMode
  983. );
  984. if ( t_StartupStatus == SNMPAPI_FAILURE )
  985. {
  986. DWORD t_LastError = SnmpGetLastError ( 0 ) ;
  987. throw GeneralException (
  988. Snmp_Error,
  989. Snmp_Local_Error,
  990. __FILE__,
  991. __LINE__
  992. );
  993. }
  994. HSNMP_SESSION t_Session ;
  995. t_Session = SnmpOpen (
  996. ( ( Window * ) m_Window ) ->GetWindowHandle (),
  997. Window :: g_NullEventId
  998. ) ;
  999. if ( t_Session == SNMPAPI_FAILURE )
  1000. {
  1001. DWORD t_LastError = SnmpGetLastError ( 0 ) ;
  1002. throw GeneralException (
  1003. Snmp_Error,
  1004. Snmp_Local_Error,
  1005. __FILE__,
  1006. __LINE__
  1007. );
  1008. }
  1009. m_Session = ( void * ) t_Session ;
  1010. m_IsValid = TRUE;
  1011. }
  1012. void SnmpV1EncodeDecode :: SetTranslateMode ()
  1013. {
  1014. SnmpSetTranslateMode ( SNMPAPI_UNTRANSLATED_V1 ) ;
  1015. }
  1016. SnmpV2CEncodeDecode::SnmpV2CEncodeDecode ()
  1017. {
  1018. InitializeVariables();
  1019. }
  1020. SnmpV2CEncodeDecode::~SnmpV2CEncodeDecode(void)
  1021. {
  1022. }
  1023. void SnmpV2CEncodeDecode::InitializeVariables()
  1024. {
  1025. m_IsValid = FALSE;
  1026. smiUINT32 t_MajorVersion = 1 ;
  1027. smiUINT32 t_MinorVersion = 1 ;
  1028. smiUINT32 t_Level = 2 ;
  1029. smiUINT32 t_TranslateMode = SNMPAPI_UNTRANSLATED_V2 ;
  1030. smiUINT32 t_RetransmitMode = SNMPAPI_OFF ;
  1031. SNMPAPI_STATUS t_StartupStatus = SnmpStartup (
  1032. &t_MajorVersion,
  1033. &t_MinorVersion,
  1034. &t_Level,
  1035. &t_TranslateMode,
  1036. &t_RetransmitMode
  1037. );
  1038. if ( t_StartupStatus == SNMPAPI_FAILURE )
  1039. {
  1040. throw GeneralException (
  1041. Snmp_Error,
  1042. Snmp_Local_Error,
  1043. __FILE__,
  1044. __LINE__
  1045. );
  1046. }
  1047. HSNMP_SESSION t_Session ;
  1048. t_Session = SnmpOpen (
  1049. ( ( Window * ) m_Window ) ->GetWindowHandle (),
  1050. Window :: g_NullEventId
  1051. ) ;
  1052. if ( t_Session == SNMPAPI_FAILURE )
  1053. {
  1054. DWORD t_LastError = SnmpGetLastError ( 0 ) ;
  1055. throw GeneralException (
  1056. Snmp_Error,
  1057. Snmp_Local_Error,
  1058. __FILE__,
  1059. __LINE__
  1060. );
  1061. }
  1062. m_Session = ( void * ) t_Session ;
  1063. m_IsValid = TRUE;
  1064. }
  1065. void SnmpV2CEncodeDecode :: SetTranslateMode ()
  1066. {
  1067. SnmpSetTranslateMode ( SNMPAPI_UNTRANSLATED_V2 ) ;
  1068. }