Update the information about getting Xcode, and note that
[obnox/wireshark/wip.git] / gtk / radius_stat.c
1 /* radius_stat.c
2  * radius-statistics for Wireshark
3  * Copyright 2006 Alejandro Vaquero <alejandrovaquero@yahoo.com>
4  *
5  * $Id$
6  *
7  * Wireshark - Network traffic analyzer
8  * By Gerald Combs <gerald@wireshark.org>
9  * Copyright 1998 Gerald Combs
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #ifdef HAVE_SYS_TYPES_H
31 # include <sys/types.h>
32 #endif
33
34 #include <string.h>
35
36 #include <gtk/gtk.h>
37
38 #include <epan/packet_info.h>
39 #include <epan/epan.h>
40 #include <epan/value_string.h>
41 #include <epan/tap.h>
42 #include <epan/dissectors/packet-radius.h>
43
44 #include "../timestats.h"
45 #include "../simple_dialog.h"
46 #include "../file.h"
47 #include "../stat_menu.h"
48
49 #include "gtk/gui_stat_util.h"
50 #include "gtk/dlg_utils.h"
51 #include "gtk/tap_param_dlg.h"
52 #include "gtk/gui_utils.h"
53 #include "gtk/main.h"
54
55 #include "gtk/old-gtk-compat.h"
56
57 #define NUM_TIMESTATS 8
58 #define NUM_COLUMNS 11
59
60 /* Summary of response-time calculations*/
61 typedef struct _radius_rtd_t {
62         guint32 open_req_num;
63         guint32 disc_rsp_num;
64         guint32 req_dup_num;
65         guint32 rsp_dup_num;
66         timestat_t stats;
67 } radius_rtd_t;
68
69 /* used to keep track of the statistics for an entire program interface */
70 typedef struct _radiusstat_t {
71         GtkWidget *win;
72         GtkWidget *vbox;
73         char *filter;
74         GtkWidget *scrolled_window;
75         GtkTreeView *table;
76         radius_rtd_t radius_rtd[NUM_TIMESTATS];
77 } radiusstat_t;
78
79 static const value_string radius_message_code[] = {
80   {  0, "Overall"},
81   {  1, "Access"},
82   {  2, "Accounting"},
83   {  3, "Access Password"},
84   {  4, "Ascend Access Event"},
85   {  5, "Disconnect"},
86   {  6, "Change Filter"},
87   {  7, "Other"},
88   {  0, NULL}
89 };
90
91 typedef enum _radius_category {
92         OVERALL,
93         ACCESS,
94         ACCOUNTING,
95         ACCESS_PASSWORD,
96         ASCEND_ACCESS_EVENT,
97         DISCONNECT,
98         CHANGE_FILTER,
99         OTHERS
100 }radius_category;
101
102 static void
103 radiusstat_reset(void *prs)
104 {
105         radiusstat_t *rs=(radiusstat_t *)prs;
106         int i;
107
108
109         for(i=0;i<NUM_TIMESTATS;i++) {
110                 rs->radius_rtd[i].stats.num=0;
111                 rs->radius_rtd[i].stats.min_num=0;
112                 rs->radius_rtd[i].stats.max_num=0;
113                 rs->radius_rtd[i].stats.min.secs=0;
114         rs->radius_rtd[i].stats.min.nsecs=0;
115         rs->radius_rtd[i].stats.max.secs=0;
116         rs->radius_rtd[i].stats.max.nsecs=0;
117         rs->radius_rtd[i].stats.tot.secs=0;
118         rs->radius_rtd[i].stats.tot.nsecs=0;
119                 rs->radius_rtd[i].open_req_num = 0;
120                 rs->radius_rtd[i].disc_rsp_num = 0;
121                 rs->radius_rtd[i].req_dup_num = 0;
122                 rs->radius_rtd[i].rsp_dup_num = 0;
123         }
124
125 }
126
127
128 static int
129 radiusstat_packet(void *prs, packet_info *pinfo, epan_dissect_t *edt _U_, const void *pri)
130 {
131         radiusstat_t *rs=(radiusstat_t *)prs;
132         const radius_info_t *ri=pri;
133         nstime_t delta;
134         radius_category radius_cat = OTHERS;
135         int ret = 0;
136
137         switch (ri->code) {
138                 case RADIUS_ACCESS_REQUEST:
139                 case RADIUS_ACCESS_ACCEPT:
140                 case RADIUS_ACCESS_REJECT:
141                         radius_cat = ACCESS;
142                         break;
143                 case RADIUS_ACCOUNTING_REQUEST:
144                 case RADIUS_ACCOUNTING_RESPONSE:
145                         radius_cat = ACCOUNTING;
146                         break;
147                 case RADIUS_ACCESS_PASSWORD_REQUEST:
148                 case RADIUS_ACCESS_PASSWORD_ACK:
149                 case RADIUS_ACCESS_PASSWORD_REJECT:
150                         radius_cat = ACCESS_PASSWORD;
151                         break;
152                 case RADIUS_ASCEND_ACCESS_EVENT_REQUEST:
153                 case RADIUS_ASCEND_ACCESS_EVENT_RESPONSE:
154                         radius_cat = ASCEND_ACCESS_EVENT;
155                         break;
156                 case RADIUS_DISCONNECT_REQUEST:
157                 case RADIUS_DISCONNECT_REQUEST_ACK:
158                 case RADIUS_DISCONNECT_REQUEST_NAK:
159                         radius_cat = DISCONNECT;
160                         break;
161                 case RADIUS_CHANGE_FILTER_REQUEST:
162                 case RADIUS_CHANGE_FILTER_REQUEST_ACK:
163                 case RADIUS_CHANGE_FILTER_REQUEST_NAK:
164                         radius_cat = CHANGE_FILTER;
165                         break;
166         }
167
168         switch (ri->code) {
169
170         case RADIUS_ACCESS_REQUEST:
171         case RADIUS_ACCOUNTING_REQUEST:
172         case RADIUS_ACCESS_PASSWORD_REQUEST:
173         case RADIUS_ASCEND_ACCESS_EVENT_REQUEST:
174         case RADIUS_DISCONNECT_REQUEST:
175         case RADIUS_CHANGE_FILTER_REQUEST:
176                 if(ri->is_duplicate){
177                         /* Duplicate is ignored */
178                         rs->radius_rtd[OVERALL].req_dup_num++;
179                         rs->radius_rtd[radius_cat].req_dup_num++;
180                 }
181                 else {
182                         rs->radius_rtd[OVERALL].open_req_num++;
183                         rs->radius_rtd[radius_cat].open_req_num++;
184                 }
185                 break;
186
187         case RADIUS_ACCESS_ACCEPT:
188         case RADIUS_ACCESS_REJECT:
189         case RADIUS_ACCOUNTING_RESPONSE:
190         case RADIUS_ACCESS_PASSWORD_ACK:
191         case RADIUS_ACCESS_PASSWORD_REJECT:
192         case RADIUS_ASCEND_ACCESS_EVENT_RESPONSE:
193         case RADIUS_DISCONNECT_REQUEST_ACK:
194         case RADIUS_DISCONNECT_REQUEST_NAK:
195         case RADIUS_CHANGE_FILTER_REQUEST_ACK:
196         case RADIUS_CHANGE_FILTER_REQUEST_NAK:
197                 if(ri->is_duplicate){
198                         /* Duplicate is ignored */
199                         rs->radius_rtd[OVERALL].rsp_dup_num++;
200                         rs->radius_rtd[radius_cat].rsp_dup_num++;
201                 }
202                 else if (!ri->request_available) {
203                         /* no request was seen */
204                         rs->radius_rtd[OVERALL].disc_rsp_num++;
205                         rs->radius_rtd[radius_cat].disc_rsp_num++;
206                 }
207                 else {
208                         rs->radius_rtd[OVERALL].open_req_num--;
209                         rs->radius_rtd[radius_cat].open_req_num--;
210                         /* calculate time delta between request and response */
211                         nstime_delta(&delta, &pinfo->fd->abs_ts, &ri->req_time);
212
213                         time_stat_update(&(rs->radius_rtd[OVERALL].stats),&delta, pinfo);
214                         time_stat_update(&(rs->radius_rtd[radius_cat].stats),&delta, pinfo);
215
216                         ret = 1;
217                 }
218                 break;
219
220         default:
221                 break;
222         }
223
224         return ret;
225 }
226
227 static void
228 radiusstat_draw(void *prs)
229 {
230         radiusstat_t *rs=(radiusstat_t *)prs;
231         int i;
232         char str[5][256];
233         GtkListStore *store;
234         GtkTreeIter iter;
235
236         /* clear list before printing */
237         store = GTK_LIST_STORE(gtk_tree_view_get_model(rs->table));
238         gtk_list_store_clear(store);
239
240         for(i=0;i<NUM_TIMESTATS;i++) {
241                 /* nothing seen, nothing to do */
242                 if(rs->radius_rtd[i].stats.num==0){
243                         continue;
244                 }
245                 g_snprintf(str[0], 256, "%8.2f msec", nstime_to_msec(&(rs->radius_rtd[i].stats.min)));
246                 g_snprintf(str[1], 256, "%8.2f msec", nstime_to_msec(&(rs->radius_rtd[i].stats.max)));
247                 g_snprintf(str[2], 256, "%8.2f msec", get_average(&(rs->radius_rtd[i].stats.tot), rs->radius_rtd[i].stats.num));
248                 g_snprintf(str[3], 256, "%4u (%4.2f%%)", rs->radius_rtd[i].req_dup_num,
249                         rs->radius_rtd[i].stats.num?((double)rs->radius_rtd[i].req_dup_num*100)/(double)rs->radius_rtd[i].stats.num:0);
250                 g_snprintf(str[4], 256, "%4u (%4.2f%%)", rs->radius_rtd[i].rsp_dup_num,
251                         rs->radius_rtd[i].stats.num?((double)rs->radius_rtd[i].rsp_dup_num*100)/(double)rs->radius_rtd[i].stats.num:0);
252
253                 gtk_list_store_append(store, &iter);
254                 gtk_list_store_set(store, &iter,
255                         0, val_to_str(i, radius_message_code,"Other"),
256                         1, rs->radius_rtd[i].stats.num,
257                         2, str[0],
258                         3, str[1],
259                         4, str[2],
260                         5, rs->radius_rtd[i].stats.min_num,
261                         6, rs->radius_rtd[i].stats.max_num,
262                         7, rs->radius_rtd[i].open_req_num,
263                         8, rs->radius_rtd[i].disc_rsp_num,
264                         9, str[3],
265                         10, str[4],
266                         -1);
267         }
268 }
269
270 static void
271 win_destroy_cb(GtkWindow *win _U_, gpointer data)
272 {
273         radiusstat_t *rs=(radiusstat_t *)data;
274
275         protect_thread_critical_region();
276         remove_tap_listener(rs);
277         unprotect_thread_critical_region();
278
279         if(rs->filter){
280                 g_free(rs->filter);
281                 rs->filter=NULL;
282         }
283         g_free(rs);
284 }
285
286 static const stat_column titles[]={
287         {G_TYPE_STRING, LEFT,  "Type" },
288         {G_TYPE_UINT, RIGHT,   "Messages" },
289         {G_TYPE_STRING, RIGHT, "Min SRT" },
290         {G_TYPE_STRING, RIGHT, "Max SRT" },
291         {G_TYPE_STRING, RIGHT, "Avg SRT" },
292         {G_TYPE_UINT, RIGHT,   "Min in Frame" },
293         {G_TYPE_UINT, RIGHT,   "Max in Frame" },
294         {G_TYPE_UINT, RIGHT,   "Open Requests" },
295         {G_TYPE_UINT, RIGHT,   "Discarded Responses" },
296         {G_TYPE_STRING, RIGHT, "Repeated Requests" },
297         {G_TYPE_STRING, RIGHT, "Repeated Responses"}
298 };
299
300 static void
301 gtk_radiusstat_init(const char *optarg, void *userdata _U_)
302 {
303         radiusstat_t *rs;
304         GString *error_string;
305         GtkWidget *bt_close;
306         GtkWidget *bbox;
307
308         rs=g_malloc(sizeof(radiusstat_t));
309
310         if(strncmp(optarg,"radius,srt,",11) == 0){
311                 rs->filter=g_strdup(optarg+11);
312         } else {
313                 rs->filter=NULL;
314         }
315
316         radiusstat_reset(rs);
317
318         rs->win = dlg_window_new("RADIUS SRT");  /* transient_for top_level */
319         gtk_window_set_destroy_with_parent (GTK_WINDOW(rs->win), TRUE);
320         gtk_window_set_default_size(GTK_WINDOW(rs->win), 600, 150);
321
322         rs->vbox=gtk_vbox_new(FALSE, 3);
323
324         init_main_stat_window(rs->win, rs->vbox, "RADIUS Service Response Time (SRT) Statistics", rs->filter);
325
326         /* init a scrolled window*/
327         rs->scrolled_window = scrolled_window_new(NULL, NULL);
328
329         rs->table = create_stat_table(rs->scrolled_window, rs->vbox, NUM_COLUMNS, titles);
330
331         error_string=register_tap_listener("radius", rs, rs->filter, 0, radiusstat_reset, radiusstat_packet, radiusstat_draw);
332         if(error_string){
333                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s", error_string->str);
334                 g_string_free(error_string, TRUE);
335                 g_free(rs->filter);
336                 g_free(rs);
337                 return;
338         }
339
340         /* Button row. */
341         bbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
342         gtk_box_pack_start(GTK_BOX(rs->vbox), bbox, FALSE, FALSE, 0);
343
344         bt_close = g_object_get_data(G_OBJECT(bbox), GTK_STOCK_CLOSE);
345         window_set_cancel_button(rs->win, bt_close, window_cancel_button_cb);
346
347         g_signal_connect(rs->win, "delete_event", G_CALLBACK(window_delete_event_cb), NULL);
348         g_signal_connect(rs->win, "destroy", G_CALLBACK(win_destroy_cb), rs);
349
350         gtk_widget_show_all(rs->win);
351         window_present(rs->win);
352
353         cf_retap_packets(&cfile);
354         gdk_window_raise(gtk_widget_get_window(rs->win));
355 }
356
357 static tap_param radius_stat_params[] = {
358         { PARAM_FILTER, "Filter", NULL }
359 };
360
361 static tap_param_dlg radius_srt_dlg = {
362         "RADIUS Service Response Time (SRT) Statistics",
363         "radius,srt",
364         gtk_radiusstat_init,
365         -1,
366         G_N_ELEMENTS(radius_stat_params),
367         radius_stat_params
368 };
369
370 void
371 register_tap_listener_gtkradiusstat(void)
372 {
373         register_dfilter_stat(&radius_srt_dlg, "RADIUS",
374                     REGISTER_STAT_GROUP_RESPONSE_TIME);
375 }
376 #ifdef MAIN_MENU_USE_UIMANAGER
377 void radius_srt_cb(GtkAction *action, gpointer user_data _U_)
378 {
379         tap_param_dlg_cb(action, &radius_srt_dlg);
380 }
381 #endif