SNMP1.cpp
资源名称:SNMP范例源代码.zip [点击查看]
上传用户:shgx688
上传日期:2009-12-27
资源大小:855k
文件大小:8k
源码类别:
SNMP编程
开发平台:
MultiPlatform
- // SNMP1.cpp: implementation of the CSNMP1 class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "oam.h"
- #include "SNMP1.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- CSNMP1::CSNMP1()
- {
- nMajorVersion=new unsigned long;
- nMinorVersion=new unsigned long;
- nLevel=new unsigned long;
- nTranslateMode=new unsigned long;
- nRetransmitMode=new unsigned long;
- /* Init MIB var_table , the following tasks should be done:
- 1. Design the table structure
- 2. Design the table managment service routines
- 3. design the data access support routines for each mib var.
- In this table, the operations for each specific var
- should be defined, and their function pointers should be stored here
- */
- if(SnmpStartup(nMajorVersion,nMinorVersion,nLevel,nTranslateMode,
- nRetransmitMode)!=SNMPAPI_SUCCESS) /*初始化*/
- {
- //dwErr=SnmpGetLastError(NULL);
- switch (SnmpGetLastError(NULL))
- {
- case SNMPAPI_NOT_INITIALIZED :
- AfxMessageBox("The SnmpStartup function did not complete successfully.");
- break;
- case SNMPAPI_ALLOC_ERROR:
- AfxMessageBox("An error occurred during memory allocation.");
- break;
- case SNMPAPI_OTHER_ERROR :
- AfxMessageBox("An unknown or undefined error occurred. ");
- break;
- default:
- break;
- }
- AfxMessageBox("initilization failure");
- }
- if(SnmpSetTranslateMode(SNMPAPI_UNTRANSLATED_V1)!=SNMPAPI_SUCCESS)
- //if(SnmpSetTranslateMode(SNMPAPI_TRANSLATED)!=SNMPAPI_SUCCESS)
- AfxMessageBox("SetTranslateMode failure");
- if(SnmpSetRetransmitMode(SNMPAPI_ON)!=SNMPAPI_SUCCESS)/*设置重传机制*/
- AfxMessageBox("SetRetransmitMode failure");
- sessionID=FALSE;
- }
- CSNMP1::~CSNMP1()
- {
- if(nRetransmitMode!=NULL)
- delete nRetransmitMode;
- if(nTranslateMode!=NULL)
- delete nTranslateMode;
- if(nLevel!=NULL)
- delete nLevel;
- if(nMinorVersion!=NULL)
- delete nMinorVersion;
- if(nMajorVersion!=NULL)
- delete nMajorVersion;
- if(session!=NULL)
- SnmpClose(session);
- SnmpCleanup();
- }
- CSNMP1::CreateSession(HWND hWnd,UINT wMsg)
- {
- if((session=SnmpCreateSession(hWnd,wMsg,NULL,NULL))==SNMPAPI_FAILURE)
- AfxMessageBox("CreateSession failure");
- }
- //ADD BY qIBING
- CSNMP1::CreateSession_Trap(HWND hWnd,UINT wMsg)
- {
- if((session_trap=SnmpCreateSession(hWnd,wMsg,NULL,NULL))==SNMPAPI_FAILURE)
- AfxMessageBox("CreateSession trap failure");
- }
- CSNMP1::CreateVbl(LPCSTR name,smiLPVALUE pvalue)
- {
- smiLPOID pOid=new smiOID;
- if(SnmpStrToOid(name,pOid)==SNMPAPI_FAILURE)
- {AfxMessageBox("CreateVbl failure");
- //dwErr=SnmpGetLastError(session);
- }
- m_hvbl=SnmpCreateVbl(session,pOid,pvalue);
- if(m_hvbl==SNMPAPI_FAILURE)
- {AfxMessageBox("CreateVbl failure");
- //dwErr=SnmpGetLastError(session);
- }
- if(pOid!=NULL)
- delete pOid;
- }
- CSNMP1::SetVbl(LPCSTR name,smiLPVALUE pvalue)
- {
- smiLPOID pOid=new smiOID;
- //if(SnmpStrToOid(name,pOid)==SNMPAPI_FAILURE)
- /******************qibing*/
- // SnmpMgrStrToOid(name,pOid);
- /*********************qibing*/
- if(SnmpStrToOid(name,pOid)==SNMPAPI_FAILURE)
- {AfxMessageBox("SetVbl failure--oid");
- /* { //dwErr=SnmpGetLastError(session);
- switch( dwErr)
- {
- case SNMPAPI_NOT_INITIALIZED:
- AfxMessageBox(" The SnmpStartup function did not complete successfully!");
- break;
- case SNMPAPI_ALLOC_ERROR:
- AfxMessgeBox("error in memory allocation");
- break;
- case SNMPAPI_OID_INVALID:
- AfxMessgeBox("invalid Oid ");
- break;
- case SNMPAPI_OTHER_ERROR :
- AfxMessgeBox( " other error");
- break;
- default:
- break
- }*/
- }
- if(SnmpSetVb(m_hvbl,0,pOid,NULL)==SNMPAPI_FAILURE) /*0--1*/
- {AfxMessageBox("SetVbl failure");
- //dwErr=SnmpGetLastError(session);
- }
- if(pOid!=NULL)
- delete pOid;
- }
- CSNMP1::CreatePdu(smiINT PDU_type,smiINT32 request_id,
- smiINT error_status,smiINT error_index)
- {
- m_hpdu=SnmpCreatePdu(session,PDU_type,NULL,error_status,error_index,m_hvbl);
- if(m_hpdu==SNMPAPI_FAILURE)
- {
- AfxMessageBox("CreatePdu failure");
- //dwErr=SnmpGetLastError(session);
- }
- else if (error_status > 0)/* 进对getBulk*/
- {
- // AfxMessageBox("Error: error_status=%d, error_index=%dn",
- // error_status, error_index);
- }
- }
- CSNMP1::Send(LPCSTR address,const char * community)
- {
- // oldMemState.Checkpoint();
- HSNMP_ENTITY hAgent;
- if((hAgent=SnmpStrToEntity(session,address))==SNMPAPI_FAILURE)
- AfxMessageBox("SendMsg failure--entity");
- smiOCTETS contextName;
- contextName.ptr=(unsigned char *)community;
- contextName.len=lstrlen(community);
- HSNMP_CONTEXT hView;
- if((hView=SnmpStrToContext(session,&contextName))==SNMPAPI_FAILURE)
- AfxMessageBox("SendMsg failure--context");
- if(SnmpSendMsg(session,NULL,hAgent,hView,m_hpdu)==SNMPAPI_FAILURE)
- {
- AfxMessageBox("SendMsg failure");
- //dwErr=SnmpGetLastError(session);
- CString str;
- str.Format("%d",SnmpGetLastError(session));
- AfxMessageBox(str);
- }
- if(m_hpdu!=NULL)
- SnmpFreePdu(m_hpdu);
- if(m_hvbl!=NULL)
- SnmpFreeVbl(m_hvbl);
- if(hAgent!=NULL)
- SnmpFreeEntity(hAgent);
- if(hView!=NULL)
- SnmpFreeContext(hView);
- }
- CSNMP1::Register()
- {
- SNMPAPI_STATUS dwErr;
- if(SnmpRegister(session,NULL,NULL,NULL,NULL,SNMPAPI_ON)==SNMPAPI_FAILURE)
- {
- AfxMessageBox("Register failure");
- if((dwErr=SnmpGetLastError(session_trap))==SNMPAPI_SUCCESS)
- AfxMessageBox("strange");
- CString str;
- str.Format("%d",dwErr);
- AfxMessageBox(str);
- }
- }
- int CSNMP1::Receive(LPTSTR *name,smiLPVALUE *value,smiLPINT PDU_type,char* pSrc)
- {
- HSNMP_ENTITY srcEntity;
- HSNMP_ENTITY dstEntity;
- HSNMP_CONTEXT context; // store for the COMMUNITY field
- HSNMP_PDU pPdu;
- smiLPINT32 request_id=new smiINT32;
- smiLPINT error_status=new smiINT;
- smiLPINT error_index=new smiINT;
- HSNMP_VBL varbindlist;
- smiLPOID pOid=new smiOID;
- if(SnmpRecvMsg(session,&srcEntity,&dstEntity,&context,&pPdu)!=SNMPAPI_SUCCESS)
- {
- // CString str;
- // str.Format("%d",SnmpGetLastError(NULL));
- // AfxMessageBox(str);
- //AfxMessageBox("receive failure--recv");
- return 0;
- }
- if(SnmpGetPduData(pPdu,PDU_type,request_id,error_status,error_index,&varbindlist)!=SNMPAPI_SUCCESS)
- {
- /* AfxMessageBox("receive failure--getpdu");
- CString str;
- str.Format("%d",SnmpGetLastError(NULL));
- AfxMessageBox(str);*/
- return 0;
- }
- if((nCount=SnmpCountVbl(varbindlist))==SNMPAPI_FAILURE)
- //AfxMessageBox("Count Vbl Error");
- return 0;
- for(int i=1;i<=nCount;i++)
- {
- if(SnmpGetVb(varbindlist,i,pOid,value[i])!=SNMPAPI_SUCCESS)
- {
- /* AfxMessageBox("receive failure--getvb");
- CString str;
- str.Format("%d",SnmpGetLastError(NULL));
- AfxMessageBox(str);*/
- return 0;
- }
- if(SnmpOidToStr(pOid,100,name[i])==SNMPAPI_FAILURE)
- {
- /*AfxMessageBox("Get Vb Error");
- CString str;
- str.Format("%d",SnmpGetLastError(NULL));
- AfxMessageBox(str);*/
- return 0;
- }
- }
- //SnmpOidToStr(pOid,100,name[i]);
- /* insert here the processing routines, such as :
- 1.Check whether the PDU_type is GetResponse PDU or Trap PDU
- 2.if PDU_type is GetResponse PDU, verify its requestid. else goto step 5
- 3.Record error_status, error_index, into the MIB-II snmp group
- 4.Store requested data into database
- 5. intriger Trap handle process.*/
- //if (*PDU_type==SNMP_PDU_TRAP )
- //{
- SnmpEntityToStr(srcEntity,100,pSrc);
- /* smiOCTETS* a;
- SnmpContextToStr(context,a);
- CString test
- =a->ptr;
- AfxMessageBox(test);*/
- //}
- SnmpFreeEntity(srcEntity);
- SnmpFreeEntity(dstEntity);
- SnmpFreeContext(context);
- SnmpFreePdu(pPdu);
- SnmpFreeVbl(varbindlist);
- //SnmpFreeDescriptor(SNMP_SYNTAX_OID,*pOid);
- // delete PDU_type;
- delete request_id;
- delete error_status;
- delete error_index;
- delete pOid;
- return 1;
- }