From Harald Welte:
[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 typedef enum _radius_category {
44         RADIUS_CAT_OVERALL = 0,
45         RADIUS_CAT_ACCESS,
46         RADIUS_CAT_ACCOUNTING,
47         RADIUS_CAT_PASSWORD,
48         RADIUS_CAT_RESOURCE_FREE,
49         RADIUS_CAT_RESOURCE_QUERY,
50         RADIUS_CAT_NAS_REBOOT,
51         RADIUS_CAT_EVENT,
52         RADIUS_CAT_DISCONNECT,
53         RADIUS_CAT_COA,
54         RADIUS_CAT_OTHERS,
55         RADIUS_CAT_NUM_TIMESTATS
56 } radius_category;
57
58 /* used to keep track of the statistics for an entire program interface */
59 typedef struct _radiusstat_t {
60         char *filter;
61         timestat_t rtd[RADIUS_CAT_NUM_TIMESTATS];
62         guint32 open_req_num;
63         guint32 disc_rsp_num;
64         guint32 req_dup_num;
65         guint32 rsp_dup_num;
66 } radiusstat_t;
67
68
69
70
71 static const value_string radius_message_code[] = {
72         {  RADIUS_CAT_OVERALL,        "Overall       "},
73         {  RADIUS_CAT_ACCESS,         "Access        "},
74         {  RADIUS_CAT_ACCOUNTING,     "Accounting    "},
75         {  RADIUS_CAT_PASSWORD,       "Password      "},
76         {  RADIUS_CAT_RESOURCE_FREE,  "Resource Free "},
77         {  RADIUS_CAT_RESOURCE_QUERY, "Resource Query"},
78         {  RADIUS_CAT_NAS_REBOOT,     "NAS Reboot    "},
79         {  RADIUS_CAT_EVENT,          "Event         "},
80         {  RADIUS_CAT_DISCONNECT,     "Disconnect    "},
81         {  RADIUS_CAT_COA,            "CoA           "},
82         {  RADIUS_CAT_OTHERS,         "Other         "},
83         {  0, NULL}
84 };
85
86 static int
87 radiusstat_packet(void *prs, packet_info *pinfo, epan_dissect_t *edt _U_, const void *pri)
88 {
89         radiusstat_t *rs=(radiusstat_t *)prs;
90         const radius_info_t *ri=pri;
91         nstime_t delta;
92         int ret = 0;
93
94         switch (ri->code) {
95
96         case RADIUS_PKT_TYPE_ACCESS_REQUEST:
97         case RADIUS_PKT_TYPE_ACCOUNTING_REQUEST:
98         case RADIUS_PKT_TYPE_PASSWORD_REQUEST:
99         case RADIUS_PKT_TYPE_RESOURCE_FREE_REQUEST:
100         case RADIUS_PKT_TYPE_RESOURCE_QUERY_REQUEST:
101         case RADIUS_PKT_TYPE_NAS_REBOOT_REQUEST:
102         case RADIUS_PKT_TYPE_EVENT_REQUEST:
103         case RADIUS_PKT_TYPE_DISCONNECT_REQUEST:
104         case RADIUS_PKT_TYPE_COA_REQUEST:
105                 if(ri->is_duplicate){
106                         /* Duplicate is ignored */
107                         rs->req_dup_num++;
108                 }
109                 else {
110                         rs->open_req_num++;
111                 }
112                 break;
113
114         case RADIUS_PKT_TYPE_ACCESS_ACCEPT:
115         case RADIUS_PKT_TYPE_ACCESS_REJECT:
116         case RADIUS_PKT_TYPE_ACCOUNTING_RESPONSE:
117         case RADIUS_PKT_TYPE_PASSWORD_ACK:
118         case RADIUS_PKT_TYPE_PASSWORD_REJECT:
119         case RADIUS_PKT_TYPE_RESOURCE_FREE_RESPONSE:
120         case RADIUS_PKT_TYPE_RESOURCE_QUERY_RESPONSE:
121         case RADIUS_PKT_TYPE_NAS_REBOOT_RESPONSE:
122         case RADIUS_PKT_TYPE_EVENT_RESPONSE:
123         case RADIUS_PKT_TYPE_DISCONNECT_ACK:
124         case RADIUS_PKT_TYPE_DISCONNECT_NAK:
125         case RADIUS_PKT_TYPE_COA_ACK:
126         case RADIUS_PKT_TYPE_COA_NAK:
127                 if(ri->is_duplicate){
128                         /* Duplicate is ignored */
129                         rs->rsp_dup_num++;
130                 }
131                 else if (!ri->request_available) {
132                         /* no request was seen */
133                         rs->disc_rsp_num++;
134                 }
135                 else {
136                         rs->open_req_num--;
137                         /* calculate time delta between request and response */
138                         nstime_delta(&delta, &pinfo->fd->abs_ts, &ri->req_time);
139
140                         time_stat_update(&(rs->rtd[RADIUS_CAT_OVERALL]),&delta, pinfo);
141
142                         if (ri->code == RADIUS_PKT_TYPE_ACCESS_ACCEPT || ri->code == RADIUS_PKT_TYPE_ACCESS_REJECT) {
143                                 time_stat_update(&(rs->rtd[RADIUS_CAT_ACCESS]),&delta, pinfo);
144                         }
145                         else if (ri->code == RADIUS_PKT_TYPE_ACCOUNTING_RESPONSE) {
146                                 time_stat_update(&(rs->rtd[RADIUS_CAT_ACCOUNTING]),&delta, pinfo);
147                         }
148                         else {
149                                 time_stat_update(&(rs->rtd[RADIUS_CAT_OTHERS]),&delta, pinfo);
150                         }
151
152                         ret = 1;
153                 }
154                 break;
155
156         default:
157                 break;
158         }
159
160         return ret;
161 }
162
163 static void
164 radiusstat_draw(void *prs)
165 {
166         radiusstat_t *rs=(radiusstat_t *)prs;
167         int i;
168
169         /* printing results */
170         printf("\n");
171         printf("===========================================================================================================\n");
172         printf("RADIUS Response Time Delay (RTD) Statistics:\n");
173         printf("Filter for statistics: %s\n",rs->filter?rs->filter:"");
174         printf("Duplicate requests: %u\n",rs->req_dup_num);
175         printf("Duplicate responses: %u\n",rs->rsp_dup_num);
176         printf("Open requests: %u\n",rs->open_req_num);
177         printf("Discarded responses: %u\n",rs->disc_rsp_num);
178         printf("Type           | Messages   |    Min RTD    |    Max RTD    |    Avg RTD    | Min in Frame | Max in Frame |\n");
179         for(i=0;i<RADIUS_CAT_NUM_TIMESTATS;i++) {
180                 if(rs->rtd[i].num) {
181                         printf("%s | %7u    | %8.2f msec | %8.2f msec | %8.2f msec |  %10u  |  %10u  |\n",
182                                 val_to_str(i,radius_message_code,"Other  "),rs->rtd[i].num,
183                                 nstime_to_msec(&(rs->rtd[i].min)), nstime_to_msec(&(rs->rtd[i].max)),
184                                 get_average(&(rs->rtd[i].tot), rs->rtd[i].num),
185                                 rs->rtd[i].min_num, rs->rtd[i].max_num
186                         );
187                 }
188         }
189         printf("===========================================================================================================\n");
190 }
191
192
193 static void
194 radiusstat_init(const char *optarg, void* userdata _U_)
195 {
196         radiusstat_t *rs;
197         int i;
198         GString *error_string;
199
200         rs=g_malloc(sizeof(radiusstat_t));
201         if(!strncmp(optarg,"radius,rtd,",11)){
202                 rs->filter=g_strdup(optarg+11);
203         } else {
204                 rs->filter=NULL;
205         }
206
207         for(i=0;i<RADIUS_CAT_NUM_TIMESTATS;i++) {
208                 rs->rtd[i].num=0;
209                 rs->rtd[i].min_num=0;
210                 rs->rtd[i].max_num=0;
211                 rs->rtd[i].min.secs=0;
212                 rs->rtd[i].min.nsecs=0;
213                 rs->rtd[i].max.secs=0;
214                 rs->rtd[i].max.nsecs=0;
215                 rs->rtd[i].tot.secs=0;
216                 rs->rtd[i].tot.nsecs=0;
217         }
218
219         rs->open_req_num=0;
220         rs->disc_rsp_num=0;
221         rs->req_dup_num=0;
222         rs->rsp_dup_num=0;
223
224         error_string=register_tap_listener("radius", rs, rs->filter, 0, NULL, radiusstat_packet, radiusstat_draw);
225         if(error_string){
226                 /* error, we failed to attach to the tap. clean up */
227                 g_free(rs->filter);
228                 g_free(rs);
229
230                 fprintf(stderr, "tshark: Couldn't register radius,rtd tap: %s\n",
231                     error_string->str);
232                 g_string_free(error_string, TRUE);
233                 exit(1);
234         }
235 }
236
237
238 void
239 register_tap_listener_radiusstat(void)
240 {
241         register_stat_cmd_arg("radius,rtd", radiusstat_init, NULL);
242 }
243