1 /* $OpenBSD: ping.c,v 1.71 2005/05/27 04:55:27 mcbride Exp $ */
2 /* $NetBSD: ping.c,v 1.20 1995/08/11 22:37:58 cgd Exp $ */
3
4 /*
5 * Copyright (c) 1989, 1993
6 * The Regents of the University of California. All rights reserved.
7 *
8 * This code is derived from software contributed to Berkeley by
9 * Mike Muuss.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #ifndef lint
37 static const char copyright[] =
38 "@(#) Copyright (c) 1989, 1993\n\
39 The Regents of the University of California. All rights reserved.\n";
40 #endif /* not lint */
41
42 #ifndef lint
43 #if 0
44 static char sccsid[] = "@(#)ping.c 8.1 (Berkeley) 6/5/93";
45 #else
46 static const char rcsid[] = "$OpenBSD: ping.c,v 1.71 2005/05/27 04:55:27 mcbride Exp $";
47 #endif
48 #endif /* not lint */
49
50 /*
51 * P I N G . C
52 *
53 * Using the InterNet Control Message Protocol (ICMP) "ECHO" facility,
54 * measure round-trip-delays and packet loss across network paths.
55 *
56 * Author -
57 * Mike Muuss
58 * U. S. Army Ballistic Research Laboratory
59 * December, 1983
60 *
61 * Status -
62 * Public Domain. Distribution Unlimited.
63 * Bugs -
64 * More statistics could always be gathered.
65 * This program has to run SUID to ROOT to access the ICMP socket.
66 */
67
68 #include <sys/param.h>
69 #include <sys/queue.h>
70 #include <sys/socket.h>
71 #include <sys/file.h>
72 #include <sys/time.h>
73
74 #include <netinet/in_systm.h>
75 #include <netinet/in.h>
76 #include <netinet/ip.h>
77 #include <netinet/ip_icmp.h>
78 #include <netinet/ip_var.h>
79 #include <arpa/inet.h>
80 #include <netdb.h>
81 #include <signal.h>
82 #include <unistd.h>
83 #include <stdio.h>
84 #include <ctype.h>
85 #include <err.h>
86 #include <errno.h>
87 #include <string.h>
88 #include <stdlib.h>
89
90 struct tvi {
91 u_int tv_sec;
92 u_int tv_usec;
93 };
94
95 #define DEFDATALEN (64 - 8) /* default data length */
96 #define MAXIPLEN 60
97 #define MAXICMPLEN 76
98 #define MAXPAYLOAD (IP_MAXPACKET - MAXIPLEN - 8) /* max ICMP payload size */
99 #define MAXWAIT_DEFAULT 10 /* secs to wait for response */
100 #define NROUTES 9 /* number of record route slots */
101
102 #define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */
103 #define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */
104 #define SET(bit) (A(bit) |= B(bit))
105 #define CLR(bit) (A(bit) &= (~B(bit)))
106 #define TST(bit) (A(bit) & B(bit))
107
108 /* various options */
109 int options;
110 #define F_FLOOD 0x0001
111 #define F_INTERVAL 0x0002
112 #define F_NUMERIC 0x0004
113 #define F_PINGFILLED 0x0008
114 #define F_QUIET 0x0010
115 #define F_RROUTE 0x0020
116 #define F_SO_DEBUG 0x0040
117 #define F_SO_DONTROUTE 0x0080
118 #define F_VERBOSE 0x0100
119 #define F_SADDR 0x0200
120 #define F_HDRINCL 0x0400
121 #define F_TTL 0x0800
122 #define F_SO_JUMBO 0x1000
123
124 /* multicast options */
125 int moptions;
126 #define MULTICAST_NOLOOP 0x001
127 #define MULTICAST_TTL 0x002
128 #define MULTICAST_IF 0x004
129
130 /*
131 * MAX_DUP_CHK is the number of bits in received table, i.e. the maximum
132 * number of received sequence numbers we can keep track of. Change 128
133 * to 8192 for complete accuracy...
134 */
135 #define MAX_DUP_CHK (8 * 128)
136 int mx_dup_ck = MAX_DUP_CHK;
137 char rcvd_tbl[MAX_DUP_CHK / 8];
138
139 struct sockaddr whereto; /* who to ping */
140 struct sockaddr_in whence; /* Which interface we come from */
141 unsigned int datalen = DEFDATALEN;
142 int s; /* socket file descriptor */
143 u_char outpackhdr[IP_MAXPACKET]; /* Max packet size = 65535 */
144 u_char *outpack = outpackhdr+sizeof(struct ip);
145 char BSPACE = '\b'; /* characters written for flood */
146 char DOT = '.';
147 char *hostname;
148 int ident; /* process id to identify our packets */
149
150 /* counters */
151 unsigned long npackets; /* max packets to transmit */
152 unsigned long nreceived; /* # of packets we got back */
153 unsigned long nrepeats; /* number of duplicates */
154 unsigned long ntransmitted; /* sequence # for outbound packets = #sent */
155 double interval = 1; /* interval between packets */
156 struct itimerval interstr; /* interval structure for use with setitimer */
157
158 /* timing */
159 int timing; /* flag to do timing */
160 unsigned int maxwait = MAXWAIT_DEFAULT; /* max seconds to wait for response */
161 quad_t tmin = 999999999; /* minimum round trip time in millisec */
162 quad_t tmax = 0; /* maximum round trip time in millisec */
163 quad_t tsum = 0; /* sum of all times in millisec, for doing average */
164 quad_t tsumsq = 0; /* sum of all times squared, for std. dev. */
165
166 int bufspace = IP_MAXPACKET;
167
168 void fill(char *, char *);
169 void catcher(int signo);
170 void prtsig(int signo);
171 void finish(int signo);
172 void summary(int, int);
173 int in_cksum(u_short *, int);
174 void pinger(void);
175 char *pr_addr(in_addr_t);
176 int check_icmph(struct ip *);
177 void pr_icmph(struct icmp *);
178 void pr_pack(char *, int, struct sockaddr_in *);
179 void pr_retip(struct ip *);
180 quad_t qsqrt(quad_t);
181 void pr_iph(struct ip *);
182 void usage(void);
183
184 int
main(int argc,char * argv[])185 main(int argc, char *argv[])
186 {
187 struct timeval timeout;
188 struct hostent *hp;
189 struct sockaddr_in *to;
190 struct in_addr saddr;
191 int i;
192 int ch, hold = 1, packlen, preload;
193 int maxsize, fdmasks;
194 socklen_t maxsizelen;
195 u_char *datap, *packet;
196 char *target, hnamebuf[MAXHOSTNAMELEN];
197 u_char ttl = MAXTTL, loop = 1;
198 int df = 0, tos = 0;
199 #ifdef IP_OPTIONS
200 char rspace[3 + 4 * NROUTES + 1]; /* record route space */
201 #endif
202 fd_set *fdmaskp;
203 const char *errstr;
204
205 if ((s = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) < 0)
206 err(1, "socket");
207
208 /* revoke privs */
209 seteuid(getuid());
210 setuid(getuid());
211
212 preload = 0;
213 datap = &outpack[8 + sizeof(struct tvi)];
214 while ((ch = getopt(argc, argv,
215 "DI:LRS:c:dfi:jl:np:qrs:T:t:vw:")) != -1)
216 switch(ch) {
217 case 'c':
218 npackets = strtonum(optarg, 1, INT_MAX, &errstr);
219 if (errstr)
220 errx(1,
221 "number of packets to transmit is %s: %s",
222 errstr, optarg);
223 break;
224 case 'D':
225 options |= F_HDRINCL;
226 df = 1;
227 break;
228 case 'd':
229 options |= F_SO_DEBUG;
230 break;
231 case 'f':
232 if (getuid())
233 errx(1, "%s", strerror(EPERM));
234 options |= F_FLOOD;
235 setbuf(stdout, (char *)NULL);
236 break;
237 case 'I':
238 case 'S': /* deprecated */
239 if (inet_aton(optarg, &saddr) == 0) {
240 if ((hp = gethostbyname(optarg)) == NULL)
241 errx(1, "bad interface address: %s",
242 optarg);
243 memcpy(&saddr, hp->h_addr, sizeof(saddr));
244 }
245 options |= F_SADDR;
246 break;
247 case 'i': /* wait between sending packets */
248 interval = strtod(optarg, NULL);
249
250 if (interval <= 0 || interval >= INT_MAX)
251 errx(1, "bad timing interval: %s", optarg);
252
253 if (interval < 1)
254 if (getuid())
255 errx(1, "%s: only root may use interval < 1s",
256 strerror(EPERM));
257
258 if (interval < 0.01)
259 interval = 0.01;
260
261 options |= F_INTERVAL;
262 break;
263 case 'j':
264 options |= F_SO_JUMBO;
265 break;
266 case 'L':
267 moptions |= MULTICAST_NOLOOP;
268 loop = 0;
269 break;
270 case 'l':
271 if (getuid())
272 errx(1, "%s", strerror(EPERM));
273 preload = strtonum(optarg, 1, INT_MAX, &errstr);
274 if (errstr)
275 errx(1, "preload value is %s: %s",
276 errstr, optarg);
277 break;
278 case 'n':
279 options |= F_NUMERIC;
280 break;
281 case 'p': /* fill buffer with user pattern */
282 options |= F_PINGFILLED;
283 fill((char *)datap, optarg);
284 break;
285 case 'q':
286 options |= F_QUIET;
287 break;
288 case 'R':
289 options |= F_RROUTE;
290 break;
291 case 'r':
292 options |= F_SO_DONTROUTE;
293 break;
294 case 's': /* size of packet to send */
295 datalen = strtonum(optarg, 0, MAXPAYLOAD, &errstr);
296 if (errstr)
297 errx(1, "packet size is %s: %s",
298 errstr, optarg);
299 break;
300 case 'T':
301 options |= F_HDRINCL;
302 tos = strtonum(optarg, 0, 0xff, &errstr);
303 if (errstr)
304 errx(1, "tos value is %s: %s", errstr, optarg);
305 break;
306 case 't':
307 options |= F_TTL;
308 ttl = strtonum(optarg, 1, 255, &errstr);
309 if (errstr)
310 errx(1, "ttl value is %s: %s", errstr, optarg);
311 break;
312 case 'v':
313 options |= F_VERBOSE;
314 break;
315 case 'w':
316 maxwait = strtonum(optarg, 1, INT_MAX, &errstr);
317 if (errstr)
318 errx(1, "maxwait value is %s: %s",
319 errstr, optarg);
320 break;
321 default:
322 usage();
323 }
324 argc -= optind;
325 argv += optind;
326
327 if (argc != 1)
328 usage();
329
330 memset(&interstr, 0, sizeof(interstr));
331
332 interstr.it_value.tv_sec = (long) interval;
333 interstr.it_value.tv_usec =
334 (long) ((interval - interstr.it_value.tv_sec) * 1000000);
335
336 target = *argv;
337
338 memset(&whereto, 0, sizeof(struct sockaddr));
339 to = (struct sockaddr_in *)&whereto;
340 to->sin_len = sizeof(struct sockaddr_in);
341 to->sin_family = AF_INET;
342 if (inet_aton(target, &to->sin_addr) != 0)
343 hostname = target;
344 else {
345 hp = gethostbyname(target);
346 if (!hp)
347 errx(1, "unknown host: %s", target);
348 to->sin_family = hp->h_addrtype;
349 memcpy(&to->sin_addr, hp->h_addr, hp->h_length);
350 (void)strlcpy(hnamebuf, hp->h_name, sizeof(hnamebuf));
351 hostname = hnamebuf;
352 }
353
354 if (options & F_FLOOD && options & F_INTERVAL)
355 errx(1, "-f and -i options are incompatible");
356
357 if (datalen >= sizeof(struct tvi)) /* can we time transfer */
358 timing = 1;
359 packlen = datalen + MAXIPLEN + MAXICMPLEN;
360 if (!(packet = (u_char *)malloc((u_int)packlen)))
361 err(1, "malloc");
362 if (!(options & F_PINGFILLED))
363 for (i = sizeof(struct tvi); i < datalen; ++i)
364 *datap++ = i;
365
366 ident = getpid() & 0xFFFF;
367
368 if (options & F_SADDR) {
369 if (IN_MULTICAST(ntohl(to->sin_addr.s_addr)))
370 moptions |= MULTICAST_IF;
371 else {
372 memset(&whence, 0, sizeof(whence));
373 whence.sin_len = sizeof(whence);
374 whence.sin_family = AF_INET;
375 memcpy(&whence.sin_addr.s_addr, &saddr, sizeof(saddr));
376 if (bind(s, (struct sockaddr*)&whence,
377 sizeof(whence)) < 0)
378 err(1, "bind");
379 }
380 }
381
382 if (options & F_SO_DEBUG)
383 (void)setsockopt(s, SOL_SOCKET, SO_DEBUG, (char *)&hold,
384 sizeof(hold));
385 if (options & F_SO_DONTROUTE)
386 (void)setsockopt(s, SOL_SOCKET, SO_DONTROUTE, (char *)&hold,
387 sizeof(hold));
388 if (options & F_SO_JUMBO)
389 (void)setsockopt(s, SOL_SOCKET, SO_JUMBO, (char *)&hold,
390 sizeof(hold));
391
392 if (options & F_TTL) {
393 if (IN_MULTICAST(ntohl(to->sin_addr.s_addr)))
394 moptions |= MULTICAST_TTL;
395 else
396 options |= F_HDRINCL;
397 }
398
399 if (options & F_RROUTE && options & F_HDRINCL)
400 errx(1, "-R option and -D or -T, or -t to unicast destinations"
401 " are incompatible");
402
403 if (options & F_HDRINCL) {
404 struct ip *ip = (struct ip *)outpackhdr;
405
406 setsockopt(s, IPPROTO_IP, IP_HDRINCL, &hold, sizeof(hold));
407 ip->ip_v = IPVERSION;
408 ip->ip_hl = sizeof(struct ip) >> 2;
409 ip->ip_tos = tos;
410 ip->ip_id = 0;
411 ip->ip_off = htons(df ? IP_DF : 0);
412 ip->ip_ttl = ttl;
413 ip->ip_p = IPPROTO_ICMP;
414 if (options & F_SADDR)
415 ip->ip_src = saddr;
416 else
417 ip->ip_src.s_addr = INADDR_ANY;
418 ip->ip_dst = to->sin_addr;
419 }
420
421 /* record route option */
422 if (options & F_RROUTE) {
423 if (IN_MULTICAST(ntohl(to->sin_addr.s_addr)))
424 errx(1, "record route not valid to multicast destinations");
425 #ifdef IP_OPTIONS
426 memset(rspace, 0, sizeof(rspace));
427 rspace[IPOPT_OPTVAL] = IPOPT_RR;
428 rspace[IPOPT_OLEN] = sizeof(rspace)-1;
429 rspace[IPOPT_OFFSET] = IPOPT_MINOFF;
430 if (setsockopt(s, IPPROTO_IP, IP_OPTIONS, rspace,
431 sizeof(rspace)) < 0) {
432 perror("ping: record route");
433 exit(1);
434 }
435 #else
436 errx(1, "record route not available in this implementation");
437 #endif /* IP_OPTIONS */
438 }
439
440 if ((moptions & MULTICAST_NOLOOP) &&
441 setsockopt(s, IPPROTO_IP, IP_MULTICAST_LOOP, &loop,
442 sizeof(loop)) < 0)
443 err(1, "setsockopt IP_MULTICAST_LOOP");
444 if ((moptions & MULTICAST_TTL) &&
445 setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL, &ttl,
446 sizeof(ttl)) < 0)
447 err(1, "setsockopt IP_MULTICAST_TTL");
448 if ((moptions & MULTICAST_IF) &&
449 setsockopt(s, IPPROTO_IP, IP_MULTICAST_IF, &saddr,
450 sizeof(saddr)) < 0)
451 err(1, "setsockopt IP_MULTICAST_IF");
452
453 /*
454 * When trying to send large packets, you must increase the
455 * size of both the send and receive buffers...
456 */
457 maxsizelen = sizeof maxsize;
458 if (getsockopt(s, SOL_SOCKET, SO_SNDBUF, &maxsize, &maxsizelen) < 0)
459 err(1, "getsockopt");
460 if (maxsize < packlen &&
461 setsockopt(s, SOL_SOCKET, SO_SNDBUF, &packlen, sizeof(maxsize)) < 0)
462 err(1, "setsockopt");
463
464 /*
465 * When pinging the broadcast address, you can get a lot of answers.
466 * Doing something so evil is useful if you are trying to stress the
467 * ethernet, or just want to fill the arp cache to get some stuff for
468 * /etc/ethers.
469 */
470 while (setsockopt(s, SOL_SOCKET, SO_RCVBUF,
471 (void*)&bufspace, sizeof(bufspace)) < 0) {
472 if ((bufspace -= 1024) <= 0)
473 err(1, "Cannot set the receive buffer size");
474 }
475 if (bufspace < IP_MAXPACKET)
476 warnx("Could only allocate a receive buffer of %i bytes (default %i)",
477 bufspace, IP_MAXPACKET);
478
479 if (to->sin_family == AF_INET)
480 (void)printf("PING %s (%s): %d data bytes\n", hostname,
481 inet_ntoa(*(struct in_addr *)&to->sin_addr.s_addr),
482 datalen);
483 else
484 (void)printf("PING %s: %d data bytes\n", hostname, datalen);
485
486 (void)signal(SIGINT, finish);
487 (void)signal(SIGALRM, catcher);
488 #ifdef SIGINFO
489 (void)signal(SIGINFO, prtsig);
490 #endif
491
492 while (preload--) /* fire off them quickies */
493 pinger();
494
495 if ((options & F_FLOOD) == 0)
496 catcher(0); /* start things going */
497
498 fdmasks = howmany(s+1, NFDBITS) * sizeof(fd_mask);
499 if ((fdmaskp = (fd_set *)malloc(fdmasks)) == NULL)
500 err(1, "malloc");
501
502 for (;;) {
503 struct sockaddr_in from;
504 sigset_t omask, nmask;
505 socklen_t fromlen;
506 int cc;
507
508 if (options & F_FLOOD) {
509 pinger();
510 timeout.tv_sec = 0;
511 timeout.tv_usec = 10000;
512 memset(fdmaskp, 0, fdmasks);
513 FD_SET(s, fdmaskp);
514 if (select(s + 1, (fd_set *)fdmaskp, (fd_set *)NULL,
515 (fd_set *)NULL, &timeout) < 1)
516 continue;
517 }
518 fromlen = sizeof(from);
519 if ((cc = recvfrom(s, (char *)packet, packlen, 0,
520 (struct sockaddr *)&from, &fromlen)) < 0) {
521 if (errno == EINTR)
522 continue;
523 perror("ping: recvfrom");
524 continue;
525 }
526 sigemptyset(&nmask);
527 sigaddset(&nmask, SIGALRM);
528 sigprocmask(SIG_BLOCK, &nmask, &omask);
529 pr_pack((char *)packet, cc, &from);
530 sigprocmask(SIG_SETMASK, &omask, NULL);
531 if (npackets && nreceived >= npackets)
532 break;
533 }
534 free(fdmaskp);
535 finish(0);
536 /* NOTREACHED */
537 exit(0); /* Make the compiler happy */
538 }
539
540 /*
541 * catcher --
542 * This routine causes another PING to be transmitted, and then
543 * schedules another SIGALRM for 1 second from now.
544 *
545 * bug --
546 * Our sense of time will slowly skew (i.e., packets will not be
547 * launched exactly at 1-second intervals). This does not affect the
548 * quality of the delay and loss statistics.
549 */
550 /* ARGSUSED */
551 void
catcher(int signo)552 catcher(int signo)
553 {
554 int save_errno = errno;
555 unsigned int waittime;
556
557 pinger();
558 (void)signal(SIGALRM, catcher);
559 if (!npackets || ntransmitted < npackets)
560 setitimer(ITIMER_REAL, &interstr, (struct itimerval *)0);
561 else {
562 if (nreceived) {
563 waittime = 2 * tmax / 1000000;
564 if (!waittime)
565 waittime = 1;
566 } else
567 waittime = maxwait;
568 (void)signal(SIGALRM, finish);
569 (void)alarm(waittime);
570 }
571 errno = save_errno;
572 }
573
574 /*
575 * Print statistics when SIGINFO is received.
576 * XXX not race safe
577 */
578 /* ARGSUSED */
579 void
prtsig(int signo)580 prtsig(int signo)
581 {
582 int save_errno = errno;
583
584 summary(0, 1);
585 errno = save_errno;
586 }
587
588 /*
589 * pinger --
590 * Compose and transmit an ICMP ECHO REQUEST packet. The IP packet
591 * will be added on by the kernel. The ID field is our UNIX process ID,
592 * and the sequence number is an ascending integer. The first 8 bytes
593 * of the data portion are used to hold a UNIX "timeval" struct in VAX
594 * byte-order, to compute the round-trip time.
595 */
596 void
pinger(void)597 pinger(void)
598 {
599 struct icmp *icp;
600 char buf[8192];
601 int cc, i;
602 u_char *packet = outpack;
603
604 icp = (struct icmp *)outpack;
605 icp->icmp_type = ICMP_ECHO;
606 icp->icmp_code = 0;
607 icp->icmp_cksum = 0;
608 icp->icmp_seq = htons(ntransmitted);
609 icp->icmp_id = ident; /* ID */
610
611 CLR(ntohs(icp->icmp_seq) % mx_dup_ck);
612
613 if (timing) {
614 struct timeval tv;
615 struct tvi tvi;
616
617 (void)gettimeofday(&tv, (struct timezone *)NULL);
618 tvi.tv_sec = htonl(tv.tv_sec);
619 tvi.tv_usec = htonl(tv.tv_usec);
620 memcpy((u_int *)&outpack[8], &tvi, sizeof tvi);
621 }
622
623 cc = datalen + 8; /* skips ICMP portion */
624
625 /* compute ICMP checksum here */
626 icp->icmp_cksum = in_cksum((u_short *)icp, cc);
627
628 if (options & F_HDRINCL) {
629 struct ip *ip = (struct ip *)outpackhdr;
630
631 packet = (u_char *)ip;
632 cc += sizeof(struct ip);
633 ip->ip_len = htons(cc);
634 ip->ip_sum = in_cksum((u_short *)outpackhdr, cc);
635 }
636
637 i = sendto(s, (char *)packet, cc, 0, &whereto,
638 sizeof(struct sockaddr));
639
640 if (i < 0 || i != cc) {
641 if (i < 0)
642 perror("ping: sendto");
643 snprintf(buf, sizeof buf, "ping: wrote %s %d chars, ret=%d\n",
644 hostname, cc, i);
645 write(STDOUT_FILENO, buf, strlen(buf));
646 }
647 if (!(options & F_QUIET) && options & F_FLOOD)
648 (void)write(STDOUT_FILENO, &DOT, 1);
649
650 ntransmitted++;
651 }
652
653 /*
654 * pr_pack --
655 * Print out the packet, if it came from us. This logic is necessary
656 * because ALL readers of the ICMP socket get a copy of ALL ICMP packets
657 * which arrive ('tis only fair). This permits multiple copies of this
658 * program to be run without having intermingled output (or statistics!).
659 */
660 void
pr_pack(char * buf,int cc,struct sockaddr_in * from)661 pr_pack(char *buf, int cc, struct sockaddr_in *from)
662 {
663 struct icmp *icp;
664 in_addr_t l;
665 u_int i, j;
666 u_char *cp, *dp;
667 static int old_rrlen;
668 static char old_rr[MAX_IPOPTLEN];
669 struct ip *ip, *ip2;
670 struct timeval tv, tp;
671 char *pkttime;
672 quad_t triptime = 0;
673 int hlen, hlen2, dupflag;
674
675 (void)gettimeofday(&tv, (struct timezone *)NULL);
676
677 /* Check the IP header */
678 ip = (struct ip *)buf;
679 hlen = ip->ip_hl << 2;
680 if (cc < hlen + ICMP_MINLEN) {
681 if (options & F_VERBOSE)
682 warnx("packet too short (%d bytes) from %s", cc,
683 inet_ntoa(*(struct in_addr *)&from->sin_addr.s_addr));
684 return;
685 }
686
687 /* Now the ICMP part */
688 cc -= hlen;
689 icp = (struct icmp *)(buf + hlen);
690 if (icp->icmp_type == ICMP_ECHOREPLY) {
691 if (icp->icmp_id != ident)
692 return; /* 'Twas not our ECHO */
693 ++nreceived;
694 if (timing) {
695 struct tvi tvi;
696
697 #ifndef icmp_data
698 pkttime = (char *)&icp->icmp_ip;
699 #else
700 pkttime = (char *)icp->icmp_data;
701 #endif
702 memcpy(&tvi, pkttime, sizeof tvi);
703 tp.tv_sec = ntohl(tvi.tv_sec);
704 tp.tv_usec = ntohl(tvi.tv_usec);
705
706 timersub(&tv, &tp, &tv);
707 triptime = (tv.tv_sec * 1000000) + tv.tv_usec;
708 tsum += triptime;
709 tsumsq += triptime * triptime;
710 if (triptime < tmin)
711 tmin = triptime;
712 if (triptime > tmax)
713 tmax = triptime;
714 }
715
716 if (TST(ntohs(icp->icmp_seq) % mx_dup_ck)) {
717 ++nrepeats;
718 --nreceived;
719 dupflag = 1;
720 } else {
721 SET(ntohs(icp->icmp_seq) % mx_dup_ck);
722 dupflag = 0;
723 }
724
725 if (options & F_QUIET)
726 return;
727
728 if (options & F_FLOOD)
729 (void)write(STDOUT_FILENO, &BSPACE, 1);
730 else {
731 (void)printf("%d bytes from %s: icmp_seq=%u", cc,
732 inet_ntoa(*(struct in_addr *)&from->sin_addr.s_addr),
733 ntohs(icp->icmp_seq));
734 (void)printf(" ttl=%d", ip->ip_ttl);
735 if (timing)
736 (void)printf(" time=%d.%03d ms",
737 (int)(triptime / 1000),
738 (int)(triptime % 1000));
739 if (dupflag)
740 (void)printf(" (DUP!)");
741 /* check the data */
742 cp = (u_char *)&icp->icmp_data[sizeof(struct tvi)];
743 dp = &outpack[8 + sizeof(struct tvi)];
744 for (i = 8 + sizeof(struct tvi); i < datalen;
745 ++i, ++cp, ++dp) {
746 if (*cp != *dp) {
747 (void)printf("\nwrong data byte #%d "
748 "should be 0x%x but was 0x%x",
749 i, *dp, *cp);
750 cp = (u_char *)&icp->icmp_data[0];
751 for (i = 8; i < datalen; ++i, ++cp) {
752 if ((i % 32) == 8)
753 (void)printf("\n\t");
754 (void)printf("%x ", *cp);
755 }
756 break;
757 }
758 }
759 }
760 } else {
761 /* We've got something other than an ECHOREPLY */
762 if (!(options & F_VERBOSE))
763 return;
764 ip2 = (struct ip *) (buf + hlen + sizeof (struct icmp));
765 hlen2 = ip2->ip_hl << 2;
766 if (cc >= hlen2 + 8 && check_icmph((struct ip *)(icp +
767 sizeof (struct icmp))) != 1)
768 return;
769 (void)printf("%d bytes from %s: ", cc,
770 pr_addr(from->sin_addr.s_addr));
771 pr_icmph(icp);
772 }
773
774 /* Display any IP options */
775 cp = (u_char *)buf + sizeof(struct ip);
776
777 for (; hlen > (int)sizeof(struct ip); --hlen, ++cp)
778 switch (*cp) {
779 case IPOPT_EOL:
780 hlen = 0;
781 break;
782 case IPOPT_LSRR:
783 (void)printf("\nLSRR: ");
784 hlen -= 2;
785 j = *++cp;
786 ++cp;
787 i = 0;
788 if (j > IPOPT_MINOFF) {
789 for (;;) {
790 l = *++cp;
791 l = (l<<8) + *++cp;
792 l = (l<<8) + *++cp;
793 l = (l<<8) + *++cp;
794 if (l == 0)
795 (void)printf("\t0.0.0.0");
796 else
797 (void)printf("\t%s",
798 pr_addr(ntohl(l)));
799 hlen -= 4;
800 j -= 4;
801 i += 4;
802 if (j <= IPOPT_MINOFF)
803 break;
804 if (i >= MAX_IPOPTLEN) {
805 (void)printf("\t(truncated route)");
806 break;
807 }
808 (void)putchar('\n');
809 }
810 }
811 break;
812 case IPOPT_RR:
813 j = *++cp; /* get length */
814 i = *++cp; /* and pointer */
815 hlen -= 2;
816 if (i > j)
817 i = j;
818 i -= IPOPT_MINOFF;
819 if (i <= 0)
820 continue;
821 if (i == old_rrlen &&
822 cp == (u_char *)buf + sizeof(struct ip) + 2 &&
823 !memcmp(cp, old_rr, i) &&
824 !(options & F_FLOOD)) {
825 (void)printf("\t(same route)");
826 i = ((i + 3) / 4) * 4;
827 hlen -= i;
828 cp += i;
829 break;
830 }
831 if (i < MAX_IPOPTLEN) {
832 old_rrlen = i;
833 memcpy(old_rr, cp, i);
834 } else
835 old_rrlen = 0;
836
837 (void)printf("\nRR: ");
838 j = 0;
839 for (;;) {
840 l = *++cp;
841 l = (l<<8) + *++cp;
842 l = (l<<8) + *++cp;
843 l = (l<<8) + *++cp;
844 if (l == 0)
845 (void)printf("\t0.0.0.0");
846 else
847 (void)printf("\t%s", pr_addr(ntohl(l)));
848 hlen -= 4;
849 i -= 4;
850 j += 4;
851 if (i <= 0)
852 break;
853 if (j >= MAX_IPOPTLEN) {
854 (void)printf("\t(truncated route)");
855 break;
856 }
857 (void)putchar('\n');
858 }
859 break;
860 case IPOPT_NOP:
861 (void)printf("\nNOP");
862 break;
863 default:
864 (void)printf("\nunknown option %x", *cp);
865 hlen = hlen - (cp[IPOPT_OLEN] - 1);
866 cp = cp + (cp[IPOPT_OLEN] - 1);
867 break;
868 }
869 if (!(options & F_FLOOD)) {
870 (void)putchar('\n');
871 (void)fflush(stdout);
872 }
873 }
874
875 /*
876 * in_cksum --
877 * Checksum routine for Internet Protocol family headers (C Version)
878 */
879 int
in_cksum(u_short * addr,int len)880 in_cksum(u_short *addr, int len)
881 {
882 int nleft = len;
883 u_short *w = addr;
884 int sum = 0;
885 u_short answer = 0;
886
887 /*
888 * Our algorithm is simple, using a 32 bit accumulator (sum), we add
889 * sequential 16 bit words to it, and at the end, fold back all the
890 * carry bits from the top 16 bits into the lower 16 bits.
891 */
892 while (nleft > 1) {
893 sum += *w++;
894 nleft -= 2;
895 }
896
897 /* mop up an odd byte, if necessary */
898 if (nleft == 1) {
899 *(u_char *)(&answer) = *(u_char *)w ;
900 sum += answer;
901 }
902
903 /* add back carry outs from top 16 bits to low 16 bits */
904 sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */
905 sum += (sum >> 16); /* add carry */
906 answer = ~sum; /* truncate to 16 bits */
907 return(answer);
908 }
909
910 void
summary(int header,int sig)911 summary(int header, int sig)
912 {
913 char buf[8192], buft[8192];
914
915 buf[0] = '\0';
916
917 if (!sig) {
918 (void)putchar('\r');
919 (void)fflush(stdout);
920 } else
921 strlcat(buf, "\r", sizeof buf);
922
923 if (header) {
924 snprintf(buft, sizeof buft, "--- %s ping statistics ---\n",
925 hostname);
926 strlcat(buf, buft, sizeof buf);
927 }
928
929 snprintf(buft, sizeof buft, "%ld packets transmitted, ", ntransmitted);
930 strlcat(buf, buft, sizeof buf);
931 snprintf(buft, sizeof buft, "%ld packets received, ", nreceived);
932 strlcat(buf, buft, sizeof buf);
933
934 if (nrepeats) {
935 snprintf(buft, sizeof buft, "%ld duplicates, ", nrepeats);
936 strlcat(buf, buft, sizeof buf);
937 }
938 if (ntransmitted) {
939 if (nreceived > ntransmitted)
940 snprintf(buft, sizeof buft,
941 "-- somebody's duplicating packets!");
942 else
943 snprintf(buft, sizeof buft, "%.1f%% packet loss",
944 ((((double)ntransmitted - nreceived) * 100) /
945 ntransmitted));
946 strlcat(buf, buft, sizeof buf);
947 }
948 strlcat(buf, "\n", sizeof buf);
949 if (nreceived && timing) {
950 quad_t num = nreceived + nrepeats;
951 quad_t avg = tsum / num;
952 quad_t dev = qsqrt(tsumsq / num - avg * avg);
953
954 snprintf(buft, sizeof buft, "round-trip min/avg/max/std-dev = "
955 "%d.%03d/%d.%03d/%d.%03d/%d.%03d ms\n",
956 (int)(tmin / 1000), (int)(tmin % 1000),
957 (int)(avg / 1000), (int)(avg % 1000),
958 (int)(tmax / 1000), (int)(tmax % 1000),
959 (int)(dev / 1000), (int)(dev % 1000));
960 strlcat(buf, buft, sizeof buf);
961 }
962 write(STDOUT_FILENO, buf, strlen(buf)); /* XXX atomicio? */
963 }
964
965 quad_t
qsqrt(quad_t qdev)966 qsqrt(quad_t qdev)
967 {
968 quad_t y, x = 1;
969
970 if (!qdev)
971 return(0);
972
973 do { /* newton was a stinker */
974 y = x;
975 x = qdev / x;
976 x += y;
977 x /= 2;
978 } while ((x - y) > 1 || (x - y) < -1);
979
980 return(x);
981 }
982
983 /*
984 * finish --
985 * Print out statistics, and give up.
986 */
987 void
finish(int signo)988 finish(int signo)
989 {
990 (void)signal(SIGINT, SIG_IGN);
991
992 summary(1, 0);
993 if (signo)
994 _exit(nreceived ? 0 : 1);
995 else
996 exit(nreceived ? 0 : 1);
997 }
998
999 #ifdef notdef
1000 static char *ttab[] = {
1001 "Echo Reply", /* ip + seq + udata */
1002 "Dest Unreachable", /* net, host, proto, port, frag, sr + IP */
1003 "Source Quench", /* IP */
1004 "Redirect", /* redirect type, gateway, + IP */
1005 "Echo",
1006 "Time Exceeded", /* transit, frag reassem + IP */
1007 "Parameter Problem", /* pointer + IP */
1008 "Timestamp", /* id + seq + three timestamps */
1009 "Timestamp Reply", /* " */
1010 "Info Request", /* id + sq */
1011 "Info Reply" /* " */
1012 };
1013 #endif
1014
1015 /*
1016 * pr_icmph --
1017 * Print a descriptive string about an ICMP header.
1018 */
1019 void
pr_icmph(struct icmp * icp)1020 pr_icmph(struct icmp *icp)
1021 {
1022 switch(icp->icmp_type) {
1023 case ICMP_ECHOREPLY:
1024 (void)printf("Echo Reply\n");
1025 /* XXX ID + Seq + Data */
1026 break;
1027 case ICMP_UNREACH:
1028 switch(icp->icmp_code) {
1029 case ICMP_UNREACH_NET:
1030 (void)printf("Destination Net Unreachable\n");
1031 break;
1032 case ICMP_UNREACH_HOST:
1033 (void)printf("Destination Host Unreachable\n");
1034 break;
1035 case ICMP_UNREACH_PROTOCOL:
1036 (void)printf("Destination Protocol Unreachable\n");
1037 break;
1038 case ICMP_UNREACH_PORT:
1039 (void)printf("Destination Port Unreachable\n");
1040 break;
1041 case ICMP_UNREACH_NEEDFRAG:
1042 if (icp->icmp_nextmtu != 0)
1043 (void)printf("frag needed and DF set (MTU %d)\n",
1044 ntohs(icp->icmp_nextmtu));
1045 else
1046 (void)printf("frag needed and DF set\n");
1047 break;
1048 case ICMP_UNREACH_SRCFAIL:
1049 (void)printf("Source Route Failed\n");
1050 break;
1051 case ICMP_UNREACH_NET_UNKNOWN:
1052 (void)printf("Network Unknown\n");
1053 break;
1054 case ICMP_UNREACH_HOST_UNKNOWN:
1055 (void)printf("Host Unknown\n");
1056 break;
1057 case ICMP_UNREACH_ISOLATED:
1058 (void)printf("Source Isolated\n");
1059 break;
1060 case ICMP_UNREACH_NET_PROHIB:
1061 (void)printf("Dest. Net Administratively Prohibited\n");
1062 break;
1063 case ICMP_UNREACH_HOST_PROHIB:
1064 (void)printf("Dest. Host Administratively Prohibited\n");
1065 break;
1066 case ICMP_UNREACH_TOSNET:
1067 (void)printf("Destination Net Unreachable for TOS\n");
1068 break;
1069 case ICMP_UNREACH_TOSHOST:
1070 (void)printf("Desination Host Unreachable for TOS\n");
1071 break;
1072 case ICMP_UNREACH_FILTER_PROHIB:
1073 (void)printf("Route administratively prohibited\n");
1074 break;
1075 case ICMP_UNREACH_HOST_PRECEDENCE:
1076 (void)printf("Host Precedence Violation\n");
1077 break;
1078 case ICMP_UNREACH_PRECEDENCE_CUTOFF:
1079 (void)printf("Precedence Cutoff\n");
1080 break;
1081 default:
1082 (void)printf("Dest Unreachable, Unknown Code: %d\n",
1083 icp->icmp_code);
1084 break;
1085 }
1086 /* Print returned IP header information */
1087 #ifndef icmp_data
1088 pr_retip(&icp->icmp_ip);
1089 #else
1090 pr_retip((struct ip *)icp->icmp_data);
1091 #endif
1092 break;
1093 case ICMP_SOURCEQUENCH:
1094 (void)printf("Source Quench\n");
1095 #ifndef icmp_data
1096 pr_retip(&icp->icmp_ip);
1097 #else
1098 pr_retip((struct ip *)icp->icmp_data);
1099 #endif
1100 break;
1101 case ICMP_REDIRECT:
1102 switch(icp->icmp_code) {
1103 case ICMP_REDIRECT_NET:
1104 (void)printf("Redirect Network");
1105 break;
1106 case ICMP_REDIRECT_HOST:
1107 (void)printf("Redirect Host");
1108 break;
1109 case ICMP_REDIRECT_TOSNET:
1110 (void)printf("Redirect Type of Service and Network");
1111 break;
1112 case ICMP_REDIRECT_TOSHOST:
1113 (void)printf("Redirect Type of Service and Host");
1114 break;
1115 default:
1116 (void)printf("Redirect, Unknown Code: %d", icp->icmp_code);
1117 break;
1118 }
1119 (void)printf("(New addr: %s)\n",
1120 inet_ntoa(icp->icmp_gwaddr));
1121 #ifndef icmp_data
1122 pr_retip(&icp->icmp_ip);
1123 #else
1124 pr_retip((struct ip *)icp->icmp_data);
1125 #endif
1126 break;
1127 case ICMP_ECHO:
1128 (void)printf("Echo Request\n");
1129 /* XXX ID + Seq + Data */
1130 break;
1131 case ICMP_ROUTERADVERT:
1132 /* RFC1256 */
1133 (void)printf("Router Discovery Advertisement\n");
1134 (void)printf("(%d entries, lifetime %d seconds)\n",
1135 icp->icmp_num_addrs, ntohs(icp->icmp_lifetime));
1136 break;
1137 case ICMP_ROUTERSOLICIT:
1138 /* RFC1256 */
1139 (void)printf("Router Discovery Solicitation\n");
1140 break;
1141 case ICMP_TIMXCEED:
1142 switch(icp->icmp_code) {
1143 case ICMP_TIMXCEED_INTRANS:
1144 (void)printf("Time to live exceeded\n");
1145 break;
1146 case ICMP_TIMXCEED_REASS:
1147 (void)printf("Frag reassembly time exceeded\n");
1148 break;
1149 default:
1150 (void)printf("Time exceeded, Unknown Code: %d\n",
1151 icp->icmp_code);
1152 break;
1153 }
1154 #ifndef icmp_data
1155 pr_retip(&icp->icmp_ip);
1156 #else
1157 pr_retip((struct ip *)icp->icmp_data);
1158 #endif
1159 break;
1160 case ICMP_PARAMPROB:
1161 switch(icp->icmp_code) {
1162 case ICMP_PARAMPROB_OPTABSENT:
1163 (void)printf("Parameter problem, required option "
1164 "absent: pointer = 0x%02x\n",
1165 ntohs(icp->icmp_hun.ih_pptr));
1166 break;
1167 default:
1168 (void)printf("Parameter problem: pointer = 0x%02x\n",
1169 ntohs(icp->icmp_hun.ih_pptr));
1170 break;
1171 }
1172 #ifndef icmp_data
1173 pr_retip(&icp->icmp_ip);
1174 #else
1175 pr_retip((struct ip *)icp->icmp_data);
1176 #endif
1177 break;
1178 case ICMP_TSTAMP:
1179 (void)printf("Timestamp\n");
1180 /* XXX ID + Seq + 3 timestamps */
1181 break;
1182 case ICMP_TSTAMPREPLY:
1183 (void)printf("Timestamp Reply\n");
1184 /* XXX ID + Seq + 3 timestamps */
1185 break;
1186 case ICMP_IREQ:
1187 (void)printf("Information Request\n");
1188 /* XXX ID + Seq */
1189 break;
1190 case ICMP_IREQREPLY:
1191 (void)printf("Information Reply\n");
1192 /* XXX ID + Seq */
1193 break;
1194 #ifdef ICMP_MASKREQ
1195 case ICMP_MASKREQ:
1196 (void)printf("Address Mask Request\n");
1197 break;
1198 #endif
1199 #ifdef ICMP_MASKREPLY
1200 case ICMP_MASKREPLY:
1201 (void)printf("Address Mask Reply (Mask 0x%08x)\n",
1202 ntohl(icp->icmp_mask));
1203 break;
1204 #endif
1205 default:
1206 (void)printf("Unknown ICMP type: %d\n", icp->icmp_type);
1207 }
1208 }
1209
1210 /*
1211 * pr_iph --
1212 * Print an IP header with options.
1213 */
1214 void
pr_iph(struct ip * ip)1215 pr_iph(struct ip *ip)
1216 {
1217 int hlen;
1218 u_char *cp;
1219
1220 hlen = ip->ip_hl << 2;
1221 cp = (u_char *)ip + 20; /* point to options */
1222
1223 (void)printf("Vr HL TOS Len ID Flg off TTL Pro cks Src Dst Data\n");
1224 (void)printf(" %1x %1x %02x %04x %04x",
1225 ip->ip_v, ip->ip_hl, ip->ip_tos, ip->ip_len, ip->ip_id);
1226 (void)printf(" %1x %04x", ((ip->ip_off) & 0xe000) >> 13,
1227 (ip->ip_off) & 0x1fff);
1228 (void)printf(" %02x %02x %04x", ip->ip_ttl, ip->ip_p, ip->ip_sum);
1229 (void)printf(" %s ", inet_ntoa(*(struct in_addr *)&ip->ip_src.s_addr));
1230 (void)printf(" %s ", inet_ntoa(*(struct in_addr *)&ip->ip_dst.s_addr));
1231 /* dump and option bytes */
1232 while (hlen-- > 20) {
1233 (void)printf("%02x", *cp++);
1234 }
1235 (void)putchar('\n');
1236 }
1237
1238 /*
1239 * pr_addr --
1240 * Return an ascii host address as a dotted quad and optionally with
1241 * a hostname.
1242 */
1243 char *
pr_addr(in_addr_t a)1244 pr_addr(in_addr_t a)
1245 {
1246 struct hostent *hp;
1247 struct in_addr in;
1248 static char buf[16+3+MAXHOSTNAMELEN];
1249
1250 in.s_addr = a;
1251 if ((options & F_NUMERIC) ||
1252 !(hp = gethostbyaddr((char *)&in.s_addr, sizeof(in.s_addr), AF_INET)))
1253 (void)snprintf(buf, sizeof buf, "%s", inet_ntoa(in));
1254 else
1255 (void)snprintf(buf, sizeof buf, "%s (%s)", hp->h_name,
1256 inet_ntoa(in));
1257 return(buf);
1258 }
1259
1260 /*
1261 * pr_retip --
1262 * Dump some info on a returned (via ICMP) IP packet.
1263 */
1264 void
pr_retip(struct ip * ip)1265 pr_retip(struct ip *ip)
1266 {
1267 int hlen;
1268 u_char *cp;
1269
1270 pr_iph(ip);
1271 hlen = ip->ip_hl << 2;
1272 cp = (u_char *)ip + hlen;
1273
1274 if (ip->ip_p == 6)
1275 (void)printf("TCP: from port %u, to port %u (decimal)\n",
1276 (*cp * 256 + *(cp + 1)), (*(cp + 2) * 256 + *(cp + 3)));
1277 else if (ip->ip_p == 17)
1278 (void)printf("UDP: from port %u, to port %u (decimal)\n",
1279 (*cp * 256 + *(cp + 1)), (*(cp + 2) * 256 + *(cp + 3)));
1280 }
1281
1282 void
fill(char * bp,char * patp)1283 fill(char *bp, char *patp)
1284 {
1285 int ii, jj, kk;
1286 int pat[16];
1287 char *cp;
1288
1289 for (cp = patp; *cp; cp++)
1290 if (!isxdigit(*cp))
1291 errx(1, "patterns must be specified as hex digits");
1292 ii = sscanf(patp,
1293 "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x",
1294 &pat[0], &pat[1], &pat[2], &pat[3], &pat[4], &pat[5], &pat[6],
1295 &pat[7], &pat[8], &pat[9], &pat[10], &pat[11], &pat[12],
1296 &pat[13], &pat[14], &pat[15]);
1297
1298 if (ii > 0)
1299 for (kk = 0;
1300 kk <= MAXPAYLOAD - (8 + sizeof(struct tvi) + ii);
1301 kk += ii)
1302 for (jj = 0; jj < ii; ++jj)
1303 bp[jj + kk] = pat[jj];
1304 if (!(options & F_QUIET)) {
1305 (void)printf("PATTERN: 0x");
1306 for (jj = 0; jj < ii; ++jj)
1307 (void)printf("%02x", bp[jj] & 0xFF);
1308 (void)printf("\n");
1309 }
1310 }
1311
1312 /*
1313 * when we get types of ICMP message with parts of the orig. datagram
1314 * we want to try to assure ourselves that it is from this instance
1315 * of ping, and not say, a refused finger connection or something
1316 */
1317 int
check_icmph(struct ip * iph)1318 check_icmph(struct ip *iph)
1319 {
1320 struct icmp *icmph;
1321
1322 /* only allow IP version 4 */
1323 if (iph->ip_v != 4)
1324 return 0;
1325
1326 /* Only allow ICMP */
1327 if (iph->ip_p != IPPROTO_ICMP)
1328 return 0;
1329
1330 icmph = (struct icmp *) (iph + (4 * iph->ip_hl));
1331
1332 /* make sure it is in response to an ECHO request */
1333 if (icmph->icmp_type != 8)
1334 return 0;
1335
1336 /* ok, make sure it has the right id on it */
1337 if (icmph->icmp_hun.ih_idseq.icd_id != ident)
1338 return 0;
1339
1340 return 1;
1341 }
1342
1343 void
usage(void)1344 usage(void)
1345 {
1346 (void)fprintf(stderr,
1347 "usage: ping [-DdfjLnqRrv] [-c count] [-I ifaddr] [-i wait]\n"
1348 "\t[-l preload] [-p pattern] [-s packetsize] [-T tos] [-t ttl]\n"
1349 "\t[-w maxwait] host\n");
1350 exit(1);
1351 }
1352