aboutsummaryrefslogtreecommitdiffstats
path: root/ui/cli/tap-iousers.c
blob: 129b56e580287785495935c7c708325b0afccc61 (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-iousers.c
 * iostat   2003 Ronnie Sahlberg
 *
 * 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.h>
#include <epan/timestamp.h>
#include <ui/cli/tshark-tap.h>

typedef struct _io_users_t {
	const char *type;
	const char *filter;
	conv_hash_t hash;
} io_users_t;

static void
iousers_draw(void *arg)
{
	conv_hash_t *hash = (conv_hash_t*)arg;
	io_users_t *iu = (io_users_t *)hash->user_data;
	conv_item_t *iui;
	guint64 last_frames, max_frames;
	struct tm * tm_time;
	guint i;
	gboolean display_ports = (!strncmp(iu->type, "TCP", 3) || !strncmp(iu->type, "UDP", 3) || !strncmp(iu->type, "SCTP", 4)) ? TRUE : FALSE;

	printf("================================================================================\n");
	printf("%s Conversations\n", iu->type);
	printf("Filter:%s\n", iu->filter ? iu->filter : "<No Filter>");

	switch (timestamp_get_type()) {
	case TS_ABSOLUTE:
	case TS_UTC:
		printf("%s                                               |       <-      | |       ->      | |     Total     | Absolute Time  |   Duration   |\n",
			display_ports ? "            " : "");
		printf("%s                                               | Frames  Bytes | | Frames  Bytes | | Frames  Bytes |      Start     |              |\n",
			display_ports ? "            " : "");
		break;
	case TS_ABSOLUTE_WITH_YMD:
	case TS_ABSOLUTE_WITH_YDOY:
	case TS_UTC_WITH_YMD:
	case TS_UTC_WITH_YDOY:
		printf("%s                                               |       <-      | |       ->      | |     Total     | Absolute Date  |   Duration   |\n",
			display_ports ? "            " : "");
		printf("%s                                               | Frames  Bytes | | Frames  Bytes | | Frames  Bytes |     Start      |              |\n",
			display_ports ? "            " : "");
		break;
	case TS_RELATIVE:
	case TS_NOT_SET:
	default:
		printf("%s                                               |       <-      | |       ->      | |     Total     |    Relative    |   Duration   |\n",
			display_ports ? "            " : "");
		printf("%s                                               | Frames  Bytes | | Frames  Bytes | | Frames  Bytes |      Start     |              |\n",
			display_ports ? "            " : "");
		break;
	}

	max_frames = UINT_MAX;
	do {
		last_frames = 0;
		for (i=0; (iu->hash.conv_array && i < iu->hash.conv_array->len); i++) {
			guint64 tot_frames;

			iui = &g_array_index(iu->hash.conv_array, conv_item_t, i);
			tot_frames = iui->rx_frames + iui->tx_frames;

			if ((tot_frames > last_frames) && (tot_frames < max_frames)) {
				last_frames = tot_frames;
			}
		}

		for (i=0; (iu->hash.conv_array && i < iu->hash.conv_array->len); i++) {
			guint64 tot_frames;
			char *src_addr, *dst_addr;

			iui = &g_array_index(iu->hash.conv_array, conv_item_t, i);
			tot_frames = iui->rx_frames + iui->tx_frames;

			if (tot_frames == last_frames) {
				/* XXX - TODO: make name / port resolution configurable (through gbl_resolv_flags?) */
				src_addr = get_conversation_address(NULL, &iui->src_address, TRUE);
				dst_addr = get_conversation_address(NULL, &iui->dst_address, TRUE);
				if (display_ports) {
					char *src, *dst, *src_port, *dst_port;
					src_port = get_conversation_port(NULL, iui->src_port, iui->ptype, TRUE);
					dst_port = get_conversation_port(NULL, iui->dst_port, iui->ptype, TRUE);
					src = wmem_strconcat(NULL, src_addr, ":", src_port, NULL);
					dst = wmem_strconcat(NULL, dst_addr, ":", dst_port, NULL);
					printf("%-26s <-> %-26s  %6" G_GINT64_MODIFIER "u %9" G_GINT64_MODIFIER
					       "u  %6" G_GINT64_MODIFIER "u %9" G_GINT64_MODIFIER "u  %6"
					       G_GINT64_MODIFIER "u %9" G_GINT64_MODIFIER "u  ",
						src, dst,
						iui->rx_frames, iui->rx_bytes,
						iui->tx_frames, iui->tx_bytes,
						iui->tx_frames+iui->rx_frames,
						iui->tx_bytes+iui->rx_bytes
					);
					wmem_free(NULL, src_port);
					wmem_free(NULL, dst_port);
					wmem_free(NULL, src);
					wmem_free(NULL, dst);
				} else {
					printf("%-20s <-> %-20s  %6" G_GINT64_MODIFIER "u %9" G_GINT64_MODIFIER
					       "u  %6" G_GINT64_MODIFIER "u %9" G_GINT64_MODIFIER "u  %6"
					       G_GINT64_MODIFIER "u %9" G_GINT64_MODIFIER "u  ",
						src_addr, dst_addr,
						iui->rx_frames, iui->rx_bytes,
						iui->tx_frames, iui->tx_bytes,
						iui->tx_frames+iui->rx_frames,
						iui->tx_bytes+iui->rx_bytes
					);
				}

				wmem_free(NULL, src_addr);
				wmem_free(NULL, dst_addr);

				switch (timestamp_get_type()) {
				case TS_ABSOLUTE:
					tm_time = localtime(&iui->start_abs_time.secs);
					if (tm_time != NULL) {
						printf("%02d:%02d:%02d",
							 tm_time->tm_hour,
							 tm_time->tm_min,
							 tm_time->tm_sec);
					} else
						printf("XX:XX:XX");
					break;
				case TS_ABSOLUTE_WITH_YMD:
					tm_time = localtime(&iui->start_abs_time.secs);
					if (tm_time != NULL) {
						printf("%04d-%02d-%02d %02d:%02d:%02d",
							 tm_time->tm_year + 1900,
							 tm_time->tm_mon + 1,
							 tm_time->tm_mday,
							 tm_time->tm_hour,
							 tm_time->tm_min,
							 tm_time->tm_sec);
					} else
						printf("XXXX-XX-XX XX:XX:XX");
					break;
				case TS_ABSOLUTE_WITH_YDOY:
					tm_time = localtime(&iui->start_abs_time.secs);
					if (tm_time != NULL) {
						printf("%04d/%03d %02d:%02d:%02d",
							 tm_time->tm_year + 1900,
							 tm_time->tm_yday + 1,
							 tm_time->tm_hour,
							 tm_time->tm_min,
							 tm_time->tm_sec);
					} else
						printf("XXXX/XXX XX:XX:XX");
					break;
				case TS_UTC:
					tm_time = gmtime(&iui->start_abs_time.secs);
					if (tm_time != NULL) {
						printf("%02d:%02d:%02d",
							 tm_time->tm_hour,
							 tm_time->tm_min,
							 tm_time->tm_sec);
					} else
						printf("XX:XX:XX");
					break;
				case TS_UTC_WITH_YMD:
					tm_time = gmtime(&iui->start_abs_time.secs);
					if (tm_time != NULL) {
						printf("%04d-%02d-%02d %02d:%02d:%02d",
							 tm_time->tm_year + 1900,
							 tm_time->tm_mon + 1,
							 tm_time->tm_mday,
							 tm_time->tm_hour,
							 tm_time->tm_min,
							 tm_time->tm_sec);
					} else
						printf("XXXX-XX-XX XX:XX:XX");
					break;
				case TS_UTC_WITH_YDOY:
					tm_time = gmtime(&iui->start_abs_time.secs);
					if (tm_time != NULL) {
						printf("%04d/%03d %02d:%02d:%02d",
							 tm_time->tm_year + 1900,
							 tm_time->tm_yday + 1,
							 tm_time->tm_hour,
							 tm_time->tm_min,
							 tm_time->tm_sec);
					} else
						printf("XXXX/XXX XX:XX:XX");
					break;
				case TS_RELATIVE:
				case TS_NOT_SET:
				default:
					printf("%14.9f",
						nstime_to_sec(&iui->start_time));
					break;
				}
				printf("   %12.4f\n",
					 nstime_to_sec(&iui->stop_time) - nstime_to_sec(&iui->start_time));
			}
		}
		max_frames = last_frames;
	} while (last_frames);
	printf("================================================================================\n");
}

void init_iousers(struct register_ct *ct, const char *filter)
{
	io_users_t *iu;
	GString *error_string;

	iu = g_new0(io_users_t, 1);
	iu->type = proto_get_protocol_short_name(find_protocol_by_id(get_conversation_proto_id(ct)));
	iu->filter = g_strdup(filter);
	iu->hash.user_data = iu;

	error_string = register_tap_listener(proto_get_protocol_filter_name(get_conversation_proto_id(ct)), &iu->hash, filter, 0, NULL, get_conversation_packet_func(ct), iousers_draw);
	if (error_string) {
		g_free(iu);
		fprintf(stderr, "tshark: Couldn't register conversations tap: %s\n",
		    error_string->str);
		g_string_free(error_string, TRUE);
		exit(1);
	}

}

/*
 * 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:
 */