aboutsummaryrefslogtreecommitdiffstats
path: root/ui/cli/tap-radiusstat.c
blob: b892b16089c460ba9b40c2eff604d40ec97c9f70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/* tap-radiusstat.c
 * Copyright 2006 Alejandro Vaquero <alejandrovaquero@yahoo.com>
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "epan/packet_info.h"
#include <epan/tap.h>
#include <epan/stat_tap_ui.h>
#include "epan/value_string.h"
#include <epan/dissectors/packet-radius.h>
#include "epan/timestats.h"

void register_tap_listener_radiusstat(void);

typedef enum _radius_category {
	RADIUS_CAT_OVERALL = 0,
	RADIUS_CAT_ACCESS,
	RADIUS_CAT_ACCOUNTING,
	RADIUS_CAT_PASSWORD,
	RADIUS_CAT_RESOURCE_FREE,
	RADIUS_CAT_RESOURCE_QUERY,
	RADIUS_CAT_NAS_REBOOT,
	RADIUS_CAT_EVENT,
	RADIUS_CAT_DISCONNECT,
	RADIUS_CAT_COA,
	RADIUS_CAT_OTHERS,
	RADIUS_CAT_NUM_TIMESTATS
} radius_category;

/* used to keep track of the statistics for an entire program interface */
typedef struct _radiusstat_t {
	char *filter;
	timestat_t rtd[RADIUS_CAT_NUM_TIMESTATS];
	guint32 open_req_num;
	guint32 disc_rsp_num;
	guint32 req_dup_num;
	guint32 rsp_dup_num;
} radiusstat_t;




static const value_string radius_message_code[] = {
	{  RADIUS_CAT_OVERALL,        "Overall       "},
	{  RADIUS_CAT_ACCESS,         "Access        "},
	{  RADIUS_CAT_ACCOUNTING,     "Accounting    "},
	{  RADIUS_CAT_PASSWORD,       "Password      "},
	{  RADIUS_CAT_RESOURCE_FREE,  "Resource Free "},
	{  RADIUS_CAT_RESOURCE_QUERY, "Resource Query"},
	{  RADIUS_CAT_NAS_REBOOT,     "NAS Reboot    "},
	{  RADIUS_CAT_EVENT,          "Event         "},
	{  RADIUS_CAT_DISCONNECT,     "Disconnect    "},
	{  RADIUS_CAT_COA,            "CoA           "},
	{  RADIUS_CAT_OTHERS,         "Other         "},
	{  0, NULL}
};

static int
radiusstat_packet(void *prs, packet_info *pinfo, epan_dissect_t *edt _U_, const void *pri)
{
	radiusstat_t *rs = (radiusstat_t *)prs;
	const radius_info_t *ri = (const radius_info_t *)pri;
	nstime_t delta;
	int ret = 0;

	switch (ri->code) {

	case RADIUS_PKT_TYPE_ACCESS_REQUEST:
	case RADIUS_PKT_TYPE_ACCOUNTING_REQUEST:
	case RADIUS_PKT_TYPE_PASSWORD_REQUEST:
	case RADIUS_PKT_TYPE_RESOURCE_FREE_REQUEST:
	case RADIUS_PKT_TYPE_RESOURCE_QUERY_REQUEST:
	case RADIUS_PKT_TYPE_NAS_REBOOT_REQUEST:
	case RADIUS_PKT_TYPE_EVENT_REQUEST:
	case RADIUS_PKT_TYPE_DISCONNECT_REQUEST:
	case RADIUS_PKT_TYPE_COA_REQUEST:
		if (ri->is_duplicate) {
			/* Duplicate is ignored */
			rs->req_dup_num++;
		}
		else {
			rs->open_req_num++;
		}
		break;

	case RADIUS_PKT_TYPE_ACCESS_ACCEPT:
	case RADIUS_PKT_TYPE_ACCESS_REJECT:
	case RADIUS_PKT_TYPE_ACCOUNTING_RESPONSE:
	case RADIUS_PKT_TYPE_PASSWORD_ACK:
	case RADIUS_PKT_TYPE_PASSWORD_REJECT:
	case RADIUS_PKT_TYPE_RESOURCE_FREE_RESPONSE:
	case RADIUS_PKT_TYPE_RESOURCE_QUERY_RESPONSE:
	case RADIUS_PKT_TYPE_NAS_REBOOT_RESPONSE:
	case RADIUS_PKT_TYPE_EVENT_RESPONSE:
	case RADIUS_PKT_TYPE_DISCONNECT_ACK:
	case RADIUS_PKT_TYPE_DISCONNECT_NAK:
	case RADIUS_PKT_TYPE_COA_ACK:
	case RADIUS_PKT_TYPE_COA_NAK:
		if (ri->is_duplicate) {
			/* Duplicate is ignored */
			rs->rsp_dup_num++;
		}
		else if (!ri->request_available) {
			/* no request was seen */
			rs->disc_rsp_num++;
		}
		else {
			rs->open_req_num--;
			/* calculate time delta between request and response */
			nstime_delta(&delta, &pinfo->fd->abs_ts, &ri->req_time);

			time_stat_update(&(rs->rtd[RADIUS_CAT_OVERALL]), &delta, pinfo);

			if (ri->code == RADIUS_PKT_TYPE_ACCESS_ACCEPT || ri->code == RADIUS_PKT_TYPE_ACCESS_REJECT) {
				time_stat_update(&(rs->rtd[RADIUS_CAT_ACCESS]), &delta, pinfo);
			}
			else if (ri->code == RADIUS_PKT_TYPE_ACCOUNTING_RESPONSE) {
				time_stat_update(&(rs->rtd[RADIUS_CAT_ACCOUNTING]), &delta, pinfo);
			}
			else {
				time_stat_update(&(rs->rtd[RADIUS_CAT_OTHERS]), &delta, pinfo);
			}

			ret = 1;
		}
		break;

	default:
		break;
	}

	return ret;
}

static void
radiusstat_draw(void *prs)
{
	radiusstat_t *rs = (radiusstat_t *)prs;
	int i;

	/* printing results */
	printf("\n");
	printf("===========================================================================================================\n");
	printf("RADIUS Response Time Delay (RTD) Statistics:\n");
	printf("Filter for statistics: %s\n", rs->filter ? rs->filter : "");
	printf("Duplicate requests: %u\n", rs->req_dup_num);
	printf("Duplicate responses: %u\n", rs->rsp_dup_num);
	printf("Open requests: %u\n", rs->open_req_num);
	printf("Discarded responses: %u\n", rs->disc_rsp_num);
	printf("Type           | Messages   |    Min RTD    |    Max RTD    |    Avg RTD    | Min in Frame | Max in Frame |\n");
	for (i=0; i<RADIUS_CAT_NUM_TIMESTATS; i++) {
		if (rs->rtd[i].num) {
			printf("%s | %7u    | %8.2f msec | %8.2f msec | %8.2f msec |  %10u  |  %10u  |\n",
			       val_to_str(i, radius_message_code, "Other  "), rs->rtd[i].num,
			       nstime_to_msec(&(rs->rtd[i].min)), nstime_to_msec(&(rs->rtd[i].max)),
			       get_average(&(rs->rtd[i].tot), rs->rtd[i].num),
			       rs->rtd[i].min_num, rs->rtd[i].max_num
			);
		}
	}
	printf("===========================================================================================================\n");
}


static void
radiusstat_init(const char *opt_arg, void *userdata _U_)
{
	radiusstat_t *rs;
	int i;
	GString *error_string;

	rs = g_new(radiusstat_t, 1);
	if (!strncmp(opt_arg, "radius,rtd,", 11)) {
		rs->filter = g_strdup(opt_arg+11);
	} else {
		rs->filter = NULL;
	}

	for (i=0; i<RADIUS_CAT_NUM_TIMESTATS; i++) {
		rs->rtd[i].num	     = 0;
		rs->rtd[i].min_num   = 0;
		rs->rtd[i].max_num   = 0;
		rs->rtd[i].min.secs  = 0;
		rs->rtd[i].min.nsecs = 0;
		rs->rtd[i].max.secs  = 0;
		rs->rtd[i].max.nsecs = 0;
		rs->rtd[i].tot.secs  = 0;
		rs->rtd[i].tot.nsecs = 0;
	}

	rs->open_req_num = 0;
	rs->disc_rsp_num = 0;
	rs->req_dup_num	 = 0;
	rs->rsp_dup_num	 = 0;

	error_string = register_tap_listener("radius", rs, rs->filter, 0, NULL, radiusstat_packet, radiusstat_draw);
	if (error_string) {
		/* error, we failed to attach to the tap. clean up */
		g_free(rs->filter);
		g_free(rs);

		fprintf(stderr, "tshark: Couldn't register radius,rtd tap: %s\n",
			error_string->str);
		g_string_free(error_string, TRUE);
		exit(1);
	}
}

static stat_tap_ui radiusstat_ui = {
	REGISTER_STAT_GROUP_GENERIC,
	NULL,
	"radius,rtd",
	radiusstat_init,
	-1,
	0,
	NULL
};

void
register_tap_listener_radiusstat(void)
{
	register_stat_tap_ui(&radiusstat_ui, NULL);
}

/*
 * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
 *
 * Local variables:
 * c-basic-offset: 8
 * tab-width: 8
 * indent-tabs-mode: t
 * End:
 *
 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
 * :indentSize=8:tabSize=8:noTabs=false:
 */