dd9b8236ac88edae1a87fe734f6bc2f9c0c75952
[obnox/wireshark/wip.git] / gtk / fc_stat.c
1 /* fc_stat.c
2  * fc_stat   2003 Ronnie Sahlberg
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
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.
14  *
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.
19  *
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.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #ifdef HAVE_SYS_TYPES_H
30 # include <sys/types.h>
31 #endif
32
33 #include <string.h>
34
35 #include <gtk/gtk.h>
36
37 #include <epan/packet_info.h>
38 #include <epan/epan.h>
39 #include <epan/value_string.h>
40 #include <epan/tap.h>
41 #include <epan/conversation.h>
42 #include <epan/dissectors/packet-scsi.h>
43 #include <epan/dissectors/packet-fc.h>
44
45 #include "../timestats.h"
46 #include "../simple_dialog.h"
47 #include "../file.h"
48 #include "../stat_menu.h"
49
50 #include "gtk/gui_utils.h"
51 #include "gtk/dlg_utils.h"
52 #include "gtk/filter_dlg.h"
53 #include "gtk/service_response_time_table.h"
54 #include "gtk/tap_dfilter_dlg.h"
55 #include "gtk/gtkglobals.h"
56 #include "gtk/main.h"
57
58
59 /* used to keep track of the statistics for an entire program interface */
60 typedef struct _fcstat_t {
61         GtkWidget *win;
62         srt_stat_table fc_srt_table;
63 } fcstat_t;
64
65 static void
66 fcstat_set_title(fcstat_t *fc)
67 {
68         char            *title;
69
70         title = g_strdup_printf("Fibre Channel Service Response Time statistics: %s",
71             cf_get_display_name(&cfile));
72         gtk_window_set_title(GTK_WINDOW(fc->win), title);
73         g_free(title);
74 }
75
76 static void
77 fcstat_reset(void *pfc)
78 {
79         fcstat_t *fc=(fcstat_t *)pfc;
80
81         reset_srt_table_data(&fc->fc_srt_table);
82         fcstat_set_title(fc);
83 }
84
85 static int
86 fcstat_packet(void *pfc, packet_info *pinfo, epan_dissect_t *edt _U_, const void *psi)
87 {
88         const fc_hdr *fc=psi;
89         fcstat_t *fs=(fcstat_t *)pfc;
90
91         /* we are only interested in reply packets */
92         if(!(fc->fctl&FC_FCTL_EXCHANGE_RESPONDER)){
93                 return 0;
94         }
95         /* if we havnt seen the request, just ignore it */
96         if( (!fc->itlq) || (fc->itlq->first_exchange_frame==0) ){
97                 return 0;
98         }
99
100         add_srt_table_data(&fs->fc_srt_table, fc->type, &fc->itlq->fc_time, pinfo);
101
102         return 1;
103 }
104
105
106
107 static void
108 fcstat_draw(void *pfc)
109 {
110         fcstat_t *fc=(fcstat_t *)pfc;
111
112         draw_srt_table_data(&fc->fc_srt_table);
113 }
114
115
116 static void
117 win_destroy_cb(GtkWindow *win _U_, gpointer data)
118 {
119         fcstat_t *fc=(fcstat_t *)data;
120
121         protect_thread_critical_region();
122         remove_tap_listener(fc);
123         unprotect_thread_critical_region();
124
125         free_srt_table_data(&fc->fc_srt_table);
126         g_free(fc);
127 }
128
129
130 static void
131 gtk_fcstat_init(const char *optarg, void *userdata _U_)
132 {
133         fcstat_t *fc;
134         const char *filter=NULL;
135         GtkWidget *label;
136         char *filter_string;
137         GString *error_string;
138         int i;
139         GtkWidget *vbox;
140         GtkWidget *bbox;
141         GtkWidget *close_bt;
142
143         if(!strncmp(optarg,"fc,srt,",7)){
144                 filter=optarg+7;
145         } else {
146                 filter=NULL;
147         }
148
149         fc=g_malloc(sizeof(fcstat_t));
150
151         fc->win = dlg_window_new("fc-stat");  /* transient_for top_level */
152         gtk_window_set_destroy_with_parent (GTK_WINDOW(fc->win), TRUE);
153
154         gtk_window_set_default_size(GTK_WINDOW(fc->win), 550, 400);
155         fcstat_set_title(fc);
156
157         vbox=gtk_vbox_new(FALSE, 3);
158         gtk_container_add(GTK_CONTAINER(fc->win), vbox);
159         gtk_container_set_border_width(GTK_CONTAINER(vbox), 12);
160
161         label=gtk_label_new("Fibre Channel Service Response Time statistics");
162         gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
163
164         filter_string = g_strdup_printf("Filter: %s", filter ? filter : "");
165         label=gtk_label_new(filter_string);
166         g_free(filter_string);
167         gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
168         gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
169
170         label=gtk_label_new("Fibre Channel Types");
171         gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
172
173         /* We must display TOP LEVEL Widget before calling init_srt_table() */
174         gtk_widget_show_all(fc->win);
175
176         init_srt_table(&fc->fc_srt_table, 256, vbox, NULL);
177         for(i=0;i<256;i++){
178                 init_srt_table_row(&fc->fc_srt_table, i, val_to_str(i, fc_fc4_val, "Unknown(0x%02x)"));
179         }
180
181
182         error_string=register_tap_listener("fc", fc, filter, 0, fcstat_reset, fcstat_packet, fcstat_draw);
183         if(error_string){
184                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s", error_string->str);
185                 g_string_free(error_string, TRUE);
186                 g_free(fc);
187                 return;
188         }
189
190         /* Button row. */
191         bbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
192         gtk_box_pack_end(GTK_BOX(vbox), bbox, FALSE, FALSE, 0);
193
194         close_bt = g_object_get_data(G_OBJECT(bbox), GTK_STOCK_CLOSE);
195         window_set_cancel_button(fc->win, close_bt, window_cancel_button_cb);
196
197         g_signal_connect(fc->win, "delete_event", G_CALLBACK(window_delete_event_cb), NULL);
198         g_signal_connect(fc->win, "destroy", G_CALLBACK(win_destroy_cb), fc);
199
200         gtk_widget_show_all(fc->win);
201         window_present(fc->win);
202
203         cf_retap_packets(&cfile);
204         gdk_window_raise(fc->win->window);
205 }
206
207 static tap_param fc_stat_params[] = {
208         { PARAM_FILTER, "Filter", NULL }
209 };
210
211 static tap_param_dlg fc_stat_dlg = {
212         "Fibre Channel Service Response Time statistics",
213         "fc,srt",
214         gtk_fcstat_init,
215         -1,
216         G_N_ELEMENTS(fc_stat_params),
217         fc_stat_params
218 };
219
220 void
221 register_tap_listener_gtkfcstat(void)
222 {
223         register_dfilter_stat(&fc_stat_dlg, "Fibre Channel",
224             REGISTER_STAT_GROUP_RESPONSE_TIME);
225 }