forked from phhusson/mptcp-proxy
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmptcpproxy_util.c
850 lines (670 loc) · 24.9 KB
/
mptcpproxy_util.c
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
//*****************************************************
//*****************************************************
//
// mptcpproxy_util.c
// Project: mptcp_proxy
//
//*****************************************************
//*****************************************************
//
// GEORG HAMPEL - Bell Labs/NJ/USA: All Rights Reserved
//
//*****************************************************
//*****************************************************
//*****************************************************
#include "mptcpproxy_util.h"
#include "sha1.h"
#include "hmac.h"
struct print_data prt_data;
struct print_msg_array prt_msg_array;
//+++new
uint32_t ip_whitelist[MAX_IP_WHITELIST_LEN] = {0};
uint16_t ip_whitelist_counter = 0;
//++++++++++++++++++++++++++++++++++++++++++++++++
//sn smaller
// returns 1 if a "<" b
//++++++++++++++++++++++++++++++++++++++++++++++++
int sn_smaller(uint32_t a, uint32_t b) { return ((a-b) > (b-a));}
//++++++++++++++++++++++++++++++++++++++++++++++++
//sn smaller_equal
// returns 1 if a "<=" b
//++++++++++++++++++++++++++++++++++++++++++++++++
int sn_smaller_equal(uint32_t a, uint32_t b){ return ((a-b) >= (b-a));}
//++++++++++++++++++++++++++++++++++++++++++++++++
//translate_SM_state
//++++++++++++++++++++++++++++++++++++++++++++++++
void translate_SM_state(int state_nb, char *state_str){
switch(state_nb){
case CLOSED: strcpy(state_str,"CLOSED"); break;
case SYN_SENT: strcpy(state_str,"SYN_SENT"); break;
case PRE_SYN_REC_1: strcpy(state_str,"PRE_SYN_REC_1"); break;
case PRE_SYN_REC_2: strcpy(state_str,"PRE_SYN_REC_2"); break;
case SYN_REC: strcpy(state_str,"SYN_REC"); break;
case PRE_EST: strcpy(state_str,"PRE_EST"); break;
case ESTABLISHED: strcpy(state_str,"ESTABLISHED"); break;
case FIN_WAIT_1: strcpy(state_str,"FIN_WAIT_1"); break;
case FIN_WAIT_2: strcpy(state_str,"FIN_WAIT_2"); break;
case PRE_CLOSING: strcpy(state_str,"PRE_CLOSING"); break;
case PRE_TIME_WAIT: strcpy(state_str,"PRE_TIME_WAIT"); break;
case CLOSING: strcpy(state_str,"CLOSING"); break;
case PRE_CLOSE_WAIT: strcpy(state_str,"PRE_CLOSE_WAIT"); break;
case CLOSE_WAIT: strcpy(state_str,"CLOSE_WAIT"); break;
case LAST_ACK: strcpy(state_str,"LAST_ACK"); break;
case TIME_WAIT: strcpy(state_str,"TIME_WAIT"); break;
default: strcpy(state_str,"UNRECK");
}
}
//++++++++++++++++++++++++++++++++++++++++++++++++
//translate_event_state
//++++++++++++++++++++++++++++++++++++++++++++++++
void translate_event_state(int event_nb, char *state_str){
switch(event_nb){
case RETRANSMIT: strcpy(state_str,"RETRANSMIT"); break;
case SESS_BREAK: strcpy(state_str,"SESS_BREAK"); break;
case SFL_BREAK: strcpy(state_str,"SFL_BREAK"); break;
case PRIO: strcpy(state_str,"PRIO"); break;
case SESS_CLOSE: strcpy(state_str,"SESS_CLOSE"); break;
case SFL_CLOSE: strcpy(state_str,"SFL_CLOSE"); break;
case REMOVE_ADDR: strcpy(state_str,"REMOVE_ADDR"); break;
default: strcpy(state_str,"UNRECK");
}
}
//++++++++++++++++++++++++++++++++++++++++++++++++
//handle_error
// Prints out a string and exits
//++++++++++++++++++++++++++++++++++++++++++++++++
inline void handle_error(char *message, int exit_flag) {
printf("%s\n", message);
if(exit_flag){
printf("exit set\n");
exit(1);
}
}
//++++++++++++++++++++++++++++++++++++++++++++++++
//print_msg
// Prints message
//++++++++++++++++++++++++++++++++++++++++++++++++
inline void print_msg(char *msg) {
printf("%s\n", msg);
}
//++++++++++++++++++++++++++++++++++++++++++++++++
//print_buffer
// Prints out a string and exits
//++++++++++++++++++++++++++++++++++++++++++++++++
inline void print_buffer(unsigned char *buf, uint16_t len, int hex_flag) {
int i;
for(i=0;i<len;i++) {
if(hex_flag) {
printf("%x.", buf[i]);
if(i%4 == 3) printf(" ");
} else {
printf("%u.", buf[i]);
if(i%4 == 3) printf(" ");
}
}
}
//++++++++++++++++++++++++++++++++++++++++++++++++
//sprint_buffer
// Prints buffer to string
//++++++++++++++++++++++++++++++++++++++++++++++++
extern inline void sprint_buffer(unsigned char *buf_in, char *str_out, uint16_t len, int hex_flag) {
int i;
str_out[0] = '\0';
char buf[5];
for(i=0;i<len;i++) {
if(hex_flag){
sprintf(buf, "%x.", buf_in[i]);
strcat(str_out, buf);
if(i%4 == 3) strcat(str_out," ");
} else {
sprintf(buf, "%u.", buf_in[i]);
strcat(str_out, buf);
if(i%4 == 3) strcat(str_out," ");
}
}
}
//++++++++++++++++++++++++++++++++++++++++++++++++
//void init_msg_data();
// Initializes msg printing
//++++++++++++++++++++++++++++++++++++++++++++++++
void init_msg_data() {
gettimeofday(&prt_msg_array.start, NULL);
prt_msg_array.prt_msgs = malloc(MAX_MSG_LINES * sizeof(struct print_msg*));//creates an array of pointers to msgs
prt_msg_array.nmb_msg = 0;
prt_msg_array.curr_msg_index = 0;
int i;
for(i=0;i<MAX_MSG_LINES; i++) {
prt_msg_array.prt_msgs[i] = malloc(sizeof(struct print_msg));
}
FILE* file = fopen(FILE_NAME_MSG_LOCAL, "w");
fprintf(file,"start\n");
fclose(file);
prt_msg_array.file_msg = fopen(FILE_NAME_MSG_LOCAL, "a");
}
//++++++++++++++++++++++++++++++++++++++++++++++++
//void add_msg(char* msg);
//Adds msg to msg array
//++++++++++++++++++++++++++++++++++++++++++++++++
void add_msg(char *msg){
// prt_msg_array.file_msg = fopen(FILE_NAME_MSG_LOCAL, "a");
// fprintf(prt_msg_array.file_msg,"%s\n", msg);
// fclose(prt_msg_array.file_msg);
if(!prt_msg_array.file_msg)
{
perror("Failed to open file");
return;
} else{
if(fprintf(prt_msg_array.file_msg,"%s\n", msg) <= 0)
perror("Failed to write to file");
}
// printf("%s\n",msg);
prt_msg_array.prt_msgs[prt_msg_array.curr_msg_index]->index = prt_msg_array.nmb_msg;
gettimeofday(&prt_msg_array.prt_msgs[prt_msg_array.curr_msg_index]->now, NULL);
if(strlen(msg) < MAX_MSG_LENGTH) {
strncpy(prt_msg_array.prt_msgs[prt_msg_array.curr_msg_index]->msg, msg, strlen(msg)+1);
} else {
strncpy(prt_msg_array.prt_msgs[prt_msg_array.curr_msg_index]->msg, msg, MAX_MSG_LENGTH);
prt_msg_array.prt_msgs[prt_msg_array.curr_msg_index]->msg[MAX_MSG_LENGTH] = '\0';
}
//inc msg counter
prt_msg_array.nmb_msg++;//with natural overflow
prt_msg_array.curr_msg_index++;
prt_msg_array.curr_msg_index %= MAX_MSG_LINES;//loops around
}
void write_msg_file(FILE *pfile, char* msg){
if(!pfile)
{
perror("Failed to open file");
return;
} else{
if(fprintf(pfile,"%s\n", msg) <= 0)
perror("Failed to write to file");
}
return;
}
void add_err_msg(char* msg) {
snprintf(msg_buf, MAX_MSG_LENGTH, "[Error] %s", msg);
add_msg(msg_buf);
}
//++++++++++++++++++++++++++++++++++++++++++++++++
//void terminate_msg_data();
// Prints prt_msg_array and closes file
//++++++++++++++++++++++++++++++++++++++++++++++++
void terminate_msg_data(){
prt_msg_array.file_msg = fopen(FILE_NAME_MSG, "w");
fprintf(prt_msg_array.file_msg, "index\ttime[s]\tmsg\n");
int i;
struct print_msg *prt;
if(prt_msg_array.curr_msg_index < prt_msg_array.nmb_msg) {//it has looped around yet
for(i=prt_msg_array.curr_msg_index; i<MAX_MSG_LINES;i++) {
prt = prt_msg_array.prt_msgs[i];
double dtime = 1.0 * (prt->now.tv_sec - prt_msg_array.start.tv_sec) + (prt->now.tv_usec - prt_msg_array.start.tv_usec)/1000000.0;
fprintf(prt_msg_array.file_msg,"%u\t%f\t%s\n", prt->index, dtime, prt->msg);
}
}
for(unsigned i=0; i<prt_msg_array.curr_msg_index;i++) {
prt = prt_msg_array.prt_msgs[i];
double dtime = 1.0 * (prt->now.tv_sec - prt_msg_array.start.tv_sec) + (prt->now.tv_usec - prt_msg_array.start.tv_usec)/1000000.0;
fprintf(prt_msg_array.file_msg,"%u\t%f\t%s\n", prt->index, dtime, prt->msg);
}
fclose(prt_msg_array.file_msg);
}
//++++++++++++++++++++++++++++++++++++++++++++++++
//void init_print_data();
// Initializes data printing
//++++++++++++++++++++++++++++++++++++++++++++++++
void init_print_data() {
gettimeofday(&prt_data.start, NULL);
prt_data.print_line_array = malloc(MAX_PRINT_LINES * sizeof(struct print_line*));//creates an array of pointers to printlines
if(PRINT_TABLE) prt_data.print_table_array = malloc(MAX_PRINT_LINES * sizeof(struct print_table*));
prt_data.nmb_lines = 0;
prt_data.nmb_tables = 0;
prt_data.do_print = 0;
int i;
for(i=0;i<MAX_PRINT_LINES; i++) {
prt_data.print_line_array[i] = malloc(sizeof(struct print_line));
if(PRINT_TABLE) prt_data.print_table_array[i] = malloc(sizeof(struct print_table));
}
}
//++++++++++++++++++++++++++++++++++++++++++++++++
//void load_print_line();
// loads data to packd.print_line
//++++++++++++++++++++++++++++++++++++++++++++++++
void load_print_line(uint32_t id, size_t hook,
size_t sess_id, size_t sfl_id,
int rex, uint32_t len, unsigned char flags,
uint32_t ssn, uint32_t san, uint32_t dsn, uint32_t dan,
unsigned char nb_sack_in, uint32_t *sack_in, unsigned char nb_sack_out, uint32_t *sack_out, int verdict) {
if(prt_data.nmb_lines >= MAX_PRINT_LINES) return;
packd.prt_line.id = id;
packd.prt_line.hook = hook;
packd.prt_line.sess_id = sess_id;
packd.prt_line.sfl_id = sfl_id;
packd.prt_line.rex = rex;
packd.prt_line.paylen = len;
packd.prt_line.flags = flags;
packd.prt_line.ssn = ssn;
packd.prt_line.san = san;
packd.prt_line.dsn = dsn;
packd.prt_line.dan = dan;
packd.prt_line.verdict = verdict;
unsigned char i;
unsigned char nb;
nb = (nb_sack_in < 3)? nb_sack_in:3;
for(i=0;i<(nb<<1);i++) packd.prt_line.sack_in[i] = sack_in[i];
for(i=(nb<<1);i<6;i++) packd.prt_line.sack_in[i] = 0;
packd.prt_line.nb_sack_in = nb_sack_in;
nb = (nb_sack_out < 3)? nb_sack_out:3;
for(i=0;i<(nb<<1);i++) packd.prt_line.sack_out[i] = sack_out[i];
for(i=(nb<<1);i<6;i++) packd.prt_line.sack_out[i] = 0;
packd.prt_line.nb_sack_out = nb_sack_out;
add_print_data();
}
//++++++++++++++++++++++++++++++++++++++++++++++++
//void load_print_table();
//++++++++++++++++++++++++++++++++++++++++++++++++
void load_print_table(uint32_t id, size_t hook, struct subflow *sfl) {
if(prt_data.nmb_tables >= MAX_PRINT_LINES) return;
if(sfl == NULL) return;
uint32_t i = prt_data.nmb_tables;
prt_data.print_table_array[i]->id = id;
prt_data.print_table_array[i]->hook = hook;
prt_data.print_table_array[i]->sfl_id = sfl->index;
struct map_table *map;
if(hook == 1) map = sfl->map_recv;
else map = sfl->map_send;
struct map_entry *e = map->top;
unsigned char j = 0;
uint32_t dsn_offset;
uint32_t ssn_offset;
if(hook == 1) {
dsn_offset = sfl->sess->idsn_rem;
ssn_offset = sfl->isn_rem;
}
else{
dsn_offset = sfl->sess->idsn_loc;
ssn_offset = sfl->isn_loc;
}
while(e && j < 10) {
prt_data.print_table_array[i]->dsn[j] = e->dsn - dsn_offset;
prt_data.print_table_array[i]->ssn[j] = e->ssn - ssn_offset;
prt_data.print_table_array[i]->range[j] = e->range;
e = e->next;
j++;
}
prt_data.print_table_array[i]->nb_entries = j;
prt_data.nmb_tables++;
}
//++++++++++++++++++++++++++++++++++++++++++++++++
//void add_print_data();
// adds a print_line to print_data
//++++++++++++++++++++++++++++++++++++++++++++++++
void add_print_data() {
if(!prt_data.do_print || prt_data.nmb_lines >= MAX_PRINT_LINES) return;
gettimeofday(&packd.prt_line.now, NULL);
memcpy(prt_data.print_line_array[prt_data.nmb_lines], &packd.prt_line, sizeof(struct print_line));
prt_data.nmb_lines++;
if(prt_data.nmb_lines == MAX_PRINT_LINES) terminate_print_data();
}
//++++++++++++++++++++++++++++++++++++++++++++++++
//void terminate_print_data();
// Prints print_line_array and closes file
//++++++++++++++++++++++++++++++++++++++++++++++++
void terminate_print_data() {
snprintf(msg_buf,MAX_MSG_LENGTH, "terminate_print_data: number of lines=%u", prt_data.nmb_lines);
add_msg(msg_buf);
prt_data.file1 = fopen(FILE_NAME_1, "w");
prt_data.file3 = fopen(FILE_NAME_3, "w");
snprintf(msg_buf,MAX_MSG_LENGTH, "terminate_print_data: file1 pnt=%lu", (long unsigned int) prt_data.file1);
add_msg(msg_buf);
fprintf(prt_data.file1,"id \thook \ttime \tsess \tsfl");
fprintf(prt_data.file1,"\trex \tsyn \tack \tfin \trst \tlen");
fprintf(prt_data.file1,"\tssn \tsan \tdsn \tdan");
fprintf(prt_data.file1,"\tnb_sack_ssn \tLsack_ssn_1 \tRsack_ssn_1 \tLsack_ssn_2 \tRsack_ssn_2 \tLsack_ssn_3 \tRsack_ssn_3");
fprintf(prt_data.file1,"\tnb_sack_dsn \tLsack_dsn_1 \tRsack_dsn_1 \tLsack_dsn_2 \tRsack_dsn_2 \tLsack_dsn_3 \tRsack_dsn_3");
fprintf(prt_data.file1,"\tverdict\n");
fprintf(prt_data.file3,"id \thook \ttime \tsess \tsfl");
fprintf(prt_data.file3,"\trex \tsyn \tack \tfin \trst \tlen");
fprintf(prt_data.file3,"\tssn \tsan \tdsn \tdan");
fprintf(prt_data.file3,"\tnb_sack_dsn \tLsack_dsn_1 \tRsack_dsn_1 \tLsack_dsn_2 \tRsack_dsn_2 \tLsack_dsn_3 \tRsack_dsn_3");
fprintf(prt_data.file3,"\tnb_sack_ssn \tLsack_ssn_1 \tRsack_ssn_1 \tLsack_ssn_2 \tRsack_ssn_2 \tLsack_ssn_3 \tRsack_ssn_3");
fprintf(prt_data.file3,"\tverdict\n");
for(unsigned i=0;i<prt_data.nmb_lines;i++) {
struct print_line *prt = prt_data.print_line_array[i];
double dtime = 1.0 * (prt->now.tv_sec - prt_data.start.tv_sec) + (prt->now.tv_usec - prt_data.start.tv_usec)/1000000.0;
if(prt->hook == 1){
fprintf(prt_data.file1,"%lu\t%zu\t%f\t%zu\t%zu", (long unsigned int) prt->id, prt->hook, dtime, prt->sess_id, prt->sfl_id);
fprintf(prt_data.file1,"\t%d\t%d\t%d\t%d\t%d\t%u",
prt->rex, (prt->flags)>>1 & 0x01,(prt->flags)>>4 & 0x01,(prt->flags) & 0x01,(prt->flags)>>2 & 0x01, prt->paylen);
fprintf(prt_data.file1,"\t%lu\t%lu",(long unsigned int) prt->ssn, (long unsigned int) prt->san);
fprintf(prt_data.file1,"\t%lu\t%lu",(long unsigned int) prt->dsn, (long unsigned int) prt->dan);
fprintf(prt_data.file1,"\t%u \t%lu \t%lu \t%lu \t%lu \t%lu \t%lu", prt->nb_sack_in,
(long unsigned) prt->sack_in[0], (long unsigned) prt->sack_in[1],
(long unsigned) prt->sack_in[2], (long unsigned) prt->sack_in[3],
(long unsigned) prt->sack_in[4], (long unsigned) prt->sack_in[5]);
fprintf(prt_data.file1,"\t%u \t%lu \t%lu \t%lu \t%lu \t%lu \t%lu", prt->nb_sack_out,
(long unsigned) prt->sack_out[0], (long unsigned) prt->sack_out[1],
(long unsigned) prt->sack_out[2], (long unsigned) prt->sack_out[3],
(long unsigned) prt->sack_out[4], (long unsigned) prt->sack_out[5]);
fprintf(prt_data.file1,"\t%d\n",prt->verdict);
}
else{
fprintf(prt_data.file3,"%lu\t%zu\t%f\t%zu\t%zu", (long unsigned int) prt->id, prt->hook, dtime, prt->sess_id, prt->sfl_id);
fprintf(prt_data.file3,"\t%d\t%d\t%d\t%d\t%d\t%u",
prt->rex, (prt->flags)>>1 & 0x01,(prt->flags)>>4 & 0x01,(prt->flags) & 0x01,(prt->flags)>>2 & 0x01, prt->paylen);
fprintf(prt_data.file3,"\t%lu\t%lu",(long unsigned int) prt->ssn, (long unsigned int) prt->san);
fprintf(prt_data.file3,"\t%lu\t%lu",(long unsigned int) prt->dsn, (long unsigned int) prt->dan);
fprintf(prt_data.file3,"\t%u \t%lu \t%lu \t%lu \t%lu \t%lu \t%lu", prt->nb_sack_in,
(long unsigned) prt->sack_in[0], (long unsigned) prt->sack_in[1],
(long unsigned) prt->sack_in[2], (long unsigned) prt->sack_in[3],
(long unsigned) prt->sack_in[4], (long unsigned) prt->sack_in[5]);
fprintf(prt_data.file3,"\t%u \t%lu \t%lu \t%lu \t%lu \t%lu \t%lu", prt->nb_sack_out,
(long unsigned) prt->sack_out[0], (long unsigned) prt->sack_out[1],
(long unsigned) prt->sack_out[2], (long unsigned) prt->sack_out[3],
(long unsigned) prt->sack_out[4], (long unsigned) prt->sack_out[5]);
fprintf(prt_data.file3,"\t%d\n",prt->verdict);
}
}
if(PRINT_TABLE) {
snprintf(msg_buf,MAX_MSG_LENGTH, "terminate_print_data: number of tables =%u", prt_data.nmb_tables);
add_msg(msg_buf);
prt_data.file10 = fopen(FILE_NAME_10, "w");
prt_data.file30 = fopen(FILE_NAME_30, "w");
fprintf(prt_data.file10,"id \thook \tsfl \n");
fprintf(prt_data.file30,"id \thook \tsfl \n");
for(unsigned i=0; i<prt_data.nmb_tables; i++) {
struct print_table *prtt = prt_data.print_table_array[i];
if(prtt->hook == 1) {
fprintf(prt_data.file10,"%lu\t%zu\t%zu\t", (long unsigned int) prtt->id, prtt->hook, prtt->sfl_id);
for(int j=0; j<prtt->nb_entries; j++) {
fprintf(prt_data.file10,"[%lu,%lu][%lu,%lu]\t",
(long unsigned) prtt->dsn[j], (long unsigned) (prtt->dsn[j] + prtt->range[j] - 1),
(long unsigned) prtt->ssn[j], (long unsigned) (prtt->ssn[j] + prtt->range[j] - 1));
}
fprintf(prt_data.file10,"\n");
} else{
fprintf(prt_data.file30,"%lu\t%zu\t%zu\t", (long unsigned int) prtt->id, prtt->hook, prtt->sfl_id);
for(int j=0; j<prtt->nb_entries; j++){
fprintf(prt_data.file30,"[%lu,%lu][%lu,%lu]\t",
(long unsigned) prtt->dsn[j], (long unsigned) (prtt->dsn[j] + prtt->range[j] - 1),
(long unsigned) prtt->ssn[j], (long unsigned) (prtt->ssn[j] + prtt->range[j] - 1));
}
fprintf(prt_data.file30,"\n");
}
}
}
fclose(prt_data.file1);
fclose(prt_data.file3);
fclose(prt_data.file10);
fclose(prt_data.file30);
}
//++++++++++++++++++++++++++++++++++++++++++++++++
//void print_sack(uint32_t *sack, unsigned char nb_sack);
// Prints sack array
//++++++++++++++++++++++++++++++++++++++++++++++++
void print_sack(uint32_t *sack, unsigned char nb_sack) {
int i;
printf("print_sack:\n");
for(i=0;i<nb_sack;i++) printf("[%lu, %lu] ", (long unsigned) sack[(i<<1)], (long unsigned) sack[(i<<1)+1]);
printf("\n");
}
//++++++++++++++++++++++++++++++++++++++++++++++++
//util: get_rand:
//++++++++++++++++++++++++++++++++++++++++++++++++
uint32_t get_rand() {
uint32_t nmb;
nmb = rand();
nmb += ( (rand()%2) <<31);
return nmb;
}
//++++++++++++++++++++++++++++++++++++++++++++++++
//util: create_key:
//++++++++++++++++++++++++++++++++++++++++++++++++
void create_key(uint32_t *key) {
*key = get_rand();
*(key + 1) = get_rand();
return;
}
//++++++++++++++++++++++++++++++++++++++++++++++++
//util: create IDSN: 32bit trunc of SHA1(key)
//++++++++++++++++++++++++++++++++++++++++++++++++
void create_idsn_token(uint32_t * const key, uint32_t *idsn, uint32_t *token, uint32_t *idsn_h) {
uint32_t resblock[5];
sha1_buffer ( (const char *) key, 8, (unsigned char *) resblock);
*token = (resblock[0]);
*idsn = ntohl( *( resblock+4) );
if(idsn_h)
*idsn_h = ntohl( *( resblock+3) );
}
//++++++++++++++++++++++++++++++++++++++++++++++++
//util: create_mac: 20B mac of key = keyA || keyB and msg = R_A || R_B
//++++++++++++++++++++++++++++++++++++++++++++++++
void create_mac(uint32_t *keyA, uint32_t *keyB, uint32_t rand_nmb_A, uint32_t rand_nmb_B, uint32_t* mac){
uint32_t key[4];
memcpy(key, keyA, 8);
memcpy(key+2, keyB, 8);
uint32_t msg[2];
msg[0] = rand_nmb_A;
msg[1] = rand_nmb_B;
hmac_sha1(key, 16, msg, 8, (void*) mac);
}
//++++++++++++++++++++++++++++++++++++++++++++++++
//util: create Token
//++++++++++++++++++++++++++++++++++++++++++++++++
inline uint32_t create_token(uint32_t idsn) {
//Token is derived from IDSN
return idsn;
}
//++++++++++++++++++++++++++++++++++++++++++++++++
//util: create ISSN
//++++++++++++++++++++++++++++++++++++++++++++++++
uint32_t create_issn() {
//since rand() only creates a positive long int,
// it covers only 31 bits.
// therefore we add a random bit at the 32 position
uint32_t ISSN = rand();
ISSN += ( (rand()%2) <<31);
return ISSN;
}
//++++++++++++++++++++++++++++++++++++++++++++++++
//util: convert IP address from uint32_t to char
// IP address must be in host format
//++++++++++++++++++++++++++++++++++++++++++++++++
inline void printIPaddr(uint32_t ipaddr) {
printf("%d.%d.%d.%d",(ipaddr>>24)&0xff,(ipaddr>>16)&0xff,(ipaddr>>8)&0xff,ipaddr&0xff);
}
//++++++++++++++++++++++++++++++++++++++++++++++++
//util: convert IP address from uint32_t to char
// IP address must be in host format
//++++++++++++++++++++++++++++++++++++++++++++++++
inline void sprintIPaddr(char* buf, uint32_t ipaddr) {
sprintf(buf, "%d.%d.%d.%d",(ipaddr>>24)&0xff,(ipaddr>>16)&0xff,(ipaddr>>8)&0xff,ipaddr&0xff);
return;
}
//++++++++++++++++++++++++++++++++++++++++++++++++
//util: printFourtuple
//++++++++++++++++++++++++++++++++++++++++++++++++
inline void printFourtuple(struct fourtuple *ft) {
printf("ipL=");
printIPaddr(ft->ip_loc);
printf(" ipR=");
printIPaddr(ft->ip_rem);
printf(" prtL=%u prtR=%u\n", ft->prt_loc, ft->prt_rem);
return;
}
//++++++++++++++++++++++++++++++++++++++++++++++++
//util: sprintFourtuple
//++++++++++++++++++++++++++++++++++++++++++++++++
void sprintFourtuple(char* buf, struct fourtuple *ft) {
char buf_loc[34];
char buf_rem[34];
sprintIPaddr(buf_loc, ft->ip_loc);
sprintIPaddr(buf_rem, ft->ip_rem);
sprintf(buf, "ipL=%s ipR=%s prtL=%u prtR=%u", buf_loc, buf_rem, ft->prt_loc, ft->prt_rem);
}
//++++++++++++++++++++++++++++++++++++++++++++++++
//util: mirrorFourtuple: switches loc and rem
//++++++++++++++++++++++++++++++++++++++++++++++++
void mirrorFourtuple(struct fourtuple *ft) {
uint32_t ip_buf = ft->ip_loc;
uint16_t prt_buf = ft->prt_loc;
ft->ip_loc = ft->ip_rem;
ft->prt_loc = ft->prt_rem;
ft->ip_rem = ip_buf;
ft->prt_rem = prt_buf;
}
//HERE COMES POINTER ARRAY
//++++++++++++++++++++++++++++++++++++++++++++++++
//util: init_pA: Initializes pntArray
//++++++++++++++++++++++++++++++++++++++++++++++++
void init_pA(struct pntArray *pa) {
pa->alloc = 10;
pa->pnts = malloc(pa->alloc * sizeof(void*));
pa->number = 0;
}
//++++++++++++++++++++++++++++++++++++++++++++++++
//util: add_pnt_pA: Adds pointer to pntArray
//++++++++++++++++++++++++++++++++++++++++++++++++
void add_pnt_pA(struct pntArray *pa, void *pnt) {
if(pa->number+1 >= pa->alloc){
pa->alloc+=10;
pa->pnts = realloc(pa->pnts, pa->alloc * sizeof(void*));
}
*(pa->pnts + pa->number) = pnt;
pa->number++;
}
//++++++++++++++++++++++++++++++++++++++++++++++++
//util: write_index_pA: Writes to pnt to index in pntArray
//++++++++++++++++++++++++++++++++++++++++++++++++
void write_pnt_pA(struct pntArray *pa, int index, void *pnt) {
if(index >= (int)pa->number || index < 0) return;
*(pa->pnts + index) = pnt;
}
//++++++++++++++++++++++++++++++++++++++++++++++++
//util: del_index_pA: Deletes pnt to index in pntArray and puts NULL in the spot
//++++++++++++++++++++++++++++++++++++++++++++++++
void del_index_pA(struct pntArray *pa, int index) {
if(index >= (int)pa->number || index < 0) return;
for(unsigned i = index; i< pa->number-1; i++){
*(pa->pnts+i) = *(pa->pnts +i+1);
}
pa->number--;
}
//++++++++++++++++++++++++++++++++++++++++++++++++
//util: get_index_pA: Gets index to pnt in pntArray
// -1 if not found
//++++++++++++++++++++++++++++++++++++++++++++++++
int get_index_pA(struct pntArray *pa, void *pnt) {
unsigned i=0;
while(i < pa->number && ( *(pa->pnts+i) != pnt) ) i++;
if( *(pa->pnts+i) != pnt) return -1;
return i;
}
//++++++++++++++++++++++++++++++++++++++++++++++++
//util: get_pnt_pA: Gets pnt to index in anyArray
// NULL if not found
//++++++++++++++++++++++++++++++++++++++++++++++++
void* get_pnt_pA(struct pntArray *pa, int index) {
if(index >= (int)pa->number || index < 0) return NULL;
return *(pa->pnts+index);
}
//++++++++++++++++++++++++++++++++++++++++++++++++
//util: del_pnt_pA: Deletes pnt from pntArray (if present)
//++++++++++++++++++++++++++++++++++++++++++++++++
void del_pnt_pA(struct pntArray *pa, void *pnt) {
int index = get_index_pA(pa, pnt);
del_index_pA(pa, index);
}
//++++++++++++++++++++++++++++++++++++++++++++++++
//util: clear_pA(index_pntArray* pa)
//++++++++++++++++++++++++++++++++++++++++++++++++
void clear_pA(struct pntArray *pa) {
pa->number = 0;
free(pa->pnts);
}
int system_safe(const char *command){
int systemRet = system(command);
if(systemRet == -1){
snprintf(msg_buf,MAX_MSG_LENGTH, "system return error");
add_msg(msg_buf);
}
return systemRet;
}
void hex_dump(const unsigned char *packet, size_t size)
{
/* unsigned char *byte = (unsigned char*)packet;
int count = 0;
int len = 0;
memset(msg_buf, 0, MAX_MSG_LENGTH);
snprintf(msg_buf, MAX_MSG_LENGTH, "\t\t");
len += 2;
for (; byte < ((unsigned char*)packet) + size; byte++) {
count++;
snprintf(msg_buf + len, MAX_MSG_LENGTH, "%02x ", *byte);
len += 3;
if (count % 16 == 0) {
add_msg(msg_buf);
len = 0;
snprintf(msg_buf + len, MAX_MSG_LENGTH, "\t\t");
len += 3;
}
}
add_msg(msg_buf);
*/
}
/*
* Options
// 0 - print log to stdout, 1 - output log to /tmp/log.txt
int opt_logging_to_file = 1;
int init_log()
{
log_file = fopen(FILE_NAME_MSG, "w");
if (log_file == NULL) {
fprintf(stderr, "Failed to open or create log file %s\n", FILE_NAME_MSG);
return -1;
}
chmod(FILE_NAME_MSG, 0644);
setbuf(log_file, NULL);
return 0;
}
int fin_log()
{
if (log_file != NULL) {
fclose(log_file);
}
return 0;
}
const char LEVEL_STR[][10] = {
"ERROR",
"WARNING",
"INFO",
"DEBUG",
"DEBUGV",
};
void log_func(int level, const char *fmt, ...)
{
va_list ap;
char buffer[1024];
char time_str[20];
time_t rawtime;
struct tm * timeinfo;
struct timespec ts;
double time_ts;
if (level > 4)
return;
time(&rawtime);
timeinfo = localtime(&rawtime);
strftime(time_str, 20, "%Y-%m-%d %H:%M:%S", timeinfo);
// a more acurate timestamp
clock_gettime(CLOCK_REALTIME, &ts);
time_ts = ts.tv_sec + ts.tv_nsec / 1000000000.0;
va_start(ap, fmt);
vsnprintf(buffer, sizeof(buffer), fmt, ap);
if (log_file != NULL) {
fprintf(log_file, "%lf [%s] %s\n", time_ts, LEVEL_STR[level], buffer);
} else {
fprintf(stdout, "%lf [%s] %s\n", time_ts, LEVEL_STR[level], buffer);
}
va_end(ap);
}
*/