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