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