forked from wangyu-/tinyPortMapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.h
283 lines (230 loc) · 5.09 KB
/
common.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
* common.h
*
* Created on: Jul 29, 2017
* Author: wangyu
*/
#pragma once
//#ifndef COMMON_H_
//#define COMMON_H_
//#define __STDC_FORMAT_MACROS 1
#include <inttypes.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<getopt.h>
#include<unistd.h>
#include<errno.h>
#include <sys/epoll.h>
#include <sys/wait.h>
#include <sys/socket.h> //for socket ofcourse
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h> //for exit(0);
#include <errno.h> //For errno - the error number
#include <netinet/tcp.h> //Provides declarations for tcp header
#include <netinet/udp.h>
#include <netinet/ip.h> //Provides declarations for ip header
#include <netinet/if_ether.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <byteswap.h>
#include <arpa/inet.h>
#include <linux/if_ether.h>
#include <linux/filter.h>
#include <sys/time.h>
#include <time.h>
#include <sys/timerfd.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <net/if.h>
#include <arpa/inet.h>
#include <stdarg.h>
#include <assert.h>
#include <linux/if_packet.h>
#include <linux/if_tun.h>
#include<unordered_map>
#include<unordered_set>
#include<map>
#include<list>
#include <memory>
#include <vector>
//#include <pair>
using namespace std;
typedef unsigned long long u64_t; //this works on most platform,avoid using the PRId64
typedef long long i64_t;
typedef unsigned int u32_t;
typedef int i32_t;
typedef unsigned short u16_t;
typedef short i16_t;
typedef u64_t my_time_t;
//const int max_data_len=2200;
//const int buf_len=max_data_len+200;
const int max_addr_len=100;
const int max_data_len_udp=65536;
const int max_data_len_tcp=4096*4;
const u32_t conn_timeout_udp=180000;
const u32_t conn_timeout_tcp=360000;
const int max_conn_num=20000;
const int conn_clear_ratio=30;
const int conn_clear_min=1;
const u32_t conn_clear_interval=1000;
const u32_t timer_interval=400;//this should be smaller than heartbeat_interval and retry interval;
extern int about_to_exit;
extern int socket_buf_size;
typedef u64_t fd64_t;
struct not_copy_able_t
{
not_copy_able_t()
{
}
not_copy_able_t(const not_copy_able_t &other)
{
assert(0==1);
}
not_copy_able_t & operator=(const not_copy_able_t &other)
{
assert(0==1);
}
};
struct tcp_info_t:not_copy_able_t
{
fd64_t fd64;
epoll_event ev;
char * data;
//char data[max_data_len_tcp+200];//use a larger buffer than udp
char * begin;
int data_len;
tcp_info_t()
{
data=(char*)malloc(max_data_len_tcp+200);
begin=data;
data_len=0;
}
~tcp_info_t()
{
if(data)
delete data;
}
void free_memory()
{
delete data;
data=0;
begin=0;
}
};
u32_t djb2(unsigned char *str,int len);
u32_t sdbm(unsigned char *str,int len);
struct address_t //TODO scope id
{
struct hash_function
{
u32_t operator()(const address_t &key) const
{
return sdbm((unsigned char*)&key.inner,sizeof(key.inner));
}
};
union storage_t //sockaddr_storage is too huge, we dont use it.
{
sockaddr_in ipv4;
sockaddr_in6 ipv6;
};
storage_t inner;
address_t()
{
clear();
}
void clear()
{
memset(&inner,0,sizeof(inner));
}
int from_str(char * str);
int from_sockaddr(sockaddr *,socklen_t);
char* get_str();
void to_str(char *);
inline u32_t get_type()
{
return ((sockaddr*)&inner)->sa_family;
}
inline u32_t get_len()
{
u32_t type=get_type();
switch(type)
{
case AF_INET:
return sizeof(sockaddr_in);
case AF_INET6:
return sizeof(sockaddr_in6);
default:
assert(0==1);
}
return -1;
}
bool operator == (const address_t &b) const
{
//return this->data==b.data;
return memcmp(&this->inner,&b.inner,sizeof(this->inner))==0;
}
int new_connected_udp_fd();
};
struct udp_pair_t:not_copy_able_t
{
address_t adress;
fd64_t fd64;
u64_t last_active_time;
char addr_s[max_addr_len];
list<udp_pair_t>::iterator it;
udp_pair_t()
{
addr_s[0]=0;
}
//int not_used=0;
};
struct tcp_pair_t:not_copy_able_t
{
tcp_info_t local;
tcp_info_t remote;
u64_t last_active_time;
list<tcp_pair_t>::iterator it;
char addr_s[max_addr_len];
int not_used=0;
tcp_pair_t()
{
addr_s[0]=0;
}
};
struct fd_info_t:not_copy_able_t
{
int is_tcp=0;
tcp_pair_t *tcp_pair_p=0;
udp_pair_t *udp_pair_p=0;
};
u64_t get_current_time();
u64_t get_current_time_us();
u64_t pack_u64(u32_t a,u32_t b);
u32_t get_u64_h(u64_t a);
u32_t get_u64_l(u64_t a);
void write_u16(char *,u16_t a);
u16_t read_u16(char *);
void write_u32(char *,u32_t a);
u32_t read_u32(char *);
void write_u64(char *,u64_t a);
u64_t read_uu64(char *);
char * my_ntoa(u32_t ip);
void myexit(int a);
void init_random_number_fd();
u64_t get_true_random_number_64();
u32_t get_true_random_number();
u32_t get_true_random_number_nz();
u64_t ntoh64(u64_t a);
u64_t hton64(u64_t a);
bool larger_than_u16(uint16_t a,uint16_t b);
bool larger_than_u32(u32_t a,u32_t b);
void setnonblocking(int sock);
int set_buf_size(int fd,int socket_buf_size,int force_socket_buf=0);
void signal_handler(int sig);
void get_true_random_chars(char * s,int len);
int random_between(u32_t a,u32_t b);
int round_up_div(int a,int b);
int set_timer(int epollfd,int &timer_fd);
//#endif /* COMMON_H_ */