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