ping.c revision 1.115 1 1.115 joerg /* $NetBSD: ping.c,v 1.115 2017/01/11 12:13:52 joerg 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.73 agc * 3. Neither the name of the University nor the names of its contributors
19 1.1 cgd * may be used to endorse or promote products derived from this software
20 1.1 cgd * without specific prior written permission.
21 1.1 cgd *
22 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.1 cgd * SUCH DAMAGE.
33 1.1 cgd */
34 1.41 christos
35 1.1 cgd /*
36 1.1 cgd * P I N G . C
37 1.1 cgd *
38 1.30 christos * Using the InterNet Control Message Protocol (ICMP) "ECHO" facility,
39 1.1 cgd * measure round-trip-delays and packet loss across network paths.
40 1.1 cgd *
41 1.1 cgd * Author -
42 1.1 cgd * Mike Muuss
43 1.1 cgd * U. S. Army Ballistic Research Laboratory
44 1.1 cgd * December, 1983
45 1.22 christos * Modified at Uc Berkeley
46 1.22 christos * Record Route and verbose headers - Phil Dykstra, BRL, March 1988.
47 1.22 christos * Multicast options (ttl, if, loop) - Steve Deering, Stanford, August 1988.
48 1.22 christos * ttl, duplicate detection - Cliff Frost, UCB, April 1989
49 1.22 christos * Pad pattern - Cliff Frost (from Tom Ferrin, UCSF), April 1989
50 1.1 cgd *
51 1.1 cgd * Status -
52 1.1 cgd * Public Domain. Distribution Unlimited.
53 1.22 christos *
54 1.1 cgd * Bugs -
55 1.1 cgd * More statistics could always be gathered.
56 1.1 cgd * This program has to run SUID to ROOT to access the ICMP socket.
57 1.1 cgd */
58 1.1 cgd
59 1.32 lukem #include <sys/cdefs.h>
60 1.22 christos #ifndef lint
61 1.115 joerg __RCSID("$NetBSD: ping.c,v 1.115 2017/01/11 12:13:52 joerg Exp $");
62 1.22 christos #endif
63 1.22 christos
64 1.22 christos #include <stdio.h>
65 1.60 yamt #include <stddef.h>
66 1.22 christos #include <errno.h>
67 1.81 christos #include <signal.h>
68 1.22 christos #include <sys/time.h>
69 1.22 christos #include <sys/types.h>
70 1.1 cgd #include <sys/param.h>
71 1.1 cgd #include <sys/socket.h>
72 1.1 cgd #include <sys/file.h>
73 1.41 christos #include <termios.h>
74 1.22 christos #include <stdlib.h>
75 1.22 christos #include <unistd.h>
76 1.67 mycroft #include <poll.h>
77 1.22 christos #include <limits.h>
78 1.44 jwise #include <math.h>
79 1.22 christos #include <string.h>
80 1.22 christos #include <err.h>
81 1.1 cgd
82 1.1 cgd #include <netinet/in_systm.h>
83 1.1 cgd #include <netinet/in.h>
84 1.1 cgd #include <netinet/ip.h>
85 1.1 cgd #include <netinet/ip_icmp.h>
86 1.1 cgd #include <netinet/ip_var.h>
87 1.12 cgd #include <arpa/inet.h>
88 1.22 christos #include <ctype.h>
89 1.1 cgd #include <netdb.h>
90 1.1 cgd
91 1.50 itojun #ifdef IPSEC
92 1.102 drochner #include <netipsec/ipsec.h>
93 1.50 itojun #endif /*IPSEC*/
94 1.50 itojun
95 1.92 pooka #include "prog_ops.h"
96 1.91 pooka
97 1.22 christos #define FLOOD_INTVL 0.01 /* default flood output interval */
98 1.61 yamt #define MAXPACKET (IP_MAXPACKET-60-8) /* max packet size */
99 1.7 hpeyerl
100 1.22 christos #define F_VERBOSE 0x0001
101 1.22 christos #define F_QUIET 0x0002 /* minimize all output */
102 1.22 christos #define F_SEMI_QUIET 0x0004 /* ignore our ICMP errors */
103 1.22 christos #define F_FLOOD 0x0008 /* flood-ping */
104 1.22 christos #define F_RECORD_ROUTE 0x0010 /* record route */
105 1.22 christos #define F_SOURCE_ROUTE 0x0020 /* loose source route */
106 1.22 christos #define F_PING_FILLED 0x0040 /* is buffer filled with user data? */
107 1.22 christos #define F_PING_RANDOM 0x0080 /* use random data */
108 1.22 christos #define F_NUMERIC 0x0100 /* do not do gethostbyaddr() calls */
109 1.22 christos #define F_TIMING 0x0200 /* room for a timestamp */
110 1.30 christos #define F_DF 0x0400 /* set IP DF bit */
111 1.30 christos #define F_SOURCE_ADDR 0x0800 /* set source IP address/interface */
112 1.30 christos #define F_ONCE 0x1000 /* exit(0) after receiving 1 reply */
113 1.30 christos #define F_MCAST 0x2000 /* multicast target */
114 1.30 christos #define F_MCAST_NOLOOP 0x4000 /* no multicast loopback */
115 1.49 sommerfe #define F_AUDIBLE 0x8000 /* audible output */
116 1.99 christos #define F_TIMING64 0x10000 /* 64 bit time, nanoseconds */
117 1.50 itojun #ifdef IPSEC
118 1.50 itojun #ifdef IPSEC_POLICY_IPSEC
119 1.103 msaitoh #define F_POLICY 0x20000
120 1.50 itojun #else
121 1.103 msaitoh #define F_AUTHHDR 0x20000
122 1.103 msaitoh #define F_ENCRYPT 0x40000
123 1.50 itojun #endif /*IPSEC_POLICY_IPSEC*/
124 1.50 itojun #endif /*IPSEC*/
125 1.22 christos
126 1.41 christos
127 1.22 christos /* MAX_DUP_CHK is the number of bits in received table, the
128 1.22 christos * maximum number of received sequence numbers we can track to check
129 1.22 christos * for duplicates.
130 1.1 cgd */
131 1.22 christos #define MAX_DUP_CHK (8 * 2048)
132 1.98 joerg static u_char rcvd_tbl[MAX_DUP_CHK/8];
133 1.98 joerg static int nrepeats = 0;
134 1.22 christos #define A(seq) rcvd_tbl[(seq/8)%sizeof(rcvd_tbl)] /* byte in array */
135 1.22 christos #define B(seq) (1 << (seq & 0x07)) /* bit in byte */
136 1.22 christos #define SET(seq) (A(seq) |= B(seq))
137 1.22 christos #define CLR(seq) (A(seq) &= (~B(seq)))
138 1.22 christos #define TST(seq) (A(seq) & B(seq))
139 1.22 christos
140 1.75 itojun struct tv32 {
141 1.75 itojun int32_t tv32_sec;
142 1.75 itojun int32_t tv32_usec;
143 1.75 itojun };
144 1.22 christos
145 1.22 christos
146 1.98 joerg static u_char *packet;
147 1.98 joerg static int packlen;
148 1.98 joerg static int pingflags = 0, options;
149 1.98 joerg static int pongflags = 0;
150 1.98 joerg static char *fill_pat;
151 1.22 christos
152 1.98 joerg static int s; /* Socket file descriptor */
153 1.98 joerg static int sloop; /* Socket file descriptor/loopback */
154 1.22 christos
155 1.99 christos #define PHDR_LEN sizeof(struct tv32) /* size of timestamp header */
156 1.99 christos #define PHDR64_LEN sizeof(struct timespec) /* size of timestamp header */
157 1.98 joerg static struct sockaddr_in whereto, send_addr; /* Who to ping */
158 1.98 joerg static struct sockaddr_in src_addr; /* from where */
159 1.98 joerg static struct sockaddr_in loc_addr; /* 127.1 */
160 1.99 christos static int datalen; /* How much data */
161 1.99 christos static int phdrlen;
162 1.22 christos
163 1.111 dholland static sigset_t blockmask, enablemask; /* signal masks */
164 1.111 dholland
165 1.59 cgd #ifndef __NetBSD__
166 1.59 cgd static char *progname;
167 1.59 cgd #define getprogname() (progname)
168 1.59 cgd #define setprogname(name) ((void)(progname = (name)))
169 1.30 christos #endif
170 1.22 christos
171 1.98 joerg static char hostname[MAXHOSTNAMELEN];
172 1.22 christos
173 1.22 christos static struct {
174 1.22 christos struct ip o_ip;
175 1.30 christos char o_opt[MAX_IPOPTLEN];
176 1.22 christos union {
177 1.60 yamt u_char u_buf[MAXPACKET+offsetof(struct icmp, icmp_data)];
178 1.30 christos struct icmp u_icmp;
179 1.22 christos } o_u;
180 1.22 christos } out_pack;
181 1.30 christos #define opack_icmp out_pack.o_u.u_icmp
182 1.98 joerg static struct ip *opack_ip;
183 1.30 christos
184 1.112 joerg static uint8_t optspace[MAX_IPOPTLEN]; /* record route space */
185 1.98 joerg static int optlen;
186 1.30 christos
187 1.98 joerg static int npackets; /* total packets to send */
188 1.98 joerg static int preload; /* number of packets to "preload" */
189 1.98 joerg static int ntransmitted; /* output sequence # = #sent */
190 1.98 joerg static int ident; /* our ID, in network byte order */
191 1.98 joerg
192 1.98 joerg static int nreceived; /* # of packets we got back */
193 1.98 joerg
194 1.98 joerg static double interval; /* interval between packets */
195 1.99 christos static struct timespec interval_tv;
196 1.98 joerg static double tmin = 999999999.0;
197 1.98 joerg static double tmax = 0.0;
198 1.98 joerg static double tsum = 0.0; /* sum of all times */
199 1.98 joerg static double tsumsq = 0.0;
200 1.98 joerg static double maxwait = 0.0;
201 1.98 joerg
202 1.98 joerg static int bufspace = IP_MAXPACKET;
203 1.98 joerg
204 1.99 christos static struct timespec now, clear_cache, last_tx, next_tx, first_tx;
205 1.99 christos static struct timespec last_rx, first_rx;
206 1.98 joerg static int lastrcvd = 1; /* last ping sent has been received */
207 1.22 christos
208 1.99 christos static struct timespec jiggle_time;
209 1.22 christos static int jiggle_cnt, total_jiggled, jiggle_direction = -1;
210 1.22 christos
211 1.98 joerg __dead static void doit(void);
212 1.22 christos static void prefinish(int);
213 1.22 christos static void prtsig(int);
214 1.98 joerg __dead static void finish(int);
215 1.111 dholland static void blocksignals(void);
216 1.111 dholland static void enablesignals(void);
217 1.22 christos static void summary(int);
218 1.22 christos static void pinger(void);
219 1.22 christos static void fill(void);
220 1.22 christos static void rnd_fill(void);
221 1.99 christos static double diffsec(struct timespec *, struct timespec *);
222 1.99 christos #if 0
223 1.99 christos static void timespecadd(struct timespec *, struct timespec *);
224 1.99 christos #endif
225 1.99 christos static void sec_to_timespec(const double, struct timespec *);
226 1.99 christos static double timespec_to_sec(const struct timespec *);
227 1.22 christos static void pr_pack(u_char *, int, struct sockaddr_in *);
228 1.65 itojun static u_int16_t in_cksum(u_int16_t *, u_int);
229 1.57 is static void pr_saddr(u_char *);
230 1.22 christos static char *pr_addr(struct in_addr *);
231 1.22 christos static void pr_iph(struct icmp *, int);
232 1.22 christos static void pr_retip(struct icmp *, int);
233 1.22 christos static int pr_icmph(struct icmp *, struct sockaddr_in *, int);
234 1.22 christos static void jiggle(int), jiggle_flush(int);
235 1.30 christos static void gethost(const char *, const char *,
236 1.30 christos struct sockaddr_in *, char *, int);
237 1.98 joerg __dead static void usage(void);
238 1.1 cgd
239 1.12 cgd int
240 1.22 christos main(int argc, char *argv[])
241 1.1 cgd {
242 1.30 christos int c, i, on = 1, hostind = 0;
243 1.30 christos long l;
244 1.99 christos int len = -1, compat = 0;
245 1.30 christos u_char ttl = 0;
246 1.30 christos u_long tos = 0;
247 1.22 christos char *p;
248 1.50 itojun #ifdef IPSEC
249 1.50 itojun #ifdef IPSEC_POLICY_IPSEC
250 1.55 itojun char *policy_in = NULL;
251 1.55 itojun char *policy_out = NULL;
252 1.50 itojun #endif
253 1.50 itojun #endif
254 1.81 christos #ifdef SIGINFO
255 1.81 christos struct sigaction sa;
256 1.81 christos #endif
257 1.1 cgd
258 1.92 pooka if (prog_init && prog_init() == -1)
259 1.109 christos err(EXIT_FAILURE, "init failed");
260 1.91 pooka
261 1.92 pooka if ((s = prog_socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) < 0)
262 1.109 christos err(EXIT_FAILURE, "Cannot create socket");
263 1.92 pooka if ((sloop = prog_socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) < 0)
264 1.109 christos err(EXIT_FAILURE, "Cannot create socket");
265 1.83 elad
266 1.87 seanb /*
267 1.87 seanb * sloop is never read on. This prevents packets from
268 1.87 seanb * queueing in its recv buffer.
269 1.87 seanb */
270 1.92 pooka if (prog_shutdown(sloop, SHUT_RD) == -1)
271 1.87 seanb warn("Cannot shutdown for read");
272 1.87 seanb
273 1.93 pooka if (prog_setuid(prog_getuid()) == -1)
274 1.109 christos err(EXIT_FAILURE, "setuid");
275 1.83 elad
276 1.59 cgd setprogname(argv[0]);
277 1.59 cgd
278 1.50 itojun #ifndef IPSEC
279 1.50 itojun #define IPSECOPT
280 1.50 itojun #else
281 1.50 itojun #ifdef IPSEC_POLICY_IPSEC
282 1.50 itojun #define IPSECOPT "E:"
283 1.50 itojun #else
284 1.50 itojun #define IPSECOPT "AE"
285 1.50 itojun #endif /*IPSEC_POLICY_IPSEC*/
286 1.50 itojun #endif
287 1.30 christos while ((c = getopt(argc, argv,
288 1.99 christos "ac:CdDfg:h:i:I:l:Lnop:PqQrRs:t:T:vw:" IPSECOPT)) != -1) {
289 1.50 itojun #undef IPSECOPT
290 1.22 christos switch (c) {
291 1.49 sommerfe case 'a':
292 1.49 sommerfe pingflags |= F_AUDIBLE;
293 1.49 sommerfe break;
294 1.99 christos case 'C':
295 1.99 christos compat = 1;
296 1.99 christos break;
297 1.1 cgd case 'c':
298 1.113 dholland l = strtol(optarg, &p, 0);
299 1.113 dholland if (*p != '\0' || l <= 0)
300 1.109 christos errx(EXIT_FAILURE,
301 1.109 christos "Bad/invalid number of packets: %s",
302 1.109 christos optarg);
303 1.113 dholland #if INT_MAX < LONG_MAX
304 1.113 dholland if (l > INT_MAX)
305 1.113 dholland errx(EXIT_FAILURE,
306 1.113 dholland "Too many packets to count: %ld", l);
307 1.113 dholland #endif
308 1.113 dholland npackets = l;
309 1.22 christos break;
310 1.22 christos case 'D':
311 1.30 christos pingflags |= F_DF;
312 1.1 cgd break;
313 1.1 cgd case 'd':
314 1.22 christos options |= SO_DEBUG;
315 1.1 cgd break;
316 1.1 cgd case 'f':
317 1.22 christos pingflags |= F_FLOOD;
318 1.8 mycroft break;
319 1.30 christos case 'h':
320 1.30 christos hostind = optind-1;
321 1.30 christos break;
322 1.1 cgd case 'i': /* wait between sending packets */
323 1.22 christos interval = strtod(optarg, &p);
324 1.22 christos if (*p != '\0' || interval <= 0)
325 1.109 christos errx(EXIT_FAILURE, "Bad/invalid interval: %s",
326 1.109 christos optarg);
327 1.113 dholland /*
328 1.113 dholland * In order to avoid overflowing the microseconds
329 1.113 dholland * argument of poll() the interval must be less than
330 1.113 dholland * INT_MAX/1000. Limit it to one second less than
331 1.113 dholland * that to be safe.
332 1.113 dholland */
333 1.113 dholland if (interval >= INT_MAX/1000.0 - 1.0)
334 1.113 dholland errx(EXIT_FAILURE,
335 1.113 dholland "Timing interval %g too large", interval);
336 1.8 mycroft break;
337 1.1 cgd case 'l':
338 1.113 dholland l = strtol(optarg, &p, 0);
339 1.113 dholland if (*p != '\0' || l < 0)
340 1.109 christos errx(EXIT_FAILURE, "Bad/invalid preload value: "
341 1.109 christos "%s", optarg);
342 1.113 dholland #if INT_MAX < LONG_MAX
343 1.113 dholland if (l > INT_MAX)
344 1.113 dholland errx(EXIT_FAILURE,
345 1.113 dholland "Too many preload packets: %ld", l);
346 1.113 dholland #endif
347 1.113 dholland preload = l;
348 1.1 cgd break;
349 1.1 cgd case 'n':
350 1.22 christos pingflags |= F_NUMERIC;
351 1.1 cgd break;
352 1.25 christos case 'o':
353 1.25 christos pingflags |= F_ONCE;
354 1.25 christos break;
355 1.1 cgd case 'p': /* fill buffer with user pattern */
356 1.22 christos if (pingflags & F_PING_RANDOM)
357 1.109 christos errx(EXIT_FAILURE,
358 1.109 christos "Only one of -P and -p allowed");
359 1.22 christos pingflags |= F_PING_FILLED;
360 1.22 christos fill_pat = optarg;
361 1.22 christos break;
362 1.22 christos case 'P':
363 1.22 christos if (pingflags & F_PING_FILLED)
364 1.109 christos errx(EXIT_FAILURE,
365 1.109 christos "Only one of -P and -p allowed");
366 1.22 christos pingflags |= F_PING_RANDOM;
367 1.22 christos break;
368 1.1 cgd case 'q':
369 1.22 christos pingflags |= F_QUIET;
370 1.1 cgd break;
371 1.22 christos case 'Q':
372 1.22 christos pingflags |= F_SEMI_QUIET;
373 1.1 cgd break;
374 1.1 cgd case 'r':
375 1.22 christos options |= SO_DONTROUTE;
376 1.19 ghudson break;
377 1.1 cgd case 's': /* size of packet to send */
378 1.89 lukem l = strtol(optarg, &p, 0);
379 1.89 lukem if (*p != '\0' || l < 0)
380 1.109 christos errx(EXIT_FAILURE,
381 1.109 christos "Bad/invalid packet size: %s", optarg);
382 1.89 lukem if (l > MAXPACKET)
383 1.109 christos errx(EXIT_FAILURE, "packet size is too large");
384 1.99 christos len = (int)l;
385 1.22 christos break;
386 1.22 christos case 'v':
387 1.22 christos pingflags |= F_VERBOSE;
388 1.22 christos break;
389 1.22 christos case 'R':
390 1.22 christos pingflags |= F_RECORD_ROUTE;
391 1.22 christos break;
392 1.22 christos case 'L':
393 1.30 christos pingflags |= F_MCAST_NOLOOP;
394 1.8 mycroft break;
395 1.8 mycroft case 't':
396 1.30 christos tos = strtoul(optarg, &p, 0);
397 1.30 christos if (*p != '\0' || tos > 0xFF)
398 1.109 christos errx(EXIT_FAILURE, "bad tos value: %s", optarg);
399 1.22 christos break;
400 1.22 christos case 'T':
401 1.30 christos l = strtol(optarg, &p, 0);
402 1.30 christos if (*p != '\0' || l > 255 || l <= 0)
403 1.109 christos errx(EXIT_FAILURE, "ttl out of range: %s",
404 1.109 christos optarg);
405 1.30 christos ttl = (u_char)l; /* cannot check >255 otherwise */
406 1.22 christos break;
407 1.22 christos case 'I':
408 1.30 christos pingflags |= F_SOURCE_ADDR;
409 1.30 christos gethost("-I", optarg, &src_addr, 0, 0);
410 1.1 cgd break;
411 1.22 christos case 'g':
412 1.22 christos pingflags |= F_SOURCE_ROUTE;
413 1.30 christos gethost("-g", optarg, &send_addr, 0, 0);
414 1.1 cgd break;
415 1.19 ghudson case 'w':
416 1.22 christos maxwait = strtod(optarg, &p);
417 1.22 christos if (*p != '\0' || maxwait <= 0)
418 1.109 christos errx(EXIT_FAILURE, "Bad/invalid maxwait time: "
419 1.109 christos "%s", optarg);
420 1.19 ghudson break;
421 1.50 itojun #ifdef IPSEC
422 1.50 itojun #ifdef IPSEC_POLICY_IPSEC
423 1.50 itojun case 'E':
424 1.50 itojun pingflags |= F_POLICY;
425 1.70 itojun if (!strncmp("in", optarg, 2)) {
426 1.55 itojun policy_in = strdup(optarg);
427 1.70 itojun if (!policy_in)
428 1.109 christos err(EXIT_FAILURE, "strdup");
429 1.70 itojun } else if (!strncmp("out", optarg, 3)) {
430 1.55 itojun policy_out = strdup(optarg);
431 1.70 itojun if (!policy_out)
432 1.109 christos err(EXIT_FAILURE, "strdup");
433 1.70 itojun } else
434 1.109 christos errx(EXIT_FAILURE, "invalid security policy: "
435 1.109 christos "%s", optarg);
436 1.50 itojun break;
437 1.50 itojun #else
438 1.50 itojun case 'A':
439 1.50 itojun pingflags |= F_AUTHHDR;
440 1.50 itojun break;
441 1.50 itojun case 'E':
442 1.50 itojun pingflags |= F_ENCRYPT;
443 1.50 itojun break;
444 1.50 itojun #endif /*IPSEC_POLICY_IPSEC*/
445 1.50 itojun #endif /*IPSEC*/
446 1.1 cgd default:
447 1.1 cgd usage();
448 1.22 christos break;
449 1.1 cgd }
450 1.22 christos }
451 1.8 mycroft
452 1.22 christos if (interval == 0)
453 1.22 christos interval = (pingflags & F_FLOOD) ? FLOOD_INTVL : 1.0;
454 1.28 christos #ifndef sgi
455 1.93 pooka if (pingflags & F_FLOOD && prog_getuid())
456 1.109 christos errx(EXIT_FAILURE, "Must be superuser to use -f");
457 1.93 pooka if (interval < 1.0 && prog_getuid())
458 1.109 christos errx(EXIT_FAILURE, "Must be superuser to use < 1 sec "
459 1.109 christos "ping interval");
460 1.93 pooka if (preload > 0 && prog_getuid())
461 1.109 christos errx(EXIT_FAILURE, "Must be superuser to use -l");
462 1.28 christos #endif
463 1.99 christos sec_to_timespec(interval, &interval_tv);
464 1.114 dholland if (interval_tv.tv_sec == 0 && interval_tv.tv_nsec == 0) {
465 1.114 dholland errx(EXIT_FAILURE, "Packet interval must be at least 1 ns");
466 1.114 dholland }
467 1.22 christos
468 1.49 sommerfe if ((pingflags & (F_AUDIBLE|F_FLOOD)) == (F_AUDIBLE|F_FLOOD))
469 1.49 sommerfe warnx("Sorry, no audible output for flood pings");
470 1.49 sommerfe
471 1.22 christos if (npackets != 0) {
472 1.22 christos npackets += preload;
473 1.22 christos } else {
474 1.22 christos npackets = INT_MAX;
475 1.22 christos }
476 1.22 christos
477 1.30 christos if (hostind == 0) {
478 1.30 christos if (optind != argc-1)
479 1.30 christos usage();
480 1.22 christos else
481 1.30 christos hostind = optind;
482 1.22 christos }
483 1.30 christos else if (hostind >= argc - 1)
484 1.30 christos usage();
485 1.22 christos
486 1.30 christos gethost("", argv[hostind], &whereto, hostname, sizeof(hostname));
487 1.30 christos if (IN_MULTICAST(ntohl(whereto.sin_addr.s_addr)))
488 1.30 christos pingflags |= F_MCAST;
489 1.22 christos if (!(pingflags & F_SOURCE_ROUTE))
490 1.22 christos (void) memcpy(&send_addr, &whereto, sizeof(send_addr));
491 1.22 christos
492 1.22 christos loc_addr.sin_family = AF_INET;
493 1.71 itojun loc_addr.sin_len = sizeof(struct sockaddr_in);
494 1.95 christos loc_addr.sin_addr.s_addr = htonl((127 << 24) + 1);
495 1.22 christos
496 1.99 christos if (len != -1)
497 1.99 christos datalen = len;
498 1.99 christos else
499 1.109 christos datalen = 64 - PHDR_LEN;
500 1.99 christos if (!compat && datalen >= (int)PHDR64_LEN) { /* can we time them? */
501 1.99 christos pingflags |= F_TIMING64;
502 1.99 christos phdrlen = PHDR64_LEN;
503 1.99 christos } else if (datalen >= (int)PHDR_LEN) { /* can we time them? */
504 1.22 christos pingflags |= F_TIMING;
505 1.99 christos phdrlen = PHDR_LEN;
506 1.99 christos } else
507 1.99 christos phdrlen = 0;
508 1.104 christos
509 1.105 christos packlen = datalen + 60 + 76; /* MAXIP + MAXICMP */
510 1.95 christos if ((packet = malloc(packlen)) == NULL)
511 1.109 christos err(EXIT_FAILURE, "Can't allocate %d bytes", packlen);
512 1.22 christos
513 1.22 christos if (pingflags & F_PING_FILLED) {
514 1.22 christos fill();
515 1.22 christos } else if (pingflags & F_PING_RANDOM) {
516 1.22 christos rnd_fill();
517 1.22 christos } else {
518 1.99 christos for (i = phdrlen; i < datalen; i++)
519 1.22 christos opack_icmp.icmp_data[i] = i;
520 1.22 christos }
521 1.1 cgd
522 1.77 kleink ident = arc4random() & 0xFFFF;
523 1.1 cgd
524 1.22 christos if (options & SO_DEBUG) {
525 1.92 pooka if (prog_setsockopt(s, SOL_SOCKET, SO_DEBUG,
526 1.41 christos (char *)&on, sizeof(on)) == -1)
527 1.30 christos warn("Can't turn on socket debugging");
528 1.22 christos }
529 1.22 christos if (options & SO_DONTROUTE) {
530 1.92 pooka if (prog_setsockopt(s, SOL_SOCKET, SO_DONTROUTE,
531 1.41 christos (char *)&on, sizeof(on)) == -1)
532 1.30 christos warn("SO_DONTROUTE");
533 1.22 christos }
534 1.22 christos
535 1.50 itojun if (options & SO_DEBUG) {
536 1.92 pooka if (prog_setsockopt(sloop, SOL_SOCKET, SO_DEBUG,
537 1.50 itojun (char *)&on, sizeof(on)) == -1)
538 1.50 itojun warn("Can't turn on socket debugging");
539 1.50 itojun }
540 1.50 itojun if (options & SO_DONTROUTE) {
541 1.92 pooka if (prog_setsockopt(sloop, SOL_SOCKET, SO_DONTROUTE,
542 1.50 itojun (char *)&on, sizeof(on)) == -1)
543 1.50 itojun warn("SO_DONTROUTE");
544 1.50 itojun }
545 1.50 itojun
546 1.30 christos if (pingflags & F_SOURCE_ROUTE) {
547 1.30 christos optspace[IPOPT_OPTVAL] = IPOPT_LSRR;
548 1.30 christos optspace[IPOPT_OLEN] = optlen = 7;
549 1.30 christos optspace[IPOPT_OFFSET] = IPOPT_MINOFF;
550 1.41 christos (void)memcpy(&optspace[IPOPT_MINOFF-1], &whereto.sin_addr,
551 1.41 christos sizeof(whereto.sin_addr));
552 1.30 christos optspace[optlen++] = IPOPT_NOP;
553 1.30 christos }
554 1.30 christos if (pingflags & F_RECORD_ROUTE) {
555 1.30 christos optspace[optlen+IPOPT_OPTVAL] = IPOPT_RR;
556 1.30 christos optspace[optlen+IPOPT_OLEN] = (MAX_IPOPTLEN -1-optlen);
557 1.30 christos optspace[optlen+IPOPT_OFFSET] = IPOPT_MINOFF;
558 1.30 christos optlen = MAX_IPOPTLEN;
559 1.30 christos }
560 1.30 christos /* this leaves opack_ip 0(mod 4) aligned */
561 1.30 christos opack_ip = (struct ip *)((char *)&out_pack.o_ip
562 1.30 christos + sizeof(out_pack.o_opt)
563 1.30 christos - optlen);
564 1.30 christos (void) memcpy(opack_ip + 1, optspace, optlen);
565 1.30 christos
566 1.109 christos if (prog_setsockopt(s, IPPROTO_IP, IP_HDRINCL,
567 1.92 pooka (char *) &on, sizeof(on)) < 0)
568 1.109 christos err(EXIT_FAILURE, "Can't set special IP header");
569 1.30 christos
570 1.30 christos opack_ip->ip_v = IPVERSION;
571 1.30 christos opack_ip->ip_hl = (sizeof(struct ip)+optlen) >> 2;
572 1.30 christos opack_ip->ip_tos = tos;
573 1.30 christos opack_ip->ip_off = (pingflags & F_DF) ? IP_DF : 0;
574 1.30 christos opack_ip->ip_ttl = ttl ? ttl : MAXTTL;
575 1.30 christos opack_ip->ip_p = IPPROTO_ICMP;
576 1.30 christos opack_ip->ip_src = src_addr.sin_addr;
577 1.30 christos opack_ip->ip_dst = send_addr.sin_addr;
578 1.30 christos
579 1.30 christos if (pingflags & F_MCAST) {
580 1.30 christos if (pingflags & F_MCAST_NOLOOP) {
581 1.30 christos u_char loop = 0;
582 1.92 pooka if (prog_setsockopt(s, IPPROTO_IP,
583 1.92 pooka IP_MULTICAST_LOOP,
584 1.30 christos (char *) &loop, 1) < 0)
585 1.109 christos err(EXIT_FAILURE, "Can't disable multicast loopback");
586 1.22 christos }
587 1.1 cgd
588 1.30 christos if (ttl != 0
589 1.92 pooka && prog_setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL,
590 1.30 christos (char *) &ttl, 1) < 0)
591 1.109 christos err(EXIT_FAILURE, "Can't set multicast time-to-live");
592 1.30 christos
593 1.30 christos if ((pingflags & F_SOURCE_ADDR)
594 1.92 pooka && prog_setsockopt(s, IPPROTO_IP, IP_MULTICAST_IF,
595 1.30 christos (char *) &src_addr.sin_addr,
596 1.30 christos sizeof(src_addr.sin_addr)) < 0)
597 1.109 christos err(EXIT_FAILURE, "Can't set multicast source interface");
598 1.30 christos
599 1.30 christos } else if (pingflags & F_SOURCE_ADDR) {
600 1.92 pooka if (prog_setsockopt(s, IPPROTO_IP, IP_MULTICAST_IF,
601 1.30 christos (char *) &src_addr.sin_addr,
602 1.30 christos sizeof(src_addr.sin_addr)) < 0)
603 1.109 christos err(EXIT_FAILURE, "Can't set source interface/address");
604 1.22 christos }
605 1.50 itojun #ifdef IPSEC
606 1.50 itojun #ifdef IPSEC_POLICY_IPSEC
607 1.50 itojun {
608 1.50 itojun char *buf;
609 1.50 itojun if (pingflags & F_POLICY) {
610 1.55 itojun if (policy_in != NULL) {
611 1.55 itojun buf = ipsec_set_policy(policy_in, strlen(policy_in));
612 1.55 itojun if (buf == NULL)
613 1.109 christos errx(EXIT_FAILURE, "%s", ipsec_strerror());
614 1.92 pooka if (prog_setsockopt(s, IPPROTO_IP, IP_IPSEC_POLICY,
615 1.55 itojun buf, ipsec_get_policylen(buf)) < 0) {
616 1.109 christos err(EXIT_FAILURE, "ipsec policy cannot be "
617 1.109 christos "configured");
618 1.55 itojun }
619 1.55 itojun free(buf);
620 1.55 itojun }
621 1.55 itojun if (policy_out != NULL) {
622 1.55 itojun buf = ipsec_set_policy(policy_out, strlen(policy_out));
623 1.55 itojun if (buf == NULL)
624 1.109 christos errx(EXIT_FAILURE, "%s", ipsec_strerror());
625 1.92 pooka if (prog_setsockopt(s, IPPROTO_IP, IP_IPSEC_POLICY,
626 1.55 itojun buf, ipsec_get_policylen(buf)) < 0) {
627 1.109 christos err(EXIT_FAILURE, "ipsec policy cannot be "
628 1.109 christos "configured");
629 1.55 itojun }
630 1.55 itojun free(buf);
631 1.55 itojun }
632 1.50 itojun }
633 1.55 itojun buf = ipsec_set_policy("out bypass", strlen("out bypass"));
634 1.55 itojun if (buf == NULL)
635 1.109 christos errx(EXIT_FAILURE, "%s", ipsec_strerror());
636 1.92 pooka if (prog_setsockopt(sloop, IPPROTO_IP, IP_IPSEC_POLICY,
637 1.55 itojun buf, ipsec_get_policylen(buf)) < 0) {
638 1.52 itojun #if 0
639 1.50 itojun warnx("ipsec is not configured");
640 1.52 itojun #else
641 1.52 itojun /* ignore it, should be okay */
642 1.52 itojun #endif
643 1.52 itojun }
644 1.50 itojun free(buf);
645 1.50 itojun }
646 1.50 itojun #else
647 1.50 itojun {
648 1.50 itojun int optval;
649 1.50 itojun if (pingflags & F_AUTHHDR) {
650 1.50 itojun optval = IPSEC_LEVEL_REQUIRE;
651 1.50 itojun #ifdef IP_AUTH_TRANS_LEVEL
652 1.92 pooka (void)prog_setsockopt(s, IPPROTO_IP, IP_AUTH_TRANS_LEVEL,
653 1.50 itojun (char *)&optval, sizeof(optval));
654 1.50 itojun #else
655 1.92 pooka (void)prog_setsockopt(s, IPPROTO_IP, IP_AUTH_LEVEL,
656 1.50 itojun (char *)&optval, sizeof(optval));
657 1.50 itojun #endif
658 1.50 itojun }
659 1.50 itojun if (pingflags & F_ENCRYPT) {
660 1.50 itojun optval = IPSEC_LEVEL_REQUIRE;
661 1.92 pooka (void)prog_setsockopt(s, IPPROTO_IP, IP_ESP_TRANS_LEVEL,
662 1.50 itojun (char *)&optval, sizeof(optval));
663 1.50 itojun }
664 1.50 itojun optval = IPSEC_LEVEL_BYPASS;
665 1.50 itojun #ifdef IP_AUTH_TRANS_LEVEL
666 1.92 pooka (void)prog_setsockopt(sloop, IPPROTO_IP, IP_AUTH_TRANS_LEVEL,
667 1.50 itojun (char *)&optval, sizeof(optval));
668 1.50 itojun #else
669 1.92 pooka (void)prog_setsockopt(sloop, IPPROTO_IP, IP_AUTH_LEVEL,
670 1.50 itojun (char *)&optval, sizeof(optval));
671 1.50 itojun #endif
672 1.92 pooka (void)prog_setsockopt(sloop, IPPROTO_IP, IP_ESP_TRANS_LEVEL,
673 1.50 itojun (char *)&optval, sizeof(optval));
674 1.50 itojun }
675 1.50 itojun #endif /*IPSEC_POLICY_IPSEC*/
676 1.50 itojun #endif /*IPSEC*/
677 1.8 mycroft
678 1.30 christos (void)printf("PING %s (%s): %d data bytes\n", hostname,
679 1.108 christos inet_ntoa(whereto.sin_addr), datalen);
680 1.22 christos
681 1.22 christos /* When pinging the broadcast address, you can get a lot
682 1.22 christos * of answers. Doing something so evil is useful if you
683 1.22 christos * are trying to stress the ethernet, or just want to
684 1.22 christos * fill the arp cache to get some stuff for /etc/ethers.
685 1.1 cgd */
686 1.92 pooka while (0 > prog_setsockopt(s, SOL_SOCKET, SO_RCVBUF,
687 1.22 christos (char*)&bufspace, sizeof(bufspace))) {
688 1.60 yamt if ((bufspace -= 4096) <= 0)
689 1.109 christos err(EXIT_FAILURE, "Cannot set the receive buffer size");
690 1.22 christos }
691 1.1 cgd
692 1.30 christos /* make it possible to send giant probes, but do not worry now
693 1.30 christos * if it fails, since we probably won't send giant probes.
694 1.30 christos */
695 1.92 pooka (void)prog_setsockopt(s, SOL_SOCKET, SO_SNDBUF,
696 1.30 christos (char*)&bufspace, sizeof(bufspace));
697 1.22 christos
698 1.22 christos (void)signal(SIGINT, prefinish);
699 1.48 mjl
700 1.111 dholland /*
701 1.111 dholland * Set up two signal masks:
702 1.111 dholland * - blockmask blocks the signals we catch
703 1.111 dholland * - enablemask does not
704 1.111 dholland */
705 1.111 dholland
706 1.111 dholland sigemptyset(&enablemask);
707 1.111 dholland sigemptyset(&blockmask);
708 1.111 dholland sigaddset(&blockmask, SIGINT);
709 1.111 dholland #ifdef SIGINFO
710 1.111 dholland sigaddset(&blockmask, SIGINFO);
711 1.111 dholland #else
712 1.111 dholland sigaddset(&blockmask, SIGQUIT);
713 1.111 dholland #endif
714 1.111 dholland
715 1.22 christos #ifdef SIGINFO
716 1.81 christos sa.sa_handler = prtsig;
717 1.81 christos sa.sa_flags = SA_NOKERNINFO;
718 1.81 christos sigemptyset(&sa.sa_mask);
719 1.81 christos (void)sigaction(SIGINFO, &sa, NULL);
720 1.22 christos #else
721 1.22 christos (void)signal(SIGQUIT, prtsig);
722 1.22 christos #endif
723 1.22 christos (void)signal(SIGCONT, prtsig);
724 1.22 christos
725 1.111 dholland blocksignals();
726 1.111 dholland
727 1.22 christos /* fire off them quickies */
728 1.22 christos for (i = 0; i < preload; i++) {
729 1.99 christos clock_gettime(CLOCK_MONOTONIC, &now);
730 1.1 cgd pinger();
731 1.22 christos }
732 1.22 christos
733 1.22 christos doit();
734 1.22 christos return 0;
735 1.22 christos }
736 1.22 christos
737 1.22 christos static void
738 1.22 christos doit(void)
739 1.22 christos {
740 1.22 christos int cc;
741 1.22 christos struct sockaddr_in from;
742 1.78 mrg socklen_t fromlen;
743 1.30 christos double sec, last, d_last;
744 1.67 mycroft struct pollfd fdmaskp[1];
745 1.1 cgd
746 1.99 christos (void)clock_gettime(CLOCK_MONOTONIC, &clear_cache);
747 1.30 christos if (maxwait != 0) {
748 1.99 christos last = timespec_to_sec(&clear_cache) + maxwait;
749 1.30 christos d_last = 0;
750 1.30 christos } else {
751 1.30 christos last = 0;
752 1.30 christos d_last = 365*24*60*60;
753 1.30 christos }
754 1.1 cgd
755 1.30 christos do {
756 1.99 christos clock_gettime(CLOCK_MONOTONIC, &now);
757 1.22 christos
758 1.30 christos if (last != 0)
759 1.99 christos d_last = last - timespec_to_sec(&now);
760 1.22 christos
761 1.30 christos if (ntransmitted < npackets && d_last > 0) {
762 1.22 christos /* send if within 100 usec or late for next packet */
763 1.95 christos sec = diffsec(&next_tx, &now);
764 1.75 itojun if (sec <= 0.0001 ||
765 1.75 itojun (lastrcvd && (pingflags & F_FLOOD))) {
766 1.22 christos pinger();
767 1.95 christos sec = diffsec(&next_tx, &now);
768 1.22 christos }
769 1.22 christos if (sec < 0.0)
770 1.22 christos sec = 0.0;
771 1.30 christos if (d_last < sec)
772 1.30 christos sec = d_last;
773 1.22 christos
774 1.22 christos } else {
775 1.22 christos /* For the last response, wait twice as long as the
776 1.22 christos * worst case seen, or 10 times as long as the
777 1.22 christos * maximum interpacket interval, whichever is longer.
778 1.22 christos */
779 1.75 itojun sec = MAX(2 * tmax, 10 * interval) -
780 1.75 itojun diffsec(&now, &last_tx);
781 1.30 christos if (d_last < sec)
782 1.30 christos sec = d_last;
783 1.22 christos if (sec <= 0)
784 1.30 christos break;
785 1.22 christos }
786 1.22 christos
787 1.111 dholland
788 1.67 mycroft fdmaskp[0].fd = s;
789 1.67 mycroft fdmaskp[0].events = POLLIN;
790 1.111 dholland
791 1.111 dholland enablesignals();
792 1.92 pooka cc = prog_poll(fdmaskp, 1, (int)(sec * 1000));
793 1.111 dholland blocksignals();
794 1.111 dholland
795 1.22 christos if (cc <= 0) {
796 1.22 christos if (cc < 0) {
797 1.22 christos if (errno == EINTR)
798 1.22 christos continue;
799 1.22 christos jiggle_flush(1);
800 1.109 christos err(EXIT_FAILURE, "poll");
801 1.22 christos }
802 1.22 christos continue;
803 1.1 cgd }
804 1.22 christos
805 1.22 christos fromlen = sizeof(from);
806 1.92 pooka cc = prog_recvfrom(s, (char *) packet, packlen,
807 1.30 christos 0, (struct sockaddr *)&from,
808 1.22 christos &fromlen);
809 1.22 christos if (cc < 0) {
810 1.22 christos if (errno != EINTR) {
811 1.22 christos jiggle_flush(1);
812 1.30 christos warn("recvfrom");
813 1.22 christos (void)fflush(stderr);
814 1.22 christos }
815 1.1 cgd continue;
816 1.1 cgd }
817 1.99 christos clock_gettime(CLOCK_MONOTONIC, &now);
818 1.22 christos pr_pack(packet, cc, &from);
819 1.30 christos
820 1.30 christos } while (nreceived < npackets
821 1.30 christos && (nreceived == 0 || !(pingflags & F_ONCE)));
822 1.30 christos
823 1.30 christos finish(0);
824 1.22 christos }
825 1.22 christos
826 1.22 christos
827 1.22 christos static void
828 1.22 christos jiggle_flush(int nl) /* new line if there are dots */
829 1.22 christos {
830 1.22 christos int serrno = errno;
831 1.22 christos
832 1.22 christos if (jiggle_cnt > 0) {
833 1.22 christos total_jiggled += jiggle_cnt;
834 1.22 christos jiggle_direction = 1;
835 1.22 christos do {
836 1.22 christos (void)putchar('.');
837 1.22 christos } while (--jiggle_cnt > 0);
838 1.22 christos
839 1.22 christos } else if (jiggle_cnt < 0) {
840 1.22 christos total_jiggled -= jiggle_cnt;
841 1.22 christos jiggle_direction = -1;
842 1.22 christos do {
843 1.22 christos (void)putchar('\b');
844 1.22 christos } while (++jiggle_cnt < 0);
845 1.22 christos }
846 1.22 christos
847 1.22 christos if (nl) {
848 1.22 christos if (total_jiggled != 0)
849 1.22 christos (void)putchar('\n');
850 1.22 christos total_jiggled = 0;
851 1.22 christos jiggle_direction = -1;
852 1.1 cgd }
853 1.22 christos
854 1.22 christos (void)fflush(stdout);
855 1.22 christos (void)fflush(stderr);
856 1.22 christos jiggle_time = now;
857 1.22 christos errno = serrno;
858 1.1 cgd }
859 1.1 cgd
860 1.22 christos
861 1.22 christos /* jiggle the cursor for flood-ping
862 1.1 cgd */
863 1.22 christos static void
864 1.22 christos jiggle(int delta)
865 1.1 cgd {
866 1.22 christos double dt;
867 1.1 cgd
868 1.22 christos if (pingflags & F_QUIET)
869 1.22 christos return;
870 1.22 christos
871 1.22 christos /* do not back up into messages */
872 1.22 christos if (total_jiggled+jiggle_cnt+delta < 0)
873 1.22 christos return;
874 1.22 christos
875 1.22 christos jiggle_cnt += delta;
876 1.22 christos
877 1.22 christos /* flush the FLOOD dots when things are quiet
878 1.22 christos * or occassionally to make the cursor jiggle.
879 1.22 christos */
880 1.22 christos dt = diffsec(&last_tx, &jiggle_time);
881 1.22 christos if (dt > 0.2 || (dt >= 0.15 && delta*jiggle_direction < 0))
882 1.22 christos jiggle_flush(0);
883 1.1 cgd }
884 1.1 cgd
885 1.22 christos
886 1.1 cgd /*
887 1.22 christos * Compose and transmit an ICMP ECHO REQUEST packet. The IP packet
888 1.30 christos * will be added on by the kernel. The ID field is our UNIX process ID,
889 1.99 christos * and the sequence number is an ascending integer. The first phdrlen bytes
890 1.1 cgd * of the data portion are used to hold a UNIX "timeval" struct in VAX
891 1.99 christos * byte-order, to compute the round-trip time, or a UNIX "timespec" in native
892 1.99 christos * format.
893 1.1 cgd */
894 1.22 christos static void
895 1.22 christos pinger(void)
896 1.1 cgd {
897 1.75 itojun struct tv32 tv32;
898 1.30 christos int i, cc, sw;
899 1.114 dholland double waittime;
900 1.114 dholland long numskip;
901 1.22 christos
902 1.22 christos opack_icmp.icmp_code = 0;
903 1.65 itojun opack_icmp.icmp_seq = htons((u_int16_t)(ntransmitted));
904 1.30 christos
905 1.30 christos /* clear the cached route in the kernel after an ICMP
906 1.30 christos * response such as a Redirect is seen to stop causing
907 1.30 christos * more such packets. Also clear the cached route
908 1.30 christos * periodically in case of routing changes that make
909 1.30 christos * black holes come and go.
910 1.30 christos */
911 1.22 christos if (clear_cache.tv_sec != now.tv_sec) {
912 1.22 christos opack_icmp.icmp_type = ICMP_ECHOREPLY;
913 1.22 christos opack_icmp.icmp_id = ~ident;
914 1.22 christos opack_icmp.icmp_cksum = 0;
915 1.65 itojun opack_icmp.icmp_cksum = in_cksum((u_int16_t *)&opack_icmp,
916 1.99 christos phdrlen);
917 1.30 christos sw = 0;
918 1.109 christos if (prog_setsockopt(sloop, IPPROTO_IP, IP_HDRINCL,
919 1.109 christos (char *)&sw, sizeof(sw)) < 0)
920 1.109 christos err(EXIT_FAILURE, "Can't turn off special IP header");
921 1.92 pooka if (prog_sendto(sloop, (char *) &opack_icmp,
922 1.104 christos ICMP_MINLEN, MSG_DONTROUTE,
923 1.30 christos (struct sockaddr *)&loc_addr,
924 1.34 christos sizeof(struct sockaddr_in)) < 0) {
925 1.34 christos /*
926 1.34 christos * XXX: we only report this as a warning in verbose
927 1.34 christos * mode because people get confused when they see
928 1.34 christos * this error when they are running in single user
929 1.34 christos * mode and they have not configured lo0
930 1.34 christos */
931 1.34 christos if (pingflags & F_VERBOSE)
932 1.34 christos warn("failed to clear cached route");
933 1.34 christos }
934 1.30 christos sw = 1;
935 1.109 christos if (prog_setsockopt(sloop, IPPROTO_IP, IP_HDRINCL,
936 1.30 christos (char *)&sw, sizeof(sw)) < 0)
937 1.109 christos err(EXIT_FAILURE, "Can't set special IP header");
938 1.30 christos
939 1.99 christos (void)clock_gettime(CLOCK_MONOTONIC, &clear_cache);
940 1.22 christos }
941 1.30 christos
942 1.22 christos opack_icmp.icmp_type = ICMP_ECHO;
943 1.22 christos opack_icmp.icmp_id = ident;
944 1.99 christos
945 1.99 christos if (pingflags & F_TIMING) {
946 1.99 christos tv32.tv32_sec = (uint32_t)htonl(now.tv_sec);
947 1.99 christos tv32.tv32_usec = htonl(now.tv_nsec / 1000);
948 1.75 itojun (void) memcpy(&opack_icmp.icmp_data[0], &tv32, sizeof(tv32));
949 1.99 christos } else if (pingflags & F_TIMING64)
950 1.99 christos (void) memcpy(&opack_icmp.icmp_data[0], &now, sizeof(now));
951 1.99 christos
952 1.108 christos cc = MAX(datalen, ICMP_MINLEN) + PHDR_LEN;
953 1.22 christos opack_icmp.icmp_cksum = 0;
954 1.65 itojun opack_icmp.icmp_cksum = in_cksum((u_int16_t *)&opack_icmp, cc);
955 1.22 christos
956 1.30 christos cc += opack_ip->ip_hl<<2;
957 1.30 christos opack_ip->ip_len = cc;
958 1.92 pooka i = prog_sendto(s, (char *) opack_ip, cc, 0,
959 1.30 christos (struct sockaddr *)&send_addr, sizeof(struct sockaddr_in));
960 1.22 christos if (i != cc) {
961 1.22 christos jiggle_flush(1);
962 1.22 christos if (i < 0)
963 1.30 christos warn("sendto");
964 1.22 christos else
965 1.30 christos warnx("wrote %s %d chars, ret=%d", hostname, cc, i);
966 1.22 christos (void)fflush(stderr);
967 1.22 christos }
968 1.22 christos lastrcvd = 0;
969 1.22 christos
970 1.22 christos CLR(ntransmitted);
971 1.22 christos ntransmitted++;
972 1.22 christos
973 1.22 christos last_tx = now;
974 1.22 christos if (next_tx.tv_sec == 0) {
975 1.22 christos first_tx = now;
976 1.22 christos next_tx = now;
977 1.22 christos }
978 1.1 cgd
979 1.22 christos /* Transmit regularly, at always the same microsecond in the
980 1.22 christos * second when going at one packet per second.
981 1.22 christos * If we are at most 100 ms behind, send extras to get caught up.
982 1.22 christos * Otherwise, skip packets we were too slow to send.
983 1.22 christos */
984 1.114 dholland waittime = diffsec(&next_tx, &now);
985 1.114 dholland if (waittime < -1.0) {
986 1.114 dholland /* very behind - forget about being precise */
987 1.114 dholland next_tx.tv_sec += (int)(-waittime);
988 1.114 dholland } else if (waittime < -0.1) {
989 1.114 dholland /* behind - skip a few */
990 1.114 dholland if (interval_tv.tv_sec == 0) {
991 1.114 dholland numskip = (long)(-waittime / interval_tv.tv_nsec);
992 1.114 dholland next_tx.tv_nsec += numskip * interval_tv.tv_nsec;
993 1.114 dholland /*
994 1.114 dholland * We can add at most one second's worth, but allow
995 1.114 dholland * for tv_nsec reaching 2 billion just in case FP
996 1.114 dholland * issues strike.
997 1.114 dholland */
998 1.114 dholland while (next_tx.tv_nsec >= 1000000000) {
999 1.114 dholland next_tx.tv_sec++;
1000 1.114 dholland next_tx.tv_nsec -= 1000000000;
1001 1.114 dholland }
1002 1.114 dholland } else {
1003 1.114 dholland do {
1004 1.114 dholland timespecadd(&next_tx, &interval_tv, &next_tx);
1005 1.114 dholland } while (diffsec(&next_tx, &now) < -0.1);
1006 1.114 dholland }
1007 1.114 dholland
1008 1.114 dholland } else if (waittime <= interval) {
1009 1.114 dholland timespecadd(&next_tx, &interval_tv, &next_tx);
1010 1.22 christos }
1011 1.1 cgd
1012 1.22 christos if (pingflags & F_FLOOD)
1013 1.22 christos jiggle(1);
1014 1.1 cgd
1015 1.22 christos /* While the packet is going out, ready buffer for the next
1016 1.22 christos * packet. Use a fast but not very good random number generator.
1017 1.22 christos */
1018 1.22 christos if (pingflags & F_PING_RANDOM)
1019 1.22 christos rnd_fill();
1020 1.22 christos }
1021 1.1 cgd
1022 1.1 cgd
1023 1.22 christos static void
1024 1.30 christos pr_pack_sub(int cc,
1025 1.30 christos char *addr,
1026 1.30 christos int seqno,
1027 1.30 christos int dupflag,
1028 1.30 christos int ttl,
1029 1.22 christos double triptime)
1030 1.22 christos {
1031 1.22 christos jiggle_flush(1);
1032 1.1 cgd
1033 1.22 christos if (pingflags & F_FLOOD)
1034 1.22 christos return;
1035 1.1 cgd
1036 1.22 christos (void)printf("%d bytes from %s: icmp_seq=%u", cc, addr, seqno);
1037 1.30 christos if (dupflag)
1038 1.30 christos (void)printf(" DUP!");
1039 1.22 christos (void)printf(" ttl=%d", ttl);
1040 1.106 yamt if (pingflags & (F_TIMING|F_TIMING64)) {
1041 1.106 yamt const unsigned int prec = (pingflags & F_TIMING64) != 0 ? 6 : 3;
1042 1.106 yamt
1043 1.106 yamt (void)printf(" time=%.*f ms", prec, triptime*1000.0);
1044 1.106 yamt }
1045 1.49 sommerfe
1046 1.49 sommerfe /*
1047 1.49 sommerfe * Send beep to stderr, since that's more likely than stdout
1048 1.49 sommerfe * to go to a terminal..
1049 1.49 sommerfe */
1050 1.49 sommerfe if (pingflags & F_AUDIBLE && !dupflag)
1051 1.49 sommerfe (void)fprintf(stderr,"\a");
1052 1.1 cgd }
1053 1.1 cgd
1054 1.22 christos
1055 1.1 cgd /*
1056 1.22 christos * Print out the packet, if it came from us. This logic is necessary
1057 1.1 cgd * because ALL readers of the ICMP socket get a copy of ALL ICMP packets
1058 1.1 cgd * which arrive ('tis only fair). This permits multiple copies of this
1059 1.1 cgd * program to be run without having intermingled output (or statistics!).
1060 1.1 cgd */
1061 1.22 christos static void
1062 1.30 christos pr_pack(u_char *buf,
1063 1.41 christos int tot_len,
1064 1.22 christos struct sockaddr_in *from)
1065 1.1 cgd {
1066 1.22 christos struct ip *ip;
1067 1.32 lukem struct icmp *icp;
1068 1.41 christos int i, j, net_len;
1069 1.32 lukem u_char *cp;
1070 1.1 cgd static int old_rrlen;
1071 1.1 cgd static char old_rr[MAX_IPOPTLEN];
1072 1.22 christos int hlen, dupflag = 0, dumped;
1073 1.22 christos double triptime = 0.0;
1074 1.22 christos #define PR_PACK_SUB() {if (!dumped) { \
1075 1.22 christos dumped = 1; \
1076 1.41 christos pr_pack_sub(net_len, inet_ntoa(from->sin_addr), \
1077 1.65 itojun ntohs((u_int16_t)icp->icmp_seq), \
1078 1.22 christos dupflag, ip->ip_ttl, triptime);}}
1079 1.1 cgd
1080 1.1 cgd /* Check the IP header */
1081 1.22 christos ip = (struct ip *) buf;
1082 1.1 cgd hlen = ip->ip_hl << 2;
1083 1.41 christos if (tot_len < hlen + ICMP_MINLEN) {
1084 1.22 christos if (pingflags & F_VERBOSE) {
1085 1.22 christos jiggle_flush(1);
1086 1.30 christos (void)printf("packet too short (%d bytes) from %s\n",
1087 1.41 christos tot_len, inet_ntoa(from->sin_addr));
1088 1.22 christos }
1089 1.1 cgd return;
1090 1.1 cgd }
1091 1.1 cgd
1092 1.1 cgd /* Now the ICMP part */
1093 1.22 christos dumped = 0;
1094 1.41 christos net_len = tot_len - hlen;
1095 1.1 cgd icp = (struct icmp *)(buf + hlen);
1096 1.22 christos if (icp->icmp_type == ICMP_ECHOREPLY
1097 1.22 christos && icp->icmp_id == ident) {
1098 1.99 christos
1099 1.65 itojun if (icp->icmp_seq == htons((u_int16_t)(ntransmitted-1)))
1100 1.22 christos lastrcvd = 1;
1101 1.22 christos last_rx = now;
1102 1.22 christos if (first_rx.tv_sec == 0)
1103 1.22 christos first_rx = last_rx;
1104 1.22 christos nreceived++;
1105 1.107 christos if (pingflags & (F_TIMING|F_TIMING64)) {
1106 1.107 christos struct timespec tv;
1107 1.107 christos
1108 1.107 christos if (pingflags & F_TIMING) {
1109 1.107 christos struct tv32 tv32;
1110 1.75 itojun
1111 1.107 christos (void)memcpy(&tv32, icp->icmp_data, sizeof(tv32));
1112 1.107 christos tv.tv_sec = (uint32_t)ntohl(tv32.tv32_sec);
1113 1.107 christos tv.tv_nsec = ntohl(tv32.tv32_usec) * 1000;
1114 1.107 christos } else if (pingflags & F_TIMING64)
1115 1.107 christos (void)memcpy(&tv, icp->icmp_data, sizeof(tv));
1116 1.107 christos else
1117 1.107 christos memset(&tv, 0, sizeof(tv)); /* XXX: gcc */
1118 1.99 christos
1119 1.26 christos triptime = diffsec(&last_rx, &tv);
1120 1.1 cgd tsum += triptime;
1121 1.44 jwise tsumsq += triptime * triptime;
1122 1.1 cgd if (triptime < tmin)
1123 1.1 cgd tmin = triptime;
1124 1.1 cgd if (triptime > tmax)
1125 1.1 cgd tmax = triptime;
1126 1.1 cgd }
1127 1.1 cgd
1128 1.65 itojun if (TST(ntohs((u_int16_t)icp->icmp_seq))) {
1129 1.22 christos nrepeats++, nreceived--;
1130 1.22 christos dupflag=1;
1131 1.30 christos } else {
1132 1.65 itojun SET(ntohs((u_int16_t)icp->icmp_seq));
1133 1.30 christos }
1134 1.1 cgd
1135 1.41 christos if (tot_len != opack_ip->ip_len) {
1136 1.41 christos PR_PACK_SUB();
1137 1.88 christos switch (opack_ip->ip_len - tot_len) {
1138 1.88 christos case MAX_IPOPTLEN:
1139 1.88 christos if ((pongflags & F_RECORD_ROUTE) != 0)
1140 1.88 christos break;
1141 1.88 christos if ((pingflags & F_RECORD_ROUTE) == 0)
1142 1.88 christos goto out;
1143 1.88 christos pongflags |= F_RECORD_ROUTE;
1144 1.88 christos (void)printf("\nremote host does not "
1145 1.88 christos "support record route");
1146 1.88 christos break;
1147 1.88 christos case 8:
1148 1.88 christos if ((pongflags & F_SOURCE_ROUTE) != 0)
1149 1.88 christos break;
1150 1.88 christos if ((pingflags & F_SOURCE_ROUTE) == 0)
1151 1.88 christos goto out;
1152 1.88 christos pongflags |= F_SOURCE_ROUTE;
1153 1.88 christos (void)printf("\nremote host does not "
1154 1.88 christos "support source route");
1155 1.88 christos break;
1156 1.88 christos default:
1157 1.88 christos out:
1158 1.88 christos (void)printf("\nwrong total length %d "
1159 1.88 christos "instead of %d", tot_len, opack_ip->ip_len);
1160 1.88 christos break;
1161 1.88 christos }
1162 1.66 matt }
1163 1.66 matt
1164 1.66 matt if (!dupflag) {
1165 1.66 matt static u_int16_t last_seqno = 0xffff;
1166 1.66 matt u_int16_t seqno = ntohs((u_int16_t)icp->icmp_seq);
1167 1.66 matt u_int16_t gap = seqno - (last_seqno + 1);
1168 1.66 matt if (gap > 0 && gap < 0x8000 &&
1169 1.66 matt (pingflags & F_VERBOSE)) {
1170 1.66 matt (void)printf("[*** sequence gap of %u "
1171 1.66 matt "packets from %u ... %u ***]\n", gap,
1172 1.66 matt (u_int16_t) (last_seqno + 1),
1173 1.66 matt (u_int16_t) (seqno - 1));
1174 1.66 matt if (pingflags & F_QUIET)
1175 1.66 matt summary(0);
1176 1.66 matt }
1177 1.66 matt
1178 1.66 matt if (gap < 0x8000)
1179 1.66 matt last_seqno = seqno;
1180 1.41 christos }
1181 1.41 christos
1182 1.22 christos if (pingflags & F_QUIET)
1183 1.1 cgd return;
1184 1.1 cgd
1185 1.22 christos if (!(pingflags & F_FLOOD))
1186 1.22 christos PR_PACK_SUB();
1187 1.22 christos
1188 1.22 christos /* check the data */
1189 1.104 christos if ((size_t)(tot_len - hlen) >
1190 1.104 christos offsetof(struct icmp, icmp_data) + datalen
1191 1.22 christos && !(pingflags & F_PING_RANDOM)
1192 1.99 christos && memcmp(icp->icmp_data + phdrlen,
1193 1.99 christos opack_icmp.icmp_data + phdrlen,
1194 1.99 christos datalen - phdrlen)) {
1195 1.99 christos for (i = phdrlen; i < datalen; i++) {
1196 1.54 mycroft if (icp->icmp_data[i] !=
1197 1.54 mycroft opack_icmp.icmp_data[i])
1198 1.1 cgd break;
1199 1.22 christos }
1200 1.22 christos PR_PACK_SUB();
1201 1.22 christos (void)printf("\nwrong data byte #%d should have been"
1202 1.108 christos " %#x but was %#x", i - phdrlen,
1203 1.54 mycroft (u_char)opack_icmp.icmp_data[i],
1204 1.22 christos (u_char)icp->icmp_data[i]);
1205 1.99 christos for (i = phdrlen; i < datalen; i++) {
1206 1.104 christos if ((i % 16) == 0)
1207 1.22 christos (void)printf("\n\t");
1208 1.30 christos (void)printf("%2x ",(u_char)icp->icmp_data[i]);
1209 1.1 cgd }
1210 1.1 cgd }
1211 1.22 christos
1212 1.1 cgd } else {
1213 1.41 christos if (!pr_icmph(icp, from, net_len))
1214 1.1 cgd return;
1215 1.22 christos dumped = 2;
1216 1.1 cgd }
1217 1.1 cgd
1218 1.1 cgd /* Display any IP options */
1219 1.22 christos cp = buf + sizeof(struct ip);
1220 1.22 christos while (hlen > (int)sizeof(struct ip)) {
1221 1.1 cgd switch (*cp) {
1222 1.1 cgd case IPOPT_EOL:
1223 1.1 cgd hlen = 0;
1224 1.1 cgd break;
1225 1.1 cgd case IPOPT_LSRR:
1226 1.1 cgd hlen -= 2;
1227 1.1 cgd j = *++cp;
1228 1.1 cgd ++cp;
1229 1.22 christos j -= IPOPT_MINOFF;
1230 1.22 christos if (j <= 0)
1231 1.22 christos continue;
1232 1.22 christos if (dumped <= 1) {
1233 1.22 christos j = ((j+3)/4)*4;
1234 1.22 christos hlen -= j;
1235 1.22 christos cp += j;
1236 1.22 christos break;
1237 1.22 christos }
1238 1.22 christos PR_PACK_SUB();
1239 1.22 christos (void)printf("\nLSRR: ");
1240 1.22 christos for (;;) {
1241 1.57 is pr_saddr(cp);
1242 1.22 christos cp += 4;
1243 1.1 cgd hlen -= 4;
1244 1.1 cgd j -= 4;
1245 1.22 christos if (j <= 0)
1246 1.1 cgd break;
1247 1.1 cgd (void)putchar('\n');
1248 1.1 cgd }
1249 1.1 cgd break;
1250 1.1 cgd case IPOPT_RR:
1251 1.22 christos j = *++cp; /* get length */
1252 1.22 christos i = *++cp; /* and pointer */
1253 1.1 cgd hlen -= 2;
1254 1.1 cgd if (i > j)
1255 1.1 cgd i = j;
1256 1.1 cgd i -= IPOPT_MINOFF;
1257 1.1 cgd if (i <= 0)
1258 1.1 cgd continue;
1259 1.22 christos if (dumped <= 1) {
1260 1.22 christos if (i == old_rrlen
1261 1.30 christos && !memcmp(cp, old_rr, i)) {
1262 1.22 christos if (dumped)
1263 1.22 christos (void)printf("\t(same route)");
1264 1.22 christos j = ((i+3)/4)*4;
1265 1.22 christos hlen -= j;
1266 1.22 christos cp += j;
1267 1.22 christos break;
1268 1.22 christos }
1269 1.22 christos old_rrlen = i;
1270 1.30 christos (void) memcpy(old_rr, cp, i);
1271 1.22 christos }
1272 1.22 christos if (!dumped) {
1273 1.22 christos jiggle_flush(1);
1274 1.22 christos (void)printf("RR: ");
1275 1.22 christos dumped = 1;
1276 1.22 christos } else {
1277 1.22 christos (void)printf("\nRR: ");
1278 1.1 cgd }
1279 1.1 cgd for (;;) {
1280 1.57 is pr_saddr(cp);
1281 1.22 christos cp += 4;
1282 1.1 cgd hlen -= 4;
1283 1.1 cgd i -= 4;
1284 1.1 cgd if (i <= 0)
1285 1.1 cgd break;
1286 1.1 cgd (void)putchar('\n');
1287 1.1 cgd }
1288 1.1 cgd break;
1289 1.1 cgd case IPOPT_NOP:
1290 1.22 christos if (dumped <= 1)
1291 1.22 christos break;
1292 1.22 christos PR_PACK_SUB();
1293 1.1 cgd (void)printf("\nNOP");
1294 1.1 cgd break;
1295 1.22 christos #ifdef sgi
1296 1.22 christos case IPOPT_SECURITY: /* RFC 1108 RIPSO BSO */
1297 1.22 christos case IPOPT_ESO: /* RFC 1108 RIPSO ESO */
1298 1.22 christos case IPOPT_CIPSO: /* Commercial IPSO */
1299 1.22 christos if ((sysconf(_SC_IP_SECOPTS)) > 0) {
1300 1.22 christos i = (unsigned)cp[1];
1301 1.22 christos hlen -= i - 1;
1302 1.22 christos PR_PACK_SUB();
1303 1.22 christos (void)printf("\nSEC:");
1304 1.22 christos while (i--) {
1305 1.22 christos (void)printf(" %02x", *cp++);
1306 1.22 christos }
1307 1.22 christos cp--;
1308 1.22 christos break;
1309 1.22 christos }
1310 1.22 christos #endif
1311 1.1 cgd default:
1312 1.22 christos PR_PACK_SUB();
1313 1.32 lukem (void)printf("\nunknown option 0x%x", *cp);
1314 1.1 cgd break;
1315 1.1 cgd }
1316 1.22 christos hlen--;
1317 1.22 christos cp++;
1318 1.22 christos }
1319 1.22 christos
1320 1.22 christos if (dumped) {
1321 1.1 cgd (void)putchar('\n');
1322 1.1 cgd (void)fflush(stdout);
1323 1.22 christos } else {
1324 1.22 christos jiggle(-1);
1325 1.22 christos }
1326 1.22 christos }
1327 1.22 christos
1328 1.22 christos
1329 1.22 christos /* Compute the IP checksum
1330 1.22 christos * This assumes the packet is less than 32K long.
1331 1.22 christos */
1332 1.65 itojun static u_int16_t
1333 1.65 itojun in_cksum(u_int16_t *p, u_int len)
1334 1.22 christos {
1335 1.65 itojun u_int32_t sum = 0;
1336 1.22 christos int nwords = len >> 1;
1337 1.22 christos
1338 1.22 christos while (nwords-- != 0)
1339 1.22 christos sum += *p++;
1340 1.22 christos
1341 1.22 christos if (len & 1) {
1342 1.22 christos union {
1343 1.65 itojun u_int16_t w;
1344 1.65 itojun u_int8_t c[2];
1345 1.22 christos } u;
1346 1.22 christos u.c[0] = *(u_char *)p;
1347 1.22 christos u.c[1] = 0;
1348 1.22 christos sum += u.w;
1349 1.1 cgd }
1350 1.22 christos
1351 1.22 christos /* end-around-carry */
1352 1.22 christos sum = (sum >> 16) + (sum & 0xffff);
1353 1.22 christos sum += (sum >> 16);
1354 1.22 christos return (~sum);
1355 1.1 cgd }
1356 1.1 cgd
1357 1.22 christos
1358 1.1 cgd /*
1359 1.99 christos * compute the difference of two timespecs in seconds
1360 1.1 cgd */
1361 1.22 christos static double
1362 1.99 christos diffsec(struct timespec *timenow,
1363 1.99 christos struct timespec *then)
1364 1.22 christos {
1365 1.97 christos if (timenow->tv_sec == 0)
1366 1.97 christos return -1;
1367 1.99 christos return (timenow->tv_sec - then->tv_sec)
1368 1.99 christos * 1.0 + (timenow->tv_nsec - then->tv_nsec) / 1000000000.0;
1369 1.22 christos }
1370 1.22 christos
1371 1.22 christos
1372 1.99 christos #if 0
1373 1.22 christos static void
1374 1.99 christos timespecadd(struct timespec *t1,
1375 1.99 christos struct timespec *t2)
1376 1.22 christos {
1377 1.22 christos
1378 1.22 christos t1->tv_sec += t2->tv_sec;
1379 1.99 christos if ((t1->tv_nsec += t2->tv_nsec) >= 1000000000) {
1380 1.22 christos t1->tv_sec++;
1381 1.99 christos t1->tv_nsec -= 1000000000;
1382 1.22 christos }
1383 1.22 christos }
1384 1.99 christos #endif
1385 1.22 christos
1386 1.22 christos
1387 1.22 christos static void
1388 1.99 christos sec_to_timespec(const double sec, struct timespec *tp)
1389 1.22 christos {
1390 1.30 christos tp->tv_sec = sec;
1391 1.99 christos tp->tv_nsec = (sec - tp->tv_sec) * 1000000000.0;
1392 1.22 christos }
1393 1.1 cgd
1394 1.41 christos
1395 1.22 christos static double
1396 1.99 christos timespec_to_sec(const struct timespec *tp)
1397 1.22 christos {
1398 1.99 christos return tp->tv_sec + tp->tv_nsec / 1000000000.0;
1399 1.1 cgd }
1400 1.1 cgd
1401 1.22 christos
1402 1.1 cgd /*
1403 1.22 christos * Print statistics.
1404 1.22 christos * Heavily buffered STDIO is used here, so that all the statistics
1405 1.22 christos * will be written with 1 sys-write call. This is nice when more
1406 1.22 christos * than one copy of the program is running on a terminal; it prevents
1407 1.22 christos * the statistics output from becomming intermingled.
1408 1.1 cgd */
1409 1.22 christos static void
1410 1.22 christos summary(int header)
1411 1.1 cgd {
1412 1.22 christos jiggle_flush(1);
1413 1.8 mycroft
1414 1.22 christos if (header)
1415 1.22 christos (void)printf("\n----%s PING Statistics----\n", hostname);
1416 1.22 christos (void)printf("%d packets transmitted, ", ntransmitted);
1417 1.22 christos (void)printf("%d packets received, ", nreceived);
1418 1.1 cgd if (nrepeats)
1419 1.22 christos (void)printf("+%d duplicates, ", nrepeats);
1420 1.22 christos if (ntransmitted) {
1421 1.1 cgd if (nreceived > ntransmitted)
1422 1.64 itojun (void)printf("-- somebody's duplicating packets!");
1423 1.1 cgd else
1424 1.41 christos (void)printf("%.1f%% packet loss",
1425 1.41 christos (((ntransmitted-nreceived)*100.0) /
1426 1.22 christos ntransmitted));
1427 1.22 christos }
1428 1.22 christos (void)printf("\n");
1429 1.101 christos if (nreceived && (pingflags & (F_TIMING|F_TIMING64))) {
1430 1.44 jwise double n = nreceived + nrepeats;
1431 1.44 jwise double avg = (tsum / n);
1432 1.47 sommerfe double variance = 0.0;
1433 1.106 yamt const unsigned int prec = (pingflags & F_TIMING64) != 0 ? 6 : 3;
1434 1.47 sommerfe if (n>1)
1435 1.47 sommerfe variance = (tsumsq - n*avg*avg) /(n-1);
1436 1.44 jwise
1437 1.108 christos (void)printf("round-trip min/avg/max/stddev = "
1438 1.106 yamt "%.*f/%.*f/%.*f/%.*f ms\n",
1439 1.106 yamt prec, tmin * 1000.0,
1440 1.106 yamt prec, avg * 1000.0,
1441 1.106 yamt prec, tmax * 1000.0,
1442 1.106 yamt prec, sqrt(variance) * 1000.0);
1443 1.22 christos if (pingflags & F_FLOOD) {
1444 1.22 christos double r = diffsec(&last_rx, &first_rx);
1445 1.22 christos double t = diffsec(&last_tx, &first_tx);
1446 1.22 christos if (r == 0)
1447 1.22 christos r = 0.0001;
1448 1.22 christos if (t == 0)
1449 1.22 christos t = 0.0001;
1450 1.22 christos (void)printf(" %.1f packets/sec sent, "
1451 1.30 christos " %.1f packets/sec received\n",
1452 1.22 christos ntransmitted/t, nreceived/r);
1453 1.22 christos }
1454 1.8 mycroft }
1455 1.21 cgd }
1456 1.21 cgd
1457 1.22 christos
1458 1.22 christos /*
1459 1.22 christos * Print statistics when SIGINFO is received.
1460 1.22 christos */
1461 1.22 christos /* ARGSUSED */
1462 1.22 christos static void
1463 1.62 lukem prtsig(int dummy)
1464 1.22 christos {
1465 1.82 yamt
1466 1.22 christos summary(0);
1467 1.81 christos #ifndef SIGINFO
1468 1.22 christos (void)signal(SIGQUIT, prtsig);
1469 1.22 christos #endif
1470 1.22 christos }
1471 1.22 christos
1472 1.22 christos
1473 1.21 cgd /*
1474 1.22 christos * On the first SIGINT, allow any outstanding packets to dribble in
1475 1.21 cgd */
1476 1.22 christos static void
1477 1.110 dholland prefinish(int sig)
1478 1.21 cgd {
1479 1.22 christos if (lastrcvd /* quit now if caught up */
1480 1.22 christos || nreceived == 0) /* or if remote is dead */
1481 1.22 christos finish(0);
1482 1.22 christos
1483 1.110 dholland (void)signal(sig, finish); /* do this only the 1st time */
1484 1.21 cgd
1485 1.22 christos if (npackets > ntransmitted) /* let the normal limit work */
1486 1.22 christos npackets = ntransmitted;
1487 1.21 cgd }
1488 1.21 cgd
1489 1.21 cgd /*
1490 1.22 christos * Print statistics and give up.
1491 1.21 cgd */
1492 1.22 christos /* ARGSUSED */
1493 1.22 christos static void
1494 1.62 lukem finish(int dummy)
1495 1.21 cgd {
1496 1.81 christos #ifdef SIGINFO
1497 1.81 christos (void)signal(SIGINFO, SIG_DFL);
1498 1.22 christos #else
1499 1.22 christos (void)signal(SIGQUIT, SIG_DFL);
1500 1.22 christos #endif
1501 1.22 christos
1502 1.22 christos summary(1);
1503 1.30 christos exit(nreceived > 0 ? 0 : 2);
1504 1.22 christos }
1505 1.22 christos
1506 1.111 dholland static void
1507 1.111 dholland blocksignals(void)
1508 1.111 dholland {
1509 1.111 dholland if (sigprocmask(SIG_SETMASK, &blockmask, NULL) == -1) {
1510 1.111 dholland err(EXIT_FAILURE, "blocksignals: sigprocmask");
1511 1.111 dholland }
1512 1.111 dholland }
1513 1.111 dholland
1514 1.111 dholland static void
1515 1.111 dholland enablesignals(void)
1516 1.111 dholland {
1517 1.111 dholland if (sigprocmask(SIG_SETMASK, &enablemask, NULL) == -1) {
1518 1.111 dholland err(EXIT_FAILURE, "enablesignals: sigprocmask");
1519 1.111 dholland }
1520 1.111 dholland }
1521 1.111 dholland
1522 1.22 christos
1523 1.22 christos static int /* 0=do not print it */
1524 1.30 christos ck_pr_icmph(struct icmp *icp,
1525 1.30 christos struct sockaddr_in *from,
1526 1.30 christos int cc,
1527 1.22 christos int override) /* 1=override VERBOSE if interesting */
1528 1.22 christos {
1529 1.22 christos int hlen;
1530 1.41 christos struct ip ipb, *ip = &ipb;
1531 1.41 christos struct icmp icp2b, *icp2 = &icp2b;
1532 1.22 christos int res;
1533 1.21 cgd
1534 1.22 christos if (pingflags & F_VERBOSE) {
1535 1.22 christos res = 1;
1536 1.22 christos jiggle_flush(1);
1537 1.22 christos } else {
1538 1.22 christos res = 0;
1539 1.22 christos }
1540 1.21 cgd
1541 1.41 christos (void) memcpy(ip, icp->icmp_data, sizeof(*ip));
1542 1.41 christos hlen = ip->ip_hl << 2;
1543 1.41 christos if (ip->ip_p == IPPROTO_ICMP
1544 1.22 christos && hlen + 6 <= cc) {
1545 1.41 christos (void) memcpy(icp2, &icp->icmp_data[hlen], sizeof(*icp2));
1546 1.41 christos if (icp2->icmp_id == ident) {
1547 1.22 christos /* remember to clear route cached in kernel
1548 1.41 christos * if this non-Echo-Reply ICMP message was for one
1549 1.41 christos * of our packets.
1550 1.22 christos */
1551 1.22 christos clear_cache.tv_sec = 0;
1552 1.22 christos
1553 1.22 christos if (!res && override
1554 1.22 christos && (pingflags & (F_QUIET|F_SEMI_QUIET)) == 0) {
1555 1.22 christos jiggle_flush(1);
1556 1.30 christos (void)printf("%d bytes from %s: ",
1557 1.22 christos cc, pr_addr(&from->sin_addr));
1558 1.22 christos res = 1;
1559 1.22 christos }
1560 1.22 christos }
1561 1.22 christos }
1562 1.22 christos
1563 1.22 christos return res;
1564 1.1 cgd }
1565 1.1 cgd
1566 1.1 cgd
1567 1.1 cgd /*
1568 1.22 christos * Print a descriptive string about an ICMP header other than an echo reply.
1569 1.1 cgd */
1570 1.22 christos static int /* 0=printed nothing */
1571 1.30 christos pr_icmph(struct icmp *icp,
1572 1.30 christos struct sockaddr_in *from,
1573 1.22 christos int cc)
1574 1.1 cgd {
1575 1.22 christos switch (icp->icmp_type ) {
1576 1.1 cgd case ICMP_UNREACH:
1577 1.22 christos if (!ck_pr_icmph(icp, from, cc, 1))
1578 1.22 christos return 0;
1579 1.22 christos switch (icp->icmp_code) {
1580 1.1 cgd case ICMP_UNREACH_NET:
1581 1.22 christos (void)printf("Destination Net Unreachable");
1582 1.1 cgd break;
1583 1.1 cgd case ICMP_UNREACH_HOST:
1584 1.22 christos (void)printf("Destination Host Unreachable");
1585 1.1 cgd break;
1586 1.1 cgd case ICMP_UNREACH_PROTOCOL:
1587 1.22 christos (void)printf("Destination Protocol Unreachable");
1588 1.1 cgd break;
1589 1.1 cgd case ICMP_UNREACH_PORT:
1590 1.22 christos (void)printf("Destination Port Unreachable");
1591 1.1 cgd break;
1592 1.1 cgd case ICMP_UNREACH_NEEDFRAG:
1593 1.30 christos (void)printf("frag needed and DF set. Next MTU=%d",
1594 1.36 kleink ntohs(icp->icmp_nextmtu));
1595 1.1 cgd break;
1596 1.1 cgd case ICMP_UNREACH_SRCFAIL:
1597 1.22 christos (void)printf("Source Route Failed");
1598 1.22 christos break;
1599 1.22 christos case ICMP_UNREACH_NET_UNKNOWN:
1600 1.22 christos (void)printf("Unreachable unknown net");
1601 1.22 christos break;
1602 1.22 christos case ICMP_UNREACH_HOST_UNKNOWN:
1603 1.22 christos (void)printf("Unreachable unknown host");
1604 1.22 christos break;
1605 1.22 christos case ICMP_UNREACH_ISOLATED:
1606 1.22 christos (void)printf("Unreachable host isolated");
1607 1.22 christos break;
1608 1.22 christos case ICMP_UNREACH_NET_PROHIB:
1609 1.22 christos (void)printf("Net prohibited access");
1610 1.22 christos break;
1611 1.22 christos case ICMP_UNREACH_HOST_PROHIB:
1612 1.22 christos (void)printf("Host prohibited access");
1613 1.22 christos break;
1614 1.22 christos case ICMP_UNREACH_TOSNET:
1615 1.22 christos (void)printf("Bad TOS for net");
1616 1.22 christos break;
1617 1.22 christos case ICMP_UNREACH_TOSHOST:
1618 1.22 christos (void)printf("Bad TOS for host");
1619 1.22 christos break;
1620 1.22 christos case 13:
1621 1.22 christos (void)printf("Communication prohibited");
1622 1.22 christos break;
1623 1.22 christos case 14:
1624 1.22 christos (void)printf("Host precedence violation");
1625 1.22 christos break;
1626 1.22 christos case 15:
1627 1.22 christos (void)printf("Precedence cutoff");
1628 1.1 cgd break;
1629 1.1 cgd default:
1630 1.30 christos (void)printf("Bad Destination Unreachable Code: %d",
1631 1.22 christos icp->icmp_code);
1632 1.1 cgd break;
1633 1.1 cgd }
1634 1.1 cgd /* Print returned IP header information */
1635 1.22 christos pr_retip(icp, cc);
1636 1.1 cgd break;
1637 1.22 christos
1638 1.1 cgd case ICMP_SOURCEQUENCH:
1639 1.22 christos if (!ck_pr_icmph(icp, from, cc, 1))
1640 1.22 christos return 0;
1641 1.22 christos (void)printf("Source Quench");
1642 1.22 christos pr_retip(icp, cc);
1643 1.1 cgd break;
1644 1.22 christos
1645 1.1 cgd case ICMP_REDIRECT:
1646 1.22 christos if (!ck_pr_icmph(icp, from, cc, 1))
1647 1.22 christos return 0;
1648 1.22 christos switch (icp->icmp_code) {
1649 1.1 cgd case ICMP_REDIRECT_NET:
1650 1.41 christos (void)printf("Redirect Network");
1651 1.1 cgd break;
1652 1.1 cgd case ICMP_REDIRECT_HOST:
1653 1.41 christos (void)printf("Redirect Host");
1654 1.1 cgd break;
1655 1.1 cgd case ICMP_REDIRECT_TOSNET:
1656 1.41 christos (void)printf("Redirect Type of Service and Network");
1657 1.1 cgd break;
1658 1.1 cgd case ICMP_REDIRECT_TOSHOST:
1659 1.41 christos (void)printf("Redirect Type of Service and Host");
1660 1.1 cgd break;
1661 1.1 cgd default:
1662 1.41 christos (void)printf("Redirect--Bad Code: %d", icp->icmp_code);
1663 1.1 cgd break;
1664 1.1 cgd }
1665 1.41 christos (void)printf(" New router addr: %s",
1666 1.22 christos pr_addr(&icp->icmp_hun.ih_gwaddr));
1667 1.22 christos pr_retip(icp, cc);
1668 1.22 christos break;
1669 1.22 christos
1670 1.22 christos case ICMP_ECHO:
1671 1.22 christos if (!ck_pr_icmph(icp, from, cc, 0))
1672 1.22 christos return 0;
1673 1.30 christos (void)printf("Echo Request: ID=%d seq=%d",
1674 1.41 christos ntohs(icp->icmp_id), ntohs(icp->icmp_seq));
1675 1.22 christos break;
1676 1.22 christos
1677 1.22 christos case ICMP_ECHOREPLY:
1678 1.22 christos /* displaying other's pings is too noisey */
1679 1.22 christos #if 0
1680 1.22 christos if (!ck_pr_icmph(icp, from, cc, 0))
1681 1.22 christos return 0;
1682 1.30 christos (void)printf("Echo Reply: ID=%d seq=%d",
1683 1.41 christos ntohs(icp->icmp_id), ntohs(icp->icmp_seq));
1684 1.22 christos break;
1685 1.1 cgd #else
1686 1.22 christos return 0;
1687 1.1 cgd #endif
1688 1.22 christos
1689 1.22 christos case ICMP_ROUTERADVERT:
1690 1.22 christos if (!ck_pr_icmph(icp, from, cc, 0))
1691 1.22 christos return 0;
1692 1.22 christos (void)printf("Router Discovery Advert");
1693 1.1 cgd break;
1694 1.22 christos
1695 1.22 christos case ICMP_ROUTERSOLICIT:
1696 1.22 christos if (!ck_pr_icmph(icp, from, cc, 0))
1697 1.22 christos return 0;
1698 1.22 christos (void)printf("Router Discovery Solicit");
1699 1.1 cgd break;
1700 1.22 christos
1701 1.1 cgd case ICMP_TIMXCEED:
1702 1.22 christos if (!ck_pr_icmph(icp, from, cc, 1))
1703 1.22 christos return 0;
1704 1.22 christos switch (icp->icmp_code ) {
1705 1.1 cgd case ICMP_TIMXCEED_INTRANS:
1706 1.22 christos (void)printf("Time To Live exceeded");
1707 1.1 cgd break;
1708 1.1 cgd case ICMP_TIMXCEED_REASS:
1709 1.22 christos (void)printf("Frag reassembly time exceeded");
1710 1.1 cgd break;
1711 1.1 cgd default:
1712 1.30 christos (void)printf("Time exceeded, Bad Code: %d",
1713 1.22 christos icp->icmp_code);
1714 1.1 cgd break;
1715 1.1 cgd }
1716 1.22 christos pr_retip(icp, cc);
1717 1.1 cgd break;
1718 1.22 christos
1719 1.1 cgd case ICMP_PARAMPROB:
1720 1.22 christos if (!ck_pr_icmph(icp, from, cc, 1))
1721 1.22 christos return 0;
1722 1.30 christos (void)printf("Parameter problem: pointer = 0x%02x",
1723 1.22 christos icp->icmp_hun.ih_pptr);
1724 1.22 christos pr_retip(icp, cc);
1725 1.1 cgd break;
1726 1.22 christos
1727 1.1 cgd case ICMP_TSTAMP:
1728 1.22 christos if (!ck_pr_icmph(icp, from, cc, 0))
1729 1.22 christos return 0;
1730 1.22 christos (void)printf("Timestamp");
1731 1.1 cgd break;
1732 1.22 christos
1733 1.1 cgd case ICMP_TSTAMPREPLY:
1734 1.22 christos if (!ck_pr_icmph(icp, from, cc, 0))
1735 1.22 christos return 0;
1736 1.22 christos (void)printf("Timestamp Reply");
1737 1.1 cgd break;
1738 1.22 christos
1739 1.1 cgd case ICMP_IREQ:
1740 1.22 christos if (!ck_pr_icmph(icp, from, cc, 0))
1741 1.22 christos return 0;
1742 1.22 christos (void)printf("Information Request");
1743 1.1 cgd break;
1744 1.22 christos
1745 1.1 cgd case ICMP_IREQREPLY:
1746 1.22 christos if (!ck_pr_icmph(icp, from, cc, 0))
1747 1.22 christos return 0;
1748 1.22 christos (void)printf("Information Reply");
1749 1.1 cgd break;
1750 1.22 christos
1751 1.1 cgd case ICMP_MASKREQ:
1752 1.22 christos if (!ck_pr_icmph(icp, from, cc, 0))
1753 1.22 christos return 0;
1754 1.22 christos (void)printf("Address Mask Request");
1755 1.1 cgd break;
1756 1.22 christos
1757 1.1 cgd case ICMP_MASKREPLY:
1758 1.22 christos if (!ck_pr_icmph(icp, from, cc, 0))
1759 1.22 christos return 0;
1760 1.22 christos (void)printf("Address Mask Reply");
1761 1.1 cgd break;
1762 1.22 christos
1763 1.1 cgd default:
1764 1.22 christos if (!ck_pr_icmph(icp, from, cc, 0))
1765 1.22 christos return 0;
1766 1.22 christos (void)printf("Bad ICMP type: %d", icp->icmp_type);
1767 1.22 christos if (pingflags & F_VERBOSE)
1768 1.22 christos pr_iph(icp, cc);
1769 1.1 cgd }
1770 1.22 christos
1771 1.22 christos return 1;
1772 1.1 cgd }
1773 1.1 cgd
1774 1.22 christos
1775 1.1 cgd /*
1776 1.22 christos * Print an IP header with options.
1777 1.1 cgd */
1778 1.22 christos static void
1779 1.30 christos pr_iph(struct icmp *icp,
1780 1.22 christos int cc)
1781 1.1 cgd {
1782 1.22 christos int hlen;
1783 1.22 christos u_char *cp;
1784 1.115 joerg struct ip __aligned(4) ipb;
1785 1.115 joerg struct ip *ip = &ipb;
1786 1.26 christos
1787 1.41 christos (void) memcpy(ip, icp->icmp_data, sizeof(*ip));
1788 1.1 cgd
1789 1.41 christos hlen = ip->ip_hl << 2;
1790 1.30 christos cp = (u_char *) &icp->icmp_data[20]; /* point to options */
1791 1.1 cgd
1792 1.22 christos (void)printf("\n Vr HL TOS Len ID Flg off TTL Pro cks Src Dst\n");
1793 1.30 christos (void)printf(" %1x %1x %02x %04x %04x",
1794 1.41 christos ip->ip_v, ip->ip_hl, ip->ip_tos, ip->ip_len, ip->ip_id);
1795 1.30 christos (void)printf(" %1x %04x",
1796 1.41 christos ((ip->ip_off)&0xe000)>>13, (ip->ip_off)&0x1fff);
1797 1.30 christos (void)printf(" %02x %02x %04x",
1798 1.41 christos ip->ip_ttl, ip->ip_p, ip->ip_sum);
1799 1.30 christos (void)printf(" %15s ",
1800 1.41 christos inet_ntoa(*(struct in_addr *)&ip->ip_src.s_addr));
1801 1.41 christos (void)printf(" %s ", inet_ntoa(*(struct in_addr *)&ip->ip_dst.s_addr));
1802 1.22 christos /* dump any option bytes */
1803 1.22 christos while (hlen-- > 20 && cp < (u_char*)icp+cc) {
1804 1.1 cgd (void)printf("%02x", *cp++);
1805 1.1 cgd }
1806 1.1 cgd }
1807 1.1 cgd
1808 1.1 cgd /*
1809 1.22 christos * Print an ASCII host address starting from a string of bytes.
1810 1.1 cgd */
1811 1.22 christos static void
1812 1.57 is pr_saddr(u_char *cp)
1813 1.22 christos {
1814 1.32 lukem n_long l;
1815 1.22 christos struct in_addr addr;
1816 1.22 christos
1817 1.22 christos l = (u_char)*++cp;
1818 1.22 christos l = (l<<8) + (u_char)*++cp;
1819 1.22 christos l = (l<<8) + (u_char)*++cp;
1820 1.22 christos l = (l<<8) + (u_char)*++cp;
1821 1.22 christos addr.s_addr = htonl(l);
1822 1.57 is (void)printf("\t%s", (l == 0) ? "0.0.0.0" : pr_addr(&addr));
1823 1.22 christos }
1824 1.22 christos
1825 1.22 christos
1826 1.22 christos /*
1827 1.22 christos * Return an ASCII host address
1828 1.22 christos * as a dotted quad and optionally with a hostname
1829 1.22 christos */
1830 1.22 christos static char *
1831 1.22 christos pr_addr(struct in_addr *addr) /* in network order */
1832 1.22 christos {
1833 1.22 christos struct hostent *hp;
1834 1.22 christos static char buf[MAXHOSTNAMELEN+4+16+1];
1835 1.22 christos
1836 1.22 christos if ((pingflags & F_NUMERIC)
1837 1.22 christos || !(hp = gethostbyaddr((char *)addr, sizeof(*addr), AF_INET))) {
1838 1.37 mycroft (void)snprintf(buf, sizeof(buf), "%s", inet_ntoa(*addr));
1839 1.22 christos } else {
1840 1.37 mycroft (void)snprintf(buf, sizeof(buf), "%s (%s)", hp->h_name,
1841 1.37 mycroft inet_ntoa(*addr));
1842 1.22 christos }
1843 1.22 christos
1844 1.22 christos return buf;
1845 1.1 cgd }
1846 1.1 cgd
1847 1.1 cgd /*
1848 1.22 christos * Dump some info on a returned (via ICMP) IP packet.
1849 1.1 cgd */
1850 1.22 christos static void
1851 1.30 christos pr_retip(struct icmp *icp,
1852 1.22 christos int cc)
1853 1.1 cgd {
1854 1.22 christos int hlen;
1855 1.30 christos u_char *cp;
1856 1.41 christos struct ip ipb, *ip = &ipb;
1857 1.26 christos
1858 1.41 christos (void) memcpy(ip, icp->icmp_data, sizeof(*ip));
1859 1.22 christos
1860 1.22 christos if (pingflags & F_VERBOSE)
1861 1.22 christos pr_iph(icp, cc);
1862 1.1 cgd
1863 1.41 christos hlen = ip->ip_hl << 2;
1864 1.30 christos cp = (u_char *) &icp->icmp_data[hlen];
1865 1.1 cgd
1866 1.41 christos if (ip->ip_p == IPPROTO_TCP) {
1867 1.22 christos if (pingflags & F_VERBOSE)
1868 1.30 christos (void)printf("\n TCP: from port %u, to port %u",
1869 1.22 christos (*cp*256+*(cp+1)), (*(cp+2)*256+*(cp+3)));
1870 1.41 christos } else if (ip->ip_p == IPPROTO_UDP) {
1871 1.22 christos if (pingflags & F_VERBOSE)
1872 1.30 christos (void)printf("\n UDP: from port %u, to port %u",
1873 1.22 christos (*cp*256+*(cp+1)), (*(cp+2)*256+*(cp+3)));
1874 1.41 christos } else if (ip->ip_p == IPPROTO_ICMP) {
1875 1.26 christos struct icmp icp2;
1876 1.26 christos (void) memcpy(&icp2, cp, sizeof(icp2));
1877 1.26 christos if (icp2.icmp_type == ICMP_ECHO) {
1878 1.26 christos if (pingflags & F_VERBOSE)
1879 1.30 christos (void)printf("\n ID=%u icmp_seq=%u",
1880 1.65 itojun ntohs((u_int16_t)icp2.icmp_id),
1881 1.65 itojun ntohs((u_int16_t)icp2.icmp_seq));
1882 1.26 christos else
1883 1.30 christos (void)printf(" for icmp_seq=%u",
1884 1.65 itojun ntohs((u_int16_t)icp2.icmp_seq));
1885 1.26 christos }
1886 1.22 christos }
1887 1.1 cgd }
1888 1.1 cgd
1889 1.22 christos static void
1890 1.22 christos fill(void)
1891 1.1 cgd {
1892 1.32 lukem int i, j, k;
1893 1.22 christos char *cp;
1894 1.1 cgd int pat[16];
1895 1.1 cgd
1896 1.22 christos for (cp = fill_pat; *cp != '\0'; cp++) {
1897 1.43 christos if (!isxdigit((unsigned char)*cp))
1898 1.22 christos break;
1899 1.22 christos }
1900 1.22 christos if (cp == fill_pat || *cp != '\0' || (cp-fill_pat) > 16*2) {
1901 1.22 christos (void)fflush(stdout);
1902 1.109 christos errx(EXIT_FAILURE, "\"-p %s\": patterns must be specified with"
1903 1.30 christos " 1-32 hex digits\n",
1904 1.30 christos fill_pat);
1905 1.22 christos }
1906 1.22 christos
1907 1.30 christos i = sscanf(fill_pat,
1908 1.30 christos "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x",
1909 1.30 christos &pat[0], &pat[1], &pat[2], &pat[3],
1910 1.30 christos &pat[4], &pat[5], &pat[6], &pat[7],
1911 1.30 christos &pat[8], &pat[9], &pat[10], &pat[11],
1912 1.22 christos &pat[12], &pat[13], &pat[14], &pat[15]);
1913 1.22 christos
1914 1.108 christos for (k = phdrlen, j = 0; k < datalen; k++) {
1915 1.22 christos opack_icmp.icmp_data[k] = pat[j];
1916 1.22 christos if (++j >= i)
1917 1.22 christos j = 0;
1918 1.22 christos }
1919 1.22 christos
1920 1.22 christos if (!(pingflags & F_QUIET)) {
1921 1.1 cgd (void)printf("PATTERN: 0x");
1922 1.22 christos for (j=0; j<i; j++)
1923 1.30 christos (void)printf("%02x",
1924 1.99 christos (u_char)opack_icmp.icmp_data[phdrlen + j]);
1925 1.1 cgd (void)printf("\n");
1926 1.1 cgd }
1927 1.22 christos
1928 1.1 cgd }
1929 1.1 cgd
1930 1.22 christos
1931 1.22 christos static void
1932 1.22 christos rnd_fill(void)
1933 1.22 christos {
1934 1.41 christos static u_int32_t rnd;
1935 1.22 christos int i;
1936 1.22 christos
1937 1.99 christos for (i = phdrlen; i < datalen; i++) {
1938 1.41 christos rnd = (3141592621U * rnd + 663896637U);
1939 1.41 christos opack_icmp.icmp_data[i] = rnd>>24;
1940 1.22 christos }
1941 1.22 christos }
1942 1.22 christos
1943 1.41 christos
1944 1.22 christos static void
1945 1.30 christos gethost(const char *arg,
1946 1.30 christos const char *name,
1947 1.30 christos struct sockaddr_in *sa,
1948 1.30 christos char *realname,
1949 1.30 christos int realname_len)
1950 1.22 christos {
1951 1.30 christos struct hostent *hp;
1952 1.22 christos
1953 1.33 cgd (void)memset(sa, 0, sizeof(*sa));
1954 1.30 christos sa->sin_family = AF_INET;
1955 1.69 itojun sa->sin_len = sizeof(struct sockaddr_in);
1956 1.22 christos
1957 1.30 christos /* If it is an IP address, try to convert it to a name to
1958 1.30 christos * have something nice to display.
1959 1.30 christos */
1960 1.30 christos if (inet_aton(name, &sa->sin_addr) != 0) {
1961 1.30 christos if (realname) {
1962 1.30 christos if (pingflags & F_NUMERIC)
1963 1.30 christos hp = 0;
1964 1.30 christos else
1965 1.30 christos hp = gethostbyaddr((char *)&sa->sin_addr,
1966 1.109 christos sizeof(sa->sin_addr), AF_INET);
1967 1.69 itojun (void)strlcpy(realname, hp ? hp->h_name : name,
1968 1.69 itojun realname_len);
1969 1.30 christos }
1970 1.22 christos return;
1971 1.30 christos }
1972 1.30 christos
1973 1.30 christos hp = gethostbyname(name);
1974 1.30 christos if (!hp)
1975 1.109 christos errx(EXIT_FAILURE, "Cannot resolve \"%s\" (%s)",
1976 1.109 christos name, hstrerror(h_errno));
1977 1.30 christos
1978 1.30 christos if (hp->h_addrtype != AF_INET)
1979 1.109 christos errx(EXIT_FAILURE, "%s only supported with IP", arg);
1980 1.30 christos
1981 1.41 christos (void)memcpy(&sa->sin_addr, hp->h_addr, sizeof(sa->sin_addr));
1982 1.30 christos
1983 1.69 itojun if (realname)
1984 1.69 itojun (void)strlcpy(realname, hp->h_name, realname_len);
1985 1.22 christos }
1986 1.22 christos
1987 1.22 christos
1988 1.22 christos static void
1989 1.22 christos usage(void)
1990 1.1 cgd {
1991 1.50 itojun #ifdef IPSEC
1992 1.50 itojun #ifdef IPSEC_POLICY_IPSEC
1993 1.50 itojun #define IPSECOPT "\n [-E policy] "
1994 1.50 itojun #else
1995 1.50 itojun #define IPSECOPT "\n [-AE] "
1996 1.50 itojun #endif /*IPSEC_POLICY_IPSEC*/
1997 1.50 itojun #else
1998 1.50 itojun #define IPSECOPT ""
1999 1.50 itojun #endif /*IPSEC*/
2000 1.42 enami
2001 1.74 jmmv (void)fprintf(stderr, "usage: \n"
2002 1.100 wiz "%s [-aCDdfLnoPQqRrv] [-c count] [-g gateway] [-h host]"
2003 1.100 wiz " [-I addr] [-i interval]\n"
2004 1.100 wiz " [-l preload] [-p pattern] [-s size] [-T ttl] [-t tos]"
2005 1.50 itojun " [-w maxwait] " IPSECOPT "host\n",
2006 1.59 cgd getprogname());
2007 1.1 cgd exit(1);
2008 1.1 cgd }
2009