File: synopt_report.c

package info (click to toggle)
libtrace3 3.0.22-0.2
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,452 kB
  • sloc: ansic: 24,574; sh: 11,372; cpp: 1,811; makefile: 460; yacc: 96; lex: 50
file content (242 lines) | stat: -rw-r--r-- 6,186 bytes parent folder | download | duplicates (4)
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
#include <netdb.h>
#include <inttypes.h>
#include <lt_inttypes.h>
#include <stdio.h>
#include "libtrace.h"
#include "tracereport.h"
#include "report.h"

struct tcp_opts {
	bool mss;
	bool sack;
	bool winscale;
	bool ts;
	bool ttcp;
	bool other;
};

struct opt_counter {
	uint64_t no_options;
	uint64_t mss_only;
	uint64_t ts_only;
	uint64_t ms;
	uint64_t mw;
	uint64_t msw;
	uint64_t mt;
	uint64_t all_four;
	uint64_t ts_and_sack;
	uint64_t wt;
	uint64_t tms;
	uint64_t tws;
	uint64_t tmw;
	uint64_t ts_and_another;
	uint64_t ttcp;
	uint64_t other;
};

struct opt_counter syn_counts = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
struct opt_counter synack_counts = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

uint64_t total_syns = 0;
uint64_t total_synacks = 0;

static void classify_packet(struct tcp_opts opts, struct opt_counter *counts) {
	if (!opts.mss && !opts.sack && !opts.winscale && !opts.ts && !opts.ttcp && !opts.other)
	{
		counts->no_options ++;
		return;
	}

	if (opts.mss && !opts.sack && !opts.winscale && !opts.ts && !opts.ttcp && !opts.other)
	{
		counts->mss_only ++;
		return;
	}

	if (!opts.mss && !opts.sack && !opts.winscale && opts.ts && !opts.ttcp && !opts.other)
	{
		counts->ts_only ++;
		return;
	}

	if (opts.mss && opts.sack && !opts.winscale && !opts.ts)
		counts->ms ++;

	if (opts.mss && opts.winscale && !opts.sack && !opts.ts)
		counts->mw ++;

	if (opts.mss && opts.winscale && opts.sack && !opts.ts)
		counts->msw ++;
	
	if (opts.mss && opts.ts && !opts.winscale && !opts.sack)
		counts->mt ++;
	if (opts.ts && opts.sack && !opts.mss && !opts.winscale)
		counts->ts_and_sack ++;

	if (opts.ts && opts.winscale && !opts.mss && !opts.sack)
		counts->wt ++;

	if (opts.ts && opts.mss && opts.winscale && !opts.sack)
		counts->tmw ++;
	if (opts.ts && opts.mss && opts.sack && !opts.winscale)
		counts->tms ++;
	if (opts.ts && opts.sack && opts.winscale && !opts.mss)
		counts->tws ++;
	
	
	if (opts.mss && opts.sack && opts.winscale && opts.ts) {
		counts->all_four ++;
	}

	if (opts.ts && (opts.mss || opts.winscale || opts.sack)) {
		counts->ts_and_another ++;
	}
	
	if (opts.ttcp)
		counts->ttcp ++;
	if (opts.other)
		counts->other ++;	
}

void synopt_per_packet(struct libtrace_packet_t *packet)
{
	struct libtrace_tcp *tcp = trace_get_tcp(packet);
	unsigned char *opt_ptr;
	libtrace_direction_t dir = trace_get_direction(packet);
	int len;
	unsigned char type, optlen, *data;
	struct tcp_opts opts_seen = {false, false, false, false, false, false};
	
	if(!tcp)
		return;

	if (!tcp->syn)
		return;
	
	if (dir != TRACE_DIR_INCOMING && dir != TRACE_DIR_OUTGOING)
		dir = TRACE_DIR_OTHER;
	
	len = tcp->doff * 4 - sizeof(libtrace_tcp_t);
	if(len == 0)
		return;
	
	opt_ptr = (unsigned char *)tcp + sizeof (libtrace_tcp_t);
	
	while(trace_get_next_option(&opt_ptr,&len,&type,&optlen,&data)){
		/* I don't think we need to count NO-OPs */
		if (type == 1)
			continue;
		switch(type) {
			case 2:
				opts_seen.mss = true;
				break;
			case 3:
				opts_seen.winscale = true;
				break;
			case 4:
				opts_seen.sack = true;
				break;
			case 5:
				opts_seen.sack = true;
				break;
			case 8:
				opts_seen.ts = true;
				break;
			case 11:
			case 12:
			case 13:
				opts_seen.ttcp = true;
				break;
			default:
				opts_seen.other = true;
		}
	}

	if (tcp->ack) {
		total_synacks ++;
		classify_packet(opts_seen, &synack_counts);
	} else {
		total_syns ++;
		classify_packet(opts_seen, &syn_counts);
	}
}


void synopt_report(void)
{
	
	FILE *out = fopen("tcpopt_syn.rpt", "w");
	if (!out) {
		perror("fopen");
		return;
	}


	
	fprintf(out, "%-20s\t%.2f%%\t%.2f%%\n",
			"No Options",
			(double)(syn_counts.no_options) / total_syns * 100.0,
			(double)(synack_counts.no_options) / total_synacks * 100.0);
	fprintf(out, "%-20s\t%.2f%%\t%.2f%%\n",
			"M Only",
			(double)(syn_counts.mss_only) / total_syns * 100.0,
			(double)(synack_counts.mss_only) / total_synacks * 100.0);
	fprintf(out, "%-20s\t%.2f%%\t%.2f%%\n",
			"T Only",
			(double)(syn_counts.ts_only) / total_syns * 100.0,
			(double)(synack_counts.ts_only) / total_synacks * 100.0);
	fprintf(out, "%-20s\t%.2f%%\t%.2f%%\n",
			"M and S",
			(double)(syn_counts.ms) / total_syns * 100.0,
			(double)(synack_counts.ms) / total_synacks * 100.0);
	fprintf(out, "%-20s\t%.2f%%\t%.2f%%\n",
			"M and W",
			(double)(syn_counts.mw) / total_syns * 100.0,
			(double)(synack_counts.mw) / total_synacks * 100.0);
	fprintf(out, "%-20s\t%.2f%%\t%.2f%%\n",
			"M, S and W",
			(double)(syn_counts.msw) / total_syns * 100.0,
			(double)(synack_counts.msw) / total_synacks * 100.0);
	fprintf(out, "%-20s\t%.2f%%\t%.2f%%\n",
			"M, T",
			(double)(syn_counts.mt) / total_syns * 100.0,
			(double)(synack_counts.mt) / total_synacks * 100.0);
	fprintf(out, "%-20s\t%.2f%%\t%.2f%%\n",
			"W, T",
			(double)(syn_counts.wt) / total_syns * 100.0,
			(double)(synack_counts.wt) / total_synacks * 100.0);
	fprintf(out, "%-20s\t%.2f%%\t%.2f%%\n",
			"S, T",
			(double)(syn_counts.ts_and_sack) / total_syns * 100.0,
			(double)(synack_counts.ts_and_sack) / total_synacks * 100.0);
	fprintf(out, "%-20s\t%.2f%%\t%.2f%%\n",
			"S, M, T",
			(double)(syn_counts.tms) / total_syns * 100.0,
			(double)(synack_counts.tms) / total_synacks * 100.0);
	fprintf(out, "%-20s\t%.2f%%\t%.2f%%\n",
			"W, M, T",
			(double)(syn_counts.tmw) / total_syns * 100.0,
			(double)(synack_counts.tmw) / total_synacks * 100.0);
	fprintf(out, "%-20s\t%.2f%%\t%.2f%%\n",
			"S, W, T",
			(double)(syn_counts.tws) / total_syns * 100.0,
			(double)(synack_counts.tws) / total_synacks * 100.0);
	fprintf(out, "%-20s\t%.2f%%\t%.2f%%\n",
			"M, S, W and T",
			(double)(syn_counts.all_four) / total_syns * 100.0,
			(double)(synack_counts.all_four) / total_synacks * 100.0);
	//fprintf(out, "%-20s\t%.2f%%\n",
	//		"T and (M or S or W)",
	//		(double)(counts.ts_and_another) / total_syns * 100.0);
	fprintf(out, "%-20s\t%.2f%%\t%.2f%%\n",
			"T/TCP",
			(double)(syn_counts.ttcp) / total_syns * 100.0,
			(double)(synack_counts.ttcp) / total_synacks * 100.0);
	fprintf(out, "%-20s\t%.2f%%\t%.2f%%\n",
			"Other options",
			(double)(syn_counts.other) / total_syns * 100.0,
			(double)(synack_counts.other) / total_synacks * 100.0);
	
	
	fclose(out);
}