Make infiniband plugin compile.
[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 "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         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         const char *filter=NULL;
173         GString *error_string;
174
175         if(!strncmp(optarg,"radius,rtd,",11)){
176                 filter=optarg+11;
177         } else {
178                 filter="";
179         }
180
181         rs=g_malloc(sizeof(radiusstat_t));
182         rs->filter=g_strdup(filter);
183
184         for(i=0;i<NUM_TIMESTATS;i++) {
185                 rs->rtd[i].num=0;
186                 rs->rtd[i].min_num=0;
187                 rs->rtd[i].max_num=0;
188                 rs->rtd[i].min.secs=0;
189                 rs->rtd[i].min.nsecs=0;
190                 rs->rtd[i].max.secs=0;
191                 rs->rtd[i].max.nsecs=0;
192                 rs->rtd[i].tot.secs=0;
193                 rs->rtd[i].tot.nsecs=0;
194         }
195
196         rs->open_req_num=0;
197         rs->disc_rsp_num=0;
198         rs->req_dup_num=0;
199         rs->rsp_dup_num=0;
200
201         error_string=register_tap_listener("radius", rs, filter, NULL, radiusstat_packet, radiusstat_draw);
202         if(error_string){
203                 /* error, we failed to attach to the tap. clean up */
204                 g_free(rs->filter);
205                 g_free(rs);
206
207                 fprintf(stderr, "tshark: Couldn't register radius,rtd tap: %s\n",
208                     error_string->str);
209                 g_string_free(error_string, TRUE);
210                 exit(1);
211         }
212 }
213
214
215 void
216 register_tap_listener_radiusstat(void)
217 {
218         register_stat_cmd_arg("radius,rtd", radiusstat_init, NULL);
219 }
220