📄 snmp.cpp
字号:
// Snmp.cpp: implementation of the CSnmp class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "SnmpManager.h"
#include "Snmp.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CSnmp::CSnmp()
{
nMajorVersion = new unsigned long;
nMinorVersion = new unsigned long;
nlevel = new unsigned long;
nTranslateMode = new unsigned long;
nRetransmitMode = new unsigned long;
if(SnmpStartup(nMajorVersion, nMinorVersion, nlevel, nTranslateMode,
nRetransmitMode) != SNMPAPI_SUCCESS)
{
AfxMessageBox("SnmpStartup failed:%d",GetLastError());
return;
}
if(SnmpSetTranslateMode(SNMPAPI_UNTRANSLATED_V1) != SNMPAPI_SUCCESS)
{
AfxMessageBox("SnmpSetTranslateMode failed:%d", GetLastError());
return;
}
if(SnmpSetRetransmitMode(SNMPAPI_ON) != SNMPAPI_SUCCESS)
{
AfxMessageBox("SnmpSetTranslateMode failed:%d", GetLastError());
return;
}
sessionID = FALSE;
}
CSnmp::~CSnmp()
{
if(nMajorVersion != NULL)
delete nMajorVersion;
if(nMinorVersion != NULL)
delete nMinorVersion;
if(nlevel != NULL)
delete nlevel;
if(nTranslateMode != NULL)
delete nTranslateMode;
if(nRetransmitMode != NULL)
delete nRetransmitMode;
}
CSnmp::CreateSession(HWND hWnd, UINT wMsg)
{
if((session = SnmpCreateSession(hWnd,wMsg,NULL,NULL)) == SNMPAPI_FAILURE)
AfxMessageBox("CreateSession failure");
}
CSnmp::CreateVbl(LPCSTR name, smiLPVALUE pvalue)
{
smiLPOID pOid = new smiOID;
if(SnmpStrToOid(name, pOid) == SNMPAPI_FAILURE)
{
AfxMessageBox("CreateVbl failure");
}
m_hvbl = SnmpCreateVbl(session, pOid, pvalue);
if(m_hvbl == SNMPAPI_FAILURE)
{
AfxMessageBox("CreateVbl failure");
}
if(pOid != NULL)
delete pOid;
}
CSnmp::SetVbl(LPCSTR name)
{
smiLPOID pOid = new smiOID;
if(SnmpStrToOid(name, pOid) == SNMPAPI_FAILURE)
{
AfxMessageBox("SetVbl failure--oid");
}
if(SnmpSetVb(m_hvbl, 0, pOid, NULL) == SNMPAPI_FAILURE)
{
AfxMessageBox("SetVbl failure");
}
if(pOid != NULL)
delete pOid;
}
CSnmp::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");
}
else if (error_status > 0)
{
AfxMessageBox("Error: error_status=%d, error_index=%d\n",
error_status, error_index);
}
}
CSnmp::Send(LPCSTR address, const char * community)
{
HSNMP_ENTITY hAgent;
if((hAgent = SnmpStrToEntity(session,address)) == SNMPAPI_FAILURE)
AfxMessageBox("SendMsg failure--entity", NULL, MB_OK);
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", NULL, MB_OK);
if(SnmpSendMsg(session, NULL, hAgent, hView, m_hpdu) == SNMPAPI_FAILURE)
{
AfxMessageBox("SendMsg failure", NULL, MB_OK);
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);
}
CSnmp::Register()
{
}
CSnmp::Receive(LPTSTR *name, smiLPVALUE *value)
{
HSNMP_ENTITY srcEntity;
HSNMP_ENTITY dstEntity;
HSNMP_CONTEXT context;
HSNMP_PDU pPdu;
smiLPINT PDU_type = new smiINT;
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)
{
AfxMessageBox("receive failure--recv");
}
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);
}
if((nCount = SnmpCountVbl(varbindlist)) == SNMPAPI_FAILURE)
AfxMessageBox("Count Vbl Error");
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);}
}
SnmpFreeEntity(srcEntity);
SnmpFreeEntity(dstEntity);
SnmpFreeContext(context);
SnmpFreePdu(pPdu);
SnmpFreeVbl(varbindlist);
delete PDU_type;
delete request_id;
delete error_status;
delete error_index;
delete pOid;
return 1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -