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