www.pudn.com > SNMPApp.rar > DoFindSubnet().cpp


#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("\n\nStart subnet finding...\n",italic,black); 
	for (int i=0;im_sys.sysName); 
		strPrintf(strTemp,italic,black); 
		for (int j=0;jm_ipAddrTable.m_ipAddrTableEntries.GetSize();j++) 
		{ 
			CipAddrTableEntry* pEntry= 
				(CipAddrTableEntry*)pRouter->m_ipAddrTable.m_ipAddrTableEntries.GetAt(j); 
			strTemp.Format("find subnet in interface %s\n",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