aboutsummaryrefslogtreecommitdiffstats
path: root/packet-t38.c
blob: 3c8b9dbd736339dcf30277bcd55541a8b44868c8 (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
/* packet-t38.c
 * Routines for T.38 packet dissection
 * 2003  Hans Viens
 *
 * $Id: packet-t38.c,v 1.4 2004/01/09 23:24:54 guy Exp $
 *
 * 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.
 */

/* TO DO:
 * - Currently there is not support for T.38 IFP over TPKT over TCP, 
 *      just T.38 UDPTL over UDP and T.38 IFP packets directly over TCP.  
 * - Currently no TCP desegmentation is done.  
 * - SDP and H.245 dissectors should maybe be updated to start conversations for T.38 similar to RTP.
 * - It would be nice if we could dissect the T.30 data.
 * - Sometimes the last octet is not high-lighted when selecting something in the tree. Bug in PER dissector? 
 */


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

#include <glib.h>
#include <epan/packet.h>
#include <epan/conversation.h>

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

#include "prefs.h"
#include "ipproto.h"
#include "packet-per.h"
#include "prefs.h"

#define PORT_T38 6004  
static guint global_t38_tcp_port = PORT_T38;
static guint global_t38_udp_port = PORT_T38;

/*
* Variables to allow for proper deletion of dissector registration when
* the user changes port from the gui.
*/
static guint tcp_port = 0;
static guint udp_port = 0;

/* dissect using the Pre Corrigendum T.38 ASN.1 specification (1998) */
static gboolean use_pre_corrigendum_asn1_specification = TRUE;

/* dissect packets that looks like RTP version 2 packets as RTP     */
/* instead of as T.38. This may result in that some T.38 UPTL       */
/* packets with sequence number values higher than 32767 may be     */
/* shown as RTP packets.                                            */ 
/* XXX - this should be handled by having starting and ending       */
/* frames for conversations and by having H.245 close conversations */
/* and start new ones when appropriate.                             */
static gboolean dissect_possible_rtpv2_packets_as_rtp = FALSE;

static dissector_handle_t t38_handle;
static dissector_handle_t rtp_handle;

static guint32 Type_of_msg_value;
static guint32 Data_Field_field_type_value;
static guint32 Data_value;
static guint32 T30ind_value;

static int proto_t38 = -1;
static int hf_t38_IFPPacket = -1;
static int hf_t38_Type_of_msg = -1;
static int hf_t38_t30_indicator = -1;
static int hf_t38_data = -1;
static int hf_t38_Data_Field = -1;
static int hf_t38_Data_Field_item = -1;
static int hf_t38_Data_Field_field_type = -1;
static int hf_t38_Data_Field_field_data = -1;
static int hf_t38_UDPTLPacket = -1;
static int hf_t38_seq_number = -1;
static int hf_t38_primary_ifp_packet = -1;
static int hf_t38_primary_ifp_packet_length = -1;
static int hf_t38_error_recovery = -1;
static int hf_t38_secondary_ifp_packets = -1;
static int hf_t38_secondary_ifp_packets_item = -1;
static int hf_t38_secondary_ifp_packets_item_length = -1;
static int hf_t38_fec_info = -1;
static int hf_t38_fec_npackets = -1;
static int hf_t38_fec_data = -1;
static int hf_t38_fec_data_item = -1;

static gint ett_t38 = -1;
static gint ett_t38_IFPPacket = -1;
static gint ett_t38_Type_of_msg = -1;
static gint ett_t38_t30_indicator = -1;
static gint ett_t38_data = -1;
static gint ett_t38_Data_Field = -1;
static gint ett_t38_Data_Field_item = -1;
static gint ett_t38_Data_Field_field_type = -1;
static gint ett_t38_UDPTLPacket = -1;
static gint ett_t38_error_recovery = -1;
static gint ett_t38_secondary_ifp_packets = -1;
static gint ett_t38_fec_info = -1;
static gint ett_t38_fec_data = -1;

static gboolean primary_part = TRUE;
static guint32 seq_number = 0;


/* RTP Version is the first 2 bits of the first octet in the UDP payload*/
#define RTP_VERSION(octet)	((octet) >> 6)

void proto_reg_handoff_t38(void);


static int
dissect_t38_NULL(tvbuff_t *tvb _U_, int offset, packet_info *pinfo _U_, proto_tree *tree _U_)
{
	return offset;
}

static per_choice_t t30_indicator_choice[] = {
	{ 0, "no-signal", ASN1_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 1, "cng", ASN1_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 2, "ced", ASN1_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 3, "v21-preamble", ASN1_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 4, "v27-2400-training", ASN1_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 5, "v27-4800-training", ASN1_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 6, "v29-7200-training", ASN1_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 7, "v29-9600-training", ASN1_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 8, "v17-7200-short-training", ASN1_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 9, "v17-7200-long-training", ASN1_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 10, "v17-9600-short-training", ASN1_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 11, "v17-9600-long-training", ASN1_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 12, "v17-12000-short-training", ASN1_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 13, "v17-12000-long-training", ASN1_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 14, "v17-14400-short-training", ASN1_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 15, "v17-14400-long-training", ASN1_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 16, "v8-ansam", ASN1_NOT_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 17, "v8-signal", ASN1_NOT_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 18, "v34-cntl-channel-1200", ASN1_NOT_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 19, "v34-pri-channel", ASN1_NOT_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 20, "v34-CC-retrain", ASN1_NOT_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 21, "v33-12000-training", ASN1_NOT_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 22, "v33-14400-training", ASN1_NOT_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 0, NULL, 0, NULL }
};

static const value_string t30_indicator_vals[] = {
	{ 0, "no-signal" },
	{ 1, "cng" },
	{ 2, "ced" },
	{ 3, "v21-preamble" },
	{ 4, "v27-2400-training" },
	{ 5, "v27-4800-training" },
	{ 6, "v29-7200-training" },
	{ 7, "v29-9600-training" },
	{ 8, "v17-7200-short-training" },
	{ 9, "v17-7200-long-training" },
	{ 10, "v17-9600-short-training" },
	{ 11, "v17-9600-long-training" },
	{ 12, "v17-12000-short-training" },
	{ 13, "v17-12000-long-training" },
	{ 14, "v17-14400-short-training" },
	{ 15, "v17-14400-long-training" },
    { 16, "v8-ansam" },
    { 17, "v8-signal" },
    { 18, "v34-cntl-channel-1200" },
    { 19, "v34-pri-channel" },
    { 20, "v34-CC-retrain" },
    { 21, "v33-12000-training" },
    { 22, "v33-14400-training" },
	{ 0, NULL },
};

static int
dissect_t38_t30_indicator(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree)
{
    offset=dissect_per_choice(tvb, offset, pinfo,
        tree, hf_t38_t30_indicator, ett_t38_t30_indicator,
        t30_indicator_choice, "T30 Indicator", &T30ind_value);

	if (check_col(pinfo->cinfo, COL_INFO) && primary_part){
        col_append_fstr(pinfo->cinfo, COL_INFO, " t30ind: %s",
         val_to_str(T30ind_value,t30_indicator_vals,"<unknown>"));
	}
	return offset;
}

static per_choice_t data_choice[] = {
	{ 0, "v21", ASN1_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 1, "v27-2400", ASN1_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 2, "v27-4800", ASN1_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 3, "v29-7200", ASN1_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 4, "v29-9600", ASN1_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 5, "v17-7200", ASN1_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 6, "v17-9600", ASN1_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 7, "v17-12000", ASN1_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 8, "v17-14400", ASN1_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 9, "v8", ASN1_NOT_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 10, "v34-pri-rate", ASN1_NOT_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 11, "v34-CC-1200", ASN1_NOT_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 12, "v34-pri-ch", ASN1_NOT_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 13, "v33-12000", ASN1_NOT_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 14, "v33-14400", ASN1_NOT_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 0, NULL, 0, NULL }
};

static const value_string data_vals[] = {
	{ 0, "v21" },
	{ 1, "v27-2400" },
	{ 2, "v27-4800" },
	{ 3, "v29-7200" },
	{ 4, "v29-9600" },
	{ 5, "v17-7200" },
	{ 6, "v17-9600" },
	{ 7, "v17-12000" },
	{ 8, "v17-14400" },
	{ 9, "v8" },
	{ 10, "v34-pri-rate" },
	{ 11, "v34-CC-1200" },
	{ 12, "v34-pri-ch" },
	{ 13, "v33-12000" },
	{ 14, "v33-14400" },
	{ 0, NULL },
};

static int
dissect_t38_data(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree)
{
    offset=dissect_per_choice(tvb, offset, pinfo,
        tree, hf_t38_data, ett_t38_data,
        data_choice, "data", &Data_value);

    if (check_col(pinfo->cinfo, COL_INFO) && primary_part){
        col_append_fstr(pinfo->cinfo, COL_INFO, " data:%s:",
         val_to_str(Data_value,data_vals,"<unknown>"));
	}
	return offset;
}

static per_choice_t Type_of_msg_choice[] = {
	{ 0, "t30-indicator", ASN1_NO_EXTENSIONS,
		dissect_t38_t30_indicator},
	{ 1, "data", ASN1_NO_EXTENSIONS,
		dissect_t38_data},
	{ 0, NULL, 0, NULL }
};

static const value_string Type_of_msg_vals[] = {
	{ 0, "t30-indicator" },
	{ 1, "data" },
    { 0, NULL}
};

static int
dissect_t38_Type_of_msg(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree)
{
	offset=dissect_per_choice(tvb, offset, pinfo,
        tree, hf_t38_Type_of_msg, ett_t38_Type_of_msg,
        Type_of_msg_choice, "Type of message", &Type_of_msg_value);
	return offset;
}

static per_choice_t Data_Field_field_type_PreCorrigendum_choice[] = {
	{ 0, "hdlc-data", ASN1_NO_EXTENSIONS,
		dissect_t38_NULL},
	{ 1, "hdlc-sig-end", ASN1_NO_EXTENSIONS,
		dissect_t38_NULL},
	{ 2, "hdlc-fcs-OK", ASN1_NO_EXTENSIONS,
		dissect_t38_NULL},
	{ 3, "hdlc-fcs-BAD", ASN1_NO_EXTENSIONS,
		dissect_t38_NULL},
	{ 4, "hdlc-fcs-OK-sig-end", ASN1_NO_EXTENSIONS,
		dissect_t38_NULL},
	{ 5, "hdlc-fcs-BAD-sig-end", ASN1_NO_EXTENSIONS,
		dissect_t38_NULL},
	{ 6, "t4-non-ecm-data", ASN1_NO_EXTENSIONS,
		dissect_t38_NULL},
	{ 7, "t4-non-ecm-sig-end", ASN1_NO_EXTENSIONS,
		dissect_t38_NULL},
	{ 0, NULL, 0, NULL }
};


static per_choice_t Data_Field_field_type_choice[] = {
	{ 0, "hdlc-data", ASN1_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 1, "hdlc-sig-end", ASN1_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 2, "hdlc-fcs-OK", ASN1_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 3, "hdlc-fcs-BAD", ASN1_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 4, "hdlc-fcs-OK-sig-end", ASN1_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 5, "hdlc-fcs-BAD-sig-end", ASN1_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 6, "t4-non-ecm-data", ASN1_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 7, "t4-non-ecm-sig-end", ASN1_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 0, NULL, 0, NULL }
};


static const value_string Data_Field_field_type_vals[] = {
	{ 0, "hdlc-data" },
	{ 1, "hdlc-sig-end" },
	{ 2, "hdlc-fcs-OK" },
	{ 3, "hdlc-fcs-BAD" },
	{ 4, "hdlc-fcs-OK-sig-end" },
	{ 5, "hdlc-fcs-BAD-sig-end" },
	{ 6, "t4-non-ecm-data" },
	{ 7, "t4-non-ecm-sig-end" },
	{ 0, NULL },
};

static int
dissect_t38_Data_Field_field_type(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree)
{
	if(use_pre_corrigendum_asn1_specification){
		offset=dissect_per_choice(tvb, offset, pinfo,
			tree, hf_t38_Data_Field_field_type, ett_t38_Data_Field_field_type,
			Data_Field_field_type_PreCorrigendum_choice, "Field Type", &Data_Field_field_type_value);
	}
	else{
		offset=dissect_per_choice(tvb, offset, pinfo,
			tree, hf_t38_Data_Field_field_type, ett_t38_Data_Field_field_type,
			Data_Field_field_type_choice, "Field Type", &Data_Field_field_type_value);
	}

    if (check_col(pinfo->cinfo, COL_INFO) && primary_part){
        col_append_fstr(pinfo->cinfo, COL_INFO, " %s",
         val_to_str(Data_Field_field_type_value,Data_Field_field_type_vals,"<unknown>"));
	}

    return offset;
}

static int
dissect_t38_Data_Field_field_data(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree)
{
	guint32 value_offset = 0;
	guint32 value_len = 0;

	offset=dissect_per_octet_string(tvb, offset, pinfo,
        tree, hf_t38_Data_Field_field_data, 1, 65535,
        &value_offset, &value_len);

	if (check_col(pinfo->cinfo, COL_INFO) && primary_part){
        if(value_len < 8){
        	col_append_fstr(pinfo->cinfo, COL_INFO, "[%s]",
               tvb_bytes_to_str(tvb,value_offset,value_len));
        }
        else {
        	col_append_fstr(pinfo->cinfo, COL_INFO, "[%s...]",
               tvb_bytes_to_str(tvb,value_offset,7));
        }
	}
	return offset;
}

static per_sequence_t Data_Field_item_sequence[] = {
	{ "field-type", ASN1_NO_EXTENSIONS, ASN1_NOT_OPTIONAL,
		dissect_t38_Data_Field_field_type },
	{ "field-data", ASN1_NO_EXTENSIONS, ASN1_OPTIONAL,
		dissect_t38_Data_Field_field_data },
	{ NULL, 0, 0, NULL }
};

static int
dissect_t38_Data_Field_item(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree)
{
	offset=dissect_per_sequence(tvb, offset, pinfo,
        tree, hf_t38_Data_Field_item, ett_t38_Data_Field_item,
        Data_Field_item_sequence);
	return offset;
}

static int
dissect_t38_Data_Field(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree)
{
	offset=dissect_per_sequence_of(tvb, offset, pinfo,
        tree, hf_t38_Data_Field, ett_t38_Data_Field,
        dissect_t38_Data_Field_item);
	return offset;
}

static per_sequence_t IFPPacket_sequence[] = {
	{ "type-of-msg", ASN1_NO_EXTENSIONS, ASN1_NOT_OPTIONAL,
		dissect_t38_Type_of_msg },
	{ "data-field", ASN1_NO_EXTENSIONS, ASN1_OPTIONAL,
		dissect_t38_Data_Field },
	{ NULL, 0, 0, NULL }
};

static int
dissect_t38_IFPPacket(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree)
{
	offset=dissect_per_sequence(tvb, offset, pinfo,
        tree, hf_t38_IFPPacket, ett_t38_IFPPacket,
        IFPPacket_sequence);
	return offset;
}

static int
dissect_t38_seq_number(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree)
{
	offset=dissect_per_constrained_integer(tvb, offset, pinfo,
		tree, hf_t38_seq_number, 0, 65535,
		&seq_number, NULL, FALSE);

      if (check_col(pinfo->cinfo, COL_INFO)){
        col_append_fstr(pinfo->cinfo, COL_INFO, "Seq=%05u ",seq_number);
	}
	return offset;
}

static int
dissect_t38_primary_ifp_packet(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree)
{
    guint32 length;
	primary_part = TRUE;

    offset=dissect_per_length_determinant(tvb, offset, pinfo,
        tree, hf_t38_primary_ifp_packet_length, &length);
    offset=dissect_t38_IFPPacket(tvb, offset, pinfo, tree);
	return offset;
}

static int
dissect_t38_secondary_ifp_packets_item(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree)
{
    guint32 length;

    offset=dissect_per_length_determinant(tvb, offset, pinfo,
        tree, hf_t38_secondary_ifp_packets_item_length, &length);
    offset=dissect_t38_IFPPacket(tvb, offset, pinfo, tree);
	return offset;
}

static int
dissect_t38_secondary_ifp_packets(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree)
{
    /* When the field-data is not present, we MUST offset 1 byte*/
    if((Data_Field_field_type_value != 0) &&
       (Data_Field_field_type_value != 6))
    {
        offset=offset+8;
    }

    offset=dissect_per_sequence_of(tvb, offset, pinfo,
        tree, hf_t38_secondary_ifp_packets, ett_t38_secondary_ifp_packets,
        dissect_t38_secondary_ifp_packets_item);
	return offset;
}

static int
dissect_t38_fec_npackets(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree)
{
    offset=dissect_per_integer(tvb, offset, pinfo,
        tree, hf_t38_fec_npackets,
        NULL, NULL);
	return offset;
}

static int
dissect_t38_fec_data_item(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree)
{
    offset=dissect_per_octet_string(tvb, offset, pinfo,
        tree, hf_t38_fec_data_item, -1, -1,
        NULL, NULL);
	return offset;
}

static int
dissect_t38_fec_data(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree)
{
    offset=dissect_per_sequence_of(tvb, offset, pinfo,
        tree, hf_t38_fec_data, ett_t38_fec_data,
        dissect_t38_fec_data_item);
	return offset;
}

static per_sequence_t fec_info_sequence[] = {
	{ "fec-npackets", ASN1_NO_EXTENSIONS, ASN1_NOT_OPTIONAL,
		dissect_t38_fec_npackets },
	{ "fec-data", ASN1_NO_EXTENSIONS, ASN1_NOT_OPTIONAL,
		dissect_t38_fec_data },
	{ NULL, 0, 0, NULL }
};

static int
dissect_t38_fec_info(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree)
{
	offset=dissect_per_sequence(tvb, offset, pinfo,
        tree, hf_t38_fec_info, ett_t38_fec_info,
        fec_info_sequence);
	return offset;
}

static per_choice_t error_recovery_choice[] = {
	{ 0, "secondary-ifp-packets", ASN1_NO_EXTENSIONS,
		dissect_t38_secondary_ifp_packets},
	{ 1, "fec-info", ASN1_NO_EXTENSIONS,
		dissect_t38_fec_info},
	{ 0, NULL, 0, NULL }
};

static const value_string error_recovery_vals[] = {
	{ 0, "secondary-ifp-packets" },
	{ 1, "fec-info" },
    { 0, NULL}
};

static int
dissect_t38_error_recovery(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree)
{
	primary_part = FALSE;

    offset=dissect_per_choice(tvb, offset, pinfo,
        tree, hf_t38_error_recovery, ett_t38_error_recovery,
        error_recovery_choice, "Error recovery", NULL);

	primary_part = TRUE;

	return offset;
}

static per_sequence_t UDPTLPacket_sequence[] = {
	{ "seq-number", ASN1_NO_EXTENSIONS, ASN1_NOT_OPTIONAL,
		dissect_t38_seq_number },
	{ "primary-ifp-packet", ASN1_NO_EXTENSIONS, ASN1_NOT_OPTIONAL,
		dissect_t38_primary_ifp_packet },
	{ "error-recovery", ASN1_NO_EXTENSIONS, ASN1_NOT_OPTIONAL,
		dissect_t38_error_recovery },
	{ NULL, 0, 0, NULL }
};

static int
dissect_t38_UDPTLPacket(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree)
{
    /* Initialize to something else than data type */
    Data_Field_field_type_value = 1;

	offset=dissect_per_sequence(tvb, offset, pinfo,
        tree, hf_t38_UDPTLPacket, ett_t38_UDPTLPacket,
        UDPTLPacket_sequence);
    return offset;
}

/* Entry point for dissection */
static void
dissect_t38_udp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
{
	proto_item *it;
	proto_tree *tr;
    guint32 offset=0;

	if (check_col(pinfo->cinfo, COL_PROTOCOL)){
		col_set_str(pinfo->cinfo, COL_PROTOCOL, "T.38");
	}
	if (check_col(pinfo->cinfo, COL_INFO)){
		col_clear(pinfo->cinfo, COL_INFO);
	}

	it=proto_tree_add_protocol_format(tree, proto_t38, tvb, 0, tvb_length(tvb),
        "ITU-T Recommendation T.38");
	tr=proto_item_add_subtree(it, ett_t38);

	if (check_col(pinfo->cinfo, COL_INFO)){
        col_append_fstr(pinfo->cinfo, COL_INFO, "UDP: UDPTLPacket ");
	}

    offset=dissect_t38_UDPTLPacket(tvb, offset, pinfo, tr);

}

static void
dissect_t38_tcp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
{
    proto_item *it;
	proto_tree *tr;
    guint32 offset=0;

	if (check_col(pinfo->cinfo, COL_PROTOCOL)){
		col_set_str(pinfo->cinfo, COL_PROTOCOL, "T.38");
	}
	if (check_col(pinfo->cinfo, COL_INFO)){
		col_clear(pinfo->cinfo, COL_INFO);
	}

	it=proto_tree_add_protocol_format(tree, proto_t38, tvb, 0, tvb_length(tvb),
        "ITU-T Recommendation T.38");
	tr=proto_item_add_subtree(it, ett_t38);

	if (check_col(pinfo->cinfo, COL_INFO)){
        col_append_fstr(pinfo->cinfo, COL_INFO, "TCP: IFPPacket");
	}

    offset=dissect_t38_IFPPacket(tvb, offset, pinfo, tr);

}

static void
dissect_t38(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
{
	guint8      octet1;
	unsigned int offset = 0;
	primary_part = TRUE;

	if(pinfo->ipproto == IP_PROTO_TCP)
	{
		dissect_t38_tcp(tvb, pinfo, tree);
	}
	else if(pinfo->ipproto == IP_PROTO_UDP)
	{   
		if(dissect_possible_rtpv2_packets_as_rtp){
			octet1 = tvb_get_guint8( tvb, offset );
			if(RTP_VERSION(octet1) == 2){
                call_dissector(rtp_handle,tvb,pinfo,tree);
				return;
			}
		}
		dissect_t38_udp(tvb, pinfo, tree);
	}
}

/* Ethereal Protocol Registration */
void
proto_register_t38(void)
{
	static hf_register_info hf[] =
	{
        {  &hf_t38_IFPPacket,
            { "IFPPacket", "t38.IFPPacket", FT_NONE, BASE_NONE,
		      NULL, 0, "IFPPacket sequence", HFILL }},
        {  &hf_t38_Type_of_msg,
            { "Type of msg", "t38.Type_of_msg_type", FT_UINT32, BASE_DEC,
		      VALS(Type_of_msg_vals), 0, "Type_of_msg choice", HFILL }},
        {  &hf_t38_t30_indicator,
            { "T30 indicator", "t38.t30_indicator", FT_UINT32, BASE_DEC,
              VALS(t30_indicator_vals), 0, "t30_indicator", HFILL }},
        {  &hf_t38_data,
            { "data", "t38.t38_data", FT_UINT32, BASE_DEC,
              VALS(data_vals), 0, "data", HFILL }},
        {  &hf_t38_Data_Field,
            { "Data Field", "t38.Data_Field", FT_NONE, BASE_NONE,
              NULL, 0, "Data_Field sequence of", HFILL }},
        {  &hf_t38_Data_Field_item,
            { "Data_Field_item", "t38.Data_Field_item", FT_NONE, BASE_NONE,
              NULL, 0, "Data_Field_item sequence", HFILL }},
        {  &hf_t38_Data_Field_field_type,
            { "Data_Field_field_type", "t38.Data_Field_field_type", FT_UINT32, BASE_DEC,
              VALS(Data_Field_field_type_vals), 0, "Data_Field_field_type choice", HFILL }},
        {  &hf_t38_Data_Field_field_data,
            { "Data_Field_field_data", "t38.Data_Field_field_data", FT_BYTES, BASE_HEX,
            NULL, 0, "Data_Field_field_data octet string", HFILL }},
        {  &hf_t38_UDPTLPacket,
            { "UDPTLPacket", "t38.UDPTLPacket", FT_NONE, BASE_NONE,
		      NULL, 0, "UDPTLPacket sequence", HFILL }},
        {  &hf_t38_seq_number,
            { "Sequence number", "t38.seq_number", FT_UINT32, BASE_DEC,
		      NULL, 0, "seq_number", HFILL }},
        {  &hf_t38_primary_ifp_packet,
            { "Primary IFPPacket", "t38.primary_ifp_packet", FT_BYTES, BASE_HEX,
              NULL, 0, "primary_ifp_packet octet string", HFILL }},
        {  &hf_t38_primary_ifp_packet_length,
            { "primary_ifp_packet_length", "t38.primary_ifp_packet_length", FT_UINT32, BASE_DEC,
            NULL, 0, "primary_ifp_packet_length", HFILL }},
        {  &hf_t38_error_recovery,
            { "Error recovery", "t38.error_recovery", FT_UINT32, BASE_DEC,
		      VALS(error_recovery_vals), 0, "error_recovery choice", HFILL }},
        {  &hf_t38_secondary_ifp_packets,
            { "Secondary IFPPackets", "t38.secondary_ifp_packets", FT_NONE, BASE_NONE,
              NULL, 0, "secondary_ifp_packets sequence of", HFILL }},
        {  &hf_t38_secondary_ifp_packets_item,
            { "Secondary IFPPackets item", "t38.secondary_ifp_packets_item", FT_BYTES, BASE_HEX,
              NULL, 0, "secondary_ifp_packets_item octet string", HFILL }},
        {  &hf_t38_secondary_ifp_packets_item_length,
            { "secondary_ifp_packets_item_length", "t38.secondary_ifp_packets_item_length", FT_UINT32, BASE_DEC,
            NULL, 0, "secondary_ifp_packets_item_length", HFILL }},
        {  &hf_t38_fec_info,
            { "Fec info", "t38.fec_info", FT_NONE, BASE_NONE,
		      NULL, 0, "fec_info sequence", HFILL }},
        {  &hf_t38_fec_npackets,
            { "Fec npackets", "h245.fec_npackets", FT_INT32, BASE_DEC,
              NULL, 0, "fec_npackets value", HFILL }},
        {  &hf_t38_fec_data,
            { "Fec data", "t38.fec_data", FT_NONE, BASE_NONE,
              NULL, 0, "fec_data sequence of", HFILL }},
        {  &hf_t38_fec_data_item,
            { "t38_fec_data_item", "t38.t38_fec_data_item", FT_BYTES, BASE_HEX,
            NULL, 0, "t38_fec_data_item octet string", HFILL }},
	};

	static gint *ett[] =
	{
		&ett_t38,
        &ett_t38_IFPPacket,
        &ett_t38_Type_of_msg,
        &ett_t38_t30_indicator,
        &ett_t38_data,
        &ett_t38_Data_Field,
        &ett_t38_Data_Field_item,
        &ett_t38_Data_Field_field_type,
        &ett_t38_UDPTLPacket,
        &ett_t38_error_recovery,
        &ett_t38_secondary_ifp_packets,
        &ett_t38_fec_info,
        &ett_t38_fec_data,
	};
	module_t *t38_module;

	proto_t38 = proto_register_protocol("T38", "T38", "t38");
	proto_register_field_array(proto_t38, hf, array_length(hf));
	proto_register_subtree_array(ett, array_length(ett));
	register_dissector("t38", dissect_t38, proto_t38);

	t38_module = prefs_register_protocol(proto_t38, proto_reg_handoff_t38);
	prefs_register_bool_preference(t38_module, "use_pre_corrigendum_asn1_specification",
	    "Use the Pre-Corrigendum ASN.1 specification",
	    "Whether the T.38 dissector should decode using the Pre-Corrigendum T.38 "
		"ASN.1 specification (1998).",
	    &use_pre_corrigendum_asn1_specification);
	prefs_register_bool_preference(t38_module, "dissect_possible_rtpv2_packets_as_rtp",
	    "Dissect possible RTP version 2 packets with RTP dissector",
	    "Whether a UDP packet that looks like RTP version 2 packet will "
		"be dissected as RTP packet or T.38 packet. If enabled there is a risk that T.38 UDPTL "
		"packets with sequence number higher than 32767 may be dissected as RTP.",
	    &dissect_possible_rtpv2_packets_as_rtp);
	prefs_register_uint_preference(t38_module, "tcp.port",
		"T.38 TCP Port",
		"Set the TCP port for T.38 messages",
		10, &global_t38_tcp_port);
	prefs_register_uint_preference(t38_module, "udp.port",
		"T.38 UDP Port",
		"Set the UDP port for T.38 messages",
		10, &global_t38_udp_port);	

}

void
proto_reg_handoff_t38(void)
{
	static int t38_prefs_initialized = FALSE;

	if (!t38_prefs_initialized) {
		t38_handle=create_dissector_handle(dissect_t38, proto_t38);
		t38_prefs_initialized = TRUE;
	}
	else {
		dissector_delete("tcp.port", tcp_port, t38_handle);
		dissector_delete("udp.port", udp_port, t38_handle);
	}
	tcp_port = global_t38_tcp_port;
	udp_port = global_t38_udp_port;

	dissector_add("tcp.port", tcp_port, t38_handle);
	dissector_add("udp.port", udp_port, t38_handle);

	rtp_handle = find_dissector("rtp");
}