Rename the routines that handle dissector tables with unsigned integer
[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 <string.h>
67
68 #include "packet-t38.h"
69 #include <epan/prefs.h>
70 #include <epan/ipproto.h>
71 #include <epan/asn1.h>
72 #include "packet-per.h"
73 #include "packet-tpkt.h"
74 #include <epan/emem.h>
75 #include <epan/strutil.h>
76
77 #define PORT_T38 6004  
78 static guint global_t38_tcp_port = PORT_T38;
79 static guint global_t38_udp_port = PORT_T38;
80
81 static int t38_tap = -1;
82
83 /* dissect using the Pre Corrigendum T.38 ASN.1 specification (1998) */
84 static gboolean use_pre_corrigendum_asn1_specification = TRUE;
85
86 /* dissect packets that looks like RTP version 2 packets as RTP     */
87 /* instead of as T.38. This may result in that some T.38 UPTL       */
88 /* packets with sequence number values higher than 32767 may be     */
89 /* shown as RTP packets.                                            */ 
90 static gboolean dissect_possible_rtpv2_packets_as_rtp = FALSE;
91
92
93 /* Reassembly of T.38 PDUs over TPKT over TCP */
94 static gboolean t38_tpkt_reassembly = TRUE;
95
96 /* Preference setting whether TPKT header is used when sending T.38 over TCP.
97  * The default setting is Maybe where the dissector will look on the first
98  * bytes to try to determine whether TPKT header is used or not. This may not
99  * work so well in some cases. You may want to change the setting to Always or
100  * Newer.
101  */
102 #define T38_TPKT_NEVER 0   /* Assume that there is never a TPKT header    */
103 #define T38_TPKT_ALWAYS 1  /* Assume that there is always a TPKT header   */
104 #define T38_TPKT_MAYBE 2   /* Assume TPKT if first octets are 03-00-xx-xx */
105 static gint t38_tpkt_usage = T38_TPKT_MAYBE;
106
107 static const enum_val_t t38_tpkt_options[] = {
108   {"never", "Never", T38_TPKT_NEVER},
109   {"always", "Always", T38_TPKT_ALWAYS},
110   {"maybe", "Maybe", T38_TPKT_MAYBE},
111   {NULL, NULL, -1}
112 };
113
114
115
116 /* T38 */
117 static dissector_handle_t t38_udp_handle;
118 static dissector_handle_t t38_tcp_handle;
119 static dissector_handle_t t38_tcp_pdu_handle;
120 static dissector_handle_t rtp_handle;
121 static dissector_handle_t t30_hdlc_handle;
122 static dissector_handle_t data_handle;
123
124 static gint32 Type_of_msg_value;
125 static guint32 Data_Field_field_type_value;
126 static guint32 Data_value;
127 static guint32 T30ind_value;
128 static guint32 Data_Field_item_num;
129
130 static int proto_t38 = -1;
131
132 /*--- Included file: packet-t38-hf.c ---*/
133 #line 1 "packet-t38-hf.c"
134 static int hf_t38_IFPPacket_PDU = -1;             /* IFPPacket */
135 static int hf_t38_UDPTLPacket_PDU = -1;           /* UDPTLPacket */
136 static int hf_t38_type_of_msg = -1;               /* Type_of_msg */
137 static int hf_t38_data_field = -1;                /* Data_Field */
138 static int hf_t38_t30_indicator = -1;             /* T30_indicator */
139 static int hf_t38_t30_data = -1;                  /* T30_data */
140 static int hf_t38_Data_Field_item = -1;           /* Data_Field_item */
141 static int hf_t38_field_type = -1;                /* T_field_type */
142 static int hf_t38_field_data = -1;                /* T_field_data */
143 static int hf_t38_seq_number = -1;                /* T_seq_number */
144 static int hf_t38_primary_ifp_packet = -1;        /* T_primary_ifp_packet */
145 static int hf_t38_error_recovery = -1;            /* T_error_recovery */
146 static int hf_t38_secondary_ifp_packets = -1;     /* T_secondary_ifp_packets */
147 static int hf_t38_secondary_ifp_packets_item = -1;  /* OpenType_IFPPacket */
148 static int hf_t38_fec_info = -1;                  /* T_fec_info */
149 static int hf_t38_fec_npackets = -1;              /* INTEGER */
150 static int hf_t38_fec_data = -1;                  /* T_fec_data */
151 static int hf_t38_fec_data_item = -1;             /* OCTET_STRING */
152
153 /*--- End of included file: packet-t38-hf.c ---*/
154 #line 124 "packet-t38-template.c"
155
156 /* T38 setup fields */
157 static int hf_t38_setup        = -1;
158 static int hf_t38_setup_frame  = -1;
159 static int hf_t38_setup_method = -1;
160
161 /* T38 Data reassemble fields */
162 static int hf_t38_fragments = -1;
163 static int hf_t38_fragment = -1;
164 static int hf_t38_fragment_overlap = -1;
165 static int hf_t38_fragment_overlap_conflicts = -1;
166 static int hf_t38_fragment_multiple_tails = -1;
167 static int hf_t38_fragment_too_long_fragment = -1;
168 static int hf_t38_fragment_error = -1;
169 static int hf_t38_reassembled_in = -1;
170 static int hf_t38_reassembled_length = -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
199 static const fragment_items data_frag_items = {
200         /* Fragment subtrees */
201         &ett_data_fragment,
202         &ett_data_fragments,
203         /* Fragment fields */
204         &hf_t38_fragments,
205         &hf_t38_fragment,
206         &hf_t38_fragment_overlap,
207         &hf_t38_fragment_overlap_conflicts,
208         &hf_t38_fragment_multiple_tails,
209         &hf_t38_fragment_too_long_fragment,
210         &hf_t38_fragment_error,
211         /* Reassembled in field */
212         &hf_t38_reassembled_in,
213         /* Reassembled length field */
214         &hf_t38_reassembled_length,
215         /* Tag */
216         "Data fragments"
217 };
218
219 typedef struct _fragment_key {
220         address src;
221         address dst;
222         guint32 id;
223 } fragment_key;
224
225 static conversation_t *p_conv= NULL;
226 static t38_conv *p_t38_conv = NULL;
227 static t38_conv *p_t38_packet_conv = NULL;
228 static t38_conv_info *p_t38_conv_info = NULL;
229 static t38_conv_info *p_t38_packet_conv_info = NULL;
230
231 /* RTP Version is the first 2 bits of the first octet in the UDP payload*/
232 #define RTP_VERSION(octet)      ((octet) >> 6)
233
234 void proto_reg_handoff_t38(void);
235
236 static void show_setup_info(tvbuff_t *tvb, proto_tree *tree, t38_conv *p_t38_conv);
237 /* Preferences bool to control whether or not setup info should be shown */
238 static gboolean global_t38_show_setup_info = TRUE;
239
240 /* Can tap up to 4 T38 packets within same packet */
241 /* We only tap the primary part, not the redundancy */
242 #define MAX_T38_MESSAGES_IN_PACKET 4
243 static t38_packet_info t38_info_arr[MAX_T38_MESSAGES_IN_PACKET];
244 static int t38_info_current=0;
245 static t38_packet_info *t38_info=NULL;
246
247 static void t38_defragment_init(void)
248 {
249         /* Init reassemble tables */
250         fragment_table_init(&data_fragment_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_conversation;
262         t38_conv* p_conversation_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_conversation = 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_conversation || p_conversation->setup_frame != setup_frame_number) {
287                 p_conversation = 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_conversation, t38_udp_handle);
294
295         /*
296          * Check if the conversation has data associated with it.
297          */
298         p_conversation_data = conversation_get_proto_data(p_conversation, proto_t38);
299
300         /*
301          * If not, add a new data item.
302          */
303         if ( ! p_conversation_data ) {
304                 /* Create conversation data */
305                 p_conversation_data = se_alloc(sizeof(t38_conv));
306
307                 conversation_add_proto_data(p_conversation, proto_t38, p_conversation_data);
308         }
309
310         /*
311          * Update the conversation data.
312          */
313         g_strlcpy(p_conversation_data->setup_method, setup_method, MAX_T38_SETUP_METHOD_SIZE);
314         p_conversation_data->setup_frame_number = setup_frame_number;
315                 p_conversation_data->src_t38_info.reass_ID = 0;
316                 p_conversation_data->src_t38_info.reass_start_seqnum = -1;
317                 p_conversation_data->src_t38_info.reass_data_type = 0;
318                 p_conversation_data->src_t38_info.last_seqnum = -1;
319                 p_conversation_data->src_t38_info.packet_lost = 0;
320                 p_conversation_data->src_t38_info.burst_lost = 0;
321                 p_conversation_data->src_t38_info.time_first_t4_data = 0;
322
323
324                 p_conversation_data->dst_t38_info.reass_ID = 0;
325                 p_conversation_data->dst_t38_info.reass_start_seqnum = -1;
326                 p_conversation_data->dst_t38_info.reass_data_type = 0;
327                 p_conversation_data->dst_t38_info.last_seqnum = -1;
328                 p_conversation_data->dst_t38_info.packet_lost = 0;
329                 p_conversation_data->dst_t38_info.burst_lost = 0;
330                 p_conversation_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                         col_append_str(actx->pinfo->cinfo, COL_INFO, " (t4-data Reassembled: No packet lost)"); 
617                         
618                         g_snprintf(t38_info->desc_comment, MAX_T38_DESC, "No packet lost");
619                     }
620
621                     
622                     if (p_t38_packet_conv_info->packet_lost) {
623                         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);
624                     } else {
625                         g_snprintf(t38_info->desc_comment, MAX_T38_DESC, "No packet lost");
626                     }
627
628                     new_tvb = process_reassembled_data(tvb, offset, actx->pinfo,
629                                 "Reassembled T38", frag_msg, &data_frag_items, NULL, tree);
630
631                     /* Now reset fragmentation information in pinfo */
632                     actx->pinfo->fragmented = save_fragmented;
633
634                     t38_info->time_first_t4_data = p_t38_packet_conv_info->time_first_t4_data; 
635                     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 */
636
637                 } else {
638                     new_tvb = process_reassembled_data(tvb, offset, actx->pinfo,
639                                 "Reassembled T38", frag_msg, &data_frag_items, NULL, tree);
640
641                     /* Now reset fragmentation information in pinfo */
642                     actx->pinfo->fragmented = save_fragmented;
643                     actx->pinfo->private_data = t38_info;
644
645                     if (new_tvb) call_dissector((t30_hdlc_handle) ? t30_hdlc_handle : data_handle, new_tvb, actx->pinfo, tree);
646                 }
647             } else {
648                 if(tree){
649                     proto_tree_add_text(tree, tvb, offset, tvb_reported_length_remaining(tvb, offset),
650                         "[RECEIVED END OF FRAGMENT W/OUT ANY FRAGMENT DATA]");
651                 }
652                 col_append_str(actx->pinfo->cinfo, COL_INFO, " [Malformed?]");
653                 actx->pinfo->fragmented = save_fragmented;
654             }
655         }
656
657         /* reset the reassemble ID and the start seq number if it is not HDLC data */
658         if ( p_t38_conv && ( ((Data_Field_field_type_value >0) && (Data_Field_field_type_value <6)) || (Data_Field_field_type_value == 7) ) ){
659             p_t38_conv_info->reass_ID = 0;
660             p_t38_conv_info->reass_start_seqnum = -1;
661         }
662         t38_info->Data_Field_field_type_value = Data_Field_field_type_value;
663     }
664
665   return offset;
666 }
667
668
669
670 static int
671 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_) {
672 #line 152 "t38.cnf"
673     tvbuff_t *value_tvb = NULL;
674     guint32 value_len;
675
676   offset = dissect_per_octet_string(tvb, offset, actx, tree, hf_index,
677                                        1, 65535, FALSE, &value_tvb);
678
679     value_len = tvb_length(value_tvb);
680
681
682
683 #line 159 "t38.cnf"
684     if (check_col(actx->pinfo->cinfo, COL_INFO) && primary_part){
685         if(value_len < 8){
686             col_append_fstr(actx->pinfo->cinfo, COL_INFO, "[%s]",
687                tvb_bytes_to_str(value_tvb,0,value_len));
688         }
689         else {
690             col_append_fstr(actx->pinfo->cinfo, COL_INFO, "[%s...]",
691                tvb_bytes_to_str(value_tvb,0,7));
692         }
693     }
694
695     /* We only reassmeble packets in the Primary part and in the first two Items.                       */
696     /* There maybe be t38 packets with more than two Items, but reassemble those packets is not easy    */
697     /* using the current ressaemble functions.                                                          */
698     /* TODO: reassemble all the Items in one frame */
699     if (primary_part && (Data_Field_item_num<2)) {
700         tvbuff_t* new_tvb = NULL;
701         fragment_data *frag_msg = NULL;
702     
703         /* HDLC Data or t4-non-ecm-data */
704         if (Data_Field_field_type_value == 0 || Data_Field_field_type_value == 6) { /* 0=HDLC Data or 6=t4-non-ecm-data*/
705             gboolean save_fragmented = actx->pinfo->fragmented;
706
707             actx->pinfo->fragmented = TRUE;
708
709             /* if we have not reassembled this packet and it is the first fragment, reset the reassemble ID and the start seq number*/
710             if (p_t38_packet_conv && p_t38_conv && (p_t38_packet_conv_info->reass_ID == 0)) {
711                 /* we use the first fragment's frame_number as fragment ID because the protocol doesn't provide it */
712                     p_t38_conv_info->reass_ID = actx->pinfo->fd->num;
713                     p_t38_conv_info->reass_start_seqnum = seq_number;
714                     p_t38_conv_info->time_first_t4_data = nstime_to_sec(&actx->pinfo->fd->rel_ts);
715                     p_t38_packet_conv_info->reass_ID = p_t38_conv_info->reass_ID;
716                     p_t38_packet_conv_info->reass_start_seqnum = p_t38_conv_info->reass_start_seqnum;
717                     p_t38_packet_conv_info->time_first_t4_data = p_t38_conv_info->time_first_t4_data;
718             }
719
720             frag_msg = fragment_add_seq(value_tvb, 0, actx->pinfo,
721                 p_t38_packet_conv_info->reass_ID, /* ID for fragments belonging together */
722                 data_fragment_table, /* list of message fragments */
723                 seq_number - (guint32)p_t38_packet_conv_info->reass_start_seqnum, /* fragment sequence number */
724                 value_len, /* fragment length */
725                 TRUE); /* More fragments */
726
727             new_tvb = process_reassembled_data(tvb, offset, actx->pinfo,
728                         "Reassembled T38", frag_msg, &data_frag_items, NULL, tree);
729
730             if (!frag_msg) { /* Not last packet of reassembled */
731                 if (Data_Field_field_type_value == 0) {
732                     if (check_col(actx->pinfo->cinfo, COL_INFO))
733                         col_append_fstr(actx->pinfo->cinfo, COL_INFO," (HDLC fragment %u)", seq_number - (guint32)p_t38_packet_conv_info->reass_start_seqnum);
734                 } else {
735                     if (check_col(actx->pinfo->cinfo, COL_INFO))
736                         col_append_fstr(actx->pinfo->cinfo, COL_INFO," (t4-data fragment %u)", seq_number - (guint32)p_t38_packet_conv_info->reass_start_seqnum);
737                 }
738             }
739
740             /* Now reset fragmentation information in pinfo */
741             actx->pinfo->fragmented = save_fragmented;
742         }
743     }
744
745   return offset;
746 }
747
748
749 static const per_sequence_t Data_Field_item_sequence[] = {
750   { &hf_t38_field_type      , ASN1_NO_EXTENSIONS     , ASN1_NOT_OPTIONAL, dissect_t38_T_field_type },
751   { &hf_t38_field_data      , ASN1_NO_EXTENSIONS     , ASN1_OPTIONAL    , dissect_t38_T_field_data },
752   { NULL, 0, 0, NULL }
753 };
754
755 static int
756 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_) {
757   offset = dissect_per_sequence(tvb, offset, actx, tree, hf_index,
758                                    ett_t38_Data_Field_item, Data_Field_item_sequence);
759
760 #line 55 "t38.cnf"
761     if (primary_part) Data_Field_item_num++;
762
763   return offset;
764 }
765
766
767 static const per_sequence_t Data_Field_sequence_of[1] = {
768   { &hf_t38_Data_Field_item , ASN1_NO_EXTENSIONS     , ASN1_NOT_OPTIONAL, dissect_t38_Data_Field_item },
769 };
770
771 static int
772 dissect_t38_Data_Field(tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
773   offset = dissect_per_sequence_of(tvb, offset, actx, tree, hf_index,
774                                       ett_t38_Data_Field, Data_Field_sequence_of);
775
776   return offset;
777 }
778
779
780 static const per_sequence_t IFPPacket_sequence[] = {
781   { &hf_t38_type_of_msg     , ASN1_NO_EXTENSIONS     , ASN1_NOT_OPTIONAL, dissect_t38_Type_of_msg },
782   { &hf_t38_data_field      , ASN1_NO_EXTENSIONS     , ASN1_OPTIONAL    , dissect_t38_Data_Field },
783   { NULL, 0, 0, NULL }
784 };
785
786 static int
787 dissect_t38_IFPPacket(tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
788   offset = dissect_per_sequence(tvb, offset, actx, tree, hf_index,
789                                    ett_t38_IFPPacket, IFPPacket_sequence);
790
791   return offset;
792 }
793
794
795
796 static int
797 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_) {
798   offset = dissect_per_constrained_integer(tvb, offset, actx, tree, hf_index,
799                                                             0U, 65535U, &seq_number, FALSE);
800
801 #line 229 "t38.cnf"
802     /* info for tap */
803     if (primary_part)
804         t38_info->seq_num = seq_number;
805
806       if (check_col(actx->pinfo->cinfo, COL_INFO)){
807         col_append_fstr(actx->pinfo->cinfo, COL_INFO, "Seq=%05u ",seq_number);
808     }
809
810   return offset;
811 }
812
813
814
815 static int
816 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_) {
817 #line 239 "t38.cnf"
818     primary_part = TRUE;
819
820   offset = dissect_per_open_type(tvb, offset, actx, tree, hf_index, dissect_t38_IFPPacket);
821
822 #line 241 "t38.cnf"
823     /* if is a valid t38 packet, add to tap */
824     if (p_t38_packet_conv && (!actx->pinfo->in_error_pkt) && ((gint32) seq_number != p_t38_packet_conv_info->last_seqnum))
825         tap_queue_packet(t38_tap, actx->pinfo, t38_info);
826
827     if (p_t38_conv) p_t38_conv_info->last_seqnum = (gint32) seq_number;
828
829   return offset;
830 }
831
832
833
834 static int
835 dissect_t38_OpenType_IFPPacket(tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
836   offset = dissect_per_open_type(tvb, offset, actx, tree, hf_index, dissect_t38_IFPPacket);
837
838   return offset;
839 }
840
841
842 static const per_sequence_t T_secondary_ifp_packets_sequence_of[1] = {
843   { &hf_t38_secondary_ifp_packets_item, ASN1_NO_EXTENSIONS     , ASN1_NOT_OPTIONAL, dissect_t38_OpenType_IFPPacket },
844 };
845
846 static int
847 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_) {
848   offset = dissect_per_sequence_of(tvb, offset, actx, tree, hf_index,
849                                       ett_t38_T_secondary_ifp_packets, T_secondary_ifp_packets_sequence_of);
850
851   return offset;
852 }
853
854
855
856 static int
857 dissect_t38_INTEGER(tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
858   offset = dissect_per_integer(tvb, offset, actx, tree, hf_index, NULL);
859
860   return offset;
861 }
862
863
864
865 static int
866 dissect_t38_OCTET_STRING(tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
867   offset = dissect_per_octet_string(tvb, offset, actx, tree, hf_index,
868                                        NO_BOUND, NO_BOUND, FALSE, NULL);
869
870   return offset;
871 }
872
873
874 static const per_sequence_t T_fec_data_sequence_of[1] = {
875   { &hf_t38_fec_data_item   , ASN1_NO_EXTENSIONS     , ASN1_NOT_OPTIONAL, dissect_t38_OCTET_STRING },
876 };
877
878 static int
879 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_) {
880   offset = dissect_per_sequence_of(tvb, offset, actx, tree, hf_index,
881                                       ett_t38_T_fec_data, T_fec_data_sequence_of);
882
883   return offset;
884 }
885
886
887 static const per_sequence_t T_fec_info_sequence[] = {
888   { &hf_t38_fec_npackets    , ASN1_NO_EXTENSIONS     , ASN1_NOT_OPTIONAL, dissect_t38_INTEGER },
889   { &hf_t38_fec_data        , ASN1_NO_EXTENSIONS     , ASN1_NOT_OPTIONAL, dissect_t38_T_fec_data },
890   { NULL, 0, 0, NULL }
891 };
892
893 static int
894 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_) {
895   offset = dissect_per_sequence(tvb, offset, actx, tree, hf_index,
896                                    ett_t38_T_fec_info, T_fec_info_sequence);
897
898   return offset;
899 }
900
901
902 static const value_string t38_T_error_recovery_vals[] = {
903   {   0, "secondary-ifp-packets" },
904   {   1, "fec-info" },
905   { 0, NULL }
906 };
907
908 static const per_choice_t T_error_recovery_choice[] = {
909   {   0, &hf_t38_secondary_ifp_packets, ASN1_NO_EXTENSIONS     , dissect_t38_T_secondary_ifp_packets },
910   {   1, &hf_t38_fec_info        , ASN1_NO_EXTENSIONS     , dissect_t38_T_fec_info },
911   { 0, NULL, 0, NULL }
912 };
913
914 static int
915 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_) {
916 #line 249 "t38.cnf"
917     primary_part = FALSE;
918
919   offset = dissect_per_choice(tvb, offset, actx, tree, hf_index,
920                                  ett_t38_T_error_recovery, T_error_recovery_choice,
921                                  NULL);
922
923 #line 251 "t38.cnf"
924     primary_part = TRUE;
925
926   return offset;
927 }
928
929
930 static const per_sequence_t UDPTLPacket_sequence[] = {
931   { &hf_t38_seq_number      , ASN1_NO_EXTENSIONS     , ASN1_NOT_OPTIONAL, dissect_t38_T_seq_number },
932   { &hf_t38_primary_ifp_packet, ASN1_NO_EXTENSIONS     , ASN1_NOT_OPTIONAL, dissect_t38_T_primary_ifp_packet },
933   { &hf_t38_error_recovery  , ASN1_NO_EXTENSIONS     , ASN1_NOT_OPTIONAL, dissect_t38_T_error_recovery },
934   { NULL, 0, 0, NULL }
935 };
936
937 static int
938 dissect_t38_UDPTLPacket(tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
939 #line 223 "t38.cnf"
940     /* Initialize to something else than data type */
941     Data_Field_field_type_value = 1;
942
943   offset = dissect_per_sequence(tvb, offset, actx, tree, hf_index,
944                                    ett_t38_UDPTLPacket, UDPTLPacket_sequence);
945
946   return offset;
947 }
948
949 /*--- PDUs ---*/
950
951 static int dissect_IFPPacket_PDU(tvbuff_t *tvb _U_, packet_info *pinfo _U_, proto_tree *tree _U_) {
952   int offset = 0;
953   asn1_ctx_t asn1_ctx;
954   asn1_ctx_init(&asn1_ctx, ASN1_ENC_PER, TRUE, pinfo);
955   offset = dissect_t38_IFPPacket(tvb, offset, &asn1_ctx, tree, hf_t38_IFPPacket_PDU);
956   offset += 7; offset >>= 3;
957   return offset;
958 }
959 static int dissect_UDPTLPacket_PDU(tvbuff_t *tvb _U_, packet_info *pinfo _U_, proto_tree *tree _U_) {
960   int offset = 0;
961   asn1_ctx_t asn1_ctx;
962   asn1_ctx_init(&asn1_ctx, ASN1_ENC_PER, TRUE, pinfo);
963   offset = dissect_t38_UDPTLPacket(tvb, offset, &asn1_ctx, tree, hf_t38_UDPTLPacket_PDU);
964   offset += 7; offset >>= 3;
965   return offset;
966 }
967
968
969 /*--- End of included file: packet-t38-fn.c ---*/
970 #line 393 "packet-t38-template.c"
971
972 /* initialize the tap t38_info and the conversation */
973 static void
974 init_t38_info_conv(packet_info *pinfo)
975 {
976         /* tap info */
977         t38_info_current++;
978         if (t38_info_current==MAX_T38_MESSAGES_IN_PACKET) {
979                 t38_info_current=0;
980         }
981         t38_info = &t38_info_arr[t38_info_current];
982
983         t38_info->seq_num = 0;
984         t38_info->type_msg = 0;
985         t38_info->data_value = 0;
986         t38_info->t30ind_value =0;
987         t38_info->setup_frame_number = 0;
988         t38_info->Data_Field_field_type_value = 0;
989         t38_info->desc[0] = '\0';
990         t38_info->desc_comment[0] = '\0';
991         t38_info->time_first_t4_data = 0;
992         t38_info->frame_num_first_t4_data = 0;
993
994
995         /* 
996                 p_t38_packet_conv hold the conversation info in each of the packets.
997                 p_t38_conv hold the conversation info used to reassemble the HDLC packets, and also the Setup info (e.g SDP)
998                 If we already have p_t38_packet_conv in the packet, it means we already reassembled the HDLC packets, so we don't 
999                 need to use p_t38_conv 
1000         */
1001         p_t38_packet_conv = NULL;
1002         p_t38_conv = NULL;
1003
1004         /* Use existing packet info if available */
1005          p_t38_packet_conv = p_get_proto_data(pinfo->fd, proto_t38);
1006
1007
1008         /* find the conversation used for Reassemble and Setup Info */
1009         p_conv = find_conversation(pinfo->fd->num, &pinfo->net_dst, &pinfo->net_src,
1010                                    pinfo->ptype,
1011                                    pinfo->destport, pinfo->srcport, NO_ADDR_B | NO_PORT_B);
1012
1013         /* create a conv if it doen't exist */
1014         if (!p_conv) {
1015                 p_conv = conversation_new(pinfo->fd->num, &pinfo->net_src, &pinfo->net_dst,
1016                               pinfo->ptype, pinfo->srcport, pinfo->destport, NO_ADDR_B | NO_PORT_B);
1017
1018                 /* Set dissector */
1019                 conversation_set_dissector(p_conv, t38_udp_handle);
1020         }
1021
1022         if (!p_t38_packet_conv) {
1023                 p_t38_conv = conversation_get_proto_data(p_conv, proto_t38);
1024
1025                 /* create the conversation if it doen't exist */
1026                 if (!p_t38_conv) {
1027                         p_t38_conv = se_alloc(sizeof(t38_conv));
1028                         p_t38_conv->setup_method[0] = '\0';
1029                         p_t38_conv->setup_frame_number = 0;
1030
1031                         p_t38_conv->src_t38_info.reass_ID = 0;
1032                         p_t38_conv->src_t38_info.reass_start_seqnum = -1;
1033                         p_t38_conv->src_t38_info.reass_data_type = 0;
1034                         p_t38_conv->src_t38_info.last_seqnum = -1;
1035                         p_t38_conv->src_t38_info.packet_lost = 0;
1036                         p_t38_conv->src_t38_info.burst_lost = 0;
1037                         p_t38_conv->src_t38_info.time_first_t4_data = 0;
1038
1039                         p_t38_conv->dst_t38_info.reass_ID = 0;
1040                         p_t38_conv->dst_t38_info.reass_start_seqnum = -1;
1041                         p_t38_conv->dst_t38_info.reass_data_type = 0;
1042                         p_t38_conv->dst_t38_info.last_seqnum = -1;
1043                         p_t38_conv->dst_t38_info.packet_lost = 0;
1044                         p_t38_conv->dst_t38_info.burst_lost = 0;
1045                         p_t38_conv->dst_t38_info.time_first_t4_data = 0;
1046
1047                         conversation_add_proto_data(p_conv, proto_t38, p_t38_conv);
1048                 }
1049
1050                 /* copy the t38 conversation info to the packet t38 conversation */
1051                 p_t38_packet_conv = se_alloc(sizeof(t38_conv));
1052                 g_strlcpy(p_t38_packet_conv->setup_method, p_t38_conv->setup_method, MAX_T38_SETUP_METHOD_SIZE);
1053                 p_t38_packet_conv->setup_frame_number = p_t38_conv->setup_frame_number;
1054
1055                 memcpy(&(p_t38_packet_conv->src_t38_info), &(p_t38_conv->src_t38_info), sizeof(t38_conv_info));
1056                 memcpy(&(p_t38_packet_conv->dst_t38_info), &(p_t38_conv->dst_t38_info), sizeof(t38_conv_info));
1057
1058                 p_add_proto_data(pinfo->fd, proto_t38, p_t38_packet_conv);
1059         }
1060
1061         if (ADDRESSES_EQUAL(&p_conv->key_ptr->addr1, &pinfo->net_src)) {
1062                 p_t38_conv_info = &(p_t38_conv->src_t38_info);
1063                 p_t38_packet_conv_info = &(p_t38_packet_conv->src_t38_info);
1064         } else {
1065                 p_t38_conv_info = &(p_t38_conv->dst_t38_info);
1066                 p_t38_packet_conv_info = &(p_t38_packet_conv->dst_t38_info);
1067         }
1068
1069         /* update t38_info */
1070         t38_info->setup_frame_number = p_t38_packet_conv->setup_frame_number;
1071 }
1072
1073 /* Entry point for dissection */
1074 static void
1075 dissect_t38_udp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
1076 {
1077         guint8 octet1;
1078         proto_item *it;
1079         proto_tree *tr;
1080         guint32 offset=0;
1081
1082         /*
1083          * XXX - heuristic to check for misidentified packets.
1084          */
1085         if (dissect_possible_rtpv2_packets_as_rtp){
1086                 octet1 = tvb_get_guint8(tvb, offset);
1087                 if (RTP_VERSION(octet1) == 2){
1088                         call_dissector(rtp_handle,tvb,pinfo,tree);
1089                         return;
1090                 }
1091         }
1092
1093         col_set_str(pinfo->cinfo, COL_PROTOCOL, "T.38");
1094         col_clear(pinfo->cinfo, COL_INFO);
1095
1096         primary_part = TRUE;
1097
1098         /* This indicate the item number in the primary part of the T38 message, it is used for the reassemble of T30 packets */
1099         Data_Field_item_num = 0;
1100
1101         it=proto_tree_add_protocol_format(tree, proto_t38, tvb, 0, -1, "ITU-T Recommendation T.38");
1102         tr=proto_item_add_subtree(it, ett_t38);
1103
1104         /* init tap and conv info */
1105         init_t38_info_conv(pinfo);
1106
1107         /* Show Conversation setup info if exists*/
1108         if (global_t38_show_setup_info) {
1109                 show_setup_info(tvb, tr, p_t38_packet_conv);
1110         }
1111
1112         col_append_str(pinfo->cinfo, COL_INFO, "UDP: UDPTLPacket ");
1113
1114         offset = dissect_UDPTLPacket_PDU(tvb, pinfo, tr);
1115
1116         if (tvb_length_remaining(tvb,offset)>0){
1117                 if (tr){
1118                         proto_tree_add_text(tr, tvb, offset, tvb_reported_length_remaining(tvb, offset),
1119                                 "[MALFORMED PACKET or wrong preference settings]");
1120                 }
1121                 col_append_str(pinfo->cinfo, COL_INFO, " [Malformed?]");
1122         }
1123 }
1124
1125 static void
1126 dissect_t38_tcp_pdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
1127 {
1128         proto_item *it;
1129         proto_tree *tr;
1130         guint32 offset=0;
1131     tvbuff_t *next_tvb;
1132         guint16 ifp_packet_number=1;
1133
1134         col_set_str(pinfo->cinfo, COL_PROTOCOL, "T.38");
1135         col_clear(pinfo->cinfo, COL_INFO);
1136
1137         primary_part = TRUE;
1138
1139         /* This indicate the item number in the primary part of the T38 message, it is used for the reassemble of T30 packets */
1140         Data_Field_item_num = 0;
1141
1142         it=proto_tree_add_protocol_format(tree, proto_t38, tvb, 0, -1, "ITU-T Recommendation T.38");
1143         tr=proto_item_add_subtree(it, ett_t38);
1144
1145         /* init tap and conv info */
1146         init_t38_info_conv(pinfo);
1147
1148         /* Show Conversation setup info if exists*/
1149         if (global_t38_show_setup_info) {
1150                 show_setup_info(tvb, tr, p_t38_packet_conv);
1151         }
1152
1153         col_append_str(pinfo->cinfo, COL_INFO, "TCP: IFPPacket");
1154
1155         while(tvb_length_remaining(tvb,offset)>0)
1156         {
1157                 next_tvb = tvb_new_subset_remaining(tvb, offset);
1158                 offset += dissect_IFPPacket_PDU(next_tvb, pinfo, tr);
1159                 ifp_packet_number++;
1160
1161                 if(tvb_length_remaining(tvb,offset)>0){
1162                         if(t38_tpkt_usage == T38_TPKT_ALWAYS){
1163                                 if(tr){
1164                                         proto_tree_add_text(tr, tvb, offset, tvb_reported_length_remaining(tvb, offset),
1165                                                 "[MALFORMED PACKET or wrong preference settings]");
1166                                 }
1167                                 col_append_str(pinfo->cinfo, COL_INFO, " [Malformed?]");
1168                                 break;
1169                         } 
1170                         else {
1171                                 if (check_col(pinfo->cinfo, COL_INFO)){
1172                                         col_append_fstr(pinfo->cinfo, COL_INFO, " IFPPacket#%u",ifp_packet_number);
1173                                 }
1174                         }
1175                 }
1176         }
1177
1178 }
1179
1180 static void
1181 dissect_t38_tcp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
1182 {
1183         primary_part = TRUE;
1184
1185         if(t38_tpkt_usage == T38_TPKT_ALWAYS){
1186                 dissect_tpkt_encap(tvb,pinfo,tree,t38_tpkt_reassembly,t38_tcp_pdu_handle);
1187         } 
1188         else if((t38_tpkt_usage == T38_TPKT_NEVER) || (is_tpkt(tvb,1) == -1)){
1189                 dissect_t38_tcp_pdu(tvb, pinfo, tree);
1190         } 
1191         else {
1192                 dissect_tpkt_encap(tvb,pinfo,tree,t38_tpkt_reassembly,t38_tcp_pdu_handle);
1193         }
1194 }
1195
1196 static void
1197 dissect_t38(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
1198 {
1199         if(pinfo->ipproto == IP_PROTO_TCP)
1200         {
1201                 dissect_t38_tcp(tvb, pinfo, tree);
1202         }
1203         else if(pinfo->ipproto == IP_PROTO_UDP)
1204         {   
1205                 dissect_t38_udp(tvb, pinfo, tree);
1206         }
1207 }
1208
1209 /* Look for conversation info and display any setup info found */
1210 void 
1211 show_setup_info(tvbuff_t *tvb, proto_tree *tree, t38_conv *p_t38_conversation)
1212 {
1213         proto_tree *t38_setup_tree;
1214         proto_item *ti;
1215
1216         if (!p_t38_conversation || p_t38_conversation->setup_frame_number == 0) {
1217                 /* there is no Setup info */
1218                 return;
1219         }
1220
1221         ti =  proto_tree_add_string_format(tree, hf_t38_setup, tvb, 0, 0,
1222                       "",
1223                       "Stream setup by %s (frame %u)",
1224                       p_t38_conversation->setup_method,
1225                       p_t38_conversation->setup_frame_number);
1226     PROTO_ITEM_SET_GENERATED(ti);
1227     t38_setup_tree = proto_item_add_subtree(ti, ett_t38_setup);
1228     if (t38_setup_tree)
1229     {
1230                 /* Add details into subtree */
1231                 proto_item* item = proto_tree_add_uint(t38_setup_tree, hf_t38_setup_frame,
1232                                                                tvb, 0, 0, p_t38_conversation->setup_frame_number);
1233                 PROTO_ITEM_SET_GENERATED(item);
1234                 item = proto_tree_add_string(t38_setup_tree, hf_t38_setup_method,
1235                                                      tvb, 0, 0, p_t38_conversation->setup_method);
1236                 PROTO_ITEM_SET_GENERATED(item);
1237     }
1238 }
1239
1240
1241
1242 /* Wireshark Protocol Registration */
1243 void
1244 proto_register_t38(void)
1245 {
1246         static hf_register_info hf[] =
1247         {
1248
1249 /*--- Included file: packet-t38-hfarr.c ---*/
1250 #line 1 "packet-t38-hfarr.c"
1251     { &hf_t38_IFPPacket_PDU,
1252       { "IFPPacket", "t38.IFPPacket",
1253         FT_NONE, BASE_NONE, NULL, 0,
1254         NULL, HFILL }},
1255     { &hf_t38_UDPTLPacket_PDU,
1256       { "UDPTLPacket", "t38.UDPTLPacket",
1257         FT_NONE, BASE_NONE, NULL, 0,
1258         NULL, HFILL }},
1259     { &hf_t38_type_of_msg,
1260       { "type-of-msg", "t38.type_of_msg",
1261         FT_UINT32, BASE_DEC, VALS(t38_Type_of_msg_vals), 0,
1262         NULL, HFILL }},
1263     { &hf_t38_data_field,
1264       { "data-field", "t38.data_field",
1265         FT_UINT32, BASE_DEC, NULL, 0,
1266         NULL, HFILL }},
1267     { &hf_t38_t30_indicator,
1268       { "t30-indicator", "t38.t30_indicator",
1269         FT_UINT32, BASE_DEC, VALS(t38_T30_indicator_vals), 0,
1270         NULL, HFILL }},
1271     { &hf_t38_t30_data,
1272       { "t30-data", "t38.t30_data",
1273         FT_UINT32, BASE_DEC, VALS(t38_T30_data_vals), 0,
1274         NULL, HFILL }},
1275     { &hf_t38_Data_Field_item,
1276       { "Data-Field item", "t38.Data_Field_item",
1277         FT_NONE, BASE_NONE, NULL, 0,
1278         NULL, HFILL }},
1279     { &hf_t38_field_type,
1280       { "field-type", "t38.field_type",
1281         FT_UINT32, BASE_DEC, VALS(t38_T_field_type_vals), 0,
1282         NULL, HFILL }},
1283     { &hf_t38_field_data,
1284       { "field-data", "t38.field_data",
1285         FT_BYTES, BASE_NONE, NULL, 0,
1286         NULL, HFILL }},
1287     { &hf_t38_seq_number,
1288       { "seq-number", "t38.seq_number",
1289         FT_UINT32, BASE_DEC, NULL, 0,
1290         NULL, HFILL }},
1291     { &hf_t38_primary_ifp_packet,
1292       { "primary-ifp-packet", "t38.primary_ifp_packet",
1293         FT_NONE, BASE_NONE, NULL, 0,
1294         NULL, HFILL }},
1295     { &hf_t38_error_recovery,
1296       { "error-recovery", "t38.error_recovery",
1297         FT_UINT32, BASE_DEC, VALS(t38_T_error_recovery_vals), 0,
1298         NULL, HFILL }},
1299     { &hf_t38_secondary_ifp_packets,
1300       { "secondary-ifp-packets", "t38.secondary_ifp_packets",
1301         FT_UINT32, BASE_DEC, NULL, 0,
1302         NULL, HFILL }},
1303     { &hf_t38_secondary_ifp_packets_item,
1304       { "secondary-ifp-packets item", "t38.secondary_ifp_packets_item",
1305         FT_NONE, BASE_NONE, NULL, 0,
1306         "OpenType_IFPPacket", HFILL }},
1307     { &hf_t38_fec_info,
1308       { "fec-info", "t38.fec_info",
1309         FT_NONE, BASE_NONE, NULL, 0,
1310         NULL, HFILL }},
1311     { &hf_t38_fec_npackets,
1312       { "fec-npackets", "t38.fec_npackets",
1313         FT_INT32, BASE_DEC, NULL, 0,
1314         "INTEGER", HFILL }},
1315     { &hf_t38_fec_data,
1316       { "fec-data", "t38.fec_data",
1317         FT_UINT32, BASE_DEC, NULL, 0,
1318         NULL, HFILL }},
1319     { &hf_t38_fec_data_item,
1320       { "fec-data item", "t38.fec_data_item",
1321         FT_BYTES, BASE_NONE, NULL, 0,
1322         "OCTET_STRING", HFILL }},
1323
1324 /*--- End of included file: packet-t38-hfarr.c ---*/
1325 #line 671 "packet-t38-template.c"
1326                 {   &hf_t38_setup,
1327                     { "Stream setup", "t38.setup", FT_STRING, BASE_NONE,
1328                     NULL, 0x0, "Stream setup, method and frame number", HFILL }},
1329                 {   &hf_t38_setup_frame,
1330             { "Stream frame", "t38.setup-frame", FT_FRAMENUM, BASE_NONE,
1331             NULL, 0x0, "Frame that set up this stream", HFILL }},
1332         {   &hf_t38_setup_method,
1333             { "Stream Method", "t38.setup-method", FT_STRING, BASE_NONE,
1334             NULL, 0x0, "Method used to set up this stream", HFILL }},
1335                 {&hf_t38_fragments,
1336                         {"Message fragments", "t38.fragments",
1337                         FT_NONE, BASE_NONE, NULL, 0x00, NULL, HFILL } },
1338                 {&hf_t38_fragment,
1339                         {"Message fragment", "t38.fragment",
1340                         FT_FRAMENUM, BASE_NONE, NULL, 0x00, NULL, HFILL } },
1341                 {&hf_t38_fragment_overlap,
1342                         {"Message fragment overlap", "t38.fragment.overlap",
1343                         FT_BOOLEAN, BASE_NONE, NULL, 0x0, NULL, HFILL } },
1344                 {&hf_t38_fragment_overlap_conflicts,
1345                         {"Message fragment overlapping with conflicting data",
1346                         "t38.fragment.overlap.conflicts",
1347                         FT_BOOLEAN, BASE_NONE, NULL, 0x0, NULL, HFILL } },
1348                 {&hf_t38_fragment_multiple_tails,
1349                         {"Message has multiple tail fragments",
1350                         "t38.fragment.multiple_tails", 
1351                         FT_BOOLEAN, BASE_NONE, NULL, 0x0, NULL, HFILL } },
1352                 {&hf_t38_fragment_too_long_fragment,
1353                         {"Message fragment too long", "t38.fragment.too_long_fragment",
1354                         FT_BOOLEAN, BASE_NONE, NULL, 0x0, NULL, HFILL } },
1355                 {&hf_t38_fragment_error,
1356                         {"Message defragmentation error", "t38.fragment.error",
1357                         FT_FRAMENUM, BASE_NONE, NULL, 0x00, NULL, HFILL } },
1358                 {&hf_t38_reassembled_in,
1359                         {"Reassembled in", "t38.reassembled.in",
1360                         FT_FRAMENUM, BASE_NONE, NULL, 0x00, NULL, HFILL } },
1361                 {&hf_t38_reassembled_length,
1362                         {"Reassembled T38 length", "t38.reassembled.length",
1363                         FT_UINT32, BASE_DEC, NULL, 0x00, NULL, HFILL } },
1364         };
1365
1366         static gint *ett[] =
1367         {
1368                 &ett_t38,
1369
1370 /*--- Included file: packet-t38-ettarr.c ---*/
1371 #line 1 "packet-t38-ettarr.c"
1372     &ett_t38_IFPPacket,
1373     &ett_t38_Type_of_msg,
1374     &ett_t38_Data_Field,
1375     &ett_t38_Data_Field_item,
1376     &ett_t38_UDPTLPacket,
1377     &ett_t38_T_error_recovery,
1378     &ett_t38_T_secondary_ifp_packets,
1379     &ett_t38_T_fec_info,
1380     &ett_t38_T_fec_data,
1381
1382 /*--- End of included file: packet-t38-ettarr.c ---*/
1383 #line 715 "packet-t38-template.c"
1384                 &ett_t38_setup,
1385                 &ett_data_fragment,
1386                 &ett_data_fragments
1387         };
1388
1389         module_t *t38_module;
1390
1391         proto_t38 = proto_register_protocol("T.38", "T.38", "t38");
1392         proto_register_field_array(proto_t38, hf, array_length(hf));
1393         proto_register_subtree_array(ett, array_length(ett));
1394         register_dissector("t38", dissect_t38, proto_t38);
1395
1396         /* Init reassemble tables for HDLC */
1397         register_init_routine(t38_defragment_init);
1398
1399         t38_tap = register_tap("t38");
1400
1401         t38_module = prefs_register_protocol(proto_t38, proto_reg_handoff_t38);
1402         prefs_register_bool_preference(t38_module, "use_pre_corrigendum_asn1_specification",
1403             "Use the Pre-Corrigendum ASN.1 specification",
1404             "Whether the T.38 dissector should decode using the Pre-Corrigendum T.38 "
1405                 "ASN.1 specification (1998).",
1406             &use_pre_corrigendum_asn1_specification);
1407         prefs_register_bool_preference(t38_module, "dissect_possible_rtpv2_packets_as_rtp",
1408             "Dissect possible RTP version 2 packets with RTP dissector",
1409             "Whether a UDP packet that looks like RTP version 2 packet will "
1410                 "be dissected as RTP packet or T.38 packet. If enabled there is a risk that T.38 UDPTL "
1411                 "packets with sequence number higher than 32767 may be dissected as RTP.",
1412             &dissect_possible_rtpv2_packets_as_rtp);
1413         prefs_register_uint_preference(t38_module, "tcp.port",
1414                 "T.38 TCP Port",
1415                 "Set the TCP port for T.38 messages",
1416                 10, &global_t38_tcp_port);
1417         prefs_register_uint_preference(t38_module, "udp.port",
1418                 "T.38 UDP Port",
1419                 "Set the UDP port for T.38 messages",
1420                 10, &global_t38_udp_port);      
1421         prefs_register_bool_preference(t38_module, "reassembly",
1422                 "Reassemble T.38 PDUs over TPKT over TCP",
1423                 "Whether the dissector should reassemble T.38 PDUs spanning multiple TCP segments "
1424                 "when TPKT is used over TCP. "
1425                 "To use this option, you must also enable \"Allow subdissectors to reassemble "
1426                 "TCP streams\" in the TCP protocol settings.",
1427                 &t38_tpkt_reassembly);
1428         prefs_register_enum_preference(t38_module, "tpkt_usage",
1429                 "TPKT used over TCP",
1430                 "Whether T.38 is used with TPKT for TCP",
1431                 (gint *)&t38_tpkt_usage,t38_tpkt_options,FALSE);
1432
1433         prefs_register_bool_preference(t38_module, "show_setup_info",
1434                 "Show stream setup information",
1435                 "Where available, show which protocol and frame caused "
1436                 "this T.38 stream to be created",
1437                 &global_t38_show_setup_info);
1438
1439 }
1440
1441 void
1442 proto_reg_handoff_t38(void)
1443 {
1444         static gboolean t38_prefs_initialized = FALSE;
1445         static guint tcp_port;
1446         static guint udp_port;
1447
1448         if (!t38_prefs_initialized) {
1449                 t38_udp_handle=create_dissector_handle(dissect_t38_udp, proto_t38);
1450                 t38_tcp_handle=create_dissector_handle(dissect_t38_tcp, proto_t38);
1451                 t38_tcp_pdu_handle=create_dissector_handle(dissect_t38_tcp_pdu, proto_t38);
1452                 rtp_handle = find_dissector("rtp");
1453                 t30_hdlc_handle = find_dissector("t30.hdlc");
1454                 data_handle = find_dissector("data");
1455                 t38_prefs_initialized = TRUE;
1456         }
1457         else {
1458                 dissector_delete_uint("tcp.port", tcp_port, t38_tcp_handle);
1459                 dissector_delete_uint("udp.port", udp_port, t38_udp_handle);
1460         }
1461         tcp_port = global_t38_tcp_port;
1462         udp_port = global_t38_udp_port;
1463
1464         dissector_add_uint("tcp.port", tcp_port, t38_tcp_handle);
1465         dissector_add_uint("udp.port", udp_port, t38_udp_handle);
1466
1467 }
1468