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