2 * expert_comp_table 2005 Greg Morris
3 * Portions copied from service_response_time_table.c by Ronnie Sahlberg
4 * Helper routines common to all composite expert statistics
9 * Wireshark - Network traffic analyzer
10 * By Gerald Combs <gerald@wireshark.org>
11 * Copyright 1998 Gerald Combs
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
39 #include "epan/packet_info.h"
40 #include <epan/expert.h>
42 #include "../simple_dialog.h"
43 #include "../globals.h"
46 #include "gtk/expert_comp_table.h"
47 #include "gtk/filter_utils.h"
48 #include "gtk/find_dlg.h"
49 #include "gtk/color_dlg.h"
51 #include "gtk/gui_utils.h"
52 #include "gtk/gtkglobals.h"
53 #include "gtk/webbrowser.h"
56 #define SORT_ALPHABETICAL 0
59 sort_iter_compare_func (GtkTreeModel *model,
64 gint sortcol = GPOINTER_TO_INT(userdata);
68 case SORT_ALPHABETICAL:
71 gtk_tree_model_get(model, a, 0, &name1, -1);
72 gtk_tree_model_get(model, b, 0, &name2, -1);
73 if (name1 == NULL || name2 == NULL)
75 if (name1 == NULL && name2 == NULL)
76 break; /* both equal => ret = 0 */
77 ret = (name1 == NULL) ? -1 : 1;
81 ret = g_ascii_strcasecmp(name1,name2);
88 g_return_val_if_reached(0);
102 static gint find_summary_data(error_equiv_table *err, const expert_info_t *expert_data)
106 /* First time thru values will be 0 */
107 if (err->num_procs==0) {
110 for (i=0;i<err->num_procs;i++) {
111 if (strcmp(err->procedures[i].entries[1], expert_data->protocol) == 0 &&
112 strcmp(err->procedures[i].entries[2], expert_data->summary) == 0) {
120 error_select_filter_cb(GtkWidget *widget _U_, gpointer callback_data, guint callback_action)
122 int action, type, selection;
123 error_equiv_table *err = (error_equiv_table *)callback_data;
125 const char *current_filter;
129 expert_info_t expert_data;
132 action=FILTER_ACTION(callback_action);
133 type=FILTER_ACTYPE(callback_action);
136 gtk_tree_selection_get_selected(err->select, &model, &iter);
138 gtk_tree_model_get (model, &iter,
140 PROTOCOL_COLUMN, &expert_data.protocol,
141 SUMMARY_COLUMN, &expert_data.summary,
144 if (strcmp(grp, "Packet:")==0) {
145 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "You cannot filter or search for packet number. Click on a valid item header.");
147 g_free(expert_data.protocol);
148 g_free(expert_data.summary);
154 /* XXX: find_summary_data doesn't (currently) reference expert_data.group. */
155 /* If "group" is required, then the message from GROUP_COLUMN will need */
156 /* to be translated to the group number (or the actual group number */
157 /* will also need to be stored in the TreeModel). */
158 selection = find_summary_data(err, &expert_data);
160 g_free(expert_data.protocol);
161 g_free(expert_data.summary);
163 if(selection>=(int)err->num_procs){
164 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "No items are selected");
167 current_filter=gtk_entry_get_text(GTK_ENTRY(main_display_filter_widget));
169 /* Some expert data doesn't pass an expert item. Without this we cannot create a filter */
170 /* But allow for searching of internet for error string */
171 if (action != 6 && action != 7) {
172 if (err->procedures[selection].fvalue_value==NULL) {
173 if (action != 2 && action != 3 && action != 4) {
174 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "Wireshark cannot create a filter on this item - %s, try using find instead.", err->procedures[selection].entries[2]);
179 case ACTYPE_SELECTED:
180 /* if no expert item was passed */
181 if (err->procedures[selection].fvalue_value==NULL) {
182 g_strlcpy(str, err->procedures[selection].entries[2], sizeof(str));
186 /* expert item exists. Use it. */
187 g_strlcpy(str, err->procedures[selection].fvalue_value, sizeof(str));
190 case ACTYPE_NOT_SELECTED:
191 /* if no expert item was passed */
192 if (err->procedures[selection].fvalue_value==NULL) {
193 g_snprintf(str, sizeof(str), "!%s", err->procedures[selection].entries[2]);
197 /* expert item exists. Use it. */
198 g_snprintf(str, sizeof(str), "!(%s)", err->procedures[selection].fvalue_value);
201 /* the remaining cases will only exist if the expert item exists so no need to check */
202 case ACTYPE_AND_SELECTED:
203 if ((!current_filter) || (0 == strlen(current_filter)))
204 g_strlcpy(str, err->procedures[selection].fvalue_value, sizeof(str));
206 g_snprintf(str, sizeof(str), "(%s) && (%s)", current_filter, err->procedures[selection].fvalue_value);
208 case ACTYPE_OR_SELECTED:
209 if ((!current_filter) || (0 == strlen(current_filter)))
210 g_strlcpy(str, err->procedures[selection].fvalue_value, sizeof(str));
212 g_snprintf(str, sizeof(str), "(%s) || (%s)", current_filter, err->procedures[selection].fvalue_value);
214 case ACTYPE_AND_NOT_SELECTED:
215 if ((!current_filter) || (0 == strlen(current_filter)))
216 g_snprintf(str, sizeof(str), "!(%s)", err->procedures[selection].fvalue_value);
218 g_snprintf(str, sizeof(str), "(%s) && !(%s)", current_filter, err->procedures[selection].fvalue_value);
220 case ACTYPE_OR_NOT_SELECTED:
221 if ((!current_filter) || (0 == strlen(current_filter)))
222 g_snprintf(str, sizeof(str), "!(%s)", err->procedures[selection].fvalue_value);
224 g_snprintf(str, sizeof(str), "(%s) || !(%s)", current_filter, err->procedures[selection].fvalue_value);
227 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "Can't find menu type - %u", type);
233 main_filter_packets(&cfile, str, FALSE);
236 gtk_entry_set_text(GTK_ENTRY(main_display_filter_widget), str);
238 case ACTION_FIND_FRAME:
239 /* When trying to perform a find without expert item, we must pass
240 * the expert string to the find window. The user might need to modify
241 * the string and click on the text search to locate the packet in question.
242 * So regardless of the type we will just bring up the find window and allow
243 * the user to modify the search criteria and options.
245 find_frame_with_filter(str);
247 case ACTION_FIND_NEXT:
248 /* In the case of find next, if there was no expert item, then most likely the expert
249 * string was modified to locate the text inside the message. So we can't just perform
250 * a find with the expert string or we will not really be performing a find next.
251 * In an effort to allow the user to modify the string and/or continue searching, we
252 * will just present the user with the find window again with the default expert string.
253 * A better aproach would be to attempt in capturing the last find string and utilize this
254 * with a find next/previous. Also a better approach might be to just send a <Ctl-N> keystroke.
256 if (err->procedures[selection].fvalue_value==NULL) {
257 find_frame_with_filter(str);
261 /* We have an expert item so just continue search without find dialog. */
262 find_previous_next_frame_with_filter(str, FALSE);
265 case ACTION_FIND_PREVIOUS:
266 /* In the case of find previous, if there was no expert item, then most likely the expert
267 * string was modified to locate the text inside the message. So we can't just perform
268 * a find with the expert string or we will not really be performing a find previous.
269 * In an effort to allow the user to modify the string and/or continue searching, we
270 * will just present the user with the find window again with the default expert string.
271 * A better aproach would be to attempt in capturing the last find string and utilize this
272 * with a find next/previous. Also a better approach might be to just send a <Ctl-B> keystroke.
274 if (err->procedures[selection].fvalue_value==NULL) {
275 find_frame_with_filter(str);
279 /* We have an expert item so just continue search without find dialog. */
280 find_previous_next_frame_with_filter(str, TRUE);
283 case ACTION_COLORIZE:
284 color_display_with_filter(str);
286 case ACTION_WEB_LOOKUP:
287 /* Lookup expert string on internet. Default search via www.google.com */
288 g_snprintf(str, sizeof(str), "http://www.google.com/search?hl=en&q=%s+'%s'", err->procedures[selection].entries[1], err->procedures[selection].entries[2]);
289 browser_open_url(str);
292 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "Can't find menu action - %u", action);
297 error_show_popup_menu_cb(void *widg _U_, GdkEvent *event, error_equiv_table *err)
299 GdkEventButton *bevent = (GdkEventButton *)event;
301 if(event->type==GDK_BUTTON_PRESS && bevent->button==3){
302 gtk_menu_popup(GTK_MENU(err->menu), NULL, NULL, NULL, NULL,
303 bevent->button, bevent->time);
309 static GtkItemFactoryEntry error_list_menu_items[] =
312 {"/Apply as Filter", NULL, NULL, 0, "<Branch>", NULL,},
313 {"/Apply as Filter/Selected", NULL,
314 GTK_MENU_FUNC(error_select_filter_cb), CALLBACK_MATCH(ACTYPE_SELECTED, 0),
316 {"/Apply as Filter/... not Selected", NULL,
317 GTK_MENU_FUNC(error_select_filter_cb), CALLBACK_MATCH(ACTYPE_NOT_SELECTED, 0),
319 {"/Apply as Filter/.. and Selected", NULL,
320 GTK_MENU_FUNC(error_select_filter_cb), CALLBACK_MATCH(ACTYPE_AND_SELECTED, 0),
322 {"/Apply as Filter/... or Selected", NULL,
323 GTK_MENU_FUNC(error_select_filter_cb), CALLBACK_MATCH(ACTYPE_OR_SELECTED, 0),
325 {"/Apply as Filter/... and not Selected", NULL,
326 GTK_MENU_FUNC(error_select_filter_cb), CALLBACK_MATCH(ACTYPE_AND_NOT_SELECTED, 0),
328 {"/Apply as Filter/... or not Selected", NULL,
329 GTK_MENU_FUNC(error_select_filter_cb), CALLBACK_MATCH(ACTYPE_OR_NOT_SELECTED, 0),
333 {"/Prepare a Filter", NULL, NULL, 0, "<Branch>", NULL,},
334 {"/Prepare a Filter/Selected", NULL,
335 GTK_MENU_FUNC(error_select_filter_cb), CALLBACK_PREPARE(ACTYPE_SELECTED, 0),
337 {"/Prepare a Filter/Not Selected", NULL,
338 GTK_MENU_FUNC(error_select_filter_cb), CALLBACK_PREPARE(ACTYPE_NOT_SELECTED, 0),
340 {"/Prepare a Filter/... and Selected", NULL,
341 GTK_MENU_FUNC(error_select_filter_cb), CALLBACK_PREPARE(ACTYPE_AND_SELECTED, 0),
343 {"/Prepare a Filter/... or Selected", NULL,
344 GTK_MENU_FUNC(error_select_filter_cb), CALLBACK_PREPARE(ACTYPE_OR_SELECTED, 0),
346 {"/Prepare a Filter/... and not Selected", NULL,
347 GTK_MENU_FUNC(error_select_filter_cb), CALLBACK_PREPARE(ACTYPE_AND_NOT_SELECTED, 0),
349 {"/Prepare a Filter/... or not Selected", NULL,
350 GTK_MENU_FUNC(error_select_filter_cb), CALLBACK_PREPARE(ACTYPE_OR_NOT_SELECTED, 0),
354 {"/Find Frame", NULL, NULL, 0, "<Branch>", NULL,},
355 {"/Find Frame/Find Frame", NULL, NULL, 0, "<Branch>", NULL,},
356 {"/Find Frame/Find Frame/Selected", NULL,
357 GTK_MENU_FUNC(error_select_filter_cb), CALLBACK_FIND_FRAME(ACTYPE_SELECTED, 0),
359 {"/Find Frame/Find Frame/Not Selected", NULL,
360 GTK_MENU_FUNC(error_select_filter_cb), CALLBACK_FIND_FRAME(ACTYPE_NOT_SELECTED, 0),
363 {"/Find Frame/Find Next", NULL, NULL, 0, "<Branch>", NULL,},
364 {"/Find Frame/Find Next/Selected", NULL,
365 GTK_MENU_FUNC(error_select_filter_cb), CALLBACK_FIND_NEXT(ACTYPE_SELECTED, 0),
367 {"/Find Frame/Find Next/Not Selected", NULL,
368 GTK_MENU_FUNC(error_select_filter_cb), CALLBACK_FIND_NEXT(ACTYPE_NOT_SELECTED, 0),
372 {"/Find Frame/Find Previous", NULL, NULL, 0, "<Branch>", NULL,},
373 {"/Find Frame/Find Previous/Selected", NULL,
374 GTK_MENU_FUNC(error_select_filter_cb), CALLBACK_FIND_PREVIOUS(ACTYPE_SELECTED, 0),
376 {"/Find Frame/Find Previous/Not Selected", NULL,
377 GTK_MENU_FUNC(error_select_filter_cb), CALLBACK_FIND_PREVIOUS(ACTYPE_NOT_SELECTED, 0),
380 /* Colorize Procedure */
381 {"/Colorize Procedure", NULL, NULL, 0, "<Branch>", NULL,},
382 {"/Colorize Procedure/Selected", NULL,
383 GTK_MENU_FUNC(error_select_filter_cb), CALLBACK_COLORIZE(ACTYPE_SELECTED, 0),
385 {"/Colorize Procedure/Not Selected", NULL,
386 GTK_MENU_FUNC(error_select_filter_cb), CALLBACK_COLORIZE(ACTYPE_NOT_SELECTED, 0),
389 /* Search Internet */
390 {"/Internet Search for Info Text", NULL,
391 GTK_MENU_FUNC(error_select_filter_cb), CALLBACK_WEB_LOOKUP, NULL, NULL,}
395 expert_goto_pkt_cb (GtkTreeSelection *selection, gpointer data _U_)
402 if (gtk_tree_selection_get_selected (selection, &model, &iter))
404 gtk_tree_model_get (model, &iter,
405 PROTOCOL_COLUMN, &pkt,
409 if (strcmp(grp, "Packet:")==0) {
410 cf_goto_frame(&cfile, atoi(pkt));
418 error_create_popup_menu(error_equiv_table *err)
420 GtkItemFactory *item_factory;
423 err->select = gtk_tree_view_get_selection (GTK_TREE_VIEW (err->tree_view));
424 gtk_tree_selection_set_mode (err->select, GTK_SELECTION_SINGLE);
425 g_signal_connect (G_OBJECT (err->select), "changed",
426 G_CALLBACK (expert_goto_pkt_cb),
428 item_factory = gtk_item_factory_new(GTK_TYPE_MENU, "<main>", NULL);
430 gtk_item_factory_create_items_ac(item_factory, sizeof(error_list_menu_items)/sizeof(error_list_menu_items[0]), error_list_menu_items, err, 2);
432 err->menu = gtk_item_factory_get_widget(item_factory, "<main>");
433 g_signal_connect(err->tree_view, "button_press_event", G_CALLBACK(error_show_popup_menu_cb), err);
437 init_error_table(error_equiv_table *err, guint16 num_procs, GtkWidget *vbox)
442 GtkTreeViewColumn *column;
443 GtkCellRenderer *renderer;
444 GtkTreeSortable *sortable;
446 /* Create the store */
447 store = gtk_tree_store_new (4, /* Total number of columns */
448 G_TYPE_STRING, /* Group */
449 G_TYPE_STRING, /* Protocol */
450 G_TYPE_STRING, /* Summary */
451 G_TYPE_INT); /* Count */
454 tree = gtk_tree_view_new_with_model (GTK_TREE_MODEL (store));
455 err->tree_view = GTK_TREE_VIEW(tree);
456 sortable = GTK_TREE_SORTABLE(store);
458 /* Setup the sortable columns */
459 gtk_tree_sortable_set_sort_func(sortable, SORT_ALPHABETICAL, sort_iter_compare_func, GINT_TO_POINTER(SORT_ALPHABETICAL), NULL);
460 gtk_tree_sortable_set_sort_column_id(sortable, SORT_ALPHABETICAL, GTK_SORT_ASCENDING);
461 gtk_tree_view_set_headers_clickable(GTK_TREE_VIEW (tree), FALSE);
463 /* The view now holds a reference. We can get rid of our own reference */
464 g_object_unref (G_OBJECT (store));
466 /* Create a cell renderer */
467 renderer = gtk_cell_renderer_text_new ();
469 /* Create the first column, associating the "text" attribute of the
470 * cell_renderer to the first column of the model */
471 column = gtk_tree_view_column_new_with_attributes ("Group", renderer, "text", GROUP_COLUMN, NULL);
472 gtk_tree_view_column_set_sort_column_id(column, 0);
473 gtk_tree_view_column_set_resizable(column, TRUE);
474 /* Add the column to the view. */
475 gtk_tree_view_append_column (GTK_TREE_VIEW (err->tree_view), column);
477 /* Second column.. Protocol. */
478 renderer = gtk_cell_renderer_text_new ();
479 column = gtk_tree_view_column_new_with_attributes ("Protocol", renderer, "text", PROTOCOL_COLUMN, NULL);
480 gtk_tree_view_column_set_sort_column_id(column, 1);
481 gtk_tree_view_column_set_resizable(column, TRUE);
482 gtk_tree_view_append_column (GTK_TREE_VIEW (err->tree_view), column);
484 /* Third column.. Summary. */
485 renderer = gtk_cell_renderer_text_new ();
486 column = gtk_tree_view_column_new_with_attributes ("Summary", renderer, "text", SUMMARY_COLUMN, NULL);
487 gtk_tree_view_column_set_sort_column_id(column, 2);
488 gtk_tree_view_column_set_resizable(column, TRUE);
489 gtk_tree_view_append_column (GTK_TREE_VIEW (err->tree_view), column);
491 /* Last column.. Count. */
492 column = gtk_tree_view_column_new_with_attributes ("Count", renderer, "text", COUNT_COLUMN, NULL);
493 gtk_tree_view_column_set_sort_column_id(column, 3);
494 gtk_tree_view_column_set_resizable(column, TRUE);
495 gtk_tree_view_append_column (GTK_TREE_VIEW (err->tree_view), column);
497 err->scrolled_window=scrolled_window_new(NULL, NULL);
499 gtk_container_add(GTK_CONTAINER(err->scrolled_window), GTK_WIDGET (err->tree_view));
501 gtk_box_pack_start(GTK_BOX(vbox), err->scrolled_window, TRUE, TRUE, 0);
503 gtk_tree_view_set_search_column (err->tree_view, SUMMARY_COLUMN); /* Allow searching the summary */
504 gtk_tree_view_set_reorderable (err->tree_view, TRUE); /* Allow user to reorder data with drag n drop */
506 /* Now enable the sorting of each column */
507 gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(err->tree_view), TRUE);
508 gtk_tree_view_set_headers_clickable(GTK_TREE_VIEW(err->tree_view), TRUE);
510 gtk_widget_show(err->scrolled_window);
512 err->num_procs=num_procs;
513 err->procedures=g_malloc(sizeof(error_procedure_t)*(num_procs+1));
514 for(i=0;i<num_procs;i++){
516 err->procedures[i].entries[j]=NULL; /* reset all values */
520 /* create popup menu for this table */
521 error_create_popup_menu(err);
525 init_error_table_row(error_equiv_table *err, const expert_info_t *expert_data)
527 guint16 old_num_procs=err->num_procs;
533 /* we have discovered a new procedure. Extend the table accordingly */
534 row = find_summary_data(err, expert_data);
536 /* First time we have seen this event so initialize memory table */
537 row = old_num_procs; /* Number of expert events since this is a new event */
538 err->procedures=g_realloc(err->procedures, (sizeof(error_procedure_t)*(old_num_procs+1)));
539 err->procedures[row].count=0; /* count of events for this item */
540 err->procedures[row].fvalue_value = NULL; /* Filter string value */
542 err->procedures[row].entries[j]=NULL;
545 /* Create the item in our memory table */
546 err->procedures[row].entries[0]=(char *)g_strdup(val_to_str(expert_data->group, expert_group_vals,"Unknown group (%u)")); /* Group */
547 err->procedures[row].entries[1]=(char *)g_strdup(expert_data->protocol); /* Protocol */
548 err->procedures[row].entries[2]=(char *)g_strdup(expert_data->summary); /* Summary */
550 /* Create a new item in our tree view */
551 store = GTK_TREE_STORE(gtk_tree_view_get_model(err->tree_view)); /* Get store */
552 gtk_tree_store_append (store, &err->procedures[row].iter, NULL); /* Acquire an iterator */
554 /* (Note: gtk_tree_store_set *copies* the input strings) */
555 gtk_tree_store_set (store, &err->procedures[row].iter,
556 GROUP_COLUMN, val_to_str(expert_data->group, expert_group_vals,"Unknown group (%u)"),
557 PROTOCOL_COLUMN, expert_data->protocol,
558 SUMMARY_COLUMN, expert_data->summary,
561 /* If an expert item was passed then build the filter string */
562 if (expert_data->pitem) {
565 filter = proto_construct_match_selected_string(PITEM_FINFO(expert_data->pitem), NULL);
567 err->procedures[row].fvalue_value = g_strdup(filter);
569 /* Store the updated count of events */
570 err->num_procs = ++old_num_procs;
573 /* Update our memory table with event data */
574 err->procedures[row].count++; /* increment the count of events for this item */
576 /* Store the updated count for this event item */
577 err->procedures[row].entries[3]=(char *)g_strdup_printf("%d", err->procedures[row].count); /* Count */
579 /* Update the tree with new count for this event */
580 store = GTK_TREE_STORE(gtk_tree_view_get_model(err->tree_view));
581 gtk_tree_store_set(store, &err->procedures[row].iter, COUNT_COLUMN, err->procedures[row].count, -1);
585 add_error_table_data(error_equiv_table *err, const expert_info_t *expert_data)
587 error_procedure_t *errp;
590 GtkTreeIter new_iter;
593 index = find_summary_data(err, expert_data);
595 /* We should never encounter a condition where we cannot find the expert data. If
596 * we do then we will just abort.
599 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "Could not find expert data. Aborting");
602 errp=&err->procedures[index];
604 store = GTK_TREE_STORE(gtk_tree_view_get_model(err->tree_view));
606 g_snprintf(str, sizeof(str), "%d", expert_data->packet_num);
607 gtk_tree_store_append(store, &new_iter, &errp->iter);
608 gtk_tree_store_set(store, &new_iter,
609 GROUP_COLUMN, "Packet:",
610 PROTOCOL_COLUMN, str,
616 reset_error_table_data(error_equiv_table *err)
621 for(i=0;i<err->num_procs;i++){
622 err->procedures[i].entries[0] = NULL;
623 err->procedures[i].entries[1] = NULL;
624 err->procedures[i].entries[2] = NULL;
625 err->procedures[i].entries[3] = NULL;
626 err->procedures[i].count=0;
629 store = GTK_TREE_STORE(gtk_tree_view_get_model(err->tree_view));
630 gtk_tree_store_clear(store);
635 free_error_table_data(error_equiv_table *err)
639 for(i=0;i<err->num_procs;i++){
641 if(err->procedures[i].entries[j]){
642 err->procedures[i].entries[j]=NULL;
644 err->procedures[i].fvalue_value=NULL;
645 err->procedures[i].count=0;
648 err->procedures=NULL;