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[3][256] = {{{0,0}}}; |
---|
9 | |
---|
10 | void tcpopt_per_packet(struct libtrace_packet_t *packet) |
---|
11 | { |
---|
12 | struct libtrace_tcp *tcp = trace_get_tcp(packet); |
---|
13 | unsigned char *opt_ptr; |
---|
14 | libtrace_direction_t dir = trace_get_direction(packet); |
---|
15 | int tcp_payload, len; |
---|
16 | unsigned char type, optlen, *data; |
---|
17 | |
---|
18 | if(!tcp) |
---|
19 | return; |
---|
20 | |
---|
21 | if (dir != TRACE_DIR_INCOMING && dir != TRACE_DIR_OUTGOING) |
---|
22 | dir = TRACE_DIR_OTHER; |
---|
23 | |
---|
24 | len = tcp->doff * 4 - sizeof(libtrace_tcp_t); |
---|
25 | if(len == 0) |
---|
26 | return; |
---|
27 | |
---|
28 | tcp_payload = trace_get_wire_length(packet) - trace_get_capture_length(packet); |
---|
29 | |
---|
30 | opt_ptr = (unsigned char *)tcp + sizeof (libtrace_tcp_t); |
---|
31 | |
---|
32 | while(trace_get_next_option(&opt_ptr,&len,&type,&optlen,&data)){ |
---|
33 | /* I don't think we need to count NO-OPs */ |
---|
34 | if (type == 1) |
---|
35 | continue; |
---|
36 | tcpopt_stat[dir][type].count++; |
---|
37 | tcpopt_stat[dir][type].bytes+= tcp_payload; |
---|
38 | } |
---|
39 | |
---|
40 | } |
---|
41 | |
---|
42 | |
---|
43 | void tcpopt_report(void) |
---|
44 | { |
---|
45 | |
---|
46 | int i,j; |
---|
47 | |
---|
48 | FILE *out = fopen("tcpopt.out", "w"); |
---|
49 | if (!out) { |
---|
50 | perror("fopen"); |
---|
51 | return; |
---|
52 | } |
---|
53 | |
---|
54 | /* Put some headings up for human-readability */ |
---|
55 | fprintf(out, "%-12s\t%10s\t%16s %16s\n", |
---|
56 | "OPTION", |
---|
57 | "DIRECTION", |
---|
58 | "BYTES", |
---|
59 | "PACKETS"); |
---|
60 | |
---|
61 | for(i=0;i<256;++i) { |
---|
62 | if (tcpopt_stat[0][i].count==0 && |
---|
63 | tcpopt_stat[1][i].count==0 && tcpopt_stat[2][i].count==0) |
---|
64 | continue; |
---|
65 | |
---|
66 | switch(i) { |
---|
67 | case 1: |
---|
68 | fprintf(out, "%12s", "NOP |"); |
---|
69 | break; |
---|
70 | case 2: |
---|
71 | fprintf(out, "%12s", "MSS |"); |
---|
72 | break; |
---|
73 | case 3: |
---|
74 | fprintf(out, "%12s", "Winscale |"); |
---|
75 | break; |
---|
76 | case 4: |
---|
77 | fprintf(out, "%12s", "SACK Perm |"); |
---|
78 | break; |
---|
79 | case 5: |
---|
80 | fprintf(out, "%12s", "SACK Info |"); |
---|
81 | break; |
---|
82 | case 8: |
---|
83 | fprintf(out, "%12s", "Timestamp |"); |
---|
84 | break; |
---|
85 | case 12: |
---|
86 | fprintf(out, "%12s", "CC.New |"); |
---|
87 | break; |
---|
88 | case 19: |
---|
89 | fprintf(out, "%12s", "MD5 |"); |
---|
90 | break; |
---|
91 | default: |
---|
92 | fprintf(out, "%10i |",i); |
---|
93 | } |
---|
94 | |
---|
95 | for(j=0;j<3;j++){ |
---|
96 | if (j != 0) { |
---|
97 | fprintf(out, "%12s", " |"); |
---|
98 | } |
---|
99 | |
---|
100 | switch (j) { |
---|
101 | case 0: |
---|
102 | fprintf(out, "\t%10s", "Outbound"); |
---|
103 | break; |
---|
104 | case 1: |
---|
105 | fprintf(out, "\t%10s", "Inbound"); |
---|
106 | break; |
---|
107 | case 2: |
---|
108 | fprintf(out, "\t%10s", "Unknown"); |
---|
109 | break; |
---|
110 | } |
---|
111 | |
---|
112 | fprintf(out, "\t%16llu %16llu\n", |
---|
113 | tcpopt_stat[j][i].bytes, |
---|
114 | tcpopt_stat[j][i].count); |
---|
115 | } |
---|
116 | } |
---|
117 | fclose(out); |
---|
118 | } |
---|