ifpps: show link status
[netsniff-ng.git] / src / xutils.h
blobfdb3990b5f0e2957e0352d4f4c641156554b4869
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * By Daniel Borkmann <daniel@netsniff-ng.org>
4 * Copyright 2009, 2010 Daniel Borkmann.
5 * Copyright 2009, 2010 Emmanuel Roullit.
6 * Subject to the GPL, version 2.
7 */
9 #ifndef XSYS_H
10 #define XSYS_H
12 #define _GNU_SOURCE
13 #include <errno.h>
14 #include <stdint.h>
15 #include <sys/socket.h>
16 #include <unistd.h>
17 #include <linux/ethtool.h>
18 #include <linux/if.h>
19 #include <linux/wireless.h>
20 #include <poll.h>
21 #include <sys/poll.h>
22 #include <sched.h>
23 #include <sys/resource.h>
24 #include <sys/time.h>
26 #include "die.h"
27 #include "built_in.h"
29 enum {
30 sock_rmem_max = 0,
31 sock_rmem_def,
32 sock_wmem_max,
33 sock_wmem_def,
36 #define SMEM_SUG_MAX 104857600
37 #define SMEM_SUG_DEF 4194304
39 extern int af_socket(int af);
40 extern int pf_socket(void);
41 extern int adjust_dbm_level(int in_dbm, int dbm_val);
42 extern int wireless_sigqual(const char *ifname, struct iw_statistics *stats);
43 extern int wireless_rangemax_sigqual(const char *ifname);
44 extern u32 wireless_bitrate(const char *ifname);
45 extern u32 ethtool_bitrate(const char *ifname);
46 extern u32 device_bitrate(const char *ifname);
47 extern int ethtool_drvinf(const char *ifname, struct ethtool_drvinfo *drvinf);
48 extern int ethtool_link(const char *ifname);
49 extern int device_mtu(const char *ifname);
50 extern int device_address(const char *ifname, int af,
51 struct sockaddr_storage *ss);
52 extern int device_irq_number(const char *ifname);
53 extern int device_bind_irq_to_cpu(int irq, int cpu);
54 extern void sock_print_net_stats(int sock, unsigned long skipped);
55 extern int device_ifindex(const char *ifname);
56 extern short device_get_flags(const char *ifname);
57 extern void device_set_flags(const char *ifname, const short flags);
58 extern int set_nonblocking(int fd);
59 extern int set_nonblocking_sloppy(int fd);
60 extern int set_reuseaddr(int fd);
61 extern void set_sock_prio(int fd, int prio);
62 extern void set_tcp_cork(int fd);
63 extern void set_tcp_uncork(int fd);
64 extern void set_udp_cork(int fd);
65 extern void set_udp_uncork(int fd);
66 extern void set_sock_cork(int fd, int udp);
67 extern void set_sock_uncork(int fd, int udp);
68 extern void set_tcp_nodelay(int fd);
69 extern void set_socket_keepalive(int fd);
70 extern int set_ipv6_only(int fd);
71 extern void set_mtu_disc_dont(int fd);
72 extern int get_system_socket_mem(int which);
73 extern void set_system_socket_mem(int which, int val);
74 extern void register_signal(int signal, void (*handler)(int));
75 extern void register_signal_f(int signal, void (*handler)(int), int flags);
76 extern int get_tty_size(void);
77 extern short enter_promiscuous_mode(char *ifname);
78 extern void leave_promiscuous_mode(char *ifname, short oldflags);
79 extern int device_up(char *ifname);
80 extern int device_running(char *ifname);
81 extern int device_up_and_running(char *ifname);
82 extern int poll_error_maybe_die(int sock, struct pollfd *pfd);
83 extern void set_epoll_descriptor(int fd_epoll, int action,
84 int fd_toadd, int events);
85 extern int set_epoll_descriptor2(int fd_epoll, int action,
86 int fd_toadd, int events);
87 extern int set_cpu_affinity(char *str, int inverted);
88 extern int set_proc_prio(int prio);
89 extern int set_sched_status(int policy, int priority);
90 extern void ioprio_print(void);
91 extern void set_ioprio_rt(void);
92 extern void set_ioprio_be(void);
93 extern void xusleep(const struct timespec *ts_delay);
94 extern void xusleep2(long usecs);
95 extern int xnanosleep(double seconds);
96 extern int set_timeout(struct timeval *timeval, unsigned int msec);
97 extern size_t strlcpy(char *dest, const char *src, size_t size);
98 extern int slprintf(char *dst, size_t size, const char *fmt, ...);
99 extern noinline void *xmemset(void *s, int c, size_t n);
100 extern char *getuint(char *in, uint32_t *out);
101 extern char *strtrim_right(register char *p, register char c);
102 extern char *strtrim_left(register char *p, register char c);
104 static inline int get_default_sched_policy(void)
106 return SCHED_FIFO;
109 static inline int get_default_sched_prio(void)
111 return sched_get_priority_max(get_default_sched_policy());
114 static inline int get_number_cpus(void)
116 return sysconf(_SC_NPROCESSORS_CONF);
119 static inline int get_number_cpus_online(void)
121 return sysconf(_SC_NPROCESSORS_ONLN);
124 static inline int get_default_proc_prio(void)
126 return -20;
129 static inline struct timeval tv_subtract(struct timeval time1,
130 struct timeval time2)
132 /* time1 - time2 */
133 struct timeval result;
135 if ((time1.tv_sec < time2.tv_sec) || ((time1.tv_sec == time2.tv_sec) &&
136 (time1.tv_usec <= time2.tv_usec))) {
137 result.tv_sec = result.tv_usec = 0;
138 } else {
139 result.tv_sec = time1.tv_sec - time2.tv_sec;
140 if (time1.tv_usec < time2.tv_usec) {
141 result.tv_usec = time1.tv_usec + 1000000L -
142 time2.tv_usec;
143 result.tv_sec--;
144 } else {
145 result.tv_usec = time1.tv_usec - time2.tv_usec;
149 return result;
152 static inline char *skips(char *p)
154 return strtrim_left(p, ' ');
157 static inline char *skipchar(char *in, char c)
159 if (*in != c)
160 panic("Syntax error!\n");
161 return ++in;
164 #endif /* XSYS_H */