1 | #include <netdb.h> |
---|
2 | #include <inttypes.h> |
---|
3 | #include <lt_inttypes.h> |
---|
4 | #include <stdio.h> |
---|
5 | #include "libtrace.h" |
---|
6 | #include "tracereport.h" |
---|
7 | |
---|
8 | static stat_t tcpseg_stat[3][2048] = {{{0,0}}} ; |
---|
9 | static bool suppress[3] = {true,true,true}; |
---|
10 | |
---|
11 | void tcpseg_per_packet(struct libtrace_packet_t *packet) |
---|
12 | { |
---|
13 | struct libtrace_tcp *tcp = trace_get_tcp(packet); |
---|
14 | libtrace_ip_t *ip = trace_get_ip(packet); |
---|
15 | libtrace_direction_t dir = trace_get_direction(packet); |
---|
16 | int ss; |
---|
17 | uint16_t ip_len ; |
---|
18 | |
---|
19 | if (!tcp) |
---|
20 | return; |
---|
21 | |
---|
22 | if (dir != TRACE_DIR_INCOMING && dir != TRACE_DIR_OUTGOING) |
---|
23 | dir = TRACE_DIR_OTHER; |
---|
24 | |
---|
25 | ip_len = ntohs(ip->ip_len); |
---|
26 | ss = ip_len - (ip->ip_hl * 4); |
---|
27 | |
---|
28 | tcpseg_stat[dir][ss].count++; |
---|
29 | tcpseg_stat[dir][ss].bytes+=trace_get_wire_length(packet); |
---|
30 | suppress[dir] = false; |
---|
31 | } |
---|
32 | |
---|
33 | void tcpseg_suppress() |
---|
34 | { |
---|
35 | int i; |
---|
36 | printf("%-20s","Direction:"); |
---|
37 | for(i=0;i<3;i++){ |
---|
38 | if(!suppress[i]){ |
---|
39 | switch(i){ |
---|
40 | case 0: |
---|
41 | printf("\t%24s", "Outbound "); |
---|
42 | break; |
---|
43 | case 1: |
---|
44 | printf("\t%24s", "Inbound "); |
---|
45 | break; |
---|
46 | case 2: |
---|
47 | printf("\t%24s", "Undefined "); |
---|
48 | break; |
---|
49 | default: |
---|
50 | break; |
---|
51 | } |
---|
52 | } |
---|
53 | } |
---|
54 | printf("\n"); |
---|
55 | printf("%-20s","TCP SS"); |
---|
56 | for(i=0;i<3;i++){ |
---|
57 | if(!suppress[i]){ |
---|
58 | printf("\t%12s\t%12s", "bytes","packets"); |
---|
59 | } |
---|
60 | } |
---|
61 | printf("\n"); |
---|
62 | } |
---|
63 | |
---|
64 | void tcpseg_report(void) |
---|
65 | { |
---|
66 | int i,j; |
---|
67 | FILE *out = fopen("tcpseg.rpt", "w"); |
---|
68 | if (!out) { |
---|
69 | perror("fopen"); |
---|
70 | return; |
---|
71 | } |
---|
72 | fprintf(out, "%-16s\t%10s\t%16s %16s\n", |
---|
73 | "SEGMENT SIZE", |
---|
74 | "DIRECTION", |
---|
75 | "BYTES", |
---|
76 | "PACKETS"); |
---|
77 | |
---|
78 | for(i=0;i<2048;++i) { |
---|
79 | if (tcpseg_stat[0][i].count==0 && |
---|
80 | tcpseg_stat[1][i].count==0 && tcpseg_stat[2][i].count==0) |
---|
81 | continue; |
---|
82 | fprintf(out, "%16i:",i); |
---|
83 | for(j=0;j<3;j++){ |
---|
84 | if (j != 0) { |
---|
85 | fprintf(out, "%16s", " "); |
---|
86 | } |
---|
87 | switch (j) { |
---|
88 | case 0: |
---|
89 | fprintf(out, "\t%10s", "Outbound"); |
---|
90 | break; |
---|
91 | case 1: |
---|
92 | fprintf(out, "\t%10s", "Inbound"); |
---|
93 | break; |
---|
94 | case 2: |
---|
95 | fprintf(out, "\t%10s", "Unknown"); |
---|
96 | break; |
---|
97 | } |
---|
98 | fprintf(out, "\t%16llu %16llu\n", |
---|
99 | tcpseg_stat[j][i].bytes, |
---|
100 | tcpseg_stat[j][i].count); |
---|
101 | } |
---|
102 | } |
---|
103 | fclose(out); |
---|
104 | } |
---|