-
Notifications
You must be signed in to change notification settings - Fork 134
/
Copy pathlib.h
128 lines (109 loc) · 3.26 KB
/
lib.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#ifndef __LIB_H
#define __LIB_H
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/syscall.h>
#include <fcntl.h>
#include <string.h>
#include <sys/ioctl.h>
#include <poll.h>
#include <errno.h>
#include <signal.h>
#include <assert.h>
#include <ctype.h>
#include <stdarg.h>
/* pthread */
#include <pthread.h>
/* Why does not pthread.h extern this function? */
extern int pthread_mutexattr_settype(pthread_mutexattr_t *, int);
typedef void *(*pfunc_t)(void *);
extern pthread_t threads[];
extern pthread_t newthread(pfunc_t thread_func);
#define gettid() syscall(SYS_gettid)
/* colour macro */
#define red(str) "\e[01;31m"#str"\e\[0m"
#define green(str) "\e[01;32m"#str"\e\[0m"
#define yellow(str) "\e[01;33m"#str"\e\[0m"
#define purple(str) "\e[01;35m"#str"\e\[0m"
#define grey(str) "\e[01;30m"#str"\e\[0m"
#define cambrigeblue(str) "\e[01;36m"#str"\e\[0m"
#define navyblue(str) "\e[01;34m"#str"\e\[0m"
#define blue(str) navyblue(str)
#define ferr(fmt, args...) fprintf(stderr, fmt, ##args)
#define dbg(fmt, args...) ferr("[%d]%s " fmt "\n", (int)gettid(), __FUNCTION__, ##args)
#define devdbg(fmt, args...)\
do {\
if (net_debug & NET_DEBUG_DEV)\
dbg(green(dev)" "fmt, ##args);\
} while (0)
#define l2dbg(fmt, args...)\
do {\
if (net_debug & NET_DEBUG_L2)\
dbg(yellow(l2)" "fmt, ##args);\
} while (0)
#define arpdbg(fmt, args...)\
do {\
if (net_debug & NET_DEBUG_ARP)\
dbg(red(arp)" "fmt, ##args);\
} while (0)
#define ipdbg(fmt, args...)\
do {\
if (net_debug & NET_DEBUG_IP)\
dbg(blue(ip)" "fmt, ##args);\
} while (0)
#define icmpdbg(fmt, args...)\
do {\
if (net_debug & NET_DEBUG_ICMP)\
dbg(purple(icmp)" "fmt, ##args);\
} while (0)
#define udpdbg(fmt, args...)\
do {\
if (net_debug & NET_DEBUG_UDP)\
dbg(purple(udp)" "fmt, ##args);\
} while (0)
#define tcpdbg(fmt, args...)\
do {\
if (net_debug & NET_DEBUG_TCP)\
dbg(purple(tcp)" "fmt, ##args);\
} while (0)
#define tcpsdbg(fmt, args...)\
do {\
if (net_debug & NET_DEBUG_TCPSTATE)\
dbg(green(tcpstate)" "fmt, ##args);\
} while (0)
#define NET_DEBUG_DEV 0x00000001
#define NET_DEBUG_L2 0x00000002
#define NET_DEBUG_ARP 0x00000004
#define NET_DEBUG_IP 0x00000008
#define NET_DEBUG_ICMP 0x00000010
#define NET_DEBUG_UDP 0x00000020
#define NET_DEBUG_TCP 0x00000040
#define NET_DEBUG_TCPSTATE 0x00000080
#define NET_DEBUG_ALL 0xffffffff
#define min(x,y) ({\
typeof(x) _x = (x);\
typeof(y) _y = (y);\
(void) (&_x == &_y);\
_x < _y ? _x : _y; })
extern unsigned int net_debug;
extern void *xmalloc(int);
extern void *xzalloc(int);
extern void perrx(char *str);
extern int str2ip(char *str, unsigned int *ip);
extern void printfs(int mlen, const char *fmt, ...);
extern int parse_ip_port(char *, unsigned int *, unsigned short *);
extern unsigned short ip_chksum(unsigned short *data, int size);
extern unsigned short icmp_chksum(unsigned short *data, int size);
extern unsigned short tcp_chksum(unsigned int src, unsigned dst,
unsigned short len, unsigned short *data);
extern unsigned short udp_chksum(unsigned int src, unsigned int dst,
unsigned short len, unsigned short *data);
struct ip;
struct udp;
struct tcp;
extern void udp_set_checksum(struct ip *, struct udp *);
extern void tcp_set_checksum(struct ip *, struct tcp *);
extern void ip_set_checksum(struct ip *);
#endif /* lib.h */