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.

971 lines
21 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. #include "precomp.h"
  13. #include <provexpt.h>
  14. #include <snmptempl.h>
  15. #include <snmpmt.h>
  16. #include <typeinfo.h>
  17. #include <process.h>
  18. #include <objbase.h>
  19. #include <stdio.h>
  20. #include <wbemidl.h>
  21. #include <snmplog.h>
  22. #include <snmpcl.h>
  23. #include <snmpcont.h>
  24. #include <snmptype.h>
  25. #include <snmpauto.h>
  26. #include <snmpevt.h>
  27. #include <snmpthrd.h>
  28. #include <snmpobj.h>
  29. #include <smir.h>
  30. #include <notify.h>
  31. #include <evtdefs.h>
  32. #include <evtthrd.h>
  33. #include <evtmap.h>
  34. #include <evtprov.h>
  35. #include <winsock2.h>
  36. CMapToEvent::CMapToEvent() : m_vbdefn(NULL), m_vbs(NULL, 0)
  37. {
  38. m_btriedGeneric = FALSE;
  39. m_btryGeneric = FALSE;
  40. m_bCheckedVersion = FALSE;
  41. m_object = NULL;
  42. m_nspace = NULL;
  43. }
  44. CMapToEvent::~CMapToEvent()
  45. {
  46. if (m_vbs.vbs)
  47. {
  48. delete [] m_vbs.vbs;
  49. }
  50. if (m_vbdefn)
  51. {
  52. m_vbdefn->Release();
  53. }
  54. }
  55. void CMapToEvent::ResetData()
  56. {
  57. m_btriedGeneric = FALSE;
  58. m_btryGeneric = FALSE;
  59. m_bCheckedVersion = FALSE;
  60. m_class.Empty();
  61. if(m_vbs.vbs)
  62. {
  63. delete [] m_vbs.vbs;
  64. m_vbs.vbs = NULL;
  65. m_vbs.length = 0;
  66. }
  67. if (m_object)
  68. {
  69. m_object->Release();
  70. m_object = NULL;
  71. }
  72. }
  73. void CMapToEvent::SetTryGeneric()
  74. {
  75. m_btriedGeneric = TRUE;
  76. m_btryGeneric = TRUE;
  77. }
  78. BOOL CMapToEvent::IsSNMPv1()
  79. {
  80. if (!m_bCheckedVersion)
  81. {
  82. m_bCheckedVersion = TRUE;
  83. m_bSNMPv1 = FALSE;
  84. //is v1 if the last varbind has objid = snmpTrapEnterprise.0
  85. //and the trapOID truncated by it last sub-id equals the last
  86. //varbind's value with a .0 appended.
  87. //Get the last varbind's oid and make sure it's the enterpriseOid
  88. const SnmpObjectIdentifier& id = m_vbs.vbs[m_vbs.length - 1].pVarBind->GetInstance();
  89. const SnmpValue& snmp_val = m_vbs.vbs[m_vbs.length - 1].pVarBind->GetValue();
  90. if ((id != SNMP_ENT_OID) || (typeid(SnmpObjectIdentifier) != typeid(snmp_val)))
  91. {
  92. return FALSE;
  93. }
  94. //create the trapoid and truncate the last subid
  95. SnmpObjectIdentifierType trapid(m_oid);
  96. SnmpObjectIdentifier trunctrapid(trapid.GetValue(), trapid.GetValueLength() - 1);
  97. //now add .0 to the value of the enterpriseOid
  98. ULONG val[1] = {0};
  99. SnmpObjectIdentifier zeroid(val, 1);
  100. SnmpObjectIdentifier extendoid((const SnmpObjectIdentifier&)snmp_val + zeroid);
  101. m_bSNMPv1 = (extendoid == trunctrapid);
  102. }
  103. return m_bSNMPv1;
  104. }
  105. void CMapToEvent::SetData(const char* sender_addr, const char* security_Context,
  106. const char* snmpTrapOid, const char* transport,
  107. SnmpVarBindList& vbList, CWbemServerWrap* nspace)
  108. {
  109. m_nspace = nspace;
  110. m_addr = sender_addr;
  111. m_ctxt = security_Context;
  112. m_oid = snmpTrapOid;
  113. m_transport = transport;
  114. UINT length = vbList.GetLength();
  115. if (length >= 2) //must have at least two varbinds!
  116. {
  117. m_vbs.vbs = new VarBindObjectStruct[length];
  118. m_vbs.length = length;
  119. vbList.Reset();
  120. for (UINT i=0; i < length; i++)
  121. {
  122. vbList.Next () ;
  123. m_vbs.vbs[i].fDone = FALSE;
  124. m_vbs.vbs[i].pVarBind = (SnmpVarBind*) vbList.Get();
  125. }
  126. }
  127. else
  128. {
  129. m_vbs.vbs = NULL;
  130. m_vbs.length = 0;
  131. }
  132. }
  133. void CMapToEvent::GetClassInstance(IWbemClassObject **ppObj)
  134. {
  135. DebugMacro9(
  136. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  137. __FILE__,__LINE__,
  138. L"CMapToEvent::GetClassInstance getting class object\r\n");
  139. )
  140. if (m_object != NULL)
  141. {
  142. *ppObj = m_object;
  143. return;
  144. }
  145. if (m_class.IsEmpty())
  146. {
  147. if (!GetClass() || m_class.IsEmpty())
  148. {
  149. *ppObj = NULL;
  150. DebugMacro9(
  151. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  152. __FILE__,__LINE__,
  153. L"CMapToEvent::GetClassInstance failed to get class name\r\n");
  154. )
  155. return;
  156. }
  157. }
  158. BSTR path = m_class.AllocSysString();
  159. if (SUCCEEDED(m_nspace->GetObject(path, NULL, ppObj )))
  160. {
  161. if (FAILED((*ppObj)->SpawnInstance(0, &m_object)))
  162. {
  163. DebugMacro9(
  164. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  165. __FILE__,__LINE__,
  166. L"CMapToEvent::GetClassInstance failed to spawn instance\r\n");
  167. )
  168. m_object = NULL;
  169. }
  170. else
  171. {
  172. DebugMacro9(
  173. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  174. __FILE__,__LINE__,
  175. L"CMapToEvent::GetClassInstance got instance\r\n");
  176. )
  177. }
  178. (*ppObj)->Release();
  179. }
  180. else
  181. {
  182. DebugMacro9(
  183. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  184. __FILE__,__LINE__,
  185. L"CMapToEvent::GetClassInstance failed to get class definition\r\n");
  186. )
  187. }
  188. SysFreeString(path);
  189. *ppObj = m_object;
  190. }
  191. HRESULT CMapToEvent::GetPropertyValue(long lNumElements, MYWBEM_NAME_ELEMENT *aElements,
  192. long lFlags, VARIANT *pvValue)
  193. {
  194. VariantInit(pvValue);
  195. if ((lNumElements == 0) || (NULL == aElements) || (NULL == pvValue) || (pvValue->vt != VT_EMPTY))
  196. {
  197. return E_INVALIDARG;
  198. }
  199. if (m_vbs.length == 0)
  200. {
  201. return WBEM_E_FAILED;
  202. }
  203. //try the standard properties first
  204. HRESULT result = GetStandardProperty(lNumElements, aElements, lFlags, pvValue);
  205. if (SUCCEEDED(result))
  206. {
  207. return result;
  208. }
  209. if (m_class.IsEmpty())
  210. {
  211. if (!GetClass() || m_class.IsEmpty())
  212. {
  213. return WBEM_E_FAILED;
  214. }
  215. }
  216. if (TriedGeneric())
  217. {
  218. //if we are generic, check the varbind properties
  219. return GetVBProperty(lNumElements, aElements, lFlags, pvValue);
  220. }
  221. else
  222. {
  223. //else check the specific properties...
  224. return GetSpecificPropertyValue(lNumElements, aElements, lFlags, pvValue);
  225. }
  226. return WBEM_E_FAILED;
  227. }
  228. HRESULT CMapToEvent::GetStandardProperty(long lNumElements,
  229. MYWBEM_NAME_ELEMENT *aElements,
  230. long lFlags,
  231. VARIANT *pvValue)
  232. {
  233. if ((lNumElements == 1) && (0 == aElements[0].m_nType)) //the property name!
  234. {
  235. if (0 == _wcsicmp(aElements[0].Element.m_wszPropertyName, WBEMS_CLASS_PROP))
  236. {
  237. if (m_class.IsEmpty())
  238. {
  239. if (!GetClass() || m_class.IsEmpty())
  240. {
  241. return E_FAIL;
  242. }
  243. }
  244. pvValue->vt = VT_BSTR;
  245. pvValue->bstrVal = m_class.AllocSysString();
  246. }
  247. else if (0 == _wcsicmp(aElements[0].Element.m_wszPropertyName, EVENT_SOID_PROP))
  248. {
  249. if (m_oid.IsEmpty())
  250. {
  251. return E_FAIL;
  252. }
  253. pvValue->vt = VT_BSTR;
  254. pvValue->bstrVal = m_oid.AllocSysString();
  255. }
  256. else if (0 == _wcsicmp(aElements[0].Element.m_wszPropertyName, EVENT_ADDR_PROP))
  257. {
  258. if (m_addr.IsEmpty())
  259. {
  260. return E_FAIL;
  261. }
  262. pvValue->vt = VT_BSTR;
  263. pvValue->bstrVal = m_addr.AllocSysString();
  264. }
  265. else if (0 == _wcsicmp(aElements[0].Element.m_wszPropertyName, EVENT_TADDR_PROP))
  266. {
  267. if (m_addr.IsEmpty())
  268. {
  269. return E_FAIL;
  270. }
  271. pvValue->vt = VT_BSTR;
  272. pvValue->bstrVal = m_addr.AllocSysString();
  273. }
  274. else if (0 == _wcsicmp(aElements[0].Element.m_wszPropertyName, EVENT_TRANS_PROP))
  275. {
  276. if (m_transport.IsEmpty())
  277. {
  278. return E_FAIL;
  279. }
  280. pvValue->vt = VT_BSTR;
  281. pvValue->bstrVal = m_transport.AllocSysString();
  282. }
  283. else if (0 == _wcsicmp(aElements[0].Element.m_wszPropertyName, EVENT_COMM_PROP))
  284. {
  285. if (m_ctxt.IsEmpty())
  286. {
  287. return E_FAIL;
  288. }
  289. pvValue->vt = VT_BSTR;
  290. pvValue->bstrVal = m_ctxt.AllocSysString();
  291. }
  292. else if (0 == _wcsicmp(aElements[0].Element.m_wszPropertyName, EVENT_TIME_PROP))
  293. {
  294. const SnmpObjectIdentifier& id = m_vbs.vbs[0].pVarBind->GetInstance();
  295. if (id != SNMP_SYS_UP_OID)
  296. {
  297. return E_FAIL;
  298. }
  299. const SnmpValue& val = m_vbs.vbs[0].pVarBind->GetValue();
  300. if (typeid(SnmpTimeTicks) == typeid(val))
  301. {
  302. pvValue->vt = VT_I4;
  303. pvValue->lVal = ((const SnmpTimeTicks&)val).GetValue();
  304. }
  305. else
  306. {
  307. return E_FAIL;
  308. }
  309. }
  310. else
  311. {
  312. return E_FAIL;
  313. }
  314. }
  315. else
  316. {
  317. return E_FAIL; //no standard property has more than one element to its name
  318. }
  319. return S_OK; //got the property!
  320. }
  321. HRESULT CMapToEvent::GetVBProperty(long lNumElements,
  322. MYWBEM_NAME_ELEMENT *aElements,
  323. long lFlags,
  324. VARIANT *pvValue)
  325. {
  326. if ((lNumElements <= 0) || (NULL == aElements) || (NULL == pvValue))
  327. {
  328. return E_INVALIDARG;
  329. }
  330. HRESULT status = E_FAIL;
  331. if ((0 == aElements[0].m_nType) && (m_vbs.length > 2) &&
  332. (0 == _wcsicmp(aElements[0].Element.m_wszPropertyName, EVENT_VBL_PROP)) )
  333. {
  334. //the property name is correct...
  335. if (lNumElements == 1)
  336. {
  337. //all the varbinds except 1 and 2...
  338. //create the classobjects stick 'em in a safearray and send 'em back!
  339. SAFEARRAYBOUND rgsabound[1];
  340. SAFEARRAY* psa;
  341. rgsabound[0].lLbound = 0;
  342. rgsabound[0].cElements = m_vbs.length - 2;
  343. psa = SafeArrayCreate(VT_UNKNOWN, 1, rgsabound);
  344. LONG ix;
  345. BOOL berror = FALSE;
  346. for (UINT i = 2; i < m_vbs.length; i++)
  347. {
  348. IWbemClassObject* vbobj = GetVBClassObjectByIndex(i);
  349. if (NULL != vbobj)
  350. {
  351. ix = i-2;
  352. if ( S_OK != SafeArrayPutElement(psa, &ix, (IUnknown *)vbobj))
  353. {
  354. vbobj->Release ();
  355. berror = TRUE;
  356. break;
  357. }
  358. vbobj->Release ();
  359. }
  360. else
  361. {
  362. berror = TRUE;
  363. break;
  364. }
  365. }
  366. if (berror)
  367. {
  368. SafeArrayDestroy(psa);
  369. }
  370. else
  371. {
  372. status = WBEM_NO_ERROR;
  373. pvValue->vt = VT_ARRAY | VT_UNKNOWN;
  374. pvValue->parray = psa;
  375. }
  376. }
  377. else if ((1 == aElements[1].m_nType) && (m_vbs.length > (aElements[1].Element.m_lArrayIndex + 2)))
  378. {
  379. if (lNumElements == 2)
  380. {
  381. //get the n(= aElements[1].Element.m_lArrayIndex + 2)th varbind
  382. //create a classobject with it and send back the class object
  383. IWbemClassObject* vbobj = GetVBClassObjectByIndex(aElements[1].Element.m_lArrayIndex + 2);
  384. if (NULL != vbobj)
  385. {
  386. status = WBEM_NO_ERROR;
  387. pvValue->vt = VT_UNKNOWN;
  388. pvValue->punkVal = (IUnknown*) vbobj;
  389. }
  390. }
  391. else if (0 == aElements[2].m_nType)
  392. {
  393. if (lNumElements == 3)
  394. {
  395. //get the single property value
  396. int cmpval = _wcsicmp(aElements[2].Element.m_wszPropertyName, VB_OBJID_PROP2);
  397. if (0 == cmpval)
  398. {
  399. //get the objectid
  400. if (GetVBPropOIDByIndex((aElements[1].Element.m_lArrayIndex + 2), *pvValue))
  401. {
  402. status = WBEM_NO_ERROR;
  403. }
  404. }
  405. else if ((cmpval > 0) &&
  406. (0 == _wcsicmp(aElements[2].Element.m_wszPropertyName, VB_ENCODING_PROP1)))
  407. {
  408. //get the ASNType (Encoding < ObjectIdentifier)
  409. CString t;
  410. if (GetVBPropValueByIndex((aElements[1].Element.m_lArrayIndex + 2), t, *pvValue))
  411. {
  412. VariantClear(pvValue);
  413. status = WBEM_NO_ERROR;
  414. pvValue->vt = VT_BSTR;
  415. pvValue->bstrVal = t.AllocSysString();
  416. }
  417. }
  418. else if (0 == _wcsicmp(aElements[2].Element.m_wszPropertyName, VB_VALUE_PROP3))
  419. {
  420. //get the Value (Value < ObjectID)
  421. CString t;
  422. if (GetVBPropValueByIndex((aElements[1].Element.m_lArrayIndex + 2), t, *pvValue))
  423. {
  424. status = WBEM_NO_ERROR;
  425. }
  426. }
  427. }
  428. else if ((lNumElements == 4) && (1 == aElements[3].m_nType) &&
  429. (0 == _wcsicmp(aElements[2].Element.m_wszPropertyName, VB_VALUE_PROP3)) )
  430. {
  431. //get the byte value of Value[x]
  432. CString t;
  433. VARIANT v;
  434. if (GetVBPropValueByIndex((aElements[1].Element.m_lArrayIndex + 2), t, v))
  435. {
  436. if ((VT_ARRAY | VT_UI1) == v.vt)
  437. {
  438. LONG ix = aElements[3].Element.m_lArrayIndex;
  439. UCHAR datum;
  440. if (S_OK == SafeArrayGetElement(v.parray, &ix, (void*)&datum))
  441. {
  442. status = WBEM_NO_ERROR;
  443. pvValue->vt = VT_UI1;
  444. pvValue->bVal = datum;
  445. }
  446. }
  447. VariantClear(&v);
  448. }
  449. }
  450. }
  451. }
  452. }
  453. return status;
  454. }
  455. IWbemClassObject* CMapToEvent::GetVBClassDefn()
  456. {
  457. if (NULL == m_vbdefn)
  458. {
  459. BSTR path = SysAllocString(VB_CLASS_PATH);
  460. m_nspace->GetObject(path, NULL, &m_vbdefn ) ;
  461. SysFreeString(path);
  462. }
  463. return m_vbdefn;
  464. }
  465. IWbemClassObject* CMapToEvent::GetVBClassObjectByIndex(UINT index)
  466. {
  467. IWbemClassObject* ret = NULL;
  468. if (NULL != GetVBClassDefn())
  469. {
  470. if (SUCCEEDED(m_vbdefn->SpawnInstance(0, &ret)))
  471. {
  472. CString type;
  473. VARIANT vV;
  474. if (GetVBPropValueByIndex(index, type, vV))
  475. {
  476. BSTR asntypeprop = SysAllocString(VB_ENCODING_PROP1);
  477. VARIANT vT;
  478. vT.vt = VT_BSTR;
  479. vT.bstrVal = type.AllocSysString();
  480. //set the type and value.
  481. if (SUCCEEDED(ret->Put(asntypeprop, 0, &vT, 0)))
  482. {
  483. BSTR valueprop = SysAllocString(VB_VALUE_PROP3);
  484. VARIANT* pV = &vV;
  485. VARTYPE Vtype = 0;
  486. if ((VT_EMPTY == vV.vt) || (VT_EMPTY == vV.vt))
  487. {
  488. Vtype = VT_ARRAY|VT_UI1; //default type
  489. pV = NULL;
  490. }
  491. if (SUCCEEDED(ret->Put(valueprop, 0, pV, Vtype)))
  492. {
  493. VARIANT vID;
  494. //get the oid
  495. if(GetVBPropOIDByIndex(index, vID))
  496. {
  497. BSTR oidprop = SysAllocString(VB_OBJID_PROP2);
  498. //set the oid
  499. if (FAILED(ret->Put(oidprop, 0, &vID, 0)))
  500. {
  501. ret->Release();
  502. ret = NULL;
  503. }
  504. SysFreeString(oidprop);
  505. VariantClear(&vID);
  506. }
  507. else
  508. {
  509. ret->Release();
  510. ret = NULL;
  511. }
  512. }
  513. else
  514. {
  515. ret->Release();
  516. ret = NULL;
  517. }
  518. SysFreeString(valueprop);
  519. }
  520. else //failed to put the type property
  521. {
  522. ret->Release();
  523. ret = NULL;
  524. }
  525. VariantClear(&vT);
  526. SysFreeString(asntypeprop);
  527. VariantClear(&vV);
  528. }
  529. else //failed to SpawnInstance
  530. {
  531. ret->Release();
  532. ret = NULL;
  533. }
  534. }
  535. }
  536. return ret;
  537. }
  538. //index assumed to be in valid range
  539. BOOL CMapToEvent::GetVBPropValueByIndex(UINT index, CString& type, VARIANT& vval)
  540. {
  541. const SnmpValue& val = m_vbs.vbs[index].pVarBind->GetValue();
  542. //the data is an array of bytes...
  543. SAFEARRAYBOUND rgsabound[1];
  544. SAFEARRAY* psa;
  545. rgsabound[0].lLbound = 0;
  546. VariantInit(&vval);
  547. if (typeid(SnmpNull) == typeid(val))
  548. {
  549. type = ASN_NULL;
  550. }
  551. else if (typeid(SnmpNoSuchObject) == typeid(val))
  552. {
  553. type = ASN_NSO;
  554. }
  555. else if (typeid(SnmpNoSuchInstance) == typeid(val))
  556. {
  557. type = ASN_NSI;
  558. }
  559. else if (typeid(SnmpEndOfMibView) == typeid(val))
  560. {
  561. type = ASN_EOMV;
  562. }
  563. else if (typeid(SnmpInteger) == typeid(val))
  564. {
  565. type = ASN_INTEGER;
  566. UCHAR* pdata;
  567. UINT datalen = sizeof(UINT);
  568. rgsabound[0].cElements = datalen;
  569. psa = SafeArrayCreate(VT_UI1, 1, rgsabound);
  570. if (NULL == psa)
  571. {
  572. return FALSE; //out of memory!!!
  573. }
  574. if (FAILED(SafeArrayAccessData(psa, (void HUGEP* FAR*)&pdata)))
  575. {
  576. return FALSE;
  577. }
  578. UINT data = ((const SnmpInteger&)val).GetValue();
  579. memcpy((void *)pdata, (void *)&data, datalen);
  580. SafeArrayUnaccessData(psa);
  581. vval.vt = VT_ARRAY|VT_UI1;
  582. vval.parray = psa;
  583. }
  584. else if(typeid(SnmpGauge) == typeid(val))
  585. {
  586. type = ASN_GUAGE;
  587. UCHAR* pdata;
  588. UINT datalen = sizeof(UINT);
  589. rgsabound[0].cElements = datalen;
  590. psa = SafeArrayCreate(VT_UI1, 1, rgsabound);
  591. if (NULL == psa)
  592. {
  593. return FALSE; //out of memory!!!
  594. }
  595. if (FAILED(SafeArrayAccessData(psa, (void HUGEP* FAR*)&pdata)))
  596. {
  597. return FALSE;
  598. }
  599. UINT data = ((const SnmpGauge&)val).GetValue();
  600. memcpy((void *)pdata, (void *)&data, datalen);
  601. SafeArrayUnaccessData(psa);
  602. vval.vt = VT_ARRAY|VT_UI1;
  603. vval.parray = psa;
  604. }
  605. else if(typeid(SnmpCounter) == typeid(val))
  606. {
  607. type = ASN_COUNTER;
  608. UCHAR* pdata;
  609. UINT datalen = sizeof(UINT);
  610. rgsabound[0].cElements = datalen;
  611. psa = SafeArrayCreate(VT_UI1, 1, rgsabound);
  612. if (NULL == psa)
  613. {
  614. return FALSE; //out of memory!!!
  615. }
  616. if (FAILED(SafeArrayAccessData(psa, (void HUGEP* FAR*)&pdata)))
  617. {
  618. return FALSE;
  619. }
  620. UINT data = ((const SnmpCounter&)val).GetValue();
  621. memcpy((void *)pdata, (void *)&data, datalen);
  622. SafeArrayUnaccessData(psa);
  623. vval.vt = VT_ARRAY|VT_UI1;
  624. vval.parray = psa;
  625. }
  626. else if(typeid(SnmpTimeTicks) == typeid(val))
  627. {
  628. type = ASN_TIME;
  629. UCHAR* pdata;
  630. UINT datalen = sizeof(UINT);
  631. rgsabound[0].cElements = datalen;
  632. psa = SafeArrayCreate(VT_UI1, 1, rgsabound);
  633. if (NULL == psa)
  634. {
  635. return FALSE; //out of memory!!!
  636. }
  637. if (FAILED(SafeArrayAccessData(psa, (void HUGEP* FAR*)&pdata)))
  638. {
  639. return FALSE;
  640. }
  641. UINT data = ((const SnmpTimeTicks&)val).GetValue();
  642. memcpy((void *)pdata, (void *)&data, datalen);
  643. SafeArrayUnaccessData(psa);
  644. vval.vt = VT_ARRAY|VT_UI1;
  645. vval.parray = psa;
  646. }
  647. else if(typeid(SnmpOctetString) == typeid(val))
  648. {
  649. type = ASN_OCTET;
  650. UCHAR* pdata;
  651. UINT datalen = (((const SnmpOctetString&)val).GetValueLength()) * sizeof(UCHAR);
  652. rgsabound[0].cElements = datalen;
  653. psa = SafeArrayCreate(VT_UI1, 1, rgsabound);
  654. if (NULL == psa)
  655. {
  656. return FALSE; //out of memory!!!
  657. }
  658. if (FAILED(SafeArrayAccessData(psa, (void HUGEP* FAR*)&pdata)))
  659. {
  660. return FALSE;
  661. }
  662. UCHAR* data = ((const SnmpOctetString&)val).GetValue();
  663. memcpy((void *)pdata, (void *)data, datalen);
  664. SafeArrayUnaccessData(psa);
  665. vval.vt = VT_ARRAY|VT_UI1;
  666. vval.parray = psa;
  667. }
  668. else if(typeid(SnmpOpaque) == typeid(val))
  669. {
  670. type = ASN_OPAQUE;
  671. UCHAR* pdata;
  672. UINT datalen = (((const SnmpOpaque&)val).GetValueLength()) * sizeof(UCHAR);
  673. rgsabound[0].cElements = datalen;
  674. psa = SafeArrayCreate(VT_UI1, 1, rgsabound);
  675. if (NULL == psa)
  676. {
  677. return FALSE; //out of memory!!!
  678. }
  679. if (FAILED(SafeArrayAccessData(psa, (void HUGEP* FAR*)&pdata)))
  680. {
  681. return FALSE;
  682. }
  683. UCHAR* data = ((const SnmpOpaque&)val).GetValue();
  684. memcpy((void *)pdata, (void *)data, datalen);
  685. SafeArrayUnaccessData(psa);
  686. vval.vt = VT_ARRAY|VT_UI1;
  687. vval.parray = psa;
  688. }
  689. else if(typeid(SnmpObjectIdentifier) == typeid(val))
  690. {
  691. type = ASN_OID;
  692. UCHAR* pdata;
  693. UINT datalen = (((const SnmpObjectIdentifier&)val).GetValueLength()) * sizeof(ULONG);
  694. rgsabound[0].cElements = datalen;
  695. psa = SafeArrayCreate(VT_UI1, 1, rgsabound);
  696. if (NULL == psa)
  697. {
  698. return FALSE; //out of memory!!!
  699. }
  700. if (FAILED(SafeArrayAccessData(psa, (void HUGEP* FAR*)&pdata)))
  701. {
  702. return FALSE;
  703. }
  704. ULONG* data = ((const SnmpObjectIdentifier&)val).GetValue();
  705. memcpy((void *)pdata, (void *)data, datalen);
  706. SafeArrayUnaccessData(psa);
  707. vval.vt = VT_ARRAY|VT_UI1;
  708. vval.parray = psa;
  709. }
  710. else if(typeid(SnmpIpAddress) == typeid(val))
  711. {
  712. type = ASN_ADDR;
  713. UCHAR* pdata;
  714. UINT datalen = sizeof(UINT);
  715. rgsabound[0].cElements = datalen;
  716. psa = SafeArrayCreate(VT_UI1, 1, rgsabound);
  717. if (NULL == psa)
  718. {
  719. return FALSE; //out of memory!!!
  720. }
  721. if (FAILED(SafeArrayAccessData(psa, (void HUGEP* FAR*)&pdata)))
  722. {
  723. return FALSE;
  724. }
  725. UINT data = htonl(((const SnmpIpAddress&)val).GetValue());
  726. memcpy((void *)pdata, (void *)&data, datalen);
  727. SafeArrayUnaccessData(psa);
  728. vval.vt = VT_ARRAY|VT_UI1;
  729. vval.parray = psa;
  730. }
  731. else if(typeid(SnmpUInteger32) == typeid(val))
  732. {
  733. type = ASN_UINT32;
  734. UCHAR* pdata;
  735. UINT datalen = sizeof(UINT);
  736. rgsabound[0].cElements = datalen;
  737. psa = SafeArrayCreate(VT_UI1, 1, rgsabound);
  738. if (NULL == psa)
  739. {
  740. return FALSE; //out of memory!!!
  741. }
  742. if (FAILED(SafeArrayAccessData(psa, (void HUGEP* FAR*)&pdata)))
  743. {
  744. return FALSE;
  745. }
  746. UINT data = ((const SnmpUInteger32&)val).GetValue();
  747. memcpy((void *)pdata, (void *)&data, datalen);
  748. SafeArrayUnaccessData(psa);
  749. vval.vt = VT_ARRAY|VT_UI1;
  750. vval.parray = psa;
  751. }
  752. else if(typeid(SnmpCounter64) == typeid(val))
  753. {
  754. type = ASN_COUNTER64;
  755. UCHAR* pdata;
  756. UINT datalen = sizeof(UINT);
  757. rgsabound[0].cElements = (datalen * 2);
  758. psa = SafeArrayCreate(VT_UI1, 1, rgsabound);
  759. if (NULL == psa)
  760. {
  761. return FALSE; //out of memory!!!
  762. }
  763. if (FAILED(SafeArrayAccessData(psa, (void HUGEP* FAR*)&pdata)))
  764. {
  765. return FALSE;
  766. }
  767. UINT data = ((const SnmpCounter64&)val).GetHighValue();
  768. memcpy((void *)pdata, (void *)&data, datalen);
  769. pdata += datalen;
  770. data = ((const SnmpCounter64&)val).GetLowValue();
  771. memcpy((void *)pdata, (void *)&data, datalen);
  772. SafeArrayUnaccessData(psa);
  773. vval.vt = VT_ARRAY|VT_UI1;
  774. vval.parray = psa;
  775. }
  776. else
  777. {
  778. //should not get here!
  779. return FALSE;
  780. }
  781. return TRUE;
  782. }
  783. //index assumed to be in valid range
  784. BOOL CMapToEvent::GetVBPropOIDByIndex(UINT index, VARIANT& vOid)
  785. {
  786. const SnmpObjectIdentifier& id = m_vbs.vbs[index].pVarBind->GetInstance();
  787. char * oid = id.GetAllocatedString();
  788. if (NULL != oid)
  789. {
  790. VariantInit(&vOid);
  791. CString oidstr(oid);
  792. delete [] oid;
  793. vOid.vt = VT_BSTR;
  794. vOid.bstrVal = oidstr.AllocSysString();
  795. return TRUE;
  796. }
  797. return FALSE;
  798. }
  799. //sets the m_class variable. if btryGeneric is set gets the generic class.
  800. //if m_btryGeneric is not set and a generic class is returned m_btriedGeneric
  801. //must be set to true.
  802. BOOL CMapToEvent::GetClass()
  803. {
  804. if (!m_btryGeneric)
  805. {
  806. if (GetSpecificClass())
  807. {
  808. DebugMacro9(
  809. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  810. __FILE__,__LINE__,
  811. L"CMapToEvent::GetClass got specific class\r\n");
  812. )
  813. return TRUE;
  814. }
  815. }
  816. SetTryGeneric();
  817. if (IsSNMPv1())
  818. {
  819. m_class = GetV1Class();
  820. DebugMacro9(
  821. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  822. __FILE__,__LINE__,
  823. L"CMapToEvent::GetClass getting V1 class\r\n");
  824. )
  825. }
  826. else
  827. {
  828. DebugMacro9(
  829. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  830. __FILE__,__LINE__,
  831. L"CMapToEvent::GetClass getting V2 class\r\n");
  832. )
  833. m_class = GetV2Class();
  834. }
  835. return TRUE;
  836. }