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 | |
---|
9 | struct tcp_opts { |
---|
10 | bool mss; |
---|
11 | bool sack; |
---|
12 | bool winscale; |
---|
13 | bool ts; |
---|
14 | bool ttcp; |
---|
15 | bool other; |
---|
16 | }; |
---|
17 | |
---|
18 | struct opt_counter { |
---|
19 | uint64_t no_options; |
---|
20 | uint64_t mss_only; |
---|
21 | uint64_t ts_only; |
---|
22 | uint64_t ms; |
---|
23 | uint64_t mw; |
---|
24 | uint64_t msw; |
---|
25 | uint64_t mt; |
---|
26 | uint64_t all_four; |
---|
27 | uint64_t ts_and_another; |
---|
28 | uint64_t ttcp; |
---|
29 | uint64_t other; |
---|
30 | }; |
---|
31 | |
---|
32 | struct opt_counter counts = {0,0,0,0,0,0,0,0,0,0,0}; |
---|
33 | uint64_t total_syns = 0; |
---|
34 | |
---|
35 | void classify_packet(struct tcp_opts opts) { |
---|
36 | if (!opts.mss && !opts.sack && !opts.winscale && !opts.ts && !opts.ttcp && !opts.other) |
---|
37 | { |
---|
38 | counts.no_options ++; |
---|
39 | return; |
---|
40 | } |
---|
41 | |
---|
42 | if (opts.mss && !opts.sack && !opts.winscale && !opts.ts && !opts.ttcp && !opts.other) |
---|
43 | { |
---|
44 | counts.mss_only ++; |
---|
45 | return; |
---|
46 | } |
---|
47 | |
---|
48 | if (!opts.mss && !opts.sack && !opts.winscale && opts.ts && !opts.ttcp && !opts.other) |
---|
49 | { |
---|
50 | counts.ts_only ++; |
---|
51 | return; |
---|
52 | } |
---|
53 | |
---|
54 | if (opts.mss && opts.sack && !opts.winscale && !opts.ts) |
---|
55 | counts.ms ++; |
---|
56 | |
---|
57 | if (opts.mss && opts.winscale && !opts.sack && !opts.ts) |
---|
58 | counts.mw ++; |
---|
59 | |
---|
60 | if (opts.mss && opts.winscale && opts.sack && !opts.ts) |
---|
61 | counts.msw ++; |
---|
62 | |
---|
63 | if (opts.mss && opts.ts && !opts.winscale && !opts.sack) |
---|
64 | counts.mt ++; |
---|
65 | if (opts.mss && opts.sack && opts.winscale && opts.ts) { |
---|
66 | counts.all_four ++; |
---|
67 | } |
---|
68 | |
---|
69 | if (opts.ts && (opts.mss || opts.winscale || opts.sack)) { |
---|
70 | counts.ts_and_another ++; |
---|
71 | } |
---|
72 | |
---|
73 | if (opts.ttcp) |
---|
74 | counts.ttcp ++; |
---|
75 | if (opts.other) |
---|
76 | counts.other ++; |
---|
77 | } |
---|
78 | |
---|
79 | void synopt_per_packet(struct libtrace_packet_t *packet) |
---|
80 | { |
---|
81 | struct libtrace_tcp *tcp = trace_get_tcp(packet); |
---|
82 | unsigned char *opt_ptr; |
---|
83 | libtrace_direction_t dir = trace_get_direction(packet); |
---|
84 | int tcp_payload, len; |
---|
85 | unsigned char type, optlen, *data; |
---|
86 | struct tcp_opts opts_seen = {false, false, false, false, false, false}; |
---|
87 | |
---|
88 | if(!tcp) |
---|
89 | return; |
---|
90 | |
---|
91 | if (!tcp->syn) |
---|
92 | return; |
---|
93 | |
---|
94 | total_syns += 1; |
---|
95 | if (dir != TRACE_DIR_INCOMING && dir != TRACE_DIR_OUTGOING) |
---|
96 | dir = TRACE_DIR_OTHER; |
---|
97 | |
---|
98 | len = tcp->doff * 4 - sizeof(libtrace_tcp_t); |
---|
99 | if(len == 0) |
---|
100 | return; |
---|
101 | |
---|
102 | tcp_payload = trace_get_wire_length(packet) - trace_get_capture_length(packet); |
---|
103 | |
---|
104 | opt_ptr = (unsigned char *)tcp + sizeof (libtrace_tcp_t); |
---|
105 | |
---|
106 | while(trace_get_next_option(&opt_ptr,&len,&type,&optlen,&data)){ |
---|
107 | /* I don't think we need to count NO-OPs */ |
---|
108 | if (type == 1) |
---|
109 | continue; |
---|
110 | switch(type) { |
---|
111 | case 2: |
---|
112 | opts_seen.mss = true; |
---|
113 | break; |
---|
114 | case 3: |
---|
115 | opts_seen.winscale = true; |
---|
116 | break; |
---|
117 | case 4: |
---|
118 | opts_seen.sack = true; |
---|
119 | break; |
---|
120 | case 5: |
---|
121 | opts_seen.sack = true; |
---|
122 | break; |
---|
123 | case 8: |
---|
124 | opts_seen.ts = true; |
---|
125 | break; |
---|
126 | case 11: |
---|
127 | case 12: |
---|
128 | case 13: |
---|
129 | opts_seen.ttcp = true; |
---|
130 | break; |
---|
131 | default: |
---|
132 | opts_seen.other = true; |
---|
133 | } |
---|
134 | } |
---|
135 | |
---|
136 | classify_packet(opts_seen); |
---|
137 | } |
---|
138 | |
---|
139 | |
---|
140 | void synopt_report(void) |
---|
141 | { |
---|
142 | |
---|
143 | FILE *out = fopen("tcpopt_syn.out", "w"); |
---|
144 | if (!out) { |
---|
145 | perror("fopen"); |
---|
146 | return; |
---|
147 | } |
---|
148 | |
---|
149 | |
---|
150 | fprintf(out, "%-20s\t%.2f%%\n", |
---|
151 | "No Options", |
---|
152 | (double)(counts.no_options) / total_syns * 100.0); |
---|
153 | fprintf(out, "%-20s\t%.2f%%\n", |
---|
154 | "M Only", |
---|
155 | (double)(counts.mss_only) / total_syns * 100.0); |
---|
156 | fprintf(out, "%-20s\t%.2f%%\n", |
---|
157 | "T Only", |
---|
158 | (double)(counts.ts_only) / total_syns * 100.0); |
---|
159 | fprintf(out, "%-20s\t%.2f%%\n", |
---|
160 | "M and S", |
---|
161 | (double)(counts.ms) / total_syns * 100.0); |
---|
162 | fprintf(out, "%-20s\t%.2f%%\n", |
---|
163 | "M and W", |
---|
164 | (double)(counts.mw) / total_syns * 100.0); |
---|
165 | fprintf(out, "%-20s\t%.2f%%\n", |
---|
166 | "M, S and W", |
---|
167 | (double)(counts.msw) / total_syns * 100.0); |
---|
168 | fprintf(out, "%-20s\t%.2f%%\n", |
---|
169 | "M, T", |
---|
170 | (double)(counts.mt) / total_syns * 100.0); |
---|
171 | fprintf(out, "%-20s\t%.2f%%\n", |
---|
172 | "M, S, W and T", |
---|
173 | (double)(counts.all_four) / total_syns * 100.0); |
---|
174 | //fprintf(out, "%-20s\t%.2f%%\n", |
---|
175 | // "T and (M or S or W)", |
---|
176 | // (double)(counts.ts_and_another) / total_syns * 100.0); |
---|
177 | fprintf(out, "%-20s\t%.2f%%\n", |
---|
178 | "T/TCP", |
---|
179 | (double)(counts.ttcp) / total_syns * 100.0); |
---|
180 | fprintf(out, "%-20s\t%.2f%%\n", |
---|
181 | "Other options", |
---|
182 | (double)(counts.other) / total_syns * 100.0); |
---|
183 | |
---|
184 | |
---|
185 | fclose(out); |
---|
186 | } |
---|