aboutsummaryrefslogtreecommitdiffstats
path: root/gtk/h323_analysis.c
blob: ba1b2111770f786162adfffa3843d2d6d16a07f3 (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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
/* h323_analysis.c
 * H323 analysis addition for ethereal
 *
 * $Id$
 *
 * Copyright 2004, Iskratel, Ltd, Kranj
 * By Miha Jemec <m.jemec@iskratel.si>
 *
 * Ethereal - Network traffic analyzer
 * By Gerald Combs <gerald@ethereal.com>
 * 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

/*do not define this symbol. will be added soon*/
/*#define USE_CONVERSATION_GRAPH 1*/

#include "h323_analysis.h"
#include "h323_conversations.h"
#include "h323_conversations_dlg.h"

#include <epan/epan_dissect.h>
#include <epan/filesystem.h>

#include "globals.h"

#include "util.h"
#include <epan/tap.h>

#include <epan/dissectors/packet-h225.h>
#include <epan/dissectors/packet-h245.h>

/* in /gtk ... */
#include "dlg_utils.h"
#include "ui_util.h"
#include "alert_box.h"
#include "simple_dialog.h"
#include "tap_menu.h"
#include "main.h"
#include "progress_dlg.h"
#include "compat_macros.h"

#include <string.h>

/****************************************************************************/
/* structure that holds general information about the connection */
typedef struct _user_data_t {
        /* tap associated data*/
        guint32 ip_src;
        guint16 port_src;
        guint32 ip_dst;
        guint16 port_dst;
        guint32 ip_src_h245;
        guint16 port_src_h245;
        guint16 transport;

	GtkWidget *window;
	GtkCList *clist1;
	GtkWidget *label_stats;
	GtkCList *selected_clist1;
	gint selected_row;

} user_data_t;

/* Column titles. */
static gchar *titles[7] =  {
	"Packet",
	"Time",
	"Delay",
	"             Side A",
	"Direction",
	"             Side B",
	"Comment"
};

typedef const guint8 * ip_addr_p;

static gint32 last_sec = 0, last_usec = 0;


/****************************************************************************/
/* TAP FUNCTIONS */

/****************************************************************************/
/* when there is a [re]reading of packet's */
static void
h225_reset(void *user_data_arg _U_)
{
	last_sec = 0;
	last_usec = 0;
	return;
}

/****************************************************************************/
/* here we can redraw the output */
/* not used yet */
static void h225_draw(void *prs _U_)
{
	return;
}


static const GdkColor COLOR_DEFAULT = {0, 0xffff, 0xffff, 0xffff};
static const GdkColor COLOR_ERROR = {0, 0xffff, 0xbfff, 0xbfff};
static const GdkColor COLOR_WARNING = {0, 0xffff, 0xdfff, 0xbfff};
static const GdkColor COLOR_CN = {0, 0xbfff, 0xbfff, 0xffff};

/****************************************************************************/
/* append a line to clist1 */
static void add_to_clist1(GtkCList *clist1, guint32 number, gchar *time,
                         double delay, gchar *sideA, gboolean direction, gchar *sideB,
                         gchar *comment, GdkColor *color)
{
        guint added_row;
        gchar *data[7];
        gchar field[7][32];

        data[0]=&field[0][0];
        data[1]=&field[1][0];
        data[2]=&field[2][0];
        data[3]=&field[3][0];
        data[4]=&field[4][0];
        data[5]=&field[5][0];
        data[6]=&field[6][0];

        g_snprintf(field[0], 32, "%u", number);
        g_snprintf(field[1], 32, "%s", time);
        g_snprintf(field[2], 32, "%f", delay);
        g_snprintf(field[3], 32, "%s", sideA);
        g_snprintf(field[4], 20, "%s", direction? "---->" : "<----");
        g_snprintf(field[5], 32, "%s", sideB);
        g_snprintf(field[6], 32, "%s", comment);

        added_row = gtk_clist_append(GTK_CLIST(clist1), data);
        gtk_clist_set_row_data(GTK_CLIST(clist1), added_row, GUINT_TO_POINTER(number));
        gtk_clist_set_background(GTK_CLIST(clist1), added_row, color);
}


/****************************************************************************/
/* whenever a h225 packet is seen by the tap listener */
static int h225_packet(void *user_data_arg, packet_info *pinfo, epan_dissect_t *edt _U_, void *h225info_arg)
{
	user_data_t *user_data = user_data_arg;
	h225_packet_info *h225ptr_info = h225info_arg;	
	GdkColor color = COLOR_DEFAULT;

        gchar timeStr[32];
        gchar message[32];
	guint32 src, dst;
	double delay;
	gint32 delay_sec, delay_usec;

	/* time since beginning of capture */
        g_snprintf(timeStr, sizeof(timeStr), "%d.%06d", pinfo->fd->rel_secs, pinfo->fd->rel_usecs);

	/* time since previous packet seen in tap listener */
	delay_sec = pinfo->fd->rel_secs - last_sec;
	delay_usec = pinfo->fd->rel_usecs - last_usec;
	
	delay = (double)delay_sec + ((double)delay_usec)/1000000;

	last_sec = pinfo->fd->rel_secs;
	last_usec = pinfo->fd->rel_usecs;

	switch (h225ptr_info->cs_type) {
		
		case H225_SETUP:
			g_snprintf(message, sizeof(message),"H225 Setup");
			break;
		case H225_CALL_PROCEDING:
			g_snprintf(message, sizeof(message),"H225 Call Proceding");
			break;
		case H225_ALERTING:
			g_snprintf(message, sizeof(message),"H225 Alerting");
			break;
		case H225_CONNECT:
			g_snprintf(message, sizeof(message),"H225 Connect");
			break;
		case H225_RELEASE_COMPLET:
			g_snprintf(message, sizeof(message),"H225 Release Complet");
			break;
		case H225_OTHER:
			g_snprintf(message, sizeof(message),"H225 Other");
		}

	g_memmove(&src, pinfo->src.data, 4);
        g_memmove(&dst, pinfo->dst.data, 4);

	if (src == user_data->ip_src)
		add_to_clist1(user_data->clist1,pinfo->fd->num,timeStr,delay,message, 1, "", "", &color);
	else
		add_to_clist1(user_data->clist1, pinfo->fd->num, timeStr,delay, "", 0, message, "", &color);

	return 0;
}


/****************************************************************************/
/* whenever a h245 packet is seen by the tap listener */
static int h245_packet(void *user_data_arg, packet_info *pinfo, epan_dissect_t *edt _U_, void *h245info_arg)
{

	user_data_t *user_data = user_data_arg;
	h245_packet_info *h245ptr_info = h245info_arg;	
	GdkColor color = COLOR_DEFAULT;

        gchar timeStr[32];
        gchar message[32];
	guint32 src, dst;
	double delay;
	gint32 delay_sec, delay_usec;

	/* time since beginning of capture */
        g_snprintf(timeStr, sizeof(timeStr), "%d.%06d", pinfo->fd->rel_secs, pinfo->fd->rel_usecs);

	/* time since previous packet seen in tap listener */
	delay_sec = pinfo->fd->rel_secs - last_sec;
	delay_usec = pinfo->fd->rel_usecs - last_usec;
	
	delay = (double)delay_sec + ((double)delay_usec)/1000000;

	last_sec = pinfo->fd->rel_secs;
	last_usec = pinfo->fd->rel_usecs;

	switch (h245ptr_info->msg_type) {
		
		case H245_TermCapSet:
			g_snprintf(message, sizeof(message),"H245 TermCapSet");
			break;
		case H245_TermCapSetAck:
			g_snprintf(message, sizeof(message),"H245_TermCapSetAck");
			break;
		case H245_TermCapSetRjc:
			g_snprintf(message, sizeof(message),"H245_TermCapSetRjc");
			break;
		case H245_TermCapSetRls:
			g_snprintf(message, sizeof(message),"H245_TermCapSetRls");
			break;
		case H245_OpenLogChn:
			g_snprintf(message, sizeof(message),"H245_OpenLogChn");
			break;
		case H245_OpenLogChnCnf:
			g_snprintf(message, sizeof(message),"H245_OpenLogChnCnf");
			break;
		case H245_OpenLogChnAck:
			g_snprintf(message, sizeof(message),"H245_OpenLogChnAck");
			break;
		case H245_OpenLogChnRjc:
			g_snprintf(message, sizeof(message),"H245_OpenLogChnRjc");
			break;
		case H245_CloseLogChn:
			g_snprintf(message, sizeof(message),"H245_CloseLogChn");
			break;
		case H245_CloseLogChnAck:
			g_snprintf(message, sizeof(message),"H245_CloseLogChnAck");
			break;
		case H245_MastSlvDet:
			g_snprintf(message, sizeof(message),"H245_MastSlvDet");
			break;
		case H245_MastSlvDetAck:
			g_snprintf(message, sizeof(message),"H245_MastSlvDetAck");
			break;
		case H245_MastSlvDetRjc:
			g_snprintf(message, sizeof(message),"H245_MastSlvDetRjc");
			break;
		case H245_MastSlvDetRls:
			g_snprintf(message, sizeof(message),"H245_MastSlvDetRls");
			break;
		case H245_OTHER:
			g_snprintf(message, sizeof(message),"H225 Other");
		}

	g_memmove(&src, pinfo->src.data, 4);
        g_memmove(&dst, pinfo->dst.data, 4);

	if (src == user_data->ip_src)
		add_to_clist1(user_data->clist1,pinfo->fd->num,timeStr,delay,message, 1, "", "", &color);
	else
		add_to_clist1(user_data->clist1, pinfo->fd->num, timeStr,delay, "", 0, message, "", &color);

	return 0;
}


/**************** Callbacks *************************************************/
/****************************************************************************/
/* XXX just copied from gtk/rpc_stat.c */
void protect_thread_critical_region(void);
void unprotect_thread_critical_region(void);


/****************************************************************************/
/* close the dialog window and remove the tap listener */
static void on_destroy(GtkWidget *win _U_, user_data_t *user_data _U_)
{
        g_free(user_data);
}

/****************************************************************************/
static void on_clist_select_row(GtkCList        *clist1 _U_,
                                gint             row _U_,
                                gint             column _U_,
                                GdkEvent        *event _U_,
                                user_data_t     *user_data _U_)
{
        user_data->selected_clist1 = clist1;
        user_data->selected_row = row;
}

/****************************************************************************/
static void on_goto_bt_clicked(GtkWidget *bt _U_, user_data_t *user_data _U_)
{
        guint fnumber;

        if (user_data->selected_clist1!=NULL) {
                fnumber = GPOINTER_TO_UINT(gtk_clist_get_row_data(
                        GTK_CLIST(user_data->selected_clist1), user_data->selected_row) );
               goto_frame(&cfile, fnumber);
        }
}


/****************************************************************************/
/* re-dissects all packets */
static void on_refresh_bt_clicked(GtkWidget *bt _U_, user_data_t *user_data _U_)
{
        gchar filter_text_h225[256];
        gchar filter_text_h245[256];
        /*gchar filter_text_rtp[256];*/
        dfilter_t *sfcode;
        GString *error_string;

        /* clear the dialog box clists */
        gtk_clist_clear(GTK_CLIST(user_data->clist1));

        /* try to compile the filter for h225 */
	g_snprintf(filter_text_h225,sizeof(filter_text_h225),
        "h225 && (( ip.src==%s && %s.srcport==%u && ip.dst==%s && %s.dstport==%u ) || ( ip.src==%s && %s.srcport==%u && ip.dst==%s && %s.dstport==%u ))",
                ip_to_str((ip_addr_p)&(user_data->ip_src)),
				transport_prot_name[user_data->transport],
                user_data->port_src,
                ip_to_str((ip_addr_p)&(user_data->ip_dst)),
				transport_prot_name[user_data->transport],
                user_data->port_dst,
                ip_to_str((ip_addr_p)&(user_data->ip_dst)),
				transport_prot_name[user_data->transport],
                user_data->port_dst,
                ip_to_str((ip_addr_p)&(user_data->ip_src)),
				transport_prot_name[user_data->transport],
                user_data->port_src
                );

        if (!dfilter_compile(filter_text_h225, &sfcode)) {
                simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, dfilter_error_msg);
                return;
        }

        /* try to compile the filter for h245 */
	g_snprintf(filter_text_h245,sizeof(filter_text_h245), 
        "h245 && (( ip.src==%s && %s.srcport==%u ) || ( ip.dst==%s && %s.dstport==%u ))",
                ip_to_str((ip_addr_p)&(user_data->ip_src_h245)),
				transport_prot_name[user_data->transport],
                user_data->port_src_h245,
                ip_to_str((ip_addr_p)&(user_data->ip_src_h245)),
				transport_prot_name[user_data->transport],
                user_data->port_src_h245
                );

        if (!dfilter_compile(filter_text_h245, &sfcode)) {
                simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, dfilter_error_msg);
                return;
        }

	/* register tap h225 listener */
        error_string = register_tap_listener("h225", user_data, filter_text_h225,
                h225_reset, h225_packet, h225_draw);
        if (error_string != NULL) {
                simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, error_string->str);
                        g_string_free(error_string, TRUE);
                return;
        }

	/* register tap h245 listener */
        error_string = register_tap_listener("h245", user_data, filter_text_h245,
                NULL, h245_packet, NULL);
        if (error_string != NULL) {
                simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, error_string->str);
                        g_string_free(error_string, TRUE);
                return;
        }

        /* retap all packets */
        retap_packets(&cfile);

        /* remove tap listener again */
        protect_thread_critical_region();
        remove_tap_listener(user_data);
        remove_tap_listener(user_data);
        unprotect_thread_critical_region();

}


/************************************************************************************/
/************** Create the dialog box with all widgets ******************************/
void create_h225_dialog(user_data_t* user_data)
{
        GtkWidget *window = NULL;
        GtkWidget *clist_h225;
        /*GtkWidget *label_stats;*/

        GtkWidget *main_vb;
        GtkWidget *label;
        GtkWidget *scrolled_window;
        GtkWidget *box4, *goto_bt, *close_bt, *refresh_bt;
        GtkTooltips *tooltips = gtk_tooltips_new();

        gchar label_forward[150];

        gchar str_ip_src[16];
        gchar str_ip_dst[16];

        /* as multiple analysis windows can be opened, 
         * don't use window_new_with_geom(), as that will place them on top of each other! */
	window = window_new(GTK_WINDOW_TOPLEVEL, "Ethereal: H.323 VoIP Analysis");
        gtk_window_set_default_size(GTK_WINDOW(window), 700, 350);

        /* Container for each row of widgets */
        main_vb = gtk_vbox_new(FALSE, 2);
        gtk_container_add(GTK_CONTAINER(window), main_vb);
        gtk_container_set_border_width(GTK_CONTAINER(main_vb), 2);
        gtk_widget_show(main_vb);

        strcpy(str_ip_src, ip_to_str((ip_addr_p)&user_data->ip_src));
        strcpy(str_ip_dst, ip_to_str((ip_addr_p)&user_data->ip_dst));

        g_snprintf(label_forward, 149,
                "\nAnalysing H.323 Call between  %s port %u (Side A) and  %s port %u (Side B)\n",
                str_ip_src, user_data->port_src, str_ip_dst, user_data->port_dst);

        /* label */
        label = gtk_label_new(label_forward);
        gtk_box_pack_start (GTK_BOX (main_vb), label, FALSE, FALSE, 0);

        /* scrolled window */
        scrolled_window = scrolled_window_new (NULL, NULL);
	gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window),
                                        GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
        gtk_box_pack_start (GTK_BOX (main_vb), scrolled_window, TRUE, TRUE, 0);

        /* place for some statistics */
        /*label_stats = gtk_label_new("\n");*/
        /*gtk_box_pack_start(GTK_BOX(main_vb), label_stats, FALSE, FALSE, 0);*/

        /* packet clist */
	clist_h225 = gtk_clist_new_with_titles(7, titles);
        gtk_container_add(GTK_CONTAINER(scrolled_window), clist_h225);
        gtk_widget_show(clist_h225);
	SIGNAL_CONNECT(clist_h225, "select_row", on_clist_select_row, user_data);

        /* column widths and justification */
        gtk_clist_set_column_width(GTK_CLIST(clist_h225), 0, 45);
        gtk_clist_set_column_width(GTK_CLIST(clist_h225), 1, 90);
        gtk_clist_set_column_width(GTK_CLIST(clist_h225), 2, 90);
        gtk_clist_set_column_width(GTK_CLIST(clist_h225), 3, 140);
        gtk_clist_set_column_width(GTK_CLIST(clist_h225), 4, 60);
        gtk_clist_set_column_width(GTK_CLIST(clist_h225), 5, 140);
        gtk_clist_set_column_justification(GTK_CLIST(clist_h225), 0, GTK_JUSTIFY_CENTER);
        gtk_clist_set_column_justification(GTK_CLIST(clist_h225), 1, GTK_JUSTIFY_CENTER);
        gtk_clist_set_column_justification(GTK_CLIST(clist_h225), 2, GTK_JUSTIFY_CENTER);
        gtk_clist_set_column_justification(GTK_CLIST(clist_h225), 3, GTK_JUSTIFY_LEFT);
        gtk_clist_set_column_justification(GTK_CLIST(clist_h225), 4, GTK_JUSTIFY_CENTER);
        gtk_clist_set_column_justification(GTK_CLIST(clist_h225), 5, GTK_JUSTIFY_LEFT);
        gtk_clist_set_column_justification(GTK_CLIST(clist_h225), 6, GTK_JUSTIFY_CENTER);

        gtk_widget_show(scrolled_window);

        /* buttons */
        box4 = gtk_hbutton_box_new();
        gtk_box_pack_start(GTK_BOX(main_vb), box4, FALSE, FALSE, 0);
        gtk_container_set_border_width(GTK_CONTAINER(box4), 10);
        gtk_button_box_set_layout(GTK_BUTTON_BOX (box4), GTK_BUTTONBOX_END);
        gtk_button_box_set_spacing(GTK_BUTTON_BOX (box4), 10);
        gtk_button_box_set_child_ipadding(GTK_BUTTON_BOX (box4), 4, 0);
        gtk_widget_show(box4);

	refresh_bt = BUTTON_NEW_FROM_STOCK(GTK_STOCK_REFRESH);
        gtk_container_add(GTK_CONTAINER(box4), refresh_bt);
        gtk_widget_show(refresh_bt);
        SIGNAL_CONNECT(refresh_bt, "clicked", on_refresh_bt_clicked, user_data);
        gtk_tooltips_set_tip (tooltips, refresh_bt, "Refresh data", NULL);

	goto_bt = BUTTON_NEW_FROM_STOCK(GTK_STOCK_JUMP_TO);
        gtk_container_add(GTK_CONTAINER(box4), goto_bt);
        gtk_widget_show(goto_bt);
        SIGNAL_CONNECT(goto_bt, "clicked", on_goto_bt_clicked, user_data);
        gtk_tooltips_set_tip (tooltips, goto_bt, "Jump to the selected packet", NULL);

        close_bt = BUTTON_NEW_FROM_STOCK(GTK_STOCK_CLOSE);
        gtk_container_add(GTK_CONTAINER(box4), close_bt);
	GTK_WIDGET_SET_FLAGS(close_bt, GTK_CAN_DEFAULT);
        gtk_widget_show(close_bt);
        gtk_tooltips_set_tip (tooltips, close_bt, "Close this dialog", NULL);
	window_set_cancel_button(window, close_bt, window_cancel_button_cb);

	SIGNAL_CONNECT(window, "delete_event", window_delete_event_cb, NULL);
        SIGNAL_CONNECT(window, "destroy", on_destroy, user_data);

	gtk_widget_show_all(window);
	window_present(window);

	/* some widget references need to be saved for outside use */
	user_data->window = window;
        user_data->clist1 = GTK_CLIST(clist_h225);
        user_data->label_stats = label;
        user_data->selected_clist1 = GTK_CLIST(clist_h225);
        user_data->selected_row = 0;
}


/****************************************************************************/
void h323_analysis(
                guint32 ip_src,
                guint16 port_src,
                guint32 ip_dst,
                guint16 port_dst,
                guint32 ip_src_h245,
                guint16 port_src_h245,
                guint16 transport
                )
{
        user_data_t *user_data;

        /* init */
        user_data = g_malloc(sizeof(user_data_t));

        user_data->ip_src = ip_src;
        user_data->port_src = port_src;
        user_data->ip_dst = ip_dst;
        user_data->port_dst = port_dst;
        user_data->ip_src_h245 = ip_src_h245;
        user_data->port_src_h245 = port_src_h245;
        user_data->transport = transport;

	/* create the dialog box */
        create_h225_dialog(user_data);

	/* proceed as if the Refresh button would have been pressed */
        on_refresh_bt_clicked(NULL, user_data);
}