133675Sbostic /*
261530Sbostic * Copyright (c) 1989, 1993
361530Sbostic * The Regents of the University of California. All rights reserved.
438644Sbostic *
538644Sbostic * This code is derived from software contributed to Berkeley by
638644Sbostic * Mike Muuss.
738644Sbostic *
842706Sbostic * %sccs.include.redist.c%
938644Sbostic */
1038644Sbostic
1138644Sbostic #ifndef lint
1261530Sbostic static char copyright[] =
1361530Sbostic "@(#) Copyright (c) 1989, 1993\n\
1461530Sbostic The Regents of the University of California. All rights reserved.\n";
1538644Sbostic #endif /* not lint */
1638644Sbostic
1738644Sbostic #ifndef lint
18*69036Sbostic static char sccsid[] = "@(#)ping.c 8.3 (Berkeley) 04/28/95";
1938644Sbostic #endif /* not lint */
2038644Sbostic
2138644Sbostic /*
2219057Skarels * P I N G . C
2319057Skarels *
2419057Skarels * Using the InterNet Control Message Protocol (ICMP) "ECHO" facility,
2519057Skarels * measure round-trip-delays and packet loss across network paths.
2619057Skarels *
2719057Skarels * Author -
2819057Skarels * Mike Muuss
2919057Skarels * U. S. Army Ballistic Research Laboratory
3019057Skarels * December, 1983
3119057Skarels *
3219057Skarels * Status -
3319057Skarels * Public Domain. Distribution Unlimited.
3419057Skarels * Bugs -
3519057Skarels * More statistics could always be gathered.
3619057Skarels * This program has to run SUID to ROOT to access the ICMP socket.
3719057Skarels */
3819057Skarels
3919057Skarels #include <sys/param.h>
4019057Skarels #include <sys/socket.h>
4119057Skarels #include <sys/file.h>
4238644Sbostic #include <sys/time.h>
4338644Sbostic #include <sys/signal.h>
4419057Skarels
4519057Skarels #include <netinet/in_systm.h>
4619057Skarels #include <netinet/in.h>
4719057Skarels #include <netinet/ip.h>
4819057Skarels #include <netinet/ip_icmp.h>
4938643Sbostic #include <netinet/ip_var.h>
5038644Sbostic #include <netdb.h>
5138644Sbostic #include <unistd.h>
5238644Sbostic #include <stdio.h>
5338643Sbostic #include <ctype.h>
5438644Sbostic #include <errno.h>
5542043Sbostic #include <string.h>
5619057Skarels
5738644Sbostic #define DEFDATALEN (64 - 8) /* default data length */
5838644Sbostic #define MAXIPLEN 60
5938644Sbostic #define MAXICMPLEN 76
6038644Sbostic #define MAXPACKET (65536 - 60 - 8)/* max packet size */
6138644Sbostic #define MAXWAIT 10 /* max seconds to wait for response */
6238644Sbostic #define NROUTES 9 /* number of record route slots */
6319057Skarels
6438644Sbostic #define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */
6538644Sbostic #define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */
6638644Sbostic #define SET(bit) (A(bit) |= B(bit))
6738644Sbostic #define CLR(bit) (A(bit) &= (~B(bit)))
6838644Sbostic #define TST(bit) (A(bit) & B(bit))
6938643Sbostic
7038644Sbostic /* various options */
7138644Sbostic int options;
7238644Sbostic #define F_FLOOD 0x001
7338644Sbostic #define F_INTERVAL 0x002
7438644Sbostic #define F_NUMERIC 0x004
7538644Sbostic #define F_PINGFILLED 0x008
7638644Sbostic #define F_QUIET 0x010
7738644Sbostic #define F_RROUTE 0x020
7838644Sbostic #define F_SO_DEBUG 0x040
7938644Sbostic #define F_SO_DONTROUTE 0x080
8038644Sbostic #define F_VERBOSE 0x100
8138643Sbostic
8238644Sbostic /*
8338644Sbostic * MAX_DUP_CHK is the number of bits in received table, i.e. the maximum
8438644Sbostic * number of received sequence numbers we can keep track of. Change 128
8538644Sbostic * to 8192 for complete accuracy...
8638644Sbostic */
8738644Sbostic #define MAX_DUP_CHK (8 * 128)
8838644Sbostic int mx_dup_ck = MAX_DUP_CHK;
8938644Sbostic char rcvd_tbl[MAX_DUP_CHK / 8];
9038643Sbostic
9138644Sbostic struct sockaddr whereto; /* who to ping */
9238644Sbostic int datalen = DEFDATALEN;
9338644Sbostic int s; /* socket file descriptor */
9438644Sbostic u_char outpack[MAXPACKET];
9538644Sbostic char BSPACE = '\b'; /* characters written for flood */
9638644Sbostic char DOT = '.';
9719057Skarels char *hostname;
9838644Sbostic int ident; /* process id to identify our packets */
9919057Skarels
10038644Sbostic /* counters */
10138644Sbostic long npackets; /* max packets to transmit */
10238644Sbostic long nreceived; /* # of packets we got back */
10338644Sbostic long nrepeats; /* number of duplicates */
10438644Sbostic long ntransmitted; /* sequence # for outbound packets = #sent */
10538644Sbostic int interval = 1; /* interval between packets */
10638643Sbostic
10738644Sbostic /* timing */
10838644Sbostic int timing; /* flag to do timing */
10953668Sbostic double tmin = 999999999.0; /* minimum round trip time */
11053668Sbostic double tmax = 0.0; /* maximum round trip time */
11153668Sbostic double tsum = 0.0; /* sum of all times, for doing average */
11219057Skarels
11346719Sbostic char *pr_addr();
11446719Sbostic void catcher(), finish();
11519057Skarels
main(argc,argv)11619057Skarels main(argc, argv)
11738644Sbostic int argc;
11838644Sbostic char **argv;
11919057Skarels {
12038644Sbostic extern int errno, optind;
12138644Sbostic extern char *optarg;
12238644Sbostic struct timeval timeout;
12338644Sbostic struct hostent *hp;
12438644Sbostic struct sockaddr_in *to;
12524199Skarels struct protoent *proto;
12638644Sbostic register int i;
12738644Sbostic int ch, fdmask, hold, packlen, preload;
12838644Sbostic u_char *datap, *packet;
12938644Sbostic char *target, hnamebuf[MAXHOSTNAMELEN], *malloc();
13038644Sbostic #ifdef IP_OPTIONS
13138644Sbostic char rspace[3 + 4 * NROUTES + 1]; /* record route space */
13238644Sbostic #endif
13319057Skarels
13438644Sbostic preload = 0;
13538644Sbostic datap = &outpack[8 + sizeof(struct timeval)];
13638644Sbostic while ((ch = getopt(argc, argv, "Rc:dfh:i:l:np:qrs:v")) != EOF)
13738644Sbostic switch(ch) {
13838644Sbostic case 'c':
13938644Sbostic npackets = atoi(optarg);
14038644Sbostic if (npackets <= 0) {
14138644Sbostic (void)fprintf(stderr,
14238644Sbostic "ping: bad number of packets to transmit.\n");
14338644Sbostic exit(1);
14438644Sbostic }
14538644Sbostic break;
14638644Sbostic case 'd':
14738644Sbostic options |= F_SO_DEBUG;
14838644Sbostic break;
14938644Sbostic case 'f':
15038644Sbostic if (getuid()) {
15138644Sbostic (void)fprintf(stderr,
15249681Sbostic "ping: %s\n", strerror(EPERM));
15338644Sbostic exit(1);
15438644Sbostic }
15538644Sbostic options |= F_FLOOD;
15638644Sbostic setbuf(stdout, (char *)NULL);
15738644Sbostic break;
15838644Sbostic case 'i': /* wait between sending packets */
15938644Sbostic interval = atoi(optarg);
16038644Sbostic if (interval <= 0) {
16138644Sbostic (void)fprintf(stderr,
16238644Sbostic "ping: bad timing interval.\n");
16338644Sbostic exit(1);
16438644Sbostic }
16538644Sbostic options |= F_INTERVAL;
16638644Sbostic break;
16738644Sbostic case 'l':
16838644Sbostic preload = atoi(optarg);
16938644Sbostic if (preload < 0) {
17038644Sbostic (void)fprintf(stderr,
17138644Sbostic "ping: bad preload value.\n");
17238644Sbostic exit(1);
17338644Sbostic }
17438644Sbostic break;
17538644Sbostic case 'n':
17638644Sbostic options |= F_NUMERIC;
17738644Sbostic break;
17838644Sbostic case 'p': /* fill buffer with user pattern */
17938644Sbostic options |= F_PINGFILLED;
18038644Sbostic fill((char *)datap, optarg);
18138643Sbostic break;
18238644Sbostic case 'q':
18338644Sbostic options |= F_QUIET;
18438644Sbostic break;
18538644Sbostic case 'R':
18638644Sbostic options |= F_RROUTE;
18738644Sbostic break;
18838644Sbostic case 'r':
18938644Sbostic options |= F_SO_DONTROUTE;
19038644Sbostic break;
19138644Sbostic case 's': /* size of packet to send */
19238644Sbostic datalen = atoi(optarg);
19338644Sbostic if (datalen > MAXPACKET) {
19438644Sbostic (void)fprintf(stderr,
19538644Sbostic "ping: packet size too large.\n");
19638643Sbostic exit(1);
19738644Sbostic }
19838644Sbostic if (datalen <= 0) {
19938644Sbostic (void)fprintf(stderr,
20038644Sbostic "ping: illegal packet size.\n");
20138644Sbostic exit(1);
20238644Sbostic }
20338644Sbostic break;
20438644Sbostic case 'v':
20538644Sbostic options |= F_VERBOSE;
20638644Sbostic break;
20738644Sbostic default:
20838644Sbostic usage();
20919064Skarels }
21038644Sbostic argc -= optind;
21138644Sbostic argv += optind;
21238643Sbostic
21338644Sbostic if (argc != 1)
21438644Sbostic usage();
21538644Sbostic target = *argv;
21619057Skarels
21768999Sbostic memset(&whereto, 0, sizeof(struct sockaddr));
21838644Sbostic to = (struct sockaddr_in *)&whereto;
21924199Skarels to->sin_family = AF_INET;
22038644Sbostic to->sin_addr.s_addr = inet_addr(target);
22138644Sbostic if (to->sin_addr.s_addr != (u_int)-1)
22238644Sbostic hostname = target;
22338644Sbostic else {
22438644Sbostic hp = gethostbyname(target);
22538644Sbostic if (!hp) {
22638644Sbostic (void)fprintf(stderr,
22738644Sbostic "ping: unknown host %s\n", target);
22824199Skarels exit(1);
22919057Skarels }
23038644Sbostic to->sin_family = hp->h_addrtype;
23168999Sbostic memmove(&to->sin_addr, hp->h_addr, hp->h_length);
23238644Sbostic (void)strncpy(hnamebuf, hp->h_name, sizeof(hnamebuf) - 1);
23338644Sbostic hostname = hnamebuf;
23419057Skarels }
23519057Skarels
23638644Sbostic if (options & F_FLOOD && options & F_INTERVAL) {
23738644Sbostic (void)fprintf(stderr,
23838644Sbostic "ping: -f and -i incompatible options.\n");
23938643Sbostic exit(1);
24038643Sbostic }
24138643Sbostic
24238644Sbostic if (datalen >= sizeof(struct timeval)) /* can we time transfer */
24319064Skarels timing = 1;
24438644Sbostic packlen = datalen + MAXIPLEN + MAXICMPLEN;
24538644Sbostic if (!(packet = (u_char *)malloc((u_int)packlen))) {
24638644Sbostic (void)fprintf(stderr, "ping: out of memory.\n");
24738643Sbostic exit(1);
24838643Sbostic }
24938644Sbostic if (!(options & F_PINGFILLED))
25038644Sbostic for (i = 8; i < datalen; ++i)
25138644Sbostic *datap++ = i;
25219057Skarels
25319057Skarels ident = getpid() & 0xFFFF;
25419057Skarels
25538644Sbostic if (!(proto = getprotobyname("icmp"))) {
25638644Sbostic (void)fprintf(stderr, "ping: unknown protocol icmp.\n");
25738644Sbostic exit(1);
25824199Skarels }
25924199Skarels if ((s = socket(AF_INET, SOCK_RAW, proto->p_proto)) < 0) {
26019057Skarels perror("ping: socket");
26138644Sbostic exit(1);
26219057Skarels }
26338644Sbostic hold = 1;
26438644Sbostic if (options & F_SO_DEBUG)
26538644Sbostic (void)setsockopt(s, SOL_SOCKET, SO_DEBUG, (char *)&hold,
26638644Sbostic sizeof(hold));
26738644Sbostic if (options & F_SO_DONTROUTE)
26838644Sbostic (void)setsockopt(s, SOL_SOCKET, SO_DONTROUTE, (char *)&hold,
26938644Sbostic sizeof(hold));
27038644Sbostic
27138644Sbostic /* record route option */
27238644Sbostic if (options & F_RROUTE) {
27338643Sbostic #ifdef IP_OPTIONS
27438643Sbostic rspace[IPOPT_OPTVAL] = IPOPT_RR;
27538643Sbostic rspace[IPOPT_OLEN] = sizeof(rspace)-1;
27638643Sbostic rspace[IPOPT_OFFSET] = IPOPT_MINOFF;
27738644Sbostic if (setsockopt(s, IPPROTO_IP, IP_OPTIONS, rspace,
27838644Sbostic sizeof(rspace)) < 0) {
27938644Sbostic perror("ping: record route");
28038644Sbostic exit(1);
28138643Sbostic }
28238643Sbostic #else
28338644Sbostic (void)fprintf(stderr,
28438644Sbostic "ping: record route not available in this implementation.\n");
28538644Sbostic exit(1);
28638644Sbostic #endif /* IP_OPTIONS */
28738643Sbostic }
28819057Skarels
28938644Sbostic /*
29038644Sbostic * When pinging the broadcast address, you can get a lot of answers.
29138644Sbostic * Doing something so evil is useful if you are trying to stress the
29238644Sbostic * ethernet, or just want to fill the arp cache to get some stuff for
29338644Sbostic * /etc/ethers.
29438643Sbostic */
29538644Sbostic hold = 48 * 1024;
29638644Sbostic (void)setsockopt(s, SOL_SOCKET, SO_RCVBUF, (char *)&hold,
29738644Sbostic sizeof(hold));
29819057Skarels
29938644Sbostic if (to->sin_family == AF_INET)
30038644Sbostic (void)printf("PING %s (%s): %d data bytes\n", hostname,
30138644Sbostic inet_ntoa(*(struct in_addr *)&to->sin_addr.s_addr),
30238644Sbostic datalen);
30338644Sbostic else
30438644Sbostic (void)printf("PING %s: %d data bytes\n", hostname, datalen);
30519057Skarels
30638646Sbostic (void)signal(SIGINT, finish);
30738644Sbostic (void)signal(SIGALRM, catcher);
30838644Sbostic
30938644Sbostic while (preload--) /* fire off them quickies */
31038643Sbostic pinger();
31119057Skarels
31238665Skarels if ((options & F_FLOOD) == 0)
31338644Sbostic catcher(); /* start things going */
31438643Sbostic
31519057Skarels for (;;) {
31638644Sbostic struct sockaddr_in from;
31738644Sbostic register int cc;
31838644Sbostic int fromlen;
31919057Skarels
32038644Sbostic if (options & F_FLOOD) {
32138643Sbostic pinger();
32238665Skarels timeout.tv_sec = 0;
32338665Skarels timeout.tv_usec = 10000;
32438665Skarels fdmask = 1 << s;
32538665Skarels if (select(s + 1, (fd_set *)&fdmask, (fd_set *)NULL,
32638665Skarels (fd_set *)NULL, &timeout) < 1)
32738643Sbostic continue;
32838643Sbostic }
32938644Sbostic fromlen = sizeof(from);
33038644Sbostic if ((cc = recvfrom(s, (char *)packet, packlen, 0,
33138644Sbostic (struct sockaddr *)&from, &fromlen)) < 0) {
33238644Sbostic if (errno == EINTR)
33319057Skarels continue;
33419057Skarels perror("ping: recvfrom");
33519057Skarels continue;
33619057Skarels }
33738644Sbostic pr_pack((char *)packet, cc, &from);
33819064Skarels if (npackets && nreceived >= npackets)
33938644Sbostic break;
34019057Skarels }
34138644Sbostic finish();
34238644Sbostic /* NOTREACHED */
34319057Skarels }
34419057Skarels
34519057Skarels /*
34638644Sbostic * catcher --
34738644Sbostic * This routine causes another PING to be transmitted, and then
34819057Skarels * schedules another SIGALRM for 1 second from now.
34953668Sbostic *
35038644Sbostic * bug --
35138644Sbostic * Our sense of time will slowly skew (i.e., packets will not be
35238644Sbostic * launched exactly at 1-second intervals). This does not affect the
35338644Sbostic * quality of the delay and loss statistics.
35419057Skarels */
35546719Sbostic void
catcher()35619057Skarels catcher()
35719057Skarels {
35819064Skarels int waittime;
35919064Skarels
36019057Skarels pinger();
36138644Sbostic (void)signal(SIGALRM, catcher);
36238644Sbostic if (!npackets || ntransmitted < npackets)
36338644Sbostic alarm((u_int)interval);
36419064Skarels else {
36519064Skarels if (nreceived) {
36619064Skarels waittime = 2 * tmax / 1000;
36738644Sbostic if (!waittime)
36819064Skarels waittime = 1;
36919064Skarels } else
37019064Skarels waittime = MAXWAIT;
37138644Sbostic (void)signal(SIGALRM, finish);
37238644Sbostic (void)alarm((u_int)waittime);
37319064Skarels }
37419057Skarels }
37519057Skarels
37619057Skarels /*
37738644Sbostic * pinger --
37853668Sbostic * Compose and transmit an ICMP ECHO REQUEST packet. The IP packet
37919057Skarels * will be added on by the kernel. The ID field is our UNIX process ID,
38019057Skarels * and the sequence number is an ascending integer. The first 8 bytes
38119057Skarels * of the data portion are used to hold a UNIX "timeval" struct in VAX
38219057Skarels * byte-order, to compute the round-trip time.
38319057Skarels */
pinger()38419057Skarels pinger()
38519057Skarels {
38638644Sbostic register struct icmp *icp;
38738644Sbostic register int cc;
38838644Sbostic int i;
38919057Skarels
39038644Sbostic icp = (struct icmp *)outpack;
39119057Skarels icp->icmp_type = ICMP_ECHO;
39219057Skarels icp->icmp_code = 0;
39319057Skarels icp->icmp_cksum = 0;
39438667Sbostic icp->icmp_seq = ntransmitted++;
39538644Sbostic icp->icmp_id = ident; /* ID */
39619057Skarels
39738644Sbostic CLR(icp->icmp_seq % mx_dup_ck);
39838643Sbostic
39919064Skarels if (timing)
40038644Sbostic (void)gettimeofday((struct timeval *)&outpack[8],
40138644Sbostic (struct timezone *)NULL);
40219057Skarels
40338644Sbostic cc = datalen + 8; /* skips ICMP portion */
40419057Skarels
40538644Sbostic /* compute ICMP checksum here */
40638644Sbostic icp->icmp_cksum = in_cksum((u_short *)icp, cc);
40719057Skarels
40838644Sbostic i = sendto(s, (char *)outpack, cc, 0, &whereto,
40938644Sbostic sizeof(struct sockaddr));
41038644Sbostic
41138644Sbostic if (i < 0 || i != cc) {
41238644Sbostic if (i < 0)
41338644Sbostic perror("ping: sendto");
41438644Sbostic (void)printf("ping: wrote %s %d chars, ret=%d\n",
41538644Sbostic hostname, cc, i);
41619057Skarels }
41749681Sbostic if (!(options & F_QUIET) && options & F_FLOOD)
41838644Sbostic (void)write(STDOUT_FILENO, &DOT, 1);
41919057Skarels }
42019057Skarels
42119057Skarels /*
42238644Sbostic * pr_pack --
42338644Sbostic * Print out the packet, if it came from us. This logic is necessary
42419057Skarels * because ALL readers of the ICMP socket get a copy of ALL ICMP packets
42519057Skarels * which arrive ('tis only fair). This permits multiple copies of this
42619057Skarels * program to be run without having intermingled output (or statistics!).
42719057Skarels */
pr_pack(buf,cc,from)42838644Sbostic pr_pack(buf, cc, from)
42938644Sbostic char *buf;
43038644Sbostic int cc;
43138644Sbostic struct sockaddr_in *from;
43219057Skarels {
43327070Skarels register struct icmp *icp;
43438644Sbostic register u_long l;
43538643Sbostic register int i, j;
43638643Sbostic register u_char *cp,*dp;
43738643Sbostic static int old_rrlen;
43838643Sbostic static char old_rr[MAX_IPOPTLEN];
43938644Sbostic struct ip *ip;
44038644Sbostic struct timeval tv, *tp;
44153668Sbostic double triptime;
44238644Sbostic int hlen, dupflag;
44319057Skarels
44438644Sbostic (void)gettimeofday(&tv, (struct timezone *)NULL);
44519057Skarels
44638643Sbostic /* Check the IP header */
44738644Sbostic ip = (struct ip *)buf;
44827070Skarels hlen = ip->ip_hl << 2;
44938644Sbostic if (cc < hlen + ICMP_MINLEN) {
45038644Sbostic if (options & F_VERBOSE)
45138644Sbostic (void)fprintf(stderr,
45238644Sbostic "ping: packet too short (%d bytes) from %s\n", cc,
45338644Sbostic inet_ntoa(*(struct in_addr *)&from->sin_addr.s_addr));
45427070Skarels return;
45527070Skarels }
45638643Sbostic
45738643Sbostic /* Now the ICMP part */
45827070Skarels cc -= hlen;
45927070Skarels icp = (struct icmp *)(buf + hlen);
46038644Sbostic if (icp->icmp_type == ICMP_ECHOREPLY) {
46138644Sbostic if (icp->icmp_id != ident)
46238643Sbostic return; /* 'Twas not our ECHO */
46338644Sbostic ++nreceived;
46438643Sbostic if (timing) {
46538643Sbostic #ifndef icmp_data
46638643Sbostic tp = (struct timeval *)&icp->icmp_ip;
46738643Sbostic #else
46838644Sbostic tp = (struct timeval *)icp->icmp_data;
46938643Sbostic #endif
47038644Sbostic tvsub(&tv, tp);
47153668Sbostic triptime = ((double)tv.tv_sec) * 1000.0 +
47253668Sbostic ((double)tv.tv_usec) / 1000.0;
47338643Sbostic tsum += triptime;
47438644Sbostic if (triptime < tmin)
47538643Sbostic tmin = triptime;
47638644Sbostic if (triptime > tmax)
47738643Sbostic tmax = triptime;
47819064Skarels }
47938643Sbostic
48038644Sbostic if (TST(icp->icmp_seq % mx_dup_ck)) {
48138644Sbostic ++nrepeats;
48238644Sbostic --nreceived;
48338644Sbostic dupflag = 1;
48438644Sbostic } else {
48538644Sbostic SET(icp->icmp_seq % mx_dup_ck);
48638644Sbostic dupflag = 0;
48738643Sbostic }
48838643Sbostic
48938644Sbostic if (options & F_QUIET)
49038643Sbostic return;
49138643Sbostic
49238644Sbostic if (options & F_FLOOD)
49338644Sbostic (void)write(STDOUT_FILENO, &BSPACE, 1);
49438644Sbostic else {
49538644Sbostic (void)printf("%d bytes from %s: icmp_seq=%u", cc,
49638644Sbostic inet_ntoa(*(struct in_addr *)&from->sin_addr.s_addr),
49738644Sbostic icp->icmp_seq);
49838644Sbostic (void)printf(" ttl=%d", ip->ip_ttl);
49938643Sbostic if (timing)
50053668Sbostic (void)printf(" time=%g ms", triptime);
50138644Sbostic if (dupflag)
50238644Sbostic (void)printf(" (DUP!)");
50338643Sbostic /* check the data */
50438643Sbostic cp = (u_char*)&icp->icmp_data[8];
50538644Sbostic dp = &outpack[8 + sizeof(struct timeval)];
50638644Sbostic for (i = 8; i < datalen; ++i, ++cp, ++dp) {
50738643Sbostic if (*cp != *dp) {
50838644Sbostic (void)printf("\nwrong data byte #%d should be 0x%x but was 0x%x",
50938644Sbostic i, *dp, *cp);
51038643Sbostic cp = (u_char*)&icp->icmp_data[0];
51138644Sbostic for (i = 8; i < datalen; ++i, ++cp) {
51238644Sbostic if ((i % 32) == 8)
51338644Sbostic (void)printf("\n\t");
51438644Sbostic (void)printf("%x ", *cp);
51538643Sbostic }
51638643Sbostic break;
51738643Sbostic }
51838643Sbostic }
51938643Sbostic }
52038643Sbostic } else {
52138643Sbostic /* We've got something other than an ECHOREPLY */
52238644Sbostic if (!(options & F_VERBOSE))
52338643Sbostic return;
52438644Sbostic (void)printf("%d bytes from %s: ", cc,
52538644Sbostic pr_addr(from->sin_addr.s_addr));
52638644Sbostic pr_icmph(icp);
52719057Skarels }
52819057Skarels
52938643Sbostic /* Display any IP options */
53038643Sbostic cp = (u_char *)buf + sizeof(struct ip);
53138644Sbostic
53247002Sbostic for (; hlen > (int)sizeof(struct ip); --hlen, ++cp)
53338644Sbostic switch (*cp) {
53438643Sbostic case IPOPT_EOL:
53538643Sbostic hlen = 0;
53638643Sbostic break;
53738643Sbostic case IPOPT_LSRR:
53838644Sbostic (void)printf("\nLSRR: ");
53938643Sbostic hlen -= 2;
54038643Sbostic j = *++cp;
54138643Sbostic ++cp;
54238644Sbostic if (j > IPOPT_MINOFF)
54338644Sbostic for (;;) {
54438644Sbostic l = *++cp;
54538644Sbostic l = (l<<8) + *++cp;
54638644Sbostic l = (l<<8) + *++cp;
54738644Sbostic l = (l<<8) + *++cp;
54838644Sbostic if (l == 0)
54938644Sbostic (void)printf("\t0.0.0.0");
55038643Sbostic else
55138644Sbostic (void)printf("\t%s", pr_addr(ntohl(l)));
55238643Sbostic hlen -= 4;
55338643Sbostic j -= 4;
55438643Sbostic if (j <= IPOPT_MINOFF)
55538643Sbostic break;
55638644Sbostic (void)putchar('\n');
55738643Sbostic }
55838643Sbostic break;
55938643Sbostic case IPOPT_RR:
56038644Sbostic j = *++cp; /* get length */
56138644Sbostic i = *++cp; /* and pointer */
56238643Sbostic hlen -= 2;
56338644Sbostic if (i > j)
56438644Sbostic i = j;
56538643Sbostic i -= IPOPT_MINOFF;
56638643Sbostic if (i <= 0)
56738643Sbostic continue;
56838643Sbostic if (i == old_rrlen
56938643Sbostic && cp == (u_char *)buf + sizeof(struct ip) + 2
570*69036Sbostic && !memcmp(cp, old_rr, i)
57138644Sbostic && !(options & F_FLOOD)) {
57238644Sbostic (void)printf("\t(same route)");
57338644Sbostic i = ((i + 3) / 4) * 4;
57438643Sbostic hlen -= i;
57538643Sbostic cp += i;
57638643Sbostic break;
57738643Sbostic }
57838643Sbostic old_rrlen = i;
57968999Sbostic memmove(old_rr, cp, i);
58038644Sbostic (void)printf("\nRR: ");
58138643Sbostic for (;;) {
58238643Sbostic l = *++cp;
58338643Sbostic l = (l<<8) + *++cp;
58438643Sbostic l = (l<<8) + *++cp;
58538643Sbostic l = (l<<8) + *++cp;
58638643Sbostic if (l == 0)
58738644Sbostic (void)printf("\t0.0.0.0");
58838643Sbostic else
58938644Sbostic (void)printf("\t%s", pr_addr(ntohl(l)));
59038643Sbostic hlen -= 4;
59138643Sbostic i -= 4;
59238643Sbostic if (i <= 0)
59338643Sbostic break;
59438644Sbostic (void)putchar('\n');
59538643Sbostic }
59638643Sbostic break;
59738643Sbostic case IPOPT_NOP:
59838644Sbostic (void)printf("\nNOP");
59938643Sbostic break;
60038643Sbostic default:
60138644Sbostic (void)printf("\nunknown option %x", *cp);
60238643Sbostic break;
60338643Sbostic }
60438644Sbostic if (!(options & F_FLOOD)) {
60538644Sbostic (void)putchar('\n');
60638644Sbostic (void)fflush(stdout);
60738643Sbostic }
60819057Skarels }
60919057Skarels
61019057Skarels /*
61138644Sbostic * in_cksum --
61238644Sbostic * Checksum routine for Internet Protocol family headers (C Version)
61319057Skarels */
in_cksum(addr,len)61419057Skarels in_cksum(addr, len)
61538644Sbostic u_short *addr;
61638644Sbostic int len;
61719057Skarels {
61824199Skarels register int nleft = len;
61924199Skarels register u_short *w = addr;
62024199Skarels register int sum = 0;
62138643Sbostic u_short answer = 0;
62219057Skarels
62319057Skarels /*
62438644Sbostic * Our algorithm is simple, using a 32 bit accumulator (sum), we add
62538644Sbostic * sequential 16 bit words to it, and at the end, fold back all the
62638644Sbostic * carry bits from the top 16 bits into the lower 16 bits.
62719057Skarels */
62838644Sbostic while (nleft > 1) {
62919057Skarels sum += *w++;
63019057Skarels nleft -= 2;
63119057Skarels }
63224199Skarels
63324199Skarels /* mop up an odd byte, if necessary */
63438644Sbostic if (nleft == 1) {
63538643Sbostic *(u_char *)(&answer) = *(u_char *)w ;
63638643Sbostic sum += answer;
63732208Sbostic }
63819057Skarels
63938644Sbostic /* add back carry outs from top 16 bits to low 16 bits */
64025244Skarels sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */
64125244Skarels sum += (sum >> 16); /* add carry */
64225244Skarels answer = ~sum; /* truncate to 16 bits */
64338644Sbostic return(answer);
64419057Skarels }
64519057Skarels
64619057Skarels /*
64738644Sbostic * tvsub --
64838644Sbostic * Subtract 2 timeval structs: out = out - in. Out is assumed to
64938644Sbostic * be >= in.
65019057Skarels */
tvsub(out,in)65138644Sbostic tvsub(out, in)
65238644Sbostic register struct timeval *out, *in;
65319057Skarels {
65438644Sbostic if ((out->tv_usec -= in->tv_usec) < 0) {
65538644Sbostic --out->tv_sec;
65619057Skarels out->tv_usec += 1000000;
65719057Skarels }
65819057Skarels out->tv_sec -= in->tv_sec;
65919057Skarels }
66019057Skarels
66138644Sbostic /*
66238644Sbostic * finish --
66338644Sbostic * Print out statistics, and give up.
66419057Skarels */
66546719Sbostic void
finish()66619057Skarels finish()
66719057Skarels {
66853668Sbostic register int i;
66953668Sbostic
67038646Sbostic (void)signal(SIGINT, SIG_IGN);
67138644Sbostic (void)putchar('\n');
67238644Sbostic (void)fflush(stdout);
67338644Sbostic (void)printf("--- %s ping statistics ---\n", hostname);
67438644Sbostic (void)printf("%ld packets transmitted, ", ntransmitted);
67538644Sbostic (void)printf("%ld packets received, ", nreceived);
67638644Sbostic if (nrepeats)
67738644Sbostic (void)printf("+%ld duplicates, ", nrepeats);
67838643Sbostic if (ntransmitted)
67938644Sbostic if (nreceived > ntransmitted)
68038644Sbostic (void)printf("-- somebody's printing up packets!");
68138029Skarels else
68238644Sbostic (void)printf("%d%% packet loss",
68338644Sbostic (int) (((ntransmitted - nreceived) * 100) /
68438644Sbostic ntransmitted));
68538644Sbostic (void)putchar('\n');
68653668Sbostic if (nreceived && timing) {
68753668Sbostic /* Only display average to microseconds */
68853668Sbostic i = 1000.0 * tsum / (nreceived + nrepeats);
68953668Sbostic (void)printf("round-trip min/avg/max = %g/%g/%g ms\n",
69053668Sbostic tmin, ((double)i) / 1000.0, tmax);
69153668Sbostic }
69219057Skarels exit(0);
69319057Skarels }
69438643Sbostic
69538644Sbostic #ifdef notdef
69638643Sbostic static char *ttab[] = {
69738643Sbostic "Echo Reply", /* ip + seq + udata */
69838643Sbostic "Dest Unreachable", /* net, host, proto, port, frag, sr + IP */
69938643Sbostic "Source Quench", /* IP */
70038643Sbostic "Redirect", /* redirect type, gateway, + IP */
70138643Sbostic "Echo",
70238643Sbostic "Time Exceeded", /* transit, frag reassem + IP */
70338643Sbostic "Parameter Problem", /* pointer + IP */
70438643Sbostic "Timestamp", /* id + seq + three timestamps */
70538643Sbostic "Timestamp Reply", /* " */
70638643Sbostic "Info Request", /* id + sq */
70738643Sbostic "Info Reply" /* " */
70838643Sbostic };
70938644Sbostic #endif
71038643Sbostic
71138643Sbostic /*
71238644Sbostic * pr_icmph --
71338644Sbostic * Print a descriptive string about an ICMP header.
71438643Sbostic */
71538644Sbostic pr_icmph(icp)
71638644Sbostic struct icmp *icp;
71738643Sbostic {
71838644Sbostic switch(icp->icmp_type) {
71938643Sbostic case ICMP_ECHOREPLY:
72038644Sbostic (void)printf("Echo Reply\n");
72138643Sbostic /* XXX ID + Seq + Data */
72238643Sbostic break;
72338643Sbostic case ICMP_UNREACH:
72438644Sbostic switch(icp->icmp_code) {
72538643Sbostic case ICMP_UNREACH_NET:
72638644Sbostic (void)printf("Destination Net Unreachable\n");
72738643Sbostic break;
72838643Sbostic case ICMP_UNREACH_HOST:
72938644Sbostic (void)printf("Destination Host Unreachable\n");
73038643Sbostic break;
73138643Sbostic case ICMP_UNREACH_PROTOCOL:
73238644Sbostic (void)printf("Destination Protocol Unreachable\n");
73338643Sbostic break;
73438643Sbostic case ICMP_UNREACH_PORT:
73538644Sbostic (void)printf("Destination Port Unreachable\n");
73638643Sbostic break;
73738643Sbostic case ICMP_UNREACH_NEEDFRAG:
73838644Sbostic (void)printf("frag needed and DF set\n");
73938643Sbostic break;
74038643Sbostic case ICMP_UNREACH_SRCFAIL:
74138644Sbostic (void)printf("Source Route Failed\n");
74238643Sbostic break;
74338643Sbostic default:
74438644Sbostic (void)printf("Dest Unreachable, Bad Code: %d\n",
74538644Sbostic icp->icmp_code);
74638643Sbostic break;
74738643Sbostic }
74838643Sbostic /* Print returned IP header information */
74938643Sbostic #ifndef icmp_data
75038644Sbostic pr_retip(&icp->icmp_ip);
75138643Sbostic #else
75238644Sbostic pr_retip((struct ip *)icp->icmp_data);
75338643Sbostic #endif
75438643Sbostic break;
75538643Sbostic case ICMP_SOURCEQUENCH:
75638644Sbostic (void)printf("Source Quench\n");
75738643Sbostic #ifndef icmp_data
75838644Sbostic pr_retip(&icp->icmp_ip);
75938643Sbostic #else
76038644Sbostic pr_retip((struct ip *)icp->icmp_data);
76138643Sbostic #endif
76238643Sbostic break;
76338643Sbostic case ICMP_REDIRECT:
76438644Sbostic switch(icp->icmp_code) {
76538643Sbostic case ICMP_REDIRECT_NET:
76638644Sbostic (void)printf("Redirect Network");
76738643Sbostic break;
76838643Sbostic case ICMP_REDIRECT_HOST:
76938644Sbostic (void)printf("Redirect Host");
77038643Sbostic break;
77138643Sbostic case ICMP_REDIRECT_TOSNET:
77238644Sbostic (void)printf("Redirect Type of Service and Network");
77338643Sbostic break;
77438643Sbostic case ICMP_REDIRECT_TOSHOST:
77538644Sbostic (void)printf("Redirect Type of Service and Host");
77638643Sbostic break;
77738643Sbostic default:
77838644Sbostic (void)printf("Redirect, Bad Code: %d", icp->icmp_code);
77938643Sbostic break;
78038643Sbostic }
78138644Sbostic (void)printf("(New addr: 0x%08lx)\n", icp->icmp_gwaddr.s_addr);
78238643Sbostic #ifndef icmp_data
78338644Sbostic pr_retip(&icp->icmp_ip);
78438643Sbostic #else
78538644Sbostic pr_retip((struct ip *)icp->icmp_data);
78638643Sbostic #endif
78738643Sbostic break;
78838643Sbostic case ICMP_ECHO:
78938644Sbostic (void)printf("Echo Request\n");
79038643Sbostic /* XXX ID + Seq + Data */
79138643Sbostic break;
79238643Sbostic case ICMP_TIMXCEED:
79338644Sbostic switch(icp->icmp_code) {
79438643Sbostic case ICMP_TIMXCEED_INTRANS:
79538644Sbostic (void)printf("Time to live exceeded\n");
79638643Sbostic break;
79738643Sbostic case ICMP_TIMXCEED_REASS:
79838644Sbostic (void)printf("Frag reassembly time exceeded\n");
79938643Sbostic break;
80038643Sbostic default:
80138644Sbostic (void)printf("Time exceeded, Bad Code: %d\n",
80238644Sbostic icp->icmp_code);
80338643Sbostic break;
80438643Sbostic }
80538643Sbostic #ifndef icmp_data
80638644Sbostic pr_retip(&icp->icmp_ip);
80738643Sbostic #else
80838644Sbostic pr_retip((struct ip *)icp->icmp_data);
80938643Sbostic #endif
81038643Sbostic break;
81138643Sbostic case ICMP_PARAMPROB:
81238644Sbostic (void)printf("Parameter problem: pointer = 0x%02x\n",
81338644Sbostic icp->icmp_hun.ih_pptr);
81438643Sbostic #ifndef icmp_data
81538644Sbostic pr_retip(&icp->icmp_ip);
81638643Sbostic #else
81738644Sbostic pr_retip((struct ip *)icp->icmp_data);
81838643Sbostic #endif
81938643Sbostic break;
82038643Sbostic case ICMP_TSTAMP:
82138644Sbostic (void)printf("Timestamp\n");
82238643Sbostic /* XXX ID + Seq + 3 timestamps */
82338643Sbostic break;
82438643Sbostic case ICMP_TSTAMPREPLY:
82538644Sbostic (void)printf("Timestamp Reply\n");
82638643Sbostic /* XXX ID + Seq + 3 timestamps */
82738643Sbostic break;
82838643Sbostic case ICMP_IREQ:
82938644Sbostic (void)printf("Information Request\n");
83038643Sbostic /* XXX ID + Seq */
83138643Sbostic break;
83238643Sbostic case ICMP_IREQREPLY:
83338644Sbostic (void)printf("Information Reply\n");
83438643Sbostic /* XXX ID + Seq */
83538643Sbostic break;
83638643Sbostic #ifdef ICMP_MASKREQ
83738643Sbostic case ICMP_MASKREQ:
83838644Sbostic (void)printf("Address Mask Request\n");
83938643Sbostic break;
84038643Sbostic #endif
84138643Sbostic #ifdef ICMP_MASKREPLY
84238643Sbostic case ICMP_MASKREPLY:
84338644Sbostic (void)printf("Address Mask Reply\n");
84438643Sbostic break;
84538643Sbostic #endif
84638643Sbostic default:
84738644Sbostic (void)printf("Bad ICMP type: %d\n", icp->icmp_type);
84838643Sbostic }
84938643Sbostic }
85038643Sbostic
85138643Sbostic /*
85238644Sbostic * pr_iph --
85338644Sbostic * Print an IP header with options.
85438643Sbostic */
85538644Sbostic pr_iph(ip)
85638644Sbostic struct ip *ip;
85738643Sbostic {
85838644Sbostic int hlen;
85938644Sbostic u_char *cp;
86038643Sbostic
86138643Sbostic hlen = ip->ip_hl << 2;
86238644Sbostic cp = (u_char *)ip + 20; /* point to options */
86338643Sbostic
86438644Sbostic (void)printf("Vr HL TOS Len ID Flg off TTL Pro cks Src Dst Data\n");
86538644Sbostic (void)printf(" %1x %1x %02x %04x %04x",
86638644Sbostic ip->ip_v, ip->ip_hl, ip->ip_tos, ip->ip_len, ip->ip_id);
86738644Sbostic (void)printf(" %1x %04x", ((ip->ip_off) & 0xe000) >> 13,
86838644Sbostic (ip->ip_off) & 0x1fff);
86938644Sbostic (void)printf(" %02x %02x %04x", ip->ip_ttl, ip->ip_p, ip->ip_sum);
87038644Sbostic (void)printf(" %s ", inet_ntoa(*(struct in_addr *)&ip->ip_src.s_addr));
87138644Sbostic (void)printf(" %s ", inet_ntoa(*(struct in_addr *)&ip->ip_dst.s_addr));
87238643Sbostic /* dump and option bytes */
87338644Sbostic while (hlen-- > 20) {
87438644Sbostic (void)printf("%02x", *cp++);
87538643Sbostic }
87638644Sbostic (void)putchar('\n');
87738643Sbostic }
87838643Sbostic
87938643Sbostic /*
88038644Sbostic * pr_addr --
88138644Sbostic * Return an ascii host address as a dotted quad and optionally with
88238644Sbostic * a hostname.
88338643Sbostic */
88438643Sbostic char *
pr_addr(l)88538644Sbostic pr_addr(l)
88638644Sbostic u_long l;
88738643Sbostic {
88838644Sbostic struct hostent *hp;
88938644Sbostic static char buf[80];
89038643Sbostic
89138644Sbostic if ((options & F_NUMERIC) ||
89238644Sbostic !(hp = gethostbyaddr((char *)&l, 4, AF_INET)))
89338644Sbostic (void)sprintf(buf, "%s", inet_ntoa(*(struct in_addr *)&l));
89438643Sbostic else
89538644Sbostic (void)sprintf(buf, "%s (%s)", hp->h_name,
89638644Sbostic inet_ntoa(*(struct in_addr *)&l));
89738644Sbostic return(buf);
89838643Sbostic }
89938643Sbostic
90038643Sbostic /*
90138644Sbostic * pr_retip --
90238644Sbostic * Dump some info on a returned (via ICMP) IP packet.
90338643Sbostic */
90438644Sbostic pr_retip(ip)
90538644Sbostic struct ip *ip;
90638643Sbostic {
90738644Sbostic int hlen;
90838644Sbostic u_char *cp;
90938643Sbostic
91038644Sbostic pr_iph(ip);
91138643Sbostic hlen = ip->ip_hl << 2;
91238644Sbostic cp = (u_char *)ip + hlen;
91338643Sbostic
91438644Sbostic if (ip->ip_p == 6)
91538644Sbostic (void)printf("TCP: from port %u, to port %u (decimal)\n",
91638644Sbostic (*cp * 256 + *(cp + 1)), (*(cp + 2) * 256 + *(cp + 3)));
91738644Sbostic else if (ip->ip_p == 17)
91838644Sbostic (void)printf("UDP: from port %u, to port %u (decimal)\n",
91938644Sbostic (*cp * 256 + *(cp + 1)), (*(cp + 2) * 256 + *(cp + 3)));
92038643Sbostic }
92138643Sbostic
fill(bp,patp)92238643Sbostic fill(bp, patp)
92338644Sbostic char *bp, *patp;
92438643Sbostic {
92538644Sbostic register int ii, jj, kk;
92638644Sbostic int pat[16];
92738644Sbostic char *cp;
92838643Sbostic
92938644Sbostic for (cp = patp; *cp; cp++)
93038644Sbostic if (!isxdigit(*cp)) {
93138644Sbostic (void)fprintf(stderr,
93238644Sbostic "ping: patterns must be specified as hex digits.\n");
93338644Sbostic exit(1);
93438644Sbostic }
93538644Sbostic ii = sscanf(patp,
93638644Sbostic "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x",
93738644Sbostic &pat[0], &pat[1], &pat[2], &pat[3], &pat[4], &pat[5], &pat[6],
93838644Sbostic &pat[7], &pat[8], &pat[9], &pat[10], &pat[11], &pat[12],
93938644Sbostic &pat[13], &pat[14], &pat[15]);
94038643Sbostic
94138644Sbostic if (ii > 0)
94258085Sbostic for (kk = 0;
94358085Sbostic kk <= MAXPACKET - (8 + sizeof(struct timeval) + ii);
94458085Sbostic kk += ii)
94538644Sbostic for (jj = 0; jj < ii; ++jj)
94638644Sbostic bp[jj + kk] = pat[jj];
94738644Sbostic if (!(options & F_QUIET)) {
94838644Sbostic (void)printf("PATTERN: 0x");
94938644Sbostic for (jj = 0; jj < ii; ++jj)
95038644Sbostic (void)printf("%02x", bp[jj] & 0xFF);
95138644Sbostic (void)printf("\n");
95238644Sbostic }
95338644Sbostic }
95438643Sbostic
usage()95538644Sbostic usage()
95638644Sbostic {
95738644Sbostic (void)fprintf(stderr,
95838644Sbostic "usage: ping [-Rdfnqrv] [-c count] [-i wait] [-l preload]\n\t[-p pattern] [-s packetsize] host\n");
95938644Sbostic exit(1);
96038643Sbostic }
961