📄 ping.h
字号:
#ifndef __PING_H__
#define __PING_H__
#include "winsock2.h"
#include "ws2tcpip.h"
#include "ipstructs.h"
#include "timer.h"
#include "ipexport.h"
#define PING_MAX_TTL 50
#define PING_TIME_OUT 5000
#define MIN_ICMP_PACKET_SIZE 8 //minimum 8 byte icmp packet (just header)
#define MAX_ICMP_PACKET_SIZE 1024 //Maximum icmp packet size
typedef HANDLE (WINAPI *PFN_ICMPCREATEFILE)(VOID);
typedef DWORD (WINAPI *PFN_ICMPSENDECHO)(HANDLE hIcmpHandle, IPAddr nDestAddress,
LPVOID pRequestData, WORD wRequestSize,
IP_OPTION_INFORMATION *pstOptions,
LPVOID pReplyBuffer, DWORD dwReplySize, DWORD dwTimeout);
typedef BOOL (WINAPI *PFN_ICMPCLOSEHANDLE)(HANDLE hIcmpHandle);
/**
* PINGREPLY_S
* structure to hold the answer to a ping. see CPing for details.
*/
struct PINGREPLY_S
{
in_addr Address; //The IP address of the replier
unsigned long avgRTT; //Round Trip time in Milliseconds
unsigned long maxRTT;
unsigned long minRTT;
int nError;
};
/**
* CPing.
* class to perform a ping on other computers with ICMP-messages.
* requires Win2k or later because it uses raw-sockets.
*/
class CPing
{
public:
CPing(int nPacketSize = 32);
~CPing();
/*!
* performs a Ping on another host via ICMP protocoll.
*
* @param pszHostName : the name of the host. either as a "raw" ip-address or as a dns-name.
* @param pr : the PINGREPLY_S which holds the answer
* @param nPings : number of pings to perform on host
* @param nTTL : the time to live. default value is 50
* @param dwTimeout : the timeout in milliseconds. default is 5000 (5 seconds)
* @param nPacketSize : the size of the ping packet. default is 32
* @param nTOS : type of service. default is 0 for Ping
* @param bDontFragment : set to TRUE if you want prevent a fragmentation of the ping datapacket. default is FALSE
*
* @return int : returns the type of the ICMP-answer (e.g. 0 for echo-repy, 11 for ttl timeout) or -1 if failed
*/
void FillIcmpData(int nPacketSize);
int Ping(LPCTSTR szHostName, PINGREPLY_S *pr, int nPings = 1, UCHAR nTTL = PING_MAX_TTL, DWORD dwTimeout = PING_TIME_OUT, UCHAR nTOS = 0, BOOL bDontFragment = FALSE);
private:
BOOL Initialize(void);
BOOL ParseHostName(LPCTSTR szHostName, sockaddr_in *psockAddr);
#ifdef PING_USE_WINSOCK2
int BindLocalHost(void);
BOOL IsSocketReadible(SOCKET socket, DWORD dwTimeout, BOOL &bReadible);
USHORT GenerateIPChecksum(USHORT *pBuffer, int nSize);
int DecodeResponse(char *pBuf, int nBytes, sockaddr_in *paddrFrom);
#endif
protected:
char *m_pIcmpBuff;
char *m_pRecvBuff;
int m_nBufferSize;
int m_nRecvSize;
BOOL m_bInitSuccess;
#ifdef PING_USE_WINSOCK2
CMediaTimer m_obTimer;
SOCKET m_sockRaw;
#else
HMODULE m_hIcmpDll;
PFN_ICMPCREATEFILE m_pfnIcmpCreateFile;
PFN_ICMPSENDECHO m_pfnIcmpSendEcho;
PFN_ICMPCLOSEHANDLE m_pfnIcmpCloseHandle;
#endif
};
#endif //__PING_H__
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -