LPSTR lpszAscii = (LPTSTR) pszHostName;
//Convert from dotted notation if required
unsigned long addr = inet_addr(lpszAscii);
if (addr == INADDR_NONE) {
//Not a dotted address, then do a lookup of the name
hostent* hp = gethostbyname(lpszAscii);
if (hp)
memcpy(&addr, hp->h_addr, hp->h_length);
else {
char msg[64];
sprintf(msg,"无法解析主机名:%s",pszHostName);
::MessageBox(NULL,msg,"ERROR",MB_OK);
return FALSE;
}
}
//Create the ICMP handle
HANDLE hIP = sm_pIcmpCreateFile();
if (hIP == INVALID_HANDLE_VALUE) {
::MessageBox(NULL,"无效的'ICMP'句柄","ERROR",MB_OK);
return FALSE;
}
//Set up the option info structure
IP_OPTION_INFORMATION OptionInfo;
ZeroMemory(&OptionInfo, sizeof(IP_OPTION_INFORMATION));
OptionInfo.Ttl = nTTL;
//Set up the data which will be sent
unsigned char* pBuf = new unsigned char[nPacketSize];
memset(pBuf, 'E', nPacketSize);
//Do the actual Ping
int nReplySize = sizeof(ICMP_ECHO_REPLY) + max(MIN_ICMP_PACKET_SIZE, nPacketSize);
unsigned char* pReply = new unsigned char[nReplySize];
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);
}
//Check we got the same amount of data back as we sent
if (bSuccess) {
bSuccess = (pEchoReply->DataSize == nPacketSize);
if (!bSuccess)
SetLastError(ERROR_UNEXP_NET_ERR);
}
//Check the data we got back is what was sent
if (bSuccess) {
char* pReplyData = (char*) pEchoReply->Data;
for (int i=0; i<nPacketSize && bSuccess; i++)
bSuccess = (pReplyData[i] == 'E');
if (!bSuccess)
SetLastError(ERROR_UNEXP_NET_ERR);
}
//Close the ICMP handle
sm_pIcmpCloseHandle(hIP);
if (bSuccess) {
//Ping was successful, copy over the pertinent info
//into the return structure
pr.Address.S_un.S_addr = pEchoReply->Address;
pr.RTT = pEchoReply->RoundTripTime;
}
//Free up the memory we allocated
delete [] pBuf;
delete [] pReply;
//These defines & structure definitions are taken from the "ipexport.h" and
//"icmpapi.h" header files as provided with the Platform SDK and
//are used internally by the CPing class. Including them here allows
//you to compile the CPing code without the need to have the full
//Platform SDK installed.
#include <winsock.h>
typedef struct tagIP_OPTION_INFORMATION
{
unsigned char Ttl; // Time To Live
unsigned char Tos; // Type Of Service
unsigned char Flags; // IP header flags
unsigned char OptionsSize; // Size in bytes of options data
unsigned char FAR *OptionsData; // Pointer to options data
} IP_OPTION_INFORMATION;
typedef struct tagICMP_ECHO_REPLY
{
IPAddr Address; // Replying address
unsigned long Status; // Reply IP_STATUS
unsigned long RoundTripTime; // RTT in milliseconds
unsigned short DataSize; // Reply data size in bytes
unsigned short Reserved; // Reserved for system use
void FAR *Data; // Pointer to the reply data
IP_OPTION_INFORMATION Options; // Reply options
} ICMP_ECHO_REPLY;
// IP header
typedef struct tagIP_HEADER
{
unsigned int h_len:4; // length of the header
unsigned int version:4; // Version of IP
unsigned char tos; // Type of service
unsigned short total_len; // total length of the packet
unsigned short ident; // unique identifier
unsigned short frag_and_flags; // flags
unsigned char ttl;
unsigned char proto; // protocol (TCP, UDP etc)
unsigned short checksum; // IP checksum
unsigned int sourceIP;
unsigned int destIP;
} IP_HEADER;
typedef IP_HEADER FAR* LPIP_HEADER;
// ICMP header
typedef struct tagICMP_HEADER
{
BYTE i_type;
BYTE i_code; /* type sub code */
USHORT i_cksum;
USHORT i_id;
USHORT i_seq;
/* This is not the std header, but we reserve space for time */
ULONG timestamp;
} ICMP_HEADER;
typedef ICMP_HEADER FAR* LPICMP_HEADER;
void FillIcmpData(LPICMP_HEADER pIcmp, int nData);
BOOL DecodeResponse(char* pBuf, int nBytes, sockaddr_in* from);
USHORT GenerateIPChecksum(USHORT* pBuffer, int nSize);
//Internal class which is used to ensure that the ICMP
//handle and winsock stack is closed upon exit
class _CPING
{
public:
_CPING();
~_CPING();
protected:
HINSTANCE sm_hIcmp;