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 tcpopt_stat[4][256] = {{{0,0}}}; |
---|
9 | static bool suppress[4] = {true,true,true,true}; |
---|
10 | |
---|
11 | void tcpopt_per_packet(struct libtrace_packet_t *packet) |
---|
12 | { |
---|
13 | unsigned char *p=trace_get_link(packet); |
---|
14 | struct libtrace_tcp *tcp = trace_get_tcp(packet); |
---|
15 | |
---|
16 | if(!tcp) |
---|
17 | return; |
---|
18 | |
---|
19 | int dir = trace_get_direction(packet); |
---|
20 | if(dir < 0 || dir > 1) |
---|
21 | dir = 2; |
---|
22 | |
---|
23 | int len = trace_get_capture_length(packet) - 54; |
---|
24 | if(len == 0) |
---|
25 | return; |
---|
26 | |
---|
27 | int tcp_bytes = trace_get_wire_length(packet) - trace_get_capture_length(packet); |
---|
28 | int i = tcp->doff*4 - sizeof *tcp; |
---|
29 | |
---|
30 | if(len > i) |
---|
31 | len = i; |
---|
32 | |
---|
33 | unsigned char type,optlen,*data; |
---|
34 | p += 54; //hax 14 mac header, 20 ip header, 20tcp header |
---|
35 | |
---|
36 | while(trace_get_next_option(&p,&len,&type,&optlen,&data)){ |
---|
37 | tcpopt_stat[dir][type].count++; |
---|
38 | tcpopt_stat[dir][type].bytes+= tcp_bytes; |
---|
39 | } |
---|
40 | |
---|
41 | suppress[dir] = false; |
---|
42 | } |
---|
43 | |
---|
44 | void tcpopt_suppress() |
---|
45 | { |
---|
46 | int i; |
---|
47 | printf("%-20s","Direction:"); |
---|
48 | //printf("%20s", " "); |
---|
49 | for(i=0;i<4;i++){ |
---|
50 | if(!suppress[i]){ |
---|
51 | switch(i){ |
---|
52 | case 0: |
---|
53 | printf("\t%24s", "Outbound "); |
---|
54 | break; |
---|
55 | case 1: |
---|
56 | printf("\t%24s", "Inbound "); |
---|
57 | break; |
---|
58 | case 2: |
---|
59 | printf("\t%24s", "Undefined "); |
---|
60 | break; |
---|
61 | default: |
---|
62 | break; |
---|
63 | } |
---|
64 | } |
---|
65 | } |
---|
66 | printf("\n"); |
---|
67 | printf("%-20s","TCP OPTIONS"); |
---|
68 | for(i=0;i<4;i++){ |
---|
69 | if(!suppress[i]){ |
---|
70 | printf("\t%12s\t%12s", "bytes","packets"); |
---|
71 | } |
---|
72 | } |
---|
73 | printf("\n"); |
---|
74 | } |
---|
75 | |
---|
76 | void tcpopt_report(void) |
---|
77 | { |
---|
78 | int i,j; |
---|
79 | printf("# TCP OPTION breakdown:\n"); |
---|
80 | tcpopt_suppress(); |
---|
81 | |
---|
82 | for(i=0;i<256;++i) { |
---|
83 | if (tcpopt_stat[0][i].count==0 && |
---|
84 | tcpopt_stat[1][i].count==0 && tcpopt_stat[2][i].count==0) |
---|
85 | continue; |
---|
86 | |
---|
87 | switch(i) { |
---|
88 | case 1: |
---|
89 | printf("%20s", "NOP: "); |
---|
90 | break; |
---|
91 | case 2: |
---|
92 | printf("%20s", "MSS: "); |
---|
93 | break; |
---|
94 | case 3: |
---|
95 | printf("%20s", "Winscale: "); |
---|
96 | break; |
---|
97 | case 4: |
---|
98 | printf("%20s", "SACK Permitted: "); |
---|
99 | break; |
---|
100 | case 5: |
---|
101 | printf("%20s", "SACK Information: "); |
---|
102 | break; |
---|
103 | case 8: |
---|
104 | printf("%20s", "Timestamp: "); |
---|
105 | break; |
---|
106 | case 19: |
---|
107 | printf("%20s", "MD5: "); |
---|
108 | default: |
---|
109 | printf("%20i:",i); |
---|
110 | } |
---|
111 | |
---|
112 | for(j=0;j<4;j++){ |
---|
113 | if (tcpopt_stat[j][i].count==0){ |
---|
114 | if(!suppress[j]) |
---|
115 | printf("\t%24s"," "); |
---|
116 | continue; |
---|
117 | } |
---|
118 | printf("\t%12" PRIu64 "\t%12" PRIu64, |
---|
119 | tcpopt_stat[j][i].bytes, |
---|
120 | tcpopt_stat[j][i].count); |
---|
121 | } |
---|
122 | printf("\n"); |
---|
123 | } |
---|
124 | } |
---|