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