func.cpp
资源名称:NetFinder.rar [点击查看]
上传用户:qiye66692
上传日期:2022-04-25
资源大小:72k
文件大小:5k
源码类别:
SNMP编程
开发平台:
Visual C++
- #include "StdAfx.h"
- #include ".func.h"
- #include ".mib.h"
- #include ".Snmp.h"
- #include ".msg.h"
- #include ".subNetDlg.h"
- #include ".Ping.h"
- #include ".Host.h"
- #include ".Router.h"
- CString GetLocalIP()
- {
- char name[255];
- PHOSTENT hostinfo;//use "afxsock.h"
- CString strIP;
- //returns the standard host name for the local computer
- if (gethostname(name, sizeof(name)) == 0)
- {
- //gets host data corresponding to a host name
- hostinfo = gethostbyname( name );
- if ( hostinfo != NULL )
- {
- strIP = inet_ntoa(* (struct in_addr *) *(hostinfo->h_addr_list) );
- }
- }
- return strIP;
- }
- bool GetRouterInfo( LPCSTR ip, LPCSTR community, CRouter* pRouter)
- {
- BOOL status;
- CSnmp snmp( ip, community ); //把ip和团体名传入,创建Csnmp类的实例
- status = snmp.Prepare(); //调snmp中的Prepare,完成SNMP的加载,会话的建立等
- if( status == FALSE )
- {
- pRouter->m_strName = "unknown";
- return FALSE;
- }
- CStringArray reqList; //请求列表
- //查路由器的ip地址表
- reqList.Add( ipAdEntAddr ); //在Mib.h中可看到ipAdEntAddr(ip)和
- reqList.Add( ipAdEntNetMask ); //ipAdEntNetMask(掩码)对应的objectID
- //根据请求列表查路由表信息,放到pRouter里(即添加到路由器链表尾部)
- status = snmp.GetTable( &reqList, pRouter );
- if( status == FALSE )
- return FALSE;
- //查系统表
- reqList.RemoveAll();
- reqList.Add( sysDescr );
- reqList.Add( sysUpTime );
- reqList.Add( sysName );
- status = snmp.GetTable( &reqList, pRouter );
- if( status == FALSE )
- return FALSE;
- //查路由表
- reqList.RemoveAll();
- reqList.Add( ipRouteDest );
- reqList.Add( ipRouteNextHop );
- reqList.Add( ipRouteMask );
- status = snmp.GetTable( &reqList, pRouter );
- if( status == FALSE )
- return FALSE;
- snmp.Close();
- return TRUE;
- }
- UINT SearchRouter( LPVOID pParam )
- {
- CNetFinderDlg* dlg = (CNetFinderDlg*)pParam;
- CString ip, community;
- ip = dlg->m_ip;
- community = dlg->m_community;
- DepthFirstSearch( dlg, ip, community, 1 ); //以对话框中的变量为参数传入,
- //进行深度优先搜索
- dlg->SendMessage( MSG_SEARCH_ROUTER_END, 0, 0 );//search结束后发结束消息
- return 0;
- }
- void DepthFirstSearch( CNetFinderDlg* dlg, CString ip, CString community, UINT depth )
- {
- BOOL status;
- CRouter router;
- if( depth > dlg->m_depth ) //根据对话框中搜索深度的设置,决定递归是否继续
- {
- return;
- }
- //先调获取路由信息的函数(根据ip,团体名,路由器链表尾指针)
- status = GetRouterInfo( ip, community, dlg->pTailRouter );
- if( status == FALSE )
- {
- dlg->SendMessage( MSG_FOUND_ROUTER, depth, 0 );
- WaitForSingleObject(dlg->m_router_added, INFINITE );
- dlg->m_router_added.ResetEvent();
- return;
- }
- else
- {
- dlg->SendMessage( MSG_FOUND_ROUTER, depth, 0 );
- WaitForSingleObject(dlg->m_router_added, INFINITE );
- dlg->m_router_added.ResetEvent();
- for( int i = 0; i < dlg->pCurrRouter->m_NextHopArray.GetSize(); i ++ )
- {
- for( int j = 0; j < dlg->routerArray.GetSize()-1; j ++ )
- {
- CRouter* temp;
- temp = (CRouter*)dlg->routerArray.GetAt(j);
- for( int k = 0; k < temp->m_NextHopArray.GetSize(); k ++ )
- {
- if( temp->m_NextHopArray.GetAt(k) == dlg->pCurrRouter->m_NextHopArray.GetAt(i) )
- {
- return;
- }
- }
- }
- CString tempIp = dlg->pCurrRouter->m_NextHopArray.GetAt(i);
- if( (tempIp.Left(3)!="127") && (tempIp.Left(3)!="255") && (tempIp.Left(1)!="0") && (tempIp.Left(3)!="224") )
- {
- DepthFirstSearch( dlg, tempIp, community, depth+1 );
- }
- }
- }
- }
- UINT PingHost( LPVOID pParam )
- {
- CSubNetDlg* dlg = (CSubNetDlg*)pParam;
- HWND hWnd = dlg->GetSafeHwnd();
- CPing pingHost;
- DWORD dNetMask = inet_addr(dlg->m_mask);
- DWORD dNetAddr = inet_addr(dlg->m_ip);
- dNetMask = ntohl(dNetMask);
- dNetAddr = ntohl(dNetAddr);
- DWORD dSubNet = dNetAddr & dNetMask;
- BYTE *byte = (BYTE *)&dSubNet;
- DWORD count = ~dNetMask; //得到子网中的主机数
- if (count > 255)
- count = 255; //only process a small subnet to save time
- CString strIP;
- for ( BYTE i = 1; i < count; i++)
- {
- if( ::WaitForSingleObject(dlg->m_StopEvent.m_hObject, 0) == WAIT_OBJECT_0 )
- {
- dlg->m_ExitEvent.SetEvent();
- return 1;
- }
- byte[0] += 1 ;
- strIP.Format("%d.%d.%d.%d",
- (int)byte[3], (int)byte[2], (int)byte[1], (int)byte[0]);
- if( pingHost.ping(strIP) == TRUE )
- {
- CHost host;
- dlg->m_host.m_strHostIp = strIP;
- dlg->m_host.m_strHostName = "";
- dlg->m_host.m_strHostDescr = "";
- dlg->m_host.m_strHostUpTime = "";
- CSnmp snmp(strIP, dlg->m_community);
- if(snmp.Prepare()) {
- CStringArray reqList; //请求列表
- reqList.Add( sysDescr );
- reqList.Add( sysUpTime );
- reqList.Add( sysName );
- CRouter router;
- if(snmp.GetTable(&reqList, &router)) {//如果支持SNMP协议
- dlg->m_host.m_strHostName = router.m_strName;
- dlg->m_host.m_strHostDescr = router.m_strDescr;
- dlg->m_host.m_strHostUpTime = router.m_strUpTime;
- }
- }
- ::SendMessage( hWnd, MSG_FOUND_HOST, i, 0 );
- }
- dlg->m_progress.SetPos(i);
- }
- //complete
- dlg->bInSearching = FALSE;
- dlg->MessageBox(_T("搜索完毕!"));
- dlg->GetDlgItem(IDC_SEARCH_BUTTON)->SetWindowText("开始搜索");
- dlg->m_StopEvent.SetEvent();
- return 0;
- }