2 * Routines for color filters
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 * Updated 1 Dec 10 jjm
36 #include <epan/filesystem.h>
37 #include "file_util.h"
39 #include <epan/packet.h>
41 #include "color_filters.h"
43 #include <epan/dfilter/dfilter.h>
44 #include "simple_dialog.h"
46 #include <epan/prefs.h>
48 #define RED_COMPONENT(x) (guint16) (((((x) >> 16) & 0xff) * 65535 / 255))
49 #define GREEN_COMPONENT(x) (guint16) (((((x) >> 8) & 0xff) * 65535 / 255))
50 #define BLUE_COMPONENT(x) (guint16) ( (((x) & 0xff) * 65535 / 255))
52 static gboolean read_users_filters(GSList **cfl);
54 /* the currently active filters */
55 static GSList *color_filter_list = NULL;
57 /* keep "old" deleted filters in this list until
58 * the dissection no longer needs them (e.g. file is closed) */
59 static GSList *color_filter_deleted_list = NULL;
60 static GSList *color_filter_valid_list = NULL;
62 /* Color Filters can en-/disabled. */
63 gboolean filters_enabled = TRUE;
66 /* Create a new filter */
68 color_filter_new(const gchar *name, /* The name of the filter to create */
69 const gchar *filter_string, /* The string representing the filter */
70 color_t *bg_color, /* The background color */
71 color_t *fg_color, /* The foreground color */
72 gboolean disabled) /* Is the filter disabled? */
74 color_filter_t *colorf;
76 colorf = g_malloc(sizeof (color_filter_t));
77 colorf->filter_name = g_strdup(name);
78 colorf->filter_text = g_strdup(filter_string);
79 colorf->bg_color = *bg_color;
80 colorf->fg_color = *fg_color;
81 colorf->disabled = disabled;
82 colorf->c_colorfilter = NULL;
83 colorf->edit_dialog = NULL;
84 colorf->selected = FALSE;
88 /* Add ten empty (temporary) colorfilters for easy coloring */
90 color_filters_add_tmp(GSList **cfl)
96 unsigned long int cval;
97 color_t bg_color, fg_color;
98 color_filter_t *colorf;
100 g_assert(strlen(prefs.gui_colorized_fg)==69);
101 g_assert(strlen(prefs.gui_colorized_bg)==69);
102 fg_colors = g_strsplit(prefs.gui_colorized_fg, ",", -1);
103 bg_colors = g_strsplit(prefs.gui_colorized_bg, ",", -1);
105 for ( i=1 ; i<=10 ; i++ ) {
106 name = g_strdup_printf("%s%02d",TEMP_COLOR_PREFIX,i);
108 /* retrieve background and foreground colors */
109 cval = strtoul(fg_colors[i-1], NULL, 16);
110 initialize_color(&fg_color, RED_COMPONENT(cval),
111 GREEN_COMPONENT(cval),
112 BLUE_COMPONENT(cval) );
113 cval = strtoul(bg_colors[i-1], NULL, 16);
114 initialize_color(&bg_color, RED_COMPONENT(cval),
115 GREEN_COMPONENT(cval),
116 BLUE_COMPONENT(cval) );
117 colorf = color_filter_new(name, NULL, &bg_color, &fg_color, TRUE);
118 colorf->filter_text = g_strdup("frame");
119 colorf->c_colorfilter = NULL;
120 *cfl = g_slist_append(*cfl, colorf);
125 g_strfreev(fg_colors);
126 g_strfreev(bg_colors);
132 color_filters_find_by_name_cb(gconstpointer arg1, gconstpointer arg2)
134 color_filter_t *colorf = (color_filter_t *)arg1;
135 gchar *name = (gchar *)arg2;
137 return (strstr(colorf->filter_name, name)==NULL) ? -1 : 0 ;
141 /* Set the filter off a temporary colorfilters and enable it */
143 color_filters_set_tmp(guint8 filt_nr, gchar *filter, gboolean disabled)
146 gchar *tmpfilter = NULL;
148 color_filter_t *colorf;
149 dfilter_t *compiled_filter;
152 /* Go through the tomporary filters and look for the same filter string.
153 * If found, clear it so that a filter can be "moved" up and down the list
155 for ( i=1 ; i<=10 ; i++ ) {
156 /* If we need to reset the temporary filter (filter==NULL), don't look
157 * for other rules with the same filter string
159 if( i!=filt_nr && filter==NULL )
162 name = g_strdup_printf("%s%02d",TEMP_COLOR_PREFIX,i);
163 cfl = g_slist_find_custom(color_filter_list, (gpointer *) name, color_filters_find_by_name_cb);
166 /* Only change the filter rule if this is the rule to change or if
167 * a matching filter string has been found
169 if(colorf && ( (i==filt_nr) || (strstr(filter,colorf->filter_text)!=NULL) ) ) {
170 /* set filter string to "frame" if we are resetting the rules
171 * or if we found a matching filter string which need to be cleared
173 tmpfilter = ( (filter==NULL) || (i!=filt_nr) ) ? "frame" : filter;
174 if (!dfilter_compile(tmpfilter, &compiled_filter)) {
175 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
176 "Could not compile color filter name: \"%s\""
177 " text: \"%s\".\n%s", name, filter, dfilter_error_msg);
179 if (colorf->filter_text != NULL)
180 g_free(colorf->filter_text);
181 if (colorf->c_colorfilter != NULL)
182 dfilter_free(colorf->c_colorfilter);
183 colorf->filter_text = g_strdup(tmpfilter);
184 colorf->c_colorfilter = compiled_filter;
185 colorf->disabled = ((i!=filt_nr) ? TRUE : disabled);
193 /* Reset the temporary colorfilters */
195 color_filters_reset_tmp()
199 for ( i=1 ; i<=10 ; i++ ) {
200 color_filters_set_tmp(i, NULL, TRUE);
205 /* delete the specified filter */
207 color_filter_delete(color_filter_t *colorf)
209 if (colorf->filter_name != NULL)
210 g_free(colorf->filter_name);
211 if (colorf->filter_text != NULL)
212 g_free(colorf->filter_text);
213 if (colorf->c_colorfilter != NULL)
214 dfilter_free(colorf->c_colorfilter);
218 /* delete the specified filter (called from g_slist_foreach) */
220 color_filter_delete_cb(gpointer filter_arg, gpointer unused _U_)
222 color_filter_t *colorf = filter_arg;
224 color_filter_delete(colorf);
227 /* delete the specified list */
229 color_filter_list_delete(GSList **cfl)
231 g_slist_foreach(*cfl, color_filter_delete_cb, NULL);
236 /* clone a single list entries from normal to edit list */
237 static color_filter_t *
238 color_filter_clone(color_filter_t *colorf)
240 color_filter_t *new_colorf;
242 new_colorf = g_malloc(sizeof (color_filter_t));
243 new_colorf->filter_name = g_strdup(colorf->filter_name);
244 new_colorf->filter_text = g_strdup(colorf->filter_text);
245 new_colorf->bg_color = colorf->bg_color;
246 new_colorf->fg_color = colorf->fg_color;
247 new_colorf->disabled = colorf->disabled;
248 new_colorf->c_colorfilter = NULL;
249 new_colorf->edit_dialog = NULL;
250 new_colorf->selected = FALSE;
256 color_filter_list_clone_cb(gpointer filter_arg, gpointer cfl_arg)
258 gpointer *cfl = cfl_arg;
259 color_filter_t *new_colorf;
261 new_colorf = color_filter_clone(filter_arg);
262 *cfl = g_slist_append(*cfl, new_colorf);
265 /* clone the specified list */
267 color_filter_list_clone(GSList *cfl)
269 GSList *new_list = NULL;
271 g_slist_foreach(cfl, color_filter_list_clone_cb, &new_list);
276 /* Initialize the filter structures (reading from file) for general running, including app startup */
278 color_filters_init(void)
280 /* delete all currently existing filters */
281 color_filter_list_delete(&color_filter_list);
283 /* start the list with the temporary colorizing rules */
284 color_filters_add_tmp(&color_filter_list);
286 /* try to read the users filters */
287 if (!read_users_filters(&color_filter_list))
288 /* if that failed, try to read the global filters */
289 color_filters_read_globals(&color_filter_list);
293 color_filters_cleanup(void)
295 /* delete the previously deleted filters */
296 color_filter_list_delete(&color_filter_deleted_list);
300 color_filters_clone_cb(gpointer filter_arg, gpointer user_data)
302 color_filter_t * new_colorf = color_filter_clone(filter_arg);
303 color_filter_add_cb (new_colorf, user_data);
307 color_filters_clone(gpointer user_data)
309 g_slist_foreach(color_filter_list, color_filters_clone_cb, user_data);
314 color_filter_compile_cb(gpointer filter_arg, gpointer unused _U_)
316 color_filter_t *colorf = filter_arg;
318 g_assert(colorf->c_colorfilter == NULL);
319 if (!dfilter_compile(colorf->filter_text, &colorf->c_colorfilter)) {
320 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
321 "Could not compile color filter name: \"%s\" text: \"%s\".\n%s",
322 colorf->filter_name, colorf->filter_text, dfilter_error_msg);
323 /* this filter was compilable before, so this should never happen */
324 /* except if the OK button of the parent window has been clicked */
325 /* so don't use g_assert_not_reached() but check the filters again */
330 color_filter_validate_cb(gpointer filter_arg, gpointer unused _U_)
332 color_filter_t *colorf = filter_arg;
334 g_assert(colorf->c_colorfilter == NULL);
335 if (!dfilter_compile(colorf->filter_text, &colorf->c_colorfilter)) {
336 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
337 "Removing color filter name: \"%s\" text: \"%s\".\n%s",
338 colorf->filter_name, colorf->filter_text, dfilter_error_msg);
339 /* Delete the color filter from the list of color filters. */
340 color_filter_valid_list = g_slist_remove(color_filter_valid_list, colorf);
341 color_filter_delete(colorf);
345 /* apply changes from the edit list */
347 color_filters_apply(GSList *tmp_cfl, GSList *edit_cfl)
349 /* "move" old entries to the deleted list
350 * we must keep them until the dissection no longer needs them */
351 color_filter_deleted_list = g_slist_concat(color_filter_deleted_list, color_filter_list);
352 color_filter_list = NULL;
354 /* clone all list entries from tmp/edit to normal list */
355 color_filter_valid_list = NULL;
356 color_filter_valid_list = color_filter_list_clone(tmp_cfl);
357 color_filter_valid_list = g_slist_concat(color_filter_valid_list,
358 color_filter_list_clone(edit_cfl) );
360 /* compile all filter */
361 g_slist_foreach(color_filter_valid_list, color_filter_validate_cb, NULL);
363 /* clone all list entries from tmp/edit to normal list */
364 color_filter_list = color_filter_list_clone(color_filter_valid_list);
366 /* compile all filter */
367 g_slist_foreach(color_filter_list, color_filter_compile_cb, NULL);
371 color_filters_used(void)
373 return color_filter_list != NULL && filters_enabled;
377 color_filters_enable(gboolean enable)
379 filters_enabled = enable;
383 /* prepare the epan_dissect_t for the filter */
385 prime_edt(gpointer data, gpointer user_data)
387 color_filter_t *colorf = data;
388 epan_dissect_t *edt = user_data;
390 if (colorf->c_colorfilter != NULL)
391 epan_dissect_prime_dfilter(edt, colorf->c_colorfilter);
394 /* Prime the epan_dissect_t with all the compiler
395 * color filters in 'color_filter_list'. */
397 color_filters_prime_edt(epan_dissect_t *edt)
399 g_slist_foreach(color_filter_list, prime_edt, edt);
402 /* Colorize a single packet of the packet list */
404 color_filters_colorize_packet(gint row, epan_dissect_t *edt)
407 color_filter_t *colorf;
409 /* If we have color filters, "search" for the matching one. */
410 if (color_filters_used()) {
411 curr = color_filter_list;
413 while(curr != NULL) {
415 if ( (!colorf->disabled) &&
416 (colorf->c_colorfilter != NULL) &&
417 dfilter_apply_edt(colorf->c_colorfilter, edt)) {
418 /* this is the filter to use, apply it to the packet list */
419 packet_list_set_colors(row, &(colorf->fg_color), &(colorf->bg_color));
422 curr = g_slist_next(curr);
429 /* read filters from the given file */
430 /* XXX - Would it make more sense to use GStrings here instead of reallocing
433 read_filters_file(FILE *f, gpointer user_data)
435 #define INIT_BUF_SIZE 128
437 gchar *filter_exp = NULL;
438 guint32 name_len = INIT_BUF_SIZE;
439 guint32 filter_exp_len = INIT_BUF_SIZE;
442 guint16 fg_r, fg_g, fg_b, bg_r, bg_g, bg_b;
443 gboolean disabled = FALSE;
444 gboolean skip_end_of_line = FALSE;
446 name = g_malloc(name_len + 1);
447 filter_exp = g_malloc(filter_exp_len + 1);
451 if (skip_end_of_line) {
454 } while (c != EOF && c != '\n');
458 skip_end_of_line = FALSE;
461 while ((c = getc(f)) != EOF && isspace(c)) {
475 /* skip # comments and invalid lines */
477 skip_end_of_line = TRUE;
481 /* we get the @ delimiter.
483 * @name@filter expression@[background r,g,b][foreground r,g,b]
490 if (c == EOF || c == '@')
493 /* buffer isn't long enough; double its length.*/
495 name = g_realloc(name, name_len + 1);
504 skip_end_of_line = TRUE;
508 /* retrieve filter expression */
512 if (c == EOF || c == '@')
514 if (i >= filter_exp_len) {
515 /* buffer isn't long enough; double its length.*/
517 filter_exp = g_realloc(filter_exp, filter_exp_len + 1);
521 filter_exp[i] = '\0';
526 skip_end_of_line = TRUE;
530 /* retrieve background and foreground colors */
531 if (fscanf(f,"[%hu,%hu,%hu][%hu,%hu,%hu]",
532 &bg_r, &bg_g, &bg_b, &fg_r, &fg_g, &fg_b) == 6) {
534 /* we got a complete color filter */
536 color_t bg_color, fg_color;
537 color_filter_t *colorf;
538 dfilter_t *temp_dfilter;
540 if (!dfilter_compile(filter_exp, &temp_dfilter)) {
541 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
542 "Could not compile color filter %s from saved filters.\n%s",
543 name, dfilter_error_msg);
544 skip_end_of_line = TRUE;
548 if (!initialize_color(&fg_color, fg_r, fg_g, fg_b)) {
550 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
551 "Could not allocate foreground color "
552 "specified in input file for %s.", name);
553 dfilter_free(temp_dfilter);
554 skip_end_of_line = TRUE;
557 if (!initialize_color(&bg_color, bg_r, bg_g, bg_b)) {
559 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
560 "Could not allocate background color "
561 "specified in input file for %s.", name);
562 dfilter_free(temp_dfilter);
563 skip_end_of_line = TRUE;
567 colorf = color_filter_new(name, filter_exp, &bg_color,
568 &fg_color, disabled);
569 if(user_data == &color_filter_list) {
570 GSList **cfl = (GSList **)user_data;
573 colorf->c_colorfilter = temp_dfilter;
574 *cfl = g_slist_append(*cfl, colorf);
577 /* just editing, don't need the compiled filter */
578 dfilter_free(temp_dfilter);
579 color_filter_add_cb (colorf, user_data);
583 skip_end_of_line = TRUE;
591 /* read filters from the user's filter file */
593 read_users_filters(GSList **cfl)
599 /* decide what file to open (from dfilter code) */
600 path = get_persconffile_path("colorfilters", FALSE);
601 if ((f = eth_fopen(path, "r")) == NULL) {
602 if (errno != ENOENT) {
603 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
604 "Could not open filter file\n\"%s\": %s.", path,
613 ret = read_filters_file(f, cfl);
618 /* read filters from the filter file */
620 color_filters_read_globals(gpointer user_data)
626 /* decide what file to open (from dfilter code) */
627 path = get_datafile_path("colorfilters");
628 if ((f = eth_fopen(path, "r")) == NULL) {
629 if (errno != ENOENT) {
630 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
631 "Could not open global filter file\n\"%s\": %s.", path,
640 ret = read_filters_file(f, user_data);
645 /* read filters from some other filter file (import) */
647 color_filters_import(gchar *path, gpointer user_data)
652 if ((f = eth_fopen(path, "r")) == NULL) {
653 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
654 "Could not open\n%s\nfor reading: %s.",
655 path, strerror(errno));
659 ret = read_filters_file(f, user_data);
664 struct write_filter_data
667 gboolean only_selected;
670 /* save a single filter */
672 write_filter(gpointer filter_arg, gpointer data_arg)
674 struct write_filter_data *data = data_arg;
675 color_filter_t *colorf = filter_arg;
678 if ( (colorf->selected || !data->only_selected) &&
679 (strstr(colorf->filter_name,TEMP_COLOR_PREFIX)==NULL) ) {
680 fprintf(f,"%s@%s@%s@[%d,%d,%d][%d,%d,%d]\n",
681 colorf->disabled ? "!" : "",
684 colorf->bg_color.red,
685 colorf->bg_color.green,
686 colorf->bg_color.blue,
687 colorf->fg_color.red,
688 colorf->fg_color.green,
689 colorf->fg_color.blue);
693 /* save filters in a filter file */
695 write_filters_file(GSList *cfl, FILE *f, gboolean only_selected)
697 struct write_filter_data data;
700 data.only_selected = only_selected;
702 fprintf(f,"# DO NOT EDIT THIS FILE! It was created by Wireshark\n");
703 g_slist_foreach(cfl, write_filter, &data);
707 /* save filters in users filter file */
709 color_filters_write(GSList *cfl)
715 /* Create the directory that holds personal configuration files,
717 if (create_persconffile_dir(&pf_dir_path) == -1) {
718 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
719 "Can't create directory\n\"%s\"\nfor color files: %s.",
720 pf_dir_path, strerror(errno));
725 path = get_persconffile_path("colorfilters", TRUE);
726 if ((f = eth_fopen(path, "w+")) == NULL) {
727 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
728 "Could not open\n%s\nfor writing: %s.",
729 path, strerror(errno));
732 write_filters_file(cfl, f, FALSE);
737 /* save filters in some other filter file (export) */
739 color_filters_export(gchar *path, GSList *cfl, gboolean only_marked)
743 if ((f = eth_fopen(path, "w+")) == NULL) {
744 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
745 "Could not open\n%s\nfor writing: %s.",
746 path, strerror(errno));
749 write_filters_file(cfl, f, only_marked);