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