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

📄 ping.cpp

📁 RouteTrace_ICMP
💻 CPP
字号:
// Ping.cpp: implementation of the CPing class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Ping.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CPing::CPing()
{
	bValid = FALSE;
	WSADATA wsaData;			// WSADATA
	int nRet;					// General use return code

	// Dynamically load the ICMP.DLL
	hndlIcmp = LoadLibrary("ICMP.DLL");
	if (hndlIcmp == NULL)
	{
		::MessageBox(NULL, "Could not load ICMP.DLL", "Error:", MB_OK);
		return;
	}
	// Retrieve ICMP function pointers
	pIcmpCreateFile  = (HANDLE (WINAPI *)(void))
		GetProcAddress((HMODULE)hndlIcmp,"IcmpCreateFile");
	pIcmpCloseHandle = (BOOL (WINAPI *)(HANDLE))
		GetProcAddress((HMODULE)hndlIcmp,"IcmpCloseHandle");
	pIcmpSendEcho = (DWORD (WINAPI *)
		(HANDLE,DWORD,LPVOID,WORD,PIPINFO,LPVOID,DWORD,DWORD))
		GetProcAddress((HMODULE)hndlIcmp,"IcmpSendEcho");
	// Check all the function pointers
	if (pIcmpCreateFile == NULL		|| 
		pIcmpCloseHandle == NULL	||
		pIcmpSendEcho == NULL)
	{
		::MessageBox(NULL, "Error loading ICMP.DLL", "Error:", MB_OK);
		FreeLibrary((HMODULE)hndlIcmp);
		return;
	}

	// Init WinSock
	nRet = WSAStartup(0x0101, &wsaData );
    if (nRet)
    {
		::MessageBox(NULL, "WSAStartup() error:", "Error:", MB_OK);
        WSACleanup();
		FreeLibrary((HMODULE)hndlIcmp);
        return;
    }
    // Check WinSock version
    if (0x0101 != wsaData.wVersion)
    {
		::MessageBox(NULL, "No WinSock version 1.1 support found", "Error:", MB_OK);
        WSACleanup();
		FreeLibrary((HMODULE)hndlIcmp);
        return;
    }
	bValid = TRUE;
}

CPing::~CPing()
{
    WSACleanup();
	FreeLibrary((HMODULE)hndlIcmp);
}

int CPing::Ping(CString strHost, int iSendDataTTL,CString *strRouterIP,int *iRTT)
{
	struct in_addr iaDest;		// Internet address structure
    LPHOSTENT pHost;			// Pointer to host entry structure
	DWORD *dwAddress;			// IP Address
	IPINFO ipInfo;				// IP Options structure
	ICMPECHO icmpEcho;			// ICMP Echo reply buffer
	HANDLE hndlFile;			// Handle for IcmpCreateFile()

    if(!bValid)
	{
		return FALSE;
	}

	// Lookup destination
    // Use inet_addr() to determine if we're dealing with a name
    // or an address
    pHost = gethostbyname(strHost);
	if (pHost == NULL)
	{
		return -1;
	}
	unsigned long  IPAddress = *(DWORD *)(*pHost->h_addr_list);
	strHost.Format("%s",inet_ntoa (*((struct in_addr *)pHost->h_addr)));

    iaDest.s_addr = inet_addr( (char *)(LPCSTR)strHost );

	// Copy the IP address
	dwAddress = (DWORD *)(*pHost->h_addr_list);

	// Get an ICMP echo request handle        
	hndlFile = pIcmpCreateFile();

	// Set some reasonable default values
	ipInfo.Ttl = iSendDataTTL;
	ipInfo.Tos = 0;
	ipInfo.IPFlags = 0;
	ipInfo.OptSize = 0;
	ipInfo.Options = NULL;
	icmpEcho.Status = 0;
	// Reqest an ICMP echo

	pIcmpSendEcho(
		hndlFile,		// Handle from IcmpCreateFile()
		*dwAddress,		// Destination IP address
		NULL,			// Pointer to buffer to send
		0,				// Size of buffer in bytes
		&ipInfo,		// Request options
		&icmpEcho,		// Reply buffer
		sizeof(struct tagICMPECHO),
		1000);			// Time to wait in milliseconds

	// Close the echo request file handle
	pIcmpCloseHandle(hndlFile);
	
	// Print the results
	iaDest.s_addr = icmpEcho.Source;

	if ( (icmpEcho.Status == REPLY_ROUTESTATE) || (icmpEcho.Status == REPLY_ARRIVE))
	{
		iaDest.s_addr = icmpEcho.Source;
		strRouterIP->Format("%s", inet_ntoa(iaDest));
		*iRTT = icmpEcho.RTTime;
		return icmpEcho.Status;

	}
	else
	{
		strRouterIP->Format("%s", "_");
		return -1;
	}

	return -1;
}

⌨️ 快捷键说明

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