aboutsummaryrefslogtreecommitdiffstats
path: root/ui/cli/tap-wspstat.c
blob: e8ab5669db3eb1eaf8d310060a915ffb98d1121b (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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/* tap-rpcstat.c
 * wspstat   2003 Jean-Michel FAYARD
 *
 * 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.
 */

/* This module provides WSP  statistics to tshark.
 * It is only used by tshark and not wireshark
 *
 */

#include "config.h"

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

#include "epan/packet_info.h"
#include <epan/tap.h>
#include <epan/stat_cmd_args.h>
#include "epan/value_string.h"
#include <epan/dissectors/packet-wsp.h>

void register_tap_listener_wspstat(void);

/* used to keep track of the stats for a specific PDU type*/
typedef struct _wsp_pdu_t {
	const gchar 	*type;
	guint32		 packets;
} wsp_pdu_t;
/* used to keep track of SRT statistics */
typedef struct _wsp_status_code_t {
	const gchar	*name;
	guint32		 packets;
} wsp_status_code_t;
/* used to keep track of the statictics for an entire program interface */
typedef struct _wsp_stats_t {
	char 		*filter;
	wsp_pdu_t 	*pdu_stats;
	guint32	num_pdus;
	GHashTable	*hash;
} wspstat_t;

static void
wsp_reset_hash(gchar *key _U_ , wsp_status_code_t *data, gpointer ptr _U_)
{
	data->packets = 0;
}
static void
wsp_print_statuscode(gint *key, wsp_status_code_t *data, char *format)
{
	if (data && (data->packets != 0))
		printf(format, *key, data->packets , data->name);
}
static void
wsp_free_hash_table( gpointer key, gpointer value, gpointer user_data _U_ )
{
	g_free(key);
	g_free(value);
}
static void
wspstat_reset(void *psp)
{
	wspstat_t *sp = (wspstat_t *)psp;
	guint32 i;

	for (i=1; i<=sp->num_pdus; i++)
	{
		sp->pdu_stats[i].packets = 0;
	}
	g_hash_table_foreach( sp->hash, (GHFunc)wsp_reset_hash, NULL);
}


/* This callback is invoked whenever the tap system has seen a packet
 * we might be interested in.
 * The function is to be used to only update internal state information
 * in the *tapdata structure, and if there were state changes which requires
 * the window to be redrawn, return 1 and (*draw) will be called sometime
 * later.
 *
 * We didn't apply a filter when we registered so we will be called for
 * ALL packets and not just the ones we are collecting stats for.
 *
 */
static gint
pdut2index(gint pdut)
{
	if (pdut <= 0x09)
		return pdut;
	if (pdut >= 0x40) {
		if (pdut <= 0x44) {
			return pdut - 54;
		} else if (pdut == 0x60 || pdut == 0x61) {
			return pdut - 81;
		}
	}
	return 0;
}
static gint
index2pdut(gint pdut)
{
	if (pdut <= 0x09)
		return pdut;
	if (pdut <= 14)
		return pdut + 54;
	if (pdut <= 16)
		return pdut + 81;
	return 0;
}
static int
wspstat_packet(void *psp, packet_info *pinfo _U_, epan_dissect_t *edt _U_, const void *pri)
{
	wspstat_t *sp = (wspstat_t *)psp;
	const wsp_info_value_t *value = (const wsp_info_value_t *)pri;
	gint idx = pdut2index(value->pdut);
	int retour = 0;

	if (value->status_code != 0) {
		gint *key = g_new(gint, 1);
		wsp_status_code_t *sc;
		*key = value->status_code ;
		sc = (wsp_status_code_t *)g_hash_table_lookup(
				sp->hash,
				key);
		if (!sc) {
			sc = g_new(wsp_status_code_t, 1);
			sc -> packets = 1;
			sc -> name = NULL;
			g_hash_table_insert(
				sp->hash,
				key,
				sc);
		} else {
			sc->packets++;
		}
		retour = 1;
	}



	if (idx != 0) {
		sp->pdu_stats[idx].packets++;
		retour = 1;
	}
	return retour;
}


/* This callback is used when tshark wants us to draw/update our
 * data to the output device. Since this is tshark only output is
 * stdout.
 * TShark will only call this callback once, which is when tshark has
 * finished reading all packets and exists.
 * If used with wireshark this may be called any time, perhaps once every 3
 * seconds or so.
 * This function may even be called in parallell with (*reset) or (*draw)
 * so make sure there are no races. The data in the rpcstat_t can thus change
 * beneath us. Beware.
 */
static void
wspstat_draw(void *psp)
{
	wspstat_t *sp = (wspstat_t *)psp;
	guint32 i;

	printf("\n");
	printf("===================================================================\n");
	printf("WSP Statistics:\n");
	printf("%-23s %9s || %-23s %9s\n", "PDU Type", "Packets", "PDU Type", "Packets");
	for (i=1; i <= ((sp->num_pdus+1)/2); i++)
	{
		guint32 ii = i+sp->num_pdus/2;
		printf("%-23s %9d", sp->pdu_stats[i ].type, sp->pdu_stats[i ].packets);
		printf(" || ");
		if (ii< (sp->num_pdus) )
			printf("%-23s %9d\n", sp->pdu_stats[ii].type, sp->pdu_stats[ii].packets);
		else
			printf("\n");
		}
	printf("\nStatus code in reply packets\n");
	printf(		"Status Code    Packets  Description\n");
	g_hash_table_foreach( sp->hash, (GHFunc) wsp_print_statuscode,
			(gpointer)"       0x%02X  %9d  %s\n" ) ;
	printf("===================================================================\n");
}

/* When called, this function will create a new instance of wspstat.
 * program and version are whick onc-rpc program/version we want to
 * collect statistics for.
 * This function is called from tshark when it parses the -z wsp, arguments
 * and it creates a new instance to store statistics in and registers this
 * new instance for the wsp tap.
 */
static void
wspstat_init(const char *opt_arg, void *userdata _U_)
{
	wspstat_t          *sp;
	const char         *filter = NULL;
	guint32             i;
	GString	           *error_string;
	wsp_status_code_t  *sc;
	const value_string *wsp_vals_status_p;

	if (!strncmp (opt_arg, "wsp,stat,", 9)) {
		filter = opt_arg+9;
	} else {
		filter = NULL;
	}


	sp = g_new(wspstat_t, 1);
	sp->hash = g_hash_table_new( g_int_hash, g_int_equal);
	wsp_vals_status_p = VALUE_STRING_EXT_VS_P(&wsp_vals_status_ext);
	for (i=0; wsp_vals_status_p[i].strptr; i++ )
	{
		gint *key;
		sc = g_new(wsp_status_code_t, 1);
		key = g_new(gint, 1);
		sc->packets = 0;
		sc->name = wsp_vals_status_p[i].strptr;
		*key = wsp_vals_status_p[i].value;
		g_hash_table_insert(
				sp->hash,
				key,
				sc);
	}
	sp->num_pdus = 16;
	sp->pdu_stats = g_new(wsp_pdu_t, (sp->num_pdus+1));
	if (filter) {
		sp->filter = g_strdup(filter);
	} else {
		sp->filter = NULL;
	}
	for (i=0; i<sp->num_pdus; i++)
	{
		sp->pdu_stats[i].packets = 0;
		sp->pdu_stats[i].type = try_val_to_str_ext( index2pdut( i ), &wsp_vals_pdu_type_ext) ;
	}

	error_string = register_tap_listener(
			"wsp",
			sp,
			filter,
			0,
			wspstat_reset,
			wspstat_packet,
			wspstat_draw);
	if (error_string) {
		/* error, we failed to attach to the tap. clean up */
		g_free(sp->pdu_stats);
		g_free(sp->filter);
		g_free(sp);
		g_hash_table_foreach( sp->hash, (GHFunc) wsp_free_hash_table, NULL ) ;
		g_hash_table_destroy( sp->hash );
		fprintf(stderr, "tshark: Couldn't register wsp,stat tap: %s\n",
				error_string->str);
		g_string_free(error_string, TRUE);
		exit(1);
	}
}

static stat_tap_ui wspstat_ui = {
	REGISTER_STAT_GROUP_GENERIC,
	NULL,
	"wsp,stat,",
	wspstat_init,
	-1,
	0,
	NULL
};

void
register_tap_listener_wspstat(void)
{
	register_stat_tap_ui(&wspstat_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:
 */