capt_get_packet(): check for key press only every 20ms
[iptraf-ng.git] / src / iptraf-ng-compat.h
blobaf17dd755592db50d8a5ca037b19a30966d8f825
1 #ifndef IPTRAF_NG_COMPAT_H
2 #define IPTRAF_NG_COMPAT_H
4 #include <stdlib.h>
5 #include <unistd.h>
6 #include <getopt.h>
7 #include <signal.h>
8 #include <string.h>
9 #include <time.h>
10 #include <fcntl.h>
11 #include <dirent.h>
12 #include <errno.h>
13 #include <ctype.h>
14 #include <netdb.h>
15 #include <curses.h>
16 #include <panel.h>
17 #include <assert.h>
18 #include <stddef.h>
19 #include <poll.h>
20 #include <limits.h>
21 #include <locale.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <sys/socket.h>
26 #include <sys/time.h>
27 #include <sys/ioctl.h>
28 #include <sys/wait.h>
29 #include <sys/un.h>
30 #include <sys/mman.h>
32 #include <netinet/in.h>
33 #include <netinet/udp.h>
34 #include <netinet/ip.h>
35 #include <netinet/tcp.h>
36 #include <netinet/ip6.h>
37 #include <netinet/icmp6.h>
38 #include <netinet/ip_icmp.h>
40 #include <arpa/inet.h>
42 #include <linux/if_ether.h>
43 #include <linux/if_packet.h>
44 #include <linux/if_fddi.h>
45 #include <linux/types.h>
47 #include <linux/if.h>
48 #include <linux/if_arp.h>
50 #ifndef ETH_P_8021AD
51 #define ETH_P_8021AD 0x88A8 /* 802.1ad Service VLAN */
52 #endif
54 #ifndef ETH_P_QINQ1
55 #define ETH_P_QINQ1 0x9100 /* deprecated QinQ VLAN [ NOT AN OFFICIALLY REGISTERED ID ] */
56 #endif
58 #ifndef ETH_P_QINQ2
59 #define ETH_P_QINQ2 0x9200 /* deprecated QinQ VLAN [ NOT AN OFFICIALLY REGISTERED ID ] */
60 #endif
62 #ifndef ETH_P_QINQ3
63 #define ETH_P_QINQ3 0x9300 /* deprecated QinQ VLAN [ NOT AN OFFICIALLY REGISTERED ID ] */
64 #endif
66 #define debug(...) \
67 do { \
68 fprintf(stderr, "%s:%s():%d:", \
69 __FILE__, __func__, __LINE__); \
70 fprintf(stderr, __VA_ARGS__); \
71 fprintf(stderr, "\n"); \
72 } while(0)
74 #define KBITS 0
76 #define dispmode(mode) \
77 (((mode) == KBITS) ? "kbps": "kBps")
79 #define __noreturn __attribute__((noreturn))
80 #define __unused __attribute__((unused))
81 #define __printf(x, y) __attribute__((format(printf, (x), (y))))
83 /* screen delay (in msecs) if update rate == 0 */
84 #define DEFAULT_UPDATE_DELAY 50
86 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
88 #define alloc_nr(x) (((x)+16)*3/2)
91 * Realloc the buffer pointed at by variable 'x' so that it can hold
92 * at least 'nr' entries; the number of entries currently allocated
93 * is 'alloc', using the standard growing factor alloc_nr() macro.
95 * DO NOT USE any expression with side-effect for 'x', 'nr', or 'alloc'.
97 #define ALLOC_GROW(x, nr, alloc) \
98 do { \
99 if ((nr) > alloc) { \
100 if (alloc_nr(alloc) < (nr)) \
101 alloc = (nr); \
102 else \
103 alloc = alloc_nr(alloc); \
104 x = xrealloc((x), alloc * sizeof(*(x))); \
106 } while (0)
108 extern int daemonized;
109 extern int exitloop;
111 extern void *xmalloc(size_t size);
112 extern void *xcalloc(size_t nmemb, size_t size);
113 extern void *xrealloc(void *ptr, size_t size);
114 extern void *xmallocz(size_t size);
115 extern char *xstrdup(const char *s);
116 extern int strtoul_ui(char const *s, int base, unsigned int *result);
117 extern int strtol_i(char const *s, int base, int *result);
119 extern void die(const char *err, ...) __noreturn __printf(1,2);
120 extern void die_errno(const char *fmt, ...) __noreturn __printf(1,2);
121 extern void error(const char *err, ...) __printf(1,2);
123 static inline char *skip_whitespace(char *str)
125 while (isspace(*str))
126 ++str;
128 return str;
131 static inline unsigned long timeval_diff_msec(const struct timeval *end,
132 const struct timeval *start)
134 if (!start || !end)
135 return 0UL;
137 signed long secs = end->tv_sec - start->tv_sec;
138 signed long usecs = end->tv_usec - start->tv_usec;
140 if(usecs < 0) {
141 usecs = 1000000 - usecs;
142 secs -= 1;
144 if(secs >= 0)
145 return secs * 1000UL + usecs / 1000UL;
146 else
147 return 0UL;
150 #endif /* IPTRAF_NG_COMPAT_H */