aboutsummaryrefslogtreecommitdiffstats
path: root/gtk/tap_rtp.c
blob: d015e5b3b1225766c4ddc0b9ce3eb7eef1467584 (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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
/*
 * tap_rtp.c
 *
 * $Id: tap_rtp.c,v 1.13 2003/05/28 01:09:57 gerald Exp $
 *
 * RTP analysing addition for ethereal
 *
 * Copyright 2003, 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.
 *
 * This tap works as follows:
 * When the user clicks on the RTP analisys button, we first check if it is a RTP packet.
 * If yes we store the SSRC, ip, and port values. Then the tap is registered and the 
 * redissect_packets() routine is called. So we go through all the RTP packets and search 
 * for SSRC of reversed connection (it has inversed socket parameters). If more than one 
 * is found a window is displayed where the user can select the appropriate from the list.
 * Rigth now we have the information about the converstion we are looking for (both SSRC). 
 * The redissect_packets() routine is called again. This time whenever a RTP packet with
 * matching SSRC values arrives, we store all the information we need (number, sequence
 * number, arrival time, ...) and compute the delay, jitter and wrong sequence number.
 * We add this values to CList. If the RTP packet carries voice in g711 alaw or ulaw, we
 * also store this voice information in a temp file. Window is displayed.
 * Then three buttons are available: Close, Refresh and Save voice.
 * The Refresh button calls the redissect_packets() routine again. It goes through the packets
 * again and does all the calculation again (if capturing in real time this means that some
 * more packets could come and can be computed in statistic). It also writes the sound
 * data again.
 * The Save voice button opens the dialog where we can choose the file name, format (not yet)
 * and direction we want to save. Currently it works only with g711 alaw and ulaw, and if the
 * length of captured packets is equal the length of packets on wire 
 *
 * To do:
 * - Support for saving voice in more different formats and with more different codecs:
 *   Since this should be portable to all OS, there is only possibility to save the 
 *   voice in a file and not play it directly through the sound card. There are enough 
 *   players on all platforms, that are doing right this. What about the format? 
 *   Currently there is only support for saving as an .au file (ulaw, 8000 Hz, 8bit)
 *   There are many players for this format on all platforms (for example Windows Media Player
 *   under Windows, command play under Linux). Support will be added for wav format and 
 *   possibility to save with two channels (separate channel for each direction)
 *
 * - Support for more codecs. Now you can save voice only if the codec is g.711 alaw or ulaw.
 *
 * - right now, the reversed connection must have the same (only inversed) ip and port numbers.
 *   I think that there is no reason that in special cases the reversed connection would not use 
 *   some different port or even the IP combination (please correct me if I am wrong). 
 *   So this will be added soon.
 *
 * - some more statistics (delay and jitter distribution)
 *
 * - GTK2 implementation
 *
 * - grammar correction
 * 
 * - some more testing (other OS)
 *
 * XXX Problems: 
 *
 * - problem with statistics for lost (late, duplicated) packets. How to make the statistic 
 *   more resistant to special (bizarre) arrival of sequence numbers
 */

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

#include <stdio.h>

#ifdef HAVE_SYS_TYPES_H
# include <sys/types.h>
#endif

#include <gtk/gtk.h>
#include "globals.h"
#include <string.h>
#include "epan/packet_info.h"
#include <epan/epan_dissect.h>
#include <epan/filesystem.h>
#include "../tap.h"
#include "../register.h"
#include "../packet-rtp.h"
#include "file_dlg.h"
#include "dlg_utils.h"
#include "ui_util.h"
#include "simple_dialog.h"
#include "menu.h"
#include "main.h"
#include <math.h>
#include "progress_dlg.h"
#include "compat_macros.h"
#include "../g711.h"
#include "../util.h"
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <fcntl.h>

#ifdef HAVE_IO_H
#include <io.h>	/* open/close on win32 */
#endif

#ifndef O_BINARY
#define O_BINARY 0
#endif

static GtkWidget *rtp_w = NULL;
static GtkWidget *save_voice_as_w = NULL;
static GtkWidget *main_vb;
static GtkWidget *clist;
static GtkWidget *clist_r;
static GtkWidget *max;
static GtkWidget *max_r;

static char f_tempname[128+1], r_tempname[128+1];

/* type of error when saving voice in a file didn't succeed */
typedef enum {
	TAP_RTP_WRONG_CODEC,
	TAP_RTP_WRONG_LENGTH,
	TAP_RTP_PADDING_ERROR,
	TAP_RTP_FILE_OPEN_ERROR,
	TAP_RTP_NO_DATA
} error_type_t; 

typedef enum {
	FIRST_PACKET,
	MARK_SET,
	NORMAL_PACKET
} packet_type;

/* structure that holds the information about the forward and reversed connection */
struct _info_direction {
	gboolean first_packet;
	guint16 seq_num;
	guint32 timestamp;
	guint32 delta_timestamp;
	double delay;
	double jitter;
	double time;
	double start_time;
	double max_delay;
	guint32 max_nr;
	guint16 start_seq_nr;
	guint16 stop_seq_nr;
	guint32 total_nr;
	guint32 sequence;
	gboolean under;
	gint cycles;
	FILE *fp;
	guint32 count;
	error_type_t error_type;
	gboolean saved;
};

/* structure that holds general information about the connection 
 * and structures for both directions */
typedef struct _info_stat {
	gchar source[16];
	gchar destination[16];
	guint16 srcport;
	guint16 dstport;
	guint32 ssrc_forward;
	guint32 ssrc_reversed;
	guint32 *ssrc_tmp;
	gboolean search_ssrc;
	guint reversed_ip;
	guint reversed_ip_and_port;
	struct _info_direction forward;
	struct _info_direction reversed;
} info_stat;

int do_calculation(gboolean direc, packet_type pkt_type, void *ptrs, void *vpri, void *vpinfo); 
static gboolean copy_file(gchar *, /*gint,*/ gint, void *);

/* when there is a [re]reading of packet's */
static void
rtp_reset(void *prs)
{
  info_stat *rs=prs;

  rs->forward.first_packet = TRUE;
  rs->reversed.first_packet = TRUE;
  rs->forward.max_delay = 0;
  rs->reversed.max_delay = 0;
  rs->forward.delay = 0;
  rs->reversed.delay = 0;
  rs->forward.jitter = 0;
  rs->reversed.jitter = 0;
  rs->forward.timestamp = 0;
  rs->reversed.timestamp = 0;
  rs->forward.max_nr = 0;
  rs->reversed.max_nr = 0;
  rs->forward.total_nr = 0;
  rs->reversed.total_nr = 0;
  rs->forward.sequence = 0;
  rs->reversed.sequence = 0;
  rs->forward.start_seq_nr = 0;
  rs->reversed.start_seq_nr = 1; /* 1 is ok (for statistics in reversed direction) */
  rs->forward.stop_seq_nr = 0;
  rs->reversed.stop_seq_nr = 0;
  rs->forward.cycles = 0;
  rs->reversed.cycles = 0;
  rs->forward.under = FALSE;
  rs->reversed.under = FALSE;
  rs->forward.saved = FALSE;
  rs->reversed.saved = FALSE;
  rs->forward.start_time = 0;
  rs->reversed.start_time = 0;
  rs->forward.time = 0;
  rs->reversed.time = 0;
  rs->forward.count = 0;
  rs->reversed.count = 0;
  /* XXX check for error at fclose? */
  rs->forward.fp = freopen(f_tempname, "wb", rs->forward.fp); 
  if (rs->forward.fp == NULL)
	rs->forward.error_type = TAP_RTP_FILE_OPEN_ERROR;
  rs->reversed.fp = freopen(r_tempname, "wb", rs->reversed.fp);
  if (rs->reversed.fp == NULL)
	rs->reversed.error_type = TAP_RTP_FILE_OPEN_ERROR;
  return;
}

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

/* when we are finished with redisection, we add the label for the statistic */
static void draw_stat(void *prs)
{
	info_stat *rs=prs;
	gchar label_max[200];
	guint32 f_expected = (rs->forward.stop_seq_nr + rs->forward.cycles*65536) 
							- rs->forward.start_seq_nr + 1;
	guint32 r_expected = (rs->reversed.stop_seq_nr + rs->reversed.cycles*65536) 
							- rs->reversed.start_seq_nr + 1;
	gint32 f_lost = f_expected - rs->forward.total_nr;
	gint32 r_lost = r_expected - rs->reversed.total_nr;

	g_snprintf(label_max, 199, "Max delay = %f sec at packet nr. %u \n\n"
		"Total RTP packets = %u   (expected %u)   Lost RTP packets = %d"  
		"   Sequence error = %u",
			rs->forward.max_delay, rs->forward.max_nr, rs->forward.total_nr, 
						f_expected, f_lost, rs->forward.sequence);

	gtk_label_set_text(GTK_LABEL(max), label_max);

	g_snprintf(label_max, 199, "Max delay = %f sec at packet nr. %u \n\n"
		"Total RTP packets = %u   (expected %u)   Lost RTP packets = %d"
		"   Sequence error = %u",
			 rs->reversed.max_delay, rs->reversed.max_nr, rs->reversed.total_nr, 
						r_expected, r_lost, rs->reversed.sequence);

	gtk_label_set_text(GTK_LABEL(max_r), label_max);

	/* could be done somewhere else, but can be here as well */
	/* if this is true, then we don't have any reversed connection, so the error type
	 * will be no data. This applies only the reversed connection */
	if (rs->reversed_ip_and_port == 0)
		rs->reversed.error_type = TAP_RTP_NO_DATA;

	return ;
}

/* append a line to clist */
/* XXX is there a nicer way to make these assignements? */
static void add_to_clist(gboolean forward, guint32 number, guint16 seq_num, 
				double delay, double jitter, gboolean status, gboolean marker)
{
	gchar *data[6];
	gchar field[6][30];

	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];

	g_snprintf(field[0], 20, "%u", number);
	g_snprintf(field[1], 20, "%u", seq_num);
	g_snprintf(field[2], 20, "%f", delay);
	g_snprintf(field[3], 20, "%f", jitter);
	g_snprintf(field[4], 20, "%s", marker? "SET" : "");
	g_snprintf(field[5], 29, "%s", status? "OK" : "NOK - Wrong sequence nr.");

	gtk_clist_append(GTK_CLIST(forward? clist : clist_r), data);

}

/* whenever a RTP packet is seen by the tap listener */
/* this function works as follows:
 * 1) packets that are not displayed are ignored
 *	return
 * 2) are we searching what could be the reversed connection (looking for reversed SSRC)
 *	if yes, do the parameters match (inversed IP and port combination from the forward one)?
 *		if yes, do we already have this SSRC stored
 *			if not store it
 * 3) if not, is current packet matching the forward direction
 *	if yes, call the function that does the calculation and saves the voice info
 * 4) if not, is current packet matching the reversed connection
 *	if yes, call the function that does the calculation and saves the voice info
 */
static int rtp_packet(void *prs, packet_info *pinfo, epan_dissect_t *edt _U_, void *vpri)
{
	info_stat *rs=prs;
	struct _rtp_info *pri=vpri;
	guint i;

	/* we ignore packets that are not displayed */
	if (pinfo->fd->flags.passed_dfilter == 0)
		return 0;

	/* are we looking for the SSRC of the reversed connection? */
	if (rs->search_ssrc != FALSE) {
		/* XXX what should be the rules for reversed connection? 
		 * 1. It should should have same inversed IP and port numbers
		 * 2. If none are found, only inversed IP's - is this possible?
		 * 3. If none are found, there isn't any reversed connection 
		 * XXX is it possible that the conversation is not P2P? 
		 * Curretly it works only if it matches the number 1. */

		/* have we found inverse parameters? */
		if ( strcmp(ip_to_str(pinfo->src.data), rs->destination) == 0  && 
				strcmp( ip_to_str(pinfo->dst.data), rs->source) == 0 ) {

			/* do the ports also match? */
			if ((rs->srcport == pinfo->destport) && (rs->dstport == pinfo->srcport)) {
				/* ok, the ip and port combination does match 
				 * do we already have this ssrc stored */
				for(i=0; i< rs->reversed_ip_and_port; i++) {
					if (pri->info_sync_src == *(rs->ssrc_tmp+i) ) 
						return 0;
				}
				
				/* no, we found new ssrc, let's store it */
				rs->ssrc_tmp = (guint32*)g_realloc(rs->ssrc_tmp, 
								(i+1)*sizeof(guint32));
				*(rs->ssrc_tmp+i) = pri->info_sync_src;
				rs->reversed_ip_and_port++;
				return 0;
			}
			/* no, only ip addresses match */
			/* XXX not implemented yet */
			else {
				rs->reversed_ip++;
				return 0;
			}

		}
	}
	
	/* ok, we are not looking for SSRC of the reversed connection */
	/* is it the forward direction?  */
	else if (rs->ssrc_forward == pri->info_sync_src)  {
		if (rs->forward.first_packet != FALSE) 
			/* first argument is the direction TRUE == forward */
			return do_calculation(TRUE, FIRST_PACKET, &rs->forward, pri, pinfo);
		else if (pri->info_marker_set != FALSE)
			return do_calculation(TRUE, MARK_SET, &rs->forward, pri, pinfo);
		else
			return do_calculation(TRUE, NORMAL_PACKET, &rs->forward, pri, pinfo);
	}
	/* is it the reversed direction? */
	else if (rs->ssrc_reversed == pri->info_sync_src) {
		if (rs->reversed.first_packet != FALSE) 
			return do_calculation(FALSE, FIRST_PACKET, &rs->reversed, pri, pinfo);
		else if (pri->info_marker_set != FALSE)
			return do_calculation(FALSE, MARK_SET, &rs->reversed, pri, pinfo);
		else
			return do_calculation(FALSE, NORMAL_PACKET, &rs->reversed, pri, pinfo);
	}

	return 0;
}


int do_calculation(gboolean direc, packet_type pkt_type, void *ptrs, void *vpri, void *vpinfo) {

	struct _info_direction *ptr=ptrs;
	struct _rtp_info *pri=vpri;
	packet_info *pinfo = vpinfo;
	guint i;
	double current_time;
	double current_jitter;
	guint8 *data;
	gint16 tmp;

	/* store the current time and calculate the current jitter */
	current_time = (double)pinfo->fd->rel_secs + (double) pinfo->fd->rel_usecs/1000000;
	current_jitter = ptr->jitter + ( fabs (current_time - (ptr->time) - 
		((double)(pri->info_timestamp)-(double)(ptr->timestamp))/8000)- ptr->jitter)/16;
	ptr->delay =  current_time-(ptr->time);

	/* We have 3 possibilities:
	 *  is this the first packet we got in this direction? */
	if (pkt_type == FIRST_PACKET) {
		ptr->first_packet = FALSE;
		ptr->start_seq_nr = pri->info_seq_num;
		ptr->start_time = current_time;
		add_to_clist(direc, pinfo->fd->num, pri->info_seq_num, 0,
				 pri->info_marker_set? TRUE: FALSE, TRUE, FALSE);
		if (ptr->fp == NULL) {
                	ptr->saved = FALSE;
                        ptr->error_type = TAP_RTP_FILE_OPEN_ERROR;
                }
		else
			ptr->saved = TRUE;
 	}
	/* or is it a packet with the mark bit set? */
	else if (pkt_type == MARK_SET) {
		ptr->delta_timestamp = pri->info_timestamp - ptr->timestamp;
		add_to_clist(direc, pinfo->fd->num, pri->info_seq_num, current_time - (ptr->time),
			 current_jitter, ptr->seq_num+1 == pri->info_seq_num? TRUE: FALSE, TRUE);
	}
	/* if neither then it is a "normal" packet pkt_type == NORMAL_PACKET */
	else {
		if (ptr->delay > ptr->max_delay) {
			ptr->max_delay = ptr->delay;
			ptr->max_nr = pinfo->fd->num;
		}
		add_to_clist(direc, pinfo->fd->num, pri->info_seq_num, current_time -(ptr->time),
			 current_jitter , ptr->seq_num+1 == pri->info_seq_num?TRUE:FALSE, FALSE);
	}

	/* When calculating expected rtp packets the seq number can wrap around
	 * so we have to count the number of cycles
	 * Variable cycles counts the wraps around in forwarding connection and
	 * under is flag that indicates where we are
	 *
	 * XXX how to determine number of cycles with all possible lost, late
	 * and duplicated packets without any doubt? It seems to me, that
	 * because of all possible combination of late, duplicated or lost
	 * packets, this can only be more or less good approximation
	 *
	 * There are some combinations (rare but theoretically possible),
	 * where below code won't work correctly - statistic may be wrong then.
	 */

	/* so if the current sequence number is less than the start one
	 * we assume, that there is another cycle running */
	if ((pri->info_seq_num < ptr->start_seq_nr) && (ptr->under == FALSE)){
		ptr->cycles++;
		ptr->under = TRUE;
	}
	/* what if the start seq nr was 0? Then the above condition will never
	 * be true, so we add another condition. XXX The problem would arise 
	 * if one of the packets with seq nr 0 or 65535 would be lost or late */
	else if ((pri->info_seq_num == 0) && (ptr->stop_seq_nr == 65535) &&
							(ptr->under == FALSE)){
		ptr->cycles++;
		ptr->under = TRUE;
	}
	/* the whole round is over, so reset the flag */
	else if ((pri->info_seq_num > ptr->start_seq_nr) && (ptr->under != FALSE)) {
       		ptr->under = FALSE;
        }

	/* Since it is difficult to count lost, duplicate or late packets separately, 
	 * we would like to know at least how many times the sequence number was not ok */

	/* if the current seq number equals the last one or if we are here for 
	 * the first time, then it is ok, we just store the current one as the last one */
	if ( ( ptr->seq_num+1 == pri->info_seq_num) || (pkt_type == FIRST_PACKET) )
		ptr->seq_num = pri->info_seq_num;
	/* if the first one is 65535. XXX same problem as above: if seq 65535 or 0 is lost... */
	else if ( (ptr->seq_num == 65535) && (pri->info_seq_num == 0) )
		ptr->seq_num = pri->info_seq_num;
	/* lost packets */
	else if (ptr->seq_num+1 < pri->info_seq_num) {
		ptr->seq_num = pri->info_seq_num;
		ptr->sequence++;
	}
	/* late or duplicated */
	else if (ptr->seq_num+1 > pri->info_seq_num)
		ptr->sequence++;

	ptr->time = current_time;
	ptr->timestamp = pri->info_timestamp;
	ptr->stop_seq_nr = pri->info_seq_num;
	ptr->total_nr++;

	/* save the voice information */
	/* if there was already an error, we quit */
        if (ptr->saved == FALSE)
        	return 0;

	/* if the captured length and packet length aren't equal, we quit
         * because there is some information missing */
        if (pinfo->fd->pkt_len != pinfo->fd->cap_len) {
		ptr->saved = FALSE;
		ptr->error_type = TAP_RTP_WRONG_LENGTH;
		return 0;
	}

	/* if padding bit is set, but the padding count is bigger
	 * then the whole RTP data - error with padding count */
        if ( (pri->info_padding_set != FALSE) &&
                      		(pri->info_padding_count > pri->info_payload_len) ) {
		ptr->saved = FALSE;
                ptr->error_type = TAP_RTP_PADDING_ERROR;
                return 0;
        }

	/* do we need to insert some silence? */
	if (pkt_type == MARK_SET) {
		/* the amount of silence should be the difference between
                 * the last timestamp and the current one minus x
                 * x should equal the amount of information in the last frame
                 * XXX not done yet */
		for(i=0; i < (ptr->delta_timestamp - pri->info_payload_len -
						pri->info_padding_count); i++) {
			tmp = (gint16 )ulaw2linear((unsigned char)(0x55));
			fwrite(&tmp, 2, 1, ptr->fp);
			ptr->count++;
		}
		fflush(ptr->fp);
	}

	/* is it the ulaw? */
	if (pri->info_payload_type == 0) {
		/* we put the pointer at the beggining of the RTP data, that is
		 * at the end of the current frame minus the length of the
		 * padding count minus length of the RTP data */
		data = cfile.pd + (pinfo->fd->pkt_len - pri->info_payload_len);
		for(i=0; i < (pri->info_payload_len - pri->info_padding_count); i++, data++) {
			tmp = (gint16 )ulaw2linear((unsigned char)*data);
			fwrite(&tmp, 2, 1, ptr->fp);
			ptr->count++;
		}
		fflush(ptr->fp);
		ptr->saved = TRUE;
		return 0;
	}

	/* alaw? */
	else if (pri->info_payload_type == 8) {
		data = cfile.pd + (pinfo->fd->pkt_len - pri->info_payload_len);
		for(i=0; i < (pri->info_payload_len - pri->info_padding_count); i++, data++) {
			tmp = (gint16 )alaw2linear((unsigned char)*data);
			fwrite(&tmp, 2, 1, ptr->fp);
			ptr->count++;
		}
		fflush(ptr->fp);
		ptr->saved = TRUE;
		return 0;
	}

	/* unsupported codec or XXX other error */
	else {
		ptr->saved = FALSE;
		ptr->error_type = TAP_RTP_WRONG_CODEC;
		return 0;
	}
}

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

/* here we close the rtp analysis dialog window and remove the tap listener */
static void rtp_destroy_cb(GtkWidget *win _U_, gpointer data _U_)
{
  info_stat *rs=(info_stat *)data;

  protect_thread_critical_region();
  remove_tap_listener(rs);
  unprotect_thread_critical_region();

  /* xxx is this enough? */
  g_free(rs->ssrc_tmp);
  g_free(rs);
 
  if (rs->forward.fp != NULL)
	fclose(rs->forward.fp);
  if (rs->reversed.fp != NULL)
	fclose(rs->reversed.fp);
  remove(f_tempname);
  remove(r_tempname);

  /* Is there a save voice window open? */
  if (save_voice_as_w != NULL)
	gtk_widget_destroy(save_voice_as_w);

  /* Note that we no longer have a "RTP Analyse" dialog box. */
  rtp_w = NULL;
}

/* when the close button in rtp window was clicked */
/* it seems to me that rtp_destroy_cb is automatically called, so we don't
 * need to do the g_free... and rtp_w = NULL ... */
static void rtp_destroy (GtkWidget *close_bt _U_, gpointer parent_w)
{
    gtk_grab_remove(GTK_WIDGET(parent_w));
    gtk_widget_destroy(GTK_WIDGET(parent_w));
}

/* we search the rtp.ssrc node here (thanks to Guy Harris - code here is magic for me */
static guint32 process_node(proto_item *ptree_node, header_field_info *hfinformation) 
{
  field_info            *finfo;
  proto_item            *proto_sibling_node;
  header_field_info     *hfssrc;
  guint32 ssrc;

  finfo = PITEM_FINFO(ptree_node);

  if (hfinformation==(finfo->hfinfo)) {
	hfssrc = proto_registrar_get_byname("rtp.ssrc");
	if (hfssrc == NULL)
		return 0;
	for(ptree_node=g_node_first_child(ptree_node); ptree_node!=NULL; 
				ptree_node=g_node_next_sibling(ptree_node)) {
		finfo=PITEM_FINFO(ptree_node);
		if (hfssrc==finfo->hfinfo) {
			ssrc = fvalue_get_integer(finfo->value);
			return ssrc;
		}
		}
  }

  proto_sibling_node = g_node_next_sibling(ptree_node);

  if (proto_sibling_node) {
	ssrc = process_node(proto_sibling_node, hfinformation);
	return ssrc;
  }
  else
	return 0;
}

/* here we search the rtp protocol */
static guint32 process_tree(proto_tree *protocol_tree)
{
  proto_item      *ptree_node;
  header_field_info     *hfinformation;

  hfinformation = proto_registrar_get_byname("rtp");
  if (hfinformation == NULL)
	return 0;

  ptree_node = g_node_first_child(protocol_tree);
  if (!ptree_node)
	return 0;

  return process_node(ptree_node, hfinformation);
}

/* when we want to update the information */
static void refresh_cb(GtkWidget *w _U_, void *pri)
{
  info_stat *rs=pri;

  gtk_clist_clear(GTK_CLIST(clist));
  gtk_clist_clear(GTK_CLIST(clist_r));
  redissect_packets(&cfile);
  draw_stat(rs);
}

static void save_voice_as_destroy_cb(GtkWidget *win _U_, gpointer user_data _U_)
{
  /* Note that we no longer have a Save voice info dialog box. */
  save_voice_as_w = NULL;
}

/* the user wants to save in a file */
/* XXX support for different formats is currently commented out */
static void save_voice_as_ok_cb(GtkWidget *ok_bt, gpointer fs)
{
  gchar *g_dest;
  /*GtkWidget *wav, *au, *sw;*/
  GtkWidget *rev, *forw, *both;
  info_stat *rs;
  gint channels /*, format*/;

  g_dest = g_strdup(gtk_file_selection_get_filename(GTK_FILE_SELECTION (fs)));

  /* Perhaps the user specified a directory instead of a file.
     Check whether they did. */
  if (test_for_directory(g_dest) == EISDIR) {
	/* It's a directory - set the file selection box to display it. */
	set_last_open_dir(g_dest);
	g_free(g_dest);
	gtk_file_selection_set_filename(GTK_FILE_SELECTION(fs), last_open_dir);
	return;
  }

  /*wav = (GtkWidget *)OBJECT_GET_DATA(ok_bt, "wav_rb");
  au = (GtkWidget *)OBJECT_GET_DATA(ok_bt, "au_rb");
  sw = (GtkWidget *)OBJECT_GET_DATA(ok_bt, "sw_rb");*/
  rev = (GtkWidget *)OBJECT_GET_DATA(ok_bt, "reversed_rb");
  forw = (GtkWidget *)OBJECT_GET_DATA(ok_bt, "forward_rb");
  both = (GtkWidget *)OBJECT_GET_DATA(ok_bt, "both_rb");
  rs = (info_stat *)OBJECT_GET_DATA(ok_bt, "info_stat");

  /* XXX user clicks the ok button, but we know we can't save the voice info because f.e.
   * we don't support that codec. So we pop up a warning. Maybe it would be better to
   * disable the ok button or disable the buttons for direction if only one is not ok. The
   * problem is if we open the save voice dialog and then click the refresh button and maybe 
   * the state changes, so we can't save anymore. In this case we should be able to update
   * the buttons. For now it is easier if we put the warning when the ok button is pressed.
   */

  /* we can not save in both dirctions */
  if ((rs->forward.saved == FALSE) && (rs->reversed.saved == FALSE) && (GTK_TOGGLE_BUTTON (both)->active)) {
	/* there are many combinations here, we just exit when first matches */
	if ((rs->forward.error_type == TAP_RTP_WRONG_CODEC) || 
					(rs->reversed.error_type == TAP_RTP_WRONG_CODEC))
		simple_dialog(ESD_TYPE_CRIT, NULL, 
			"Can't save in a file: Unsupported codec!");
	else if ((rs->forward.error_type == TAP_RTP_WRONG_LENGTH) || 
					(rs->reversed.error_type == TAP_RTP_WRONG_LENGTH))
		simple_dialog(ESD_TYPE_CRIT, NULL, 
			"Can't save in a file: Wrong length of captured packets!");
	else if ((rs->forward.error_type == TAP_RTP_PADDING_ERROR) || 
					(rs->reversed.error_type == TAP_RTP_PADDING_ERROR))
		simple_dialog(ESD_TYPE_CRIT, NULL, 
			"Can't save in a file: RTP data with padding!");
	else  
		simple_dialog(ESD_TYPE_CRIT, NULL, 
			"Can't save in a file: File I/O problem!");
	return;
  }
  /* we can not save forward direction */
  else if ((rs->forward.saved == FALSE) && ((GTK_TOGGLE_BUTTON (forw)->active) ||
						(GTK_TOGGLE_BUTTON (both)->active))) {  
	if (rs->forward.error_type == TAP_RTP_WRONG_CODEC)
                simple_dialog(ESD_TYPE_CRIT, NULL, 
		"Can't save forward direction in a file: Unsupported codec!");
        else if (rs->forward.error_type == TAP_RTP_WRONG_LENGTH)
                simple_dialog(ESD_TYPE_CRIT, NULL,
                "Can't save forward direction in a file: Wrong length of captured packets!");
        else if (rs->forward.error_type == TAP_RTP_PADDING_ERROR)
                simple_dialog(ESD_TYPE_CRIT, NULL, 
		"Can't save forward direction in a file: RTP data with padding!");
        else
                simple_dialog(ESD_TYPE_CRIT, NULL, 
		"Can't save forward direction in a file: File I/O problem!");
	return;
  }
  /* we can not save reversed direction */
  else if ((rs->reversed.saved == FALSE) && ((GTK_TOGGLE_BUTTON (rev)->active) ||
						(GTK_TOGGLE_BUTTON (both)->active))) {  
	if (rs->reversed.error_type == TAP_RTP_WRONG_CODEC)
                simple_dialog(ESD_TYPE_CRIT, NULL,
                "Can't save reversed direction in a file: Unsupported codec!");
        else if (rs->reversed.error_type == TAP_RTP_WRONG_LENGTH)
                simple_dialog(ESD_TYPE_CRIT, NULL,
                "Can't save reversed direction in a file: Wrong length of captured packets!");
        else if (rs->reversed.error_type == TAP_RTP_PADDING_ERROR)
                simple_dialog(ESD_TYPE_CRIT, NULL,
                "Can't save reversed direction in a file: RTP data with padding!");
        else if (rs->reversed.error_type == TAP_RTP_NO_DATA)
                simple_dialog(ESD_TYPE_CRIT, NULL,
                "Can't save reversed direction in a file: No RTP data!");
	else
                simple_dialog(ESD_TYPE_CRIT, NULL,
                "Can't save reversed direction in a file: File I/O problem!");
        return;
  }

  /*if (GTK_TOGGLE_BUTTON (wav)->active)
	format = 1;
  else if (GTK_TOGGLE_BUTTON (au)->active)
	format = 2;
  else if (GTK_TOGGLE_BUTTON (sw)->active)
	format = 3;*/

  if (GTK_TOGGLE_BUTTON (rev)->active)
	channels = 2;
  else if (GTK_TOGGLE_BUTTON (both)->active)
	channels = 3;
  else 
	channels = 1;

  if(!copy_file(g_dest, channels/*, format*/, rs)) {
	simple_dialog(ESD_TYPE_CRIT, NULL, "An error occured while saving voice in a file!");
	return;
  }

  /* XXX I get GTK warning (sometimes?)!!! */
  gtk_widget_destroy(GTK_WIDGET(save_voice_as_w));
}

/* when the user wants to save the voice information in a file */
/* XXX support for different formats is currently commented out */
static void save_voice_as_cb(GtkWidget *w _U_, gpointer data)
{
  info_stat *rs=(info_stat *)data;

  GtkWidget *vertb;
  GtkWidget *table1;
  GtkWidget *label_format;
  GtkWidget *channels_label;
  /*GSList *format_group = NULL;*/
  GSList *channels_group = NULL;
  GtkWidget *forward_rb;
  GtkWidget *reversed_rb;
  GtkWidget *both_rb;
  /*GtkWidget *wav_rb; GtkWidget *au_rb; GtkWidget *sw_rb;*/
  GtkWidget *ok_bt;

  /* if we can't save in a file: wrong codec, cut packets or other errors */
  /* shold the error arise here or later when you click ok button ? 
   * if we do it here, then we must disable the refresh button, so we don't do it here */

  if (save_voice_as_w != NULL) {
	/* There's already a Save voice info dialog box; reactivate it. */
	reactivate_window(save_voice_as_w);
	return;
  }
  
  save_voice_as_w = gtk_file_selection_new("Ethereal: Save Voice Data As");
  gtk_signal_connect(GTK_OBJECT(save_voice_as_w), "destroy",
       GTK_SIGNAL_FUNC(save_voice_as_destroy_cb), NULL);

  /* Container for each row of widgets */
  vertb = gtk_vbox_new(FALSE, 0);
  gtk_container_border_width(GTK_CONTAINER(vertb), 5);
  gtk_box_pack_start(GTK_BOX(GTK_FILE_SELECTION(save_voice_as_w)->action_area),
    vertb, FALSE, FALSE, 0);
  gtk_widget_show (vertb);

  table1 = gtk_table_new (2, 4, FALSE);
  gtk_widget_show (table1);
  gtk_box_pack_start (GTK_BOX (vertb), table1, FALSE, FALSE, 0);
  gtk_container_set_border_width (GTK_CONTAINER (table1), 10);
  gtk_table_set_row_spacings (GTK_TABLE (table1), 20);

  label_format = gtk_label_new ("Format: .au (ulaw, 8 bit, 8000 Hz, mono) ");
  gtk_widget_show (label_format);
  gtk_table_attach (GTK_TABLE (table1), label_format, 0, 3, 0, 1,
                    (GtkAttachOptions) (GTK_FILL),
                    (GtkAttachOptions) (0), 0, 0);

  /* we support .au - ulaw*/ 
/*  wav_rb = gtk_radio_button_new_with_label (format_group, ".wav");
  format_group = gtk_radio_button_group (GTK_RADIO_BUTTON (wav_rb));
  gtk_widget_show (wav_rb);
  gtk_table_attach (GTK_TABLE (table1), wav_rb, 1, 2, 0, 1,
                    (GtkAttachOptions) (GTK_FILL),
                    (GtkAttachOptions) (0), 0, 0);

  sw_rb = gtk_radio_button_new_with_label (format_group, "8 kHz, 16 bit  ");
  format_group = gtk_radio_button_group (GTK_RADIO_BUTTON (sw_rb));
  gtk_widget_show (sw_rb);
  gtk_table_attach (GTK_TABLE (table1), sw_rb, 2, 3, 0, 1,
                    (GtkAttachOptions) (GTK_FILL),
                    (GtkAttachOptions) (0), 0, 0);
  au_rb = gtk_radio_button_new_with_label (format_group, ".au");
  format_group = gtk_radio_button_group (GTK_RADIO_BUTTON (au_rb));
  gtk_widget_show (au_rb);
  gtk_table_attach (GTK_TABLE (table1), au_rb, 3, 4, 0, 1,
                    (GtkAttachOptions) (GTK_FILL),
                    (GtkAttachOptions) (0), 0, 0);
 */ 

  channels_label = gtk_label_new ("Channels:");
  gtk_widget_show (channels_label);
  gtk_table_attach (GTK_TABLE (table1), channels_label, 0, 1, 1, 2,
				(GtkAttachOptions) (GTK_FILL),
				(GtkAttachOptions) (0), 0, 0);
  gtk_misc_set_alignment (GTK_MISC (channels_label), 0, 0.5);

  forward_rb = gtk_radio_button_new_with_label (channels_group, "forward  ");
  channels_group = gtk_radio_button_group (GTK_RADIO_BUTTON (forward_rb));
  gtk_widget_show (forward_rb);
  gtk_table_attach (GTK_TABLE (table1), forward_rb, 1, 2, 1, 2,
			(GtkAttachOptions) (GTK_FILL),
			(GtkAttachOptions) (0), 0, 0);

  reversed_rb = gtk_radio_button_new_with_label (channels_group, "reversed");
  channels_group = gtk_radio_button_group (GTK_RADIO_BUTTON (reversed_rb));
  gtk_widget_show (reversed_rb);
  gtk_table_attach (GTK_TABLE (table1), reversed_rb, 2, 3, 1, 2,
			(GtkAttachOptions) (GTK_FILL),
			(GtkAttachOptions) (0), 0, 0);

  both_rb = gtk_radio_button_new_with_label (channels_group, "both");
  channels_group = gtk_radio_button_group (GTK_RADIO_BUTTON (both_rb));
  gtk_widget_show (both_rb);
  gtk_table_attach (GTK_TABLE (table1), both_rb, 3, 4, 1, 2,
			(GtkAttachOptions) (GTK_FILL),
			(GtkAttachOptions) (0), 0, 0);

  gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(both_rb), TRUE);

  /* if one direction is nok we don't allow saving 
  XXX this is not ok since the user can click the refresh button and cause changes
  but we can not update this window. So we move all the decision on the time the ok
  button is clicked
  if (rs->forward.saved == FALSE) {
	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(reversed_rb), TRUE);
	gtk_widget_set_sensitive(forward_rb, FALSE);
	gtk_widget_set_sensitive(both_rb, FALSE);
  }
  else if (rs->reversed.saved == FALSE) {
	gtk_widget_set_sensitive(reversed_rb, FALSE);
	gtk_widget_set_sensitive(both_rb, FALSE);
  }
  */

  ok_bt = GTK_FILE_SELECTION(save_voice_as_w)->ok_button;
  /*OBJECT_SET_DATA(ok_bt, "wav_rb", wav_rb);
  OBJECT_SET_DATA(ok_bt, "au_rb", au_rb);
  OBJECT_SET_DATA(ok_bt, "sw_rb", sw_rb);*/
  OBJECT_SET_DATA(ok_bt, "forward_rb", forward_rb);
  OBJECT_SET_DATA(ok_bt, "reversed_rb", reversed_rb);
  OBJECT_SET_DATA(ok_bt, "both_rb", both_rb);
  OBJECT_SET_DATA(ok_bt, "info_stat", rs);

  /* Connect the cancel_button to destroy the widget */
  SIGNAL_CONNECT_OBJECT(GTK_FILE_SELECTION(save_voice_as_w)->cancel_button,
                        "clicked", (GtkSignalFunc)gtk_widget_destroy,
                        save_voice_as_w);

  /* Catch the "key_press_event" signal in the window, so that we can catch
     the ESC key being pressed and act as if the "Cancel" button had
     been selected. */
  dlg_set_cancel(save_voice_as_w, GTK_FILE_SELECTION(save_voice_as_w)->cancel_button);
  
  gtk_signal_connect(GTK_OBJECT(ok_bt), "clicked",
		GTK_SIGNAL_FUNC(save_voice_as_ok_cb), save_voice_as_w);
  
  gtk_widget_show(save_voice_as_w);
}

/* all the graphics on the window is done here */
static void add_rtp_notebook(void *pri) 
{
  info_stat *rs=pri;

  GtkWidget *notebook, *page, *page_r, *label, *label1, *label2, *label3;
  GtkWidget *scrolled_window, *scrolled_window_r/*, *frame, *text, *label4, *page_help*/;
  GtkWidget *box4, *voice_bt, *refresh_bt, *close_bn;
  
  gchar *titles[6] =  {"Packet nr.", "Sequence",  "Delay (s)", "Jitter (s)", "Marker", "Status"};
  gchar label_forward[150];
  gchar label_reverse[150];

  g_snprintf(label_forward, 149, 
		"Analysing connection from  %s port %u  to  %s port %u   SSRC = %u\n", 
		rs->source, rs->srcport, rs->destination, rs->dstport, rs->ssrc_forward);
  g_snprintf(label_reverse, 149,
		"Analysing connection from  %s port %u  to  %s port %u   SSRC = %u\n", 
		rs->destination, rs->dstport, rs->source, rs->srcport, rs->ssrc_reversed);

  gtk_widget_destroy(main_vb);
  main_vb = gtk_vbox_new(FALSE, 3);
  gtk_container_border_width(GTK_CONTAINER(main_vb), 5);
  gtk_container_add(GTK_CONTAINER(rtp_w), main_vb);
  gtk_widget_show(main_vb);

  /* Start a nootbook for flipping between sets of changes */
  notebook = gtk_notebook_new();
  gtk_container_add(GTK_CONTAINER(main_vb), notebook);
  gtk_object_set_data(GTK_OBJECT(rtp_w), "notebook", notebook);

  /* page for forward connection */
  page = gtk_vbox_new(FALSE, 5);
  gtk_container_set_border_width(GTK_CONTAINER(page), 20);

  /* scrolled window */
  scrolled_window = gtk_scrolled_window_new(NULL, NULL);
  gtk_widget_set_usize(scrolled_window, 600, 200);
  gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window), 
					GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);

  /* direction label */
  label1 = gtk_label_new(label_forward);
  gtk_box_pack_start(GTK_BOX(page), label1, FALSE, FALSE, 0);

  /* place for some statistics */
  max = gtk_label_new("\n\n");
  gtk_box_pack_end(GTK_BOX(page), max, FALSE, FALSE, 5);

  /* clist for the information */
  clist = gtk_clist_new_with_titles(6, titles);
  gtk_widget_show(clist);
  gtk_container_add(GTK_CONTAINER(scrolled_window), clist);
  gtk_box_pack_start(GTK_BOX(page), scrolled_window, TRUE, TRUE, 0);

  /* and the label */
  label = gtk_label_new("     Forward Direction     ");
  gtk_notebook_append_page(GTK_NOTEBOOK(notebook), page, label);

  /* column width and justification */
  gtk_clist_set_column_width(GTK_CLIST(clist), 0, 80);
  gtk_clist_set_column_width(GTK_CLIST(clist), 1, 80);
  gtk_clist_set_column_width(GTK_CLIST(clist), 2, 80);
  gtk_clist_set_column_width(GTK_CLIST(clist), 3, 80);
  gtk_clist_set_column_width(GTK_CLIST(clist), 4, 40);
  gtk_clist_set_column_justification(GTK_CLIST(clist), 0, GTK_JUSTIFY_CENTER);
  gtk_clist_set_column_justification(GTK_CLIST(clist), 1, GTK_JUSTIFY_CENTER);
  gtk_clist_set_column_justification(GTK_CLIST(clist), 2, GTK_JUSTIFY_CENTER);
  gtk_clist_set_column_justification(GTK_CLIST(clist), 3, GTK_JUSTIFY_CENTER);
  gtk_clist_set_column_justification(GTK_CLIST(clist), 4, GTK_JUSTIFY_CENTER);
  gtk_clist_set_column_justification(GTK_CLIST(clist), 5, GTK_JUSTIFY_CENTER);

  /* same page for reversed connection */
  page_r = gtk_vbox_new(FALSE, 5);
  gtk_container_set_border_width(GTK_CONTAINER(page_r), 20);
  scrolled_window_r = gtk_scrolled_window_new(NULL, NULL);
  gtk_widget_set_usize(scrolled_window_r, 600, 200);
  gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window_r), 
				GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
  label3 = gtk_label_new(label_reverse);
  gtk_box_pack_start(GTK_BOX(page_r), label3, FALSE, FALSE, 0);
  max_r = gtk_label_new("\n\n");
  gtk_box_pack_end(GTK_BOX(page_r), max_r, FALSE, FALSE, 5);
  clist_r = gtk_clist_new_with_titles(6, titles);
  gtk_widget_show(clist_r);
  gtk_container_add(GTK_CONTAINER(scrolled_window_r), clist_r);
  gtk_box_pack_start(GTK_BOX(page_r), scrolled_window_r, TRUE, TRUE, 0);
  label2 = gtk_label_new("     Reversed Direction     ");
  gtk_notebook_append_page(GTK_NOTEBOOK(notebook), page_r, label2);

  gtk_clist_set_column_width(GTK_CLIST(clist_r), 0, 80);
  gtk_clist_set_column_width(GTK_CLIST(clist_r), 1, 80);
  gtk_clist_set_column_width(GTK_CLIST(clist_r), 2, 80);
  gtk_clist_set_column_width(GTK_CLIST(clist_r), 3, 80);
  gtk_clist_set_column_width(GTK_CLIST(clist_r), 4, 40);
  gtk_clist_set_column_justification(GTK_CLIST(clist_r), 0, GTK_JUSTIFY_CENTER);
  gtk_clist_set_column_justification(GTK_CLIST(clist_r), 1, GTK_JUSTIFY_CENTER);
  gtk_clist_set_column_justification(GTK_CLIST(clist_r), 2, GTK_JUSTIFY_CENTER);
  gtk_clist_set_column_justification(GTK_CLIST(clist_r), 3, GTK_JUSTIFY_CENTER);
  gtk_clist_set_column_justification(GTK_CLIST(clist_r), 4, GTK_JUSTIFY_CENTER);
  gtk_clist_set_column_justification(GTK_CLIST(clist_r), 5, GTK_JUSTIFY_CENTER);

  /* page for help&about or future 
  page_help = gtk_hbox_new(FALSE, 5);
  label4 = gtk_label_new("     Future    ");
  gtk_notebook_append_page(GTK_NOTEBOOK(notebook), page_help, label4);
  frame = gtk_frame_new("");
  text = gtk_label_new("\n\nMaybe some more statistics: delay and jitter distribution,...");
  gtk_label_set_justify(GTK_LABEL(text), GTK_JUSTIFY_LEFT);
  gtk_container_add(GTK_CONTAINER(frame), text);
  gtk_container_set_border_width(GTK_CONTAINER(frame), 20);
  gtk_box_pack_start(GTK_BOX(page_help), frame, TRUE, TRUE, 0);
*/
  /* show all notebooks */
  gtk_widget_show_all(notebook);

  /* and the buttons */
  box4 = gtk_hbutton_box_new();
  gtk_box_pack_start(GTK_BOX(main_vb), box4, FALSE, TRUE, 0);
  gtk_container_set_border_width(GTK_CONTAINER(box4), 10);
  gtk_button_box_set_layout(GTK_BUTTON_BOX(box4), GTK_BUTTONBOX_SPREAD);
  gtk_widget_show(box4);

  voice_bt = gtk_button_new_with_label("Save voice data as...");
  gtk_container_add(GTK_CONTAINER(box4), voice_bt);
  gtk_widget_show(voice_bt);
  gtk_signal_connect(GTK_OBJECT(voice_bt), "clicked",
		GTK_SIGNAL_FUNC(save_voice_as_cb), rs);

  refresh_bt = gtk_button_new_with_label("Refresh");
  gtk_container_add(GTK_CONTAINER(box4), refresh_bt);
  gtk_widget_show(refresh_bt);
  gtk_signal_connect(GTK_OBJECT(refresh_bt), "clicked",
		GTK_SIGNAL_FUNC(refresh_cb), rs);

  close_bn = gtk_button_new_with_label("Close");
  gtk_container_add(GTK_CONTAINER(box4), close_bn);
  gtk_widget_show(close_bn);
  gtk_signal_connect(GTK_OBJECT(close_bn), "clicked",
		GTK_SIGNAL_FUNC(rtp_destroy), GTK_OBJECT(rtp_w));

  redissect_packets(&cfile);

  draw_stat(rs);
}


/* when we click on the selected row it copies that ssrc value into ssrc_reversed */
static void get_selected_ssrc(GtkWidget *clist_r, gint row, gint column, 
						GdkEventButton *event _U_, gpointer data)
{
  info_stat *rs=(info_stat *)data;
  gchar *text;

  gtk_clist_get_text(GTK_CLIST(clist_r), row, column, &text);
  /* XXX is this strtoul portable for guint32? */
  rs->ssrc_reversed = strtoul(text, (char **)NULL, 10);
  return;
}

/* when we click apply button in ssrc reversed dialog */
static void apply_selected_ssrc(GtkWidget *w _U_, gpointer data)
{
  info_stat *rs=(info_stat *)data;
  add_rtp_notebook(rs);
}

/* this function goes through all the packets that have the same ip and port combination 
 * (only inversed) as the forward direction (XXX what if the reversed direction doesn't use 
 * the same ports???) and looks for different SSRC values. This can happen if you capture
 * two RTP conversations one after another from the same pair of phones (PC's). 
 * Both have same IP's and can also have same port numbers, so they (should) differ only 
 * in SSRC values. In such case we get a list of ssrc values and we have to choose the right 
 * one from the list. If there is only one or none, we do it automatically */ 
static void get_reversed_ssrc(void *prs)
{
	info_stat *ri = prs;
	GtkWidget *scroll_r, *clist_r, *ok_bt, *label, *label2, *label1, *main_hbnbox;
	gchar temp[150];
	guint i;

	switch(ri->reversed_ip_and_port)
	{
		/* in case we haven't found any reversed ssrc */
		/* XXX in this case we could look for the inversed IP only */
		case 0: {
			ri->ssrc_reversed = 0;
			ri->search_ssrc = FALSE;
			add_rtp_notebook(ri);
			return;
		}
		/* in case we found exactly one matching ssrc for reversed connection */ 
		case 1: { 
			ri->ssrc_reversed = ri->ssrc_tmp[0];
			ri->search_ssrc = FALSE;
			add_rtp_notebook(ri);
			return;
		}
		/* there is more then one matching ssrc, so we have to choose between them */
		default: {
			ri->search_ssrc = FALSE;
			/* let's draw the window */
			label = gtk_label_new("Found more SSRC values for the reversed\n"
						 "connection with following parameters:\n");
			g_snprintf(temp, 149, "Source %s port %u Destination %s port %u", 
					ri->destination, ri->dstport, ri->source, ri->srcport);
			label2 = gtk_label_new(temp);
			gtk_box_pack_start(GTK_BOX(main_vb), label, FALSE, FALSE, 0);
			gtk_box_pack_start(GTK_BOX(main_vb), label2, FALSE, FALSE, 0);
			scroll_r = gtk_scrolled_window_new(NULL, NULL);
			gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll_r), 
						GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
			clist_r = gtk_clist_new(1);
			gtk_clist_set_column_width(GTK_CLIST(clist_r), 0, 80);
			gtk_container_add(GTK_CONTAINER(scroll_r), clist_r);
			gtk_box_pack_start(GTK_BOX(main_vb), scroll_r, TRUE, TRUE, 0);
			label1 = gtk_label_new("Select one value and click apply");
			gtk_box_pack_start(GTK_BOX(main_vb), label1, FALSE, FALSE, 0);

			main_hbnbox = gtk_hbutton_box_new();
			gtk_box_pack_start(GTK_BOX(main_vb), main_hbnbox, FALSE, TRUE, 0);
			gtk_container_set_border_width(GTK_CONTAINER(main_hbnbox), 10);
			gtk_button_box_set_layout(GTK_BUTTON_BOX(main_hbnbox), 
								GTK_BUTTONBOX_SPREAD);
			gtk_widget_show(main_hbnbox);

			ok_bt = gtk_button_new_with_label("Apply");
			gtk_container_add(GTK_CONTAINER(main_hbnbox), ok_bt);
			gtk_signal_connect(GTK_OBJECT(clist_r), "select_row", 
					GTK_SIGNAL_FUNC(get_selected_ssrc), ri);
			gtk_signal_connect(GTK_OBJECT(ok_bt), "clicked", 
					GTK_SIGNAL_FUNC(apply_selected_ssrc), ri);

			/* add all the ssrc values in the clist */
			/* XXX I'm sure the tmp variable could be avoided here
			 * i tried to assign guint32 from ri->ssrc_tmp somehow to gchar **text
			 * but gave up. So if you can do this, just go ahead */
			for (i=0; i < ri->reversed_ip_and_port; i++) {
				gchar *text[1];
				gchar tmp[20];
				g_snprintf(tmp, 20, "%u", ri->ssrc_tmp[i]);
				text[0] = (gchar *)&tmp;
				gtk_clist_append(GTK_CLIST(clist_r), text);
			}
			
			gtk_clist_select_row(GTK_CLIST(clist_r), 0, 0);

			gtk_widget_show(label);
			gtk_widget_show(label1);
			gtk_widget_show(label2);
			gtk_widget_show(ok_bt);
			gtk_widget_show(clist_r);
			gtk_widget_show(scroll_r);
		}
	}
}

/* XXX only handles RTP over IPv4, should add IPv6 support */
/* when the user clicks the RTP dialog button */
static void rtp_analyse_cb(GtkWidget *w _U_, gpointer data _U_) 
{ 
  info_stat *rs;
  gchar filter_text[256];
  dfilter_t *sfcode;
  capture_file *cf;
  epan_dissect_t *edt;
  gint err;
  gboolean frame_matched;
  frame_data *fdata;
  GString *error_string;
  int fd;

  /* There's already a "Display Options" dialog box; reactivate it. */
  if (rtp_w != NULL) {
	reactivate_window(rtp_w);
	return;
  }

  /* Try to compile the filter. */
  strcpy(filter_text,"rtp && ip");
  if (!dfilter_compile(filter_text, &sfcode)) {
	simple_dialog(ESD_TYPE_CRIT, NULL, dfilter_error_msg);
	return;
  }
  /* we load the current file into cf variable */
  cf = &cfile;
  fdata = cf->current_frame;

  /* we are on the selected frame now */
  if (fdata == NULL)
	return; /* if we exit here it's an error */

  /* XXX instead of looking for RTP protocol like this, we could do the process_node() staff */
  /* dissect the current frame */
  wtap_seek_read(cf->wth, fdata->file_off, &cf->pseudo_header, cf->pd, fdata->cap_len, &err);
  edt = epan_dissect_new(TRUE, FALSE);
  epan_dissect_prime_dfilter(edt, sfcode);
  epan_dissect_run(edt, &cf->pseudo_header, cf->pd, fdata, &cf->cinfo);
  frame_matched = dfilter_apply_edt(sfcode, edt);

  /* if it is not an rtp frame, exit */
  frame_matched = dfilter_apply_edt(sfcode, edt);
  if (frame_matched != 1) {
	epan_dissect_free(edt);
	simple_dialog(ESD_TYPE_CRIT, NULL, "You didn't choose a RTP packet!");
	return;
	}

  /* in rs we put all the info */
  rs=g_malloc(sizeof(info_stat));	

  /* ok, it is a RTP frame, so let's get the ip and port values */
  rs->srcport = edt->pi.srcport;
  rs->dstport = edt->pi.destport;
  strncpy(rs->source, ip_to_str(edt->pi.src.data), 16);  
  strncpy(rs->destination, ip_to_str(edt->pi.dst.data), 16);  

  /* now we need the SSRC value of the current frame */
  rs->ssrc_forward = process_tree(edt->tree);
  if (rs->ssrc_forward == 0) {
	simple_dialog(ESD_TYPE_CRIT, NULL, "SSRC value couldn't be found!");
	return;
  }

  /* now we have all the information about the forwarding connection
   * we need to go through all the packets and search for reversed connection
   */
  rs->search_ssrc = TRUE;
  rs->ssrc_reversed = 0;	
  rs->reversed_ip = 0;
  rs->reversed_ip_and_port = 0;
  rs->ssrc_tmp = NULL;

  sprintf(filter_text,"rtp && ip && !icmp && (( ip.src==%s && udp.srcport==%d && ip.dst==%s && udp.dstport==%d ) || ( ip.src==%s && udp.srcport==%d && ip.dst==%s && udp.dstport==%d ))",
	  ip_to_str(edt->pi.src.data),
	  edt->pi.srcport,
	  ip_to_str(edt->pi.dst.data),
	  edt->pi.destport,
	  ip_to_str(edt->pi.dst.data),
	  edt->pi.destport,
	  ip_to_str(edt->pi.src.data),
	  edt->pi.srcport
	  );
/* XXX compiler warning:passing arg 5 of `register_tap_listener' from incompatible pointer type */
  error_string = register_tap_listener("rtp", rs, filter_text, rtp_reset, rtp_packet, rtp_draw);
  if (error_string != NULL) {
        simple_dialog(ESD_TYPE_WARN, NULL, error_string->str);
	/* XXX is this enough or do I have to free anything else? */
	g_string_free(error_string, TRUE);
	g_free(rs);
	exit(1);
  }

  /* let's draw the window */
  rtp_w = dlg_window_new("Ethereal: RTP Analyse");
  gtk_window_set_position (GTK_WINDOW (rtp_w), GTK_WIN_POS_CENTER);
  gtk_signal_connect(GTK_OBJECT(rtp_w), "destroy",
	GTK_SIGNAL_FUNC(rtp_destroy_cb), rs);

  /* Container for each row of widgets */
  main_vb = gtk_vbox_new(FALSE, 3);
  gtk_container_border_width(GTK_CONTAINER(main_vb), 5);
  gtk_container_add(GTK_CONTAINER(rtp_w), main_vb);
  gtk_widget_show(main_vb);

  /* file names for storing sound data */
  fd = create_tempfile(f_tempname, sizeof(f_tempname), "ethereal_rtp_fwd");
  rs->forward.fp = fdopen(fd, "wb");
  fd = create_tempfile(r_tempname, sizeof(f_tempname), "ethereal_rtp_rev");
  rs->reversed.fp = fdopen(fd, "wb");

  redissect_packets(cf);

  /* so how many reversed connection we have ? */
  get_reversed_ssrc(rs);

  /* and finally display this window */
  gtk_widget_show(rtp_w);
}

static void
rtp_analyse_init(char *dummy _U_)
{
	rtp_analyse_cb(NULL, NULL);
}

void
register_tap_listener_gtkrtp(void)
{
	register_ethereal_tap("rtp", rtp_analyse_init);
}

void
register_tap_menu_gtkrtp(void)
{
	register_tap_menu_item("RTP Analysis...", rtp_analyse_cb);
}


/* here we save it into a file that user specified */
/* XXX what about endians here? could go something wrong? */
static gboolean copy_file(gchar *dest, gint channels, /*gint format,*/ void *data)
{
	info_stat *rs=(info_stat *)data;
	int to_fd, forw_fd, rev_fd, fread = 0, rread = 0, fwritten, rwritten;
	gint16 f_pd;
	gint16 r_pd;
	gchar pd[1];
	guint32 f_write_silence = 0;
	guint32 r_write_silence = 0;
	progdlg_t *progbar;
	guint32 progbar_count, progbar_quantum, progbar_nextstep = 0, count = 0;
	gboolean stop_flag = FALSE;

	forw_fd = open(f_tempname, O_RDONLY | O_BINARY);
	if (forw_fd < 0) 
		return FALSE;
	rev_fd = open(r_tempname, O_RDONLY | O_BINARY);
	if (rev_fd < 0) {
		close(forw_fd); 
		return FALSE;
	}

	/* open file for saving */
	to_fd = open(dest, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
	if (to_fd < 0) {
		close(forw_fd);
		close(rev_fd);
		return FALSE;
	}

	progbar = create_progress_dlg("Saving voice in a file", dest, "Stop", &stop_flag);

	/* First we write the .au header. XXX Hope this is endian independant */
	/* the magic word 0x2e736e64 == .snd */
	*pd = (unsigned char)0x2e; write(to_fd, pd, 1);
	*pd = (unsigned char)0x73; write(to_fd, pd, 1);
	*pd = (unsigned char)0x6e; write(to_fd, pd, 1);
	*pd = (unsigned char)0x64; write(to_fd, pd, 1);
	/* header offset == 24 bytes */
	*pd = (unsigned char)0x00; write(to_fd, pd, 1);
	write(to_fd, pd, 1);
	write(to_fd, pd, 1);
	*pd = (unsigned char)0x18; write(to_fd, pd, 1);
	/* total length, it is permited to set this to 0xffffffff */
	*pd = (unsigned char)0xff; write(to_fd, pd, 1); 
	write(to_fd, pd, 1); 
	write(to_fd, pd, 1); 
	write(to_fd, pd, 1);
	/* encoding format == 8 bit ulaw */
	*pd = (unsigned char)0x00; write(to_fd, pd, 1);
	write(to_fd, pd, 1);
	write(to_fd, pd, 1);
	*pd = (unsigned char)0x01; write(to_fd, pd, 1);
	/* sample rate == 8000 Hz */
	*pd = (unsigned char)0x00; write(to_fd, pd, 1);
	write(to_fd, pd, 1);
	*pd = (unsigned char)0x1f; write(to_fd, pd, 1);
	*pd = (unsigned char)0x40; write(to_fd, pd, 1);
	/* channels == 1 */
	*pd = (unsigned char)0x00; write(to_fd, pd, 1);
	write(to_fd, pd, 1);
	write(to_fd, pd, 1);
	*pd = (unsigned char)0x01; write(to_fd, pd, 1);
	
	switch (channels) {
		/* only forward direction */
		case 1: {
			progbar_count = rs->forward.count;
			progbar_quantum = rs->forward.count/100;
			while ((fread = read(forw_fd, &f_pd, 2)) > 0) {
				if(stop_flag) 
					break;
				if((count > progbar_nextstep) && (count <= progbar_count)) {
					update_progress_dlg(progbar, 
						(gfloat) count/progbar_count, "Saving");
					progbar_nextstep = progbar_nextstep + progbar_quantum;
				}
				count++;
				*pd = (unsigned char)linear2ulaw(f_pd);
				fwritten = write(to_fd, pd, 1);
				if ((fwritten*2 < fread) || (fwritten < 0) || (fread < 0)) {
					close(forw_fd);
					close(rev_fd);
					close(to_fd);
					destroy_progress_dlg(progbar);
					return FALSE;
				}
			}
			break;
		}
		/* only reversed direction */
		case 2: {
			progbar_count = rs->reversed.count;
			progbar_quantum = rs->reversed.count/100;
			while ((rread = read(rev_fd, &r_pd, 2)) > 0) {
				if(stop_flag) 
					break;
				if((count > progbar_nextstep) && (count <= progbar_count)) {
					update_progress_dlg(progbar, 
						(gfloat) count/progbar_count, "Saving");
					progbar_nextstep = progbar_nextstep + progbar_quantum;
				}
				count++;
				*pd = (unsigned char)linear2ulaw(r_pd);
				rwritten = write(to_fd, pd, 1);
				if ((rwritten*2 < rread) || (rwritten < 0) || (rread < 0)) {
					close(forw_fd);
					close(rev_fd);
					close(to_fd);
					destroy_progress_dlg(progbar);
					return FALSE;
				}
			}
			break;
		}
		/* both directions */
		default: {
			(rs->forward.count > rs->reversed.count) ? 
					(progbar_count = rs->forward.count) : 
						(progbar_count = rs->reversed.count);
			progbar_quantum = progbar_count/100;
			/* since conversation in one way can start later than in the other one, 
			 * we have to write some silence information for one channel */
			if (rs->forward.start_time > rs->reversed.start_time) {
				f_write_silence = 
					(rs->forward.start_time-rs->reversed.start_time)*8000;
			}
			else if (rs->forward.start_time < rs->reversed.start_time) {
				r_write_silence = 
					(rs->reversed.start_time-rs->forward.start_time)*8000;
			}
			for(;;) {
				if(stop_flag) 
					break;
				if((count > progbar_nextstep) && (count <= progbar_count)) {
					update_progress_dlg(progbar, 
						(gfloat) count/progbar_count, "Saving");
					progbar_nextstep = progbar_nextstep + progbar_quantum;
				}
				count++;
				if(f_write_silence > 0) {
					rread = read(rev_fd, &r_pd, 2);
					f_pd = 0;
					fread = 1;
					f_write_silence--;
				}
				else if(r_write_silence > 0) {
					fread = read(forw_fd, &f_pd, 2);
					r_pd = 0;
					rread = 1;
					r_write_silence--;
				}
				else {
					fread = read(forw_fd, &f_pd, 2); 
					rread = read(rev_fd, &r_pd, 2);
				}
				if ((rread == 0) && (fread == 0)) 
					break;
				*pd = (unsigned char)linear2ulaw( (f_pd + r_pd)/2 );
				rwritten = write(to_fd, pd, 1);
				if ((rwritten < 0) || (rread < 0) || (fread < 0)) {
					close(forw_fd);
					close(rev_fd);
					close(to_fd);
					destroy_progress_dlg(progbar);
					return FALSE;
				}
			}
		}
	}
	destroy_progress_dlg(progbar);
	close(forw_fd);
	close(rev_fd);
	close(to_fd);
	return TRUE;

}