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