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