<<

Department of Master of. Computer Applications Submitted by, 18CA910 . Ping is a basic Internet program that allows a user to verify that a particular IP address exists and can accept requests. Ping is used diagnostically to ensure that a computer the user is trying to reach is actually operating.

. Ping can also refer to the process of sending a message to all the members of a mailing list requesting an ACK (acknowledgment code). This is done before sending e- in order to confirm that all of the addresses are reachable. . The Internet Ping bounces a small packet off a domain or IP address to network communications, and then determines how long the packet took to the round trip. The Ping command is one of the commonly used utilities on the Internet by both people and automated programs for conducting the most basic network test: can your computer reach another computer on the network . The Internet Ping program works much like a sonar -location, sending a small packet of information containing an ICMP ECHO_REQUEST to a specified computer, then sends an ECHO_REPLY packet in return. The IP address 127.0.0.1 is set by convention to always indicate your own computer. Therefore, a ping to that address will always ping yourself and the delay should be very short. This provides the most basic test of your local communications. . Access : You can use Ping to see if you can reach another computer. If you cannot ping a site all, but you can ping other sites, then it is a pretty good sign that your Internet network is working and that site is down. On the other hand, if you cannot ping any site, then likely your entire network connection is down due to a bad connection. . & distance : You can use the Ping command to determine how long it takes to bounce a packet off of another site, which tells you its Internet distance in network terms. .

Option Example Definition

ping -c count ping -c 10 Specify the number of echo requests to send.

ping -d ping -d Set the SO_DEBUG option.

Flood ping. Sends another echo request immediately after receiving a reply to the last one. ping -f ping -f Only the super-user can use this option. .

ping host ping 121.4.3.2 Specify the host name (or IP address) of computer to ping

ping -i ping -i 2 Wait time. The number of seconds to wait between each ping

ping -l preload ping -l 4 Sends "preload" packets one after another.

ping -n ping -n Numeric output, without host to symbolic name lookup.

ping -p pattern ping -p ff00 Ping Pattern. The example sends two bytes, one filled with ones, and one with zeros. . Two of the most practical connectivity test commands are ping and . These two commands are good mechanisms for network troubleshooting, and both of these use ICMP. . There are four easy steps defined for troubleshooting IP addressing: . Ping 127.0.0.1 . Ping localhost IP address . Ping default gateway . Ping the remote server . Ping is a computer program that determines if a host is up or not. Ping basically consists of a source sending an ICMP "echo request" to a target, followed by the target replying with an ICMP "echo response" - assuming the target is up . the ICMP echo request and reply comes up in the capture when a Ping command is performed. The and Code fields represent the different kinds of action and error results. A brief description of each is shown in the following table:

Type Code description

0 0 echo reply (ping)

3 0 dest network unreachable

3 1 dest host unreachable

3 3 dest port unreachable

3 6 dest network unknown

3 7 dest host unknown

4 0 source quench (congestion control - not used)

8 0 echo request (ping)

9 0 route advertisement

10 0 router discovery

11 0 TTL expired

12 0 bad IP header

. #include

. #include

. #include

. #include

. #include

. #include

. #include

. #include

. #include

. #include

. #include

. #include

. #define MAXWAIT 10 /* max time to wait for response, sec. */

. #define MAXPACKET 4096 /* max packet size */

. #define VERBOSE 1 /* verbose flag */ #define QUIET 2 /* quiet flag */

. #define FLOOD 4 /* floodping flag */

. #ifndef MAXHOSTNAMELEN #define MAXHOSTNAMELEN 64

. #endif u_char packet[MAXPACKET]; int i, pingflags, options; extern int errno; int s; /* Socket descriptor */ struct hostent *hp; /* Pointer to host */ struct timezone tz; /* leftover */ struct sockaddr whereto;/* to ping */ int datalen; /* How much data */ char usage[] ="Usage: ping [-dfqrv] host [packetsize [count [preload]]]\n"; . char *hostname; . char hnamebuf[MAXHOSTNAMELEN]; . int npackets; . int preload = 0; /* number of packets to "preload" */ int ntransmitted = 0; /* sequence . # for outbound packets = #sent */ int ident; . int nreceived = 0; /* # of packets we got back */ . int timing = 0; . Int tmin = 999999999; . Int tmax = 0; . intt tsum = 0; /* of all times, for doing average */ int finish(), catcher(); char *inet_ntoa(); . main(argc, argv) char *argv[];

. { struct sockaddr_in from;

. char **av = argv;

. struct sockaddr_in *to = (struct sockaddr_in *) &whereto;

. int on = 1;

struct protoent *proto; argc--, av++; while (argc > 0 && *av[0] == '-’)

{ while (*++av[0]) switch (*av[0])

{ case 'd': options |= SO_DEBUG; break; case 'r': options |= SO_DONTROUTE; break; case 'v': pingflags |= VERBOSE; break; case 'q': pingflags |= QUIET; break; case 'f': pingflags |= FLOOD; break; } argc--, av++; } if(argc < 1 || argc > 4) { printf(usage); (1); } . bzero((char *)&whereto, sizeof(struct sockaddr) ); . to->sin_family = AF_INET; . to->sin_addr.s_addr = inet_addr(av[0]); . if(to->sin_addr.s_addr != (unsigned)-1) { strcpy(hnamebuf, av[0]); . hostname = hnamebuf; } . else { hp = gethostbyname(av[0]); . if (hp) { to->sin_family = hp->h_addrtype; . bcopy(hp->h_addr, (caddr_t)&to->sin_addr, hp->h_length); . hostname = hp->h_name; } . else { printf("%s: unknown host %s\n", argv[0], av[0]); exit(1); } } . if( argc >= 2 ) datalen = atoi( av[1] );

. else datalen = 64-8;

. if (datalen > MAXPACKET) { fprintf(stderr, "ping: packet size too large\n");

. exit(1); }

. if (datalen >= sizeof(struct timeval)) /* can we time 'em? */ timing = 1;

. if (argc >= 3) npackets = atoi(av[2]);

. if (argc == 4) preload = atoi(av[3]);

. ident = getpid() & 0xFFFF;

. if ((proto = getprotobyname("icmp")) == NULL) { fprintf(stderr, "icmp: unknown protocol\n");

. exit(10); }

. if ((s = socket(AF_INET, SOCK_RAW, proto->p_proto)) < 0) { perror("ping: socket");

. exit(5); }

. if (options & SO_DEBUG)

. { if(pingflags & VERBOSE) printf("...debug on.\n");

. setsockopt(s, SOL_SOCKET, SO_DEBUG, &on, sizeof(on)); } . if (options & SO_DONTROUTE) . { if(pingflags & VERBOSE) . printf("...no routing.\n"); . setsockopt(s, SOL_SOCKET, SO_DONTROUTE, &on, sizeof(on)); } if(to->sin_family == AF_INET) . { . printf("PING %s (%s): %d data bytes\n", hostname, inet_ntoa(to->sin_addr), datalen); /* DFM */ } . else { printf("PING %s: %d data bytes\n", hostname, datalen ); } . setlinebuf( stdout ); . signal( SIGINT, finish ); . signal(SIGALRM, catcher); /* fire off them quickies */ . for(i=0; i < preload; i++) . pinger(); . if(!(pingflags & FLOOD)) catcher(); /* start things going */ . for (;;) . { int len = sizeof (packet); . int fromlen = sizeof (from); . int cc; struct timeval timeout; . int fdmask = 1 << s; . timeout.tv_sec = 0; . timeout.tv_usec = 10000; . if(pingflags & FLOOD) { pinger(); . if( select(32, &fdmask, 0, 0, &timeout) == 0) continue; } . if ( (cc=recvfrom(s, packet, len, 0, &from, &fromlen)) < 0) . { if( errno == EINTR ) continue; . perror("ping: recvfrom"); continue; } . pr_pack( packet, cc, &from ); . if (npackets && nreceived >= npackets) finish(); } . catcher() { int waittime;

. pinger(); if (npackets == 0 || ntransmitted < npackets) alarm(1);

. Else { if (nreceived) { waittime = 2 * tmax / 1000;

. Else(waittime == 0) waittime = 1; }

. Elsee waittime = MAXWAIT;

. Elseal(SIGALRM, finish);

. elsem(waittime); } }

. pinger() { static u_char outpack[MAXPACKET];

. register struct icmp *icp = (struct icmp *) outpack;

. int i, cc; register struct timeval *tp = (struct timeval *) &outpack[8];

. register u_char *datap = &outpack[8+sizeof(struct timeval)];

. icp->icmp_type = ICMP_ECHO;

. icp->icmp_code = 0; icp->icmp_cksum = 0;

. icp->icmp_seq = ntransmitted++;

. icp->icmp_id = ident; /* ID */ cc = datalen+8; /* skips ICMP portion */ if (timing) gettimeofday( tp, &tz ); for( i=8; iicmp_cksum = in_cksum( icp, cc ); /* . cc = sendto(s, msg, len, flags, to, tolen) */ . i = sendto( s, outpack, cc, 0, &whereto, sizeof(struct sockaddr) ); . if( i < 0 || i != cc ) . { if( i<0 ) perror("sendto"); . printf("ping: wrote %s %d chars, ret=%d\n", hostname, cc, i ); fflush(stdout); } if(pingflags == FLOOD) . { putchar('.'); fflush(stdout); } . }