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