MIB.cpp
上传用户:yfy060102
上传日期:2021-05-22
资源大小:60k
文件大小:7k
- // MIB.cpp : implementation file
- //
- #include "stdafx.h"
- #include "SNMPApp.h"
- #include "snmp.h"
- #include "MIB.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- void strPrintf(const char * pstr,int style=0,int color=0);
- enum {normal=0,bold=1,italic=2};
- enum {black=0,blue=0x00ff0000,green=0x0000ff00,red=0x000000ff};
- #define WM_ROUTERFINISH WM_USER+78
- CRouter::CRouter()
- {
- m_psnmp=NULL;
- }
- CRouter::~CRouter()
- {
- int i;
- for (i=0;i<m_ifTable.m_ifTableEntries.GetSize();i++)
- delete m_ifTable.m_ifTableEntries.GetAt(i);
- for (i=0;i<m_ipAddrTable.m_ipAddrTableEntries.GetSize();i++)
- delete m_ipAddrTable.m_ipAddrTableEntries.GetAt(i);
- for (i=0;i<m_ipRouteTable.m_ipRouteTableEntries.GetSize();i++)
- delete m_ipRouteTable.m_ipRouteTableEntries.GetAt(i);
- }
- BOOL CRouter::GetSystem(CSnmp* pSnmp)
- {
- m_psnmp=pSnmp;
- //internet(1.3.6.1).mgmt(2).mib2(1)
- m_CurrOid[0]="1.3.6.1.2.1.1.1.0";//system(1).sysDescr(1).0
- m_CurrOid[1]="1.3.6.1.2.1.1.2.0";//system(1).sysObjectID(2).0
- m_CurrOid[2]="1.3.6.1.2.1.1.5.0";//system(1).sysName(3).0
- m_psnmp->CreateVbl(NULL,NULL);
- for(int i=0;i<3;i++)
- m_psnmp->SetVbl(m_CurrOid[i],NULL);
- m_psnmp->CreatePdu(SNMP_PDU_GET,NULL,NULL,NULL);
- m_psnmp->Send((LPCTSTR)m_ipAddr);
- if (!m_psnmp->WaitForResponse())
- {
- m_sys.sysName="none";
- m_sys.sysDescr="Unkown";
- m_sys.sysObjectID=0;
- return FALSE;
- }
- m_psnmp->Receive();
- for (i=0;i<3;i++)
- {
- if(strcmp(m_psnmp->m_strOid[i],m_CurrOid[0])==0)
- m_sys.sysDescr=m_psnmp->m_strValue[i];
- else if (strcmp(m_psnmp->m_strOid[i],m_CurrOid[1])==0)
- m_sys.sysObjectID=atoi(m_psnmp->m_strValue[i]);
- else if (strcmp(m_psnmp->m_strOid[i],m_CurrOid[2])==0)
- m_sys.sysName=m_psnmp->m_strValue[i];
- }
- return TRUE;
- }
- void CRouter::GetifTable(CSnmp* pSnmp)
- {
- m_psnmp=pSnmp;
- ASSERT(m_psnmp->IsCreated());
- m_CurrOid[0]="1.3.6.1.2.1.2.1";
- m_psnmp->CreateVbl(NULL,NULL);
- m_psnmp->SetVbl(m_CurrOid[0],NULL);
- m_psnmp->CreatePdu(SNMP_PDU_GET,NULL,NULL,NULL);
- m_psnmp->Send((LPCTSTR)m_ipAddr);
- if (!m_psnmp->WaitForResponse())
- {
- AfxMessageBox("Get ifTable failure!");
- return;
- }
- m_psnmp->Receive();
- m_ifTable.ifNumber=atoi(m_psnmp->m_strValue[0]);
- //internet(1.3.6.1).mgmt(2).mib2(1).interfaces(2)
- CString firstStr="1.3.6.1.2.1.2.2.1.1";//ifIndex:index
- CString nextStr="1.3.6.1.2.1.2.2.1.1";//ifIndex
- do
- {
- if (!GetTableCol(nextStr,firstStr,pSnmp))
- break;
- CifTableEntry* pNewEntry=new CifTableEntry();
- pNewEntry->ifIndex=atoi(m_psnmp->m_strValue[0]);
- m_ifTable.m_ifTableEntries.Add(pNewEntry);;
- }while(1);
- m_CurrOid[0]="1.3.6.1.2.1.2.2.1.2";//ifDescr
- m_CurrOid[1]="1.3.6.1.2.1.2.2.1.3";//ifType
- m_CurrOid[2]="1.3.6.1.2.1.2.2.1.6";//ifPhysAddress
- char buff[10];
- for (int i=0;i<m_ifTable.m_ifTableEntries.GetSize();i++)
- {
- m_psnmp->CreateVbl(NULL,NULL);
- CifTableEntry* pEntry=
- (CifTableEntry*)(m_ifTable.m_ifTableEntries.GetAt(i));
- itoa(pEntry->ifIndex,buff,10);
- m_psnmp->SetVbl(m_CurrOid[0]+'.'+buff,NULL);
- m_psnmp->SetVbl(m_CurrOid[1]+'.'+buff,NULL);
- m_psnmp->CreatePdu(SNMP_PDU_GET,NULL,NULL,NULL);
- m_psnmp->Send((LPCTSTR)m_ipAddr);
- m_psnmp->WaitForResponse();
- m_psnmp->Receive();
- pEntry->ifDescr=m_psnmp->m_strValue[0];
- pEntry->ifType=atoi(m_psnmp->m_strValue[1]);
- pEntry->ifPhysAddress=atoi(m_psnmp->m_strValue[2]);
- }
- }
- void CRouter::GetipAddrTable(CSnmp* pSnmp)
- {
- m_psnmp=pSnmp;
- //internet(1.3.6.1).mgmt(2).mib2(1).
- //ip(4).ipAddrTable(20).ipAddrEntry(1)
- CString firstStr="1.3.6.1.2.1.4.20.1.1";//ipAdEntAddr:index
- CString nextStr="1.3.6.1.2.1.4.20.1.1";
- do
- {
- if (!GetTableCol(nextStr,firstStr,pSnmp))
- break;
- CipAddrTableEntry* pNewEntry=new CipAddrTableEntry();
- pNewEntry->ipAdEntAddr=m_psnmp->m_strValue[0];
- m_ipAddrTable.m_ipAddrTableEntries.Add(pNewEntry);
- }while(1);
- m_CurrOid[0]="1.3.6.1.2.1.4.20.1.2";//ipAdEntIfIndex
- m_CurrOid[1]="1.3.6.1.2.1.4.20.1.3";//ipAdEntNetMask
- for (int i=0;i<m_ipAddrTable.m_ipAddrTableEntries.GetSize();i++)
- {
- m_psnmp->CreateVbl(NULL,NULL);
- CipAddrTableEntry* pEntry=
- (CipAddrTableEntry*)(m_ipAddrTable.m_ipAddrTableEntries.GetAt(i));
- m_psnmp->SetVbl(m_CurrOid[0]+'.'+pEntry->ipAdEntAddr,NULL);
- m_psnmp->SetVbl(m_CurrOid[1]+'.'+pEntry->ipAdEntAddr,NULL);
- m_psnmp->CreatePdu(SNMP_PDU_GET,NULL,NULL,NULL);
- m_psnmp->Send((LPCTSTR)m_ipAddr);
- m_psnmp->WaitForResponse();
- m_psnmp->Receive();
- pEntry->ipAdEntIfIndex=atoi(m_psnmp->m_strValue[0]);
- pEntry->ipAdEntNetMask=m_psnmp->m_strValue[1];
- }
- }
- void CRouter::GetipRouteTable(CSnmp* pSnmp)
- {
- m_psnmp=pSnmp;
- ASSERT(m_psnmp->IsCreated());
- //internet(1.3.6.1).mgmt(2).mib2(1).
- //ip(4).ipRouteTable(21).ipRouteEntry(1)
- CString firstStr="1.3.6.1.2.1.4.21.1.1";//ipRouteDest:index
- CString nextStr="1.3.6.1.2.1.4.21.1.1";
- do
- {
- if (!GetTableCol(nextStr,firstStr,pSnmp))
- break;
- CipRouteTableEntry* pNewEntry=new CipRouteTableEntry();
- pNewEntry->ipRouteDst=m_psnmp->m_strValue[0];
- m_ipRouteTable.m_ipRouteTableEntries.Add(pNewEntry);
- }while(1);
- m_CurrOid[0]="1.3.6.1.2.1.4.21.1.2";//ipRouteIfIndex
- m_CurrOid[1]="1.3.6.1.2.1.4.21.1.7";//ipRouteNextHop
- for (int i=0;i<m_ipRouteTable.m_ipRouteTableEntries.GetSize();i++)
- {
- m_psnmp->CreateVbl(NULL,NULL);
- CipRouteTableEntry* pEntry=
- (CipRouteTableEntry*)(m_ipRouteTable.m_ipRouteTableEntries.GetAt(i));
- m_psnmp->SetVbl(m_CurrOid[0]+'.'+pEntry->ipRouteDst,NULL);
- m_psnmp->SetVbl(m_CurrOid[1]+'.'+pEntry->ipRouteDst,NULL);
- m_psnmp->CreatePdu(SNMP_PDU_GET,NULL,NULL,NULL);
- m_psnmp->Send((LPCTSTR)m_ipAddr);
- m_psnmp->WaitForResponse();
- m_psnmp->Receive();
- pEntry->ipRouteIfIndex=atoi(m_psnmp->m_strValue[0]);
- pEntry->ipRouteNextHop=m_psnmp->m_strValue[1];
- }
- }
- BOOL CRouter::GetRouterInfo(CSnmp* pSnmp)
- {
- ASSERT(pSnmp);
- m_psnmp=pSnmp;
- if (!GetSystem(pSnmp))
- return FALSE;
- GetifTable(pSnmp);
- GetipAddrTable(pSnmp);
- GetipRouteTable(pSnmp);
- m_psnmp=NULL;
- return TRUE;
- }
- BOOL CRouter::GetTableCol(CString &nStr, CString &fStr,CSnmp* psnmp)
- {
- m_psnmp=psnmp;
- ASSERT(m_psnmp->IsCreated());
- m_psnmp->CreateVbl(NULL,NULL);
- m_psnmp->SetVbl(nStr,NULL);
- m_psnmp->CreatePdu(SNMP_PDU_GETNEXT,NULL,NULL,NULL);
- m_psnmp->Send((LPCTSTR)m_ipAddr);
- if (!m_psnmp->WaitForResponse())
- return FALSE;
- m_psnmp->Receive();
- if(m_psnmp->PDU_type!=SNMP_PDU_RESPONSE)
- return FALSE;//接收到错误信息
-
- //我认为已经到达表的最后一行,任务完成
- if (m_psnmp->error_status==SNMP_ERROR_NOSUCHNAME)
- return FALSE;
- int len=fStr.GetLength();
- CString str=m_psnmp->m_strOid[0].Left(len);
- if (fStr.CompareNoCase((LPCTSTR)str)!=0)
- return FALSE;
- nStr=m_psnmp->m_strOid[0];
- return TRUE;
- }
- void CRouter::PrintRouterInfo()
- {
- CString temp;
- strPrintf("///The information of router:",italic,0x00dd00ff);
- strPrintf(m_ipAddr,italic,blue);
- strPrintf("////n",italic,0x00dd00ff);
- strPrintf("Router name:",bold,blue);
- strPrintf(m_sys.sysName);
- strPrintf("nRouter ObjectID:",bold,blue);
- temp.Format("%dn",m_sys.sysObjectID);
- strPrintf(temp);
- strPrintf("Router descriptor:",bold,blue);
- temp.Format(":n%sn",m_sys.sysDescr);
- strPrintf(temp);
- strPrintf("Router interface Ips:n ",bold,blue);
- for (int i=0;i<m_ipAddrTable.m_ipAddrTableEntries.GetSize();i++)
- {
- CipAddrTableEntry* pEntry=
- (CipAddrTableEntry*)m_ipAddrTable.m_ipAddrTableEntries.GetAt(i);
- strPrintf(pEntry->ipAdEntAddr);
- strPrintf("t");
- }
-
- strPrintf("n///Router Information end.///nn",italic,0x00dd00ff);
- }