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
38 #include <epan/filesystem.h>
39 #include <wsutil/file_util.h>
41 #include <epan/packet.h>
43 #include "color_filters.h"
45 #include <epan/dfilter/dfilter.h>
46 #include "simple_dialog.h"
48 #include <epan/prefs.h>
50 #define RED_COMPONENT(x) (guint16) (((((x) >> 16) & 0xff) * 65535 / 255))
51 #define GREEN_COMPONENT(x) (guint16) (((((x) >> 8) & 0xff) * 65535 / 255))
52 #define BLUE_COMPONENT(x) (guint16) ( (((x) & 0xff) * 65535 / 255))
54 static gboolean read_users_filters(GSList **cfl);
56 /* the currently active filters */
57 static GSList *color_filter_list = NULL;
59 /* keep "old" deleted filters in this list until
60 * the dissection no longer needs them (e.g. file is closed) */
61 static GSList *color_filter_deleted_list = NULL;
62 static GSList *color_filter_valid_list = NULL;
64 /* Color Filters can en-/disabled. */
65 static gboolean filters_enabled = TRUE;
67 /* Remember if there are temporary coloring filters set to
68 * add sensitivity to the "Reset Coloring 1-10" menu item
70 static gboolean tmp_colors_set = FALSE;
72 /* Create a new filter */
74 color_filter_new(const gchar *name, /* The name of the filter to create */
75 const gchar *filter_string, /* The string representing the filter */
76 color_t *bg_color, /* The background color */
77 color_t *fg_color, /* The foreground color */
78 gboolean disabled) /* Is the filter disabled? */
80 color_filter_t *colorf;
82 colorf = (color_filter_t *)g_malloc(sizeof (color_filter_t));
83 colorf->filter_name = g_strdup(name);
84 colorf->filter_text = g_strdup(filter_string);
85 colorf->bg_color = *bg_color;
86 colorf->fg_color = *fg_color;
87 colorf->disabled = disabled;
88 colorf->c_colorfilter = NULL;
89 colorf->edit_dialog = NULL;
90 colorf->selected = FALSE;
94 /* Add ten empty (temporary) colorfilters for easy coloring */
96 color_filters_add_tmp(GSList **cfl)
102 unsigned long int cval;
103 color_t bg_color, fg_color;
104 color_filter_t *colorf;
106 g_assert(strlen(prefs.gui_colorized_fg)==69);
107 g_assert(strlen(prefs.gui_colorized_bg)==69);
108 fg_colors = g_strsplit(prefs.gui_colorized_fg, ",", -1);
109 bg_colors = g_strsplit(prefs.gui_colorized_bg, ",", -1);
111 for ( i=1 ; i<=10 ; i++ ) {
112 name = g_strdup_printf("%s%02d",TEMP_COLOR_PREFIX,i);
114 /* retrieve background and foreground colors */
115 cval = strtoul(fg_colors[i-1], NULL, 16);
116 initialize_color(&fg_color, RED_COMPONENT(cval),
117 GREEN_COMPONENT(cval),
118 BLUE_COMPONENT(cval) );
119 cval = strtoul(bg_colors[i-1], NULL, 16);
120 initialize_color(&bg_color, RED_COMPONENT(cval),
121 GREEN_COMPONENT(cval),
122 BLUE_COMPONENT(cval) );
123 colorf = color_filter_new(name, NULL, &bg_color, &fg_color, TRUE);
124 colorf->filter_text = g_strdup("frame");
125 colorf->c_colorfilter = NULL;
126 *cfl = g_slist_append(*cfl, colorf);
131 g_strfreev(fg_colors);
132 g_strfreev(bg_colors);
138 color_filters_find_by_name_cb(gconstpointer arg1, gconstpointer arg2)
140 const color_filter_t *colorf = (const color_filter_t *)arg1;
141 const gchar *name = (const gchar *)arg2;
143 return (strstr(colorf->filter_name, name)==NULL) ? -1 : 0 ;
147 /* Set the filter off a temporary colorfilters and enable it */
149 color_filters_set_tmp(guint8 filt_nr, gchar *filter, gboolean disabled)
152 const gchar *tmpfilter = NULL;
154 color_filter_t *colorf;
155 dfilter_t *compiled_filter;
158 /* Go through the tomporary filters and look for the same filter string.
159 * If found, clear it so that a filter can be "moved" up and down the list
161 for ( i=1 ; i<=10 ; i++ ) {
162 /* If we need to reset the temporary filter (filter==NULL), don't look
163 * for other rules with the same filter string
165 if( i!=filt_nr && filter==NULL )
168 name = g_strdup_printf("%s%02d",TEMP_COLOR_PREFIX,i);
169 cfl = g_slist_find_custom(color_filter_list, name, color_filters_find_by_name_cb);
170 colorf = (color_filter_t *)cfl->data;
172 /* Only change the filter rule if this is the rule to change or if
173 * a matching filter string has been found
175 if(colorf && ( (i==filt_nr) || (strstr(filter,colorf->filter_text)!=NULL) ) ) {
176 /* set filter string to "frame" if we are resetting the rules
177 * or if we found a matching filter string which need to be cleared
179 tmpfilter = ( (filter==NULL) || (i!=filt_nr) ) ? "frame" : filter;
180 if (!dfilter_compile(tmpfilter, &compiled_filter)) {
181 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
182 "Could not compile color filter name: \"%s\""
183 " text: \"%s\".\n%s", name, filter, dfilter_error_msg);
185 if (colorf->filter_text != NULL)
186 g_free(colorf->filter_text);
187 if (colorf->c_colorfilter != NULL)
188 dfilter_free(colorf->c_colorfilter);
189 colorf->filter_text = g_strdup(tmpfilter);
190 colorf->c_colorfilter = compiled_filter;
191 colorf->disabled = ((i!=filt_nr) ? TRUE : disabled);
192 /* Remember that there are now temporary coloring filters set */
194 tmp_colors_set = TRUE;
202 /* Reset the temporary colorfilters */
204 color_filters_reset_tmp(void)
208 for ( i=1 ; i<=10 ; i++ ) {
209 color_filters_set_tmp(i, NULL, TRUE);
211 /* Remember that there are now *no* temporary coloring filters set */
212 tmp_colors_set = FALSE;
216 /* delete the specified filter */
218 color_filter_delete(color_filter_t *colorf)
220 if (colorf->filter_name != NULL)
221 g_free(colorf->filter_name);
222 if (colorf->filter_text != NULL)
223 g_free(colorf->filter_text);
224 if (colorf->c_colorfilter != NULL)
225 dfilter_free(colorf->c_colorfilter);
229 /* delete the specified filter (called from g_slist_foreach) */
231 color_filter_delete_cb(gpointer filter_arg, gpointer unused _U_)
233 color_filter_t *colorf = (color_filter_t *)filter_arg;
235 color_filter_delete(colorf);
238 /* delete the specified list */
240 color_filter_list_delete(GSList **cfl)
242 g_slist_foreach(*cfl, color_filter_delete_cb, NULL);
247 /* clone a single list entries from normal to edit list */
248 static color_filter_t *
249 color_filter_clone(color_filter_t *colorf)
251 color_filter_t *new_colorf;
253 new_colorf = (color_filter_t *)g_malloc(sizeof (color_filter_t));
254 new_colorf->filter_name = g_strdup(colorf->filter_name);
255 new_colorf->filter_text = g_strdup(colorf->filter_text);
256 new_colorf->bg_color = colorf->bg_color;
257 new_colorf->fg_color = colorf->fg_color;
258 new_colorf->disabled = colorf->disabled;
259 new_colorf->c_colorfilter = NULL;
260 new_colorf->edit_dialog = NULL;
261 new_colorf->selected = FALSE;
267 color_filter_list_clone_cb(gpointer filter_arg, gpointer cfl_arg)
269 GSList **cfl = (GSList **)cfl_arg;
270 color_filter_t *new_colorf;
272 new_colorf = color_filter_clone((color_filter_t *)filter_arg);
273 *cfl = g_slist_append(*cfl, new_colorf);
276 /* clone the specified list */
278 color_filter_list_clone(GSList *cfl)
280 GSList *new_list = NULL;
282 g_slist_foreach(cfl, color_filter_list_clone_cb, &new_list);
287 /* Initialize the filter structures (reading from file) for general running, including app startup */
289 color_filters_init(void)
291 /* delete all currently existing filters */
292 color_filter_list_delete(&color_filter_list);
294 /* start the list with the temporary colorizing rules */
295 color_filters_add_tmp(&color_filter_list);
297 /* try to read the users filters */
298 if (!read_users_filters(&color_filter_list))
299 /* if that failed, try to read the global filters */
300 color_filters_read_globals(&color_filter_list);
304 color_filters_reload(void)
306 /* "move" old entries to the deleted list
307 * we must keep them until the dissection no longer needs them */
308 color_filter_deleted_list = g_slist_concat(color_filter_deleted_list, color_filter_list);
309 color_filter_list = NULL;
311 /* start the list with the temporary colorizing rules */
312 color_filters_add_tmp(&color_filter_list);
314 /* try to read the users filters */
315 if (!read_users_filters(&color_filter_list))
316 /* if that failed, try to read the global filters */
317 color_filters_read_globals(&color_filter_list);
321 color_filters_cleanup(void)
323 /* delete the previously deleted filters */
324 color_filter_list_delete(&color_filter_deleted_list);
328 color_filters_clone_cb(gpointer filter_arg, gpointer user_data)
330 color_filter_t * new_colorf = color_filter_clone((color_filter_t *)filter_arg);
331 color_filter_add_cb (new_colorf, user_data);
335 color_filters_clone(gpointer user_data)
337 g_slist_foreach(color_filter_list, color_filters_clone_cb, user_data);
342 color_filter_compile_cb(gpointer filter_arg, gpointer unused _U_)
344 color_filter_t *colorf = (color_filter_t *)filter_arg;
346 g_assert(colorf->c_colorfilter == NULL);
347 if (!dfilter_compile(colorf->filter_text, &colorf->c_colorfilter)) {
348 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
349 "Could not compile color filter name: \"%s\" text: \"%s\".\n%s",
350 colorf->filter_name, colorf->filter_text, dfilter_error_msg);
351 /* this filter was compilable before, so this should never happen */
352 /* except if the OK button of the parent window has been clicked */
353 /* so don't use g_assert_not_reached() but check the filters again */
358 color_filter_validate_cb(gpointer filter_arg, gpointer unused _U_)
360 color_filter_t *colorf = (color_filter_t *)filter_arg;
362 g_assert(colorf->c_colorfilter == NULL);
363 if (!dfilter_compile(colorf->filter_text, &colorf->c_colorfilter)) {
364 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
365 "Removing color filter name: \"%s\" text: \"%s\".\n%s",
366 colorf->filter_name, colorf->filter_text, dfilter_error_msg);
367 /* Delete the color filter from the list of color filters. */
368 color_filter_valid_list = g_slist_remove(color_filter_valid_list, colorf);
369 color_filter_delete(colorf);
373 /* apply changes from the edit list */
375 color_filters_apply(GSList *tmp_cfl, GSList *edit_cfl)
377 /* "move" old entries to the deleted list
378 * we must keep them until the dissection no longer needs them */
379 color_filter_deleted_list = g_slist_concat(color_filter_deleted_list, color_filter_list);
380 color_filter_list = NULL;
382 /* clone all list entries from tmp/edit to normal list */
383 color_filter_valid_list = NULL;
384 color_filter_valid_list = color_filter_list_clone(tmp_cfl);
385 color_filter_valid_list = g_slist_concat(color_filter_valid_list,
386 color_filter_list_clone(edit_cfl) );
388 /* compile all filter */
389 g_slist_foreach(color_filter_valid_list, color_filter_validate_cb, NULL);
391 /* clone all list entries from tmp/edit to normal list */
392 color_filter_list = color_filter_list_clone(color_filter_valid_list);
394 /* compile all filter */
395 g_slist_foreach(color_filter_list, color_filter_compile_cb, NULL);
399 color_filters_used(void)
401 return color_filter_list != NULL && filters_enabled;
405 tmp_color_filters_used(void)
407 return tmp_colors_set;
411 color_filters_enable(gboolean enable)
413 new_packet_list_enable_color(enable);
417 /* prepare the epan_dissect_t for the filter */
419 prime_edt(gpointer data, gpointer user_data)
421 color_filter_t *colorf = (color_filter_t *)data;
422 epan_dissect_t *edt = (epan_dissect_t *)user_data;
424 if (colorf->c_colorfilter != NULL)
425 epan_dissect_prime_dfilter(edt, colorf->c_colorfilter);
428 /* Prime the epan_dissect_t with all the compiler
429 * color filters in 'color_filter_list'. */
431 color_filters_prime_edt(epan_dissect_t *edt)
433 if (color_filters_used())
434 g_slist_foreach(color_filter_list, prime_edt, edt);
437 /* * Return the color_t for later use */
438 const color_filter_t *
439 color_filters_colorize_packet(epan_dissect_t *edt)
442 color_filter_t *colorf;
444 /* If we have color filters, "search" for the matching one. */
445 if (color_filters_used()) {
446 curr = color_filter_list;
448 while(curr != NULL) {
449 colorf = (color_filter_t *)curr->data;
450 if ( (!colorf->disabled) &&
451 (colorf->c_colorfilter != NULL) &&
452 dfilter_apply_edt(colorf->c_colorfilter, edt)) {
455 curr = g_slist_next(curr);
462 /* read filters from the given file */
463 /* XXX - Would it make more sense to use GStrings here instead of reallocing
466 read_filters_file(FILE *f, gpointer user_data)
468 #define INIT_BUF_SIZE 128
470 gchar *filter_exp = NULL;
471 guint32 name_len = INIT_BUF_SIZE;
472 guint32 filter_exp_len = INIT_BUF_SIZE;
475 guint16 fg_r, fg_g, fg_b, bg_r, bg_g, bg_b;
476 gboolean disabled = FALSE;
477 gboolean skip_end_of_line = FALSE;
479 name = (gchar *)g_malloc(name_len + 1);
480 filter_exp = (gchar *)g_malloc(filter_exp_len + 1);
484 if (skip_end_of_line) {
487 } while (c != EOF && c != '\n');
491 skip_end_of_line = FALSE;
494 while ((c = getc(f)) != EOF && isspace(c)) {
508 /* skip # comments and invalid lines */
510 skip_end_of_line = TRUE;
514 /* we get the @ delimiter.
516 * @name@filter expression@[background r,g,b][foreground r,g,b]
523 if (c == EOF || c == '@')
526 /* buffer isn't long enough; double its length.*/
528 name = (gchar *)g_realloc(name, name_len + 1);
537 skip_end_of_line = TRUE;
541 /* retrieve filter expression */
545 if (c == EOF || c == '@')
547 if (i >= filter_exp_len) {
548 /* buffer isn't long enough; double its length.*/
550 filter_exp = (gchar *)g_realloc(filter_exp, filter_exp_len + 1);
554 filter_exp[i] = '\0';
559 skip_end_of_line = TRUE;
563 /* retrieve background and foreground colors */
564 if (fscanf(f,"[%hu,%hu,%hu][%hu,%hu,%hu]",
565 &bg_r, &bg_g, &bg_b, &fg_r, &fg_g, &fg_b) == 6) {
567 /* we got a complete color filter */
569 color_t bg_color, fg_color;
570 color_filter_t *colorf;
571 dfilter_t *temp_dfilter;
573 if (!dfilter_compile(filter_exp, &temp_dfilter)) {
574 g_warning("Could not compile color filter \"%s\" from saved filters: %s",
575 name, dfilter_error_msg);
576 skip_end_of_line = TRUE;
580 if (!initialize_color(&fg_color, fg_r, fg_g, fg_b)) {
582 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
583 "Could not allocate foreground color "
584 "specified in input file for %s.", name);
585 dfilter_free(temp_dfilter);
586 skip_end_of_line = TRUE;
589 if (!initialize_color(&bg_color, bg_r, bg_g, bg_b)) {
591 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
592 "Could not allocate background color "
593 "specified in input file for %s.", name);
594 dfilter_free(temp_dfilter);
595 skip_end_of_line = TRUE;
599 colorf = color_filter_new(name, filter_exp, &bg_color,
600 &fg_color, disabled);
601 if(user_data == &color_filter_list) {
602 GSList **cfl = (GSList **)user_data;
605 colorf->c_colorfilter = temp_dfilter;
606 *cfl = g_slist_append(*cfl, colorf);
609 /* just editing, don't need the compiled filter */
610 dfilter_free(temp_dfilter);
611 color_filter_add_cb (colorf, user_data);
615 skip_end_of_line = TRUE;
623 /* read filters from the user's filter file */
625 read_users_filters(GSList **cfl)
631 /* decide what file to open (from dfilter code) */
632 path = get_persconffile_path("colorfilters", TRUE, FALSE);
633 if ((f = ws_fopen(path, "r")) == NULL) {
634 if (errno != ENOENT) {
635 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
636 "Could not open filter file\n\"%s\": %s.", path,
645 ret = read_filters_file(f, cfl);
650 /* read filters from the filter file */
652 color_filters_read_globals(gpointer user_data)
658 /* decide what file to open (from dfilter code) */
659 path = get_datafile_path("colorfilters");
660 if ((f = ws_fopen(path, "r")) == NULL) {
661 if (errno != ENOENT) {
662 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
663 "Could not open global filter file\n\"%s\": %s.", path,
672 ret = read_filters_file(f, user_data);
677 /* read filters from some other filter file (import) */
679 color_filters_import(gchar *path, gpointer user_data)
684 if ((f = ws_fopen(path, "r")) == NULL) {
685 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
686 "Could not open\n%s\nfor reading: %s.",
687 path, g_strerror(errno));
691 ret = read_filters_file(f, user_data);
696 struct write_filter_data
699 gboolean only_selected;
702 /* save a single filter */
704 write_filter(gpointer filter_arg, gpointer data_arg)
706 struct write_filter_data *data = (struct write_filter_data *)data_arg;
707 color_filter_t *colorf = (color_filter_t *)filter_arg;
710 if ( (colorf->selected || !data->only_selected) &&
711 (strstr(colorf->filter_name,TEMP_COLOR_PREFIX)==NULL) ) {
712 fprintf(f,"%s@%s@%s@[%d,%d,%d][%d,%d,%d]\n",
713 colorf->disabled ? "!" : "",
716 colorf->bg_color.red,
717 colorf->bg_color.green,
718 colorf->bg_color.blue,
719 colorf->fg_color.red,
720 colorf->fg_color.green,
721 colorf->fg_color.blue);
725 /* save filters in a filter file */
727 write_filters_file(GSList *cfl, FILE *f, gboolean only_selected)
729 struct write_filter_data data;
732 data.only_selected = only_selected;
734 fprintf(f,"# DO NOT EDIT THIS FILE! It was created by Wireshark\n");
735 g_slist_foreach(cfl, write_filter, &data);
739 /* save filters in users filter file */
741 color_filters_write(GSList *cfl)
747 /* Create the directory that holds personal configuration files,
749 if (create_persconffile_dir(&pf_dir_path) == -1) {
750 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
751 "Can't create directory\n\"%s\"\nfor color files: %s.",
752 pf_dir_path, g_strerror(errno));
757 path = get_persconffile_path("colorfilters", TRUE, TRUE);
758 if ((f = ws_fopen(path, "w+")) == NULL) {
759 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
760 "Could not open\n%s\nfor writing: %s.",
761 path, g_strerror(errno));
766 write_filters_file(cfl, f, FALSE);
771 /* save filters in some other filter file (export) */
773 color_filters_export(gchar *path, GSList *cfl, gboolean only_marked)
777 if ((f = ws_fopen(path, "w+")) == NULL) {
778 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
779 "Could not open\n%s\nfor writing: %s.",
780 path, g_strerror(errno));
783 write_filters_file(cfl, f, only_marked);