Use value_string_ext fcns to access certain value_string arrays.
[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 #include <string.h>
33
34 #include <gtk/gtk.h>
35
36 #include <epan/packet_info.h>
37 #include <epan/epan.h>
38 #include <epan/value_string.h>
39 #include <epan/tap.h>
40 #include <epan/dissectors/packet-smb2.h>
41
42 #include "../timestats.h"
43 #include "../simple_dialog.h"
44 #include "../file.h"
45 #include "../globals.h"
46 #include "../stat_menu.h"
47
48 #include "gtk/gui_utils.h"
49 #include "gtk/dlg_utils.h"
50 #include "gtk/filter_dlg.h"
51 #include "gtk/service_response_time_table.h"
52 #include "gtk/tap_dfilter_dlg.h"
53 #include "gtk/gtkglobals.h"
54 #include "gtk/main.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 static void
115 win_destroy_cb(GtkWindow *win _U_, gpointer data)
116 {
117         smb2stat_t *ss=(smb2stat_t *)data;
118
119         protect_thread_critical_region();
120         remove_tap_listener(ss);
121         unprotect_thread_critical_region();
122
123         free_srt_table_data(&ss->smb2_srt_table);
124         g_free(ss);
125 }
126
127
128 static void
129 gtk_smb2stat_init(const char *optarg, void *userdata _U_)
130 {
131         smb2stat_t *ss;
132         const char *filter=NULL;
133         GtkWidget *label;
134         char *filter_string;
135         GString *error_string;
136         int i;
137         GtkWidget *vbox;
138         GtkWidget *bbox;
139         GtkWidget *close_bt;
140
141         if(!strncmp(optarg,"smb2,srt,",9)){
142                 filter=optarg+9;
143         } else {
144                 filter=NULL;
145         }
146
147         ss=g_malloc(sizeof(smb2stat_t));
148
149         ss->win = dlg_window_new("smb2-stat");  /* transient_for top_level */
150         gtk_window_set_destroy_with_parent (GTK_WINDOW(ss->win), TRUE);
151         gtk_window_set_default_size(GTK_WINDOW(ss->win), 550, 400);
152         smb2stat_set_title(ss);
153
154         vbox=gtk_vbox_new(FALSE, 3);
155         gtk_container_add(GTK_CONTAINER(ss->win), vbox);
156         gtk_container_set_border_width(GTK_CONTAINER(vbox), 12);
157
158         label=gtk_label_new("SMB2 Service Response Time statistics");
159         gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
160
161         filter_string = g_strdup_printf("Filter: %s", filter ? filter : "");
162         label=gtk_label_new(filter_string);
163         g_free(filter_string);
164         gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
165         gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
166
167         label=gtk_label_new("SMB2 Commands");
168         gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
169
170         /* We must display TOP LEVEL Widget before calling init_srt_table() */
171         gtk_widget_show_all(ss->win);
172
173         init_srt_table(&ss->smb2_srt_table, 256, vbox, "smb2.cmd");
174         for(i=0;i<256;i++){
175                 init_srt_table_row(&ss->smb2_srt_table, i, val_to_str_ext(i, &smb2_cmd_vals_ext, "Unknown(0x%02x)"));
176         }
177
178
179         error_string=register_tap_listener("smb2", ss, filter, 0, smb2stat_reset, smb2stat_packet, smb2stat_draw);
180         if(error_string){
181                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s", error_string->str);
182                 g_string_free(error_string, TRUE);
183                 g_free(ss);
184                 return;
185         }
186
187         /* Button row. */
188         bbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
189         gtk_box_pack_end(GTK_BOX(vbox), bbox, FALSE, FALSE, 0);
190
191         close_bt = g_object_get_data(G_OBJECT(bbox), GTK_STOCK_CLOSE);
192         window_set_cancel_button(ss->win, close_bt, window_cancel_button_cb);
193
194         g_signal_connect(ss->win, "delete_event", G_CALLBACK(window_delete_event_cb), NULL);
195         g_signal_connect(ss->win, "destroy", G_CALLBACK(win_destroy_cb), ss);
196
197         gtk_widget_show_all(ss->win);
198         window_present(ss->win);
199
200         cf_retap_packets(&cfile);
201         gdk_window_raise(ss->win->window);
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 }