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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 * Updated 1 Dec 10 jjm
36 #include <epan/filesystem.h>
37 #include <wsutil/file_util.h>
39 #include <epan/packet.h>
41 #include "color_filters.h"
43 #include <epan/dfilter/dfilter.h>
44 #include <epan/prefs.h>
46 #include "ui/simple_dialog.h"
47 #include "ui/ui_util.h"
49 #define RED_COMPONENT(x) (guint16) (((((x) >> 16) & 0xff) * 65535 / 255))
50 #define GREEN_COMPONENT(x) (guint16) (((((x) >> 8) & 0xff) * 65535 / 255))
51 #define BLUE_COMPONENT(x) (guint16) ( (((x) & 0xff) * 65535 / 255))
53 static gboolean read_users_filters(GSList **cfl);
55 /* the currently active filters */
56 static GSList *color_filter_list = NULL;
58 /* keep "old" deleted filters in this list until
59 * the dissection no longer needs them (e.g. file is closed) */
60 static GSList *color_filter_deleted_list = NULL;
61 static GSList *color_filter_valid_list = NULL;
63 /* Color Filters can en-/disabled. */
64 static gboolean filters_enabled = TRUE;
66 /* Remember if there are temporary coloring filters set to
67 * add sensitivity to the "Reset Coloring 1-10" menu item
69 static gboolean tmp_colors_set = FALSE;
71 /* Create a new filter */
73 color_filter_new(const gchar *name, /* The name of the filter to create */
74 const gchar *filter_string, /* The string representing the filter */
75 color_t *bg_color, /* The background color */
76 color_t *fg_color, /* The foreground color */
77 gboolean disabled) /* Is the filter disabled? */
79 color_filter_t *colorf;
81 colorf = (color_filter_t *)g_malloc(sizeof (color_filter_t));
82 colorf->filter_name = g_strdup(name);
83 colorf->filter_text = g_strdup(filter_string);
84 colorf->bg_color = *bg_color;
85 colorf->fg_color = *fg_color;
86 colorf->disabled = disabled;
87 colorf->c_colorfilter = NULL;
88 colorf->edit_dialog = NULL;
89 colorf->selected = FALSE;
93 /* Add ten empty (temporary) colorfilters for easy coloring */
95 color_filters_add_tmp(GSList **cfl)
102 color_t bg_color, fg_color;
103 color_filter_t *colorf;
105 g_assert(strlen(prefs.gui_colorized_fg)==69);
106 g_assert(strlen(prefs.gui_colorized_bg)==69);
107 fg_colors = g_strsplit(prefs.gui_colorized_fg, ",", -1);
108 bg_colors = g_strsplit(prefs.gui_colorized_bg, ",", -1);
110 for ( i=1 ; i<=10 ; i++ ) {
111 name = g_strdup_printf("%s%02d",CONVERSATION_COLOR_PREFIX,i);
113 /* retrieve background and foreground colors */
114 cval = strtoul(fg_colors[i-1], NULL, 16);
115 initialize_color(&fg_color, RED_COMPONENT(cval),
116 GREEN_COMPONENT(cval),
117 BLUE_COMPONENT(cval) );
118 cval = strtoul(bg_colors[i-1], NULL, 16);
119 initialize_color(&bg_color, RED_COMPONENT(cval),
120 GREEN_COMPONENT(cval),
121 BLUE_COMPONENT(cval) );
122 colorf = color_filter_new(name, NULL, &bg_color, &fg_color, TRUE);
123 colorf->filter_text = g_strdup("frame");
124 colorf->c_colorfilter = NULL;
125 *cfl = g_slist_append(*cfl, colorf);
130 g_strfreev(fg_colors);
131 g_strfreev(bg_colors);
137 color_filters_find_by_name_cb(gconstpointer arg1, gconstpointer arg2)
139 const color_filter_t *colorf = (const color_filter_t *)arg1;
140 const gchar *name = (const gchar *)arg2;
142 return (strstr(colorf->filter_name, name)==NULL) ? -1 : 0 ;
146 /* Set the filter off a temporary colorfilters and enable it */
148 color_filters_set_tmp(guint8 filt_nr, gchar *filter, gboolean disabled)
151 const gchar *tmpfilter = NULL;
153 color_filter_t *colorf;
154 dfilter_t *compiled_filter;
157 /* Go through the tomporary filters and look for the same filter string.
158 * If found, clear it so that a filter can be "moved" up and down the list
160 for ( i=1 ; i<=10 ; i++ ) {
161 /* If we need to reset the temporary filter (filter==NULL), don't look
162 * for other rules with the same filter string
164 if( i!=filt_nr && filter==NULL )
167 name = g_strdup_printf("%s%02d",CONVERSATION_COLOR_PREFIX,i);
168 cfl = g_slist_find_custom(color_filter_list, name, color_filters_find_by_name_cb);
169 colorf = (color_filter_t *)cfl->data;
171 /* Only change the filter rule if this is the rule to change or if
172 * a matching filter string has been found
174 if(colorf && ( (i==filt_nr) || (strstr(filter,colorf->filter_text)!=NULL) ) ) {
175 /* set filter string to "frame" if we are resetting the rules
176 * or if we found a matching filter string which need to be cleared
178 tmpfilter = ( (filter==NULL) || (i!=filt_nr) ) ? "frame" : filter;
179 if (!dfilter_compile(tmpfilter, &compiled_filter)) {
180 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
181 "Could not compile color filter name: \"%s\""
182 " text: \"%s\".\n%s", name, filter, dfilter_error_msg);
184 if (colorf->filter_text != NULL)
185 g_free(colorf->filter_text);
186 if (colorf->c_colorfilter != NULL)
187 dfilter_free(colorf->c_colorfilter);
188 colorf->filter_text = g_strdup(tmpfilter);
189 colorf->c_colorfilter = compiled_filter;
190 colorf->disabled = ((i!=filt_nr) ? TRUE : disabled);
191 /* Remember that there are now temporary coloring filters set */
193 tmp_colors_set = TRUE;
201 /* Reset the temporary colorfilters */
203 color_filters_reset_tmp(void)
207 for ( i=1 ; i<=10 ; i++ ) {
208 color_filters_set_tmp(i, NULL, TRUE);
210 /* Remember that there are now *no* temporary coloring filters set */
211 tmp_colors_set = FALSE;
215 /* delete the specified filter */
217 color_filter_delete(color_filter_t *colorf)
219 if (colorf->filter_name != NULL)
220 g_free(colorf->filter_name);
221 if (colorf->filter_text != NULL)
222 g_free(colorf->filter_text);
223 if (colorf->c_colorfilter != NULL)
224 dfilter_free(colorf->c_colorfilter);
228 /* delete the specified filter (called from g_slist_foreach) */
230 color_filter_delete_cb(gpointer filter_arg, gpointer unused _U_)
232 color_filter_t *colorf = (color_filter_t *)filter_arg;
234 color_filter_delete(colorf);
237 /* delete the specified list */
239 color_filter_list_delete(GSList **cfl)
241 g_slist_foreach(*cfl, color_filter_delete_cb, NULL);
246 /* clone a single list entries from normal to edit list */
247 static color_filter_t *
248 color_filter_clone(color_filter_t *colorf)
250 color_filter_t *new_colorf;
252 new_colorf = (color_filter_t *)g_malloc(sizeof (color_filter_t));
253 new_colorf->filter_name = g_strdup(colorf->filter_name);
254 new_colorf->filter_text = g_strdup(colorf->filter_text);
255 new_colorf->bg_color = colorf->bg_color;
256 new_colorf->fg_color = colorf->fg_color;
257 new_colorf->disabled = colorf->disabled;
258 new_colorf->c_colorfilter = NULL;
259 new_colorf->edit_dialog = NULL;
260 new_colorf->selected = FALSE;
266 color_filter_list_clone_cb(gpointer filter_arg, gpointer cfl_arg)
268 GSList **cfl = (GSList **)cfl_arg;
269 color_filter_t *new_colorf;
271 new_colorf = color_filter_clone((color_filter_t *)filter_arg);
272 *cfl = g_slist_append(*cfl, new_colorf);
275 /* clone the specified list */
277 color_filter_list_clone(GSList *cfl)
279 GSList *new_list = NULL;
281 g_slist_foreach(cfl, color_filter_list_clone_cb, &new_list);
286 /* Initialize the filter structures (reading from file) for general running, including app startup */
288 color_filters_init(void)
290 /* delete all currently existing filters */
291 color_filter_list_delete(&color_filter_list);
293 /* start the list with the temporary colorizing rules */
294 color_filters_add_tmp(&color_filter_list);
296 /* try to read the users filters */
297 if (!read_users_filters(&color_filter_list))
298 /* if that failed, try to read the global filters */
299 color_filters_read_globals(&color_filter_list);
303 color_filters_reload(void)
305 /* "move" old entries to the deleted list
306 * we must keep them until the dissection no longer needs them */
307 color_filter_deleted_list = g_slist_concat(color_filter_deleted_list, color_filter_list);
308 color_filter_list = NULL;
310 /* start the list with the temporary colorizing rules */
311 color_filters_add_tmp(&color_filter_list);
313 /* try to read the users filters */
314 if (!read_users_filters(&color_filter_list))
315 /* if that failed, try to read the global filters */
316 color_filters_read_globals(&color_filter_list);
320 color_filters_cleanup(void)
322 /* delete the previously deleted filters */
323 color_filter_list_delete(&color_filter_deleted_list);
327 color_filters_clone_cb(gpointer filter_arg, gpointer user_data)
329 color_filter_t * new_colorf = color_filter_clone((color_filter_t *)filter_arg);
330 color_filter_add_cb (new_colorf, user_data);
334 color_filters_clone(gpointer user_data)
336 g_slist_foreach(color_filter_list, color_filters_clone_cb, user_data);
341 color_filter_compile_cb(gpointer filter_arg, gpointer unused _U_)
343 color_filter_t *colorf = (color_filter_t *)filter_arg;
345 g_assert(colorf->c_colorfilter == NULL);
346 if (!dfilter_compile(colorf->filter_text, &colorf->c_colorfilter)) {
347 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
348 "Could not compile color filter name: \"%s\" text: \"%s\".\n%s",
349 colorf->filter_name, colorf->filter_text, dfilter_error_msg);
350 /* this filter was compilable before, so this should never happen */
351 /* except if the OK button of the parent window has been clicked */
352 /* so don't use g_assert_not_reached() but check the filters again */
357 color_filter_validate_cb(gpointer filter_arg, gpointer unused _U_)
359 color_filter_t *colorf = (color_filter_t *)filter_arg;
361 g_assert(colorf->c_colorfilter == NULL);
362 if (!dfilter_compile(colorf->filter_text, &colorf->c_colorfilter)) {
363 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
364 "Removing color filter name: \"%s\" text: \"%s\".\n%s",
365 colorf->filter_name, colorf->filter_text, dfilter_error_msg);
366 /* Delete the color filter from the list of color filters. */
367 color_filter_valid_list = g_slist_remove(color_filter_valid_list, colorf);
368 color_filter_delete(colorf);
372 /* apply changes from the edit list */
374 color_filters_apply(GSList *tmp_cfl, GSList *edit_cfl)
376 /* "move" old entries to the deleted list
377 * we must keep them until the dissection no longer needs them */
378 color_filter_deleted_list = g_slist_concat(color_filter_deleted_list, color_filter_list);
379 color_filter_list = NULL;
381 /* clone all list entries from tmp/edit to normal list */
382 color_filter_valid_list = NULL;
383 color_filter_valid_list = color_filter_list_clone(tmp_cfl);
384 color_filter_valid_list = g_slist_concat(color_filter_valid_list,
385 color_filter_list_clone(edit_cfl) );
387 /* compile all filter */
388 g_slist_foreach(color_filter_valid_list, color_filter_validate_cb, NULL);
390 /* clone all list entries from tmp/edit to normal list */
391 color_filter_list = color_filter_list_clone(color_filter_valid_list);
393 /* compile all filter */
394 g_slist_foreach(color_filter_list, color_filter_compile_cb, NULL);
398 color_filters_used(void)
400 return color_filter_list != NULL && filters_enabled;
404 tmp_color_filters_used(void)
406 return tmp_colors_set;
410 color_filters_enable(gboolean enable)
412 packet_list_enable_color(enable);
416 /* prepare the epan_dissect_t for the filter */
418 prime_edt(gpointer data, gpointer user_data)
420 color_filter_t *colorf = (color_filter_t *)data;
421 epan_dissect_t *edt = (epan_dissect_t *)user_data;
423 if (colorf->c_colorfilter != NULL)
424 epan_dissect_prime_dfilter(edt, colorf->c_colorfilter);
427 /* Prime the epan_dissect_t with all the compiler
428 * color filters in 'color_filter_list'. */
430 color_filters_prime_edt(epan_dissect_t *edt)
432 if (color_filters_used())
433 g_slist_foreach(color_filter_list, prime_edt, edt);
436 /* * Return the color_t for later use */
437 const color_filter_t *
438 color_filters_colorize_packet(epan_dissect_t *edt)
441 color_filter_t *colorf;
443 /* If we have color filters, "search" for the matching one. */
444 if (color_filters_used()) {
445 curr = color_filter_list;
447 while(curr != NULL) {
448 colorf = (color_filter_t *)curr->data;
449 if ( (!colorf->disabled) &&
450 (colorf->c_colorfilter != NULL) &&
451 dfilter_apply_edt(colorf->c_colorfilter, edt)) {
454 curr = g_slist_next(curr);
461 /* read filters from the given file */
462 /* XXX - Would it make more sense to use GStrings here instead of reallocing
465 read_filters_file(FILE *f, gpointer user_data)
467 #define INIT_BUF_SIZE 128
469 gchar *filter_exp = NULL;
470 guint32 name_len = INIT_BUF_SIZE;
471 guint32 filter_exp_len = INIT_BUF_SIZE;
474 guint16 fg_r, fg_g, fg_b, bg_r, bg_g, bg_b;
475 gboolean disabled = FALSE;
476 gboolean skip_end_of_line = FALSE;
478 name = (gchar *)g_malloc(name_len + 1);
479 filter_exp = (gchar *)g_malloc(filter_exp_len + 1);
483 if (skip_end_of_line) {
486 } while (c != EOF && c != '\n');
490 skip_end_of_line = FALSE;
493 while ((c = getc(f)) != EOF && isspace(c)) {
507 /* skip # comments and invalid lines */
509 skip_end_of_line = TRUE;
513 /* we get the @ delimiter.
515 * @name@filter expression@[background r,g,b][foreground r,g,b]
522 if (c == EOF || c == '@')
525 /* buffer isn't long enough; double its length.*/
527 name = (gchar *)g_realloc(name, name_len + 1);
536 skip_end_of_line = TRUE;
540 /* retrieve filter expression */
544 if (c == EOF || c == '@')
546 if (i >= filter_exp_len) {
547 /* buffer isn't long enough; double its length.*/
549 filter_exp = (gchar *)g_realloc(filter_exp, filter_exp_len + 1);
553 filter_exp[i] = '\0';
558 skip_end_of_line = TRUE;
562 /* retrieve background and foreground colors */
563 if (fscanf(f,"[%hu,%hu,%hu][%hu,%hu,%hu]",
564 &bg_r, &bg_g, &bg_b, &fg_r, &fg_g, &fg_b) == 6) {
566 /* we got a complete color filter */
568 color_t bg_color, fg_color;
569 color_filter_t *colorf;
570 dfilter_t *temp_dfilter;
572 if (!dfilter_compile(filter_exp, &temp_dfilter)) {
573 g_warning("Could not compile color filter \"%s\" from saved filters: %s",
574 name, dfilter_error_msg);
575 skip_end_of_line = TRUE;
579 if (!initialize_color(&fg_color, fg_r, fg_g, fg_b)) {
581 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
582 "Could not allocate foreground color "
583 "specified in input file for %s.", name);
584 dfilter_free(temp_dfilter);
585 skip_end_of_line = TRUE;
588 if (!initialize_color(&bg_color, bg_r, bg_g, bg_b)) {
590 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
591 "Could not allocate background color "
592 "specified in input file for %s.", name);
593 dfilter_free(temp_dfilter);
594 skip_end_of_line = TRUE;
598 colorf = color_filter_new(name, filter_exp, &bg_color,
599 &fg_color, disabled);
600 if(user_data == &color_filter_list) {
601 GSList **cfl = (GSList **)user_data;
604 colorf->c_colorfilter = temp_dfilter;
605 *cfl = g_slist_append(*cfl, colorf);
608 /* just editing, don't need the compiled filter */
609 dfilter_free(temp_dfilter);
610 color_filter_add_cb (colorf, user_data);
614 skip_end_of_line = TRUE;
622 /* read filters from the user's filter file */
624 read_users_filters(GSList **cfl)
630 /* decide what file to open (from dfilter code) */
631 path = get_persconffile_path("colorfilters", TRUE, FALSE);
632 if ((f = ws_fopen(path, "r")) == NULL) {
633 if (errno != ENOENT) {
634 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
635 "Could not open filter file\n\"%s\": %s.", path,
644 ret = read_filters_file(f, cfl);
649 /* read filters from the filter file */
651 color_filters_read_globals(gpointer user_data)
657 /* decide what file to open (from dfilter code) */
658 path = get_datafile_path("colorfilters");
659 if ((f = ws_fopen(path, "r")) == NULL) {
660 if (errno != ENOENT) {
661 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
662 "Could not open global filter file\n\"%s\": %s.", path,
671 ret = read_filters_file(f, user_data);
676 /* read filters from some other filter file (import) */
678 color_filters_import(gchar *path, gpointer user_data)
683 if ((f = ws_fopen(path, "r")) == NULL) {
684 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
685 "Could not open\n%s\nfor reading: %s.",
686 path, g_strerror(errno));
690 ret = read_filters_file(f, user_data);
695 struct write_filter_data
698 gboolean only_selected;
701 /* save a single filter */
703 write_filter(gpointer filter_arg, gpointer data_arg)
705 struct write_filter_data *data = (struct write_filter_data *)data_arg;
706 color_filter_t *colorf = (color_filter_t *)filter_arg;
709 if ( (colorf->selected || !data->only_selected) &&
710 (strstr(colorf->filter_name,CONVERSATION_COLOR_PREFIX)==NULL) ) {
711 fprintf(f,"%s@%s@%s@[%d,%d,%d][%d,%d,%d]\n",
712 colorf->disabled ? "!" : "",
715 colorf->bg_color.red,
716 colorf->bg_color.green,
717 colorf->bg_color.blue,
718 colorf->fg_color.red,
719 colorf->fg_color.green,
720 colorf->fg_color.blue);
724 /* save filters in a filter file */
726 write_filters_file(GSList *cfl, FILE *f, gboolean only_selected)
728 struct write_filter_data data;
731 data.only_selected = only_selected;
733 fprintf(f,"# DO NOT EDIT THIS FILE! It was created by Wireshark\n");
734 g_slist_foreach(cfl, write_filter, &data);
738 /* save filters in users filter file */
740 color_filters_write(GSList *cfl)
746 /* Create the directory that holds personal configuration files,
748 if (create_persconffile_dir(&pf_dir_path) == -1) {
749 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
750 "Can't create directory\n\"%s\"\nfor color files: %s.",
751 pf_dir_path, g_strerror(errno));
756 path = get_persconffile_path("colorfilters", TRUE, TRUE);
757 if ((f = ws_fopen(path, "w+")) == NULL) {
758 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
759 "Could not open\n%s\nfor writing: %s.",
760 path, g_strerror(errno));
765 write_filters_file(cfl, f, FALSE);
770 /* save filters in some other filter file (export) */
772 color_filters_export(gchar *path, GSList *cfl, gboolean only_marked)
776 if ((f = ws_fopen(path, "w+")) == NULL) {
777 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
778 "Could not open\n%s\nfor writing: %s.",
779 path, g_strerror(errno));
782 write_filters_file(cfl, f, only_marked);
788 * Editor modelines - http://www.wireshark.org/tools/modelines.html
793 * indent-tabs-mode: nil
796 * vi: set shiftwidth=4 tabstop=8 expandtab:
797 * :indentSize=4:tabSize=8:noTabs=true: