// IP Header -- RFC 791
typedef struct tagIPHDR
{
u_char VIHL; // Version and IHL
u_char TOS; // Type Of Service
short TotLen; // Total Length
short ID; // Identification
short FlagOff; // Flags and Fragment Offset
u_char TTL; // Time To Live
u_char Protocol; // Protocol
u_short Checksum; // Checksum
struct in_addr iaSrc; // Internet Address - Source
struct in_addr iaDst; // Internet Address - Destination
}IPHDR, *PIPHDR;
// SendEchoRequest()
// Fill in echo request header
// and send to destination
int __fastcall TPing::SendEchoRequest(SOCKET s,LPSOCKADDR_IN lpstToAddr)
{
static ECHOREQUEST echoReq;
static nId = 1;
static nSeq = 1;
int nRet;
// Fill in echo request
echoReq.icmpHdr.Type = ICMP_ECHOREQ;
echoReq.icmpHdr.Code = 0;
echoReq.icmpHdr.Checksum = 0;
echoReq.icmpHdr.ID = nId++;
echoReq.icmpHdr.Seq = nSeq++;
// Fill in some data to send
for(nRet = 0; nRet < REQ_DATASIZE; nRet++)
echoReq.cData[nRet] = ' '+nRet;
// Save tick count when sent
echoReq.dwTime = GetTickCount();
// Put data in packet and compute checksum
echoReq.icmpHdr.Checksum = in_cksum((u_short *)&echoReq, sizeof(ECHOREQUEST));
// WaitForEchoReply()
// Use select() to determine when
// data is waiting to be read
int __fastcall TPing::WaitForEchoReply(SOCKET s)
{
struct timeval Timeout;
fd_set readfds;
//
// Mike Muuss' in_cksum() function
// and his comments from the original
// ping program
//
// * Author -
// * Mike Muuss
// * U. S. Army Ballistic Research Laboratory
// * December, 1983
//
// I N _ C K S U M
//
// Checksum routine for Internet Protocol family headers (C Version)
//
//
u_short __fastcall TPing::in_cksum(u_short *addr, int len)
{
register int nleft = len;
register u_short *w = addr;
register u_short answer;
register int sum = 0;
//
// Our algorithm is simple, using a 32 bit accumulator (sum),
// we add sequential 16 bit words to it, and at the end, fold
// back all the carry bits from the top 16 bits into the lower
// 16 bits.
//
while( nleft > 1 )
{
sum += *w++;
nleft -= 2;
}
// mop up an odd byte, if necessary
if( nleft == 1 )
{
u_short u = 0;
*(u_char *)(&u) = *(u_char *)w ;
sum += u;
}
//
// add back carry outs from top 16 bits to low 16 bits
//
sum = (sum >> 16) + (sum & 0xffff); // add hi 16 to low 16
sum += (sum >> 16); // add carry
answer = ~sum; // truncate to 16 bits
return (answer);
}
//---------------------------------------------------------------------------
#pragma package(smart_init)