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>
41 #include <epan/emem.h>
43 #include "../simple_dialog.h"
44 #include "../globals.h"
47 #include "gtk/expert_comp_table.h"
48 #include "gtk/filter_utils.h"
49 #include "gtk/find_dlg.h"
50 #include "gtk/color_dlg.h"
52 #include "gtk/gui_utils.h"
53 #include "gtk/gtkglobals.h"
54 #include "gtk/webbrowser.h"
57 #define SORT_ALPHABETICAL 0
60 sort_iter_compare_func (GtkTreeModel *model,
65 gint sortcol = GPOINTER_TO_INT(userdata);
69 case SORT_ALPHABETICAL:
72 gtk_tree_model_get(model, a, 0, &name1, -1);
73 gtk_tree_model_get(model, b, 0, &name2, -1);
74 if (name1 == NULL || name2 == NULL)
76 if (name1 == NULL && name2 == NULL)
77 break; /* both equal => ret = 0 */
78 ret = (name1 == NULL) ? -1 : 1;
82 ret = g_ascii_strcasecmp(name1,name2);
89 g_return_val_if_reached(0);
103 static gint find_summary_data(error_equiv_table *err, const expert_info_t *expert_data)
107 /* First time thru values will be 0 */
108 if (err->num_procs==0) {
111 for (i=0;i<err->num_procs;i++) {
112 if (strcmp(err->procedures[i].entries[1], expert_data->protocol) == 0 &&
113 strcmp(err->procedures[i].entries[2], expert_data->summary) == 0) {
121 error_select_filter_cb(GtkWidget *widget _U_, gpointer callback_data, guint callback_action)
123 int action, type, selection;
124 error_equiv_table *err = (error_equiv_table *)callback_data;
126 const char *current_filter;
130 const 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, GROUP_COLUMN, &expert_data.group, -1);
139 gtk_tree_model_get (model, &iter, PROTOCOL_COLUMN, &expert_data.protocol, -1);
140 gtk_tree_model_get (model, &iter, SUMMARY_COLUMN, &expert_data.summary, -1);
142 if (strcmp((char *)(unsigned long)expert_data.group, "Packet:")==0) {
143 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "You cannot filter or search for packet number. Click on a valid item header.");
147 selection = find_summary_data(err, &expert_data);
149 if(selection>=(int)err->num_procs){
150 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "No items are selected");
153 current_filter=gtk_entry_get_text(GTK_ENTRY(main_display_filter_widget));
155 /* Some expert data doesn't pass an expert item. Without this we cannot create a filter */
156 /* But allow for searching of internet for error string */
157 if (action != 6 && action != 7) {
158 if (err->procedures[selection].fvalue_value==NULL) {
159 if (action != 2 && action != 3 && action != 4) {
160 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]);
165 case ACTYPE_SELECTED:
166 /* if no expert item was passed */
167 if (err->procedures[selection].fvalue_value==NULL) {
168 g_snprintf(str, 255, "%s", err->procedures[selection].entries[2]);
172 /* expert item exists. Use it. */
173 g_snprintf(str, 255, "%s", err->procedures[selection].fvalue_value);
176 case ACTYPE_NOT_SELECTED:
177 /* if no expert item was passed */
178 if (err->procedures[selection].fvalue_value==NULL) {
179 g_snprintf(str, 255, "!%s", err->procedures[selection].entries[2]);
183 /* expert item exists. Use it. */
184 g_snprintf(str, 255, "!(%s)", err->procedures[selection].fvalue_value);
187 /* the remaining cases will only exist if the expert item exists so no need to check */
188 case ACTYPE_AND_SELECTED:
189 if ((!current_filter) || (0 == strlen(current_filter)))
190 g_snprintf(str, 255, "%s", err->procedures[selection].fvalue_value);
192 g_snprintf(str, 255, "(%s) && (%s)", current_filter, err->procedures[selection].fvalue_value);
194 case ACTYPE_OR_SELECTED:
195 if ((!current_filter) || (0 == strlen(current_filter)))
196 g_snprintf(str, 255, "%s", err->procedures[selection].fvalue_value);
198 g_snprintf(str, 255, "(%s) || (%s)", current_filter, err->procedures[selection].fvalue_value);
200 case ACTYPE_AND_NOT_SELECTED:
201 if ((!current_filter) || (0 == strlen(current_filter)))
202 g_snprintf(str, 255, "!(%s)", err->procedures[selection].fvalue_value);
204 g_snprintf(str, 255, "(%s) && !(%s)", current_filter, err->procedures[selection].fvalue_value);
206 case ACTYPE_OR_NOT_SELECTED:
207 if ((!current_filter) || (0 == strlen(current_filter)))
208 g_snprintf(str, 255, "!(%s)", err->procedures[selection].fvalue_value);
210 g_snprintf(str, 255, "(%s) || !(%s)", current_filter, err->procedures[selection].fvalue_value);
213 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "Can't find menu type - %u", type);
219 main_filter_packets(&cfile, str, FALSE);
222 gtk_entry_set_text(GTK_ENTRY(main_display_filter_widget), str);
224 case ACTION_FIND_FRAME:
225 /* When trying to perform a find without expert item, we must pass
226 * the expert string to the find window. The user might need to modify
227 * the string and click on the text search to locate the packet in question.
228 * So regardless of the type we will just bring up the find window and allow
229 * the user to modify the search criteria and options.
231 find_frame_with_filter(str);
233 case ACTION_FIND_NEXT:
234 /* In the case of find next, if there was no expert item, then most likely the expert
235 * string was modified to locate the text inside the message. So we can't just perform
236 * a find with the expert string or we will not really be performing a find next.
237 * In an effort to allow the user to modify the string and/or continue searching, we
238 * will just present the user with the find window again with the default expert string.
239 * A better aproach would be to attempt in capturing the last find string and utilize this
240 * with a find next/previous. Also a better approach might be to just send a <Ctl-N> keystroke.
242 if (err->procedures[selection].fvalue_value==NULL) {
243 find_frame_with_filter(str);
247 /* We have an expert item so just continue search without find dialog. */
248 find_previous_next_frame_with_filter(str, FALSE);
251 case ACTION_FIND_PREVIOUS:
252 /* In the case of find previous, if there was no expert item, then most likely the expert
253 * string was modified to locate the text inside the message. So we can't just perform
254 * a find with the expert string or we will not really be performing a find previous.
255 * In an effort to allow the user to modify the string and/or continue searching, we
256 * will just present the user with the find window again with the default expert string.
257 * A better aproach would be to attempt in capturing the last find string and utilize this
258 * with a find next/previous. Also a better approach might be to just send a <Ctl-B> keystroke.
260 if (err->procedures[selection].fvalue_value==NULL) {
261 find_frame_with_filter(str);
265 /* We have an expert item so just continue search without find dialog. */
266 find_previous_next_frame_with_filter(str, TRUE);
269 case ACTION_COLORIZE:
270 color_display_with_filter(str);
272 case ACTION_WEB_LOOKUP:
273 /* Lookup expert string on internet. Default search via www.google.com */
274 g_snprintf(str, 255, "http://www.google.com/search?hl=en&q=%s+'%s'", err->procedures[selection].entries[1], err->procedures[selection].entries[2]);
275 browser_open_url(str);
278 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "Can't find menu action - %u", action);
283 error_show_popup_menu_cb(void *widg _U_, GdkEvent *event, error_equiv_table *err)
285 GdkEventButton *bevent = (GdkEventButton *)event;
287 if(event->type==GDK_BUTTON_PRESS && bevent->button==3){
288 gtk_menu_popup(GTK_MENU(err->menu), NULL, NULL, NULL, NULL,
289 bevent->button, bevent->time);
295 static GtkItemFactoryEntry error_list_menu_items[] =
298 {"/Apply as Filter", NULL, NULL, 0, "<Branch>", NULL,},
299 {"/Apply as Filter/Selected", NULL,
300 GTK_MENU_FUNC(error_select_filter_cb), CALLBACK_MATCH(ACTYPE_SELECTED, 0),
302 {"/Apply as Filter/... not Selected", NULL,
303 GTK_MENU_FUNC(error_select_filter_cb), CALLBACK_MATCH(ACTYPE_NOT_SELECTED, 0),
305 {"/Apply as Filter/.. and Selected", NULL,
306 GTK_MENU_FUNC(error_select_filter_cb), CALLBACK_MATCH(ACTYPE_AND_SELECTED, 0),
308 {"/Apply as Filter/... or Selected", NULL,
309 GTK_MENU_FUNC(error_select_filter_cb), CALLBACK_MATCH(ACTYPE_OR_SELECTED, 0),
311 {"/Apply as Filter/... and not Selected", NULL,
312 GTK_MENU_FUNC(error_select_filter_cb), CALLBACK_MATCH(ACTYPE_AND_NOT_SELECTED, 0),
314 {"/Apply as Filter/... or not Selected", NULL,
315 GTK_MENU_FUNC(error_select_filter_cb), CALLBACK_MATCH(ACTYPE_OR_NOT_SELECTED, 0),
319 {"/Prepare a Filter", NULL, NULL, 0, "<Branch>", NULL,},
320 {"/Prepare a Filter/Selected", NULL,
321 GTK_MENU_FUNC(error_select_filter_cb), CALLBACK_PREPARE(ACTYPE_SELECTED, 0),
323 {"/Prepare a Filter/Not Selected", NULL,
324 GTK_MENU_FUNC(error_select_filter_cb), CALLBACK_PREPARE(ACTYPE_NOT_SELECTED, 0),
326 {"/Prepare a Filter/... and Selected", NULL,
327 GTK_MENU_FUNC(error_select_filter_cb), CALLBACK_PREPARE(ACTYPE_AND_SELECTED, 0),
329 {"/Prepare a Filter/... or Selected", NULL,
330 GTK_MENU_FUNC(error_select_filter_cb), CALLBACK_PREPARE(ACTYPE_OR_SELECTED, 0),
332 {"/Prepare a Filter/... and not Selected", NULL,
333 GTK_MENU_FUNC(error_select_filter_cb), CALLBACK_PREPARE(ACTYPE_AND_NOT_SELECTED, 0),
335 {"/Prepare a Filter/... or not Selected", NULL,
336 GTK_MENU_FUNC(error_select_filter_cb), CALLBACK_PREPARE(ACTYPE_OR_NOT_SELECTED, 0),
340 {"/Find Frame", NULL, NULL, 0, "<Branch>", NULL,},
341 {"/Find Frame/Find Frame", NULL, NULL, 0, "<Branch>", NULL,},
342 {"/Find Frame/Find Frame/Selected", NULL,
343 GTK_MENU_FUNC(error_select_filter_cb), CALLBACK_FIND_FRAME(ACTYPE_SELECTED, 0),
345 {"/Find Frame/Find Frame/Not Selected", NULL,
346 GTK_MENU_FUNC(error_select_filter_cb), CALLBACK_FIND_FRAME(ACTYPE_NOT_SELECTED, 0),
349 {"/Find Frame/Find Next", NULL, NULL, 0, "<Branch>", NULL,},
350 {"/Find Frame/Find Next/Selected", NULL,
351 GTK_MENU_FUNC(error_select_filter_cb), CALLBACK_FIND_NEXT(ACTYPE_SELECTED, 0),
353 {"/Find Frame/Find Next/Not Selected", NULL,
354 GTK_MENU_FUNC(error_select_filter_cb), CALLBACK_FIND_NEXT(ACTYPE_NOT_SELECTED, 0),
358 {"/Find Frame/Find Previous", NULL, NULL, 0, "<Branch>", NULL,},
359 {"/Find Frame/Find Previous/Selected", NULL,
360 GTK_MENU_FUNC(error_select_filter_cb), CALLBACK_FIND_PREVIOUS(ACTYPE_SELECTED, 0),
362 {"/Find Frame/Find Previous/Not Selected", NULL,
363 GTK_MENU_FUNC(error_select_filter_cb), CALLBACK_FIND_PREVIOUS(ACTYPE_NOT_SELECTED, 0),
366 /* Colorize Procedure */
367 {"/Colorize Procedure", NULL, NULL, 0, "<Branch>", NULL,},
368 {"/Colorize Procedure/Selected", NULL,
369 GTK_MENU_FUNC(error_select_filter_cb), CALLBACK_COLORIZE(ACTYPE_SELECTED, 0),
371 {"/Colorize Procedure/Not Selected", NULL,
372 GTK_MENU_FUNC(error_select_filter_cb), CALLBACK_COLORIZE(ACTYPE_NOT_SELECTED, 0),
375 /* Search Internet */
376 {"/Internet Search for Info Text", NULL,
377 GTK_MENU_FUNC(error_select_filter_cb), CALLBACK_WEB_LOOKUP, NULL, NULL,}
381 expert_goto_pkt_cb (GtkTreeSelection *selection, gpointer data _U_)
388 if (gtk_tree_selection_get_selected (selection, &model, &iter))
390 gtk_tree_model_get (model, &iter, PROTOCOL_COLUMN, &pkt, -1);
391 gtk_tree_model_get (model, &iter, GROUP_COLUMN, &grp, -1);
393 if (strcmp(grp, "Packet:")==0) {
394 cf_goto_frame(&cfile, atoi(pkt));
402 error_create_popup_menu(error_equiv_table *err)
404 GtkItemFactory *item_factory;
407 err->select = gtk_tree_view_get_selection (GTK_TREE_VIEW (err->tree_view));
408 gtk_tree_selection_set_mode (err->select, GTK_SELECTION_SINGLE);
409 g_signal_connect (G_OBJECT (err->select), "changed",
410 G_CALLBACK (expert_goto_pkt_cb),
412 item_factory = gtk_item_factory_new(GTK_TYPE_MENU, "<main>", NULL);
414 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);
416 err->menu = gtk_item_factory_get_widget(item_factory, "<main>");
417 g_signal_connect(err->tree_view, "button_press_event", G_CALLBACK(error_show_popup_menu_cb), err);
421 init_error_table(error_equiv_table *err, guint16 num_procs, GtkWidget *vbox)
426 GtkTreeViewColumn *column;
427 GtkCellRenderer *renderer;
428 GtkTreeSortable *sortable;
430 /* Create the store */
431 store = gtk_tree_store_new (4, /* Total number of columns */
432 G_TYPE_STRING, /* Group */
433 G_TYPE_STRING, /* Protocol */
434 G_TYPE_STRING, /* Summary */
435 G_TYPE_INT); /* Count */
438 tree = gtk_tree_view_new_with_model (GTK_TREE_MODEL (store));
439 err->tree_view = GTK_TREE_VIEW(tree);
440 sortable = GTK_TREE_SORTABLE(store);
442 /* Setup the sortable columns */
443 gtk_tree_sortable_set_sort_func(sortable, SORT_ALPHABETICAL, sort_iter_compare_func, GINT_TO_POINTER(SORT_ALPHABETICAL), NULL);
444 gtk_tree_sortable_set_sort_column_id(sortable, SORT_ALPHABETICAL, GTK_SORT_ASCENDING);
445 gtk_tree_view_set_headers_clickable(GTK_TREE_VIEW (tree), FALSE);
447 /* The view now holds a reference. We can get rid of our own reference */
448 g_object_unref (G_OBJECT (store));
450 /* Create a cell render */
451 renderer = gtk_cell_renderer_text_new ();
453 /* Create the first column, associating the "text" attribute of the
454 * cell_renderer to the first column of the model */
455 column = gtk_tree_view_column_new_with_attributes ("Group", renderer, "text", GROUP_COLUMN, NULL);
456 gtk_tree_view_column_set_sort_column_id(column, 0);
457 gtk_tree_view_column_set_resizable(column, TRUE);
458 /* Add the column to the view. */
459 gtk_tree_view_append_column (GTK_TREE_VIEW (err->tree_view), column);
461 /* Second column.. Protocol. */
462 renderer = gtk_cell_renderer_text_new ();
463 column = gtk_tree_view_column_new_with_attributes ("Protocol", renderer, "text", PROTOCOL_COLUMN, NULL);
464 gtk_tree_view_column_set_sort_column_id(column, 1);
465 gtk_tree_view_column_set_resizable(column, TRUE);
466 gtk_tree_view_append_column (GTK_TREE_VIEW (err->tree_view), column);
468 /* Third column.. Summary. */
469 renderer = gtk_cell_renderer_text_new ();
470 column = gtk_tree_view_column_new_with_attributes ("Summary", renderer, "text", SUMMARY_COLUMN, NULL);
471 gtk_tree_view_column_set_sort_column_id(column, 2);
472 gtk_tree_view_column_set_resizable(column, TRUE);
473 gtk_tree_view_append_column (GTK_TREE_VIEW (err->tree_view), column);
475 /* Last column.. Count. */
476 column = gtk_tree_view_column_new_with_attributes ("Count", renderer, "text", COUNT_COLUMN, NULL);
477 gtk_tree_view_column_set_sort_column_id(column, 3);
478 gtk_tree_view_column_set_resizable(column, TRUE);
479 gtk_tree_view_append_column (GTK_TREE_VIEW (err->tree_view), column);
481 err->scrolled_window=scrolled_window_new(NULL, NULL);
483 gtk_container_add(GTK_CONTAINER(err->scrolled_window), GTK_WIDGET (err->tree_view));
485 gtk_box_pack_start(GTK_BOX(vbox), err->scrolled_window, TRUE, TRUE, 0);
487 gtk_tree_view_set_search_column (err->tree_view, SUMMARY_COLUMN); /* Allow searching the summary */
488 gtk_tree_view_set_reorderable (err->tree_view, TRUE); /* Allow user to reorder data with drag n drop */
490 /* Now enable the sorting of each column */
491 gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(err->tree_view), TRUE);
492 gtk_tree_view_set_headers_clickable(GTK_TREE_VIEW(err->tree_view), TRUE);
494 gtk_widget_show(err->scrolled_window);
496 err->num_procs=num_procs;
497 err->procedures=g_malloc(sizeof(error_procedure_t)*(num_procs+1));
498 for(i=0;i<num_procs;i++){
500 err->procedures[i].entries[j]=NULL; /* reset all values */
504 /* create popup menu for this table */
505 error_create_popup_menu(err);
509 init_error_table_row(error_equiv_table *err, const expert_info_t *expert_data)
511 guint16 old_num_procs=err->num_procs;
517 /* we have discovered a new procedure. Extend the table accordingly */
518 row = find_summary_data(err, expert_data);
520 /* First time we have seen this event so initialize memory table */
521 row = old_num_procs; /* Number of expert events since this is a new event */
522 err->procedures=g_realloc(err->procedures, (sizeof(error_procedure_t)*(old_num_procs+1)));
523 err->procedures[row].count=0; /* count of events for this item */
524 err->procedures[row].fvalue_value = NULL; /* Filter string value */
526 err->procedures[row].entries[j]=NULL;
529 /* Create the item in our memory table */
530 err->procedures[row].entries[0]=(char *)g_strdup(val_to_str(expert_data->group, expert_group_vals,"Unknown group (%u)")); /* Group */
531 err->procedures[row].entries[1]=(char *)g_strdup(expert_data->protocol); /* Protocol */
532 err->procedures[row].entries[2]=(char *)g_strdup(expert_data->summary); /* Summary */
534 /* Create a new item in our tree view */
535 store = GTK_TREE_STORE(gtk_tree_view_get_model(err->tree_view)); /* Get store */
536 gtk_tree_store_append (store, &err->procedures[row].iter, NULL); /* Acquire an iterator */
538 gtk_tree_store_set (store, &err->procedures[row].iter,
539 GROUP_COLUMN, (char *)g_strdup(val_to_str(expert_data->group, expert_group_vals,"Unknown group (%u)")),
540 PROTOCOL_COLUMN, (char *)g_strdup(expert_data->protocol),
541 SUMMARY_COLUMN, (char *)g_strdup(expert_data->summary), -1);
543 /* If an expert item was passed then build the filter string */
544 if (expert_data->pitem) {
547 filter = proto_construct_match_selected_string(expert_data->pitem->finfo, NULL);
549 err->procedures[row].fvalue_value = g_strdup(filter);
551 /* Store the updated count of events */
552 err->num_procs = ++old_num_procs;
555 /* Update our memory table with event data */
556 err->procedures[row].count++; /* increment the count of events for this item */
558 /* Store the updated count for this event item */
559 err->procedures[row].entries[3]=(char *)g_strdup_printf("%d", err->procedures[row].count); /* Count */
561 /* Update the tree with new count for this event */
562 store = GTK_TREE_STORE(gtk_tree_view_get_model(err->tree_view));
563 gtk_tree_store_set(store, &err->procedures[row].iter, COUNT_COLUMN, err->procedures[row].count, -1);
567 add_error_table_data(error_equiv_table *err, const expert_info_t *expert_data)
569 error_procedure_t *errp;
572 GtkTreeIter new_iter;
574 index = find_summary_data(err,expert_data);
576 /* We should never encounter a condition where we cannot find the expert data. If
577 * we do then we will just abort.
580 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "Could not find expert data. Aborting");
583 errp=&err->procedures[index];
585 store = GTK_TREE_STORE(gtk_tree_view_get_model(err->tree_view));
587 gtk_tree_store_append(store, &new_iter, &errp->iter);
589 gtk_tree_store_set(store, &new_iter,
590 GROUP_COLUMN, "Packet:",
591 PROTOCOL_COLUMN, (char *)g_strdup_printf("%d", expert_data->packet_num),
596 reset_error_table_data(error_equiv_table *err)
601 for(i=0;i<err->num_procs;i++){
602 err->procedures[i].entries[0] = NULL;
603 err->procedures[i].entries[1] = NULL;
604 err->procedures[i].entries[2] = NULL;
605 err->procedures[i].entries[3] = NULL;
606 err->procedures[i].count=0;
609 store = GTK_TREE_STORE(gtk_tree_view_get_model(err->tree_view));
610 gtk_tree_store_clear(store);
615 free_error_table_data(error_equiv_table *err)
619 for(i=0;i<err->num_procs;i++){
621 if(err->procedures[i].entries[j]){
622 err->procedures[i].entries[j]=NULL;
624 err->procedures[i].fvalue_value=NULL;
625 err->procedures[i].count=0;
628 err->procedures=NULL;