DoFindTopology.cpp
上传用户:yfy060102
上传日期:2021-05-22
资源大小:60k
文件大小:3k
- #include "stdafx.h"
- #include "MIB.h"
- #include "winsock2.h"
- #include "SNMPAppDlg.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- #define WM_ROUTERFINISH WM_USER+777
- #define WM_PRINTSTR WM_USER+778
- CObArray routerSet; //CRouter
- CString infprint;
- int level;
- CSNMPAppDlg* pMainWnd=NULL;
- CEvent eStop;
- void RecalRouter(CString& strIp,CSnmp* psnmp);
- void printfToStr(CString& strInf);
- void strPrintf(const char * pstr,int style=0,int color=0);
- BOOL IsIpInRouterSet(CString& strIp);
- enum {normal=0,bold=1,italic=2};
- enum {black=0,blue=0x00ff0000,green=0x0000ff00,red=0x000000ff};
- UINT DoFindTopology(LPVOID pParam)
- {
- CString strTemp;
- //Initial snmp for GET operation
- CSnmp snmp;
- if (!snmp.CreateSession())
- return 1;
- pMainWnd=(CSNMPAppDlg*)pParam;
- CString ip=pMainWnd->m_input;
- snmp.m_community=pMainWnd->m_community;
- level=atoi(pMainWnd->m_num);
- if (level<0)
- {
- strPrintf("Level value error!n",italic,black);
- return 1;
- }
- strPrintf("Starting topology finding!n",italic,black);
- RecalRouter(ip,&snmp);
- strPrintf("Find Router End,OK!n",italic,red);
- pMainWnd->SendMessage(WM_ROUTERFINISH, 0, 0);
- // snmp.Close();
- // snmp.Cleanup();
- return 0;
- }
- void RecalRouter(CString& strIp,CSnmp* psnmp)
- {
- CString strTemp;
- if (level>3)
- {
- level--;
- return;
- }
- if ( strIp.CollateNoCase("0.0.0.0")==0 ||
- strIp.CollateNoCase("127.0.0.1")==0 )
- {
- level--;
- return;
- }
- if (IsIpInRouterSet(strIp))
- {
- level--;
- return;
- }
- strTemp.Format("Trying an Ip address:%s ...n",strIp);
- strPrintf(strTemp,italic,black);
- CRouter* pRt=new CRouter();
- pRt->m_ipAddr=strIp;
- if (!pRt->GetRouterInfo(psnmp))
- {
- strPrintf("Router unkown.n",italic,red);
- delete pRt;
- level--;
- return;
- }
- routerSet.Add(pRt);
- strTemp.Format("Find a Router:%s!n",pRt->m_ipAddr);
- strPrintf(strTemp,italic,red);
- pRt->PrintRouterInfo();
- level++;
- for (int j=0;j<pRt->m_ipRouteTable.m_ipRouteTableEntries.GetSize();j++)
- {
- CipRouteTableEntry* pEntry=
- (CipRouteTableEntry*)(pRt->m_ipRouteTable.m_ipRouteTableEntries.GetAt(j));
- RecalRouter(pEntry->ipRouteNextHop,psnmp);
- }
- }
- void strPrintf(const char * pstr,int style,int color)
- {
- infprint=pstr;
- ASSERT(pMainWnd);
- pMainWnd->SendMessage(WM_PRINTSTR,style,color);
- return ;
- }
- BOOL IsIpInRouterSet(CString& strIp)
- {
- for (int i=0;i<routerSet.GetSize();i++)
- {
- CRouter* pRouter=(CRouter*)routerSet.GetAt(i);
- for (int j=0;
- j<pRouter->m_ipAddrTable.m_ipAddrTableEntries.GetSize();j++)
- {
- CipAddrTableEntry* pEntry=
- (CipAddrTableEntry*)pRouter->m_ipAddrTable.m_ipAddrTableEntries.GetAt(j);
- if (pEntry->ipAdEntAddr.CompareNoCase(strIp)==0)
- return TRUE;
- }
- }
- return FALSE;
- }