-
Notifications
You must be signed in to change notification settings - Fork 10
/
miruo.c
2222 lines (2130 loc) · 54.2 KB
/
miruo.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
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "miruo.h"
miruopt opt;
miruopt_dpi dpi;
void version()
{
const char *libpcap = pcap_lib_version();
printf("%7s version %s\n",PACKAGE_NAME, PACKAGE_VERSION);
if(libpcap){
printf("%s\n", libpcap);
}
}
void usage()
{
version();
printf("usage: miruo [option] [expression]\n");
printf(" option\n");
printf(" -h, --help # help\n");
printf(" -V, --version # version\n");
printf(" -i, --interface=dev # eth0,bond0,any...\n");
printf(" -v, --view-data=NUM # \n");
printf(" -T, --long-connect=time[ms] # Threshold of connection time for lookup. Default 0ms(off)\n");
printf(" -t, --long-delay=time[ms] # Threshold of long delay time for lookup. Default 0ms(off)\n");
printf(" -r, --retransmit=time[ms] # Threshold of retransmit time for lookup. Default 1000ms\n");
printf(" -s, --stat=interval[sec] # statistics view interval. Default 0sec(off)\n");
printf(" -f, --file=file # read file(for tcpdump -w)\n");
printf(" -S, --syn=[0|1] # syn retransmit lookup mode.default=1. 0=ignore 1=lookup\n");
printf(" -R, --rst=[0|1|2] # rst lookup mode.default=1. (see README)\n");
printf(" -F, --fragment=[0|1] # ip fragment lookup. default=1\n");
printf(" -C, --color=[0|1] # color 0=off 1=on\n");
printf(" -L, --session-limit=NUM # active session limit. Default 1024\n");
printf(" -l, --segment-limit=NUM # active segment limit. Default 65536\n");
printf(" -m, --dpi-mode=mode # deep packet inspection mode. (now support only http)\n");
printf(" -q, --quiet # \n");
printf(" --all # all session lookup\n");
printf(" --live # live mode(all segment lookup)\n");
printf("\n");
printf(" expression: see man tcpdump\n");
printf("\n");
printf(" ex)\n");
printf(" miruo -i eth0 -s 10 2>statistics.log\n");
printf(" miruo -i eth0 host 192.168.0.100 and port 80\n");
printf(" miruo -i eth0 -T5000 src host 192.168.0.1\n");
}
int is_numeric(char *str)
{
if(str == NULL){
return(0);
}
if(*str == 0){
return(0);
}
if((*str == '-') || (*str == '+')){
str++;
}
while(*str){
if((*str < '0') || (*str > '9')){
return(0);
}
str++;
}
return(1);
}
int get_cpu_utilization()
{
int cpu;
uint64_t rus;
uint64_t cus;
struct timeval rtv;
struct timeval ctv;
struct timeval nctv;
struct timeval octv;
static struct rusage nrusa;
static struct rusage orusa;
static struct timeval ntime = {0,0};
static struct timeval otime = {0,0};
if(otime.tv_sec == 0){
getrusage(RUSAGE_SELF, &(orusa));
gettimeofday(&(otime), NULL);
return(0);
}
getrusage(RUSAGE_SELF, &(nrusa));
gettimeofday(&(ntime), NULL);
timeradd(&(nrusa.ru_stime), &(nrusa.ru_utime), &nctv);
timeradd(&(orusa.ru_stime), &(orusa.ru_utime), &octv);
timersub(&(ntime), &(otime), &rtv);
timersub(&(nctv), &(octv), &ctv);
cus = ctv.tv_sec;
cus *= 1000000;
cus += ctv.tv_usec;
rus = rtv.tv_sec;
rus *= 1000000;
rus += rtv.tv_usec;
cpu = rus ? cus * 1000 / rus : 0;
cpu = (cpu > 1000) ? 1000 : cpu;
memcpy(&(otime), &(ntime), sizeof(struct timeval));
memcpy(&(orusa), &(nrusa), sizeof(struct rusage));
return(cpu);
}
meminfo *get_memmory()
{
int i;
int f;
char *r;
char buff[256], cmd[256];
static meminfo mi;
FILE *fp;
memset(buff, 0, sizeof(buff));
mi.page_size = sysconf(_SC_PAGESIZE);
f = open("/proc/self/statm", O_RDONLY);
if(f == -1){
memset(&mi, 0, sizeof(meminfo));
snprintf(cmd, sizeof(cmd), "/bin/ps -o pid=,vsz=,rss= -p %d", getpid());
fp = popen(cmd, "r");
if(fp == NULL){
return(NULL);
}
fread(buff, 1, sizeof(buff) -1, fp);
pclose(fp);
i = 1;
r = strtok(buff, " ");
if(!r || atoi(r) != getpid()){
return(NULL);
}
while(r=strtok(NULL, " ")){
switch(++i){
case 2:
mi.vsz = (uint64_t)atoi(r) * 1024;
break;
case 3:
mi.res = (uint64_t)atoi(r) * 1024;
break;
}
}
return(&mi);
}
read(f, buff, sizeof(buff) - 1);
close(f);
i = 1;
r = strtok(buff, " ");
mi.vsz = atoi(r) * mi.page_size;
while(r=strtok(NULL, " ")){
switch(++i){
case 2:
mi.res = atoi(r) * mi.page_size;
break;
case 3:
mi.share = atoi(r) * mi.page_size;
break;
case 4:
mi.text = atoi(r) * mi.page_size;
break;
case 6:
mi.data = atoi(r) * mi.page_size;
break;
}
}
return(&mi);
}
void get_kmg_string(uint8_t *str, uint64_t val)
{
if(val > 1024 * 1024 * 1024){
sprintf(str, "%lluGB", val / 1024 / 1024 / 1024);
}else if(val > 1024 * 1024){
sprintf(str, "%lluMB", val / 1024 / 1024);
}else if(val > 1024){
sprintf(str, "%lluKB", val / 1024);
}else{
sprintf(str, "%lluB", val);
}
}
uint16_t get_l3type(l2hdr *hdr)
{
switch(opt.lktype){
case DLT_EN10MB:
return(hdr->hdr.eth.type);
case DLT_LINUX_SLL:
return(hdr->hdr.sll.type);
}
return(0);
}
char *tcp_state_str(int state)
{
static char *state_string[]={
"-",
"LISTEN",
"SYN_SENT",
"SYN_RECV",
"ESTABLISHED",
"FIN_WAIT1",
"FIN_WAIT2",
"CLOSE_WAIT",
"LAST_ACK",
"CLOSED",
"TIME_WAIT",
};
return(state_string[state]);
}
char *tcp_flag_str(uint8_t flags)
{
static char fstr[16];
strcpy(fstr, "------");
if(flags & 1){
fstr[5]='F';
}
if(flags & 2){
fstr[4]='S';
}
if(flags & 4){
fstr[3]='R';
}
if(flags & 8){
fstr[2]='P';
}
if(flags & 16){
fstr[1]='A';
}
if(flags & 32){
fstr[0]='U';
}
return(fstr);
}
char *tcp_opt_str(uint8_t *opt, uint8_t optsize)
{
static uint8_t optstr[256];
optstr[0] = 0;
uint8_t t = 0;
uint8_t l = 0;
uint32_t d0 = 0;
uint32_t d1 = 0;
uint8_t buf[32];
while(optsize){
t = *(opt++);
if(t == 0){
break; /* END */
}
if(t == 1){
continue; /* NO-OP */
}
d0 = 0;
d1 = 0;
l = *(opt++);
switch(l-2){
case 1:
d0 = (uint32_t)(*((uint8_t *)opt));
break;
case 2:
d0 = (uint32_t)(ntohs(*((uint16_t *)opt)));
break;
case 4:
d0 = (uint32_t)(ntohl(*((uint32_t *)opt)));
break;
case 8:
d0 = ntohl(*((uint32_t *)(opt + 0)));
d1 = ntohl(*((uint32_t *)(opt + 4)));
break;
}
switch(t){
case 2:
sprintf(buf, "mss=%u", d0);
break;
case 3:
sprintf(buf, "wscale=%u", d0);
break;
case 4:
sprintf(buf, "sackOK");
break;
case 5:
sprintf(buf, "sack 1"); /* len = 10 */
break;
case 8:
sprintf(buf, "timestamp %u %u", d0, d1);
break;
default:
sprintf(buf, "len=%hhu opt[%hhu]", l, t);
break;
}
opt += (l - 2);
optsize -= l;
if(*optstr){
strcat(optstr, ", ");
}
strcat(optstr, buf);
}
return(optstr);
}
uint64_t get_keika_time(struct timeval *stv, struct timeval *etv)
{
struct timeval tv;
if(timercmp(etv, stv, <)){
return(0);
}
timersub(etv, stv, &tv);
return(tv.tv_sec * 1000000 + tv.tv_usec);
}
uint32_t tcp_connection_time(tcpsession *c)
{
uint32_t t;
struct timeval ct;
timerclear(&ct);
if((c != NULL) && (c->last != NULL)){
timersub(&(c->last->ts), &(c->segment.ts), &ct);
}
t = ct.tv_sec * 1000;
t += ct.tv_usec / 1000;
return(t);
}
tcpsegment *tcp_retransmit_segment(tcpsession *c, tcpsegment *t)
{
tcpsegment *p;
if(c == NULL){
return(NULL);
}
for(p=&(c->segment);p;p=p->next){
if((p->seqno == t->seqno) &&
(p->ackno == t->ackno) &&
(p->flags == t->flags) &&
(p->segsz == t->segsz)){
break;
}
}
return(p);
}
int is_tcp_retransmit_ignore(tcpsegment *p1, tcpsegment *p2)
{
int64_t delay;
if((p1->flags & 2) != 0){
return(opt.rsynfind == 0);
}
if(opt.rt_limit == 0){
return(1);
}
delay = p1->ts.tv_sec;
delay -= p2->ts.tv_sec;
delay *= 1000000;
delay += p1->ts.tv_usec;
delay -= p2->ts.tv_usec;
delay /= 1000;
delay = (delay > 0) ? delay : -delay;
return(delay < opt.rt_limit);
}
int is_tcpsession_closed(tcpsession *c){
if(c == NULL){
return(0);
}
if((c->st[0] == MIRUO_STATE_TCP_CLOSED) && (c->st[1] == MIRUO_STATE_TCP_CLOSED)){
return(1);
}
if((c->st[0] == MIRUO_STATE_TCP_TIME_WAIT) && (c->st[1] == MIRUO_STATE_TCP_CLOSED)){
return(1);
}
if((c->st[0] == MIRUO_STATE_TCP_CLOSED) && (c->st[1] == MIRUO_STATE_TCP_TIME_WAIT)){
return(1);
}
return(0);
}
/***********************************************************************
* ここからデバッグ用の関数だよ
***********************************************************************/
void debug_dumpdata(uint8_t *data, int size)
{
int i;
for(i=0;i<size;i++){
printf("%02X ", (uint32_t)(*(data + i)));
if((i % 16) == 15){
printf("\n");
}
}
printf("\n");
}
void debug_print_mac(char *mac)
{
int i=0;
printf("%02X",mac[i]);
for(i=1;i<6;i++){
printf(":%02X",mac[i]);
}
}
void debug_print_iphdr(iphdr *h)
{
int i;
printf("======= IP HEADER =======\n");
printf("Ver : %hhu\n", h->ver);
printf("IHL : %hhu\n", h->ihl);
printf("TOS : %hhu\n", h->tos);
printf("LEN : %hu\n", h->len);
printf("ID : %hu\n", h->id);
printf("Flags : %hhu\n", h->flags);
printf("Offset : %hu\n", h->offset);
printf("TTL : %hhu\n", h->ttl);
printf("Protocol: %hhu\n", h->protocol);
printf("Checksum: %hX\n", h->checksum);
printf("SrcAddr : %s\n", inet_ntoa(h->src));
printf("DstAddr : %s\n", inet_ntoa(h->dst));
for(i=0;i<(h->ihl - 20);i++){
printf("option[%02d]: %02x\n", h->option[i]);
}
}
void debug_print_tcphdr(tcphdr *h)
{
char *flags = tcp_flag_str(h->flags);
printf("======= TCP HEADER =======\n");
printf("sport : %hu\n", h->sport);
printf("dport : %hu\n", h->dport);
printf("seqno : %u\n", h->seqno);
printf("ackno : %u\n", h->ackno);
printf("offset : %hhu\n", h->offset);
printf("flags : %s\n", flags);
printf("window : %hu\n", h->window);
printf("checksum: 0x%04hx\n", h->checksum);
debug_dumpdata(h->opt, h->offset - 20);
}
void debug_timer(int n)
{
static struct timeval tv[3];
if(n == 0){
gettimeofday(&(tv[0]), NULL);
return;
}
gettimeofday(&(tv[1]), NULL);
timersub(&(tv[1]), &(tv[0]), &(tv[2]));
fprintf(stderr, "debug time=%d.%06d\n", tv[2].tv_sec, tv[2].tv_usec);
}
/***********************************************************************
* ここまでデバッグ用の関数だよ
***********************************************************************/
uint8_t *read_header_eth(ethhdr *h, uint8_t *p, uint32_t *l)
{
if(*l < sizeof(ethhdr)){
opt.err_l2++;
return(NULL);
}
memcpy(h, p, sizeof(ethhdr));
h->type = ntohs(h->type);
p += sizeof(ethhdr);
*l -= sizeof(ethhdr);
return(p);
}
uint8_t *read_header_sll(sllhdr *h, uint8_t *p, uint32_t *l)
{
if(*l < sizeof(sllhdr)){
opt.err_l2++;
return(NULL);
}
memcpy(h, p, sizeof(sllhdr));
h->type = ntohs(h->type);
p += sizeof(sllhdr);
*l -= sizeof(sllhdr);
return(p);
}
uint8_t *read_header_l2(l2hdr *h, int lktype, uint8_t *p, uint32_t *l){
switch(lktype){
case DLT_EN10MB:
return read_header_eth(&(h->hdr.eth), p, l);
case DLT_LINUX_SLL:
return read_header_sll(&(h->hdr.sll), p, l);
}
return(NULL);
}
#if defined(__APPLE__) && defined(DLT_PKTAP)
#include <net/if.h>
#include <sys/param.h>
#define PKTAP_IFXNAMESIZE (IF_NAMESIZE + 8)
/*
* Header for DLT_PKTAP
*
* In theory, there could be several types of blocks in a chain before the actual packet
*/
struct pktap_header {
uint32_t pth_length; /* length of this header */
uint32_t pth_type_next; /* type of data following */
uint32_t pth_dlt; /* DLT of packet */
char pth_ifname[PKTAP_IFXNAMESIZE]; /* interface name */
uint32_t pth_flags; /* flags */
uint32_t pth_protocol_family;
uint32_t pth_frame_pre_length;
uint32_t pth_frame_post_length;
pid_t pth_pid; /* process ID */
char pth_comm[MAXCOMLEN+1]; /* process command name */
uint32_t pth_svc; /* service class */
uint16_t pth_iftype;
uint16_t pth_ifunit;
pid_t pth_epid; /* effective process ID */
char pth_ecomm[MAXCOMLEN+1]; /* effective command name */
};
#endif
uint8_t *read_header_ip(iphdr *h, uint8_t *p, uint32_t *l)
{
int optlen;
iphdraw *hr;
int lktype;
lktype = opt.lktype;
#if defined(__APPLE__) && defined(DLT_PKTAP)
if(lktype == DLT_PKTAP){ // Apple PKTAP
struct pktap_header *pth;
pth = (struct pktap_header *)p;
p += pth->pth_length;
*l -= pth->pth_length;
lktype = pth->pth_dlt;
if(pth->pth_protocol_family != PF_INET){
return(NULL);
}
}
#endif
switch(lktype){
case DLT_RAW: // RAW IP
hr = (iphdraw *)p;
break;
case DLT_NULL: // BSD Loopback
if(*((uint32_t *)p) != PF_INET){
return(NULL);
}
p += 4;
*l -= 4;
hr = (iphdraw *)p;
break;
case DLT_LOOP: // OpenBSD Loopback
if(*((uint32_t *)p) != htonl(PF_INET)){
return(NULL);
}
p += 4;
*l -= 4;
hr = (iphdraw *)p;
break;
default: // Ethernet or Linux SLL
hr = (iphdraw *)(p = read_header_l2(&(h->l2), lktype, p, l));
if(hr == NULL){
return(NULL);
}
if(get_l3type(&(h->l2)) != 0x0800){
return(NULL); // IP以外は破棄
}
}
if(*l < sizeof(iphdraw)){
opt.err_ip++;
return(NULL); // 不完全なデータ
}
h->ver = (hr->vih & 0xf0) >> 4;
h->ihl = (hr->vih & 0x0f) << 2;
h->tos = hr->tos;
h->len = ntohs(hr->len);
h->id = ntohs(hr->id);
h->flags = ntohs(hr->ffo) >> 13;
h->offset = ntohs(hr->ffo) & 0x1fff;
h->ttl = hr->ttl;
h->protocol = hr->protocol;
h->checksum = ntohs(hr->checksum);
h->src.s_addr = hr->src;
h->dst.s_addr = hr->dst;
p += sizeof(iphdraw);
*l -= sizeof(iphdraw);
if(optlen = h->ihl - sizeof(iphdraw)){
if(*l < optlen){
return(NULL);
}
memcpy(h->option, p, optlen);
p += optlen;
*l -= optlen;
}
return(p);
}
u_char *read_header_tcp(tcphdr *h, uint8_t *p, uint32_t *l)
{
tcphdraw *hr;
p= read_header_ip(&(h->ip), p, l);
hr = (tcphdraw *)p;
if(hr == NULL){
return(NULL);
}
if(h->ip.offset != 0){
return(NULL); // フラグメントの先頭以外は破棄
}
if(h->ip.protocol != 6){
return(NULL); // TCP以外は破棄
}
if(*l < 20){
opt.err_tcp++;
return(NULL);
}
h->sport = ntohs(hr->sport);
h->dport = ntohs(hr->dport);
h->seqno = ntohl(hr->seqno);
h->ackno = ntohl(hr->ackno);
h->offset = hr->offset >> 2;
h->flags = hr->flags;
h->window = ntohs(hr->window);
h->checksum = ntohs(hr->checksum);
h->urgent = ntohs(hr->urgent);
if(*l < h->offset){
opt.err_tcp++;
return(NULL);
}
memcpy(h->opt, hr->opt, h->offset - 20);
p += h->offset;
*l -= h->offset;
return(p);
}
tcpsession *get_active_tcpsession(tcpsession *session)
{
tcpsession *c;
session->segment.sno = 0;
session->segment.rno = 1;
for(c=opt.tsact;c;c=c->next){
if((memcmp(&(c->ip[0]), &(session->ip[0]), sizeof(struct sockaddr_in)) == 0) &&
(memcmp(&(c->ip[1]), &(session->ip[1]), sizeof(struct sockaddr_in)) == 0)){
break;
}
if((memcmp(&(c->ip[0]), &(session->ip[1]), sizeof(struct sockaddr_in)) == 0) &&
(memcmp(&(c->ip[1]), &(session->ip[0]), sizeof(struct sockaddr_in)) == 0)){
session->segment.sno = 1;
session->segment.rno = 0;
break;
}
}
return(c);
}
/*************************************************
*
* tcpsegmentの生成/開放
*
*************************************************/
tcpsegment *malloc_tcpsegment_pool()
{
if(opt.sg_limit && (opt.count_sg_act >= opt.sg_limit)){
opt.count_sg_drop++;
return(NULL);
}
return(malloc(sizeof(tcpsegment)));
int i;
tcpsegment *tp;
if(opt.tsegpool.free == NULL){
opt.tsegpool.count = 32768;
opt.tsegpool.pool = realloc(opt.tsegpool.pool, sizeof(tcpsegment *) * (opt.tsegpool.block + 1));
opt.tsegpool.free = malloc(sizeof(tcpsegment) * opt.tsegpool.count);
opt.tsegpool.pool[opt.tsegpool.block] = opt.tsegpool.free;
for(i=1;i<opt.tsegpool.count;i++){
opt.tsegpool.pool[opt.tsegpool.block][i-1].next = &(opt.tsegpool.pool[opt.tsegpool.block][i]);
opt.tsegpool.pool[opt.tsegpool.block][i].prev = &(opt.tsegpool.pool[opt.tsegpool.block][i-1]);
}
opt.tsegpool.block++;
}
tp = opt.tsegpool.free;
if(opt.tsegpool.free = tp->next){
tp->next->prev = NULL;
tp->next = NULL;
}
return(tp);
}
void free_tcpsegment_pool(tcpsegment *packet)
{
if(packet == NULL){
return;
}
lnklist_destroy_with_destructor(packet->dpimsg, free);
free(packet);
return;
packet->prev = NULL;
packet->next = opt.tsegpool.free;
opt.tsegpool.free = packet;
}
tcpsegment *malloc_tcpsegment(tcpsegment *s)
{
tcpsegment *n = malloc_tcpsegment_pool();
if(n == NULL){
opt.count_sg_drop++;
return(NULL);
}
if(s){
memcpy(n, s, sizeof(tcpsegment));
}else{
memset(n, 0, sizeof(tcpsegment));
}
opt.count_sg_act++;
if(opt.count_sg_act > opt.count_sg_max){
opt.count_sg_max = opt.count_sg_act;
}
return(n);
}
void free_tcpsegment(tcpsegment *packet)
{
if(packet == NULL){
return;
}
if(packet->prev){
packet->prev->next = packet->next;
}
if(packet->next){
packet->next->prev = packet->prev;
}
free_tcpsegment_pool(packet);
opt.count_sg_act--;
}
tcpsegment *add_tcpsegment(tcpsession *c, tcpsegment *t)
{
tcpsegment *r;
tcpsegment *p;
if(c == NULL){
return(NULL);
}
t = malloc_tcpsegment(t);
if(t == NULL){
return(NULL);
}
p = (c->last) ? c->last : &(c->segment);
c->pkcnt++;
c->pkall++;
c->szall += t->segsz;
p->next = t;
c->last = t;
t->prev = p;
t->segno = p->segno + 1;
t->st[0] = c->st[t->sno];
t->st[1] = c->st[t->rno];
if(opt.fragment && (t->fragment & 1)){
c->view = 1;
t->view = 0;
t->color = COLOR_RED;
opt.count_ip_fragment++;
}
if((opt.st_limit > 0) && ((get_keika_time(&(p->ts), &(t->ts)) / 1000) > opt.st_limit)){
c->view = 1;
p->view = 0;
t->view = 0;
t->color = COLOR_RED;
opt.count_sg_delay++;
}
if(opt.live == 1){
t->view = 0;
}else{
t->view = (t->color == 0);
}
if(c->pkcnt > 2048){
p = c->segment.next;
while(p){
if(p->view == 0){
p = p->next;
continue;
}
if(p->prev){
p->prev->next = p->next;
}
if(p->next){
p->next->prev = p->prev;
}
if(c->last == p){
c->last = p->prev;
}
r = p->next;
p->prev = NULL;
p->next = NULL;
free_tcpsegment(p);
p = r;
c->pkcnt--;
if(c->pkcnt < 1024){
break;
}
}
}
return(t);
}
/*************************************************
*
* tcpsessionの生成/開放
*
*************************************************/
tcpsession *malloc_tcpsession(tcpsession *c)
{
tcpsession *s;
static uint16_t sid = 0;
if(s = opt.tsespool.free){
if(opt.tsespool.free = s->next){
s->next->prev = NULL;
s->next = NULL;
}
opt.tsespool.count--;
}else{
if(s = malloc(sizeof(tcpsession))){
opt.count_ts++;
}else{
return(NULL);
}
}
if(c){
memcpy(s, c, sizeof(tcpsession));
}else{
memset(s, 0, sizeof(tcpsession));
}
s->sid = sid = (sid > 9999) ? 1 : sid + 1;
s->pkcnt = 1;
s->pkall = 1;
return(s);
}
void free_tcpsession(tcpsession *c)
{
if(c == NULL){
return;
}
while(c->segment.next){
tcpsegment *p = c->segment.next->next;
free_tcpsegment(c->segment.next);
c->segment.next = p;
}
c->last = NULL;
c->pkcnt = 1;
if(c->prev){
c->prev->next = c->next;
}
if(c->next){
c->next->prev = c->prev;
}
if(opt.tsespool.count >= 65535){
free(c);
opt.count_ts--;
}else{
if(c->next = opt.tsespool.free){
c->next->prev = c;
}
opt.tsespool.free = c;
opt.tsespool.count++;
}
}
tcpsession *del_tcpsession(tcpsession *c)
{
if(c == NULL){
return(NULL);
}
tcpsession *p = c->prev;
tcpsession *n = c->next;
if(c == opt.tsact){
if(opt.tsact = n){
opt.tsact->prev = NULL;
}
}else{
if(p){
p->next = n;
}
if(n){
n->prev = p;
}
}
opt.count_ts_act--;
free_tcpsession(c);
return(n);
}
tcpsession *add_tcpsession(tcpsession *c)
{
if(c == NULL){
return(NULL);
}
if(c->segment.flags != 2){
return(NULL);
}
if(opt.ts_limit && (opt.count_ts_act >= opt.ts_limit)){
opt.count_ts_drop++;
return(NULL);
}
c = malloc_tcpsession(c);
if(c == NULL){
return(NULL);
}
if(c->next = opt.tsact){
c->next->prev = c;
}
opt.tsact = c;
opt.count_ts_act++;
opt.count_ts_total++;
if(opt.count_ts_max < opt.count_ts_act){
opt.count_ts_max = opt.count_ts_act;
}
return(c);
}
/********************************************************************
*
* 描画関係
*
********************************************************************/
void print_acttcpsession(FILE *fp)
{
struct tm *t;
uint64_t td;
char st[256];
char ts[2][64];
char ip[2][64];
tcpsession *c;
for(c=opt.tsact;c;c=c->next){
t = localtime(&(c->segment.ts.tv_sec));
sprintf(st, "%s/%s", tcp_state_str(c->st[0]), tcp_state_str(c->st[1]));
sprintf(ts[0], "%02d:%02d:%02d.%03u", t->tm_hour, t->tm_min, t->tm_sec, c->segment.ts.tv_usec / 1000);
sprintf(ip[0], "%s:%u", inet_ntoa(c->ip[0].sin_addr), c->ip[0].sin_port);
sprintf(ip[1], "%s:%u", inet_ntoa(c->ip[1].sin_addr), c->ip[1].sin_port);
td = 0;
if(c->last){
td = c->last->ts.tv_sec - c->segment.ts.tv_sec;
td *= 1000;
td += c->last->ts.tv_usec / 1000;
td -= c->segment.ts.tv_usec / 1000;
}
sprintf(ts[1], "%u.%03us", (int)(td/1000), (int)(td%1000));
fprintf(fp, "%04d %s(+%s) %s %s %-23s\n", c->pkcnt, ts[0], ts[1], ip[0], ip[1], st);
}
}
void print_dpimsg(FILE *fp, tcpsegment *sg)
{
char cl[2][16];
cl[0][0] = 0;
cl[1][0] = 0;
if(opt.color){
sprintf(cl[0], "\x1b[3%dm", COLOR_GREEN);
sprintf(cl[1], "\x1b[39m");
}
if(sg && sg->dpimsg){
switch(opt.mode){
case MIRUO_MODE_TCP:
break;
case MIRUO_MODE_HTTP:
lnklist_iter_init(sg->dpimsg);
while(lnklist_iter_hasnext(sg->dpimsg)){
fprintf(fp, "%s%s%s\n", cl[0], lnklist_iter_next(sg->dpimsg), cl[1]);
}
break;
}
}
}
void print_tcpsession(FILE *fp, tcpsession *c)
{
struct tm *t; //
char st[64]; // TCPステータス
char ts[32]; // タイムスタンプ
char fs[32]; // IPフラグメント文字列
char cl[2][16]; // 色指定用ESCシーケンス
char ip[2][64]; // IPアドレス
char *allow[] = {">", "<"}; //
tcpsegment *sg;
if(c == NULL){
return;
}
if(c->view == 0){
return;
}
fs[0] = 0;
cl[0][0] = 0;
cl[1][0] = 0;
if(opt.color){
sprintf(cl[0], "\x1b[3%dm", COLOR_YELLOW);
sprintf(cl[1], "\x1b[39m");
}
if(opt.live == 0){
uint32_t ct = tcp_connection_time(c);
sprintf(ip[0], "%s:%u", inet_ntoa(c->ip[0].sin_addr), c->ip[0].sin_port);
sprintf(ip[1], "%s:%u", inet_ntoa(c->ip[1].sin_addr), c->ip[1].sin_port);
if(opt.quiet){
fprintf(fp, "%s%8u.%03u|%21s == %-21s|%useg(%u)%s\n",
cl[0],