MyAgent.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  1. // MyAgent.cpp : Defines the entry point for the DLL application.
  2. //
  3. #include "stdafx.h"
  4. #include <string>
  5. #include "Mgmtapi.h"
  6. #include <snmp.h>
  7. #include <malloc.h>
  8. //#include "Winsnmp.h"
  9. #define OID_SIZEOF( Oid ) ( sizeof Oid / sizeof(UINT) )
  10. #include "DllExcel.h"
  11. #include "BasicExcelVC6.h"
  12. using namespace YExcel;
  13. //using namespace std;
  14. //////////////////////////////////////////////////////////////////////////////////
  15. #define LEN_NUM_MAX 255
  16. #define OID_NUM_MAX 5000
  17. // template MIB entry
  18. struct MIB_ENTRY
  19. {
  20. AsnObjectIdentifier asnOid;
  21. void * pStorageValue;
  22. CHAR * szStorageName;
  23. BYTE chType;
  24. UINT unAccess;
  25. MIB_ENTRY* pMibNext;
  26. };
  27. // this is the our branch starting point (clled prefix)
  28. //UINT g_unMyOIDPrefix[] = {1,3,6,1,4,1,4320};//{1.3.6.1.4.1.4320};//
  29. CHAR g_sOIDPrefix[LEN_NUM_MAX] = ".1.3.6.1.4.1.4320";
  30. // this is the trap OID to send to client
  31. UINT g_TrapOid[] = {1,3,6,1,4,1,4320};//{1.3.6.1.4.1.4320.0};//
  32. //CHAR g_sTrapOid[255] = "1.3.6.1.4.1.16.0";
  33. //AsnObjectIdentifier MIB_OidPrefix;
  34. AsnObjectIdentifier MIB_OidPrefix;// = { OID_SIZEOF(g_unMyOIDPrefix), g_unMyOIDPrefix};
  35. DWORD g_dwStartTime = 0;
  36. HANDLE g_hSimulateTrap = NULL;
  37. HANDLE g_hTrapGenThread = NULL;
  38. unsigned long __stdcall TrapGenThread(void *lpVoid);
  39. int GetRequest(SnmpVarBind *pVarBind);
  40. int GetNextRequest(SnmpVarBind *pVarBind);
  41. int SetRequest(SnmpVarBind *pVarBind);
  42. UINT GetStoreVar(MIB_ENTRY* pMIB, AsnAny *pasnValue);
  43. UINT SetStoreVar(MIB_ENTRY* pMIB, AsnAny asnValue);
  44. void InitMibTable();
  45. void ReadExcel();
  46. /////////MIB Table ////////////////////////////////////////////////
  47. CHAR g_sOID[OID_NUM_MAX][LEN_NUM_MAX];
  48. //UINT g_unSubOid[] = {0,1};
  49. //UINT g_unNameOid[] = {0,4};
  50. //UINT g_unAgeOid[] = {1,3};
  51. char *g_szAbout = NULL;
  52. char *g_szName = NULL;
  53. AsnInteger g_asnIntAge = 0;
  54. char *g_sData[OID_NUM_MAX];
  55. AsnInteger g_nData[OID_NUM_MAX];
  56. int g_nIndex2Char=0;
  57. int g_nIndex2Int=0;
  58. /// This is the MIB table and its related variable store
  59. // here evry thing is hard-coded to demonstration perpose
  60. // Actualy it should be loaded from the registry or from some file
  61. MIB_ENTRY g_MyMibTable[OID_NUM_MAX] ;
  62. MIB_ENTRY g_MyMibTableResume[OID_NUM_MAX] ;//= {
  63. // {
  64. // {OID_SIZEOF(g_unSubOid),g_unSubOid},
  65. // &g_szAbout,
  66. // "About",
  67. // ASN_OCTETSTRING,
  68. // SNMP_ACCESS_READ_ONLY,
  69. // &g_MyMibTable[1]
  70. // },
  71. // {
  72. // {OID_SIZEOF(g_unNameOid),g_unNameOid},
  73. // &g_szName,
  74. // "Name",
  75. // ASN_OCTETSTRING,
  76. // SNMP_ACCESS_READ_WRITE,
  77. // &g_MyMibTable[2]
  78. // },
  79. // {
  80. // {OID_SIZEOF(g_unAgeOid),g_unAgeOid},
  81. // &g_asnIntAge,
  82. // "Age",
  83. // ASN_INTEGER,
  84. // SNMP_ACCESS_READ_WRITE,
  85. // NULL
  86. // }
  87. //};
  88. UINT g_unMyMibCount = 0;//(sizeof(g_MyMibTable) / sizeof(MIB_ENTRY));
  89. ///////////////////////////////////////////////////////////////////
  90. void WriteLog(CHAR csMsg[], DWORD dwSize )
  91. {
  92. HANDLE hFile;
  93. DWORD dwNumberOfBytesWritten;
  94. // 必须指定 OPEN_ALWAYS 标志, 否则不会在原文件内容上添加
  95. hFile = CreateFile( "C:\\Snmplog.txt", GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL ,NULL );//FILE_ATTRIBUTE_HIDDEN
  96. if (hFile == INVALID_HANDLE_VALUE )
  97. {
  98. return ;
  99. }
  100. // 设置文件指针到文件结尾, 在 当前位置 FILE_END, 向后移动 0 字节
  101. SetFilePointer( hFile , 0, NULL, FILE_END );
  102. // 这个 API 是设置当前的位置为文件的结尾
  103. // SetEndOfFile( hFile);
  104. BOOL BRet = WriteFile( hFile, csMsg, dwSize , &dwNumberOfBytesWritten, NULL);
  105. CloseHandle( hFile );
  106. }
  107. int FindChar(char ch,char str[],int n)
  108. {
  109. int i=n-1;
  110. while(str[i]!=ch&&i>0)
  111. i--;
  112. if(i==0) i=-1;
  113. return i;
  114. }
  115. BOOL APIENTRY DllMain( HANDLE hModule,DWORD ul_reason_for_call, LPVOID lpReserved)
  116. {
  117. CHAR ss[] = "DllMain\r\n";
  118. //WriteLog( ss,strlen(ss) );
  119. switch (ul_reason_for_call)
  120. {
  121. case DLL_PROCESS_ATTACH:
  122. break;
  123. case DLL_THREAD_ATTACH:
  124. break;
  125. case DLL_THREAD_DETACH:
  126. break;
  127. case DLL_PROCESS_DETACH:
  128. TerminateThread(g_hTrapGenThread,0);
  129. CloseHandle(g_hTrapGenThread);
  130. CloseHandle(g_hSimulateTrap);
  131. break;
  132. }
  133. return TRUE;
  134. }
  135. // When exported funtion will be called during DLL loading and initialization
  136. BOOL SNMP_FUNC_TYPE SnmpExtensionInit(DWORD dwUptimeReference,HANDLE *phSubagentTrapEvent, AsnObjectIdentifier *pFirstSupportedRegion)
  137. {
  138. //DeleteFile( "C:\\Snmplog.txt" );
  139. CHAR ss[] = "SnmpExtensionInit\r\n";
  140. WriteLog( ss,strlen(ss) );
  141. SnmpMgrStrToOid( g_sOIDPrefix, &MIB_OidPrefix );
  142. // hard coded initialization
  143. g_szAbout = (char*)malloc(sizeof(char)*64);
  144. strcpy(g_szAbout,"Author : Ramanan.T");
  145. g_szName = (char*)malloc(sizeof(char)*64);
  146. strcpy(g_szName,"Your Name");
  147. g_asnIntAge = 125;
  148. g_sData[0] = (char*)malloc(sizeof(char)*64);
  149. strcpy( g_sData[0],"Author : Ramanan.T");
  150. g_sData[1] = (char*)malloc(sizeof(char)*64);
  151. strcpy( g_sData[1],"Your Name");
  152. g_nData[0] = 145;
  153. //g_nIndex2Char = 2;
  154. ReadExcel();
  155. //InitMibTable();
  156. g_hSimulateTrap = CreateEvent(NULL, FALSE, FALSE, NULL); // creaet this event for the trap
  157. *pFirstSupportedRegion = MIB_OidPrefix;
  158. *phSubagentTrapEvent = g_hSimulateTrap; // by assigning it pass it to the SNMP service
  159. // So when ever you set this event service will call
  160. // SnmpExtensionTrap exported function
  161. // on loading the our SNMP DLL create the thread
  162. //g_hTrapGenThread = CreateThread(NULL,0,TrapGenThread,NULL,0,NULL);
  163. g_dwStartTime = GetTickCount();
  164. return SNMPAPI_NOERROR;
  165. }
  166. VOID SNMP_FUNC_TYPE SnmpExtensionClose()
  167. {
  168. CHAR ss[255];// = "SnmpExtensionQuery\r\n";
  169. sprintf( ss,"SnmpExtensionClose\r\n" );
  170. WriteLog( ss,strlen(ss) );
  171. for( int i=0;i<g_nIndex2Char;i++ )
  172. {
  173. free( g_sData[i] );
  174. }
  175. //trap 用
  176. free( g_sData[4999] );
  177. }
  178. // this export is to query the MIB table and fields
  179. BOOL SNMP_FUNC_TYPE SnmpExtensionQuery(BYTE bPduType, SnmpVarBindList *pVarBindList, AsnInteger32 *pErrorStatus, AsnInteger32 *pErrorIndex)
  180. {
  181. int nRet = 0;
  182. AsnObjectName;
  183. *pErrorStatus = SNMP_ERRORSTATUS_NOERROR;
  184. *pErrorIndex = 0;
  185. CHAR ss[255];// = "SnmpExtensionQuery\r\n";
  186. //sprintf( ss,"SnmpExtensionQuery\r\n" );
  187. //WriteLog( ss,strlen(ss) );
  188. for(UINT i=0;i<pVarBindList->len;i++)
  189. {
  190. *pErrorStatus = SNMP_ERRORSTATUS_NOERROR;
  191. // what type of request we are getting?
  192. switch(bPduType)
  193. {
  194. case SNMP_PDU_GET:// // gets the variable value passed variable in pVarBindList
  195. *pErrorStatus = GetRequest(&pVarBindList->list[i]);
  196. if(*pErrorStatus != SNMP_ERRORSTATUS_NOERROR)
  197. *pErrorIndex++;
  198. //sprintf( ss,"SNMP_PDU_GET\r\n" );
  199. break;
  200. case SNMP_PDU_GETNEXT: // gets the next variable related to the passed variable in pVarBindList
  201. *pErrorStatus = GetNextRequest(&pVarBindList->list[i]);
  202. if(*pErrorStatus != SNMP_ERRORSTATUS_NOERROR)
  203. *pErrorIndex++;
  204. //sprintf( ss,"SNMP_PDU_GETNEXT\r\n" );
  205. break;
  206. case SNMP_PDU_SET: // sets a variable
  207. *pErrorStatus = SetRequest(&pVarBindList->list[i]);
  208. if(*pErrorStatus != SNMP_ERRORSTATUS_NOERROR)
  209. *pErrorIndex++;
  210. //sprintf( ss,"SNMP_PDU_SET\r\n" );
  211. break;
  212. default:
  213. *pErrorStatus = SNMP_ERRORSTATUS_NOSUCHNAME;
  214. *pErrorIndex++;
  215. //sprintf( ss,"default\r\n" );
  216. }
  217. //WriteLog( ss,strlen(ss) );
  218. }
  219. return SNMPAPI_NOERROR;
  220. }
  221. // this function just simulate traps
  222. // Traps just a 2 variables value from MIB
  223. // Trap is kind of event from server to client
  224. /// When ever the event is set service will call this function and gets the parameters filled.
  225. // After filling the parameters service willsend the trap to all the client connected
  226. BOOL SNMP_FUNC_TYPE SnmpExtensionTrap(AsnObjectIdentifier *pEnterpriseOid, AsnInteger32 *pGenericTrapId, AsnInteger32 *pSpecificTrapId, AsnTimeticks *pTimeStamp, SnmpVarBindList *pVarBindList)
  227. {
  228. static int nNoOfTraps = 1; // just ignore this, I introduced this to send meny traps at once
  229. // any way below we are generating one trap with two values
  230. CHAR ss[255];// = "SnmpExtensionQuery\r\n";
  231. SYSTEMTIME st;
  232. ::GetLocalTime( &st );
  233. if(nNoOfTraps==1) // if it is zero don't send traps
  234. {
  235. //pEnterpriseOid->idLength = OID_SIZEOF(g_TrapOid);
  236. //pEnterpriseOid->ids = g_TrapOid;
  237. SnmpMgrStrToOid( g_sOIDPrefix, pEnterpriseOid );
  238. *pGenericTrapId = SNMP_GENERICTRAP_ENTERSPECIFIC;
  239. *pSpecificTrapId = 1; // ToasterControl Up trap.
  240. *pTimeStamp = GetTickCount() - g_dwStartTime;
  241. // Allocate space for the Variable Bindings.
  242. pVarBindList->list = (SnmpVarBind*)SnmpUtilMemAlloc(1*sizeof(SnmpVarBind));
  243. SnmpUtilOidCpy(&pVarBindList->list[0].name,&g_MyMibTable[4999].asnOid);//&MIB_OidPrefix);
  244. //SnmpUtilOidAppend(&pVarBindList->list[0].name,&g_MyMibTable[1].asnOid);
  245. pVarBindList->list[0].value.asnType = ASN_OCTETSTRING;
  246. pVarBindList->list[0].value.asnValue.string.dynamic = TRUE;
  247. pVarBindList->list[0].value.asnValue.string.length = strlen(*(LPSTR*)g_MyMibTable[4999].pStorageValue);
  248. pVarBindList->list[0].value.asnValue.string.stream =(unsigned char*)SnmpUtilMemAlloc(pVarBindList->list[0].value.asnValue.string.length * sizeof(char));
  249. memcpy(pVarBindList->list[0].value.asnValue.string.stream,*(LPSTR*)g_MyMibTable[4999].pStorageValue,pVarBindList->list[0].value.asnValue.string.length);
  250. //SnmpUtilOidCpy(&pVarBindList->list[0].name,&g_MyMibTable[2].asnOid);//&MIB_OidPrefix);
  251. ////SnmpUtilOidAppend(&pVarBindList->list[1].name,&g_MyMibTable[2].asnOid);
  252. //pVarBindList->list[0].value.asnType = ASN_INTEGER;
  253. //pVarBindList->list[0].value.asnValue.number = *((AsnInteger32*)g_MyMibTable[2].pStorageValue);
  254. //sprintf( ss,"SnmpExtensionTrap %02d:%02d:%02d =%s\r\n",st.wHour,st.wMinute,st.wSecond,&pVarBindList->list[0].name );
  255. //WriteLog( ss,strlen(ss) );
  256. pVarBindList->len = 1;
  257. nNoOfTraps--;
  258. // Indicate that valid trap data exists in the parameters.
  259. return TRUE;
  260. }
  261. nNoOfTraps = 1;
  262. //PulseEvent( g_hSimulateTrap );
  263. // Indicate that no more traps are available, and parameters do not refer to any valid data
  264. return FALSE;
  265. }
  266. // get the value of the variable pased
  267. int GetRequest(SnmpVarBind *pVarBind)
  268. {
  269. int nRet = SNMP_ERRORSTATUS_NOSUCHNAME;
  270. AsnObjectIdentifier asnOidTemp;
  271. CHAR ss[255];// = "SnmpExtensionQuery\r\n";
  272. sprintf( ss,"g_unMyMibCount = %d\r\n",g_unMyMibCount );
  273. //WriteLog( ss,strlen(ss) );
  274. for(UINT i=0;i<g_unMyMibCount;i++)
  275. {
  276. SnmpUtilOidCpy(&asnOidTemp, &g_MyMibTable[i].asnOid);//&MIB_OidPrefix);
  277. //SnmpUtilOidAppend(&asnOidTemp, &g_MyMibTable[i].asnOid);
  278. if(SnmpUtilOidCmp(&asnOidTemp,&pVarBind->name) ==0)
  279. {
  280. SnmpUtilOidFree(&asnOidTemp);
  281. nRet = GetStoreVar(&g_MyMibTable[i],&pVarBind->value);
  282. break;
  283. }
  284. SnmpUtilOidFree(&asnOidTemp);
  285. }
  286. return nRet;
  287. }
  288. // get the next variable & value related to the passed variable
  289. int GetNextRequest(SnmpVarBind *pVarBind)
  290. {
  291. int nRet = SNMP_ERRORSTATUS_NOSUCHNAME;
  292. int nResult = 0;
  293. AsnObjectIdentifier asnOidTemp;
  294. for(UINT i=0;i<g_unMyMibCount;i++)
  295. {
  296. SnmpUtilOidCpy(&asnOidTemp, &g_MyMibTable[i].asnOid);//&MIB_OidPrefix);
  297. //SnmpUtilOidAppend(&asnOidTemp, &g_MyMibTable[i].asnOid);
  298. nResult = SnmpUtilOidCmp(&asnOidTemp,&pVarBind->name);
  299. if(nResult>0)
  300. {
  301. SnmpUtilOidFree(&pVarBind->name);
  302. // kind of memcpy
  303. SnmpUtilOidCpy(&pVarBind->name, &asnOidTemp);
  304. nRet = GetStoreVar(&g_MyMibTable[i],&pVarBind->value);
  305. SnmpUtilOidFree(&asnOidTemp);
  306. nRet = SNMP_ERRORSTATUS_NOERROR;
  307. break;
  308. }
  309. else if(nResult == 0)
  310. {
  311. SnmpUtilOidFree(&asnOidTemp);
  312. if(g_MyMibTable[i].pMibNext == NULL)
  313. break;
  314. // king of mem free
  315. SnmpUtilOidFree(&pVarBind->name);
  316. // kind of memcpy :)
  317. SnmpUtilOidCpy(&pVarBind->name, &g_MyMibTable[i].pMibNext->asnOid);//&MIB_OidPrefix);
  318. // kind of strcat
  319. //SnmpUtilOidAppend(&pVarBind->name, &g_MyMibTable[i].pMibNext->asnOid);
  320. nRet = GetStoreVar(g_MyMibTable[i].pMibNext,&pVarBind->value);
  321. break;
  322. }
  323. SnmpUtilOidFree(&asnOidTemp);
  324. }
  325. return nRet;
  326. }
  327. LPSTR Convert(LPCSTR str, int sourceCodepage, int targetCodepage,int &iTargetLen)
  328. {
  329. //int len=_tcslen(str);
  330. int unicodeLen=MultiByteToWideChar(sourceCodepage,0,str,-1,NULL,0);
  331. wchar_t* pUnicode;
  332. pUnicode=new wchar_t[unicodeLen+1];
  333. memset(pUnicode,0,(unicodeLen+1)*sizeof(wchar_t));
  334. MultiByteToWideChar(sourceCodepage,0,str,-1,(LPWSTR)pUnicode,unicodeLen);
  335. BYTE * pTargetData;
  336. int targetLen=WideCharToMultiByte(targetCodepage,0,(LPWSTR)pUnicode,-1,(char *)pTargetData,0,NULL,NULL);
  337. iTargetLen = targetLen;
  338. pTargetData=new BYTE[targetLen+1];
  339. memset(pTargetData,0,targetLen+1);
  340. WideCharToMultiByte(targetCodepage,0,(LPWSTR)pUnicode,-1,(char *)pTargetData,targetLen,NULL,NULL);
  341. LPSTR szResult=new TCHAR[targetLen+1];
  342. wsprintf(szResult,"%s",pTargetData);
  343. delete[] pUnicode;
  344. delete[] pTargetData;
  345. return szResult;
  346. }
  347. // if it is a writable variable passed value will be copied to it
  348. int SetRequest(SnmpVarBind *pVarBind)
  349. {
  350. int nRet = SNMP_ERRORSTATUS_NOSUCHNAME;
  351. AsnObjectIdentifier asnOidTemp;
  352. for(UINT i=0;i<g_unMyMibCount;i++)
  353. {
  354. // kind of memcpy ;), any way you can't use memcpy
  355. SnmpUtilOidCpy(&asnOidTemp, &g_MyMibTable[i].asnOid);//&MIB_OidPrefix);
  356. // kind of strcat ;)
  357. //SnmpUtilOidAppend(&asnOidTemp, &g_MyMibTable[i].asnOid);
  358. if(SnmpUtilOidCmp(&asnOidTemp,&pVarBind->name) ==0)
  359. {
  360. CHAR src[255],des[255],ss[255];
  361. memset( src,0,255 );
  362. memset( des,0,255 );
  363. memset( ss,0,255 );
  364. sprintf( src,"%s",pVarBind->value.asnValue.string );
  365. char *cPosA = strstr( src,"警" );
  366. char *cPosN = strstr( src,"恢复正常" );
  367. int nPosA = FindChar( 'A',src,strlen(src) );
  368. int nPosN = FindChar( 'N',src,strlen(src) );
  369. //sprintf( ss,"src=%s,cPosA=%d,cPosN=%d,nPosA=%d,nPosN=%d\r\n",src,cPosA,cPosN,nPosA,nPosN );
  370. //WriteLog( ss,strlen(ss) );
  371. if( cPosA==NULL && cPosN==NULL )
  372. {
  373. //WriteLog( src,strlen(src) );
  374. SnmpUtilOidFree(&asnOidTemp);
  375. nRet = SetStoreVar(&g_MyMibTable[i],pVarBind->value);
  376. }
  377. else if( cPosA )
  378. {
  379. sprintf( ss,"src=%s,cPosA=%d,cPosN=%d,nPosA=%d,nPosN=%d\r\n",src,cPosA,cPosN,nPosA,nPosN );
  380. WriteLog( ss,strlen(ss) );
  381. memcpy(des,src + 0,nPosA-1 );
  382. //memset( src,0,255 );
  383. //sprintf( src,"%s 111111111\r\n",des );
  384. //WriteLog( src,strlen(src) );
  385. int nTargetLen=0;
  386. LPSTR strTarget = Convert( des,936,CP_UTF8,nTargetLen);
  387. SnmpUtilMemFree( pVarBind->value.asnValue.string.stream );
  388. pVarBind->value.asnType = ASN_OCTETSTRING;
  389. pVarBind->value.asnValue.string.dynamic = TRUE;
  390. pVarBind->value.asnValue.string.length = nTargetLen;//nPosA-1;
  391. pVarBind->value.asnValue.string.stream =(unsigned char*)SnmpUtilMemAlloc( nTargetLen/*(nPosN-1)*/ * sizeof(char));
  392. memcpy( pVarBind->value.asnValue.string.stream,strTarget,nTargetLen );
  393. //memcpy( pVarBind->value.asnValue.string.stream,Convert( des,936,CP_UTF8),nPosA-1 );
  394. //memset( src,0,255 );
  395. //sprintf( src,"%s 发生报警\r\n",pVarBind->value.asnValue.string );
  396. //WriteLog( src,strlen(src) );
  397. SnmpUtilOidFree(&asnOidTemp);
  398. nRet = SetStoreVar(&g_MyMibTable[i],pVarBind->value);
  399. g_MyMibTable[4999].asnOid = g_MyMibTable[i].asnOid;
  400. g_MyMibTable[4999].unAccess = g_MyMibTable[i].unAccess;
  401. g_MyMibTable[4999].chType = g_MyMibTable[i].chType;
  402. g_MyMibTable[4999].pStorageValue = g_MyMibTable[i].pStorageValue;//Convert( g_MyMibTable[i].pStorageValue,936,CP_UTF8);//
  403. g_MyMibTable[4999].szStorageName = g_MyMibTable[i].szStorageName;//"发生报警";//
  404. //SetStoreVar(&g_MyMibTable[4999],pVarBind->value);
  405. SetEvent(g_hSimulateTrap);
  406. }
  407. else if( cPosN )
  408. {
  409. memcpy(des,src + 0,nPosN-1 );
  410. //memset( src,0,255 );
  411. //sprintf( src,"%s 111111111\r\n",des );
  412. //WriteLog( src,strlen(src) );
  413. int nTargetLen=0;
  414. LPSTR strTarget = Convert( des,936,CP_UTF8,nTargetLen);
  415. SnmpUtilMemFree( pVarBind->value.asnValue.string.stream );
  416. pVarBind->value.asnType = ASN_OCTETSTRING;
  417. pVarBind->value.asnValue.string.dynamic = TRUE;
  418. pVarBind->value.asnValue.string.length = nTargetLen;//nPosA-1;
  419. pVarBind->value.asnValue.string.stream =(unsigned char*)SnmpUtilMemAlloc( nTargetLen/*(nPosN-1)*/ * sizeof(char));
  420. memcpy( pVarBind->value.asnValue.string.stream,strTarget,nTargetLen );
  421. //memset( src,0,255 );
  422. //sprintf( src,"%s 回复正常\r\n",pVarBind->value.asnValue.string );
  423. //WriteLog( src,strlen(src) );
  424. SnmpUtilOidFree(&asnOidTemp);
  425. nRet = SetStoreVar(&g_MyMibTable[i],pVarBind->value);
  426. g_MyMibTable[4999].asnOid = g_MyMibTableResume[i].asnOid;
  427. g_MyMibTable[4999].unAccess = g_MyMibTable[i].unAccess;
  428. g_MyMibTable[4999].chType = g_MyMibTable[i].chType;
  429. g_MyMibTable[4999].pStorageValue = g_MyMibTable[i].pStorageValue;
  430. g_MyMibTable[4999].szStorageName = g_MyMibTable[i].szStorageName;//"回复正常";//
  431. //SetStoreVar(&g_MyMibTable[4999],pVarBind->value);
  432. SetEvent(g_hSimulateTrap);
  433. }
  434. break;
  435. }
  436. SnmpUtilOidFree(&asnOidTemp);
  437. }
  438. return nRet;
  439. }
  440. // retrieves the value from the MIB table
  441. // Imagine AsnAny is like VARIANT structure to populate that you need to go through the types
  442. UINT GetStoreVar(MIB_ENTRY* pMIB, AsnAny *pasnValue)
  443. {
  444. // check rights is there to access
  445. if((pMIB->unAccess != SNMP_ACCESS_READ_ONLY)&&(pMIB->unAccess != SNMP_ACCESS_READ_WRITE)&&(pMIB->unAccess != SNMP_ACCESS_READ_CREATE))
  446. return SNMP_ERRORSTATUS_GENERR;
  447. // set the type
  448. pasnValue->asnType = pMIB->chType;
  449. switch(pasnValue->asnType)
  450. {
  451. case ASN_INTEGER:
  452. pasnValue->asnValue.number = *(AsnInteger32*)pMIB->pStorageValue;
  453. break;
  454. case ASN_COUNTER32:
  455. case ASN_GAUGE32:
  456. case ASN_TIMETICKS:
  457. case ASN_UNSIGNED32:
  458. pasnValue->asnValue.unsigned32 = *(AsnUnsigned32*)pMIB->pStorageValue;
  459. break;
  460. case ASN_OCTETSTRING:
  461. pasnValue->asnValue.string.length = strlen(*(LPSTR*)pMIB->pStorageValue);
  462. pasnValue->asnValue.string.stream =(unsigned char*)SnmpUtilMemAlloc(pasnValue->asnValue.string.length * sizeof(char));
  463. memcpy(pasnValue->asnValue.string.stream,*(LPSTR*)pMIB->pStorageValue,pasnValue->asnValue.string.length);
  464. pasnValue->asnValue.string.dynamic = TRUE;
  465. break;
  466. case ASN_COUNTER64:
  467. pasnValue->asnValue.counter64 = *(AsnCounter64*)pMIB->pStorageValue;
  468. break;
  469. case ASN_OBJECTIDENTIFIER:
  470. SnmpUtilOidCpy(&pasnValue->asnValue.object,(AsnObjectIdentifier*)pMIB->pStorageValue);
  471. break;
  472. case ASN_IPADDRESS:
  473. pasnValue->asnValue.address.length = 4;
  474. pasnValue->asnValue.string.dynamic = TRUE;
  475. pasnValue->asnValue.address.stream[0] = ((char*)pMIB->pStorageValue)[0];
  476. pasnValue->asnValue.address.stream[1] = ((char*)pMIB->pStorageValue)[1];
  477. pasnValue->asnValue.address.stream[2] = ((char*)pMIB->pStorageValue)[2];
  478. pasnValue->asnValue.address.stream[3] = ((char*)pMIB->pStorageValue)[3];
  479. break;
  480. case ASN_OPAQUE:
  481. AsnSequence;
  482. break;
  483. case ASN_BITS:
  484. break;
  485. case ASN_SEQUENCE:
  486. break;
  487. case ASN_NULL:
  488. default:
  489. return SNMP_ERRORSTATUS_GENERR;
  490. }
  491. return SNMP_ERRORSTATUS_NOERROR;
  492. }
  493. // to store the value to the MIB table
  494. // Imagine AsnAny is like VARIANT structure to populate that you need to go through the types
  495. UINT SetStoreVar(MIB_ENTRY* pMIB, AsnAny asnValue)
  496. {
  497. // check whether rights is there to access
  498. if((pMIB->unAccess != SNMP_ACCESS_READ_WRITE)&&(pMIB->unAccess != SNMP_ACCESS_READ_CREATE))
  499. return SNMP_ERRORSTATUS_READONLY;
  500. // check type matches with the table type
  501. if(pMIB->chType != asnValue.asnType)
  502. return SNMP_ERRORSTATUS_BADVALUE;
  503. switch(asnValue.asnType)
  504. {
  505. case ASN_INTEGER:
  506. *(AsnInteger *)(pMIB->pStorageValue) = asnValue.asnValue.number;
  507. break;
  508. case ASN_COUNTER32:
  509. case ASN_GAUGE32:
  510. case ASN_TIMETICKS:
  511. case ASN_UNSIGNED32:
  512. *(AsnInteger *)(pMIB->pStorageValue) = asnValue.asnValue.unsigned32;
  513. break;
  514. case ASN_OCTETSTRING:
  515. free(*(LPSTR*)(pMIB->pStorageValue));
  516. *(LPSTR*)(pMIB->pStorageValue) = (LPSTR)malloc((asnValue.asnValue.string.length+1) * sizeof(char));
  517. memcpy(*(LPSTR*)(pMIB->pStorageValue), asnValue.asnValue.string.stream,asnValue.asnValue.string.length);
  518. (*(LPSTR*)pMIB->pStorageValue)[asnValue.asnValue.string.length] = '\0';
  519. break;
  520. case ASN_COUNTER64:
  521. *(AsnCounter64*)pMIB->pStorageValue = asnValue.asnValue.counter64;
  522. break;
  523. case ASN_OBJECTIDENTIFIER:
  524. SnmpUtilOidCpy((AsnObjectIdentifier*)pMIB->pStorageValue,&asnValue.asnValue.object);
  525. break;
  526. case ASN_IPADDRESS:
  527. ((char*)pMIB->pStorageValue)[0] = asnValue.asnValue.address.stream[0];
  528. ((char*)pMIB->pStorageValue)[1] = asnValue.asnValue.address.stream[1];
  529. ((char*)pMIB->pStorageValue)[2] = asnValue.asnValue.address.stream[2];
  530. ((char*)pMIB->pStorageValue)[3] = asnValue.asnValue.address.stream[3];
  531. break;
  532. case ASN_OPAQUE:
  533. AsnSequence;
  534. break;
  535. case ASN_BITS:
  536. break;
  537. case ASN_SEQUENCE:
  538. break;
  539. case ASN_NULL:
  540. break;
  541. default:
  542. return SNMP_ERRORSTATUS_GENERR;
  543. }
  544. return SNMP_ERRORSTATUS_NOERROR;
  545. }
  546. // simple trap generation thread
  547. // I'm sending traps in a loops in 15 second intervals
  548. unsigned long __stdcall TrapGenThread(void *lpVoid)
  549. {
  550. for(;;)
  551. {
  552. Sleep(5000);//Sleep(15000);
  553. SetEvent(g_hSimulateTrap);
  554. }
  555. return 0;
  556. }
  557. void InitMibTable()
  558. {
  559. //sprintf( g_sOID[0],".1.3.6.1.4.1.4320.1" );
  560. //sprintf( g_sOID[1],".1.3.6.1.4.1.4320.2" );
  561. //sprintf( g_sOID[2],".1.3.6.1.4.1.4320.3" );
  562. //CHAR sTemp[7][LEN_NUM_MAX];
  563. ////g_unMyMibCount = 3;//(sizeof(g_MyMibTable) / sizeof(MIB_ENTRY));
  564. //CHAR ss[255];// = "SnmpExtensionQuery\r\n";
  565. //sprintf( ss,"InitMibTable\r\n" );
  566. ////WriteLog( ss,strlen(ss) );
  567. //BasicExcel e;
  568. //e.Load("C:\\OID-斯特纽有限公司.xls");
  569. //BasicExcelWorksheet* sheet1 = e.GetWorksheet("StoneU");
  570. //if (sheet1)
  571. //{
  572. // size_t maxRows = sheet1->GetTotalRows();
  573. // size_t maxCols = sheet1->GetTotalCols();
  574. // sprintf( ss,"Excel maxRows=%d,maxCols=%d \r\n",maxRows,maxCols );
  575. // WriteLog( ss,strlen(ss) );
  576. // for (size_t r=2; r<maxRows; ++r)
  577. // {
  578. // for (size_t c=0; c<maxCols; c++)
  579. // {
  580. // BasicExcelCell* cell = sheet1->Cell(r,c);
  581. // switch (c)
  582. // {
  583. // case 0://OID
  584. // sprintf( sTemp[c],"%s",cell->GetString());
  585. // break;
  586. // case 1://变量
  587. // sprintf( sTemp[c],"%s",cell->GetString());
  588. // break;
  589. // case 2://ACCESS
  590. // sprintf( sTemp[c],"%s",cell->GetString());
  591. // break;
  592. // case 3://数据类型
  593. // sprintf( sTemp[c],"%s",cell->GetString());
  594. // break;
  595. // case 4://初始化 ("%10.6lf", cell->GetDouble())<<" ";
  596. // sprintf( sTemp[c],"%10.4lf",cell->GetDouble());
  597. // break;
  598. // case 5://DESCRIPTION
  599. // sprintf( sTemp[c],"%s",cell->GetString());
  600. // break;
  601. // default:
  602. // break;
  603. // }
  604. // }
  605. // if( sTemp[0]!="" )
  606. // {
  607. // SnmpMgrStrToOid( sTemp[0], &g_MyMibTable[g_unMyMibCount].asnOid );
  608. //
  609. // if( 0==strcmp(sTemp[2],"read-write") )
  610. // {
  611. // g_MyMibTable[g_unMyMibCount].unAccess = SNMP_ACCESS_READ_WRITE;
  612. // sprintf( ss,"read-write\r\n" );
  613. // }
  614. // else if( 0==strcmp(sTemp[2],"read-only") )
  615. // {
  616. // g_MyMibTable[g_unMyMibCount].unAccess = SNMP_ACCESS_READ_ONLY;
  617. // sprintf( ss,"read-only\r\n" );
  618. // }
  619. // else if( 0==strcmp(sTemp[2],"not-accessible") )
  620. // {
  621. // g_MyMibTable[g_unMyMibCount].unAccess = SNMP_ACCESS_NONE;
  622. // sprintf( ss,"not-accessible\r\n" );
  623. // }
  624. // //WriteLog( ss,strlen(ss) );
  625. // if( 0==strcmp(sTemp[3],"ASN_OCTETSTRING") )
  626. // {
  627. // g_MyMibTable[g_unMyMibCount].chType = ASN_OCTETSTRING;
  628. //
  629. // g_sData[g_nIndex2Char] = (char*)malloc(sizeof(char)*64);
  630. // strcpy( g_sData[g_nIndex2Char],sTemp[4] );
  631. // g_MyMibTable[g_unMyMibCount].pStorageValue = &g_sData[g_nIndex2Char];
  632. // g_nIndex2Char++;
  633. // sprintf( ss,"ASN_OCTETSTRING g_nIndex2Char=%d\r\n",g_nIndex2Char );
  634. // }
  635. // else if( 0==strcmp(sTemp[3],"ASN_INTEGER") )
  636. // {
  637. // g_MyMibTable[g_unMyMibCount].chType = ASN_INTEGER;
  638. //
  639. // g_nData[g_nIndex2Int] = atoi(sTemp[4]);
  640. // g_MyMibTable[g_unMyMibCount].pStorageValue = &g_nData[g_nIndex2Int];
  641. // g_nIndex2Int++;
  642. // sprintf( ss,"ASN_INTEGER\r\n" );
  643. // }
  644. // else if( 0==strcmp(sTemp[3],"ASN_NULL") )
  645. // {
  646. // g_MyMibTable[g_unMyMibCount].chType = ASN_NULL;
  647. // sprintf( ss,"ASN_NULL\r\n" );
  648. // }
  649. // //WriteLog( ss,strlen(ss) );
  650. //
  651. // g_MyMibTable[g_unMyMibCount].szStorageName = "TEST";
  652. //
  653. // g_MyMibTable[g_unMyMibCount].pMibNext = &g_MyMibTable[g_unMyMibCount+1];
  654. // g_unMyMibCount++;
  655. // }
  656. // }
  657. //}
  658. }
  659. void ReadExcel()
  660. {
  661. CHAR ss[255];// = "SnmpExtensionQuery\r\n";
  662. sprintf( ss,"ReadExcel in \r\n" );
  663. WriteLog( ss,strlen(ss) );
  664. int i;
  665. const long DIM1 = OID_NUM_MAX;
  666. const long DIM2 = 255;
  667. char **array1,**array2,**array3,**array4,**array5,**array6;
  668. array1 = new char *[DIM1];
  669. array2 = new char *[DIM1];
  670. array3 = new char *[DIM1];
  671. array4 = new char *[DIM1];
  672. array5 = new char *[DIM1];
  673. array6 = new char *[DIM1];
  674. for(i=0;i<DIM1;++i)
  675. {
  676. array1[i] = new char[DIM2];
  677. array2[i] = new char[DIM2];
  678. array3[i] = new char[DIM2];
  679. array4[i] = new char[DIM2];
  680. array5[i] = new char[DIM2];
  681. array6[i] = new char[DIM2];
  682. }
  683. CDllExcel DllExcel;
  684. long nCount=0;
  685. DllExcel.Excel_Open( "C:\\OID-斯特纽有限公司.xls","StoneU",array1,array2,array3,array4,array5,array6,nCount );
  686. sprintf( ss,"ReadExcel nCount = %d \r\n",nCount );
  687. WriteLog( ss,strlen(ss) );
  688. //CString str;
  689. for( int i=0;i<nCount;i++ )
  690. {
  691. SnmpMgrStrToOid( array1[i], &g_MyMibTable[g_unMyMibCount].asnOid );
  692. CHAR temp[255];
  693. sprintf( temp,"%s.1",array1[i] );
  694. //WriteLog( temp,strlen(temp) );
  695. SnmpMgrStrToOid( temp, &g_MyMibTableResume[g_unMyMibCount].asnOid );
  696. //if( 0==strcmp(array3[i],"read-write") )
  697. {
  698. g_MyMibTable[g_unMyMibCount].unAccess = SNMP_ACCESS_READ_WRITE;
  699. sprintf( ss,"read-write\r\n" );
  700. }
  701. //else if( 0==strcmp(array3[i],"read-only") )
  702. //{
  703. // g_MyMibTable[g_unMyMibCount].unAccess = SNMP_ACCESS_READ_ONLY;
  704. // sprintf( ss,"read-only\r\n" );
  705. //}
  706. //else if( 0==strcmp(array3[i],"not-accessible") )
  707. //{
  708. // g_MyMibTable[g_unMyMibCount].unAccess = SNMP_ACCESS_NONE;
  709. // sprintf( ss,"not-accessible\r\n" );
  710. //}
  711. //WriteLog( ss,strlen(ss) );
  712. //if( 0==strcmp(array4[i],"ASN_STRING") )
  713. {
  714. g_MyMibTable[g_unMyMibCount].chType = ASN_OCTETSTRING;
  715. g_sData[g_nIndex2Char] = (char*)malloc(sizeof(char)*64);
  716. strcpy( g_sData[g_nIndex2Char],array5[i] );
  717. g_MyMibTable[g_unMyMibCount].pStorageValue = &g_sData[g_nIndex2Char];
  718. g_nIndex2Char++;
  719. sprintf( ss,"ASN_OCTETSTRING g_nIndex2Char=%d\r\n",g_nIndex2Char );
  720. }
  721. //else if( 0==strcmp(array4[i],"ASN_INTEGER") )
  722. //{
  723. // g_MyMibTable[g_unMyMibCount].chType = ASN_INTEGER;
  724. //
  725. // g_nData[g_nIndex2Int] = atoi(array5[i]);
  726. // g_MyMibTable[g_unMyMibCount].pStorageValue = &g_nData[g_nIndex2Int];
  727. // g_nIndex2Int++;
  728. // sprintf( ss,"ASN_INTEGER\r\n" );
  729. //}
  730. //else if( 0==strcmp(array4[i],"ASN_NULL") )
  731. //{
  732. // g_MyMibTable[g_unMyMibCount].chType = ASN_NULL;
  733. // sprintf( ss,"ASN_NULL\r\n" );
  734. //}
  735. //WriteLog( ss,strlen(ss) );
  736. g_MyMibTable[g_unMyMibCount].szStorageName = array6[i];
  737. g_MyMibTable[g_unMyMibCount].pMibNext = &g_MyMibTable[g_unMyMibCount+1];
  738. g_unMyMibCount++;
  739. }
  740. for(int i=0;i<DIM1;++i)
  741. {
  742. delete[] array1[i];
  743. delete[] array2[i];
  744. delete[] array3[i];
  745. delete[] array4[i];
  746. delete[] array5[i];
  747. delete[] array6[i];
  748. }
  749. delete[] array1;
  750. delete[] array2;
  751. delete[] array3;
  752. delete[] array4;
  753. delete[] array5;
  754. delete[] array6;
  755. //trap 用
  756. g_sData[4999] = (char*)malloc(sizeof(char)*64);
  757. sprintf( ss,"ReadExcel out \r\n" );
  758. WriteLog( ss,strlen(ss) );
  759. //CHAR ss[255];// = "SnmpExtensionQuery\r\n";
  760. //BasicExcel e;
  761. //e.Load("C:\\test.xls");
  762. //BasicExcelWorksheet* sheet1 = e.GetWorksheet("test");
  763. //if (sheet1)
  764. //{
  765. // size_t maxRows = sheet1->GetTotalRows();
  766. // size_t maxCols = sheet1->GetTotalCols();
  767. // for (size_t r=1; r<maxRows; ++r)
  768. // {
  769. // //filetxt<<("%10d", r+1)<<" ";
  770. // for (size_t c=0; c<maxCols; ++c)
  771. // {
  772. // BasicExcelCell* cell = sheet1->Cell(r,c);
  773. // switch (cell->Type())
  774. // {
  775. // case BasicExcelCell::UNDEFINED:
  776. // //filetxt<<" ";
  777. // sprintf( ss,"ReadExcel* \r\n");
  778. // break;
  779. // case BasicExcelCell::INT:
  780. // //filetxt<<("%10d", cell->GetInteger())<<" ";
  781. // sprintf( ss,"ReadExcel* %10d\r\n", cell->GetInteger());
  782. // break;
  783. // case BasicExcelCell::DOUBLE:
  784. // //filetxt<<("%10.6lf", cell->GetDouble())<<" ";
  785. // sprintf( ss,"ReadExcel* %10.6lf\r\n", cell->GetDouble());
  786. // break;
  787. // case BasicExcelCell::STRING:
  788. // //filetxt<<("%10s", cell->GetString())<<" ";
  789. // sprintf( ss,"ReadExcel* %10s\r\n", cell->GetString());
  790. // break;
  791. // case BasicExcelCell::WSTRING:
  792. // //filetxt<<(L"%s", cell->GetWString())<<" ";
  793. // //sprintf( ss,"ReadExcel %s\r\n", cell->GetWString());
  794. // break;
  795. // default:
  796. // break;
  797. // }
  798. // WriteLog( ss,strlen(ss) );
  799. // }
  800. // }
  801. //}
  802. }