www.pudn.com > SNMPApp.rar > Snmp.cpp
// Snmp1.cpp : implementation file
#include "stdafx.h"
#include "Snmp.h"
#include "Winsock2.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
CSnmp::CSnmp()
{
if(SnmpStartup(&nMajorVersion,&nMinorVersion,&nLevel,&nTranslateMode,
&nRetransmitMode)!=SNMPAPI_SUCCESS)
AfxMessageBox("initilization failure");
if(SnmpSetTranslateMode(SNMPAPI_UNTRANSLATED_V1)!=SNMPAPI_SUCCESS)
AfxMessageBox("SetTranslateMode failure");
if(SnmpSetRetransmitMode(SNMPAPI_ON)!=SNMPAPI_SUCCESS)
AfxMessageBox("SetRetransmitMode failure");
m_hSession=NULL;
m_hvbl=NULL;
state=0;
}
CSnmp::~CSnmp()
{
if(m_hSession!=NULL)
SnmpClose(m_hSession);
SnmpCleanup();
}
BOOL CSnmp::CreateSession()
{
if (m_hSession)
{
AfxMessageBox("m_hSession has been created!");
return TRUE;
}
if((m_hSession=SnmpCreateSession(NULL,0,SnmpCallback, (LPVOID)this))==SNMPAPI_FAILURE)
{
AfxMessageBox("Createm_hSession failure");
return FALSE;
};
return TRUE;
}
//Callback function for snmp notify
SNMPAPI_STATUS CALLBACK CSnmp::SnmpCallback(
HSNMP_SESSION hSession, // handle to the WinSNMP m_hSession
HWND hWnd, // handle to the notification window
UINT wMsg, // window notification message number
WPARAM wParam, // type of notification
LPARAM lParam, // request identifier of PDU
LPVOID lpClientData) // optional application-defined data
{
if (wParam==SNMPAPI_TL_TIMEOUT)
((CSnmp *)lpClientData)->state=1;
((CSnmp *)lpClientData)->m_Event.SetEvent();
return SNMPAPI_SUCCESS;
}
BOOL CSnmp::CreateVbl(LPCSTR name,smiLPVALUE pvalue)
{
BOOL bRet=FALSE;
smiLPOID pOid=NULL;
if (name!=NULL)
{
pOid=new smiOID;
if(SnmpStrToOid(name,pOid)==SNMPAPI_FAILURE)
{
AfxMessageBox("CreateVbl failure");
bRet=FALSE;
goto out;
};
};
m_hvbl=SnmpCreateVbl(m_hSession,pOid,pvalue);
if(m_hvbl==SNMPAPI_FAILURE)
{
AfxMessageBox("CreateVbl failure");
bRet=FALSE;
goto out;
}
out:
//dwErr=SnmpGetLastError(m_hSession);
if(pOid!=NULL)
delete pOid;
return bRet;
}
BOOL CSnmp::SetVbl(LPCSTR name,smiLPCVALUE value)
{
BOOL bRet=TRUE;
smiLPOID pOid=new smiOID;
if(SnmpStrToOid(name,pOid)==SNMPAPI_FAILURE)
{
AfxMessageBox("SetVbl failure--oid");
//dwErr=SnmpGetLastError(m_hSession);
bRet=FALSE;
}
if(SnmpSetVb(m_hvbl,0,pOid,NULL)==SNMPAPI_FAILURE)
{
AfxMessageBox("SetVbl failure");
//dwErr=SnmpGetLastError(m_hSession);
bRet=FALSE;
}
if(pOid!=NULL)
delete pOid;
return bRet;
}
BOOL CSnmp::CreatePdu(smiINT PDU_type,smiINT32 request_id,
smiINT error_status,smiINT error_index)
{
m_hpdu=SnmpCreatePdu(m_hSession,PDU_type,NULL,error_status,error_index,m_hvbl);
if(m_hpdu==SNMPAPI_FAILURE)
{
AfxMessageBox("CreatePdu failure");
return FALSE;
//dwErr=SnmpGetLastError(m_hSession);
}
else if (error_status > 0)
{
AfxMessageBox("Error: error_status=%d, error_index=%d\n",
error_status, error_index);
return FALSE;
}
return TRUE;
}
BOOL CSnmp::Send(LPCSTR address)
{
state=0;
BOOL bRet=TRUE;
HSNMP_ENTITY hAgent;
if((hAgent=SnmpStrToEntity(m_hSession,address))==SNMPAPI_FAILURE)
AfxMessageBox("SendMsg failure--entity");
smiOCTETS contextName;
contextName.ptr=(unsigned char *)(m_community.GetBuffer(30));
contextName.len=lstrlen(m_community);
HSNMP_CONTEXT hView;
if((hView=SnmpStrToContext(m_hSession,&contextName))==SNMPAPI_FAILURE)
{
AfxMessageBox("SendMsg failure--context");
bRet=FALSE;
}
if(SnmpSendMsg(m_hSession,NULL,hAgent,hView,m_hpdu)==SNMPAPI_FAILURE)
{
AfxMessageBox("SendMsg failure");
//dwErr=SnmpGetLastError(m_hSession);
CString str;
str.Format("%d",SnmpGetLastError(m_hSession));
AfxMessageBox(str);
bRet=FALSE;
}
if(m_hpdu!=NULL)
SnmpFreePdu(m_hpdu);
if(m_hvbl!=NULL)
SnmpFreeVbl(m_hvbl);
if(hAgent!=NULL)
SnmpFreeEntity(hAgent);
if(hView!=NULL)
SnmpFreeContext(hView);
return bRet;
}
BOOL CSnmp::Register()
{
/* if(SnmpRegister(NULL,NULL,NULL,NULL,NULL,SNMPAPI_ON)==SNMPAPI_FAILURE)
{
AfxMessageBox("Register failure");
if((dwErr=SnmpGetLastError(m_hSession))=SNMPAPI_SUCCESS)
AfxMessageBox("strange");
CString str;
str.Format("%d",dwErr);
AfxMessageBox(str);
}*/
return TRUE;
}
BOOL CSnmp::Receive()
{
if (!Receive(m_strOid,m_value))
return FALSE;
CString strIp,strTemp;
for(int i=0;ivalue.string.ptr)));
strIp.Format("%d",*m_value[i].value.string.ptr);
strIp+=".";
strTemp.Format("%d",*(m_value[i].value.string.ptr+1));
strIp+=strTemp;
strIp+=".";
strTemp.Format("%d",*(m_value[i].value.string.ptr+2));
strIp+=strTemp;
strIp+=".";
strTemp.Format("%d",*(m_value[i].value.string.ptr+3));
strIp+=strTemp;
m_strValue[i]=strIp;//*/
break;
case SNMP_SYNTAX_OID:
smiOID oid=m_value[i].value.oid;
char str[100];
SnmpOidToStr(&oid,30,str);
m_strValue[i]=str;
//SnmpFreeDescriptor (SNMP_SYNTAX_OID, (smiLPOPAQUE)&oid);
break;
case SNMP_SYNTAX_NULL:
case SNMP_SYNTAX_NOSUCHOBJECT:
case SNMP_SYNTAX_NOSUCHINSTANCE:
case SNMP_SYNTAX_ENDOFMIBVIEW:
smiBYTE empty;
empty=m_value[i].value.empty;
m_strValue[i]="No Value Returned";
break;
default:
break;
};
}
return TRUE;
}
BOOL CSnmp::Receive(CString name[],smiVALUE value[])
{
if (state==1)
{
AfxMessageBox("Request TimeOut!");
for (int i=0;i<10;i++)
{
name[i]="Unkown";
value[i].syntax=SNMP_SYNTAX_NULL;
}
return FALSE;
}
BOOL bRet=TRUE;
HSNMP_ENTITY srcEntity;
HSNMP_ENTITY dstEntity;
HSNMP_CONTEXT context;
HSNMP_PDU pPdu;
HSNMP_VBL varbindlist;
smiOID Oid;
if(SnmpRecvMsg(m_hSession,&srcEntity,&dstEntity,&context,&pPdu)!=SNMPAPI_SUCCESS)
{
AfxMessageBox("receive failure--recv");
bRet=FALSE;
}
CHAR aa[100];
SnmpEntityToStr(srcEntity,100,aa);
strSrcEntity=aa;
SnmpEntityToStr(dstEntity,100,aa);
strDstEntity=aa;
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);
bRet=FALSE;
}
if (PDU_type!=SNMP_PDU_RESPONSE)
{
AfxMessageBox("Unknown package!");
bRet=FALSE;
}
if((nCount=SnmpCountVbl(varbindlist))==SNMPAPI_FAILURE)
{
AfxMessageBox("Count Vbl Error");
bRet=FALSE;
}
if (nCount>10)
{
AfxMessageBox("No buff,The number of parameters returned >10!");
bRet=FALSE;
}
for(int i=0;i