aboutsummaryrefslogtreecommitdiffstats
path: root/color_filters.c
blob: a1c661a95c88545aac1aebccd4ecfac563994bf6 (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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
/* color_filters.c
 * Routines for color filters
 *
 * $Id$
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
/*
 * Updated 1 Dec 10 jjm
 */
 
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <glib.h>
#include <ctype.h>
#include <string.h>

#include <epan/filesystem.h>
#include "file_util.h"

#include <epan/packet.h>
#include "color.h"
#include "color_filters.h"
#include "file.h"
#include <epan/dfilter/dfilter.h>
#include "simple_dialog.h"
#include "ui_util.h"

static gboolean read_users_filters(GSList **cfl);

/* the currently active filters */
static GSList *color_filter_list = NULL;

/* keep "old" deleted filters in this list until 
 * the dissection no longer needs them (e.g. file is closed) */
static GSList *color_filter_deleted_list = NULL;
static GSList *color_filter_valid_list = NULL;

/* Color Filters can en-/disabled. */
gboolean filters_enabled = TRUE;


/* Create a new filter */
color_filter_t *
color_filter_new(const gchar *name,    /* The name of the filter to create */
                 const gchar *filter_string, /* The string representing the filter */
                 color_t *bg_color,    /* The background color */
                 color_t *fg_color)    /* The foreground color */
{
	color_filter_t *colorf;

	colorf = g_malloc(sizeof (color_filter_t));
	colorf->filter_name = g_strdup(name);
	colorf->filter_text = g_strdup(filter_string);
	colorf->bg_color = *bg_color;
	colorf->fg_color = *fg_color;
	colorf->c_colorfilter = NULL;
	colorf->edit_dialog = NULL;
	colorf->selected = FALSE;
        return colorf;
}

/* delete the specified filter */
void
color_filter_delete(color_filter_t *colorf)
{
	if (colorf->filter_name != NULL)
		g_free(colorf->filter_name);
	if (colorf->filter_text != NULL)
		g_free(colorf->filter_text);
	if (colorf->c_colorfilter != NULL)
		dfilter_free(colorf->c_colorfilter);
	g_free(colorf);
}

/* delete the specified filter (called from g_slist_foreach) */
static void
color_filter_delete_cb(gpointer filter_arg, gpointer unused _U_)
{
	color_filter_t *colorf = filter_arg;

	color_filter_delete(colorf);
}

/* delete the specified list */
void
color_filter_list_delete(GSList **cfl)
{
        g_slist_foreach(*cfl, color_filter_delete_cb, NULL);
        g_slist_free(*cfl);
        *cfl = NULL;
}

/* clone a single list entries from normal to edit list */
static color_filter_t *
color_filter_clone(color_filter_t *colorf)
{
	color_filter_t *new_colorf;

	new_colorf = g_malloc(sizeof (color_filter_t));
	new_colorf->filter_name = g_strdup(colorf->filter_name);
	new_colorf->filter_text = g_strdup(colorf->filter_text);
	new_colorf->bg_color = colorf->bg_color;
	new_colorf->fg_color = colorf->fg_color;
	new_colorf->c_colorfilter = NULL;
	new_colorf->edit_dialog = NULL;
	new_colorf->selected = FALSE;

        return new_colorf;
}

static void
color_filter_list_clone_cb(gpointer filter_arg, gpointer cfl_arg)
{
    gpointer *cfl = cfl_arg;
    color_filter_t *new_colorf;

    new_colorf = color_filter_clone(filter_arg);
    *cfl = g_slist_append(*cfl, new_colorf);
}

/* clone the specified list */
static GSList *
color_filter_list_clone(GSList *cfl)
{
        GSList *new_list = NULL;

        g_slist_foreach(cfl, color_filter_list_clone_cb, &new_list);

        return new_list;
}

/* Initialize the filter structures (reading from file) for general running, including app startup */
void
color_filters_init(void)
{
	/* delete all currently existing filters */
	color_filter_list_delete(&color_filter_list);

	/* try to read the users filters */
	if (!read_users_filters(&color_filter_list))
		/* if that failed, try to read the global filters */
		color_filters_read_globals(&color_filter_list);
}

void
color_filters_cleanup(void)
{
        /* delete the previously deleted filters */
        color_filter_list_delete(&color_filter_deleted_list);
}

static void
color_filters_clone_cb(gpointer filter_arg, gpointer user_data)
{
	color_filter_t * new_colorf = color_filter_clone(filter_arg);

	color_filter_add_cb (new_colorf, user_data);
}

void
color_filters_clone(gpointer user_data)
{
    g_slist_foreach(color_filter_list, color_filters_clone_cb, user_data);
}


static void
color_filter_compile_cb(gpointer filter_arg, gpointer unused _U_)
{
	color_filter_t *colorf = filter_arg;

	g_assert(colorf->c_colorfilter == NULL);
	if (!dfilter_compile(colorf->filter_text, &colorf->c_colorfilter)) {
		simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
		"Could not compile color filter name: \"%s\" text: \"%s\".\n%s",
			      colorf->filter_name, colorf->filter_text, dfilter_error_msg);
		/* this filter was compilable before, so this should never happen */
		/* except if the OK button of the parent window has been clicked */
		/* so don't use g_assert_not_reached() but check the filters again */
	}
}

static void
color_filter_validate_cb(gpointer filter_arg, gpointer unused _U_)
{
	color_filter_t *colorf = filter_arg;

	g_assert(colorf->c_colorfilter == NULL);
	if (!dfilter_compile(colorf->filter_text, &colorf->c_colorfilter)) {
		simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
		"Removing color filter name: \"%s\" text: \"%s\".\n%s",
			      colorf->filter_name, colorf->filter_text, dfilter_error_msg);
		/* Delete the color filter from the list of color filters. */
		color_filter_valid_list = g_slist_remove(color_filter_valid_list, colorf);
		color_filter_delete(colorf);
	}
}

/* apply changes from the edit list */
void
color_filters_apply(GSList *cfl)
{
        /* "move" old entries to the deleted list
         * we must keep them until the dissection no longer needs them */
        color_filter_deleted_list = g_slist_concat(color_filter_deleted_list, color_filter_list);
        color_filter_list = NULL;

        /* clone all list entries from edit to normal list */
        color_filter_valid_list = NULL;
        color_filter_valid_list = color_filter_list_clone(cfl);

        /* compile all filter */
        g_slist_foreach(color_filter_valid_list, color_filter_validate_cb, NULL);

        /* clone all list entries from edit to normal list */
        color_filter_list = color_filter_list_clone(color_filter_valid_list);

        /* compile all filter */
        g_slist_foreach(color_filter_list, color_filter_compile_cb, NULL);
}

gboolean 
color_filters_used(void)
{
        return color_filter_list != NULL && filters_enabled;
}

void
color_filters_enable(gboolean enable)
{
        filters_enabled = enable;
}


/* prepare the epan_dissect_t for the filter */
static void
prime_edt(gpointer data, gpointer user_data)
{
	color_filter_t  *colorf = data;
	epan_dissect_t   *edt = user_data;

	if (colorf->c_colorfilter != NULL)
		epan_dissect_prime_dfilter(edt, colorf->c_colorfilter);
}

/* Prime the epan_dissect_t with all the compiler
 * color filters in 'color_filter_list'. */
void
color_filters_prime_edt(epan_dissect_t *edt)
{
	g_slist_foreach(color_filter_list, prime_edt, edt);
}

/* Colorize a single packet of the packet list */
color_filter_t *
color_filters_colorize_packet(gint row, epan_dissect_t *edt)
{
    GSList *curr;
    color_filter_t *colorf;

    /* If we have color filters, "search" for the matching one. */
    if (color_filters_used()) {
        curr = color_filter_list;

        while(curr != NULL) {
            colorf = curr->data;
            if ((colorf->c_colorfilter != NULL) &&
                 dfilter_apply_edt(colorf->c_colorfilter, edt)) {
                    /* this is the filter to use, apply it to the packet list */
                    packet_list_set_colors(row, &(colorf->fg_color), &(colorf->bg_color));
                    return colorf;
            }
            curr = g_slist_next(curr);
        }
    }

    return NULL;
}

/* read filters from the given file */
/* XXX - Would it make more sense to use GStrings here instead of reallocing
   our buffers? */
static gboolean
read_filters_file(FILE *f, gpointer user_data)
{
#define INIT_BUF_SIZE 128
	gchar  *name = NULL;
	gchar  *filter_exp = NULL;
	guint32 name_len = INIT_BUF_SIZE;
	guint32 filter_exp_len = INIT_BUF_SIZE;
	guint32 i = 0;
	gint32  c;
	guint16 fg_r, fg_g, fg_b, bg_r, bg_g, bg_b;
	gboolean skip_end_of_line = FALSE;

	name = g_malloc(name_len + 1);
	filter_exp = g_malloc(filter_exp_len + 1);

	while (1) {

		if (skip_end_of_line) {
			do {
				c = getc(f);
			} while (c != EOF && c != '\n');
			if (c == EOF)
				break;
			skip_end_of_line = FALSE;
		}

		while ((c = getc(f)) != EOF && isspace(c)) {
			if (c == '\n') {
				continue;
			}
		}

		if (c == EOF)
			break;

		/* skip # comments and invalid lines */
		if (c != '@') {	
			skip_end_of_line = TRUE;
			continue;
		}

		/* we get the @ delimiter.
		 * Format is:
		 * @name@filter expression@[background r,g,b][foreground r,g,b]
		 */

		/* retrieve name */
		i = 0;
		while (1) {
			c = getc(f);
			if (c == EOF || c == '@')
				break;
			if (i >= name_len) {
				/* buffer isn't long enough; double its length.*/
				name_len *= 2;
				name = g_realloc(name, name_len + 1);
			}
			name[i++] = c;		  
		}
		name[i] = '\0';

		if (c == EOF) {
			break;
		} else if (i == 0) {
			skip_end_of_line = TRUE;
			continue;
		}

		/* retrieve filter expression */
		i = 0;
		while (1) {
			c = getc(f);
			if (c == EOF || c == '@')
				break;
			if (i >= filter_exp_len) {
				/* buffer isn't long enough; double its length.*/
				filter_exp_len *= 2;
				filter_exp = g_realloc(filter_exp, filter_exp_len + 1);
			}
			filter_exp[i++] = c;
		}
		filter_exp[i] = '\0';

		if (c == EOF) {
			break;
		} else if (i == 0) {
			skip_end_of_line = TRUE;
			continue;
		}

		/* retrieve background and foreground colors */
		if (fscanf(f,"[%hu,%hu,%hu][%hu,%hu,%hu]",
			&bg_r, &bg_g, &bg_b, &fg_r, &fg_g, &fg_b) == 6) {

			/* we got a complete color filter */

			color_t bg_color, fg_color;
			color_filter_t *colorf;
			dfilter_t *temp_dfilter;

			if (!dfilter_compile(filter_exp, &temp_dfilter)) {
				simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
				"Could not compile color filter %s from saved filters.\n%s",
					      name, dfilter_error_msg);
				skip_end_of_line = TRUE;
				continue;
			}

			if (!initialize_color(&fg_color, fg_r, fg_g, fg_b)) {
				/* oops */
				simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
				    "Could not allocate foreground color "
				    "specified in input file for %s.", name);
				dfilter_free(temp_dfilter);
				skip_end_of_line = TRUE;
				continue;
			}
			if (!initialize_color(&bg_color, bg_r, bg_g, bg_b)) {
				/* oops */
				simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
				    "Could not allocate background color "
				    "specified in input file for %s.", name);
				dfilter_free(temp_dfilter);
				skip_end_of_line = TRUE;
				continue;
			}

			colorf = color_filter_new(name, filter_exp, &bg_color,
			    &fg_color);
                        if(user_data == &color_filter_list) {
                                GSList **cfl = (GSList **)user_data;

                                /* internal call */
				colorf->c_colorfilter = temp_dfilter;
                                *cfl = g_slist_append(*cfl, colorf);
                        } else {
                                /* external call */
				/* just editing, don't need the compiled filter */
				dfilter_free(temp_dfilter);
				color_filter_add_cb (colorf, user_data);
                        }
		}    /* if sscanf */

		skip_end_of_line = TRUE;
	}

	g_free(name);
	g_free(filter_exp);
	return TRUE;
}

/* read filters from the user's filter file */
static gboolean
read_users_filters(GSList **cfl)
{
	gchar *path;
	FILE *f;
	gboolean ret;

	/* decide what file to open (from dfilter code) */
	path = get_persconffile_path("colorfilters", FALSE);
	if ((f = eth_fopen(path, "r")) == NULL) {
		if (errno != ENOENT) {
			simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
			    "Could not open filter file\n\"%s\": %s.", path,
			    strerror(errno));
		}
		g_free(path);
		return FALSE;
	}
	g_free(path);
	path = NULL;

	ret = read_filters_file(f, cfl);
	fclose(f);
	return ret;
}

/* read filters from the filter file */
gboolean
color_filters_read_globals(gpointer user_data)
{
	gchar *path;
	FILE *f;
	gboolean ret;

	/* decide what file to open (from dfilter code) */
	path = get_datafile_path("colorfilters");
	if ((f = eth_fopen(path, "r")) == NULL) {
		if (errno != ENOENT) {
			simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
			    "Could not open global filter file\n\"%s\": %s.", path,
			    strerror(errno));
		}
		g_free(path);
		return FALSE;
	}
	g_free(path);
	path = NULL;

	ret = read_filters_file(f, user_data);
	fclose(f);
	return ret;
}

/* read filters from some other filter file (import) */
gboolean
color_filters_import(gchar *path, gpointer user_data)
{
	FILE *f;
	gboolean ret;

	if ((f = eth_fopen(path, "r")) == NULL) {
		simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
		    "Could not open\n%s\nfor reading: %s.",
		    path, strerror(errno));
		return FALSE;
	}

	ret = read_filters_file(f, user_data);
	fclose(f);
	return ret;
}

struct write_filter_data
{
  FILE * f;
  gboolean only_selected;
};

/* save a single filter */
static void
write_filter(gpointer filter_arg, gpointer data_arg)
{
	struct write_filter_data *data = data_arg;
	color_filter_t *colorf = filter_arg;
	FILE *f = data->f;

	if (colorf->selected || !data->only_selected) {
		fprintf(f,"@%s@%s@[%d,%d,%d][%d,%d,%d]\n",
		    colorf->filter_name,
		    colorf->filter_text,
		    colorf->bg_color.red,
		    colorf->bg_color.green,
		    colorf->bg_color.blue,
		    colorf->fg_color.red,
		    colorf->fg_color.green,
		    colorf->fg_color.blue);
	}
}

/* save filters in a filter file */
static gboolean
write_filters_file(GSList *cfl, FILE *f, gboolean only_selected)
{
	struct write_filter_data data;

	data.f = f;
	data.only_selected = only_selected;
  
	fprintf(f,"# DO NOT EDIT THIS FILE!  It was created by Wireshark\n");
        g_slist_foreach(cfl, write_filter, &data);
	return TRUE;
}

/* save filters in users filter file */
gboolean
color_filters_write(GSList *cfl)
{
	gchar *pf_dir_path;
	const gchar *path;
	FILE *f;

	/* Create the directory that holds personal configuration files,
	   if necessary.  */
	if (create_persconffile_dir(&pf_dir_path) == -1) {
		simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
		    "Can't create directory\n\"%s\"\nfor color files: %s.",
		    pf_dir_path, strerror(errno));
		g_free(pf_dir_path);
		return FALSE;
	}

	path = get_persconffile_path("colorfilters", TRUE);
	if ((f = eth_fopen(path, "w+")) == NULL) {
		simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
		    "Could not open\n%s\nfor writing: %s.",
		    path, strerror(errno));
		return FALSE;
	}
	write_filters_file(cfl, f, FALSE);
	fclose(f);
	return TRUE;
}

/* save filters in some other filter file (export) */
gboolean
color_filters_export(gchar *path, GSList *cfl, gboolean only_marked)
{
	FILE *f;

	if ((f = eth_fopen(path, "w+")) == NULL) {
		simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
		    "Could not open\n%s\nfor writing: %s.",
		    path, strerror(errno));
		return FALSE;
	}
	write_filters_file(cfl, f, only_marked);
	fclose(f);
	return TRUE;
}