]> git.saurik.com Git - apple/network_cmds.git/blame - ping.tproj/ping.c
network_cmds-76.tar.gz
[apple/network_cmds.git] / ping.tproj / ping.c
CommitLineData
b7080c8e
A
1/*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License'). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
12 * this file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
20 * under the License."
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24/*
25 * Copyright (c) 1989, 1993
26 * The Regents of the University of California. All rights reserved.
27 *
28 * This code is derived from software contributed to Berkeley by
29 * Mike Muuss.
30 *
31 * Redistribution and use in source and binary forms, with or without
32 * modification, are permitted provided that the following conditions
33 * are met:
34 * 1. Redistributions of source code must retain the above copyright
35 * notice, this list of conditions and the following disclaimer.
36 * 2. Redistributions in binary form must reproduce the above copyright
37 * notice, this list of conditions and the following disclaimer in the
38 * documentation and/or other materials provided with the distribution.
39 * 3. All advertising materials mentioning features or use of this software
40 * must display the following acknowledgement:
41 * This product includes software developed by the University of
42 * California, Berkeley and its contributors.
43 * 4. Neither the name of the University nor the names of its contributors
44 * may be used to endorse or promote products derived from this software
45 * without specific prior written permission.
46 *
47 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57 * SUCH DAMAGE.
58 */
59
60
61/*
62 * P I N G . C
63 *
64 * Using the InterNet Control Message Protocol (ICMP) "ECHO" facility,
65 * measure round-trip-delays and packet loss across network paths.
66 *
67 * Author -
68 * Mike Muuss
69 * U. S. Army Ballistic Research Laboratory
70 * December, 1983
71 *
72 * Status -
73 * Public Domain. Distribution Unlimited.
74 * Bugs -
75 * More statistics could always be gathered.
76 * This program has to run SUID to ROOT to access the ICMP socket.
77 */
78
79#include <sys/param.h>
80#include <sys/socket.h>
81#include <sys/file.h>
82#include <sys/time.h>
83#include <sys/signal.h>
84
85#include <netinet/in_systm.h>
86#include <netinet/in.h>
87#include <netinet/ip.h>
88#include <netinet/ip_icmp.h>
89#include <netinet/ip_var.h>
90#include <netdb.h>
91#include <unistd.h>
92#include <stdio.h>
93#include <ctype.h>
94#include <errno.h>
95#include <string.h>
96
97#define DEFDATALEN (64 - 8) /* default data length */
98#define MAXIPLEN 60
99#define MAXICMPLEN 76
100#define MAXPACKET (65536 - 60 - 8)/* max packet size */
101#define MAXWAIT 10 /* max seconds to wait for response */
102#define NROUTES 9 /* number of record route slots */
103
104#define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */
105#define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */
106#define SET(bit) (A(bit) |= B(bit))
107#define CLR(bit) (A(bit) &= (~B(bit)))
108#define TST(bit) (A(bit) & B(bit))
109
110/* various options */
111int options;
112#define F_FLOOD 0x001
113#define F_INTERVAL 0x002
114#define F_NUMERIC 0x004
115#define F_PINGFILLED 0x008
116#define F_QUIET 0x010
117#define F_RROUTE 0x020
118#define F_SO_DEBUG 0x040
119#define F_SO_DONTROUTE 0x080
120#define F_VERBOSE 0x100
121
122/*
123 * MAX_DUP_CHK is the number of bits in received table, i.e. the maximum
124 * number of received sequence numbers we can keep track of. Change 128
125 * to 8192 for complete accuracy...
126 */
127#define MAX_DUP_CHK (8 * 128)
128int mx_dup_ck = MAX_DUP_CHK;
129char rcvd_tbl[MAX_DUP_CHK / 8];
130
131struct sockaddr whereto; /* who to ping */
132int datalen = DEFDATALEN;
133int s; /* socket file descriptor */
134u_char outpack[MAXPACKET];
135char BSPACE = '\b'; /* characters written for flood */
136char DOT = '.';
137char *hostname;
138int ident; /* process id to identify our packets */
139
140/* counters */
141long npackets; /* max packets to transmit */
142long nreceived; /* # of packets we got back */
143long nrepeats; /* number of duplicates */
144long ntransmitted; /* sequence # for outbound packets = #sent */
145int interval = 1; /* interval between packets */
146
147/* timing */
148int timing; /* flag to do timing */
149double tmin = 999999999.0; /* minimum round trip time */
150double tmax = 0.0; /* maximum round trip time */
151double tsum = 0.0; /* sum of all times, for doing average */
152
153char *pr_addr();
154void catcher(), finish();
155
156main(argc, argv)
157 int argc;
158 char **argv;
159{
160 extern int errno, optind;
161 extern char *optarg;
162 struct timeval timeout;
163 struct hostent *hp;
164 struct sockaddr_in *to;
165 struct protoent *proto;
166 register int i;
167 int ch, fdmask, hold, packlen, preload;
168 u_char *datap, *packet;
169 char *target, hnamebuf[MAXHOSTNAMELEN], *malloc();
170#ifdef IP_OPTIONS
171 char rspace[3 + 4 * NROUTES + 1]; /* record route space */
172#endif
173
174 preload = 0;
175 datap = &outpack[8 + sizeof(struct timeval)];
176 while ((ch = getopt(argc, argv, "Rc:dfh:i:l:np:qrs:v")) != EOF)
177 switch(ch) {
178 case 'c':
179 npackets = atoi(optarg);
180 if (npackets <= 0) {
181 (void)fprintf(stderr,
182 "ping: bad number of packets to transmit.\n");
183 exit(1);
184 }
185 break;
186 case 'd':
187 options |= F_SO_DEBUG;
188 break;
189 case 'f':
190 if (getuid()) {
191 (void)fprintf(stderr,
192 "ping: %s\n", strerror(EPERM));
193 exit(1);
194 }
195 options |= F_FLOOD;
196 setbuf(stdout, (char *)NULL);
197 break;
198 case 'i': /* wait between sending packets */
199 interval = atoi(optarg);
200 if (interval <= 0) {
201 (void)fprintf(stderr,
202 "ping: bad timing interval.\n");
203 exit(1);
204 }
205 options |= F_INTERVAL;
206 break;
207 case 'l':
208 if (getuid()) {
209 (void)fprintf(stderr,
210 "ping: %s\n", strerror(EPERM));
211 exit(1);
212 }
213 preload = atoi(optarg);
214 if (preload < 0) {
215 (void)fprintf(stderr,
216 "ping: bad preload value.\n");
217 exit(1);
218 }
219 break;
220 case 'n':
221 options |= F_NUMERIC;
222 break;
223 case 'p': /* fill buffer with user pattern */
224 options |= F_PINGFILLED;
225 fill((char *)datap, optarg);
226 break;
227 case 'q':
228 options |= F_QUIET;
229 break;
230 case 'R':
231 options |= F_RROUTE;
232 break;
233 case 'r':
234 options |= F_SO_DONTROUTE;
235 break;
236 case 's': /* size of packet to send */
237 datalen = atoi(optarg);
238 if (datalen > MAXPACKET) {
239 (void)fprintf(stderr,
240 "ping: packet size too large.\n");
241 exit(1);
242 }
243 if (datalen <= 0) {
244 (void)fprintf(stderr,
245 "ping: illegal packet size.\n");
246 exit(1);
247 }
248 break;
249 case 'v':
250 options |= F_VERBOSE;
251 break;
252 default:
253 usage();
254 }
255 argc -= optind;
256 argv += optind;
257
258 if (argc != 1)
259 usage();
260 target = *argv;
261
262 memset(&whereto, 0, sizeof(struct sockaddr));
263 to = (struct sockaddr_in *)&whereto;
264 to->sin_family = AF_INET;
265 to->sin_addr.s_addr = inet_addr(target);
266 if (to->sin_addr.s_addr != (u_int)-1)
267 hostname = target;
268 else {
269 hp = gethostbyname(target);
270 if (!hp) {
271 (void)fprintf(stderr,
272 "ping: unknown host %s\n", target);
273 exit(1);
274 }
275 to->sin_family = hp->h_addrtype;
276 memmove(&to->sin_addr, hp->h_addr, hp->h_length);
277 (void)strncpy(hnamebuf, hp->h_name, sizeof(hnamebuf) - 1);
278 hostname = hnamebuf;
279 }
280
281 if (options & F_FLOOD && options & F_INTERVAL) {
282 (void)fprintf(stderr,
283 "ping: -f and -i incompatible options.\n");
284 exit(1);
285 }
286
287 if (datalen >= sizeof(struct timeval)) /* can we time transfer */
288 timing = 1;
289 packlen = datalen + MAXIPLEN + MAXICMPLEN;
290 if (!(packet = (u_char *)malloc((u_int)packlen))) {
291 (void)fprintf(stderr, "ping: out of memory.\n");
292 exit(1);
293 }
294 if (!(options & F_PINGFILLED))
295 for (i = 8; i < datalen; ++i)
296 *datap++ = i;
297
298 ident = getpid() & 0xFFFF;
299
300 if (!(proto = getprotobyname("icmp"))) {
301 (void)fprintf(stderr, "ping: unknown protocol icmp.\n");
302 exit(1);
303 }
304 if ((s = socket(AF_INET, SOCK_RAW, proto->p_proto)) < 0) {
305 perror("ping: socket");
306 exit(1);
307 }
308 hold = 1;
309 if (options & F_SO_DEBUG)
310 (void)setsockopt(s, SOL_SOCKET, SO_DEBUG, (char *)&hold,
311 sizeof(hold));
312 if (options & F_SO_DONTROUTE)
313 (void)setsockopt(s, SOL_SOCKET, SO_DONTROUTE, (char *)&hold,
314 sizeof(hold));
315
316 /* record route option */
317 if (options & F_RROUTE) {
318#ifdef IP_OPTIONS
319 rspace[IPOPT_OPTVAL] = IPOPT_RR;
320 rspace[IPOPT_OLEN] = sizeof(rspace)-1;
321 rspace[IPOPT_OFFSET] = IPOPT_MINOFF;
322 if (setsockopt(s, IPPROTO_IP, IP_OPTIONS, rspace,
323 sizeof(rspace)) < 0) {
324 perror("ping: record route");
325 exit(1);
326 }
327#else
328 (void)fprintf(stderr,
329 "ping: record route not available in this implementation.\n");
330 exit(1);
331#endif /* IP_OPTIONS */
332 }
333
334 /*
335 * When pinging the broadcast address, you can get a lot of answers.
336 * Doing something so evil is useful if you are trying to stress the
337 * ethernet, or just want to fill the arp cache to get some stuff for
338 * /etc/ethers.
339 */
340 (void)setsockopt(s, SOL_SOCKET, SO_RCVBUF, &packlen, sizeof(packlen));
341
342 if (to->sin_family == AF_INET)
343 (void)printf("PING %s (%s): %d data bytes\n", hostname,
344 inet_ntoa(*(struct in_addr *)&to->sin_addr.s_addr),
345 datalen);
346 else
347 (void)printf("PING %s: %d data bytes\n", hostname, datalen);
348
349 (void)signal(SIGINT, finish);
350 (void)signal(SIGALRM, catcher);
351
352 while (preload--) /* fire off them quickies */
353 pinger();
354
355 if ((options & F_FLOOD) == 0)
356 catcher(); /* start things going */
357
358 for (;;) {
359 struct sockaddr_in from;
360 register int cc;
361 int fromlen;
362 sigset_t omask, nmask;
363
364 if (options & F_FLOOD) {
365 pinger();
366 timeout.tv_sec = 0;
367 timeout.tv_usec = 10000;
368 fdmask = 1 << s;
369 if (select(s + 1, (fd_set *)&fdmask, (fd_set *)NULL,
370 (fd_set *)NULL, &timeout) < 1)
371 continue;
372 }
373 fromlen = sizeof(from);
374 if ((cc = recvfrom(s, (char *)packet, packlen, 0,
375 (struct sockaddr *)&from, &fromlen)) < 0) {
376 if (errno == EINTR)
377 continue;
378 perror("ping: recvfrom");
379 continue;
380 }
381 sigemptyset(&nmask);
382 sigaddset(&nmask, SIGALRM);
383 sigprocmask(SIG_BLOCK, &nmask, &omask);
384 pr_pack((char *)packet, cc, &from);
385 sigprocmask(SIG_SETMASK, &omask, NULL);
386 if (npackets && nreceived >= npackets)
387 break;
388 }
389 finish();
390 /* NOTREACHED */
391}
392
393/*
394 * catcher --
395 * This routine causes another PING to be transmitted, and then
396 * schedules another SIGALRM for 1 second from now.
397 *
398 * bug --
399 * Our sense of time will slowly skew (i.e., packets will not be
400 * launched exactly at 1-second intervals). This does not affect the
401 * quality of the delay and loss statistics.
402 */
403void
404catcher()
405{
406 int waittime;
407
408 pinger();
409 (void)signal(SIGALRM, catcher);
410 if (!npackets || ntransmitted < npackets)
411 alarm((u_int)interval);
412 else {
413 if (nreceived) {
414 waittime = 2 * tmax / 1000;
415 if (!waittime)
416 waittime = 1;
417 } else
418 waittime = MAXWAIT;
419 (void)signal(SIGALRM, finish);
420 (void)alarm((u_int)waittime);
421 }
422}
423
424/*
425 * pinger --
426 * Compose and transmit an ICMP ECHO REQUEST packet. The IP packet
427 * will be added on by the kernel. The ID field is our UNIX process ID,
428 * and the sequence number is an ascending integer. The first 8 bytes
429 * of the data portion are used to hold a UNIX "timeval" struct in VAX
430 * byte-order, to compute the round-trip time.
431 */
432pinger()
433{
434 register struct icmp *icp;
435 register int cc;
436 int i;
437
438 icp = (struct icmp *)outpack;
439 icp->icmp_type = ICMP_ECHO;
440 icp->icmp_code = 0;
441 icp->icmp_cksum = 0;
442 icp->icmp_seq = ntransmitted++;
443 icp->icmp_id = ident; /* ID */
444
445 CLR(icp->icmp_seq % mx_dup_ck);
446
447 if (timing)
448 (void)gettimeofday((struct timeval *)&outpack[8],
449 (struct timezone *)NULL);
450
451 cc = datalen + 8; /* skips ICMP portion */
452
453 /* compute ICMP checksum here */
454 icp->icmp_cksum = in_cksum((u_short *)icp, cc);
455
456 i = sendto(s, (char *)outpack, cc, 0, &whereto,
457 sizeof(struct sockaddr));
458
459 if (i < 0 || i != cc) {
460 if (i < 0)
461 perror("ping: sendto");
462 (void)printf("ping: wrote %s %d chars, ret=%d\n",
463 hostname, cc, i);
464 }
465 if (!(options & F_QUIET) && options & F_FLOOD)
466 (void)write(STDOUT_FILENO, &DOT, 1);
467}
468
469/*
470 * pr_pack --
471 * Print out the packet, if it came from us. This logic is necessary
472 * because ALL readers of the ICMP socket get a copy of ALL ICMP packets
473 * which arrive ('tis only fair). This permits multiple copies of this
474 * program to be run without having intermingled output (or statistics!).
475 */
476pr_pack(buf, cc, from)
477 char *buf;
478 int cc;
479 struct sockaddr_in *from;
480{
481 register struct icmp *icp;
482 register u_long l;
483 register u_int i, j;
484 register u_char *cp,*dp;
485 static int old_rrlen;
486 static char old_rr[MAX_IPOPTLEN];
487 struct ip *ip;
488 struct timeval tv, *tp;
489 double triptime;
490 int hlen, dupflag;
491
492 (void)gettimeofday(&tv, (struct timezone *)NULL);
493
494 /* Check the IP header */
495 ip = (struct ip *)buf;
496 hlen = ip->ip_hl << 2;
497 if (cc < hlen + ICMP_MINLEN) {
498 if (options & F_VERBOSE)
499 (void)fprintf(stderr,
500 "ping: packet too short (%d bytes) from %s\n", cc,
501 inet_ntoa(*(struct in_addr *)&from->sin_addr.s_addr));
502 return;
503 }
504
505 /* Now the ICMP part */
506 cc -= hlen;
507 icp = (struct icmp *)(buf + hlen);
508 if (icp->icmp_type == ICMP_ECHOREPLY) {
509 if (icp->icmp_id != ident)
510 return; /* 'Twas not our ECHO */
511 ++nreceived;
512 if (timing) {
513#ifndef icmp_data
514 tp = (struct timeval *)&icp->icmp_ip;
515#else
516 tp = (struct timeval *)icp->icmp_data;
517#endif
518 tvsub(&tv, tp);
519 triptime = ((double)tv.tv_sec) * 1000.0 +
520 ((double)tv.tv_usec) / 1000.0;
521 tsum += triptime;
522 if (triptime < tmin)
523 tmin = triptime;
524 if (triptime > tmax)
525 tmax = triptime;
526 }
527
528 if (TST(icp->icmp_seq % mx_dup_ck)) {
529 ++nrepeats;
530 --nreceived;
531 dupflag = 1;
532 } else {
533 SET(icp->icmp_seq % mx_dup_ck);
534 dupflag = 0;
535 }
536
537 if (options & F_QUIET)
538 return;
539
540 if (options & F_FLOOD)
541 (void)write(STDOUT_FILENO, &BSPACE, 1);
542 else {
543 (void)printf("%d bytes from %s: icmp_seq=%u", cc,
544 inet_ntoa(*(struct in_addr *)&from->sin_addr.s_addr),
545 icp->icmp_seq);
546 (void)printf(" ttl=%d", ip->ip_ttl);
547 if (timing)
548 (void)printf(" time=%g ms", triptime);
549 if (dupflag)
550 (void)printf(" (DUP!)");
551 /* check the data */
552 cp = (u_char*)&icp->icmp_data[8];
553 dp = &outpack[8 + sizeof(struct timeval)];
554 for (i = 8; i < datalen; ++i, ++cp, ++dp) {
555 if (*cp != *dp) {
556 (void)printf("\nwrong data byte #%d should be 0x%x but was 0x%x",
557 i, *dp, *cp);
558 cp = (u_char*)&icp->icmp_data[0];
559 for (i = 8; i < datalen; ++i, ++cp) {
560 if ((i % 32) == 8)
561 (void)printf("\n\t");
562 (void)printf("%x ", *cp);
563 }
564 break;
565 }
566 }
567 }
568 } else {
569 /* We've got something other than an ECHOREPLY */
570 if (!(options & F_VERBOSE))
571 return;
572 (void)printf("%d bytes from %s: ", cc,
573 pr_addr(from->sin_addr.s_addr));
574 pr_icmph(icp);
575 }
576
577 /* Display any IP options */
578 cp = (u_char *)buf + sizeof(struct ip);
579
580 for (; hlen > (int)sizeof(struct ip); --hlen, ++cp)
581 switch (*cp) {
582 case IPOPT_EOL:
583 hlen = 0;
584 break;
585 case IPOPT_LSRR:
586 (void)printf("\nLSRR: ");
587 hlen -= 2;
588 j = *++cp;
589 ++cp;
590 if (j > IPOPT_MINOFF)
591 for (;;) {
592 l = *++cp;
593 l = (l<<8) + *++cp;
594 l = (l<<8) + *++cp;
595 l = (l<<8) + *++cp;
596 if (l == 0)
597 (void)printf("\t0.0.0.0");
598 else
599 (void)printf("\t%s", pr_addr(ntohl(l)));
600 hlen -= 4;
601 j -= 4;
602 if (j <= IPOPT_MINOFF)
603 break;
604 (void)putchar('\n');
605 }
606 break;
607 case IPOPT_RR:
608 j = *++cp; /* get length */
609 i = *++cp; /* and pointer */
610 hlen -= 2;
611 if (i > j)
612 i = j;
613 i -= IPOPT_MINOFF;
614 if (i <= 0)
615 continue;
616 if (i == old_rrlen
617 && cp == (u_char *)buf + sizeof(struct ip) + 2
618 && !memcmp(cp, old_rr, i)
619 && !(options & F_FLOOD)) {
620 (void)printf("\t(same route)");
621 i = ((i + 3) / 4) * 4;
622 hlen -= i;
623 cp += i;
624 break;
625 }
626 if (i < MAX_IPOPTLEN) {
627 old_rrlen = i;
628 memcpy(old_rr, cp, i);
629 } else
630 old_rrlen = 0;
631
632 (void)printf("\nRR: ");
633 j = 0;
634 for (;;) {
635 l = *++cp;
636 l = (l<<8) + *++cp;
637 l = (l<<8) + *++cp;
638 l = (l<<8) + *++cp;
639 if (l == 0)
640 (void)printf("\t0.0.0.0");
641 else
642 (void)printf("\t%s", pr_addr(ntohl(l)));
643 hlen -= 4;
644 i -= 4;
645 j += 4;
646 if (i <= 0)
647 break;
648 if (j >= MAX_IPOPTLEN) {
649 (void)printf("\t(truncated route)");
650 break;
651 }
652 (void)putchar('\n');
653 }
654 break;
655 case IPOPT_NOP:
656 (void)printf("\nNOP");
657 break;
658 default:
659 (void)printf("\nunknown option %x", *cp);
660 break;
661 }
662 if (!(options & F_FLOOD)) {
663 (void)putchar('\n');
664 (void)fflush(stdout);
665 }
666}
667
668/*
669 * in_cksum --
670 * Checksum routine for Internet Protocol family headers (C Version)
671 */
672in_cksum(addr, len)
673 u_short *addr;
674 int len;
675{
676 register int nleft = len;
677 register u_short *w = addr;
678 register int sum = 0;
679 u_short answer = 0;
680
681 /*
682 * Our algorithm is simple, using a 32 bit accumulator (sum), we add
683 * sequential 16 bit words to it, and at the end, fold back all the
684 * carry bits from the top 16 bits into the lower 16 bits.
685 */
686 while (nleft > 1) {
687 sum += *w++;
688 nleft -= 2;
689 }
690
691 /* mop up an odd byte, if necessary */
692 if (nleft == 1) {
693 *(u_char *)(&answer) = *(u_char *)w ;
694 sum += answer;
695 }
696
697 /* add back carry outs from top 16 bits to low 16 bits */
698 sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */
699 sum += (sum >> 16); /* add carry */
700 answer = ~sum; /* truncate to 16 bits */
701 return(answer);
702}
703
704/*
705 * tvsub --
706 * Subtract 2 timeval structs: out = out - in. Out is assumed to
707 * be >= in.
708 */
709tvsub(out, in)
710 register struct timeval *out, *in;
711{
712 if ((out->tv_usec -= in->tv_usec) < 0) {
713 --out->tv_sec;
714 out->tv_usec += 1000000;
715 }
716 out->tv_sec -= in->tv_sec;
717}
718
719/*
720 * finish --
721 * Print out statistics, and give up.
722 */
723void
724finish()
725{
726 register int i;
727
728 (void)signal(SIGINT, SIG_IGN);
729 (void)putchar('\n');
730 (void)fflush(stdout);
731 (void)printf("--- %s ping statistics ---\n", hostname);
732 (void)printf("%ld packets transmitted, ", ntransmitted);
733 (void)printf("%ld packets received, ", nreceived);
734 if (nrepeats)
735 (void)printf("+%ld duplicates, ", nrepeats);
736 if (ntransmitted)
737 if (nreceived > ntransmitted)
738 (void)printf("-- somebody's printing up packets!");
739 else
740 (void)printf("%d%% packet loss",
741 (int) (((ntransmitted - nreceived) * 100) /
742 ntransmitted));
743 (void)putchar('\n');
744 if (nreceived && timing) {
745 /* Only display average to microseconds */
746 i = 1000.0 * tsum / (nreceived + nrepeats);
747 (void)printf("round-trip min/avg/max = %g/%g/%g ms\n",
748 tmin, ((double)i) / 1000.0, tmax);
749 }
750 exit(0);
751}
752
753#ifdef notdef
754static char *ttab[] = {
755 "Echo Reply", /* ip + seq + udata */
756 "Dest Unreachable", /* net, host, proto, port, frag, sr + IP */
757 "Source Quench", /* IP */
758 "Redirect", /* redirect type, gateway, + IP */
759 "Echo",
760 "Time Exceeded", /* transit, frag reassem + IP */
761 "Parameter Problem", /* pointer + IP */
762 "Timestamp", /* id + seq + three timestamps */
763 "Timestamp Reply", /* " */
764 "Info Request", /* id + sq */
765 "Info Reply" /* " */
766};
767#endif
768
769/*
770 * pr_icmph --
771 * Print a descriptive string about an ICMP header.
772 */
773pr_icmph(icp)
774 struct icmp *icp;
775{
776 switch(icp->icmp_type) {
777 case ICMP_ECHOREPLY:
778 (void)printf("Echo Reply\n");
779 /* XXX ID + Seq + Data */
780 break;
781 case ICMP_UNREACH:
782 switch(icp->icmp_code) {
783 case ICMP_UNREACH_NET:
784 (void)printf("Destination Net Unreachable\n");
785 break;
786 case ICMP_UNREACH_HOST:
787 (void)printf("Destination Host Unreachable\n");
788 break;
789 case ICMP_UNREACH_PROTOCOL:
790 (void)printf("Destination Protocol Unreachable\n");
791 break;
792 case ICMP_UNREACH_PORT:
793 (void)printf("Destination Port Unreachable\n");
794 break;
795 case ICMP_UNREACH_NEEDFRAG:
796 (void)printf("frag needed and DF set\n");
797 break;
798 case ICMP_UNREACH_SRCFAIL:
799 (void)printf("Source Route Failed\n");
800 break;
801 default:
802 (void)printf("Dest Unreachable, Bad Code: %d\n",
803 icp->icmp_code);
804 break;
805 }
806 /* Print returned IP header information */
807#ifndef icmp_data
808 pr_retip(&icp->icmp_ip);
809#else
810 pr_retip((struct ip *)icp->icmp_data);
811#endif
812 break;
813 case ICMP_SOURCEQUENCH:
814 (void)printf("Source Quench\n");
815#ifndef icmp_data
816 pr_retip(&icp->icmp_ip);
817#else
818 pr_retip((struct ip *)icp->icmp_data);
819#endif
820 break;
821 case ICMP_REDIRECT:
822 switch(icp->icmp_code) {
823 case ICMP_REDIRECT_NET:
824 (void)printf("Redirect Network");
825 break;
826 case ICMP_REDIRECT_HOST:
827 (void)printf("Redirect Host");
828 break;
829 case ICMP_REDIRECT_TOSNET:
830 (void)printf("Redirect Type of Service and Network");
831 break;
832 case ICMP_REDIRECT_TOSHOST:
833 (void)printf("Redirect Type of Service and Host");
834 break;
835 default:
836 (void)printf("Redirect, Bad Code: %d", icp->icmp_code);
837 break;
838 }
839 (void)printf("(New addr: 0x%08lx)\n", icp->icmp_gwaddr.s_addr);
840#ifndef icmp_data
841 pr_retip(&icp->icmp_ip);
842#else
843 pr_retip((struct ip *)icp->icmp_data);
844#endif
845 break;
846 case ICMP_ECHO:
847 (void)printf("Echo Request\n");
848 /* XXX ID + Seq + Data */
849 break;
850 case ICMP_TIMXCEED:
851 switch(icp->icmp_code) {
852 case ICMP_TIMXCEED_INTRANS:
853 (void)printf("Time to live exceeded\n");
854 break;
855 case ICMP_TIMXCEED_REASS:
856 (void)printf("Frag reassembly time exceeded\n");
857 break;
858 default:
859 (void)printf("Time exceeded, Bad Code: %d\n",
860 icp->icmp_code);
861 break;
862 }
863#ifndef icmp_data
864 pr_retip(&icp->icmp_ip);
865#else
866 pr_retip((struct ip *)icp->icmp_data);
867#endif
868 break;
869 case ICMP_PARAMPROB:
870 (void)printf("Parameter problem: pointer = 0x%02x\n",
871 icp->icmp_hun.ih_pptr);
872#ifndef icmp_data
873 pr_retip(&icp->icmp_ip);
874#else
875 pr_retip((struct ip *)icp->icmp_data);
876#endif
877 break;
878 case ICMP_TSTAMP:
879 (void)printf("Timestamp\n");
880 /* XXX ID + Seq + 3 timestamps */
881 break;
882 case ICMP_TSTAMPREPLY:
883 (void)printf("Timestamp Reply\n");
884 /* XXX ID + Seq + 3 timestamps */
885 break;
886 case ICMP_IREQ:
887 (void)printf("Information Request\n");
888 /* XXX ID + Seq */
889 break;
890 case ICMP_IREQREPLY:
891 (void)printf("Information Reply\n");
892 /* XXX ID + Seq */
893 break;
894#ifdef ICMP_MASKREQ
895 case ICMP_MASKREQ:
896 (void)printf("Address Mask Request\n");
897 break;
898#endif
899#ifdef ICMP_MASKREPLY
900 case ICMP_MASKREPLY:
901 (void)printf("Address Mask Reply\n");
902 break;
903#endif
904 default:
905 (void)printf("Bad ICMP type: %d\n", icp->icmp_type);
906 }
907}
908
909/*
910 * pr_iph --
911 * Print an IP header with options.
912 */
913pr_iph(ip)
914 struct ip *ip;
915{
916 int hlen;
917 u_char *cp;
918
919 hlen = ip->ip_hl << 2;
920 cp = (u_char *)ip + 20; /* point to options */
921
922 (void)printf("Vr HL TOS Len ID Flg off TTL Pro cks Src Dst Data\n");
923 (void)printf(" %1x %1x %02x %04x %04x",
924 ip->ip_v, ip->ip_hl, ip->ip_tos, ip->ip_len, ip->ip_id);
925 (void)printf(" %1x %04x", ((ip->ip_off) & 0xe000) >> 13,
926 (ip->ip_off) & 0x1fff);
927 (void)printf(" %02x %02x %04x", ip->ip_ttl, ip->ip_p, ip->ip_sum);
928 (void)printf(" %s ", inet_ntoa(*(struct in_addr *)&ip->ip_src.s_addr));
929 (void)printf(" %s ", inet_ntoa(*(struct in_addr *)&ip->ip_dst.s_addr));
930 /* dump and option bytes */
931 while (hlen-- > 20) {
932 (void)printf("%02x", *cp++);
933 }
934 (void)putchar('\n');
935}
936
937/*
938 * pr_addr --
939 * Return an ascii host address as a dotted quad and optionally with
940 * a hostname.
941 */
942char *
943pr_addr(l)
944 u_long l;
945{
946 struct hostent *hp;
947 static char buf[80];
948
949 if ((options & F_NUMERIC) ||
950 !(hp = gethostbyaddr((char *)&l, 4, AF_INET)))
951 (void)sprintf(buf, "%s", inet_ntoa(*(struct in_addr *)&l));
952 else
953 (void)sprintf(buf, "%s (%s)", hp->h_name,
954 inet_ntoa(*(struct in_addr *)&l));
955 return(buf);
956}
957
958/*
959 * pr_retip --
960 * Dump some info on a returned (via ICMP) IP packet.
961 */
962pr_retip(ip)
963 struct ip *ip;
964{
965 int hlen;
966 u_char *cp;
967
968 pr_iph(ip);
969 hlen = ip->ip_hl << 2;
970 cp = (u_char *)ip + hlen;
971
972 if (ip->ip_p == 6)
973 (void)printf("TCP: from port %u, to port %u (decimal)\n",
974 (*cp * 256 + *(cp + 1)), (*(cp + 2) * 256 + *(cp + 3)));
975 else if (ip->ip_p == 17)
976 (void)printf("UDP: from port %u, to port %u (decimal)\n",
977 (*cp * 256 + *(cp + 1)), (*(cp + 2) * 256 + *(cp + 3)));
978}
979
980fill(bp, patp)
981 char *bp, *patp;
982{
983 register int ii, jj, kk;
984 int pat[16];
985 char *cp;
986
987 for (cp = patp; *cp; cp++)
988 if (!isxdigit(*cp)) {
989 (void)fprintf(stderr,
990 "ping: patterns must be specified as hex digits.\n");
991 exit(1);
992 }
993 ii = sscanf(patp,
994 "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x",
995 &pat[0], &pat[1], &pat[2], &pat[3], &pat[4], &pat[5], &pat[6],
996 &pat[7], &pat[8], &pat[9], &pat[10], &pat[11], &pat[12],
997 &pat[13], &pat[14], &pat[15]);
998
999 if (ii > 0)
1000 for (kk = 0;
1001 kk <= MAXPACKET - (8 + sizeof(struct timeval) + ii);
1002 kk += ii)
1003 for (jj = 0; jj < ii; ++jj)
1004 bp[jj + kk] = pat[jj];
1005 if (!(options & F_QUIET)) {
1006 (void)printf("PATTERN: 0x");
1007 for (jj = 0; jj < ii; ++jj)
1008 (void)printf("%02x", bp[jj] & 0xFF);
1009 (void)printf("\n");
1010 }
1011}
1012
1013usage()
1014{
1015 (void)fprintf(stderr,
1016 "usage: ping [-Rdfnqrv] [-c count] [-i wait] [-l preload]\n\t[-p pattern] [-s packetsize] host\n");
1017 exit(1);
1018}