rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 1989 The Regents of the University of California. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * This code is derived from software contributed to Berkeley by |
| 6 | * Mike Muuss. |
| 7 | * |
| 8 | * Redistribution and use in source and binary forms, with or without |
| 9 | * modification, are permitted provided that the following conditions |
| 10 | * are met: |
| 11 | * 1. Redistributions of source code must retain the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer. |
| 13 | * 2. Redistributions in binary form must reproduce the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer in the |
| 15 | * documentation and/or other materials provided with the distribution. |
| 16 | * 3. All advertising materials mentioning features or use of this software |
| 17 | * must display the following acknowledgement: |
| 18 | * This product includes software developed by the University of |
| 19 | * California, Berkeley and its contributors. |
| 20 | * 4. Neither the name of the University nor the names of its contributors |
| 21 | * may be used to endorse or promote products derived from this software |
| 22 | * without specific prior written permission. |
| 23 | * |
| 24 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 25 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 27 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 30 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 31 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 32 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 33 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 34 | * SUCH DAMAGE. |
| 35 | */ |
| 36 | |
| 37 | #ifndef PINGNETID_PING_COMMON_H |
| 38 | #define PINGNETID_PING_COMMON_H |
| 39 | |
| 40 | #include <stdio.h> |
| 41 | #include <stdlib.h> |
| 42 | #include <unistd.h> |
| 43 | #include <time.h> |
| 44 | #include <sys/param.h> |
| 45 | #include <sys/socket.h> |
| 46 | #include <linux/sockios.h> |
| 47 | #include <sys/file.h> |
| 48 | #include <sys/time.h> |
| 49 | #include <sys/signal.h> |
| 50 | #include <sys/ioctl.h> |
| 51 | #include <net/if.h> |
| 52 | #include <sys/uio.h> |
| 53 | #include <sys/poll.h> |
| 54 | #include <ctype.h> |
| 55 | #include <errno.h> |
| 56 | #include <string.h> |
| 57 | #include <netdb.h> |
| 58 | #include <setjmp.h> |
| 59 | |
| 60 | #ifdef CAPABILITIES |
| 61 | #include <sys/prctl.h> |
| 62 | #include <sys/capability.h> |
| 63 | #endif |
| 64 | |
| 65 | #ifdef USE_IDN |
| 66 | #include <locale.h> |
| 67 | #include <idna.h> |
| 68 | #endif |
| 69 | |
| 70 | #include <netinet/in.h> |
| 71 | #include <arpa/inet.h> |
| 72 | #include <linux/types.h> |
| 73 | #include <linux/errqueue.h> |
| 74 | |
| 75 | #ifdef ANDROID |
| 76 | #include <linux/icmp.h> |
| 77 | #include <sys/auxv.h> |
| 78 | #endif |
| 79 | |
| 80 | //#include "SNAPSHOT.h" |
| 81 | |
| 82 | #define DEFDATALEN (64 - 8) /* default data length */ |
| 83 | |
| 84 | #define MAXWAIT 10 /* max seconds to wait for response */ |
| 85 | #define MININTERVAL 10 /* Minimal interpacket gap */ |
| 86 | #define MINUSERINTERVAL 200 /* Minimal allowed interval for non-root */ |
| 87 | |
| 88 | #define SCHINT(a) (((a) <= MININTERVAL) ? MININTERVAL : (a)) |
| 89 | |
| 90 | struct in6_pktinfo { |
| 91 | struct in6_addr ipi6_addr; /* src/dst IPv6 address */ |
| 92 | unsigned int ipi6_ifindex; /* send/recv interface index */ |
| 93 | }; |
| 94 | |
| 95 | /* various options */ |
| 96 | extern int options; |
| 97 | #define F_FLOOD 0x001 |
| 98 | #define F_INTERVAL 0x002 |
| 99 | #define F_NUMERIC 0x004 |
| 100 | #define F_PINGFILLED 0x008 |
| 101 | #define F_QUIET 0x010 |
| 102 | #define F_RROUTE 0x020 |
| 103 | #define F_SO_DEBUG 0x040 |
| 104 | #define F_SO_DONTROUTE 0x080 |
| 105 | #define F_VERBOSE 0x100 |
| 106 | #define F_TIMESTAMP 0x200 |
| 107 | #define F_FLOWINFO 0x200 |
| 108 | #define F_SOURCEROUTE 0x400 |
| 109 | #define F_TCLASS 0x400 |
| 110 | #define F_FLOOD_POLL 0x800 |
| 111 | #define F_LATENCY 0x1000 |
| 112 | #define F_AUDIBLE 0x2000 |
| 113 | #define F_ADAPTIVE 0x4000 |
| 114 | #define F_STRICTSOURCE 0x8000 |
| 115 | #define F_NOLOOP 0x10000 |
| 116 | #define F_TTL 0x20000 |
| 117 | #define F_MARK 0x40000 |
| 118 | #define F_PTIMEOFDAY 0x80000 |
| 119 | #define F_OUTSTANDING 0x100000 |
| 120 | |
| 121 | /* |
| 122 | * MAX_DUP_CHK is the number of bits in received table, i.e. the maximum |
| 123 | * number of received sequence numbers we can keep track of. |
| 124 | */ |
| 125 | #define MAX_DUP_CHK 0x10000 |
| 126 | |
| 127 | #if defined(__WORDSIZE) && __WORDSIZE == 64 |
| 128 | # define USE_BITMAP64 |
| 129 | #endif |
| 130 | |
| 131 | #ifdef USE_BITMAP64 |
| 132 | typedef __u64 bitmap_t; |
| 133 | # define BITMAP_SHIFT 6 |
| 134 | #else |
| 135 | typedef __u32 bitmap_t; |
| 136 | # define BITMAP_SHIFT 5 |
| 137 | #endif |
| 138 | |
| 139 | #if ((MAX_DUP_CHK >> (BITMAP_SHIFT + 3)) << (BITMAP_SHIFT + 3)) != MAX_DUP_CHK |
| 140 | # error Please MAX_DUP_CHK and/or BITMAP_SHIFT |
| 141 | #endif |
| 142 | |
| 143 | struct rcvd_table { |
| 144 | bitmap_t bitmap[MAX_DUP_CHK / (sizeof(bitmap_t) * 8)]; |
| 145 | }; |
| 146 | |
| 147 | extern struct rcvd_table rcvd_tbl; |
| 148 | extern int using_ping_socket; |
| 149 | |
| 150 | #define A(bit) (rcvd_tbl.bitmap[(bit) >> BITMAP_SHIFT]) /* identify word in array */ |
| 151 | #define B(bit) (((bitmap_t)1) << ((bit) & ((1 << BITMAP_SHIFT) - 1))) /* identify bit in word */ |
| 152 | |
| 153 | static inline void rcvd_set(__u16 seq) |
| 154 | { |
| 155 | unsigned bit = seq % MAX_DUP_CHK; |
| 156 | A(bit) |= B(bit); |
| 157 | } |
| 158 | |
| 159 | static inline void rcvd_clear(__u16 seq) |
| 160 | { |
| 161 | unsigned bit = seq % MAX_DUP_CHK; |
| 162 | A(bit) &= ~B(bit); |
| 163 | } |
| 164 | |
| 165 | static inline bitmap_t rcvd_test(__u16 seq) |
| 166 | { |
| 167 | unsigned bit = seq % MAX_DUP_CHK; |
| 168 | return A(bit) & B(bit); |
| 169 | } |
| 170 | |
| 171 | extern u_char outpack[]; |
| 172 | extern int maxpacket; |
| 173 | |
| 174 | extern int datalen; |
| 175 | extern char *hostname; |
| 176 | extern int uid; |
| 177 | extern int ident; /* process id to identify our packets */ |
| 178 | |
| 179 | extern int sndbuf; |
| 180 | extern int ttl; |
| 181 | |
| 182 | extern long npackets; /* max packets to transmit */ |
| 183 | extern long nreceived; /* # of packets we got back */ |
| 184 | extern long nrepeats; /* number of duplicates */ |
| 185 | extern long ntransmitted; /* sequence # for outbound packets = #sent */ |
| 186 | extern long nchecksum; /* replies with bad checksum */ |
| 187 | extern long nerrors; /* icmp errors */ |
| 188 | extern int interval; /* interval between packets (msec) */ |
| 189 | extern int preload; |
| 190 | extern int deadline; /* time to die */ |
| 191 | extern int lingertime; |
| 192 | extern struct timeval start_time, cur_time; |
| 193 | extern volatile int exiting; |
| 194 | extern volatile int status_snapshot; |
| 195 | extern int confirm; |
| 196 | extern int confirm_flag; |
| 197 | extern int working_recverr; |
| 198 | |
| 199 | extern volatile int in_pr_addr; /* pr_addr() is executing */ |
| 200 | extern jmp_buf pr_addr_jmp; |
| 201 | |
| 202 | #ifndef MSG_CONFIRM |
| 203 | #define MSG_CONFIRM 0 |
| 204 | #endif |
| 205 | |
| 206 | |
| 207 | /* timing */ |
| 208 | extern int timing; /* flag to do timing */ |
| 209 | extern long tmin; /* minimum round trip time */ |
| 210 | extern long tmax; /* maximum round trip time */ |
| 211 | extern long long tsum; /* sum of all times, for doing average */ |
| 212 | extern long long tsum2; |
| 213 | extern int rtt; |
| 214 | extern __u16 acked; |
| 215 | extern int pipesize; |
| 216 | |
| 217 | #define COMMON_OPTIONS \ |
| 218 | case 'a': case 'U': case 'c': case 'd': \ |
| 219 | case 'f': case 'i': case 'w': case 'l': \ |
| 220 | case 'S': case 'n': case 'p': case 'q': \ |
| 221 | case 'r': case 's': case 'v': case 'L': \ |
| 222 | case 't': case 'A': case 'W': case 'B': case 'm': \ |
| 223 | case 'D': case 'O': |
| 224 | |
| 225 | #define COMMON_OPTSTR "h?VQ:I:M:aUc:dfi:w:l:S:np:qrs:vLt:AW:Bm:DO" |
| 226 | |
| 227 | /* |
| 228 | * Write to stdout |
| 229 | */ |
| 230 | static inline void write_stdout(const char *str, size_t len) |
| 231 | { |
| 232 | size_t o = 0; |
| 233 | ssize_t cc; |
| 234 | do { |
| 235 | cc = write(STDOUT_FILENO, str + o, len - o); |
| 236 | o += cc; |
| 237 | } while (len > o || cc < 0); |
| 238 | } |
| 239 | |
| 240 | /* |
| 241 | * tvsub -- |
| 242 | * Subtract 2 timeval structs: out = out - in. Out is assumed to |
| 243 | * be >= in. |
| 244 | */ |
| 245 | static inline void tvsub(struct timeval *out, struct timeval *in) |
| 246 | { |
| 247 | if ((out->tv_usec -= in->tv_usec) < 0) { |
| 248 | --out->tv_sec; |
| 249 | out->tv_usec += 1000000; |
| 250 | } |
| 251 | out->tv_sec -= in->tv_sec; |
| 252 | } |
| 253 | |
| 254 | static inline void set_signal(int signo, void (*handler)(int)) |
| 255 | { |
| 256 | struct sigaction sa; |
| 257 | |
| 258 | memset(&sa, 0, sizeof(sa)); |
| 259 | |
| 260 | sa.sa_handler = (void (*)(int))handler; |
| 261 | #ifdef SA_INTERRUPT |
| 262 | sa.sa_flags = SA_INTERRUPT; |
| 263 | #endif |
| 264 | sigaction(signo, &sa, NULL); |
| 265 | } |
| 266 | |
| 267 | extern int __schedule_exit(int next); |
| 268 | |
| 269 | static inline int schedule_exit(int next) |
| 270 | { |
| 271 | if (npackets && ntransmitted >= npackets && !deadline) |
| 272 | next = __schedule_exit(next); |
| 273 | return next; |
| 274 | } |
| 275 | |
| 276 | static inline int in_flight(void) |
| 277 | { |
| 278 | __u16 diff = (__u16)ntransmitted - acked; |
| 279 | return (diff<=0x7FFF) ? diff : ntransmitted-nreceived-nerrors; |
| 280 | } |
| 281 | |
| 282 | static inline void acknowledge(__u16 seq) |
| 283 | { |
| 284 | __u16 diff = (__u16)ntransmitted - seq; |
| 285 | if (diff <= 0x7FFF) { |
| 286 | if ((int)diff+1 > pipesize) |
| 287 | pipesize = (int)diff+1; |
| 288 | if ((__s16)(seq - acked) > 0 || |
| 289 | (__u16)ntransmitted - acked > 0x7FFF) |
| 290 | acked = seq; |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | static inline void advance_ntransmitted(void) |
| 295 | { |
| 296 | ntransmitted++; |
| 297 | /* Invalidate acked, if 16 bit seq overflows. */ |
| 298 | if ((__u16)ntransmitted - acked > 0x7FFF) |
| 299 | acked = (__u16)ntransmitted + 1; |
| 300 | } |
| 301 | |
| 302 | extern void limit_capabilities(void); |
| 303 | static int enable_capability_raw(void); |
| 304 | static int disable_capability_raw(void); |
| 305 | static int enable_capability_admin(void); |
| 306 | static int disable_capability_admin(void); |
| 307 | #ifdef CAPABILITIES |
| 308 | extern int modify_capability(cap_value_t, cap_flag_value_t); |
| 309 | static inline int enable_capability_raw(void) { return modify_capability(CAP_NET_RAW, CAP_SET); }; |
| 310 | static inline int disable_capability_raw(void) { return modify_capability(CAP_NET_RAW, CAP_CLEAR); }; |
| 311 | static inline int enable_capability_admin(void) { return modify_capability(CAP_NET_ADMIN, CAP_SET); }; |
| 312 | static inline int disable_capability_admin(void) { return modify_capability(CAP_NET_ADMIN, CAP_CLEAR); }; |
| 313 | #else |
| 314 | extern int modify_capability(int); |
| 315 | static inline int enable_capability_raw(void) { return modify_capability(1); }; |
| 316 | static inline int disable_capability_raw(void) { return modify_capability(0); }; |
| 317 | static inline int enable_capability_admin(void) { return modify_capability(1); }; |
| 318 | static inline int disable_capability_admin(void) { return modify_capability(0); }; |
| 319 | #endif |
| 320 | extern void drop_capabilities(void); |
| 321 | extern void android_check_security(void); |
| 322 | |
| 323 | extern int send_probe(void); |
| 324 | extern int receive_error_msg(void); |
| 325 | extern int parse_reply(struct msghdr *msg, int len, void *addr, struct timeval *); |
| 326 | extern void install_filter(void); |
| 327 | extern int is_ours(uint16_t id); |
| 328 | |
| 329 | extern int pinger(void); |
| 330 | extern void sock_setbufs(int icmp_sock, int alloc); |
| 331 | extern void sock_setmark(int icmp_sock); |
| 332 | extern void setup(int icmp_sock); |
| 333 | extern void main_loop(int icmp_sock, __u8 *buf, int buflen) __attribute__((noreturn)); |
| 334 | extern void finish(void) __attribute__((noreturn)); |
| 335 | extern void status(void); |
| 336 | extern void common_options(int ch); |
| 337 | extern int gather_statistics(__u8 *ptr, int icmplen, |
| 338 | int cc, __u16 seq, int hops, |
| 339 | int csfailed, struct timeval *tv, char *from, |
| 340 | void (*pr_reply)(__u8 *ptr, int cc)); |
| 341 | extern void print_timestamp(void); |
| 342 | extern int ping_netid_getaddrinfo_android(const char *nodename, struct sockaddr_in |
| 343 | **addr_in); |
| 344 | int ping6_netid_getaddrinfo_android(const char *nodename, |
| 345 | struct addrinfo **aihead); |
| 346 | |
| 347 | |
| 348 | #endif |