aboutsummaryrefslogtreecommitdiffstats
path: root/wiretap/ct-compile.c
blob: eea612b9e8627d614f0d21e3332ce62578c35a70 (plain)
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
/* ct-compile.c
   ------------
   Compile-time filter-compiler for Wiretap

*/

#include <stdio.h>
#include <stdlib.h>

#include "config.h"
#include "ct-compile.h"

#ifdef HAVE_GLIB10
#include "glib-new.h"
#endif

#define LINE_SIZE 1024

GHashTable *field_hash;
GHashTable *protocol_hash;
char *protocol_name = NULL;
int rt_iteration;

extern struct field_info working_field; /* in ct-grammar.y */

char *ftype_text[] = {
	"NONE",
	"BOOLEAN",
	"ETHER",
	"IPv4ADDR",
	"UINT8",
	"UINT16",
	"UINT32",
	"BYTE"
};

char *ctype_text[] = {
	"NONE",
	"ANDMASK",
	"BYTECMP",
	"EITHEROF"
};

static
int many_list_subtype(struct field_info *val);

/* Called by main() to initialize the global variables that ct-compile.c
 * worries about. */
void compiler_init(void)
{
	field_hash = g_hash_table_new(g_str_hash, g_str_equal);
	protocol_hash = g_hash_table_new(g_str_hash, g_str_equal);
	field_info_init(&working_field);
}

/* takes a pointer to a field_info struct that the parser built,
 * makes a copy of the struct, and adds it to our list of fields */
void field_info_add(struct field_info *fi, char *protocol)
{
	struct field_info *new_fi;

	/* Make a duplicate of the field_info struct, destroying
	 * the pointers of the old struct in the process. */
	new_fi = g_memdup(fi, sizeof(struct field_info));
	new_fi->name = fi->name;
	new_fi->short_name = fi->short_name;
	new_fi->description = fi->description;
	new_fi->many_list = fi->many_list;
	new_fi->aliases = fi->aliases;
	fi->name = NULL;
	fi->short_name = NULL;
	fi->description = NULL;
	fi->many_list = NULL;
	fi->aliases = NULL;

	/* Find the parent */
	new_fi->parent = g_hash_table_lookup(protocol_hash, protocol);
	if (!new_fi)
		g_print("Cannot find parent protocol %s for field %s\n",
				protocol, new_fi->name);

	g_hash_table_insert(field_hash, new_fi->name, new_fi);

	g_print("field_info_add added %s (%s) \n\t"
			"ftype=%s, off=%d, len=%d, val=%d, ctype=%s\n",
			new_fi->name,
			new_fi->description,
			ftype_text[new_fi->field_type], new_fi->offset,
			new_fi->length, new_fi->value,
			ctype_text[new_fi->computation_type]);
}

/* initialize a field_info struct */
void field_info_init(struct field_info *fi)
{
	/* put NULLs in the fields that field_info_zero assumes
	 * that a non-NULL value corresponds to allocated memory. */
	fi->name = NULL;
	fi->description = NULL;
	fi->aliases = NULL;
	fi->many_list = NULL;

	field_info_zero(fi);
}

/* zero out the values of an existing field_info struct */
void field_info_zero(struct field_info *fi)
{
	if (fi->name)
		free(fi->name);
	if (fi->short_name)
		free(fi->short_name);
	if (fi->description)
		free(fi->description);

	fi->field_type = 0;
	fi->computation_type = CTYPE_NONE;
	fi->offset = 0;
	fi->length = 0;
	fi->value = 0;
	fi->parent = NULL;

	if (fi->aliases)
		g_slist_free(fi->aliases);
	if (fi->many_list)
		g_slist_free(fi->many_list);

	fi->aliases = g_slist_alloc();
	fi->many_list = g_slist_alloc();	
}

void show_aliases(gpointer alias, gpointer field)
{
	if (alias) 
		g_print("%s ", ((GString*)alias)->str);

}
/* add alias(es) to this field */
void field_info_add_alias(char *field_name, GSList *aliases)
{
	struct field_info *fi;

	fi = g_hash_table_lookup(field_hash, field_name);

	if (!fi) {
		g_print("Could not find field %s to alias.\n", field_name);
		return;
	}

	g_slist_concat(fi->aliases, aliases);
	g_print("(%s) added aliases: ", fi->name);
	g_slist_foreach(fi->aliases, show_aliases, NULL);
	g_print("\n");
}

/* Given a list of GStrings of field_names, return a list of field_info
 * pointers */
GSList* field_info_list(GSList *field_names, char *protocol)
{	
	GSList *new_list;
	char	*protocol_dot;

	protocol_dot = g_strjoin("", protocol, ".", NULL);
	g_slist_foreach(field_names, field_info_list_func1, protocol_dot);

	new_list = g_slist_alloc();
	g_slist_foreach(field_names, field_info_list_func2, new_list);

	return new_list;
}

void field_info_list_func1(gpointer node, gpointer protocol)
{
	if(node)
		g_string_prepend((GString*)node, (char*)protocol);
}
	

void field_info_list_func2(gpointer node, gpointer new_list)
{
	if (node)
		g_slist_append(new_list, 
				g_hash_table_lookup(field_hash,
						((GString*)node)->str));
/*	if (node)
		g_print("info_list added %s\n", ((GString*)node)->str);*/
}


/* add a protocol to the hash */
void protocol_layer_add(char *name, char *description)
{
	struct protocol_layer *new_pr;

	new_pr = g_malloc(sizeof(struct protocol_layer));

	new_pr->name = g_strdup(name);
	new_pr->description = g_strdup(description);

	g_hash_table_insert(protocol_hash, new_pr->name, new_pr);

	g_print("protocol_layer_add added %s (%s)\n",
			new_pr->name,
			new_pr->description);
}

/* Creates rt-scanner.l from rt-scanner-skel.l */
void write_rt_lex(void)
{
	char	buf[LINE_SIZE];
	FILE	*in, *out;

	if (!(in = fopen("rt-scanner-skel.l", "r"))) {
		g_error("Could not open rt-scanner-skel.l for reading.");
		exit(1);
	}

	if (!(out = fopen("rt-scanner.l", "w"))) {
		g_error("Could not open rt-scanner.l for writing.");
		exit(1);
	}

	while(fgets(buf, LINE_SIZE, in)) {
		if (strcmp(buf, "/* ct-compile: lex tokens */\n") == 0) {
			write_rt_lex_tokens(out);
			continue;
		}
		else {
			fprintf(out, "%s", buf);
		}
	}
	fclose(in);
	fclose(out);
}


void write_rt_lex_tokens(FILE *out)
{
	g_hash_table_foreach(field_hash, rt_lex_tokens, out);
}

void rt_lex_tokens(gpointer key, gpointer value, gpointer out)
{
	char *upcase;

	if (!value) {
		g_print("key %s has no value.\n", (char*)key);
		return;
	}

	protocol_name = ((struct field_info*) value)->parent->name;
	if (((struct field_info*) value)->aliases) {
		g_slist_foreach(((struct field_info*) value)->aliases,
				rt_lex_tokens_aliases, out);
	}
	upcase = rt_lex_token_upcase(((struct field_info*)value)->name);
	fprintf((FILE*)out, "%s\\.%s\t return %s;\n", protocol_name,
			((struct field_info*) value)->short_name,
			upcase);

	free(upcase);
}

char* rt_lex_token_upcase(char *text)
{
	char *new_text;
	char *p;
	new_text = g_strdup(text);
	g_strup(new_text);

	/* s/\./_/g */
	for (p = new_text; *p; p++) {
		if (*p == '.') {
			*p = '_';
		}
	}
	return new_text;
}


void rt_lex_tokens_aliases(gpointer node, gpointer out)
{
	if (node) {
		fprintf((FILE*)out, "%s\\.%s\t|\n",
				protocol_name,
				((GString*) node)->str);
	}
}

/* Creates rt-grammar.y from rt-grammar-skel.y */
void write_rt_yacc(void)
{
	char	buf[LINE_SIZE];
	FILE	*in, *out;

	if (!(in = fopen("rt-grammar-skel.y", "r"))) {
		g_error("Could not open rt-grammar-skel.y for reading.");
		exit(1);
	}

	if (!(out = fopen("rt-grammar.y", "w"))) {
		g_error("Could not open rt-scanner.l for writing.");
		exit(1);
	}

	while(fgets(buf, LINE_SIZE, in)) {
		if (strcmp(buf, "/* ct-compile: bytecmp_table */\n") == 0) {
			write_rt_bytecmp_table(out);
			continue;
		}
		else if (strcmp(buf, "/* ct-compile: eitherof_table */\n") == 0) {
			write_rt_eitherof_table(out);
			continue;
		}
		else if (strcmp(buf, "/* ct-compile: yacc tokens */\n") == 0) {
			write_rt_yacc_tokens(out);
			continue;
		}
		else if (strcmp(buf, "/* ct-compile: bytecmp_lval */\n") == 0) {
			write_rt_bytecmp_lval(out);
			continue;
		}
		else {
			fprintf(out, "%s", buf);
		}
	}
	fclose(in);
	fclose(out);
}

/* ------------------------- BYTECMP_TABLE -------------------- */
void write_rt_bytecmp_table(FILE *out)
{
	fprintf(out, "bytecmp_info bytecmp_table[] = {\n");
	g_hash_table_foreach(field_hash, rt_bytecmp_table, out);
	fprintf(out, "\t{ 0, 0, 0, 0 }\n};\n");
}

void rt_bytecmp_table(gpointer key, gpointer value, gpointer out)
{
	char *upcase;
	struct field_info *val = (struct field_info*) value;

	if (!val) {
		g_print("key %s has no value.\n", (char*)key);
		return;
	}

	/* return now if we're not dealing with a bytecmp field */
	if (val->computation_type == CTYPE_EITHEROF) {
		if (many_list_subtype(val) != CTYPE_BYTECMP)
			return;
	}
	else if (val->computation_type != CTYPE_BYTECMP) {
		return;
	}

	upcase = rt_lex_token_upcase(((struct field_info*)value)->name);
	fprintf((FILE*)out, "\t{ %s, %d, %d, %d },\n",
		upcase, val->computation_type, val->offset, val->length);
	free(upcase);
}

static
int many_list_subtype(struct field_info *val)
{
	struct field_info *fi;
	gchar *field1;

	if (!val->many_list)
		return 0;

	field1 = ((GString*)g_slist_nth_data(val->many_list, 1))->str;
	fi = g_hash_table_lookup(field_hash, field1);

	if (!fi)
		return 0;

	return fi->computation_type;;
}	

/* ------------------- EITHEROF_TABLE ------------------------ */
void write_rt_eitherof_table(FILE *out)
{
	fprintf(out, "eitherof_info eitherof_table[] = {\n");
	g_hash_table_foreach(field_hash, rt_eitherof_table, out);
	fprintf(out, "\t{ 0, 0, 0, 0 }\n};\n");
}

void rt_eitherof_table(gpointer key, gpointer value, gpointer out)
{
	char *upcase_field, *upcase_field1, *upcase_field2;
	struct field_info *val = (struct field_info*) value;

	if (!val) {
		g_print("key %s has no value.\n", (char*)key);
		return;
	}

	if (val->computation_type != CTYPE_EITHEROF) {
		return;
	}

	upcase_field = rt_lex_token_upcase(((struct field_info*)value)->name);
	g_print("EITHEROF checking %s\n", upcase_field);
	if (val->many_list) {
		g_print("getting fields\n");
		upcase_field1 = ((GString*)g_slist_nth_data(val->many_list, 1))->str;
		g_print("got field1 %s\n", upcase_field1);
		upcase_field2 = ((GString*)g_slist_nth_data(val->many_list, 2))->str;
		g_print("got field2 %s\n", upcase_field2);
		upcase_field1 = rt_lex_token_upcase(upcase_field1);
		g_print("got field1 %s\n", upcase_field1);
		upcase_field2 = rt_lex_token_upcase(upcase_field2);
		g_print("got field2 %s\n", upcase_field2);
	}
	else
		return;

	fprintf((FILE*)out, "\t{ %s, %d, %s, %s },\n",
			upcase_field, val->computation_type,
			upcase_field1, upcase_field2);
	free(upcase_field);
	free(upcase_field1);
	free(upcase_field2);
}

/* ---------------------- YACC_TOKENS ---------------------------- */
void write_rt_yacc_tokens(FILE *out)
{
	g_hash_table_foreach(field_hash, rt_yacc_tokens, out);
}

void rt_yacc_tokens(gpointer key, gpointer value, gpointer out)
{
	char *upcase;
	struct field_info *val = (struct field_info*) value;

	if (!val) {
		g_print("key %s has no value.\n", (char*)key);
		return;
	}

	upcase = rt_lex_token_upcase(((struct field_info*)value)->name);
	fprintf((FILE*)out, "%%token <d>\t%s\n", upcase);
	free(upcase);
}

/* ------------------------ BYTECMP_LVAL -------------------------- */
void write_rt_bytecmp_lval(FILE *out)
{
	rt_iteration = 0;
	g_hash_table_foreach(field_hash, rt_bytecmp_lval, out);
	fprintf(out, "\t;\n");
}

void rt_bytecmp_lval(gpointer key, gpointer value, gpointer out)
{
	char *upcase;
	struct field_info *val = (struct field_info*) value;

	if (!val) {
		g_print("key %s has no value.\n", (char*)key);
		return;
	}

	if (val->computation_type == CTYPE_EITHEROF) {
		if (many_list_subtype(val) != CTYPE_BYTECMP)
			return;
	}
	else if (val->computation_type != CTYPE_BYTECMP) {
		return;
	}

	if (rt_iteration == 0) {
		fprintf(out, "bytecmp_lval:\t");
	}
	else {
		fprintf(out,"\t|\t");
	}

	upcase = rt_lex_token_upcase(((struct field_info*)value)->name);
	fprintf((FILE*)out, "\t%s { $$ = %s; }\n",
			upcase, upcase);
	free(upcase);
	rt_iteration++;
}