HACK: 1. try to match RowsetProperties
[wireshark-wip.git] / ui / gtk / wsp_stat.c
blob3781fbe0738d9805f0e2432ff132cbd8e846b664
1 /* wsp_stat.c
2 * wsp_stat 2003 Jean-Michel FAYARD
4 * $Id$
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 /* #define DEBUG do{ printf("%s:%d ",__FILE__,__LINE__);} while(0); */
26 #include "config.h"
27 #include <string.h>
29 #include <gtk/gtk.h>
31 #include <epan/packet.h>
32 #include <epan/tap.h>
33 #include <epan/dissectors/packet-wsp.h>
35 #include "ui/simple_dialog.h"
36 #include "../globals.h"
37 #include "../stat_menu.h"
39 #include "ui/gtk/gui_utils.h"
40 #include "ui/gtk/dlg_utils.h"
41 #include "ui/gtk/tap_param_dlg.h"
42 #include "ui/gtk/main.h"
44 #include "ui/gtk/old-gtk-compat.h"
46 /* Used to keep track of the statistics for a specific PDU type */
47 typedef struct _wsp_pdu_t {
48 GtkLabel *widget;
49 guint32 packets;
50 } wsp_pdu_t;
52 /* Used to keep track of the statistics for an entire program interface */
53 typedef struct _wsp_stats_t {
54 char *filter;
55 wsp_pdu_t *pdu_stats;
56 guint32 num_pdus;
57 GtkWidget *win;
58 GHashTable *hash;
59 GtkWidget *grid_pdu_types;
60 GtkWidget *grid_status_code;
61 guint index; /* Number of status codes to display */
62 } wspstat_t;
64 /* Used to keep track of a single type of status code */
65 typedef struct _wsp_status_code_t {
66 const gchar *name;
67 guint32 packets;
68 GtkWidget *widget;/* label in which we print the number of packets */
69 wspstat_t *sp; /* entire program interface */
70 } wsp_status_code_t;
72 static void
73 wsp_reset_hash(gchar *key _U_ , wsp_status_code_t *data, gpointer ptr _U_)
75 data->packets = 0;
78 /* Update the entry corresponding to the number of packets of a special status code
79 * or create it if it doesn't exist.
81 static void
82 wsp_draw_statuscode(gchar *key _U_, wsp_status_code_t *data, gchar *unused _U_)
84 char string_buff[256];
86 if ((data == NULL) || (data->packets == 0))
87 return;
88 if (data->widget == NULL) { /* create an entry in the table */
89 GtkWidget *tmp;
90 int x = 2 * ((data->sp->index) % 2);
91 int y = (data->sp->index) / 2;
94 /* Maybe we should display the hexadecimal value ? */
95 /* g_snprintf(string_buff, sizeof(string_buff), "%s (0X%x)", data->name, *key); */
96 tmp = gtk_label_new(data->name /* string_buff */ );
97 ws_gtk_grid_attach_defaults(GTK_GRID(data->sp->grid_status_code), tmp, x, y, 1, 1);
98 gtk_label_set_justify(GTK_LABEL(tmp), GTK_JUSTIFY_LEFT);
99 gtk_widget_show(tmp);
101 g_snprintf(string_buff, sizeof(string_buff), "%9d", data->packets);
102 data->widget = gtk_label_new(string_buff);
103 ws_gtk_grid_attach_defaults(GTK_GRID(data->sp->grid_status_code), data->widget, x+1, y, 1, 1);
104 gtk_label_set_justify(GTK_LABEL(data->widget), GTK_JUSTIFY_LEFT);
105 gtk_widget_show(data->widget);
107 data->sp->index++;
108 } else {
109 /* Just update the label string */
110 g_snprintf(string_buff, sizeof(string_buff), "%9d", data->packets);
111 gtk_label_set_text(GTK_LABEL(data->widget), string_buff);
114 static void
115 wspstat_reset(void *psp)
117 wspstat_t *sp = (wspstat_t *)psp;
118 guint32 i;
120 for(i=1; i<=sp->num_pdus; i++)
122 sp->pdu_stats[i].packets = 0;
124 g_hash_table_foreach(sp->hash, (GHFunc)wsp_reset_hash, NULL);
127 /* Fixme: !! Magic !! */
128 /* See wsp_vals_pdu_type in packet-wsp.c */
129 static gint
130 pdut2index(gint pdut)
132 if (pdut <= 0x09)
133 return pdut;
134 if (pdut >= 0x40) {
135 if (pdut <= 0x44) {
136 return pdut-54;
137 } else if ((pdut == 0x60) || (pdut == 0x61)) {
138 return pdut-81;
141 return 0;
144 /* Fixme: !! Magic !! */
145 /* See wsp_vals_pdu_type in packet-wsp.c */
146 static gint
147 index2pdut(gint pdut)
149 if (pdut <= 9)
150 return pdut;
151 if (pdut <= 14)
152 return pdut+54;
153 if (pdut <= 16)
154 return pdut+81;
155 return 0;
158 static int
159 wspstat_packet(void *psp, packet_info *pinfo _U_, epan_dissect_t *edt _U_, const void *pri)
161 wspstat_t *sp = (wspstat_t *)psp;
162 const wsp_info_value_t *value = (wsp_info_value_t *)pri;
163 gint idx = pdut2index(value->pdut);
164 gboolean retour = FALSE;
166 if (value->status_code != 0) {
167 wsp_status_code_t *sc;
168 sc = (wsp_status_code_t *)g_hash_table_lookup(
169 sp->hash,
170 GINT_TO_POINTER(value->status_code));
171 if (!sc) {
172 g_warning("%s:%d What's Wrong, doc ?\n", __FILE__, __LINE__);
173 sc = (wsp_status_code_t *)g_malloc(sizeof(wsp_status_code_t));
174 sc -> packets = 1;
175 sc -> name = NULL;
176 sc -> widget = NULL;
177 sc -> sp = sp;
178 g_hash_table_insert(
179 sp->hash,
180 GINT_TO_POINTER(value->status_code),
181 sc);
182 } else {
183 sc->packets++;
185 retour = TRUE;
190 if (idx != 0) {
191 sp->pdu_stats[idx].packets++;
192 retour = TRUE;
194 return retour;
199 static void
200 wspstat_draw(void *psp)
202 wspstat_t *sp = (wspstat_t *)psp;
203 guint32 i;
204 char str[256];
206 for(i=1; i<=sp->num_pdus; i++)
208 g_snprintf(str, sizeof(str), "%9d", sp->pdu_stats[i].packets);
209 gtk_label_set_text(GTK_LABEL(sp->pdu_stats[i].widget), str);
212 g_hash_table_foreach(sp->hash, (GHFunc)wsp_draw_statuscode, NULL);
215 static void
216 win_destroy_cb(GtkWindow *win _U_, gpointer data)
218 wspstat_t *sp = (wspstat_t *)data;
220 remove_tap_listener(sp);
222 g_free(sp->pdu_stats);
223 g_free(sp->filter);
224 g_hash_table_destroy(sp->hash);
225 g_free(sp);
228 static void
229 add_table_entry(wspstat_t *sp, const char *str, int x, int y, int idx)
231 GtkWidget *tmp;
233 tmp = gtk_label_new(str);
234 ws_gtk_grid_attach_defaults(GTK_GRID(sp->grid_pdu_types), tmp, x, y, 1, 1);
235 gtk_label_set_justify(GTK_LABEL(tmp), GTK_JUSTIFY_LEFT);
236 gtk_widget_show(tmp);
237 if (idx != 0) {
238 sp->pdu_stats[idx].widget = GTK_LABEL(tmp);
243 static void
244 wsp_init_table(wspstat_t *sp)
246 int pos = 0;
247 guint32 i;
248 /* gchar buffer[51]; */
250 add_table_entry(sp, "PDU Type ", 0, pos, 0);
251 add_table_entry(sp, "packets " , 1, pos, 0);
252 add_table_entry(sp, "PDU Type ", 2, pos, 0);
253 add_table_entry(sp, "packets " , 3, pos, 0);
254 pos++;
255 for (i=1; i<=sp->num_pdus ;i++)
257 int x = 0;
258 if (i > (sp->num_pdus+1) / 2) {
259 x = 2;
261 /* Maybe we should display the hexadecimal value ? */
262 #if 0
263 g_snprintf(buffer, sizeof(buffer), "%s (0X%x)",
264 try_val_to_str_ext(index2pdut(i), &wsp_vals_pdu_type_ext), index2pdut(i));
265 #endif
266 add_table_entry(sp,
267 try_val_to_str_ext(index2pdut(i), &wsp_vals_pdu_type_ext), /* or buffer, */
269 pos,
272 add_table_entry(sp,
273 "0",
274 x+1,
275 pos,
276 i /* keep a pointer to this widget to update it in _draw() */
278 pos++;
279 if (i == (sp->num_pdus+1) / 2) {
280 pos = 1;
285 /* When called, this function will create a new instance of gtk2-wspstat.
287 static void
288 gtk_wspstat_init(const char *opt_arg, void *userdata _U_)
290 wspstat_t *sp;
291 const char *filter;
292 char *title;
293 GString *error_string;
294 GtkWidget *main_vb, *pdutypes_fr, *statuscode_fr ;
295 GtkWidget *bt_close;
296 GtkWidget *bbox;
297 guint32 i;
298 wsp_status_code_t *sc;
299 const value_string *wsp_vals_status_p;
301 if (strncmp(opt_arg, "wsp,stat,", 9) == 0) {
302 filter = opt_arg+9;
303 } else {
304 filter = NULL;
307 sp = (wspstat_t *)g_malloc(sizeof(wspstat_t));
308 sp->win = dlg_window_new("wsp-stat"); /* transient_for top_level */
309 gtk_window_set_destroy_with_parent(GTK_WINDOW(sp->win), TRUE);
311 sp->hash = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, g_free);
312 wsp_vals_status_p = VALUE_STRING_EXT_VS_P(&wsp_vals_status_ext);
313 for (i=0; wsp_vals_status_p[i].strptr; i++)
315 sc = (wsp_status_code_t *)g_malloc(sizeof(wsp_status_code_t));
316 sc->name = wsp_vals_status_p[i].strptr;
317 sc->packets = 0;
318 sc->widget = NULL;
319 sc->sp = sp;
320 g_hash_table_insert(
321 sp->hash,
322 GINT_TO_POINTER(wsp_vals_status_p[i].value),
323 sc);
325 sp->num_pdus = 16;
326 sp->pdu_stats = (wsp_pdu_t *)g_malloc((sp->num_pdus+1) * sizeof(wsp_pdu_t));
327 if (filter) {
328 sp->filter = g_strdup(filter);
329 title = g_strdup_printf("Wireshark: WAP-WSP statistics with filter: %s", filter);
330 } else {
331 sp->filter = NULL;
332 title = g_strdup("Wireshark: WAP-WSP statistics");
334 for (i=0; i<=sp->num_pdus; i++)
336 sp->pdu_stats[i].packets = 0;
339 gtk_window_set_title(GTK_WINDOW(sp->win), title);
340 g_free(title);
342 /* container for the two frames */
343 main_vb = ws_gtk_box_new(GTK_ORIENTATION_VERTICAL, 3, FALSE);
344 gtk_container_set_border_width(GTK_CONTAINER(main_vb), 12);
345 gtk_container_add(GTK_CONTAINER(sp->win), main_vb);
347 /* PDU Types frame */
348 pdutypes_fr = gtk_frame_new("Summary of PDU Types (wsp.pdu_type)");
349 gtk_box_pack_start(GTK_BOX(main_vb), pdutypes_fr, TRUE, TRUE, 0);
351 sp->grid_pdu_types = ws_gtk_grid_new();
352 gtk_container_add(GTK_CONTAINER(pdutypes_fr), sp->grid_pdu_types);
353 gtk_container_set_border_width(GTK_CONTAINER(sp->grid_pdu_types), 10);
355 wsp_init_table(sp);
357 /* Status Codes frame */
358 statuscode_fr = gtk_frame_new("Summary of Status Code (wsp.reply.status)");
359 gtk_box_pack_start(GTK_BOX(main_vb), statuscode_fr, FALSE, FALSE, 0);
361 sp->grid_status_code = ws_gtk_grid_new();
362 gtk_container_add(GTK_CONTAINER(statuscode_fr), sp->grid_status_code);
363 gtk_container_set_border_width(GTK_CONTAINER(sp->grid_status_code), 10);
364 sp->index = 0; /* No answers to display yet */
366 error_string = register_tap_listener(
367 "wsp",
369 filter,
371 wspstat_reset,
372 wspstat_packet,
373 wspstat_draw);
374 if (error_string) {
375 /* error, we failed to attach to the tap. clean up */
376 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s", error_string->str);
377 g_free(sp->pdu_stats);
378 g_free(sp->filter);
379 g_free(sp);
380 g_string_free(error_string, TRUE);
381 return ;
384 /* Button row. */
385 bbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
386 gtk_box_pack_start(GTK_BOX(main_vb), bbox, FALSE, FALSE, 0);
388 bt_close = (GtkWidget *)g_object_get_data(G_OBJECT(bbox), GTK_STOCK_CLOSE);
389 window_set_cancel_button(sp->win, bt_close, window_cancel_button_cb);
391 g_signal_connect(sp->win, "delete_event", G_CALLBACK(window_delete_event_cb), NULL);
392 g_signal_connect(sp->win, "destroy", G_CALLBACK(win_destroy_cb), sp);
394 gtk_widget_show_all(sp->win);
395 window_present(sp->win);
397 cf_retap_packets(&cfile);
398 gdk_window_raise(gtk_widget_get_window(sp->win));
401 static tap_param wsp_stat_params[] = {
402 { PARAM_FILTER, "Filter", NULL }
405 static tap_param_dlg wsp_stat_dlg = {
406 "WAP-WSP Packet Counter",
407 "wsp,stat",
408 gtk_wspstat_init,
410 G_N_ELEMENTS(wsp_stat_params),
411 wsp_stat_params
414 void
415 register_tap_listener_gtkwspstat(void)
417 register_param_stat(&wsp_stat_dlg, "_WAP-WSP",
418 REGISTER_STAT_GROUP_TELEPHONY);