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