ping_common.h revision 7618e8104cac3bcc0ba509f4ba40644b665a3ac1
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#include <setjmp.h>
20
21#ifdef CAPABILITIES
22#include <sys/prctl.h>
23#include <sys/capability.h>
24#endif
25
26#ifdef USE_IDN
27#include <locale.h>
28#include <idna.h>
29#endif
30
31#include <netinet/in.h>
32#include <arpa/inet.h>
33#include <linux/types.h>
34#include <linux/errqueue.h>
35
36#ifdef ANDROID
37#include <linux/icmp.h>
38#endif
39
40#include "SNAPSHOT.h"
41
42#define	DEFDATALEN	(64 - 8)	/* default data length */
43
44#define	MAXWAIT		10		/* max seconds to wait for response */
45#define MININTERVAL	10		/* Minimal interpacket gap */
46#define MINUSERINTERVAL	200		/* Minimal allowed interval for non-root */
47
48#define SCHINT(a)	(((a) <= MININTERVAL) ? MININTERVAL : (a))
49
50/* various options */
51extern int options;
52#define	F_FLOOD		0x001
53#define	F_INTERVAL	0x002
54#define	F_NUMERIC	0x004
55#define	F_PINGFILLED	0x008
56#define	F_QUIET		0x010
57#define	F_RROUTE	0x020
58#define	F_SO_DEBUG	0x040
59#define	F_SO_DONTROUTE	0x080
60#define	F_VERBOSE	0x100
61#define	F_TIMESTAMP	0x200
62#define	F_FLOWINFO	0x200
63#define	F_SOURCEROUTE	0x400
64#define	F_TCLASS	0x400
65#define	F_FLOOD_POLL	0x800
66#define	F_LATENCY	0x1000
67#define	F_AUDIBLE	0x2000
68#define	F_ADAPTIVE	0x4000
69#define	F_STRICTSOURCE	0x8000
70#define F_NOLOOP	0x10000
71#define F_TTL		0x20000
72#define F_MARK		0x40000
73#define F_PTIMEOFDAY	0x80000
74#define F_OUTSTANDING	0x100000
75
76/*
77 * MAX_DUP_CHK is the number of bits in received table, i.e. the maximum
78 * number of received sequence numbers we can keep track of.
79 */
80#define	MAX_DUP_CHK	0x10000
81
82#if defined(__WORDSIZE) && __WORDSIZE == 64
83# define USE_BITMAP64
84#endif
85
86#ifdef USE_BITMAP64
87typedef __u64	bitmap_t;
88# define BITMAP_SHIFT	6
89#else
90typedef __u32	bitmap_t;
91# define BITMAP_SHIFT	5
92#endif
93
94#if ((MAX_DUP_CHK >> (BITMAP_SHIFT + 3)) << (BITMAP_SHIFT + 3)) != MAX_DUP_CHK
95# error Please MAX_DUP_CHK and/or BITMAP_SHIFT
96#endif
97
98struct rcvd_table {
99	bitmap_t bitmap[MAX_DUP_CHK / (sizeof(bitmap_t) * 8)];
100};
101
102extern struct rcvd_table rcvd_tbl;
103extern int using_ping_socket;
104
105#define	A(bit)	(rcvd_tbl.bitmap[(bit) >> BITMAP_SHIFT])	/* identify word in array */
106#define	B(bit)	(((bitmap_t)1) << ((bit) & ((1 << BITMAP_SHIFT) - 1)))	/* identify bit in word */
107
108static inline void rcvd_set(__u16 seq)
109{
110	unsigned bit = seq % MAX_DUP_CHK;
111	A(bit) |= B(bit);
112}
113
114static inline void rcvd_clear(__u16 seq)
115{
116	unsigned bit = seq % MAX_DUP_CHK;
117	A(bit) &= ~B(bit);
118}
119
120static inline bitmap_t rcvd_test(__u16 seq)
121{
122	unsigned bit = seq % MAX_DUP_CHK;
123	return A(bit) & B(bit);
124}
125
126extern u_char outpack[];
127extern int maxpacket;
128
129extern int datalen;
130extern char *hostname;
131extern int uid;
132extern int ident;			/* process id to identify our packets */
133
134extern int sndbuf;
135extern int ttl;
136
137extern long npackets;			/* max packets to transmit */
138extern long nreceived;			/* # of packets we got back */
139extern long nrepeats;			/* number of duplicates */
140extern long ntransmitted;		/* sequence # for outbound packets = #sent */
141extern long nchecksum;			/* replies with bad checksum */
142extern long nerrors;			/* icmp errors */
143extern int interval;			/* interval between packets (msec) */
144extern int preload;
145extern int deadline;			/* time to die */
146extern int lingertime;
147extern struct timeval start_time, cur_time;
148extern volatile int exiting;
149extern volatile int status_snapshot;
150extern int confirm;
151extern int confirm_flag;
152extern int working_recverr;
153
154extern volatile int in_pr_addr;		/* pr_addr() is executing */
155extern jmp_buf pr_addr_jmp;
156
157#ifndef MSG_CONFIRM
158#define MSG_CONFIRM 0
159#endif
160
161
162/* timing */
163extern int timing;			/* flag to do timing */
164extern long tmin;			/* minimum round trip time */
165extern long tmax;			/* maximum round trip time */
166extern long long tsum;			/* sum of all times, for doing average */
167extern long long tsum2;
168extern int rtt;
169extern __u16 acked;
170extern int pipesize;
171
172#define COMMON_OPTIONS \
173case 'a': case 'U': case 'c': case 'd': \
174case 'f': case 'i': case 'w': case 'l': \
175case 'S': case 'n': case 'p': case 'q': \
176case 'r': case 's': case 'v': case 'L': \
177case 't': case 'A': case 'W': case 'B': case 'm': \
178case 'D': case 'O':
179
180#define COMMON_OPTSTR "h?VQ:I:M:aUc:dfi:w:l:S:np:qrs:vLt:AW:Bm:DO"
181
182/*
183 * Write to stdout
184 */
185static inline void write_stdout(const char *str, size_t len)
186{
187	size_t o = 0;
188	ssize_t cc;
189	do {
190		cc = write(STDOUT_FILENO, str + o, len - o);
191		o += cc;
192	} while (len > o || cc < 0);
193}
194
195/*
196 * tvsub --
197 *	Subtract 2 timeval structs:  out = out - in.  Out is assumed to
198 * be >= in.
199 */
200static inline void tvsub(struct timeval *out, struct timeval *in)
201{
202	if ((out->tv_usec -= in->tv_usec) < 0) {
203		--out->tv_sec;
204		out->tv_usec += 1000000;
205	}
206	out->tv_sec -= in->tv_sec;
207}
208
209static inline void set_signal(int signo, void (*handler)(int))
210{
211	struct sigaction sa;
212
213	memset(&sa, 0, sizeof(sa));
214
215	sa.sa_handler = (void (*)(int))handler;
216#ifdef SA_INTERRUPT
217	sa.sa_flags = SA_INTERRUPT;
218#endif
219	sigaction(signo, &sa, NULL);
220}
221
222extern int __schedule_exit(int next);
223
224static inline int schedule_exit(int next)
225{
226	if (npackets && ntransmitted >= npackets && !deadline)
227		next = __schedule_exit(next);
228	return next;
229}
230
231static inline int in_flight(void)
232{
233	__u16 diff = (__u16)ntransmitted - acked;
234	return (diff<=0x7FFF) ? diff : ntransmitted-nreceived-nerrors;
235}
236
237static inline void acknowledge(__u16 seq)
238{
239	__u16 diff = (__u16)ntransmitted - seq;
240	if (diff <= 0x7FFF) {
241		if ((int)diff+1 > pipesize)
242			pipesize = (int)diff+1;
243		if ((__s16)(seq - acked) > 0 ||
244		    (__u16)ntransmitted - acked > 0x7FFF)
245			acked = seq;
246	}
247}
248
249static inline void advance_ntransmitted(void)
250{
251	ntransmitted++;
252	/* Invalidate acked, if 16 bit seq overflows. */
253	if ((__u16)ntransmitted - acked > 0x7FFF)
254		acked = (__u16)ntransmitted + 1;
255}
256
257extern void limit_capabilities(void);
258static int enable_capability_raw(void);
259static int disable_capability_raw(void);
260static int enable_capability_admin(void);
261static int disable_capability_admin(void);
262#ifdef CAPABILITIES
263extern int modify_capability(cap_value_t, cap_flag_value_t);
264static inline int enable_capability_raw(void)		{ return modify_capability(CAP_NET_RAW,   CAP_SET);   };
265static inline int disable_capability_raw(void)		{ return modify_capability(CAP_NET_RAW,   CAP_CLEAR); };
266static inline int enable_capability_admin(void)		{ return modify_capability(CAP_NET_ADMIN, CAP_SET);   };
267static inline int disable_capability_admin(void)	{ return modify_capability(CAP_NET_ADMIN, CAP_CLEAR); };
268#else
269extern int modify_capability(int);
270static inline int enable_capability_raw(void)		{ return modify_capability(1); };
271static inline int disable_capability_raw(void)		{ return modify_capability(0); };
272static inline int enable_capability_admin(void)		{ return modify_capability(1); };
273static inline int disable_capability_admin(void)	{ return modify_capability(0); };
274#endif
275extern void drop_capabilities(void);
276
277extern int send_probe(void);
278extern int receive_error_msg(void);
279extern int parse_reply(struct msghdr *msg, int len, void *addr, struct timeval *);
280extern void install_filter(void);
281extern int is_ours(uint16_t id);
282
283extern int pinger(void);
284extern void sock_setbufs(int icmp_sock, int alloc);
285extern void setup(int icmp_sock);
286extern void main_loop(int icmp_sock, __u8 *buf, int buflen) __attribute__((noreturn));
287extern void finish(void) __attribute__((noreturn));
288extern void status(void);
289extern void common_options(int ch);
290extern int gather_statistics(__u8 *ptr, int icmplen,
291			     int cc, __u16 seq, int hops,
292			     int csfailed, struct timeval *tv, char *from,
293			     void (*pr_reply)(__u8 *ptr, int cc));
294extern void print_timestamp(void);
295