⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 threadfunc2.h

📁 一个windows系统下查看网络拓扑结构的程序
💻 H
字号:
#ifndef THREADFUNCTION
#define THREADFUNCTION
#include <afx.h>
#include <afxwin.h>         // MFC 核心组件和标准组件
#include <afxext.h>         // MFC 扩展
#include <afxdtctl.h>		// MFC 对 Internet Explorer 4 公共控件的支持
#include <winsock2.h> 
#include <ws2tcpip.h> 
#include <iphlpapi.h>
#include <stdlib.h> 
#include <afxtempl.h>
#define WM_PING (WM_USER+104)
#define WM_PING_FINISH (WM_USER+105)
CCriticalSection Section4;

#pragma comment (lib,"ws2_32")
typedef void * (__stdcall *PIcmpCreateFile)(void);
typedef void * (__stdcall *PIcmpCloseHandle)(HANDLE IcmpHandle);
typedef unsigned long (__stdcall *PIcmpSendEcho2)(HANDLE IcmpHandle,HANDLE Event,  FARPROC ApcRoutine,  PVOID ApcContext, IPAddr DestinationAddress,  LPVOID RequestData, WORD RequestSize,  PIP_OPTION_INFORMATION RequestOptions,  LPVOID ReplyBuffer, DWORD ReplySize,  DWORD Timeout);
struct IcmpThreadStruct2
{
	CWnd *MessageHandler;
	PIcmpCreateFile IcmpCreateFile;
	PIcmpSendEcho2 IcmpSendEcho2;
	PIcmpCloseHandle IcmpCloseHandle;
	FARPROC  ApcRoutine;
	PVOID Buffer;
	bool *Stop;
	LPARAM lp;
	WPARAM wp;
};
struct ApcParament
{
	CHAR Buffer[512];
	IcmpThreadStruct2* PIcmpThreadStruct;
 };
class tool2
{
public:
    char output[3*4+3+1];

	char *NetIpToStr(u_long in)
	{
		u_char *p;
		p = (u_char *)&in;
		sprintf(output, "%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
		return output;
	}
};

//struct ApcParament

void ApcFunc2(void *i)
{ 
	IcmpThreadStruct2* PIcmpThreadStruct=((ApcParament*)i)->PIcmpThreadStruct;
	if(*PIcmpThreadStruct->Stop==false)
	{	
		ICMP_ECHO_REPLY* P_Icmp_Echo_Option=(ICMP_ECHO_REPLY*)i;
		ULONG DestIP=(ULONG)P_Icmp_Echo_Option->Address;
		hostent *HostInfo=NULL;
		tool2 TheTool;
		if(P_Icmp_Echo_Option->RoundTripTime<100000)
		{
			sprintf((char*)PIcmpThreadStruct->Buffer,"Host : %-16s; Time Spend: %d ms\r\n",TheTool.NetIpToStr((ULONG)P_Icmp_Echo_Option->Address),P_Icmp_Echo_Option->RoundTripTime);
		}
		else
		{
			sprintf((char*)PIcmpThreadStruct->Buffer,"Host : %-16s; Time Out\r\n",TheTool.NetIpToStr((ULONG)P_Icmp_Echo_Option->Address));
		}

		PIcmpThreadStruct->MessageHandler->SendMessage(WM_PING,PIcmpThreadStruct->wp,PIcmpThreadStruct->lp);
	}
}
int SearchTheLan2(ULONG DestIP,ULONG NetMask,IcmpThreadStruct2* PIcmpThreadStruct)
{

	PIcmpThreadStruct->ApcRoutine=(FARPROC)&ApcFunc2;;
	WSADATA tmp;
	if(WSAStartup(MAKEWORD(2,1),&tmp)!=0)
	{
		return -1;
		
	}
	HMODULE hInst=LoadLibrary("iphlpapi.dll");
	if(!hInst)
	{
		return -1;
	}
		//依次获得所需的三个函数指针
	PIcmpThreadStruct->IcmpCreateFile=(PIcmpCreateFile)GetProcAddress(hInst,"IcmpCreateFile");
	PIcmpThreadStruct->IcmpSendEcho2=(PIcmpSendEcho2)GetProcAddress(hInst,"IcmpSendEcho2");
	PIcmpThreadStruct->IcmpCloseHandle=(PIcmpCloseHandle)GetProcAddress(hInst,"IcmpCloseHandle");
	if(PIcmpThreadStruct->IcmpCreateFile==NULL||PIcmpThreadStruct->IcmpSendEcho2==NULL||PIcmpThreadStruct->IcmpCloseHandle==NULL)
	{
		return -1;
	}
	HANDLE IcmpHandle=0;
	IcmpHandle=PIcmpThreadStruct->IcmpCreateFile();//打开ICMP句柄 
	if(IcmpHandle==0)
	{
		return -1;
	}
	else
	{
			
		IP_OPTION_INFORMATION IpOption;//该结构用来控制所发ICMP数据包的IP头的相应字段值 
		IpOption.Flags=0;
		IpOption.OptionsData=NULL;
		IpOption.OptionsSize=0;
		IpOption.Tos=0;
		IpOption.Ttl=123;
		char *SendData = "DF is the best!"; 
		ApcParament *ReplyBuffer=new ApcParament[~ntohl(NetMask)];
		ULONG DestAddress=ntohl(NetMask&DestIP);
		int NumberOfIP=((~(ntohl(NetMask)))-1);
		for(int i=0;i<NumberOfIP;i++)
		{
			ReplyBuffer[i].PIcmpThreadStruct=PIcmpThreadStruct;
			DestAddress++;
			int Res=0;
			Res=PIcmpThreadStruct->IcmpSendEcho2(IcmpHandle,NULL,PIcmpThreadStruct->ApcRoutine,(void*)(&ReplyBuffer[i]),htonl(DestAddress),SendData,(WORD)strlen(SendData),&IpOption,ReplyBuffer[i].Buffer,512,35000);
			SleepEx(1,true);
			if(*PIcmpThreadStruct->Stop)
			{
				break;
			}
		}//end of while
	
		while((*PIcmpThreadStruct->Stop)==false&&SleepEx(1000,true)==WAIT_IO_COMPLETION );
		PIcmpThreadStruct->IcmpCloseHandle(IcmpHandle);
		WSACleanup();
		delete []ReplyBuffer;
		if(*PIcmpThreadStruct->Stop!=true)
		{
			PIcmpThreadStruct->MessageHandler->SendMessage(WM_PING_FINISH,PIcmpThreadStruct->wp,PIcmpThreadStruct->lp);
		}
		return 0;
	}
}

UINT MyPingProc2( LPVOID pParam )
{
	IcmpThreadStruct2* PIcmpThreadStruct=new IcmpThreadStruct2;
	IcmpThreadParament * Para=(IcmpThreadParament *)pParam;
	PIcmpThreadStruct->Buffer=Para->Buffer;
	PIcmpThreadStruct->MessageHandler=Para->MessageHandler;
	PIcmpThreadStruct->Stop=&(Para->Stop);
	PIcmpThreadStruct->lp=Para->lp;
	PIcmpThreadStruct->wp=Para->wp;
	UINT ReturnValue=SearchTheLan2(Para->HostIP,Para->NetMask,PIcmpThreadStruct);
	delete PIcmpThreadStruct;
	return ReturnValue;
	
  // thread completed successfully
} 
#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -