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

📄 pingi.cpp

📁 黑色技术蠕虫下载者的完整源码
💻 CPP
字号:
// PingI.cpp: implementation of the CPingI class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "PingI.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

#define MIN_ICMP_PACKET_SIZE 8    //minimum 8 byte icmp packet (just header)
#define MAX_ICMP_PACKET_SIZE 1024 //Maximum icmp packet size

CPingI::CPingI()
{
//Load up the ICMP library
		sm_pIcmpSendEcho=NULL;
		sm_pIcmpCreateFile=NULL;
		sm_pIcmpCloseHandle=NULL;
		icmphandle = LoadLibrary("ICMP.DLL");
		if (icmphandle == NULL)
		{
			//TRACE(_T("Could not load up the ICMP DLL\n"));
			
			return;
		}

		//Retrieve pointers to the functions in the ICMP dll
		sm_pIcmpCreateFile = (lpIcmpCreateFile) GetProcAddress(icmphandle,"IcmpCreateFile");
		sm_pIcmpSendEcho = (lpIcmpSendEcho) GetProcAddress(icmphandle,"IcmpSendEcho" );
		sm_pIcmpCloseHandle = (lpIcmpCloseHandle) GetProcAddress(icmphandle,"IcmpCloseHandle");

		if (sm_pIcmpCreateFile == NULL || sm_pIcmpSendEcho == NULL ||	sm_pIcmpCloseHandle == NULL)
		{
		  //TRACE(_T("Could not find ICMP functions in the ICMP DLL\n"));
			OutputDebugString("Could not find ICMP functions in the ICMP DLL\n");
		}

}

CPingI::~CPingI()
{
	if (icmphandle)
	{
		FreeLibrary(icmphandle);
		icmphandle = NULL;
	}
}

int CPingI::Ping(UINT nRetries, LPCSTR pstrHost, HWND hWnd)
{
	if(sm_pIcmpSendEcho==NULL)
		return 0;
	DWORD dwTimeout = 1000;
	UCHAR nPacketSize = 32;
	unsigned long	addr = inet_addr(pstrHost);
	if (addr == INADDR_NONE)
	{
		//Not a dotted address, then do a lookup of the name
		hostent* hp = gethostbyname(pstrHost);
		if (hp)
			memcpy(&addr, hp->h_addr, hp->h_length);
		else
		{
			//TRACE(_T("Could not resolve the host name %s\n"), pstrHost);
			return FALSE;
		}
	}
	//Create the ICMP handle
	HANDLE hIP = sm_pIcmpCreateFile();
	if (hIP == INVALID_HANDLE_VALUE)
	{
		//TRACE(_T("Could not get a valid ICMP handle\n"));
		return FALSE;
	}

  //Set up the option info structure
	IP_OPTION_INFORMATION OptionInfo;
	ZeroMemory(&OptionInfo, sizeof(IP_OPTION_INFORMATION));
	OptionInfo.Ttl = 128;

  //Set up the data which will be sent
  unsigned char pBuf[36];
  memset(pBuf, 'E', nPacketSize);

	//Do the actual Ping
  int nReplySize = sizeof(ICMP_ECHO_REPLY) + max(MIN_ICMP_PACKET_SIZE, nPacketSize);
  unsigned char pReply[100];
	ICMP_ECHO_REPLY* pEchoReply = (ICMP_ECHO_REPLY*) pReply;
  DWORD nRecvPackets = sm_pIcmpSendEcho(hIP, addr, pBuf, nPacketSize, &OptionInfo, pReply, nReplySize, dwTimeout);

  //Check we got the packet back
  BOOL bSuccess = (nRecvPackets == 1);

  //Check the IP status is OK (O is IP Success)
  if (bSuccess && (pEchoReply->Status != 0))
  {
    bSuccess = FALSE;
    SetLastError(pEchoReply->Status);
  }

  //Close the ICMP handle
	sm_pIcmpCloseHandle(hIP);
  
  return bSuccess;

}

⌨️ 快捷键说明

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