• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <time.h>
5 #include <sys/param.h>
6 #include <sys/socket.h>
7 #include <linux/sockios.h>
8 #include <sys/file.h>
9 #include <sys/time.h>
10 #include <signal.h>
11 #include <sys/ioctl.h>
12 #include <net/if.h>
13 #include <sys/uio.h>
14 #include <sys/poll.h>
15 #include <ctype.h>
16 #include <errno.h>
17 #include <string.h>
18 #include <netdb.h>
19 
20 #include <netinet/in.h>
21 #include <arpa/inet.h>
22 #include <linux/errqueue.h>
23 
24 #define	DEFDATALEN	(64 - 8)	/* default data length */
25 
26 #define	MAXWAIT		10		/* max seconds to wait for response */
27 #define MININTERVAL	10		/* Minimal interpacket gap */
28 #define MINUSERINTERVAL	200		/* Minimal allowed interval for non-root */
29 
30 #define SCHINT(a)	(((a) <= MININTERVAL) ? MININTERVAL : (a))
31 
32 #define	A(bit)		rcvd_tbl[(bit)>>3]	/* identify byte in array */
33 #define	B(bit)		(1 << ((bit) & 0x07))	/* identify bit in byte */
34 #define	SET(bit)	(A(bit) |= B(bit))
35 #define	CLR(bit)	(A(bit) &= (~B(bit)))
36 #define	TST(bit)	(A(bit) & B(bit))
37 
38 /* various options */
39 extern int options;
40 #define	F_FLOOD		0x001
41 #define	F_INTERVAL	0x002
42 #define	F_NUMERIC	0x004
43 #define	F_PINGFILLED	0x008
44 #define	F_QUIET		0x010
45 #define	F_RROUTE	0x020
46 #define	F_SO_DEBUG	0x040
47 #define	F_SO_DONTROUTE	0x080
48 #define	F_VERBOSE	0x100
49 #define	F_TIMESTAMP	0x200
50 #define	F_FLOWINFO	0x200
51 #define	F_SOURCEROUTE	0x400
52 #define	F_TCLASS	0x400
53 #define	F_FLOOD_POLL	0x800
54 #define	F_LATENCY	0x1000
55 #define	F_AUDIBLE	0x2000
56 #define	F_ADAPTIVE	0x4000
57 #define	F_STRICTSOURCE	0x8000
58 #define F_NOLOOP	0x10000
59 #define F_TTL		0x20000
60 
61 /*
62  * MAX_DUP_CHK is the number of bits in received table, i.e. the maximum
63  * number of received sequence numbers we can keep track of.
64  */
65 #define	MAX_DUP_CHK	0x10000
66 extern int mx_dup_ck;
67 extern char rcvd_tbl[MAX_DUP_CHK / 8];
68 
69 
70 extern u_char outpack[];
71 extern int maxpacket;
72 
73 extern int datalen;
74 extern char *hostname;
75 extern int uid;
76 extern int ident;			/* process id to identify our packets */
77 
78 extern int sndbuf;
79 extern int ttl;
80 
81 extern long npackets;			/* max packets to transmit */
82 extern long nreceived;			/* # of packets we got back */
83 extern long nrepeats;			/* number of duplicates */
84 extern long ntransmitted;		/* sequence # for outbound packets = #sent */
85 extern long nchecksum;			/* replies with bad checksum */
86 extern long nerrors;			/* icmp errors */
87 extern int interval;			/* interval between packets (msec) */
88 extern int preload;
89 extern int deadline;			/* time to die */
90 extern int lingertime;
91 extern struct timeval start_time, cur_time;
92 extern volatile int exiting;
93 extern volatile int status_snapshot;
94 extern int confirm;
95 extern int confirm_flag;
96 extern int working_recverr;
97 
98 #ifndef MSG_CONFIRM
99 #define MSG_CONFIRM 0
100 #endif
101 
102 
103 /* timing */
104 extern int timing;			/* flag to do timing */
105 extern long tmin;			/* minimum round trip time */
106 extern long tmax;			/* maximum round trip time */
107 extern long long tsum;			/* sum of all times, for doing average */
108 extern long long tsum2;
109 extern int rtt;
110 extern __u16 acked;
111 extern int pipesize;
112 
113 #define COMMON_OPTIONS \
114 case 'a': case 'U': case 'c': case 'd': \
115 case 'f': case 'i': case 'w': case 'l': \
116 case 'S': case 'n': case 'p': case 'q': \
117 case 'r': case 's': case 'v': case 'L': \
118 case 't': case 'A': case 'W': case 'B':
119 
120 #define COMMON_OPTSTR "h?VQ:I:M:aUc:dfi:w:l:S:np:qrs:vLt:AW:B"
121 
122 
123 /*
124  * tvsub --
125  *	Subtract 2 timeval structs:  out = out - in.  Out is assumed to
126  * be >= in.
127  */
tvsub(struct timeval * out,struct timeval * in)128 static inline void tvsub(struct timeval *out, struct timeval *in)
129 {
130 	if ((out->tv_usec -= in->tv_usec) < 0) {
131 		--out->tv_sec;
132 		out->tv_usec += 1000000;
133 	}
134 	out->tv_sec -= in->tv_sec;
135 }
136 
set_signal(int signo,void (* handler)(int))137 static inline void set_signal(int signo, void (*handler)(int))
138 {
139 	struct sigaction sa;
140 
141 	memset(&sa, 0, sizeof(sa));
142 
143 	sa.sa_handler = (void (*)(int))handler;
144 #ifdef SA_INTERRUPT
145 	sa.sa_flags = SA_INTERRUPT;
146 #endif
147 	sigaction(signo, &sa, NULL);
148 }
149 
150 extern int __schedule_exit(int next);
151 
schedule_exit(int next)152 static inline int schedule_exit(int next)
153 {
154 	if (npackets && ntransmitted >= npackets && !deadline)
155 		next = __schedule_exit(next);
156 	return next;
157 }
158 
in_flight(void)159 static inline int in_flight(void)
160 {
161 	__u16 diff = (__u16)ntransmitted - acked;
162 	return (diff<=0x7FFF) ? diff : ntransmitted-nreceived-nerrors;
163 }
164 
acknowledge(__u16 seq)165 static inline void acknowledge(__u16 seq)
166 {
167 	__u16 diff = (__u16)ntransmitted - seq;
168 	if (diff <= 0x7FFF) {
169 		if ((int)diff+1 > pipesize)
170 			pipesize = (int)diff+1;
171 		if ((__s16)(seq - acked) > 0 ||
172 		    (__u16)ntransmitted - acked > 0x7FFF)
173 			acked = seq;
174 	}
175 }
176 
advance_ntransmitted(void)177 static inline void advance_ntransmitted(void)
178 {
179 	ntransmitted++;
180 	/* Invalidate acked, if 16 bit seq overflows. */
181 	if ((__u16)ntransmitted - acked > 0x7FFF)
182 		acked = (__u16)ntransmitted + 1;
183 }
184 
185 
186 extern int send_probe(void);
187 extern int receive_error_msg(void);
188 extern int parse_reply(struct msghdr *msg, int len, void *addr, struct timeval *);
189 extern void install_filter(void);
190 
191 extern int pinger(void);
192 extern void sock_setbufs(int icmp_sock, int alloc);
193 extern void setup(int icmp_sock);
194 extern void main_loop(int icmp_sock, __u8 *buf, int buflen) __attribute__((noreturn));
195 extern void finish(void) __attribute__((noreturn));
196 extern void status(void);
197 extern void common_options(int ch);
198 extern int gather_statistics(__u8 *ptr, int cc, __u16 seq, int hops,
199 			     int csfailed, struct timeval *tv, char *from);
200