Add some comments about RSN IE
[obnox/wireshark/wip.git] / tap-radiusstat.c
1 /* tap-radiusstat.c
2  * Copyright 2006 Alejandro Vaquero <alejandrovaquero@yahoo.com>
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 #include <stdio.h>
30
31 #ifdef HAVE_SYS_TYPES_H
32 # include <sys/types.h>
33 #endif
34
35 #include <string.h>
36 #include "epan/packet_info.h"
37 #include <epan/tap.h>
38 #include <epan/stat_cmd_args.h>
39 #include "epan/value_string.h"
40 #include <epan/dissectors/packet-radius.h>
41 #include "timestats.h"
42
43 #define NUM_TIMESTATS 8
44
45 /* used to keep track of the statistics for an entire program interface */
46 typedef struct _radiusstat_t {
47         char *filter;
48         timestat_t rtd[NUM_TIMESTATS];
49         guint32 open_req_num;
50         guint32 disc_rsp_num;
51         guint32 req_dup_num;
52         guint32 rsp_dup_num;
53 } radiusstat_t;
54
55 static const value_string radius_message_code[] = {
56   {  0, "Overall       "},
57   {  1, "Access        "},
58   {  2, "Accounting    "},
59   {  3, "Access Passw  "},
60   {  4, "Ascend Acce Ev"},
61   {  5, "Disconnect    "},
62   {  6, "Change Filter "},
63   {  7, "Other         "},
64   {  0, NULL}
65 };
66
67 static int
68 radiusstat_packet(void *prs, packet_info *pinfo, epan_dissect_t *edt _U_, const void *pri)
69 {
70         radiusstat_t *rs=(radiusstat_t *)prs;
71         const radius_info_t *ri=pri;
72         nstime_t delta;
73         int ret = 0;
74
75         switch (ri->code) {
76
77         case RADIUS_ACCESS_REQUEST:
78         case RADIUS_ACCOUNTING_REQUEST:
79         case RADIUS_ACCESS_PASSWORD_REQUEST:
80         case RADIUS_ASCEND_ACCESS_EVENT_REQUEST:
81         case RADIUS_DISCONNECT_REQUEST:
82         case RADIUS_CHANGE_FILTER_REQUEST:
83                 if(ri->is_duplicate){
84                         /* Duplicate is ignored */
85                         rs->req_dup_num++;
86                 }
87                 else {
88                         rs->open_req_num++;
89                 }
90                 break;
91
92         case RADIUS_ACCESS_ACCEPT:
93         case RADIUS_ACCESS_REJECT:
94         case RADIUS_ACCOUNTING_RESPONSE:
95         case RADIUS_ACCESS_PASSWORD_ACK:
96         case RADIUS_ACCESS_PASSWORD_REJECT:
97         case RADIUS_ASCEND_ACCESS_EVENT_RESPONSE:
98         case RADIUS_DISCONNECT_REQUEST_ACK:
99         case RADIUS_DISCONNECT_REQUEST_NAK:
100         case RADIUS_CHANGE_FILTER_REQUEST_ACK:
101         case RADIUS_CHANGE_FILTER_REQUEST_NAK:
102                 if(ri->is_duplicate){
103                         /* Duplicate is ignored */
104                         rs->rsp_dup_num++;
105                 }
106                 else if (!ri->request_available) {
107                         /* no request was seen */
108                         rs->disc_rsp_num++;
109                 }
110                 else {
111                         rs->open_req_num--;
112                         /* calculate time delta between request and response */
113                         nstime_delta(&delta, &pinfo->fd->abs_ts, &ri->req_time);
114
115                         time_stat_update(&(rs->rtd[0]),&delta, pinfo);
116                         if (ri->code == RADIUS_ACCESS_ACCEPT || ri->code == RADIUS_ACCESS_REJECT) {
117                                 time_stat_update(&(rs->rtd[1]),&delta, pinfo);
118                         }
119                         else if (ri->code == RADIUS_ACCOUNTING_RESPONSE) {
120                                 time_stat_update(&(rs->rtd[2]),&delta, pinfo);
121                         }
122                         else {
123                                 time_stat_update(&(rs->rtd[7]),&delta, pinfo);
124                         }
125
126                         ret = 1;
127                 }
128                 break;
129
130         default:
131                 break;
132         }
133
134         return ret;
135 }
136
137 static void
138 radiusstat_draw(void *prs)
139 {
140         radiusstat_t *rs=(radiusstat_t *)prs;
141         int i;
142
143         /* printing results */
144         printf("\n");
145         printf("===========================================================================================================\n");
146         printf("RADIUS Response Time Delay (RTD) Statistics:\n");
147         printf("Filter for statistics: %s\n",rs->filter?rs->filter:"");
148         printf("Duplicate requests: %u\n",rs->req_dup_num);
149         printf("Duplicate responses: %u\n",rs->rsp_dup_num);
150         printf("Open requests: %u\n",rs->open_req_num);
151         printf("Discarded responses: %u\n",rs->disc_rsp_num);
152         printf("Type           | Messages   |    Min RTD    |    Max RTD    |    Avg RTD    | Min in Frame | Max in Frame |\n");
153         for(i=0;i<NUM_TIMESTATS;i++) {
154                 if(rs->rtd[i].num) {
155                         printf("%s | %7u    | %8.2f msec | %8.2f msec | %8.2f msec |  %10u  |  %10u  |\n",
156                                 val_to_str(i,radius_message_code,"Other  "),rs->rtd[i].num,
157                                 nstime_to_msec(&(rs->rtd[i].min)), nstime_to_msec(&(rs->rtd[i].max)),
158                                 get_average(&(rs->rtd[i].tot), rs->rtd[i].num),
159                                 rs->rtd[i].min_num, rs->rtd[i].max_num
160                         );
161                 }
162         }
163         printf("===========================================================================================================\n");
164 }
165
166
167 static void
168 radiusstat_init(const char *optarg, void* userdata _U_)
169 {
170         radiusstat_t *rs;
171         int i;
172         GString *error_string;
173
174         rs=g_malloc(sizeof(radiusstat_t));
175         if(!strncmp(optarg,"radius,rtd,",11)){
176                 rs->filter=g_strdup(optarg+11);
177         } else {
178                 rs->filter=NULL;
179         }
180
181         for(i=0;i<NUM_TIMESTATS;i++) {
182                 rs->rtd[i].num=0;
183                 rs->rtd[i].min_num=0;
184                 rs->rtd[i].max_num=0;
185                 rs->rtd[i].min.secs=0;
186                 rs->rtd[i].min.nsecs=0;
187                 rs->rtd[i].max.secs=0;
188                 rs->rtd[i].max.nsecs=0;
189                 rs->rtd[i].tot.secs=0;
190                 rs->rtd[i].tot.nsecs=0;
191         }
192
193         rs->open_req_num=0;
194         rs->disc_rsp_num=0;
195         rs->req_dup_num=0;
196         rs->rsp_dup_num=0;
197
198         error_string=register_tap_listener("radius", rs, rs->filter, 0, NULL, radiusstat_packet, radiusstat_draw);
199         if(error_string){
200                 /* error, we failed to attach to the tap. clean up */
201                 g_free(rs->filter);
202                 g_free(rs);
203
204                 fprintf(stderr, "tshark: Couldn't register radius,rtd tap: %s\n",
205                     error_string->str);
206                 g_string_free(error_string, TRUE);
207                 exit(1);
208         }
209 }
210
211
212 void
213 register_tap_listener_radiusstat(void)
214 {
215         register_stat_cmd_arg("radius,rtd", radiusstat_init, NULL);
216 }
217