1 /* 2 * Copyright (c) 1989 The Regents of the University of California. 3 * 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 char copyright[] = 13 "@(#) Copyright (c) 1989 The Regents of the University of California.\n\ 14 All rights reserved.\n"; 15 #endif /* not lint */ 16 17 #ifndef lint 18 static char sccsid[] = "@(#)ping.c 5.8 (Berkeley) 03/05/91"; 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 long tmin = LONG_MAX; /* minimum round trip time */ 110 long tmax; /* maximum round trip time */ 111 u_long tsum; /* 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: you must be root to use the -f option.\n"); 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 bzero((char *)&whereto, 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 bcopy(hp->h_addr, (caddr_t)&to->sin_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_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 long 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 = tv.tv_sec * 1000 + (tv.tv_usec / 1000); 472 tsum += triptime; 473 if (triptime < tmin) 474 tmin = triptime; 475 if (triptime > tmax) 476 tmax = triptime; 477 } 478 479 if (TST(icp->icmp_seq % mx_dup_ck)) { 480 ++nrepeats; 481 --nreceived; 482 dupflag = 1; 483 } else { 484 SET(icp->icmp_seq % mx_dup_ck); 485 dupflag = 0; 486 } 487 488 if (options & F_QUIET) 489 return; 490 491 if (options & F_FLOOD) 492 (void)write(STDOUT_FILENO, &BSPACE, 1); 493 else { 494 (void)printf("%d bytes from %s: icmp_seq=%u", cc, 495 inet_ntoa(*(struct in_addr *)&from->sin_addr.s_addr), 496 icp->icmp_seq); 497 (void)printf(" ttl=%d", ip->ip_ttl); 498 if (timing) 499 (void)printf(" time=%ld ms", triptime); 500 if (dupflag) 501 (void)printf(" (DUP!)"); 502 /* check the data */ 503 cp = (u_char*)&icp->icmp_data[8]; 504 dp = &outpack[8 + sizeof(struct timeval)]; 505 for (i = 8; i < datalen; ++i, ++cp, ++dp) { 506 if (*cp != *dp) { 507 (void)printf("\nwrong data byte #%d should be 0x%x but was 0x%x", 508 i, *dp, *cp); 509 cp = (u_char*)&icp->icmp_data[0]; 510 for (i = 8; i < datalen; ++i, ++cp) { 511 if ((i % 32) == 8) 512 (void)printf("\n\t"); 513 (void)printf("%x ", *cp); 514 } 515 break; 516 } 517 } 518 } 519 } else { 520 /* We've got something other than an ECHOREPLY */ 521 if (!(options & F_VERBOSE)) 522 return; 523 (void)printf("%d bytes from %s: ", cc, 524 pr_addr(from->sin_addr.s_addr)); 525 pr_icmph(icp); 526 } 527 528 /* Display any IP options */ 529 cp = (u_char *)buf + sizeof(struct ip); 530 531 for (; hlen > (int)sizeof(struct ip); --hlen, ++cp) 532 switch (*cp) { 533 case IPOPT_EOL: 534 hlen = 0; 535 break; 536 case IPOPT_LSRR: 537 (void)printf("\nLSRR: "); 538 hlen -= 2; 539 j = *++cp; 540 ++cp; 541 if (j > IPOPT_MINOFF) 542 for (;;) { 543 l = *++cp; 544 l = (l<<8) + *++cp; 545 l = (l<<8) + *++cp; 546 l = (l<<8) + *++cp; 547 if (l == 0) 548 (void)printf("\t0.0.0.0"); 549 else 550 (void)printf("\t%s", pr_addr(ntohl(l))); 551 hlen -= 4; 552 j -= 4; 553 if (j <= IPOPT_MINOFF) 554 break; 555 (void)putchar('\n'); 556 } 557 break; 558 case IPOPT_RR: 559 j = *++cp; /* get length */ 560 i = *++cp; /* and pointer */ 561 hlen -= 2; 562 if (i > j) 563 i = j; 564 i -= IPOPT_MINOFF; 565 if (i <= 0) 566 continue; 567 if (i == old_rrlen 568 && cp == (u_char *)buf + sizeof(struct ip) + 2 569 && !bcmp((char *)cp, old_rr, i) 570 && !(options & F_FLOOD)) { 571 (void)printf("\t(same route)"); 572 i = ((i + 3) / 4) * 4; 573 hlen -= i; 574 cp += i; 575 break; 576 } 577 old_rrlen = i; 578 bcopy((char *)cp, old_rr, i); 579 (void)printf("\nRR: "); 580 for (;;) { 581 l = *++cp; 582 l = (l<<8) + *++cp; 583 l = (l<<8) + *++cp; 584 l = (l<<8) + *++cp; 585 if (l == 0) 586 (void)printf("\t0.0.0.0"); 587 else 588 (void)printf("\t%s", pr_addr(ntohl(l))); 589 hlen -= 4; 590 i -= 4; 591 if (i <= 0) 592 break; 593 (void)putchar('\n'); 594 } 595 break; 596 case IPOPT_NOP: 597 (void)printf("\nNOP"); 598 break; 599 default: 600 (void)printf("\nunknown option %x", *cp); 601 break; 602 } 603 if (!(options & F_FLOOD)) { 604 (void)putchar('\n'); 605 (void)fflush(stdout); 606 } 607 } 608 609 /* 610 * in_cksum -- 611 * Checksum routine for Internet Protocol family headers (C Version) 612 */ 613 in_cksum(addr, len) 614 u_short *addr; 615 int len; 616 { 617 register int nleft = len; 618 register u_short *w = addr; 619 register int sum = 0; 620 u_short answer = 0; 621 622 /* 623 * Our algorithm is simple, using a 32 bit accumulator (sum), we add 624 * sequential 16 bit words to it, and at the end, fold back all the 625 * carry bits from the top 16 bits into the lower 16 bits. 626 */ 627 while (nleft > 1) { 628 sum += *w++; 629 nleft -= 2; 630 } 631 632 /* mop up an odd byte, if necessary */ 633 if (nleft == 1) { 634 *(u_char *)(&answer) = *(u_char *)w ; 635 sum += answer; 636 } 637 638 /* add back carry outs from top 16 bits to low 16 bits */ 639 sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */ 640 sum += (sum >> 16); /* add carry */ 641 answer = ~sum; /* truncate to 16 bits */ 642 return(answer); 643 } 644 645 /* 646 * tvsub -- 647 * Subtract 2 timeval structs: out = out - in. Out is assumed to 648 * be >= in. 649 */ 650 tvsub(out, in) 651 register struct timeval *out, *in; 652 { 653 if ((out->tv_usec -= in->tv_usec) < 0) { 654 --out->tv_sec; 655 out->tv_usec += 1000000; 656 } 657 out->tv_sec -= in->tv_sec; 658 } 659 660 /* 661 * finish -- 662 * Print out statistics, and give up. 663 */ 664 void 665 finish() 666 { 667 (void)signal(SIGINT, SIG_IGN); 668 (void)putchar('\n'); 669 (void)fflush(stdout); 670 (void)printf("--- %s ping statistics ---\n", hostname); 671 (void)printf("%ld packets transmitted, ", ntransmitted); 672 (void)printf("%ld packets received, ", nreceived); 673 if (nrepeats) 674 (void)printf("+%ld duplicates, ", nrepeats); 675 if (ntransmitted) 676 if (nreceived > ntransmitted) 677 (void)printf("-- somebody's printing up packets!"); 678 else 679 (void)printf("%d%% packet loss", 680 (int) (((ntransmitted - nreceived) * 100) / 681 ntransmitted)); 682 (void)putchar('\n'); 683 if (nreceived && timing) 684 (void)printf("round-trip min/avg/max = %ld/%lu/%ld ms\n", 685 tmin, tsum / (nreceived + nrepeats), tmax); 686 exit(0); 687 } 688 689 #ifdef notdef 690 static char *ttab[] = { 691 "Echo Reply", /* ip + seq + udata */ 692 "Dest Unreachable", /* net, host, proto, port, frag, sr + IP */ 693 "Source Quench", /* IP */ 694 "Redirect", /* redirect type, gateway, + IP */ 695 "Echo", 696 "Time Exceeded", /* transit, frag reassem + IP */ 697 "Parameter Problem", /* pointer + IP */ 698 "Timestamp", /* id + seq + three timestamps */ 699 "Timestamp Reply", /* " */ 700 "Info Request", /* id + sq */ 701 "Info Reply" /* " */ 702 }; 703 #endif 704 705 /* 706 * pr_icmph -- 707 * Print a descriptive string about an ICMP header. 708 */ 709 pr_icmph(icp) 710 struct icmp *icp; 711 { 712 switch(icp->icmp_type) { 713 case ICMP_ECHOREPLY: 714 (void)printf("Echo Reply\n"); 715 /* XXX ID + Seq + Data */ 716 break; 717 case ICMP_UNREACH: 718 switch(icp->icmp_code) { 719 case ICMP_UNREACH_NET: 720 (void)printf("Destination Net Unreachable\n"); 721 break; 722 case ICMP_UNREACH_HOST: 723 (void)printf("Destination Host Unreachable\n"); 724 break; 725 case ICMP_UNREACH_PROTOCOL: 726 (void)printf("Destination Protocol Unreachable\n"); 727 break; 728 case ICMP_UNREACH_PORT: 729 (void)printf("Destination Port Unreachable\n"); 730 break; 731 case ICMP_UNREACH_NEEDFRAG: 732 (void)printf("frag needed and DF set\n"); 733 break; 734 case ICMP_UNREACH_SRCFAIL: 735 (void)printf("Source Route Failed\n"); 736 break; 737 default: 738 (void)printf("Dest Unreachable, Bad Code: %d\n", 739 icp->icmp_code); 740 break; 741 } 742 /* Print returned IP header information */ 743 #ifndef icmp_data 744 pr_retip(&icp->icmp_ip); 745 #else 746 pr_retip((struct ip *)icp->icmp_data); 747 #endif 748 break; 749 case ICMP_SOURCEQUENCH: 750 (void)printf("Source Quench\n"); 751 #ifndef icmp_data 752 pr_retip(&icp->icmp_ip); 753 #else 754 pr_retip((struct ip *)icp->icmp_data); 755 #endif 756 break; 757 case ICMP_REDIRECT: 758 switch(icp->icmp_code) { 759 case ICMP_REDIRECT_NET: 760 (void)printf("Redirect Network"); 761 break; 762 case ICMP_REDIRECT_HOST: 763 (void)printf("Redirect Host"); 764 break; 765 case ICMP_REDIRECT_TOSNET: 766 (void)printf("Redirect Type of Service and Network"); 767 break; 768 case ICMP_REDIRECT_TOSHOST: 769 (void)printf("Redirect Type of Service and Host"); 770 break; 771 default: 772 (void)printf("Redirect, Bad Code: %d", icp->icmp_code); 773 break; 774 } 775 (void)printf("(New addr: 0x%08lx)\n", icp->icmp_gwaddr.s_addr); 776 #ifndef icmp_data 777 pr_retip(&icp->icmp_ip); 778 #else 779 pr_retip((struct ip *)icp->icmp_data); 780 #endif 781 break; 782 case ICMP_ECHO: 783 (void)printf("Echo Request\n"); 784 /* XXX ID + Seq + Data */ 785 break; 786 case ICMP_TIMXCEED: 787 switch(icp->icmp_code) { 788 case ICMP_TIMXCEED_INTRANS: 789 (void)printf("Time to live exceeded\n"); 790 break; 791 case ICMP_TIMXCEED_REASS: 792 (void)printf("Frag reassembly time exceeded\n"); 793 break; 794 default: 795 (void)printf("Time exceeded, Bad Code: %d\n", 796 icp->icmp_code); 797 break; 798 } 799 #ifndef icmp_data 800 pr_retip(&icp->icmp_ip); 801 #else 802 pr_retip((struct ip *)icp->icmp_data); 803 #endif 804 break; 805 case ICMP_PARAMPROB: 806 (void)printf("Parameter problem: pointer = 0x%02x\n", 807 icp->icmp_hun.ih_pptr); 808 #ifndef icmp_data 809 pr_retip(&icp->icmp_ip); 810 #else 811 pr_retip((struct ip *)icp->icmp_data); 812 #endif 813 break; 814 case ICMP_TSTAMP: 815 (void)printf("Timestamp\n"); 816 /* XXX ID + Seq + 3 timestamps */ 817 break; 818 case ICMP_TSTAMPREPLY: 819 (void)printf("Timestamp Reply\n"); 820 /* XXX ID + Seq + 3 timestamps */ 821 break; 822 case ICMP_IREQ: 823 (void)printf("Information Request\n"); 824 /* XXX ID + Seq */ 825 break; 826 case ICMP_IREQREPLY: 827 (void)printf("Information Reply\n"); 828 /* XXX ID + Seq */ 829 break; 830 #ifdef ICMP_MASKREQ 831 case ICMP_MASKREQ: 832 (void)printf("Address Mask Request\n"); 833 break; 834 #endif 835 #ifdef ICMP_MASKREPLY 836 case ICMP_MASKREPLY: 837 (void)printf("Address Mask Reply\n"); 838 break; 839 #endif 840 default: 841 (void)printf("Bad ICMP type: %d\n", icp->icmp_type); 842 } 843 } 844 845 /* 846 * pr_iph -- 847 * Print an IP header with options. 848 */ 849 pr_iph(ip) 850 struct ip *ip; 851 { 852 int hlen; 853 u_char *cp; 854 855 hlen = ip->ip_hl << 2; 856 cp = (u_char *)ip + 20; /* point to options */ 857 858 (void)printf("Vr HL TOS Len ID Flg off TTL Pro cks Src Dst Data\n"); 859 (void)printf(" %1x %1x %02x %04x %04x", 860 ip->ip_v, ip->ip_hl, ip->ip_tos, ip->ip_len, ip->ip_id); 861 (void)printf(" %1x %04x", ((ip->ip_off) & 0xe000) >> 13, 862 (ip->ip_off) & 0x1fff); 863 (void)printf(" %02x %02x %04x", ip->ip_ttl, ip->ip_p, ip->ip_sum); 864 (void)printf(" %s ", inet_ntoa(*(struct in_addr *)&ip->ip_src.s_addr)); 865 (void)printf(" %s ", inet_ntoa(*(struct in_addr *)&ip->ip_dst.s_addr)); 866 /* dump and option bytes */ 867 while (hlen-- > 20) { 868 (void)printf("%02x", *cp++); 869 } 870 (void)putchar('\n'); 871 } 872 873 /* 874 * pr_addr -- 875 * Return an ascii host address as a dotted quad and optionally with 876 * a hostname. 877 */ 878 char * 879 pr_addr(l) 880 u_long l; 881 { 882 struct hostent *hp; 883 static char buf[80]; 884 885 if ((options & F_NUMERIC) || 886 !(hp = gethostbyaddr((char *)&l, 4, AF_INET))) 887 (void)sprintf(buf, "%s", inet_ntoa(*(struct in_addr *)&l)); 888 else 889 (void)sprintf(buf, "%s (%s)", hp->h_name, 890 inet_ntoa(*(struct in_addr *)&l)); 891 return(buf); 892 } 893 894 /* 895 * pr_retip -- 896 * Dump some info on a returned (via ICMP) IP packet. 897 */ 898 pr_retip(ip) 899 struct ip *ip; 900 { 901 int hlen; 902 u_char *cp; 903 904 pr_iph(ip); 905 hlen = ip->ip_hl << 2; 906 cp = (u_char *)ip + hlen; 907 908 if (ip->ip_p == 6) 909 (void)printf("TCP: from port %u, to port %u (decimal)\n", 910 (*cp * 256 + *(cp + 1)), (*(cp + 2) * 256 + *(cp + 3))); 911 else if (ip->ip_p == 17) 912 (void)printf("UDP: from port %u, to port %u (decimal)\n", 913 (*cp * 256 + *(cp + 1)), (*(cp + 2) * 256 + *(cp + 3))); 914 } 915 916 fill(bp, patp) 917 char *bp, *patp; 918 { 919 register int ii, jj, kk; 920 int pat[16]; 921 char *cp; 922 923 for (cp = patp; *cp; cp++) 924 if (!isxdigit(*cp)) { 925 (void)fprintf(stderr, 926 "ping: patterns must be specified as hex digits.\n"); 927 exit(1); 928 } 929 ii = sscanf(patp, 930 "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x", 931 &pat[0], &pat[1], &pat[2], &pat[3], &pat[4], &pat[5], &pat[6], 932 &pat[7], &pat[8], &pat[9], &pat[10], &pat[11], &pat[12], 933 &pat[13], &pat[14], &pat[15]); 934 935 if (ii > 0) 936 for (kk = 0; kk <= MAXPACKET - (8 + ii); kk += ii) 937 for (jj = 0; jj < ii; ++jj) 938 bp[jj + kk] = pat[jj]; 939 if (!(options & F_QUIET)) { 940 (void)printf("PATTERN: 0x"); 941 for (jj = 0; jj < ii; ++jj) 942 (void)printf("%02x", bp[jj] & 0xFF); 943 (void)printf("\n"); 944 } 945 } 946 947 usage() 948 { 949 (void)fprintf(stderr, 950 "usage: ping [-Rdfnqrv] [-c count] [-i wait] [-l preload]\n\t[-p pattern] [-s packetsize] host\n"); 951 exit(1); 952 } 953