Try to squeeze some bytes out of the frame_data structure.
[gd/wireshark/.git] / epan / dissectors / packet-t38.c
1 /* Do not modify this file. Changes will be overwritten.                      */
2 /* Generated automatically by the ASN.1 to Wireshark dissector compiler       */
3 /* packet-t38.c                                                               */
4 /* asn2wrs.py -p t38 -c ./t38.cnf -s ./packet-t38-template -D . -O ../.. T38_2002.asn */
5
6 /* Input file: packet-t38-template.c */
7
8 #line 1 "./asn1/t38/packet-t38-template.c"
9 /* packet-t38.c
10  * Routines for T.38 packet dissection
11  * 2003  Hans Viens
12  * 2004  Alejandro Vaquero, add support Conversations for SDP
13  * 2006  Alejandro Vaquero, add T30 reassemble and dissection
14  *
15  * Wireshark - Network traffic analyzer
16  * By Gerald Combs <gerald@wireshark.org>
17  * Copyright 1998 Gerald Combs
18  *
19  * SPDX-License-Identifier: GPL-2.0-or-later
20  */
21
22
23 /* Depending on what ASN.1 specification is used you may have to change
24  * the preference setting regarding Pre-Corrigendum ASN.1 specification:
25  * http://www.itu.int/ITU-T/asn1/database/itu-t/t/t38/1998/T38.html  (Pre-Corrigendum=TRUE)
26  * http://www.itu.int/ITU-T/asn1/database/itu-t/t/t38/2003/T38(1998).html (Pre-Corrigendum=TRUE)
27  *
28  * http://www.itu.int/ITU-T/asn1/database/itu-t/t/t38/2003/T38(2002).html (Pre-Corrigendum=FALSE)
29  * http://www.itu.int/ITU-T/asn1/database/itu-t/t/t38/2002/t38.html  (Pre-Corrigendum=FALSE)
30  * http://www.itu.int/ITU-T/asn1/database/itu-t/t/t38/2002-Amd1/T38.html (Pre-Corrigendum=FALSE)
31  */
32
33 /* TO DO:
34  * - TCP desegmentation is currently not supported for T.38 IFP directly over TCP.
35  * - H.245 dissectors should be updated to start conversations for T.38 similar to RTP.
36  * - Sometimes the last octet is not high-lighted when selecting something in the tree. Bug in PER dissector?
37  * - Add support for RTP payload audio/t38 (draft-jones-avt-audio-t38-03.txt), i.e. T38 in RTP packets.
38  */
39
40
41 #include "config.h"
42
43 #include <epan/packet.h>
44 #include <epan/reassemble.h>
45 #include <epan/conversation.h>
46 #include <epan/tap.h>
47 #include <epan/expert.h>
48 #include <epan/strutil.h>
49 #include <epan/prefs.h>
50 #include <epan/ipproto.h>
51 #include <epan/asn1.h>
52 #include <epan/proto_data.h>
53
54 #include "packet-t38.h"
55 #include "packet-per.h"
56 #include "packet-tpkt.h"
57
58 void proto_register_t38(void);
59
60 static int t38_tap = -1;
61
62 /* dissect using the Pre Corrigendum T.38 ASN.1 specification (1998) */
63 static gboolean use_pre_corrigendum_asn1_specification = TRUE;
64
65 /* dissect packets that looks like RTP version 2 packets as RTP     */
66 /* instead of as T.38. This may result in that some T.38 UPTL       */
67 /* packets with sequence number values higher than 32767 may be     */
68 /* shown as RTP packets.                                            */
69 static gboolean dissect_possible_rtpv2_packets_as_rtp = FALSE;
70
71
72 /* Reassembly of T.38 PDUs over TPKT over TCP */
73 static gboolean t38_tpkt_reassembly = TRUE;
74
75 /* Preference setting whether TPKT header is used when sending T.38 over TCP.
76  * The default setting is Maybe where the dissector will look on the first
77  * bytes to try to determine whether TPKT header is used or not. This may not
78  * work so well in some cases. You may want to change the setting to Always or
79  * Newer.
80  */
81 #define T38_TPKT_NEVER 0   /* Assume that there is never a TPKT header    */
82 #define T38_TPKT_ALWAYS 1  /* Assume that there is always a TPKT header   */
83 #define T38_TPKT_MAYBE 2   /* Assume TPKT if first octets are 03-00-xx-xx */
84 static gint t38_tpkt_usage = T38_TPKT_MAYBE;
85
86 static const enum_val_t t38_tpkt_options[] = {
87   {"never", "Never", T38_TPKT_NEVER},
88   {"always", "Always", T38_TPKT_ALWAYS},
89   {"maybe", "Maybe", T38_TPKT_MAYBE},
90   {NULL, NULL, -1}
91 };
92
93
94
95 /* T38 */
96 static dissector_handle_t t38_udp_handle;
97 static dissector_handle_t t38_tcp_handle;
98 static dissector_handle_t t38_tcp_pdu_handle;
99 static dissector_handle_t rtp_handle;
100 static dissector_handle_t t30_hdlc_handle;
101 static dissector_handle_t data_handle;
102
103 static gint32 Type_of_msg_value;
104 static guint32 Data_Field_field_type_value;
105 static guint32 Data_value;
106 static guint32 T30ind_value;
107 static guint32 Data_Field_item_num;
108
109 static int proto_t38 = -1;
110
111 /*--- Included file: packet-t38-hf.c ---*/
112 #line 1 "./asn1/t38/packet-t38-hf.c"
113 static int hf_t38_IFPPacket_PDU = -1;             /* IFPPacket */
114 static int hf_t38_UDPTLPacket_PDU = -1;           /* UDPTLPacket */
115 static int hf_t38_type_of_msg = -1;               /* Type_of_msg */
116 static int hf_t38_data_field = -1;                /* Data_Field */
117 static int hf_t38_t30_indicator = -1;             /* T30_indicator */
118 static int hf_t38_t30_data = -1;                  /* T30_data */
119 static int hf_t38_Data_Field_item = -1;           /* Data_Field_item */
120 static int hf_t38_field_type = -1;                /* T_field_type */
121 static int hf_t38_field_data = -1;                /* T_field_data */
122 static int hf_t38_seq_number = -1;                /* T_seq_number */
123 static int hf_t38_primary_ifp_packet = -1;        /* T_primary_ifp_packet */
124 static int hf_t38_error_recovery = -1;            /* T_error_recovery */
125 static int hf_t38_secondary_ifp_packets = -1;     /* T_secondary_ifp_packets */
126 static int hf_t38_secondary_ifp_packets_item = -1;  /* OpenType_IFPPacket */
127 static int hf_t38_fec_info = -1;                  /* T_fec_info */
128 static int hf_t38_fec_npackets = -1;              /* INTEGER */
129 static int hf_t38_fec_data = -1;                  /* T_fec_data */
130 static int hf_t38_fec_data_item = -1;             /* OCTET_STRING */
131
132 /*--- End of included file: packet-t38-hf.c ---*/
133 #line 103 "./asn1/t38/packet-t38-template.c"
134
135 /* T38 setup fields */
136 static int hf_t38_setup        = -1;
137 static int hf_t38_setup_frame  = -1;
138 static int hf_t38_setup_method = -1;
139
140 /* T38 Data reassemble fields */
141 static int hf_t38_fragments = -1;
142 static int hf_t38_fragment = -1;
143 static int hf_t38_fragment_overlap = -1;
144 static int hf_t38_fragment_overlap_conflicts = -1;
145 static int hf_t38_fragment_multiple_tails = -1;
146 static int hf_t38_fragment_too_long_fragment = -1;
147 static int hf_t38_fragment_error = -1;
148 static int hf_t38_fragment_count = -1;
149 static int hf_t38_reassembled_in = -1;
150 static int hf_t38_reassembled_length = -1;
151
152 static gint ett_t38 = -1;
153
154 /*--- Included file: packet-t38-ett.c ---*/
155 #line 1 "./asn1/t38/packet-t38-ett.c"
156 static gint ett_t38_IFPPacket = -1;
157 static gint ett_t38_Type_of_msg = -1;
158 static gint ett_t38_Data_Field = -1;
159 static gint ett_t38_Data_Field_item = -1;
160 static gint ett_t38_UDPTLPacket = -1;
161 static gint ett_t38_T_error_recovery = -1;
162 static gint ett_t38_T_secondary_ifp_packets = -1;
163 static gint ett_t38_T_fec_info = -1;
164 static gint ett_t38_T_fec_data = -1;
165
166 /*--- End of included file: packet-t38-ett.c ---*/
167 #line 123 "./asn1/t38/packet-t38-template.c"
168 static gint ett_t38_setup = -1;
169
170 static gint ett_data_fragment = -1;
171 static gint ett_data_fragments = -1;
172
173 static expert_field ei_t38_malformed = EI_INIT;
174
175 static gboolean primary_part = TRUE;
176 static guint32 seq_number = 0;
177
178 /* Tables for reassembly of Data fragments. */
179 static reassembly_table data_reassembly_table;
180
181 static const fragment_items data_frag_items = {
182         /* Fragment subtrees */
183         &ett_data_fragment,
184         &ett_data_fragments,
185         /* Fragment fields */
186         &hf_t38_fragments,
187         &hf_t38_fragment,
188         &hf_t38_fragment_overlap,
189         &hf_t38_fragment_overlap_conflicts,
190         &hf_t38_fragment_multiple_tails,
191         &hf_t38_fragment_too_long_fragment,
192         &hf_t38_fragment_error,
193         &hf_t38_fragment_count,
194         /* Reassembled in field */
195         &hf_t38_reassembled_in,
196         /* Reassembled length field */
197         &hf_t38_reassembled_length,
198         /* Reassembled data field */
199         NULL,
200         /* Tag */
201         "Data fragments"
202 };
203
204 typedef struct _fragment_key {
205         address src;
206         address dst;
207         guint32 id;
208 } fragment_key;
209
210 static conversation_t *p_conv= NULL;
211 static t38_conv *p_t38_conv = NULL;
212 static t38_conv *p_t38_packet_conv = NULL;
213 static t38_conv_info *p_t38_conv_info = NULL;
214 static t38_conv_info *p_t38_packet_conv_info = NULL;
215
216 /* RTP Version is the first 2 bits of the first octet in the UDP payload*/
217 #define RTP_VERSION(octet)      ((octet) >> 6)
218
219 void proto_reg_handoff_t38(void);
220
221 static void show_setup_info(tvbuff_t *tvb, proto_tree *tree, t38_conv *p_t38_conv);
222 /* Preferences bool to control whether or not setup info should be shown */
223 static gboolean global_t38_show_setup_info = TRUE;
224
225 /* Can tap up to 4 T38 packets within same packet */
226 /* We only tap the primary part, not the redundancy */
227 #define MAX_T38_MESSAGES_IN_PACKET 4
228 static t38_packet_info t38_info_arr[MAX_T38_MESSAGES_IN_PACKET];
229 static int t38_info_current=0;
230 static t38_packet_info *t38_info=NULL;
231
232
233 /* Set up an T38 conversation */
234 void t38_add_address(packet_info *pinfo,
235                      address *addr, int port,
236                      int other_port,
237                      const gchar *setup_method, guint32 setup_frame_number)
238 {
239         address null_addr;
240         conversation_t* p_conversation;
241         t38_conv* p_conversation_data = NULL;
242
243         /*
244          * If this isn't the first time this packet has been processed,
245          * we've already done this work, so we don't need to do it
246          * again.
247          */
248         if ((pinfo->fd->visited) || (t38_udp_handle == NULL))
249         {
250                 return;
251         }
252
253         clear_address(&null_addr);
254
255         /*
256          * Check if the ip address and port combination is not
257          * already registered as a conversation.
258          */
259         p_conversation = find_conversation( setup_frame_number, addr, &null_addr, ENDPOINT_UDP, port, other_port,
260                                 NO_ADDR_B | (!other_port ? NO_PORT_B : 0));
261
262         /*
263          * If not, create a new conversation.
264          */
265         if ( !p_conversation || p_conversation->setup_frame != setup_frame_number) {
266                 p_conversation = conversation_new( setup_frame_number, addr, &null_addr, ENDPOINT_UDP,
267                                            (guint32)port, (guint32)other_port,
268                                                                    NO_ADDR2 | (!other_port ? NO_PORT2 : 0));
269         }
270
271         /* Set dissector */
272         conversation_set_dissector(p_conversation, t38_udp_handle);
273
274         /*
275          * Check if the conversation has data associated with it.
276          */
277         p_conversation_data = (t38_conv*)conversation_get_proto_data(p_conversation, proto_t38);
278
279         /*
280          * If not, add a new data item.
281          */
282         if ( ! p_conversation_data ) {
283                 /* Create conversation data */
284                 p_conversation_data = wmem_new(wmem_file_scope(), t38_conv);
285
286                 conversation_add_proto_data(p_conversation, proto_t38, p_conversation_data);
287         }
288
289         /*
290          * Update the conversation data.
291          */
292         g_strlcpy(p_conversation_data->setup_method, setup_method, MAX_T38_SETUP_METHOD_SIZE);
293         p_conversation_data->setup_frame_number = setup_frame_number;
294         p_conversation_data->src_t38_info.reass_ID = 0;
295         p_conversation_data->src_t38_info.reass_start_seqnum = -1;
296         p_conversation_data->src_t38_info.reass_data_type = 0;
297         p_conversation_data->src_t38_info.last_seqnum = -1;
298         p_conversation_data->src_t38_info.packet_lost = 0;
299         p_conversation_data->src_t38_info.burst_lost = 0;
300         p_conversation_data->src_t38_info.time_first_t4_data = 0;
301         p_conversation_data->src_t38_info.additional_hdlc_data_field_counter = 0;
302         p_conversation_data->src_t38_info.seqnum_prev_data_field = -1;
303
304         p_conversation_data->dst_t38_info.reass_ID = 0;
305         p_conversation_data->dst_t38_info.reass_start_seqnum = -1;
306         p_conversation_data->dst_t38_info.reass_data_type = 0;
307         p_conversation_data->dst_t38_info.last_seqnum = -1;
308         p_conversation_data->dst_t38_info.packet_lost = 0;
309         p_conversation_data->dst_t38_info.burst_lost = 0;
310         p_conversation_data->dst_t38_info.time_first_t4_data = 0;
311         p_conversation_data->dst_t38_info.additional_hdlc_data_field_counter = 0;
312         p_conversation_data->dst_t38_info.seqnum_prev_data_field = -1;
313 }
314
315
316 static fragment_head *
317 force_reassemble_seq(reassembly_table *table, packet_info *pinfo, guint32 id)
318 {
319         fragment_head *fd_head;
320         fragment_item *fd_i;
321         fragment_item *last_fd;
322         guint32 dfpos, size, packet_lost, burst_lost, seq_num;
323         guint8 *data;
324
325         fd_head = fragment_get(table, pinfo, id, NULL);
326
327         /* have we already seen this frame ?*/
328         if (pinfo->fd->visited) {
329                 if (fd_head != NULL && fd_head->flags & FD_DEFRAGMENTED) {
330                         return fd_head;
331                 } else {
332                         return NULL;
333                 }
334         }
335
336         if (fd_head==NULL){
337                 /* we must have it to continue */
338                 return NULL;
339         }
340
341         /* check for packet lost and count the burst of packet lost */
342         packet_lost = 0;
343         burst_lost = 0;
344         seq_num = 0;
345         for(fd_i=fd_head->next;fd_i;fd_i=fd_i->next) {
346                 if (seq_num != fd_i->offset) {
347                         packet_lost += fd_i->offset - seq_num;
348                         if ( (fd_i->offset - seq_num) > burst_lost ) {
349                                 burst_lost = fd_i->offset - seq_num;
350                         }
351                 }
352                 seq_num = fd_i->offset + 1;
353         }
354
355         /* we have received an entire packet, defragment it and
356          * free all fragments
357          */
358         size=0;
359         last_fd=NULL;
360         for(fd_i=fd_head->next;fd_i;fd_i=fd_i->next) {
361           if(!last_fd || last_fd->offset!=fd_i->offset){
362             size+=fd_i->len;
363           }
364           last_fd=fd_i;
365         }
366
367         data = (guint8 *) wmem_alloc(pinfo->pool, size);
368         fd_head->tvb_data = tvb_new_real_data(data, size, size);
369         fd_head->len = size;            /* record size for caller       */
370
371         /* add all data fragments */
372         dfpos = 0;
373         last_fd=NULL;
374         for (fd_i=fd_head->next;fd_i && fd_i->len + dfpos <= size;fd_i=fd_i->next) {
375           if (fd_i->len) {
376             if(!last_fd || last_fd->offset!=fd_i->offset){
377               tvb_memcpy(fd_i->tvb_data, data+dfpos, 0, fd_i->len);
378               dfpos += fd_i->len;
379             } else {
380               /* duplicate/retransmission/overlap */
381               fd_i->flags    |= FD_OVERLAP;
382               fd_head->flags |= FD_OVERLAP;
383               if( (last_fd->len!=fd_i->len)
384                   || tvb_memeql(last_fd->tvb_data, 0, tvb_get_ptr(fd_i->tvb_data, 0, last_fd->len), last_fd->len) ){
385                         fd_i->flags    |= FD_OVERLAPCONFLICT;
386                         fd_head->flags |= FD_OVERLAPCONFLICT;
387               }
388             }
389           }
390           last_fd=fd_i;
391         }
392
393         /* we have defragmented the pdu, now free all fragments*/
394         for (fd_i=fd_head->next;fd_i;fd_i=fd_i->next) {
395           if(fd_i->tvb_data){
396             tvb_free(fd_i->tvb_data);
397             fd_i->tvb_data=NULL;
398           }
399         }
400
401         /* mark this packet as defragmented */
402         fd_head->flags |= FD_DEFRAGMENTED;
403         fd_head->reassembled_in=pinfo->num;
404
405         col_append_fstr(pinfo->cinfo, COL_INFO, " (t4-data Reassembled: %d pack lost, %d pack burst lost)", packet_lost, burst_lost);
406
407         p_t38_packet_conv_info->packet_lost = packet_lost;
408         p_t38_packet_conv_info->burst_lost = burst_lost;
409
410         return fd_head;
411 }
412
413 /* T38 Routines */
414
415 /*--- Included file: packet-t38-fn.c ---*/
416 #line 1 "./asn1/t38/packet-t38-fn.c"
417
418 const value_string t38_T30_indicator_vals[] = {
419   {   0, "no-signal" },
420   {   1, "cng" },
421   {   2, "ced" },
422   {   3, "v21-preamble" },
423   {   4, "v27-2400-training" },
424   {   5, "v27-4800-training" },
425   {   6, "v29-7200-training" },
426   {   7, "v29-9600-training" },
427   {   8, "v17-7200-short-training" },
428   {   9, "v17-7200-long-training" },
429   {  10, "v17-9600-short-training" },
430   {  11, "v17-9600-long-training" },
431   {  12, "v17-12000-short-training" },
432   {  13, "v17-12000-long-training" },
433   {  14, "v17-14400-short-training" },
434   {  15, "v17-14400-long-training" },
435   {  16, "v8-ansam" },
436   {  17, "v8-signal" },
437   {  18, "v34-cntl-channel-1200" },
438   {  19, "v34-pri-channel" },
439   {  20, "v34-CC-retrain" },
440   {  21, "v33-12000-training" },
441   {  22, "v33-14400-training" },
442   { 0, NULL }
443 };
444
445
446 static int
447 dissect_t38_T30_indicator(tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
448   offset = dissect_per_enumerated(tvb, offset, actx, tree, hf_index,
449                                      16, &T30ind_value, TRUE, 7, NULL);
450
451 #line 29 "./asn1/t38/t38.cnf"
452     if (primary_part){
453         col_append_fstr(actx->pinfo->cinfo, COL_INFO, " t30ind: %s",
454          val_to_str(T30ind_value,t38_T30_indicator_vals,"<unknown>"));
455     }
456
457     /* info for tap */
458     if (primary_part)
459         t38_info->t30ind_value = T30ind_value;
460
461   return offset;
462 }
463
464
465 const value_string t38_T30_data_vals[] = {
466   {   0, "v21" },
467   {   1, "v27-2400" },
468   {   2, "v27-4800" },
469   {   3, "v29-7200" },
470   {   4, "v29-9600" },
471   {   5, "v17-7200" },
472   {   6, "v17-9600" },
473   {   7, "v17-12000" },
474   {   8, "v17-14400" },
475   {   9, "v8" },
476   {  10, "v34-pri-rate" },
477   {  11, "v34-CC-1200" },
478   {  12, "v34-pri-ch" },
479   {  13, "v33-12000" },
480   {  14, "v33-14400" },
481   { 0, NULL }
482 };
483
484
485 static int
486 dissect_t38_T30_data(tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
487   offset = dissect_per_enumerated(tvb, offset, actx, tree, hf_index,
488                                      9, &Data_value, TRUE, 6, NULL);
489
490 #line 41 "./asn1/t38/t38.cnf"
491     if (primary_part){
492         col_append_fstr(actx->pinfo->cinfo, COL_INFO, " data:%s:",
493          val_to_str(Data_value,t38_T30_data_vals,"<unknown>"));
494     }
495
496
497     /* info for tap */
498     if (primary_part)
499         t38_info->data_value = Data_value;
500
501   return offset;
502 }
503
504
505 static const value_string t38_Type_of_msg_vals[] = {
506   {   0, "t30-indicator" },
507   {   1, "t30-data" },
508   { 0, NULL }
509 };
510
511 static const per_choice_t Type_of_msg_choice[] = {
512   {   0, &hf_t38_t30_indicator   , ASN1_NO_EXTENSIONS     , dissect_t38_T30_indicator },
513   {   1, &hf_t38_t30_data        , ASN1_NO_EXTENSIONS     , dissect_t38_T30_data },
514   { 0, NULL, 0, NULL }
515 };
516
517 static int
518 dissect_t38_Type_of_msg(tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
519   offset = dissect_per_choice(tvb, offset, actx, tree, hf_index,
520                                  ett_t38_Type_of_msg, Type_of_msg_choice,
521                                  &Type_of_msg_value);
522
523 #line 22 "./asn1/t38/t38.cnf"
524   /* info for tap */
525   if (primary_part)
526     t38_info->type_msg = Type_of_msg_value;
527
528   return offset;
529 }
530
531
532 static const value_string t38_T_field_type_vals[] = {
533   {   0, "hdlc-data" },
534   {   1, "hdlc-sig-end" },
535   {   2, "hdlc-fcs-OK" },
536   {   3, "hdlc-fcs-BAD" },
537   {   4, "hdlc-fcs-OK-sig-end" },
538   {   5, "hdlc-fcs-BAD-sig-end" },
539   {   6, "t4-non-ecm-data" },
540   {   7, "t4-non-ecm-sig-end" },
541   {   8, "cm-message" },
542   {   9, "jm-message" },
543   {  10, "ci-message" },
544   {  11, "v34rate" },
545   { 0, NULL }
546 };
547
548
549 static int
550 dissect_t38_T_field_type(tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
551   offset = dissect_per_enumerated(tvb, offset, actx, tree, hf_index,
552                                      8, &Data_Field_field_type_value, (use_pre_corrigendum_asn1_specification)?FALSE:TRUE, (use_pre_corrigendum_asn1_specification)?0:4, NULL);
553
554 #line 61 "./asn1/t38/t38.cnf"
555     if (primary_part){
556         col_append_fstr(actx->pinfo->cinfo, COL_INFO, " %s",
557          val_to_str(Data_Field_field_type_value,t38_T_field_type_vals,"<unknown>"));
558     }
559
560     /* We only reassmeble packets in the Primary part and in the first two Items.                       */
561     /* There maybe be t38 packets with more than two Items, but reassemble those packets is not easy    */
562     /* using the current ressaemble functions.                                                          */
563     /* TODO: reassemble all the Items in one frame */
564     if (primary_part && (Data_Field_item_num<2)) {
565         if (Data_Field_field_type_value == 2 || Data_Field_field_type_value == 4 || Data_Field_field_type_value == 7) {/* hdlc-fcs-OK or hdlc-fcs-OK-sig-end or t4-non-ecm-sig-end*/
566             fragment_head *frag_msg = NULL;
567             tvbuff_t* new_tvb = NULL;
568             gboolean save_fragmented = actx->pinfo->fragmented;
569
570             actx->pinfo->fragmented = TRUE;
571
572             /* if reass_start_seqnum=-1 it means we have received the end of the fragmente, without received any fragment data */
573             if (p_t38_packet_conv_info->reass_start_seqnum != -1) {
574                 frag_msg = fragment_add_seq(&data_reassembly_table, /* reassembly table */
575                     tvb, offset, actx->pinfo,
576                     p_t38_packet_conv_info->reass_ID, /* ID for fragments belonging together */
577                     NULL,
578                     seq_number + Data_Field_item_num - (guint32)p_t38_packet_conv_info->reass_start_seqnum + (guint32)p_t38_packet_conv_info->additional_hdlc_data_field_counter,  /* fragment sequence number */
579                     /*0,*/
580                     0, /* fragment length */
581                     FALSE, /* More fragments */
582                     0);
583                 if ( Data_Field_field_type_value == 7 ) {
584                     /* if there was packet lost or other errors during the defrag then frag_msg is NULL. This could also means
585                      * there are out of order packets (e.g, got the tail frame t4-non-ecm-sig-end before the last fragment),
586                      * but we will assume there was packet lost instead, which is more usual. So, we are going to reassemble the packet
587                      * and get some stat, like packet lost and burst number of packet lost
588                     */
589                     if (!frag_msg) {
590                         force_reassemble_seq(&data_reassembly_table, /* reassembly table */
591                             actx->pinfo,
592                             p_t38_packet_conv_info->reass_ID /* ID for fragments belonging together */
593                         );
594                     } else {
595                         col_append_str(actx->pinfo->cinfo, COL_INFO, " (t4-data Reassembled: No packet lost)");
596
597                         g_snprintf(t38_info->desc_comment, MAX_T38_DESC, "No packet lost");
598                     }
599
600
601                     if (p_t38_packet_conv_info->packet_lost) {
602                         g_snprintf(t38_info->desc_comment, MAX_T38_DESC, " Pack lost: %d, Pack burst lost: %d", p_t38_packet_conv_info->packet_lost, p_t38_packet_conv_info->burst_lost);
603                     } else {
604                         g_snprintf(t38_info->desc_comment, MAX_T38_DESC, "No packet lost");
605                     }
606
607                     process_reassembled_data(tvb, offset, actx->pinfo,
608                                 "Reassembled T38", frag_msg, &data_frag_items, NULL, tree);
609
610                     /* Now reset fragmentation information in pinfo */
611                     actx->pinfo->fragmented = save_fragmented;
612
613                     t38_info->time_first_t4_data = p_t38_packet_conv_info->time_first_t4_data;
614                     t38_info->frame_num_first_t4_data = p_t38_packet_conv_info->reass_ID; /* The reass_ID is the Frame number of the first t4 fragment */
615
616                 } else {
617                     new_tvb = process_reassembled_data(tvb, offset, actx->pinfo,
618                                 "Reassembled T38", frag_msg, &data_frag_items, NULL, tree);
619
620                     /* Now reset fragmentation information in pinfo */
621                     actx->pinfo->fragmented = save_fragmented;
622
623                     if (new_tvb) call_dissector_with_data((t30_hdlc_handle) ? t30_hdlc_handle : data_handle, new_tvb, actx->pinfo, tree, t38_info);
624                 }
625             } else {
626                 proto_tree_add_expert_format(tree, actx->pinfo, &ei_t38_malformed, tvb, offset, tvb_reported_length_remaining(tvb, offset),
627                         "[RECEIVED END OF FRAGMENT W/OUT ANY FRAGMENT DATA]");
628                 col_append_str(actx->pinfo->cinfo, COL_INFO, " [Malformed?]");
629                 actx->pinfo->fragmented = save_fragmented;
630             }
631         }
632
633         /* reset the reassemble ID and the start seq number if it is not HDLC data */
634         if ( p_t38_conv && ( ((Data_Field_field_type_value >0) && (Data_Field_field_type_value <6)) || (Data_Field_field_type_value == 7) ) ){
635             p_t38_conv_info->reass_ID = 0;
636             p_t38_conv_info->reass_start_seqnum = -1;
637             p_t38_conv_info->additional_hdlc_data_field_counter = 0;
638             p_t38_conv_info->seqnum_prev_data_field = -1;
639         }
640         t38_info->Data_Field_field_type_value = Data_Field_field_type_value;
641     }
642
643   return offset;
644 }
645
646
647
648 static int
649 dissect_t38_T_field_data(tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
650 #line 151 "./asn1/t38/t38.cnf"
651     tvbuff_t *value_tvb = NULL;
652     guint32 value_len;
653
654   offset = dissect_per_octet_string(tvb, offset, actx, tree, hf_index,
655                                        1, 65535, FALSE, &value_tvb);
656
657     value_len = tvb_reported_length(value_tvb);
658
659
660
661 #line 158 "./asn1/t38/t38.cnf"
662     if (primary_part){
663         if(value_len < 8){
664             col_append_fstr(actx->pinfo->cinfo, COL_INFO, "[%s]",
665                tvb_bytes_to_str(wmem_packet_scope(), value_tvb,0,value_len));
666         }
667         else {
668             col_append_fstr(actx->pinfo->cinfo, COL_INFO, "[%s...]",
669                tvb_bytes_to_str(wmem_packet_scope(), value_tvb,0,7));
670         }
671     }
672
673     /* We only reassmeble packets in the Primary part and in the first two Items.                       */
674     /* There maybe be t38 packets with more than two Items, but reassemble those packets is not easy    */
675     /* using the current ressaemble functions.                                                          */
676     /* TODO: reassemble all the Items in one frame */
677     if (primary_part && (Data_Field_item_num<2)) {
678         fragment_head *frag_msg = NULL;
679
680         /* HDLC Data or t4-non-ecm-data */
681         if (Data_Field_field_type_value == 0 || Data_Field_field_type_value == 6) { /* 0=HDLC Data or 6=t4-non-ecm-data*/
682             gboolean save_fragmented = actx->pinfo->fragmented;
683
684             actx->pinfo->fragmented = TRUE;
685
686             /* if we have not reassembled this packet and it is the first fragment, reset the reassemble ID and the start seq number*/
687             if (p_t38_packet_conv && p_t38_conv && (p_t38_packet_conv_info->reass_ID == 0)) {
688                 /* we use the first fragment's frame_number as fragment ID because the protocol doesn't provide it */
689                     p_t38_conv_info->reass_ID = actx->pinfo->num;
690                     p_t38_conv_info->reass_start_seqnum = seq_number;
691                     p_t38_conv_info->time_first_t4_data = nstime_to_sec(&actx->pinfo->rel_ts);
692                     p_t38_conv_info->additional_hdlc_data_field_counter = 0;
693                     p_t38_packet_conv_info->reass_ID = p_t38_conv_info->reass_ID;
694                     p_t38_packet_conv_info->reass_start_seqnum = p_t38_conv_info->reass_start_seqnum;
695                     p_t38_packet_conv_info->seqnum_prev_data_field = p_t38_conv_info->seqnum_prev_data_field;
696                     p_t38_packet_conv_info->additional_hdlc_data_field_counter = p_t38_conv_info->additional_hdlc_data_field_counter;
697                     p_t38_packet_conv_info->time_first_t4_data = p_t38_conv_info->time_first_t4_data;
698             }
699             if (seq_number == (guint32)p_t38_packet_conv_info->seqnum_prev_data_field){
700                    p_t38_packet_conv_info->additional_hdlc_data_field_counter ++;
701                    if(p_t38_conv){
702                      p_t38_conv_info->additional_hdlc_data_field_counter =  p_t38_packet_conv_info->additional_hdlc_data_field_counter;
703                    }
704             }
705             frag_msg = fragment_add_seq(&data_reassembly_table,
706                 value_tvb, 0,
707                 actx->pinfo,
708                 p_t38_packet_conv_info->reass_ID, /* ID for fragments belonging together */
709                 NULL,
710                 seq_number - (guint32)p_t38_packet_conv_info->reass_start_seqnum + (guint32)p_t38_packet_conv_info->additional_hdlc_data_field_counter, /* fragment sequence number */
711                 value_len, /* fragment length */
712                 TRUE, /* More fragments */
713                 0);
714             p_t38_packet_conv_info->seqnum_prev_data_field = (gint32)seq_number;
715             process_reassembled_data(tvb, offset, actx->pinfo,
716                         "Reassembled T38", frag_msg, &data_frag_items, NULL, tree);
717
718             if (!frag_msg) { /* Not last packet of reassembled */
719                 if (Data_Field_field_type_value == 0) {
720                     col_append_fstr(actx->pinfo->cinfo, COL_INFO," (HDLC fragment %u)",
721                                                                      seq_number + (guint32)p_t38_packet_conv_info->additional_hdlc_data_field_counter
722                                                                        - (guint32)p_t38_packet_conv_info->reass_start_seqnum);
723                 } else {
724                     col_append_fstr(actx->pinfo->cinfo, COL_INFO," (t4-data fragment %u)", seq_number - (guint32)p_t38_packet_conv_info->reass_start_seqnum);
725                 }
726             }
727
728             /* Now reset fragmentation information in pinfo */
729             actx->pinfo->fragmented = save_fragmented;
730         }
731     }
732
733   return offset;
734 }
735
736
737 static const per_sequence_t Data_Field_item_sequence[] = {
738   { &hf_t38_field_type      , ASN1_NO_EXTENSIONS     , ASN1_NOT_OPTIONAL, dissect_t38_T_field_type },
739   { &hf_t38_field_data      , ASN1_NO_EXTENSIONS     , ASN1_OPTIONAL    , dissect_t38_T_field_data },
740   { NULL, 0, 0, NULL }
741 };
742
743 static int
744 dissect_t38_Data_Field_item(tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
745   offset = dissect_per_sequence(tvb, offset, actx, tree, hf_index,
746                                    ett_t38_Data_Field_item, Data_Field_item_sequence);
747
748 #line 53 "./asn1/t38/t38.cnf"
749     if (primary_part) Data_Field_item_num++;
750
751   return offset;
752 }
753
754
755 static const per_sequence_t Data_Field_sequence_of[1] = {
756   { &hf_t38_Data_Field_item , ASN1_NO_EXTENSIONS     , ASN1_NOT_OPTIONAL, dissect_t38_Data_Field_item },
757 };
758
759 static int
760 dissect_t38_Data_Field(tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
761   offset = dissect_per_sequence_of(tvb, offset, actx, tree, hf_index,
762                                       ett_t38_Data_Field, Data_Field_sequence_of);
763
764   return offset;
765 }
766
767
768 static const per_sequence_t IFPPacket_sequence[] = {
769   { &hf_t38_type_of_msg     , ASN1_NO_EXTENSIONS     , ASN1_NOT_OPTIONAL, dissect_t38_Type_of_msg },
770   { &hf_t38_data_field      , ASN1_NO_EXTENSIONS     , ASN1_OPTIONAL    , dissect_t38_Data_Field },
771   { NULL, 0, 0, NULL }
772 };
773
774 static int
775 dissect_t38_IFPPacket(tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
776   offset = dissect_per_sequence(tvb, offset, actx, tree, hf_index,
777                                    ett_t38_IFPPacket, IFPPacket_sequence);
778
779   return offset;
780 }
781
782
783
784 static int
785 dissect_t38_T_seq_number(tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
786   offset = dissect_per_constrained_integer(tvb, offset, actx, tree, hf_index,
787                                                             0U, 65535U, &seq_number, FALSE);
788
789 #line 238 "./asn1/t38/t38.cnf"
790     /* info for tap */
791     if (primary_part)
792         t38_info->seq_num = seq_number;
793
794     col_append_fstr(actx->pinfo->cinfo, COL_INFO, "Seq=%05u ",seq_number);
795
796   return offset;
797 }
798
799
800
801 static int
802 dissect_t38_T_primary_ifp_packet(tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
803 #line 246 "./asn1/t38/t38.cnf"
804     primary_part = TRUE;
805
806   offset = dissect_per_open_type(tvb, offset, actx, tree, hf_index, dissect_t38_IFPPacket);
807
808 #line 248 "./asn1/t38/t38.cnf"
809     /* if is a valid t38 packet, add to tap */
810     /* Note that t4-non-ecm-sig-end without first_t4_data is not valid */
811     if (p_t38_packet_conv && (!actx->pinfo->flags.in_error_pkt) && ((gint32) seq_number != p_t38_packet_conv_info->last_seqnum) &&
812         !(t38_info->type_msg == 1 && t38_info->Data_Field_field_type_value == 7 && t38_info->frame_num_first_t4_data == 0))
813         tap_queue_packet(t38_tap, actx->pinfo, t38_info);
814
815     if (p_t38_conv) p_t38_conv_info->last_seqnum = (gint32) seq_number;
816
817   return offset;
818 }
819
820
821
822 static int
823 dissect_t38_OpenType_IFPPacket(tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
824   offset = dissect_per_open_type(tvb, offset, actx, tree, hf_index, dissect_t38_IFPPacket);
825
826   return offset;
827 }
828
829
830 static const per_sequence_t T_secondary_ifp_packets_sequence_of[1] = {
831   { &hf_t38_secondary_ifp_packets_item, ASN1_NO_EXTENSIONS     , ASN1_NOT_OPTIONAL, dissect_t38_OpenType_IFPPacket },
832 };
833
834 static int
835 dissect_t38_T_secondary_ifp_packets(tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
836   offset = dissect_per_sequence_of(tvb, offset, actx, tree, hf_index,
837                                       ett_t38_T_secondary_ifp_packets, T_secondary_ifp_packets_sequence_of);
838
839   return offset;
840 }
841
842
843
844 static int
845 dissect_t38_INTEGER(tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
846   offset = dissect_per_integer(tvb, offset, actx, tree, hf_index, NULL);
847
848   return offset;
849 }
850
851
852
853 static int
854 dissect_t38_OCTET_STRING(tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
855   offset = dissect_per_octet_string(tvb, offset, actx, tree, hf_index,
856                                        NO_BOUND, NO_BOUND, FALSE, NULL);
857
858   return offset;
859 }
860
861
862 static const per_sequence_t T_fec_data_sequence_of[1] = {
863   { &hf_t38_fec_data_item   , ASN1_NO_EXTENSIONS     , ASN1_NOT_OPTIONAL, dissect_t38_OCTET_STRING },
864 };
865
866 static int
867 dissect_t38_T_fec_data(tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
868   offset = dissect_per_sequence_of(tvb, offset, actx, tree, hf_index,
869                                       ett_t38_T_fec_data, T_fec_data_sequence_of);
870
871   return offset;
872 }
873
874
875 static const per_sequence_t T_fec_info_sequence[] = {
876   { &hf_t38_fec_npackets    , ASN1_NO_EXTENSIONS     , ASN1_NOT_OPTIONAL, dissect_t38_INTEGER },
877   { &hf_t38_fec_data        , ASN1_NO_EXTENSIONS     , ASN1_NOT_OPTIONAL, dissect_t38_T_fec_data },
878   { NULL, 0, 0, NULL }
879 };
880
881 static int
882 dissect_t38_T_fec_info(tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
883   offset = dissect_per_sequence(tvb, offset, actx, tree, hf_index,
884                                    ett_t38_T_fec_info, T_fec_info_sequence);
885
886   return offset;
887 }
888
889
890 static const value_string t38_T_error_recovery_vals[] = {
891   {   0, "secondary-ifp-packets" },
892   {   1, "fec-info" },
893   { 0, NULL }
894 };
895
896 static const per_choice_t T_error_recovery_choice[] = {
897   {   0, &hf_t38_secondary_ifp_packets, ASN1_NO_EXTENSIONS     , dissect_t38_T_secondary_ifp_packets },
898   {   1, &hf_t38_fec_info        , ASN1_NO_EXTENSIONS     , dissect_t38_T_fec_info },
899   { 0, NULL, 0, NULL }
900 };
901
902 static int
903 dissect_t38_T_error_recovery(tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
904 #line 258 "./asn1/t38/t38.cnf"
905     primary_part = FALSE;
906
907   offset = dissect_per_choice(tvb, offset, actx, tree, hf_index,
908                                  ett_t38_T_error_recovery, T_error_recovery_choice,
909                                  NULL);
910
911 #line 260 "./asn1/t38/t38.cnf"
912     primary_part = TRUE;
913
914   return offset;
915 }
916
917
918 static const per_sequence_t UDPTLPacket_sequence[] = {
919   { &hf_t38_seq_number      , ASN1_NO_EXTENSIONS     , ASN1_NOT_OPTIONAL, dissect_t38_T_seq_number },
920   { &hf_t38_primary_ifp_packet, ASN1_NO_EXTENSIONS     , ASN1_NOT_OPTIONAL, dissect_t38_T_primary_ifp_packet },
921   { &hf_t38_error_recovery  , ASN1_NO_EXTENSIONS     , ASN1_NOT_OPTIONAL, dissect_t38_T_error_recovery },
922   { NULL, 0, 0, NULL }
923 };
924
925 static int
926 dissect_t38_UDPTLPacket(tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
927 #line 232 "./asn1/t38/t38.cnf"
928     /* Initialize to something else than data type */
929     Data_Field_field_type_value = 1;
930
931   offset = dissect_per_sequence(tvb, offset, actx, tree, hf_index,
932                                    ett_t38_UDPTLPacket, UDPTLPacket_sequence);
933
934   return offset;
935 }
936
937 /*--- PDUs ---*/
938
939 static int dissect_IFPPacket_PDU(tvbuff_t *tvb _U_, packet_info *pinfo _U_, proto_tree *tree _U_, void *data _U_) {
940   int offset = 0;
941   asn1_ctx_t asn1_ctx;
942   asn1_ctx_init(&asn1_ctx, ASN1_ENC_PER, TRUE, pinfo);
943   offset = dissect_t38_IFPPacket(tvb, offset, &asn1_ctx, tree, hf_t38_IFPPacket_PDU);
944   offset += 7; offset >>= 3;
945   return offset;
946 }
947 static int dissect_UDPTLPacket_PDU(tvbuff_t *tvb _U_, packet_info *pinfo _U_, proto_tree *tree _U_, void *data _U_) {
948   int offset = 0;
949   asn1_ctx_t asn1_ctx;
950   asn1_ctx_init(&asn1_ctx, ASN1_ENC_PER, TRUE, pinfo);
951   offset = dissect_t38_UDPTLPacket(tvb, offset, &asn1_ctx, tree, hf_t38_UDPTLPacket_PDU);
952   offset += 7; offset >>= 3;
953   return offset;
954 }
955
956
957 /*--- End of included file: packet-t38-fn.c ---*/
958 #line 370 "./asn1/t38/packet-t38-template.c"
959
960 /* initialize the tap t38_info and the conversation */
961 static void
962 init_t38_info_conv(packet_info *pinfo)
963 {
964         /* tap info */
965         t38_info_current++;
966         if (t38_info_current==MAX_T38_MESSAGES_IN_PACKET) {
967                 t38_info_current=0;
968         }
969         t38_info = &t38_info_arr[t38_info_current];
970
971         t38_info->seq_num = 0;
972         t38_info->type_msg = 0;
973         t38_info->data_value = 0;
974         t38_info->t30ind_value =0;
975         t38_info->setup_frame_number = 0;
976         t38_info->Data_Field_field_type_value = 0;
977         t38_info->desc[0] = '\0';
978         t38_info->desc_comment[0] = '\0';
979         t38_info->time_first_t4_data = 0;
980         t38_info->frame_num_first_t4_data = 0;
981
982
983         /*
984                 p_t38_packet_conv hold the conversation info in each of the packets.
985                 p_t38_conv hold the conversation info used to reassemble the HDLC packets, and also the Setup info (e.g SDP)
986                 If we already have p_t38_packet_conv in the packet, it means we already reassembled the HDLC packets, so we don't
987                 need to use p_t38_conv
988         */
989         p_t38_packet_conv = NULL;
990         p_t38_conv = NULL;
991
992         /* Use existing packet info if available */
993         p_t38_packet_conv = (t38_conv *)p_get_proto_data(wmem_file_scope(), pinfo, proto_t38, 0);
994
995
996         /* find the conversation used for Reassemble and Setup Info */
997         p_conv = find_conversation(pinfo->num, &pinfo->net_dst, &pinfo->net_src,
998                                    conversation_pt_to_endpoint_type(pinfo->ptype),
999                                    pinfo->destport, pinfo->srcport, NO_ADDR_B | NO_PORT_B);
1000
1001         /* create a conv if it doen't exist */
1002         if (!p_conv) {
1003                 p_conv = conversation_new(pinfo->num, &pinfo->net_src, &pinfo->net_dst,
1004                               conversation_pt_to_endpoint_type(pinfo->ptype), pinfo->srcport, pinfo->destport, NO_ADDR_B | NO_PORT_B);
1005
1006                 /* Set dissector */
1007                 conversation_set_dissector(p_conv, t38_udp_handle);
1008         }
1009
1010         p_t38_conv = (t38_conv *)conversation_get_proto_data(p_conv, proto_t38);
1011
1012         /* create the conversation if it doesn't exist */
1013         if (!p_t38_conv) {
1014                 p_t38_conv = wmem_new(wmem_file_scope(), t38_conv);
1015                 p_t38_conv->setup_method[0] = '\0';
1016                 p_t38_conv->setup_frame_number = 0;
1017
1018                 p_t38_conv->src_t38_info.reass_ID = 0;
1019                 p_t38_conv->src_t38_info.reass_start_seqnum = -1;
1020                 p_t38_conv->src_t38_info.reass_data_type = 0;
1021                 p_t38_conv->src_t38_info.last_seqnum = -1;
1022                 p_t38_conv->src_t38_info.packet_lost = 0;
1023                 p_t38_conv->src_t38_info.burst_lost = 0;
1024                 p_t38_conv->src_t38_info.time_first_t4_data = 0;
1025                 p_t38_conv->src_t38_info.additional_hdlc_data_field_counter = 0;
1026                 p_t38_conv->src_t38_info.seqnum_prev_data_field = -1;
1027
1028                 p_t38_conv->dst_t38_info.reass_ID = 0;
1029                 p_t38_conv->dst_t38_info.reass_start_seqnum = -1;
1030                 p_t38_conv->dst_t38_info.reass_data_type = 0;
1031                 p_t38_conv->dst_t38_info.last_seqnum = -1;
1032                 p_t38_conv->dst_t38_info.packet_lost = 0;
1033                 p_t38_conv->dst_t38_info.burst_lost = 0;
1034                 p_t38_conv->dst_t38_info.time_first_t4_data = 0;
1035                 p_t38_conv->dst_t38_info.additional_hdlc_data_field_counter = 0;
1036                 p_t38_conv->dst_t38_info.seqnum_prev_data_field = -1;
1037
1038                 conversation_add_proto_data(p_conv, proto_t38, p_t38_conv);
1039         }
1040
1041         if (!p_t38_packet_conv) {
1042                 /* copy the t38 conversation info to the packet t38 conversation */
1043                 p_t38_packet_conv = wmem_new(wmem_file_scope(), t38_conv);
1044                 g_strlcpy(p_t38_packet_conv->setup_method, p_t38_conv->setup_method, MAX_T38_SETUP_METHOD_SIZE);
1045                 p_t38_packet_conv->setup_frame_number = p_t38_conv->setup_frame_number;
1046
1047                 memcpy(&(p_t38_packet_conv->src_t38_info), &(p_t38_conv->src_t38_info), sizeof(t38_conv_info));
1048                 memcpy(&(p_t38_packet_conv->dst_t38_info), &(p_t38_conv->dst_t38_info), sizeof(t38_conv_info));
1049
1050                 p_add_proto_data(wmem_file_scope(), pinfo, proto_t38, 0, p_t38_packet_conv);
1051         }
1052
1053         if (addresses_equal(conversation_key_addr1(p_conv->key_ptr), &pinfo->net_src)) {
1054                 p_t38_conv_info = &(p_t38_conv->src_t38_info);
1055                 p_t38_packet_conv_info = &(p_t38_packet_conv->src_t38_info);
1056         } else {
1057                 p_t38_conv_info = &(p_t38_conv->dst_t38_info);
1058                 p_t38_packet_conv_info = &(p_t38_packet_conv->dst_t38_info);
1059         }
1060
1061         /* update t38_info */
1062         t38_info->setup_frame_number = p_t38_packet_conv->setup_frame_number;
1063 }
1064
1065 /* Entry point for dissection */
1066 static int
1067 dissect_t38_udp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
1068 {
1069         guint8 octet1;
1070         proto_item *it;
1071         proto_tree *tr;
1072         guint32 offset=0;
1073
1074         /*
1075          * XXX - heuristic to check for misidentified packets.
1076          */
1077         if (dissect_possible_rtpv2_packets_as_rtp){
1078                 octet1 = tvb_get_guint8(tvb, offset);
1079                 if (RTP_VERSION(octet1) == 2){
1080                         return call_dissector(rtp_handle,tvb,pinfo,tree);
1081                 }
1082         }
1083
1084         col_set_str(pinfo->cinfo, COL_PROTOCOL, "T.38");
1085         col_clear(pinfo->cinfo, COL_INFO);
1086
1087         primary_part = TRUE;
1088
1089         /* This indicate the item number in the primary part of the T38 message, it is used for the reassemble of T30 packets */
1090         Data_Field_item_num = 0;
1091
1092         it=proto_tree_add_protocol_format(tree, proto_t38, tvb, 0, -1, "ITU-T Recommendation T.38");
1093         tr=proto_item_add_subtree(it, ett_t38);
1094
1095         /* init tap and conv info */
1096         init_t38_info_conv(pinfo);
1097
1098         /* Show Conversation setup info if exists*/
1099         if (global_t38_show_setup_info) {
1100                 show_setup_info(tvb, tr, p_t38_packet_conv);
1101         }
1102
1103         col_append_str(pinfo->cinfo, COL_INFO, "UDP: UDPTLPacket ");
1104
1105         offset = dissect_UDPTLPacket_PDU(tvb, pinfo, tr, NULL);
1106
1107         if (tvb_reported_length_remaining(tvb,offset)>0){
1108                 proto_tree_add_expert_format(tr, pinfo, &ei_t38_malformed, tvb, offset, tvb_reported_length_remaining(tvb, offset),
1109                                 "[MALFORMED PACKET or wrong preference settings]");
1110                 col_append_str(pinfo->cinfo, COL_INFO, " [Malformed?]");
1111         }
1112         return tvb_captured_length(tvb);
1113 }
1114
1115 static int
1116 dissect_t38_tcp_pdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
1117 {
1118         proto_item *it;
1119         proto_tree *tr;
1120         guint32 offset=0;
1121         tvbuff_t *next_tvb;
1122         guint16 ifp_packet_number=1;
1123
1124         col_set_str(pinfo->cinfo, COL_PROTOCOL, "T.38");
1125         col_clear(pinfo->cinfo, COL_INFO);
1126
1127         primary_part = TRUE;
1128
1129         /* This indicate the item number in the primary part of the T38 message, it is used for the reassemble of T30 packets */
1130         Data_Field_item_num = 0;
1131
1132         it=proto_tree_add_protocol_format(tree, proto_t38, tvb, 0, -1, "ITU-T Recommendation T.38");
1133         tr=proto_item_add_subtree(it, ett_t38);
1134
1135         /* init tap and conv info */
1136         init_t38_info_conv(pinfo);
1137
1138         /* Show Conversation setup info if exists*/
1139         if (global_t38_show_setup_info) {
1140                 show_setup_info(tvb, tr, p_t38_packet_conv);
1141         }
1142
1143         col_append_str(pinfo->cinfo, COL_INFO, "TCP: IFPPacket");
1144
1145         while(tvb_reported_length_remaining(tvb,offset)>0)
1146         {
1147                 next_tvb = tvb_new_subset_remaining(tvb, offset);
1148                 offset += dissect_IFPPacket_PDU(next_tvb, pinfo, tr, NULL);
1149                 ifp_packet_number++;
1150
1151                 if(tvb_reported_length_remaining(tvb,offset)>0){
1152                         if(t38_tpkt_usage == T38_TPKT_ALWAYS){
1153                                 proto_tree_add_expert_format(tr, pinfo, &ei_t38_malformed, tvb, offset, tvb_reported_length_remaining(tvb, offset),
1154                                                 "[MALFORMED PACKET or wrong preference settings]");
1155                                 col_append_str(pinfo->cinfo, COL_INFO, " [Malformed?]");
1156                                 break;
1157                         }else {
1158                                 col_append_fstr(pinfo->cinfo, COL_INFO, " IFPPacket#%u",ifp_packet_number);
1159                         }
1160                 }
1161         }
1162
1163         return tvb_captured_length(tvb);
1164 }
1165
1166 static int
1167 dissect_t38_tcp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
1168 {
1169         primary_part = TRUE;
1170
1171         if(t38_tpkt_usage == T38_TPKT_ALWAYS){
1172                 dissect_tpkt_encap(tvb,pinfo,tree,t38_tpkt_reassembly,t38_tcp_pdu_handle);
1173         }
1174         else if((t38_tpkt_usage == T38_TPKT_NEVER) || (is_tpkt(tvb,1) == -1)){
1175                 dissect_t38_tcp_pdu(tvb, pinfo, tree, data);
1176         }
1177         else {
1178                 dissect_tpkt_encap(tvb,pinfo,tree,t38_tpkt_reassembly,t38_tcp_pdu_handle);
1179         }
1180         return tvb_captured_length(tvb);
1181 }
1182
1183 /* Look for conversation info and display any setup info found */
1184 void
1185 show_setup_info(tvbuff_t *tvb, proto_tree *tree, t38_conv *p_t38_conversation)
1186 {
1187         proto_tree *t38_setup_tree;
1188         proto_item *ti;
1189
1190         if (!p_t38_conversation || p_t38_conversation->setup_frame_number == 0) {
1191                 /* there is no Setup info */
1192                 return;
1193         }
1194
1195         ti =  proto_tree_add_string_format(tree, hf_t38_setup, tvb, 0, 0,
1196                       "",
1197                       "Stream setup by %s (frame %u)",
1198                       p_t38_conversation->setup_method,
1199                       p_t38_conversation->setup_frame_number);
1200     PROTO_ITEM_SET_GENERATED(ti);
1201     t38_setup_tree = proto_item_add_subtree(ti, ett_t38_setup);
1202     if (t38_setup_tree)
1203     {
1204                 /* Add details into subtree */
1205                 proto_item* item = proto_tree_add_uint(t38_setup_tree, hf_t38_setup_frame,
1206                                                                tvb, 0, 0, p_t38_conversation->setup_frame_number);
1207                 PROTO_ITEM_SET_GENERATED(item);
1208                 item = proto_tree_add_string(t38_setup_tree, hf_t38_setup_method,
1209                                                      tvb, 0, 0, p_t38_conversation->setup_method);
1210                 PROTO_ITEM_SET_GENERATED(item);
1211     }
1212 }
1213
1214
1215
1216 /* Wireshark Protocol Registration */
1217 void
1218 proto_register_t38(void)
1219 {
1220         static hf_register_info hf[] =
1221         {
1222
1223 /*--- Included file: packet-t38-hfarr.c ---*/
1224 #line 1 "./asn1/t38/packet-t38-hfarr.c"
1225     { &hf_t38_IFPPacket_PDU,
1226       { "IFPPacket", "t38.IFPPacket_element",
1227         FT_NONE, BASE_NONE, NULL, 0,
1228         NULL, HFILL }},
1229     { &hf_t38_UDPTLPacket_PDU,
1230       { "UDPTLPacket", "t38.UDPTLPacket_element",
1231         FT_NONE, BASE_NONE, NULL, 0,
1232         NULL, HFILL }},
1233     { &hf_t38_type_of_msg,
1234       { "type-of-msg", "t38.type_of_msg",
1235         FT_UINT32, BASE_DEC, VALS(t38_Type_of_msg_vals), 0,
1236         NULL, HFILL }},
1237     { &hf_t38_data_field,
1238       { "data-field", "t38.data_field",
1239         FT_UINT32, BASE_DEC, NULL, 0,
1240         NULL, HFILL }},
1241     { &hf_t38_t30_indicator,
1242       { "t30-indicator", "t38.t30_indicator",
1243         FT_UINT32, BASE_DEC, VALS(t38_T30_indicator_vals), 0,
1244         NULL, HFILL }},
1245     { &hf_t38_t30_data,
1246       { "t30-data", "t38.t30_data",
1247         FT_UINT32, BASE_DEC, VALS(t38_T30_data_vals), 0,
1248         NULL, HFILL }},
1249     { &hf_t38_Data_Field_item,
1250       { "Data-Field item", "t38.Data_Field_item_element",
1251         FT_NONE, BASE_NONE, NULL, 0,
1252         NULL, HFILL }},
1253     { &hf_t38_field_type,
1254       { "field-type", "t38.field_type",
1255         FT_UINT32, BASE_DEC, VALS(t38_T_field_type_vals), 0,
1256         NULL, HFILL }},
1257     { &hf_t38_field_data,
1258       { "field-data", "t38.field_data",
1259         FT_BYTES, BASE_NONE, NULL, 0,
1260         NULL, HFILL }},
1261     { &hf_t38_seq_number,
1262       { "seq-number", "t38.seq_number",
1263         FT_UINT32, BASE_DEC, NULL, 0,
1264         NULL, HFILL }},
1265     { &hf_t38_primary_ifp_packet,
1266       { "primary-ifp-packet", "t38.primary_ifp_packet_element",
1267         FT_NONE, BASE_NONE, NULL, 0,
1268         NULL, HFILL }},
1269     { &hf_t38_error_recovery,
1270       { "error-recovery", "t38.error_recovery",
1271         FT_UINT32, BASE_DEC, VALS(t38_T_error_recovery_vals), 0,
1272         NULL, HFILL }},
1273     { &hf_t38_secondary_ifp_packets,
1274       { "secondary-ifp-packets", "t38.secondary_ifp_packets",
1275         FT_UINT32, BASE_DEC, NULL, 0,
1276         NULL, HFILL }},
1277     { &hf_t38_secondary_ifp_packets_item,
1278       { "secondary-ifp-packets item", "t38.secondary_ifp_packets_item_element",
1279         FT_NONE, BASE_NONE, NULL, 0,
1280         "OpenType_IFPPacket", HFILL }},
1281     { &hf_t38_fec_info,
1282       { "fec-info", "t38.fec_info_element",
1283         FT_NONE, BASE_NONE, NULL, 0,
1284         NULL, HFILL }},
1285     { &hf_t38_fec_npackets,
1286       { "fec-npackets", "t38.fec_npackets",
1287         FT_INT32, BASE_DEC, NULL, 0,
1288         "INTEGER", HFILL }},
1289     { &hf_t38_fec_data,
1290       { "fec-data", "t38.fec_data",
1291         FT_UINT32, BASE_DEC, NULL, 0,
1292         NULL, HFILL }},
1293     { &hf_t38_fec_data_item,
1294       { "fec-data item", "t38.fec_data_item",
1295         FT_BYTES, BASE_NONE, NULL, 0,
1296         "OCTET_STRING", HFILL }},
1297
1298 /*--- End of included file: packet-t38-hfarr.c ---*/
1299 #line 634 "./asn1/t38/packet-t38-template.c"
1300                 {   &hf_t38_setup,
1301                     { "Stream setup", "t38.setup", FT_STRING, BASE_NONE,
1302                     NULL, 0x0, "Stream setup, method and frame number", HFILL }},
1303                 {   &hf_t38_setup_frame,
1304             { "Stream frame", "t38.setup-frame", FT_FRAMENUM, BASE_NONE,
1305             NULL, 0x0, "Frame that set up this stream", HFILL }},
1306         {   &hf_t38_setup_method,
1307             { "Stream Method", "t38.setup-method", FT_STRING, BASE_NONE,
1308             NULL, 0x0, "Method used to set up this stream", HFILL }},
1309                 {&hf_t38_fragments,
1310                         {"Message fragments", "t38.fragments",
1311                         FT_NONE, BASE_NONE, NULL, 0x00, NULL, HFILL } },
1312                 {&hf_t38_fragment,
1313                         {"Message fragment", "t38.fragment",
1314                         FT_FRAMENUM, BASE_NONE, NULL, 0x00, NULL, HFILL } },
1315                 {&hf_t38_fragment_overlap,
1316                         {"Message fragment overlap", "t38.fragment.overlap",
1317                         FT_BOOLEAN, BASE_NONE, NULL, 0x0, NULL, HFILL } },
1318                 {&hf_t38_fragment_overlap_conflicts,
1319                         {"Message fragment overlapping with conflicting data",
1320                         "t38.fragment.overlap.conflicts",
1321                         FT_BOOLEAN, BASE_NONE, NULL, 0x0, NULL, HFILL } },
1322                 {&hf_t38_fragment_multiple_tails,
1323                         {"Message has multiple tail fragments",
1324                         "t38.fragment.multiple_tails",
1325                         FT_BOOLEAN, BASE_NONE, NULL, 0x0, NULL, HFILL } },
1326                 {&hf_t38_fragment_too_long_fragment,
1327                         {"Message fragment too long", "t38.fragment.too_long_fragment",
1328                         FT_BOOLEAN, BASE_NONE, NULL, 0x0, NULL, HFILL } },
1329                 {&hf_t38_fragment_error,
1330                         {"Message defragmentation error", "t38.fragment.error",
1331                         FT_FRAMENUM, BASE_NONE, NULL, 0x00, NULL, HFILL } },
1332                 {&hf_t38_fragment_count,
1333                         {"Message fragment count", "t38.fragment.count",
1334                         FT_UINT32, BASE_DEC, NULL, 0x00, NULL, HFILL } },
1335                 {&hf_t38_reassembled_in,
1336                         {"Reassembled in", "t38.reassembled.in",
1337                         FT_FRAMENUM, BASE_NONE, NULL, 0x00, NULL, HFILL } },
1338                 {&hf_t38_reassembled_length,
1339                         {"Reassembled T38 length", "t38.reassembled.length",
1340                         FT_UINT32, BASE_DEC, NULL, 0x00, NULL, HFILL } },
1341         };
1342
1343         static gint *ett[] =
1344         {
1345                 &ett_t38,
1346
1347 /*--- Included file: packet-t38-ettarr.c ---*/
1348 #line 1 "./asn1/t38/packet-t38-ettarr.c"
1349     &ett_t38_IFPPacket,
1350     &ett_t38_Type_of_msg,
1351     &ett_t38_Data_Field,
1352     &ett_t38_Data_Field_item,
1353     &ett_t38_UDPTLPacket,
1354     &ett_t38_T_error_recovery,
1355     &ett_t38_T_secondary_ifp_packets,
1356     &ett_t38_T_fec_info,
1357     &ett_t38_T_fec_data,
1358
1359 /*--- End of included file: packet-t38-ettarr.c ---*/
1360 #line 681 "./asn1/t38/packet-t38-template.c"
1361                 &ett_t38_setup,
1362                 &ett_data_fragment,
1363                 &ett_data_fragments
1364         };
1365
1366         static ei_register_info ei[] = {
1367                 { &ei_t38_malformed, { "t38.malformed", PI_MALFORMED, PI_ERROR, "Malformed packet", EXPFILL }},
1368         };
1369
1370         module_t *t38_module;
1371         expert_module_t* expert_t38;
1372
1373         proto_t38 = proto_register_protocol("T.38", "T.38", "t38");
1374         proto_register_field_array(proto_t38, hf, array_length(hf));
1375         proto_register_subtree_array(ett, array_length(ett));
1376         expert_t38 = expert_register_protocol(proto_t38);
1377         expert_register_field_array(expert_t38, ei, array_length(ei));
1378         register_dissector("t38_udp", dissect_t38_udp, proto_t38);
1379
1380         /* Register reassemble tables for HDLC */
1381         reassembly_table_register(&data_reassembly_table,
1382                               &addresses_reassembly_table_functions);
1383
1384         t38_tap = register_tap("t38");
1385
1386         t38_module = prefs_register_protocol(proto_t38, NULL);
1387         prefs_register_bool_preference(t38_module, "use_pre_corrigendum_asn1_specification",
1388             "Use the Pre-Corrigendum ASN.1 specification",
1389             "Whether the T.38 dissector should decode using the Pre-Corrigendum T.38 "
1390                 "ASN.1 specification (1998).",
1391             &use_pre_corrigendum_asn1_specification);
1392         prefs_register_bool_preference(t38_module, "dissect_possible_rtpv2_packets_as_rtp",
1393             "Dissect possible RTP version 2 packets with RTP dissector",
1394             "Whether a UDP packet that looks like RTP version 2 packet will "
1395                 "be dissected as RTP packet or T.38 packet. If enabled there is a risk that T.38 UDPTL "
1396                 "packets with sequence number higher than 32767 may be dissected as RTP.",
1397             &dissect_possible_rtpv2_packets_as_rtp);
1398         prefs_register_bool_preference(t38_module, "reassembly",
1399                 "Reassemble T.38 PDUs over TPKT over TCP",
1400                 "Whether the dissector should reassemble T.38 PDUs spanning multiple TCP segments "
1401                 "when TPKT is used over TCP. "
1402                 "To use this option, you must also enable \"Allow subdissectors to reassemble "
1403                 "TCP streams\" in the TCP protocol settings.",
1404                 &t38_tpkt_reassembly);
1405         prefs_register_enum_preference(t38_module, "tpkt_usage",
1406                 "TPKT used over TCP",
1407                 "Whether T.38 is used with TPKT for TCP",
1408                 (gint *)&t38_tpkt_usage,t38_tpkt_options,FALSE);
1409
1410         prefs_register_bool_preference(t38_module, "show_setup_info",
1411                 "Show stream setup information",
1412                 "Where available, show which protocol and frame caused "
1413                 "this T.38 stream to be created",
1414                 &global_t38_show_setup_info);
1415
1416 }
1417
1418 void
1419 proto_reg_handoff_t38(void)
1420 {
1421         t38_udp_handle=create_dissector_handle(dissect_t38_udp, proto_t38);
1422         t38_tcp_handle=create_dissector_handle(dissect_t38_tcp, proto_t38);
1423         t38_tcp_pdu_handle=create_dissector_handle(dissect_t38_tcp_pdu, proto_t38);
1424         rtp_handle = find_dissector_add_dependency("rtp", proto_t38);
1425         t30_hdlc_handle = find_dissector_add_dependency("t30.hdlc", proto_t38);
1426         data_handle = find_dissector("data");
1427         dissector_add_for_decode_as("tcp.port", t38_tcp_handle);
1428         dissector_add_for_decode_as("udp.port", t38_udp_handle);
1429 }