forked from wangyu-/UDPspeeder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.cpp
331 lines (292 loc) · 5.63 KB
/
common.cpp
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
* comm.cpp
*
* Created on: Jul 29, 2017
* Author: wangyu
*/
#include "common.h"
#include "log.h"
int about_to_exit=0;
raw_mode_t raw_mode=mode_faketcp;
unordered_map<int, const char*> raw_mode_tostring = {{mode_faketcp, "faketcp"}, {mode_udp, "udp"}, {mode_icmp, "icmp"}};
int socket_buf_size=1024*1024;
static int random_number_fd=-1;
char iptables_rule[200]="";
program_mode_t program_mode=unset_mode;//0 unset; 1client 2server
u64_t get_current_time()
{
timespec tmp_time;
clock_gettime(CLOCK_MONOTONIC, &tmp_time);
return tmp_time.tv_sec*1000+tmp_time.tv_nsec/(1000*1000l);
}
u64_t get_current_time_us()
{
timespec tmp_time;
clock_gettime(CLOCK_MONOTONIC, &tmp_time);
return (uint64_t(tmp_time.tv_sec))*1000llu*1000llu+ (uint64_t(tmp_time.tv_nsec))/1000llu;
}
u64_t pack_u64(u32_t a,u32_t b)
{
u64_t ret=a;
ret<<=32u;
ret+=b;
return ret;
}
u32_t get_u64_h(u64_t a)
{
return a>>32u;
}
u32_t get_u64_l(u64_t a)
{
return (a<<32u)>>32u;
}
char * my_ntoa(u32_t ip)
{
in_addr a;
a.s_addr=ip;
return inet_ntoa(a);
}
int add_iptables_rule(char * s)
{
strcpy(iptables_rule,s);
char buf[300]="iptables -I ";
strcat(buf,s);
if(system(buf)==0)
{
mylog(log_warn,"auto added iptables rule by: %s\n",buf);
}
else
{
mylog(log_fatal,"auto added iptables failed by: %s\n",buf);
myexit(-1);
}
return 0;
}
int clear_iptables_rule()
{
if(iptables_rule[0]!=0)
{
char buf[300]="iptables -D ";
strcat(buf,iptables_rule);
if(system(buf)==0)
{
mylog(log_warn,"iptables rule cleared by: %s \n",buf);
}
else
{
mylog(log_error,"clear iptables failed by: %s\n",buf);
}
}
return 0;
}
void init_random_number_fd()
{
random_number_fd=open("/dev/urandom",O_RDONLY);
if(random_number_fd==-1)
{
mylog(log_fatal,"error open /dev/urandom\n");
myexit(-1);
}
setnonblocking(random_number_fd);
}
u64_t get_true_random_number_64()
{
u64_t ret;
int size=read(random_number_fd,&ret,sizeof(ret));
if(size!=sizeof(ret))
{
mylog(log_fatal,"get random number failed %d\n",size);
myexit(-1);
}
return ret;
}
u32_t get_true_random_number()
{
u32_t ret;
int size=read(random_number_fd,&ret,sizeof(ret));
if(size!=sizeof(ret))
{
mylog(log_fatal,"get random number failed %d\n",size);
myexit(-1);
}
return ret;
}
u32_t get_true_random_number_nz() //nz for non-zero
{
u32_t ret=0;
while(ret==0)
{
ret=get_true_random_number();
}
return ret;
}
u64_t ntoh64(u64_t a)
{
if(__BYTE_ORDER == __LITTLE_ENDIAN)
{
return __bswap_64( a);
}
else return a;
}
u64_t hton64(u64_t a)
{
if(__BYTE_ORDER == __LITTLE_ENDIAN)
{
return __bswap_64( a);
}
else return a;
}
void setnonblocking(int sock) {
int opts;
opts = fcntl(sock, F_GETFL);
if (opts < 0) {
mylog(log_fatal,"fcntl(sock,GETFL)\n");
//perror("fcntl(sock,GETFL)");
myexit(1);
}
opts = opts | O_NONBLOCK;
if (fcntl(sock, F_SETFL, opts) < 0) {
mylog(log_fatal,"fcntl(sock,SETFL,opts)\n");
//perror("fcntl(sock,SETFL,opts)");
myexit(1);
}
}
/*
Generic checksum calculation function
*/
unsigned short csum(const unsigned short *ptr,int nbytes) {
register long sum;
unsigned short oddbyte;
register short answer;
sum=0;
while(nbytes>1) {
sum+=*ptr++;
nbytes-=2;
}
if(nbytes==1) {
oddbyte=0;
*((u_char*)&oddbyte)=*(u_char*)ptr;
sum+=oddbyte;
}
sum = (sum>>16)+(sum & 0xffff);
sum = sum + (sum>>16);
answer=(short)~sum;
return(answer);
}
int set_buf_size(int fd,int size)
{
//int socket_buf_size=1024*1024;
if(setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &socket_buf_size, sizeof(socket_buf_size))<0)
//if(setsockopt(fd, SOL_SOCKET, SO_SNDBUFFORCE, &socket_buf_size, sizeof(socket_buf_size))<0)
{
printf("set SO_SNDBUF fail\n");
exit(1);
}
//if(setsockopt(fd, SOL_SOCKET, SO_RCVBUFFORCE, &socket_buf_size, sizeof(socket_buf_size))<0)
if(setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &socket_buf_size, sizeof(socket_buf_size))<0)
{
printf("set SO_RCVBUF fail\n");
exit(1);
}
return 0;
}
void myexit(int a)
{
if(enable_log_color)
printf("%s\n",RESET);
clear_iptables_rule();
exit(a);
}
void signal_handler(int sig)
{
about_to_exit=1;
// myexit(0);
}
int numbers_to_char(id_t id1,id_t id2,id_t id3,char * &data,int &len)
{
static char buf[buf_len];
data=buf;
id_t tmp=htonl(id1);
memcpy(buf,&tmp,sizeof(tmp));
tmp=htonl(id2);
memcpy(buf+sizeof(tmp),&tmp,sizeof(tmp));
tmp=htonl(id3);
memcpy(buf+sizeof(tmp)*2,&tmp,sizeof(tmp));
len=sizeof(id_t)*3;
return 0;
}
int char_to_numbers(const char * data,int len,id_t &id1,id_t &id2,id_t &id3)
{
if(len<int(sizeof(id_t)*3)) return -1;
id1=ntohl( *((id_t*)(data+0)) );
id2=ntohl( *((id_t*)(data+sizeof(id_t))) );
id3=ntohl( *((id_t*)(data+sizeof(id_t)*2)) );
return 0;
}
bool larger_than_u32(u32_t a,u32_t b)
{
u32_t smaller,bigger;
smaller=min(a,b);//smaller in normal sense
bigger=max(a,b);
u32_t distance=min(bigger-smaller,smaller+(0xffffffff-bigger+1));
if(distance==bigger-smaller)
{
if(bigger==a)
{
return 1;
}
else
{
return 0;
}
}
else
{
if(smaller==b)
{
return 0;
}
else
{
return 1;
}
}
}
bool larger_than_u16(uint16_t a,uint16_t b)
{
uint16_t smaller,bigger;
smaller=min(a,b);//smaller in normal sense
bigger=max(a,b);
uint16_t distance=min(bigger-smaller,smaller+(0xffff-bigger+1));
if(distance==bigger-smaller)
{
if(bigger==a)
{
return 1;
}
else
{
return 0;
}
}
else
{
if(smaller==b)
{
return 0;
}
else
{
return 1;
}
}
}
void get_true_random_chars(char * s,int len)
{
int size=read(random_number_fd,s,len);
if(size!=len)
{
printf("get random number failed\n");
exit(-1);
}
}