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