DoFindSubnet().cpp
上传用户:yfy060102
上传日期:2021-05-22
资源大小:60k
文件大小:2k
- #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_SUBNETFINISH WM_USER+779
- extern CObArray routerSet;
- CStringArray subnetIpSet;
- BOOL g_Stop;
- BOOL InitPing();
- BOOL PingSubnet(CString& host,CString& netmask);
- BOOL Ping(char* sHost);
- 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};
- UINT DoFindSubnet(LPVOID pParam)
- {
- g_Stop=FALSE;
- CString strTemp;
- if (!InitPing())
- return 0;
-
- CSNMPAppDlg* pMainWnd=(CSNMPAppDlg*)pParam;
- strPrintf("nnStart subnet finding...n",italic,black);
- for (int i=0;i<routerSet.GetSize();i++)
- {
- CRouter* pRouter=(CRouter*)routerSet.GetAt(i);
- strTemp.Format("nFinding subnet of router %s:n",
- pRouter->m_sys.sysName);
- strPrintf(strTemp,italic,black);
- for (int j=0;j<pRouter->m_ipAddrTable.m_ipAddrTableEntries.GetSize();j++)
- {
- CipAddrTableEntry* pEntry=
- (CipAddrTableEntry*)pRouter->m_ipAddrTable.m_ipAddrTableEntries.GetAt(j);
- strTemp.Format("find subnet in interface %sn",pEntry->ipAdEntAddr);
- strPrintf(strTemp,italic,black);
- PingSubnet(pEntry->ipAdEntAddr,pEntry->ipAdEntNetMask);
- }
- strTemp.Format("The End of finding subnet of router %s:n",
- pRouter->m_sys.sysName);
- strPrintf(strTemp,italic,red);
- }
- strPrintf("Find subnet OK!n",italic,red);
- pMainWnd->SendMessage(WM_SUBNETFINISH, 0, 0);
- return 1;
- }
- BOOL PingSubnet(CString& host,CString& netmask)
- {
- CString temp;
- unsigned long subnetIp,subnetMask,currIp;
- if ( host.CompareNoCase("127.0.0.1")==0 ||
- host.CompareNoCase("0.0.0.0")==0 )
- {
- strPrintf("No need to find subnet.n",italic,red);
- return FALSE;
- }
- subnetMask=inet_addr(netmask);
- subnetIp=inet_addr(host);
- currIp=subnetIp;
- subnetIp &= subnetMask;
- temp=inet_ntoa(*((struct in_addr*)(&subnetIp)));
- unsigned long count=(~subnetMask)>>24;
- for (unsigned long i=1;i<count;i++)
- {
- currIp=subnetIp+(i<<24);
- CString strCurrIp=inet_ntoa(*((struct in_addr*)(&currIp)));
- if (g_Stop)
- {
- strPrintf("nThread stop.n",italic,red);
- return FALSE;
- }
- if (Ping(strCurrIp.GetBuffer(15)))
- {
- subnetIpSet.Add(strCurrIp);
- temp.Format(" %s",strCurrIp);
- strPrintf(temp,bold,blue);
- }
- }
- strPrintf("nn");
- return TRUE;
- }