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

📄 threadfunc.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 Section3;

#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 IcmpThreadStruct
{
	CWnd *MessageHandler;
	PIcmpCreateFile IcmpCreateFile;
	PIcmpSendEcho2 IcmpSendEcho2;
	PIcmpCloseHandle IcmpCloseHandle;
	FARPROC  ApcRoutine;
	PVOID Buffer;
	bool *Stop;
	LPARAM lp;
	WPARAM wp;
};
class tool
{
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 ApcParament2
{	
	CHAR Buffer[512];
	IcmpThreadStruct* PIcmpThreadStruct;

};

//struct ApcParament

void ApcFunc(void *i)
{
	IcmpThreadStruct* PIcmpThreadStruct=((ApcParament2*)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;
	    tool TheTool;
		if(P_Icmp_Echo_Option->RoundTripTime<100000)
		{
			sprintf((char*)PIcmpThreadStruct->Buffer,"host Address: %-15s; 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 Address: %-15s; Time Out\r\n",TheTool.NetIpToStr((ULONG)P_Icmp_Echo_Option->Address));
		}

		PIcmpThreadStruct->MessageHandler->SendMessage(WM_PING,PIcmpThreadStruct->wp,PIcmpThreadStruct->lp);
	}
}
int SearchTheLan(ULONG DestIP,ULONG NetMask,IcmpThreadStruct* PIcmpThreadStruct)
{
	PIcmpThreadStruct->ApcRoutine=(FARPROC)&ApcFunc;;
	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!"; 
		ApcParament2 *ReplyBuffer=new  ApcParament2[~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(5000,true)==WAIT_IO_COMPLETION );
	PIcmpThreadStruct->IcmpCloseHandle(IcmpHandle);
	WSACleanup();
	if(*PIcmpThreadStruct->Stop!=true)
	{
		PIcmpThreadStruct->MessageHandler->SendMessage(WM_PING_FINISH,PIcmpThreadStruct->wp,PIcmpThreadStruct->lp);
	}
	return 0;
}

UINT MyPingProc( LPVOID pParam )
{
	IcmpThreadStruct* PIcmpThreadStruct=new IcmpThreadStruct;
	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=SearchTheLan(Para->HostIP,Para->NetMask, PIcmpThreadStruct);
	delete PIcmpThreadStruct;
	return ReturnValue;
	
  // thread completed successfully
}
#endif

⌨️ 快捷键说明

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