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