Rename the tap_dfilter_dlg.[ch] files to reflect that they can do more
[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
56 #define NUM_TIMESTATS 8
57 #define NUM_COLUMNS 11
58
59 /* Summary of response-time calculations*/
60 typedef struct _radius_rtd_t {
61         guint32 open_req_num;
62         guint32 disc_rsp_num;
63         guint32 req_dup_num;
64         guint32 rsp_dup_num;
65         timestat_t stats;
66 } radius_rtd_t;
67
68 /* used to keep track of the statistics for an entire program interface */
69 typedef struct _radiusstat_t {
70         GtkWidget *win;
71         GtkWidget *vbox;
72         char *filter;
73         GtkWidget *scrolled_window;
74         GtkTreeView *table;
75         radius_rtd_t radius_rtd[NUM_TIMESTATS];
76 } radiusstat_t;
77
78 static const value_string radius_message_code[] = {
79   {  0, "Overall"},
80   {  1, "Access"},
81   {  2, "Accounting"},
82   {  3, "Access Password"},
83   {  4, "Ascend Access Event"},
84   {  5, "Disconnect"},
85   {  6, "Change Filter"},
86   {  7, "Other"},
87   {  0, NULL}
88 };
89
90 typedef enum _radius_category {
91         OVERALL,
92         ACCESS,
93         ACCOUNTING,
94         ACCESS_PASSWORD,
95         ASCEND_ACCESS_EVENT,
96         DISCONNECT,
97         CHANGE_FILTER,
98         OTHERS
99 }radius_category;
100
101 static void
102 radiusstat_reset(void *prs)
103 {
104         radiusstat_t *rs=(radiusstat_t *)prs;
105         int i;
106
107
108         for(i=0;i<NUM_TIMESTATS;i++) {
109                 rs->radius_rtd[i].stats.num=0;
110                 rs->radius_rtd[i].stats.min_num=0;
111                 rs->radius_rtd[i].stats.max_num=0;
112                 rs->radius_rtd[i].stats.min.secs=0;
113         rs->radius_rtd[i].stats.min.nsecs=0;
114         rs->radius_rtd[i].stats.max.secs=0;
115         rs->radius_rtd[i].stats.max.nsecs=0;
116         rs->radius_rtd[i].stats.tot.secs=0;
117         rs->radius_rtd[i].stats.tot.nsecs=0;
118                 rs->radius_rtd[i].open_req_num = 0;
119                 rs->radius_rtd[i].disc_rsp_num = 0;
120                 rs->radius_rtd[i].req_dup_num = 0;
121                 rs->radius_rtd[i].rsp_dup_num = 0;
122         }
123
124 }
125
126
127 static int
128 radiusstat_packet(void *prs, packet_info *pinfo, epan_dissect_t *edt _U_, const void *pri)
129 {
130         radiusstat_t *rs=(radiusstat_t *)prs;
131         const radius_info_t *ri=pri;
132         nstime_t delta;
133         radius_category radius_cat = OTHERS;
134         int ret = 0;
135
136         switch (ri->code) {
137                 case RADIUS_ACCESS_REQUEST:
138                 case RADIUS_ACCESS_ACCEPT:
139                 case RADIUS_ACCESS_REJECT:
140                         radius_cat = ACCESS;
141                         break;
142                 case RADIUS_ACCOUNTING_REQUEST:
143                 case RADIUS_ACCOUNTING_RESPONSE:
144                         radius_cat = ACCOUNTING;
145                         break;
146                 case RADIUS_ACCESS_PASSWORD_REQUEST:
147                 case RADIUS_ACCESS_PASSWORD_ACK:
148                 case RADIUS_ACCESS_PASSWORD_REJECT:
149                         radius_cat = ACCESS_PASSWORD;
150                         break;
151                 case RADIUS_ASCEND_ACCESS_EVENT_REQUEST:
152                 case RADIUS_ASCEND_ACCESS_EVENT_RESPONSE:
153                         radius_cat = ASCEND_ACCESS_EVENT;
154                         break;
155                 case RADIUS_DISCONNECT_REQUEST:
156                 case RADIUS_DISCONNECT_REQUEST_ACK:
157                 case RADIUS_DISCONNECT_REQUEST_NAK:
158                         radius_cat = DISCONNECT;
159                         break;
160                 case RADIUS_CHANGE_FILTER_REQUEST:
161                 case RADIUS_CHANGE_FILTER_REQUEST_ACK:
162                 case RADIUS_CHANGE_FILTER_REQUEST_NAK:
163                         radius_cat = CHANGE_FILTER;
164                         break;
165         }
166
167         switch (ri->code) {
168
169         case RADIUS_ACCESS_REQUEST:
170         case RADIUS_ACCOUNTING_REQUEST:
171         case RADIUS_ACCESS_PASSWORD_REQUEST:
172         case RADIUS_ASCEND_ACCESS_EVENT_REQUEST:
173         case RADIUS_DISCONNECT_REQUEST:
174         case RADIUS_CHANGE_FILTER_REQUEST:
175                 if(ri->is_duplicate){
176                         /* Duplicate is ignored */
177                         rs->radius_rtd[OVERALL].req_dup_num++;
178                         rs->radius_rtd[radius_cat].req_dup_num++;
179                 }
180                 else {
181                         rs->radius_rtd[OVERALL].open_req_num++;
182                         rs->radius_rtd[radius_cat].open_req_num++;
183                 }
184                 break;
185
186         case RADIUS_ACCESS_ACCEPT:
187         case RADIUS_ACCESS_REJECT:
188         case RADIUS_ACCOUNTING_RESPONSE:
189         case RADIUS_ACCESS_PASSWORD_ACK:
190         case RADIUS_ACCESS_PASSWORD_REJECT:
191         case RADIUS_ASCEND_ACCESS_EVENT_RESPONSE:
192         case RADIUS_DISCONNECT_REQUEST_ACK:
193         case RADIUS_DISCONNECT_REQUEST_NAK:
194         case RADIUS_CHANGE_FILTER_REQUEST_ACK:
195         case RADIUS_CHANGE_FILTER_REQUEST_NAK:
196                 if(ri->is_duplicate){
197                         /* Duplicate is ignored */
198                         rs->radius_rtd[OVERALL].rsp_dup_num++;
199                         rs->radius_rtd[radius_cat].rsp_dup_num++;
200                 }
201                 else if (!ri->request_available) {
202                         /* no request was seen */
203                         rs->radius_rtd[OVERALL].disc_rsp_num++;
204                         rs->radius_rtd[radius_cat].disc_rsp_num++;
205                 }
206                 else {
207                         rs->radius_rtd[OVERALL].open_req_num--;
208                         rs->radius_rtd[radius_cat].open_req_num--;
209                         /* calculate time delta between request and response */
210                         nstime_delta(&delta, &pinfo->fd->abs_ts, &ri->req_time);
211
212                         time_stat_update(&(rs->radius_rtd[OVERALL].stats),&delta, pinfo);
213                         time_stat_update(&(rs->radius_rtd[radius_cat].stats),&delta, pinfo);
214
215                         ret = 1;
216                 }
217                 break;
218
219         default:
220                 break;
221         }
222
223         return ret;
224 }
225
226 static void
227 radiusstat_draw(void *prs)
228 {
229         radiusstat_t *rs=(radiusstat_t *)prs;
230         int i;
231         char str[5][256];
232         GtkListStore *store;
233         GtkTreeIter iter;
234
235         /* clear list before printing */
236         store = GTK_LIST_STORE(gtk_tree_view_get_model(rs->table));
237         gtk_list_store_clear(store);
238
239         for(i=0;i<NUM_TIMESTATS;i++) {
240                 /* nothing seen, nothing to do */
241                 if(rs->radius_rtd[i].stats.num==0){
242                         continue;
243                 }
244                 g_snprintf(str[0], 256, "%8.2f msec", nstime_to_msec(&(rs->radius_rtd[i].stats.min)));
245                 g_snprintf(str[1], 256, "%8.2f msec", nstime_to_msec(&(rs->radius_rtd[i].stats.max)));
246                 g_snprintf(str[2], 256, "%8.2f msec", get_average(&(rs->radius_rtd[i].stats.tot), rs->radius_rtd[i].stats.num));
247                 g_snprintf(str[3], 256, "%4u (%4.2f%%)", rs->radius_rtd[i].req_dup_num,
248                         rs->radius_rtd[i].stats.num?((double)rs->radius_rtd[i].req_dup_num*100)/(double)rs->radius_rtd[i].stats.num:0);
249                 g_snprintf(str[4], 256, "%4u (%4.2f%%)", rs->radius_rtd[i].rsp_dup_num,
250                         rs->radius_rtd[i].stats.num?((double)rs->radius_rtd[i].rsp_dup_num*100)/(double)rs->radius_rtd[i].stats.num:0);
251
252                 gtk_list_store_append(store, &iter);
253                 gtk_list_store_set(store, &iter,
254                         0, val_to_str(i, radius_message_code,"Other"),
255                         1, rs->radius_rtd[i].stats.num,
256                         2, str[0],
257                         3, str[1],
258                         4, str[2],
259                         5, rs->radius_rtd[i].stats.min_num,
260                         6, rs->radius_rtd[i].stats.max_num,
261                         7, rs->radius_rtd[i].open_req_num,
262                         8, rs->radius_rtd[i].disc_rsp_num,
263                         9, str[3],
264                         10, str[4],
265                         -1);
266         }
267 }
268
269 static void
270 win_destroy_cb(GtkWindow *win _U_, gpointer data)
271 {
272         radiusstat_t *rs=(radiusstat_t *)data;
273
274         protect_thread_critical_region();
275         remove_tap_listener(rs);
276         unprotect_thread_critical_region();
277
278         if(rs->filter){
279                 g_free(rs->filter);
280                 rs->filter=NULL;
281         }
282         g_free(rs);
283 }
284
285 static const stat_column titles[]={
286         {G_TYPE_STRING, LEFT,  "Type" },
287         {G_TYPE_UINT, RIGHT,   "Messages" },
288         {G_TYPE_STRING, RIGHT, "Min SRT" },
289         {G_TYPE_STRING, RIGHT, "Max SRT" },
290         {G_TYPE_STRING, RIGHT, "Avg SRT" },
291         {G_TYPE_UINT, RIGHT,   "Min in Frame" },
292         {G_TYPE_UINT, RIGHT,   "Max in Frame" },
293         {G_TYPE_UINT, RIGHT,   "Open Requests" },
294         {G_TYPE_UINT, RIGHT,   "Discarded Responses" },
295         {G_TYPE_STRING, RIGHT, "Repeated Requests" },
296         {G_TYPE_STRING, RIGHT, "Repeated Responses"}
297 };
298
299 static void
300 gtk_radiusstat_init(const char *optarg, void *userdata _U_)
301 {
302         radiusstat_t *rs;
303         GString *error_string;
304         GtkWidget *bt_close;
305         GtkWidget *bbox;
306
307         rs=g_malloc(sizeof(radiusstat_t));
308
309         if(strncmp(optarg,"radius,srt,",11) == 0){
310                 rs->filter=g_strdup(optarg+11);
311         } else {
312                 rs->filter=NULL;
313         }
314
315         radiusstat_reset(rs);
316
317         rs->win = dlg_window_new("RADIUS SRT");  /* transient_for top_level */
318         gtk_window_set_destroy_with_parent (GTK_WINDOW(rs->win), TRUE);
319         gtk_window_set_default_size(GTK_WINDOW(rs->win), 600, 150);
320
321         rs->vbox=gtk_vbox_new(FALSE, 3);
322
323         init_main_stat_window(rs->win, rs->vbox, "RADIUS Service Response Time (SRT) Statistics", rs->filter);
324
325         /* init a scrolled window*/
326         rs->scrolled_window = scrolled_window_new(NULL, NULL);
327
328         rs->table = create_stat_table(rs->scrolled_window, rs->vbox, NUM_COLUMNS, titles);
329
330         error_string=register_tap_listener("radius", rs, rs->filter, 0, radiusstat_reset, radiusstat_packet, radiusstat_draw);
331         if(error_string){
332                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s", error_string->str);
333                 g_string_free(error_string, TRUE);
334                 g_free(rs->filter);
335                 g_free(rs);
336                 return;
337         }
338
339         /* Button row. */
340         bbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
341         gtk_box_pack_start(GTK_BOX(rs->vbox), bbox, FALSE, FALSE, 0);
342
343         bt_close = g_object_get_data(G_OBJECT(bbox), GTK_STOCK_CLOSE);
344         window_set_cancel_button(rs->win, bt_close, window_cancel_button_cb);
345
346         g_signal_connect(rs->win, "delete_event", G_CALLBACK(window_delete_event_cb), NULL);
347         g_signal_connect(rs->win, "destroy", G_CALLBACK(win_destroy_cb), rs);
348
349         gtk_widget_show_all(rs->win);
350         window_present(rs->win);
351
352         cf_retap_packets(&cfile);
353         gdk_window_raise(rs->win->window);
354 }
355
356 static tap_param radius_stat_params[] = {
357         { PARAM_FILTER, "Filter", NULL }
358 };
359
360 static tap_param_dlg radius_srt_dlg = {
361         "RADIUS Service Response Time (SRT) Statistics",
362         "radius,srt",
363         gtk_radiusstat_init,
364         -1,
365         G_N_ELEMENTS(radius_stat_params),
366         radius_stat_params
367 };
368
369 void
370 register_tap_listener_gtkradiusstat(void)
371 {
372         register_dfilter_stat(&radius_srt_dlg, "RADIUS",
373                     REGISTER_STAT_GROUP_RESPONSE_TIME);
374 }