Theory: Ping is a computer network administration utility used to test the reachability of a host on an Internet Protocol (IP) network and to measure the round-trip time for messages sent from the originating host to a destination computer. Ping operates by sending Internet Control Message Protocol (ICMP) echo request packets to the target host and waiting for an ICMP response. In the process it measures the time from transmission to reception (round-trip time) and records any packet loss. he results of the test are printed in the form of a statistical summary of the response packets recei!ed" including the minimum" ma#imum" and the mean round-trip times" and sometimes the standard de!iation of the mean. $epending on actual implementation" the ping utility may be e#ecuted with !arious command-line switches to enable special operational modes. %or e.g." options include specifying the packet si&e of the probe" automatic repeated operation for sending a specified count of probes" and time stamping. %ig' PI() command in command prompt. MESSAGE FOMAT ICMP packet IP Datagram Bits 07 Bits 815 Bits 1623 Bits 2431 IP Header (20 !tes" Version/IHL Type of service Length Identification flags and offset Time To Live (TTL) Protocol Checksum ource IP address !estination IP address ICMP Header (8 !tes" Type of message Code Checksum Header !ata ICMP Pa!#$ad (optional" Payload !ata )eneric composition of an ICMP *+-byte packet IP ,eader (in blue)' Protocol set to - (ICMP) and ype of .er!ice set to /. ICMP ,eader (in red)' ype of ICMP message (0 bits) Code (0 bits) Checksum (-1 bits)" calculated with the ICMP part of the packet (the IP header is not used). It is the -1-bit one2s complement of the one2s complement sum of the ICMP message starting with the ype field ,eader $ata (*+ bits) field" which in this case (ICMP echo request and replies)" will be composed of identifier (-1 bits) and sequence number (-1 bits). ICMP Payload Payload for the different kind of answers3 can be an arbitrary length" left to implementation detail. ,owe!er" the packet including IP and ICMP headers must be less than the ma#imum transmission unit of the network or risk being fragmented. $ata ransportation Program and Output: 4include 5fcntl.h6 4include 5errno.h6 4include 5sys7socket.h6 4include 5resol!.h6 4include 5netdb.h6 4include 5netinet7in.h6 4include 5netinet7ip8icmp.h6 4define P9C:;.I<; 1= struct packet > struct icmphdr hdr3 char msg?P9C:;.I<;-si&eof(struct icmphdr)@3 A3 int pidB--3 struct protoent CprotoB(DEE3 7C--- checksum - standard -s complement checksum ---C7 unsigned short checksum(!oid Cb" int len) > unsigned short Cbuf B b3 unsigned int sumB/3 unsigned short result3 for ( sum B /3 len 6 -3 len -B + ) sum FB CbufFF3 if ( len BB - ) sum FB C(unsigned charC)buf3 sum B (sum 66 -1) F (sum G /#%%%%)3 sum FB (sum 66 -1)3 result B Hsum3 return result3 A 7C--- display - present echo info ---C7 !oid display(!oid Cbuf" int bytes) > int i3 struct iphdr Cip B buf3 struct icmphdr Cicmp B bufFip-6ihlC=3 printf(I----------------JnI)3 printf(IJnI)3 printf(IIP!Kd' hdr-si&eBKd pkt-si&eBKd protocolBKd EBKd srcBKs I" ip-6!ersion" ip-6ihlC=" ntohs(ip-6tot8len)" ip-6protocol" ip-6ttl" inet8ntoa(ip-6saddr))3 printf(IdstBKsJnI" inet8ntoa(ip-6daddr))3 if ( icmp-6un.echo.id BB pid ) > printf(IICMP' type?Kd7Kd@ checksum?Kd@ id?Kd@ seq?Kd@JnI" icmp-6type" icmp-6code" ntohs(icmp-6checksum)" icmp-6un.echo.id" icmp-6un.echo.sequence)3 A A 7C--- listener - separate process to listen for and collect messages--C7 !oid listener(!oid) > int sd3 struct sockaddr8in addr3 unsigned char buf?-/+=@3 sd B socket(P%8I(;" .LC:8M9N" proto-6p8proto)3 if ( sd 5 / ) > perror(IsocketI)3 e#it(/)3 A for (33) > int bytes" lenBsi&eof(addr)3 b&ero(buf" si&eof(buf))3 bytes B rec!from(sd" buf" si&eof(buf)" /" (struct sockaddrC)Gaddr" Glen)3 if ( bytes 6 / ) display(buf" bytes)3 else perror(Irec!fromI)3 A e#it(/)3 A 7C--- ping - Create message and send it. ---C7 !oid ping(struct sockaddr8in Caddr) > const int !alB+OO3 int i" sd" cntB-3 struct packet pckt3 struct sockaddr8in r8addr3 sd B socket(P%8I(;" .LC:8M9N" proto-6p8proto)3 if ( sd 5 / ) > perror(IsocketI)3 return3 A if ( setsockopt(sd" .LE8IP" IP8E" G!al" si&eof(!al)) PB /) perror(I.et E optionI)3 if ( fcntl(sd" %8.;%E" L8(L(QELC:) PB / ) perror(IMequest nonblocking I7LI)3 for (33) > int lenBsi&eof(r8addr)3 printf(IMsg 4KdJnI" cnt)3 if ( rec!from(sd" Gpckt" si&eof(pckt)" /" (struct sockaddrC)Gr8addr" Glen) 6 / ) 77printf(ICCC)ot messagePCCCJnI)3 b&ero(Gpckt" si&eof(pckt))3 pckt.hdr.type B ICMP8;C,L3 pckt.hdr.un.echo.id B pid3 for ( i B /3 i 5 si&eof(pckt.msg)--3 iFF ) pckt.msg?i@ B iF2/23 pckt.msg?i@ B /3 pckt.hdr.un.echo.sequence B cntFF3 pckt.hdr.checksum B checksum(Gpckt" si&eof(pckt))3 77printf(ICCCCI")3 if ( sendto(sd" Gpckt" si&eof(pckt)" /" (struct sockaddrC)addr" si&eof(Caddr)) 5B / ) perror(IsendtoI)3 sleep(-)3 A A 7C--- main - look up host and start ping processes. ---C7 int main(int count" char Cstrings?@) > struct hostent Chname3 struct sockaddr8in addr3 if ( count PB + ) > printf(Iusage' Ks 5addr6JnI" strings?/@)3 e#it(/)3 A if ( count 6 - ) > pid B getpid()3 proto B getprotobyname(IICMPI)3 hname B gethostbyname(strings?-@)3 b&ero(Gaddr" si&eof(addr))3 addr.sin8family B hname-6h8addrtype3 addr.sin8port B /3 addr.sin8addr.s8addr B C(longC)hname-6h8addr3 if ( fork() BB / ) listener()3 else ping(Gaddr)3 wait(/)3 A else printf(Iusage' myping 5hostname6JnI)3 return /3 A Conclusion: hus we successfully implemented the PI() command using Ra!a.