ICMP API实现ping

本文介绍了一个使用Windows下的ICMP API进行Ping操作的C语言程序示例。该程序通过动态加载ICMP.DLL并调用其导出函数实现发送ICMP回显请求并接收响应的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//			 Microsoft ICMP API
//		在Build之前,必须加入ws2_32.lib库文件
#include <windows.h>
#include <winsock.h>
#include <stdio.h>
#include <string.h>


typedef struct tagIPINFO
{
	u_char Ttl;				// Time To Live
	u_char Tos;				// Type Of Service
	u_char IPFlags;			// IP flags
	u_char OptSize;			// Size of options data
	u_char FAR *Options;	// Options data buffer
}IPINFO, *PIPINFO;


typedef struct tagICMPECHO
{
	u_long Source;			// Source address
	u_long Status;			// IP status
	u_long RTTime;			// Round trip time in milliseconds
	u_short DataSize;		// Reply data size
	u_short Reserved;		// Unknown
	void FAR *pData;		// Reply data buffer
	IPINFO	ipInfo;			// Reply options
}ICMPECHO, *PICMPECHO;




// ICMP.DLL Export Function Pointers
HANDLE (WINAPI *pIcmpCreateFile)(VOID);
BOOL (WINAPI *pIcmpCloseHandle)(HANDLE);
DWORD (WINAPI *pIcmpSendEcho)
	(HANDLE,DWORD,LPVOID,WORD,PIPINFO,LPVOID,DWORD,DWORD);


//
//
void main(int argc, char **argv)
{
	WSADATA wsaData;			// WSADATA
	ICMPECHO icmpEcho;			// ICMP Echo reply buffer
	HANDLE hndlIcmp;			// LoadLibrary() handle to ICMP.DLL
	HANDLE hndlFile;			// Handle for IcmpCreateFile()
    LPHOSTENT pHost;			// Pointer to host entry structure
    struct in_addr iaDest;		// Internet address structure
	DWORD *dwAddress;			// IP Address
	IPINFO ipInfo;				// IP Options structure
	int nRet;					// General use return code
	DWORD dwRet;				// DWORD return code
	int x;


	// Check arguments
	if (argc != 2)
	{
		fprintf(stderr,"\nSyntax: pingi HostNameOrIPAddress\n");
		return;
	}


	// Dynamically load the ICMP.DLL
	hndlIcmp = LoadLibrary("ICMP.DLL");
	if (hndlIcmp == NULL)
	{
		fprintf(stderr,"\nCould not load ICMP.DLL\n");
		return;
	}
	// Retrieve ICMP function pointers
	pIcmpCreateFile = (HANDLE (WINAPI *)(void))
		GetProcAddress(hndlIcmp,"IcmpCreateFile");
	pIcmpCloseHandle = (BOOL (WINAPI *)(HANDLE))
		GetProcAddress(hndlIcmp,"IcmpCloseHandle");
	pIcmpSendEcho = (DWORD (WINAPI *)
		(HANDLE,DWORD,LPVOID,WORD,PIPINFO,LPVOID,DWORD,DWORD))
		GetProcAddress(hndlIcmp,"IcmpSendEcho");
	// Check all the function pointers
	if (pIcmpCreateFile == NULL		|| 
		pIcmpCloseHandle == NULL	||
		pIcmpSendEcho == NULL)
	{
		fprintf(stderr,"\nError getting ICMP proc address\n");
		FreeLibrary(hndlIcmp);
		return;
	}


	// Init WinSock
	nRet = WSAStartup(0x0101, &wsaData );
    if (nRet)
    {
        fprintf(stderr,"\nWSAStartup() error: %d\n", nRet); 
        WSACleanup();
		FreeLibrary(hndlIcmp);
        return;
    }
    // Check WinSock version
    if (0x0101 != wsaData.wVersion)
    {
        fprintf(stderr,"\nWinSock version 1.1 not supported\n");
        WSACleanup();
		FreeLibrary(hndlIcmp);
        return;
    }


	// Lookup destination
    // Use inet_addr() to determine if we're dealing with a name
    // or an address
    iaDest.s_addr = inet_addr(argv[1]);
    if (iaDest.s_addr == INADDR_NONE)
        pHost = gethostbyname(argv[1]);
    else
        pHost = gethostbyaddr((const char *)&iaDest, 
                        sizeof(struct in_addr), AF_INET);
	if (pHost == NULL)
	{
		fprintf(stderr, "\n%s not found\n", argv[1]);
        WSACleanup();
		FreeLibrary(hndlIcmp);
		return;
	}


	// Tell the user what we're doing
	printf("\nPinging %s [%s]", pHost->h_name,
			inet_ntoa((*(LPIN_ADDR)pHost->h_addr_list[0])));


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


	// Get an ICMP echo request handle        
	hndlFile = pIcmpCreateFile();
	for (x = 0; x < 4; x++)
	{
		// Set some reasonable default values
		ipInfo.Ttl = 255;
		ipInfo.Tos = 0;
		ipInfo.IPFlags = 0;
		ipInfo.OptSize = 0;
		ipInfo.Options = NULL;
		//icmpEcho.ipInfo.Ttl = 256;
		// Reqest an ICMP echo
		dwRet = 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),
			5000);			// Time to wait in milliseconds
		// Print the results
		iaDest.s_addr = icmpEcho.Source;
		printf("\nReply from %s  Time=%ldms  TTL=%d",
				inet_ntoa(iaDest),
				icmpEcho.RTTime,
				icmpEcho.ipInfo.Ttl);
		if (icmpEcho.Status)
		{
			printf("\nError: icmpEcho.Status=%ld",
				icmpEcho.Status);
			break;
		}
	}
	printf("\n");
	// Close the echo request file handle
	pIcmpCloseHandle(hndlFile);
	FreeLibrary(hndlIcmp);
	WSACleanup();
}


支持wce下实现ping指令 // PingExampleDlg.cpp : implementation file // #include "stdafx.h" #include "PingExample.h" #include "PingExampleDlg.h" #include <icmpapi.h> #include <winsock.h> #include <ipExport.h> #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CPingExampleDlg dialog CPingExampleDlg::CPingExampleDlg(CWnd* pParent /*=NULL*/) : CDialog(CPingExampleDlg::IDD, pParent) { //{{AFX_DATA_INIT(CPingExampleDlg) m_ipAddr = _T(""); //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CPingExampleDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CPingExampleDlg) DDX_Control(pDX, IDC_LIST_RECV, m_lstRecv); DDX_Text(pDX, IDC_EDIT_IP, m_ipAddr); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CPingExampleDlg, CDialog) //{{AFX_MSG_MAP(CPingExampleDlg) ON_BN_CLICKED(IDC_BTNCLEAR, OnBtnclear) ON_BN_CLICKED(IDC_BTNPING, OnBtnping) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CPingExampleDlg message handlers BOOL CPingExampleDlg::OnInitDialog() { CDialog::OnInitDialog(); // Set the icon for this dialog. The framework does this automatically // when the application's main window is not a dialog SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon CenterWindow(GetDesktopWindow()); // center to the hpc screen // TODO: Add extra initialization here return TRUE; // return TRUE unless you set the focus to a control } /* *函数介绍:CE下Ping方法 *入口参数:cIPAddr : IP地址 pWnd : CWnd对象,用于指定窗体对象 *出口参数:pListBox : CListBox对象,用于显示Ping信息 *返回值:(无) */ void CEPing(CString cIPAddr,CListBox * pListBox,CWnd * pWnd) { WSAData wsaData; //初始化Socket动态链接库 if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0) { return ; } /*将IP地址转换成单字节*/ char szDestIPAddr[255]; ZeroMemory(szDestIPAddr,255); WideCharToMultiByte(CP_ACP,WC_COMPOSITECHECK ,cIPAddr.GetBuffer(cIPAddr.GetLength()),cIPAddr.GetLength() ,szDestIPAddr,cIPAddr.GetLength(),NULL,NULL); IPAddr ipAddr; //将目标字符串IP地址转换成IPAddr结构 ipAddr = inet_addr(szDestIPAddr); if (ipAddr == INADDR_NONE) { AfxMessageBox(_T("地址无效")); return ; } // 打开ping服务 HANDLE hIP = IcmpCreateFile(); if (hIP == INVALID_HANDLE_VALUE) { AfxMessageBox(_T("不能打开Ping服务")); return ; } // 构造ping数据包 char acPingBuffer[64]; memset(acPingBuffer, '*', sizeof(acPingBuffer)); PICMP_ECHO_REPLY pIpe = (PICMP_ECHO_REPLY)GlobalAlloc( GMEM_FIXED | GMEM_ZEROINIT, sizeof(ICMP_ECHO_REPLY) + sizeof(acPingBuffer)); if (pIpe == 0) { AfxMessageBox(_T("分配ping包缓冲区失败")); return ; } CString m_recv; // 发送ping数据包 m_recv = "ping "+cIPAddr+" with 64 Bytes"; pListBox->AddString(m_recv); for (int i=0;i<4;i++) { //发送ping服务包,等待接收时间为1秒 DWORD dwStatus = IcmpSendEcho(hIP,ipAddr, acPingBuffer, sizeof(acPingBuffer), NULL, pIpe, sizeof(ICMP_ECHO_REPLY) + sizeof(acPingBuffer), 1000); //当dwStatus不等于0,代表接收到回应 if (dwStatus != 0) { m_recv.Format(_T("Reply From %d.%d.%d.%d :bytes=%d time=%d TTL") ,int(LOBYTE(LOWORD(pIpe->Address))) ,int(HIBYTE(LOWORD(pIpe->Address))) ,int(LOBYTE(HIWORD(pIpe->Address))) ,int(HIBYTE(HIWORD(pIpe->Address))) ,int(pIpe->DataSize) ,int(pIpe->RoundTripTime) ,int(pIpe->Options.Ttl)); pListBox->AddString(m_recv); Sleep(500); } else { pListBox->AddString(_T("Error obtaining info from ping packet.")); } pWnd->UpdateWindow(); } //释放已分配的内存 GlobalFree(pIpe); //关闭Ping服务 IcmpCloseHandle(hIP); //释放Socket资源 WSACleanup(); } void CPingExampleDlg::OnBtnclear() { int nCount = m_lstRecv.GetCount(); for (int i=0;i < nCount;i++) { m_lstRecv.DeleteString(0); } } void CPingExampleDlg::OnBtnping() { UpdateData(TRUE); CEPing(m_ipAddr,&m_lstRecv,this); UpdateWindow(); }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值