5ed1b73282d1602ef61aaf87bf6e4c939f4cc616
[obnox/wireshark/wip.git] / asn1 / t38 / packet-t38-template.c
1 /* packet-t38.c
2  * Routines for T.38 packet dissection
3  * 2003  Hans Viens
4  * 2004  Alejandro Vaquero, add support Conversations for SDP
5  * 2006  Alejandro Vaquero, add T30 reassemble and dissection
6  *
7  * $Id$
8  *
9  * Wireshark - Network traffic analyzer
10  * By Gerald Combs <gerald@wireshark.org>
11  * Copyright 1998 Gerald Combs
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
26  */
27
28
29 /* Depending on what ASN.1 specification is used you may have to change
30  * the preference setting regarding Pre-Corrigendum ASN.1 specification:
31  * http://www.itu.int/ITU-T/asn1/database/itu-t/t/t38/1998/T38.html  (Pre-Corrigendum=TRUE)
32  * http://www.itu.int/ITU-T/asn1/database/itu-t/t/t38/2003/T38(1998).html (Pre-Corrigendum=TRUE)
33  *
34  * http://www.itu.int/ITU-T/asn1/database/itu-t/t/t38/2003/T38(2002).html (Pre-Corrigendum=FALSE)
35  * http://www.itu.int/ITU-T/asn1/database/itu-t/t/t38/2002/t38.html  (Pre-Corrigendum=FALSE)
36  * http://www.itu.int/ITU-T/asn1/database/itu-t/t/t38/2002-Amd1/T38.html (Pre-Corrigendum=FALSE)
37  */
38
39 /* TO DO:  
40  * - TCP desegmentation is currently not supported for T.38 IFP directly over TCP. 
41  * - H.245 dissectors should be updated to start conversations for T.38 similar to RTP.
42  * - Sometimes the last octet is not high-lighted when selecting something in the tree. Bug in PER dissector? 
43  * - Add support for RTP payload audio/t38 (draft-jones-avt-audio-t38-03.txt), i.e. T38 in RTP packets.
44  */
45
46
47 #ifdef HAVE_CONFIG_H
48 # include "config.h"
49 #endif
50
51 #include <glib.h>
52 #include <epan/packet.h>
53 #include <epan/reassemble.h>
54 #include <epan/conversation.h>
55 #include <epan/tap.h>
56 #include <epan/expert.h>
57
58 #include <string.h>
59
60 #include "packet-t38.h"
61 #include <epan/prefs.h>
62 #include <epan/ipproto.h>
63 #include <epan/asn1.h>
64 #include "packet-per.h"
65 #include "packet-tpkt.h"
66 #include <epan/emem.h>
67 #include <epan/strutil.h>
68
69 #define PORT_T38 6004  
70 static guint global_t38_tcp_port = PORT_T38;
71 static guint global_t38_udp_port = PORT_T38;
72
73 static int t38_tap = -1;
74
75 /* dissect using the Pre Corrigendum T.38 ASN.1 specification (1998) */
76 static gboolean use_pre_corrigendum_asn1_specification = TRUE;
77
78 /* dissect packets that looks like RTP version 2 packets as RTP     */
79 /* instead of as T.38. This may result in that some T.38 UPTL       */
80 /* packets with sequence number values higher than 32767 may be     */
81 /* shown as RTP packets.                                            */ 
82 static gboolean dissect_possible_rtpv2_packets_as_rtp = FALSE;
83
84
85 /* Reassembly of T.38 PDUs over TPKT over TCP */
86 static gboolean t38_tpkt_reassembly = TRUE;
87
88 /* Preference setting whether TPKT header is used when sending T.38 over TCP.
89  * The default setting is Maybe where the dissector will look on the first
90  * bytes to try to determine whether TPKT header is used or not. This may not
91  * work so well in some cases. You may want to change the setting to Always or
92  * Newer.
93  */
94 #define T38_TPKT_NEVER 0   /* Assume that there is never a TPKT header    */
95 #define T38_TPKT_ALWAYS 1  /* Assume that there is always a TPKT header   */
96 #define T38_TPKT_MAYBE 2   /* Assume TPKT if first octets are 03-00-xx-xx */
97 static gint t38_tpkt_usage = T38_TPKT_MAYBE;
98
99 static const enum_val_t t38_tpkt_options[] = {
100   {"never", "Never", T38_TPKT_NEVER},
101   {"always", "Always", T38_TPKT_ALWAYS},
102   {"maybe", "Maybe", T38_TPKT_MAYBE},
103   {NULL, NULL, -1}
104 };
105
106
107
108 /* T38 */
109 static dissector_handle_t t38_udp_handle;
110 static dissector_handle_t t38_tcp_handle;
111 static dissector_handle_t t38_tcp_pdu_handle;
112 static dissector_handle_t rtp_handle;
113 static dissector_handle_t t30_hdlc_handle;
114 static dissector_handle_t data_handle;
115
116 static gint32 Type_of_msg_value;
117 static guint32 Data_Field_field_type_value;
118 static guint32 Data_value;
119 static guint32 T30ind_value;
120 static guint32 Data_Field_item_num;
121
122 static int proto_t38 = -1;
123 #include "packet-t38-hf.c"
124
125 /* T38 setup fields */
126 static int hf_t38_setup        = -1;
127 static int hf_t38_setup_frame  = -1;
128 static int hf_t38_setup_method = -1;
129
130 /* T38 Data reassemble fields */
131 static int hf_t38_fragments = -1;
132 static int hf_t38_fragment = -1;
133 static int hf_t38_fragment_overlap = -1;
134 static int hf_t38_fragment_overlap_conflicts = -1;
135 static int hf_t38_fragment_multiple_tails = -1;
136 static int hf_t38_fragment_too_long_fragment = -1;
137 static int hf_t38_fragment_error = -1;
138 static int hf_t38_reassembled_in = -1;
139 static int hf_t38_reassembled_length = -1;
140
141 static gint ett_t38 = -1;
142 #include "packet-t38-ett.c"
143 static gint ett_t38_setup = -1;
144
145 static gint ett_data_fragment = -1;
146 static gint ett_data_fragments = -1;
147
148 static gboolean primary_part = TRUE;
149 static guint32 seq_number = 0;
150
151 /* Tables for reassembly of Data fragments. */
152 static GHashTable *data_fragment_table = NULL;
153
154 static const fragment_items data_frag_items = {
155         /* Fragment subtrees */
156         &ett_data_fragment,
157         &ett_data_fragments,
158         /* Fragment fields */
159         &hf_t38_fragments,
160         &hf_t38_fragment,
161         &hf_t38_fragment_overlap,
162         &hf_t38_fragment_overlap_conflicts,
163         &hf_t38_fragment_multiple_tails,
164         &hf_t38_fragment_too_long_fragment,
165         &hf_t38_fragment_error,
166         /* Reassembled in field */
167         &hf_t38_reassembled_in,
168         /* Reassembled length field */
169         &hf_t38_reassembled_length,
170         /* Tag */
171         "Data fragments"
172 };
173
174 typedef struct _fragment_key {
175         address src;
176         address dst;
177         guint32 id;
178 } fragment_key;
179
180 static conversation_t *p_conv= NULL;
181 static t38_conv *p_t38_conv = NULL;
182 static t38_conv *p_t38_packet_conv = NULL;
183 static t38_conv_info *p_t38_conv_info = NULL;
184 static t38_conv_info *p_t38_packet_conv_info = NULL;
185
186 /* RTP Version is the first 2 bits of the first octet in the UDP payload*/
187 #define RTP_VERSION(octet)      ((octet) >> 6)
188
189 void proto_reg_handoff_t38(void);
190
191 static void show_setup_info(tvbuff_t *tvb, proto_tree *tree, t38_conv *p_t38_conv);
192 /* Preferences bool to control whether or not setup info should be shown */
193 static gboolean global_t38_show_setup_info = TRUE;
194
195 /* Can tap up to 4 T38 packets within same packet */
196 /* We only tap the primary part, not the redundancy */
197 #define MAX_T38_MESSAGES_IN_PACKET 4
198 static t38_packet_info t38_info_arr[MAX_T38_MESSAGES_IN_PACKET];
199 static int t38_info_current=0;
200 static t38_packet_info *t38_info=NULL;
201
202 static void t38_defragment_init(void)
203 {
204         /* Init reassemble tables */
205         fragment_table_init(&data_fragment_table);
206 }
207
208
209 /* Set up an T38 conversation */
210 void t38_add_address(packet_info *pinfo,
211                      address *addr, int port,
212                      int other_port,
213                      const gchar *setup_method, guint32 setup_frame_number)
214 {
215         address null_addr;
216         conversation_t* p_conversation;
217         t38_conv* p_conversation_data = NULL;
218
219         /*
220          * If this isn't the first time this packet has been processed,
221          * we've already done this work, so we don't need to do it
222          * again.
223          */
224         if (pinfo->fd->flags.visited)
225         {
226                 return;
227         }
228
229         SET_ADDRESS(&null_addr, AT_NONE, 0, NULL);
230
231         /*
232          * Check if the ip address and port combination is not
233          * already registered as a conversation.
234          */
235         p_conversation = find_conversation( setup_frame_number, addr, &null_addr, PT_UDP, port, other_port,
236                                 NO_ADDR_B | (!other_port ? NO_PORT_B : 0));
237
238         /*
239          * If not, create a new conversation.
240          */
241         if ( !p_conversation || p_conversation->setup_frame != setup_frame_number) {
242                 p_conversation = conversation_new( setup_frame_number, addr, &null_addr, PT_UDP,
243                                            (guint32)port, (guint32)other_port,
244                                                                    NO_ADDR2 | (!other_port ? NO_PORT2 : 0));
245         }
246
247         /* Set dissector */
248         conversation_set_dissector(p_conversation, t38_udp_handle);
249
250         /*
251          * Check if the conversation has data associated with it.
252          */
253         p_conversation_data = conversation_get_proto_data(p_conversation, proto_t38);
254
255         /*
256          * If not, add a new data item.
257          */
258         if ( ! p_conversation_data ) {
259                 /* Create conversation data */
260                 p_conversation_data = se_alloc(sizeof(t38_conv));
261
262                 conversation_add_proto_data(p_conversation, proto_t38, p_conversation_data);
263         }
264
265         /*
266          * Update the conversation data.
267          */
268         g_strlcpy(p_conversation_data->setup_method, setup_method, MAX_T38_SETUP_METHOD_SIZE);
269         p_conversation_data->setup_frame_number = setup_frame_number;
270                 p_conversation_data->src_t38_info.reass_ID = 0;
271                 p_conversation_data->src_t38_info.reass_start_seqnum = -1;
272                 p_conversation_data->src_t38_info.reass_data_type = 0;
273                 p_conversation_data->src_t38_info.last_seqnum = -1;
274                 p_conversation_data->src_t38_info.packet_lost = 0;
275                 p_conversation_data->src_t38_info.burst_lost = 0;
276                 p_conversation_data->src_t38_info.time_first_t4_data = 0;
277
278
279                 p_conversation_data->dst_t38_info.reass_ID = 0;
280                 p_conversation_data->dst_t38_info.reass_start_seqnum = -1;
281                 p_conversation_data->dst_t38_info.reass_data_type = 0;
282                 p_conversation_data->dst_t38_info.last_seqnum = -1;
283                 p_conversation_data->dst_t38_info.packet_lost = 0;
284                 p_conversation_data->dst_t38_info.burst_lost = 0;
285                 p_conversation_data->dst_t38_info.time_first_t4_data = 0;
286 }
287
288
289 fragment_data *
290 force_reassemble_seq(packet_info *pinfo, guint32 id,
291              GHashTable *fragment_table)
292 {
293         fragment_key key;
294         fragment_data *fd_head;
295         fragment_data *fd_i;
296         fragment_data *last_fd;
297         guint32 dfpos, size, packet_lost, burst_lost, seq_num;
298
299         /* create key to search hash with */
300         key.src = pinfo->src;
301         key.dst = pinfo->dst;
302         key.id  = id;
303
304         fd_head = g_hash_table_lookup(fragment_table, &key);
305
306         /* have we already seen this frame ?*/
307         if (pinfo->fd->flags.visited) {
308                 if (fd_head != NULL && fd_head->flags & FD_DEFRAGMENTED) {
309                         return fd_head;
310                 } else {
311                         return NULL;
312                 }
313         }
314
315         if (fd_head==NULL){
316                 /* we must have it to continue */
317                 return NULL;
318         }
319
320         /* check for packet lost and count the burst of packet lost */
321         packet_lost = 0;
322         burst_lost = 0;
323         seq_num = 0;
324         for(fd_i=fd_head->next;fd_i;fd_i=fd_i->next) {
325                 if (seq_num != fd_i->offset) {
326                         packet_lost += fd_i->offset - seq_num;
327                         if ( (fd_i->offset - seq_num) > burst_lost ) {
328                                 burst_lost = fd_i->offset - seq_num;
329                         }
330                 }
331                 seq_num = fd_i->offset + 1;
332         }
333
334         /* we have received an entire packet, defragment it and
335      * free all fragments
336      */
337         size=0;
338         last_fd=NULL;
339         for(fd_i=fd_head->next;fd_i;fd_i=fd_i->next) {
340           if(!last_fd || last_fd->offset!=fd_i->offset){
341             size+=fd_i->len;
342           }
343           last_fd=fd_i;
344         }
345         fd_head->data = g_malloc(size);
346         fd_head->len = size;            /* record size for caller       */
347
348         /* add all data fragments */
349         dfpos = 0;
350         last_fd=NULL;
351         for (fd_i=fd_head->next;fd_i && fd_i->len + dfpos <= size;fd_i=fd_i->next) {
352           if (fd_i->len) {
353             if(!last_fd || last_fd->offset!=fd_i->offset){
354               memcpy(fd_head->data+dfpos,fd_i->data,fd_i->len);
355               dfpos += fd_i->len;
356             } else {
357               /* duplicate/retransmission/overlap */
358               fd_i->flags    |= FD_OVERLAP;
359               fd_head->flags |= FD_OVERLAP;
360               if( (last_fd->len!=fd_i->datalen)
361                   || memcmp(last_fd->data, fd_i->data, last_fd->len) ){
362                         fd_i->flags    |= FD_OVERLAPCONFLICT;
363                         fd_head->flags |= FD_OVERLAPCONFLICT;
364               }
365             }
366           }
367           last_fd=fd_i;
368         }
369
370         /* we have defragmented the pdu, now free all fragments*/
371         for (fd_i=fd_head->next;fd_i;fd_i=fd_i->next) {
372           if(fd_i->data){
373             g_free(fd_i->data);
374             fd_i->data=NULL;
375           }
376         }
377
378         /* mark this packet as defragmented */
379         fd_head->flags |= FD_DEFRAGMENTED;
380         fd_head->reassembled_in=pinfo->fd->num;
381
382         if (check_col(pinfo->cinfo, COL_INFO))
383                         col_append_fstr(pinfo->cinfo, COL_INFO, " (t4-data Reassembled: %d pack lost, %d pack burst lost)", packet_lost, burst_lost);
384         
385         p_t38_packet_conv_info->packet_lost = packet_lost;
386         p_t38_packet_conv_info->burst_lost = burst_lost;
387
388         return fd_head;
389 }
390
391 /* T38 Routines */
392 #include "packet-t38-fn.c"
393
394 /* initialize the tap t38_info and the conversation */
395 static void
396 init_t38_info_conv(packet_info *pinfo)
397 {
398         /* tap info */
399         t38_info_current++;
400         if (t38_info_current==MAX_T38_MESSAGES_IN_PACKET) {
401                 t38_info_current=0;
402         }
403         t38_info = &t38_info_arr[t38_info_current];
404
405         t38_info->seq_num = 0;
406         t38_info->type_msg = 0;
407         t38_info->data_value = 0;
408         t38_info->t30ind_value =0;
409         t38_info->setup_frame_number = 0;
410         t38_info->Data_Field_field_type_value = 0;
411         t38_info->desc[0] = '\0';
412         t38_info->desc_comment[0] = '\0';
413         t38_info->time_first_t4_data = 0;
414         t38_info->frame_num_first_t4_data = 0;
415
416
417         /* 
418                 p_t38_packet_conv hold the conversation info in each of the packets.
419                 p_t38_conv hold the conversation info used to reassemble the HDLC packets, and also the Setup info (e.g SDP)
420                 If we already have p_t38_packet_conv in the packet, it means we already reassembled the HDLC packets, so we don't 
421                 need to use p_t38_conv 
422         */
423         p_t38_packet_conv = NULL;
424         p_t38_conv = NULL;
425
426         /* Use existing packet info if available */
427          p_t38_packet_conv = p_get_proto_data(pinfo->fd, proto_t38);
428
429
430         /* find the conversation used for Reassemble and Setup Info */
431         p_conv = find_conversation(pinfo->fd->num, &pinfo->net_dst, &pinfo->net_src,
432                                    pinfo->ptype,
433                                    pinfo->destport, pinfo->srcport, NO_ADDR_B | NO_PORT_B);
434
435         /* create a conv if it doen't exist */
436         if (!p_conv) {
437                 p_conv = conversation_new(pinfo->fd->num, &pinfo->net_src, &pinfo->net_dst,
438                               pinfo->ptype, pinfo->srcport, pinfo->destport, NO_ADDR_B | NO_PORT_B);
439
440                 /* Set dissector */
441                 conversation_set_dissector(p_conv, t38_udp_handle);
442         }
443
444         if (!p_t38_packet_conv) {
445                 p_t38_conv = conversation_get_proto_data(p_conv, proto_t38);
446
447                 /* create the conversation if it doen't exist */
448                 if (!p_t38_conv) {
449                         p_t38_conv = se_alloc(sizeof(t38_conv));
450                         p_t38_conv->setup_method[0] = '\0';
451                         p_t38_conv->setup_frame_number = 0;
452
453                         p_t38_conv->src_t38_info.reass_ID = 0;
454                         p_t38_conv->src_t38_info.reass_start_seqnum = -1;
455                         p_t38_conv->src_t38_info.reass_data_type = 0;
456                         p_t38_conv->src_t38_info.last_seqnum = -1;
457                         p_t38_conv->src_t38_info.packet_lost = 0;
458                         p_t38_conv->src_t38_info.burst_lost = 0;
459                         p_t38_conv->src_t38_info.time_first_t4_data = 0;
460
461                         p_t38_conv->dst_t38_info.reass_ID = 0;
462                         p_t38_conv->dst_t38_info.reass_start_seqnum = -1;
463                         p_t38_conv->dst_t38_info.reass_data_type = 0;
464                         p_t38_conv->dst_t38_info.last_seqnum = -1;
465                         p_t38_conv->dst_t38_info.packet_lost = 0;
466                         p_t38_conv->dst_t38_info.burst_lost = 0;
467                         p_t38_conv->dst_t38_info.time_first_t4_data = 0;
468
469                         conversation_add_proto_data(p_conv, proto_t38, p_t38_conv);
470                 }
471
472                 /* copy the t38 conversation info to the packet t38 conversation */
473                 p_t38_packet_conv = se_alloc(sizeof(t38_conv));
474                 g_strlcpy(p_t38_packet_conv->setup_method, p_t38_conv->setup_method, MAX_T38_SETUP_METHOD_SIZE);
475                 p_t38_packet_conv->setup_frame_number = p_t38_conv->setup_frame_number;
476
477                 memcpy(&(p_t38_packet_conv->src_t38_info), &(p_t38_conv->src_t38_info), sizeof(t38_conv_info));
478                 memcpy(&(p_t38_packet_conv->dst_t38_info), &(p_t38_conv->dst_t38_info), sizeof(t38_conv_info));
479
480                 p_add_proto_data(pinfo->fd, proto_t38, p_t38_packet_conv);
481         }
482
483         if (ADDRESSES_EQUAL(&p_conv->key_ptr->addr1, &pinfo->net_src)) {
484                 p_t38_conv_info = &(p_t38_conv->src_t38_info);
485                 p_t38_packet_conv_info = &(p_t38_packet_conv->src_t38_info);
486         } else {
487                 p_t38_conv_info = &(p_t38_conv->dst_t38_info);
488                 p_t38_packet_conv_info = &(p_t38_packet_conv->dst_t38_info);
489         }
490
491         /* update t38_info */
492         t38_info->setup_frame_number = p_t38_packet_conv->setup_frame_number;
493 }
494
495 /* Entry point for dissection */
496 static void
497 dissect_t38_udp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
498 {
499         guint8 octet1;
500         proto_item *it;
501         proto_tree *tr;
502         guint32 offset=0;
503
504         /*
505          * XXX - heuristic to check for misidentified packets.
506          */
507         if (dissect_possible_rtpv2_packets_as_rtp){
508                 octet1 = tvb_get_guint8(tvb, offset);
509                 if (RTP_VERSION(octet1) == 2){
510                         call_dissector(rtp_handle,tvb,pinfo,tree);
511                         return;
512                 }
513         }
514
515         col_set_str(pinfo->cinfo, COL_PROTOCOL, "T.38");
516         col_clear(pinfo->cinfo, COL_INFO);
517
518         primary_part = TRUE;
519
520         /* This indicate the item number in the primary part of the T38 message, it is used for the reassemble of T30 packets */
521         Data_Field_item_num = 0;
522
523         it=proto_tree_add_protocol_format(tree, proto_t38, tvb, 0, -1, "ITU-T Recommendation T.38");
524         tr=proto_item_add_subtree(it, ett_t38);
525
526         /* init tap and conv info */
527         init_t38_info_conv(pinfo);
528
529         /* Show Conversation setup info if exists*/
530         if (global_t38_show_setup_info) {
531                 show_setup_info(tvb, tr, p_t38_packet_conv);
532         }
533
534         col_append_str(pinfo->cinfo, COL_INFO, "UDP: UDPTLPacket ");
535
536         offset = dissect_UDPTLPacket_PDU(tvb, pinfo, tr);
537
538         if (tvb_length_remaining(tvb,offset)>0){
539                 if (tr){
540                         proto_tree_add_text(tr, tvb, offset, tvb_reported_length_remaining(tvb, offset),
541                                 "[MALFORMED PACKET or wrong preference settings]");
542                 }
543                 col_append_str(pinfo->cinfo, COL_INFO, " [Malformed?]");
544         }
545 }
546
547 static void
548 dissect_t38_tcp_pdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
549 {
550         proto_item *it;
551         proto_tree *tr;
552         guint32 offset=0;
553     tvbuff_t *next_tvb;
554         guint16 ifp_packet_number=1;
555
556         col_set_str(pinfo->cinfo, COL_PROTOCOL, "T.38");
557         col_clear(pinfo->cinfo, COL_INFO);
558
559         primary_part = TRUE;
560
561         /* This indicate the item number in the primary part of the T38 message, it is used for the reassemble of T30 packets */
562         Data_Field_item_num = 0;
563
564         it=proto_tree_add_protocol_format(tree, proto_t38, tvb, 0, -1, "ITU-T Recommendation T.38");
565         tr=proto_item_add_subtree(it, ett_t38);
566
567         /* init tap and conv info */
568         init_t38_info_conv(pinfo);
569
570         /* Show Conversation setup info if exists*/
571         if (global_t38_show_setup_info) {
572                 show_setup_info(tvb, tr, p_t38_packet_conv);
573         }
574
575         col_append_str(pinfo->cinfo, COL_INFO, "TCP: IFPPacket");
576
577         while(tvb_length_remaining(tvb,offset)>0)
578         {
579                 next_tvb = tvb_new_subset_remaining(tvb, offset);
580                 offset += dissect_IFPPacket_PDU(next_tvb, pinfo, tr);
581                 ifp_packet_number++;
582
583                 if(tvb_length_remaining(tvb,offset)>0){
584                         if(t38_tpkt_usage == T38_TPKT_ALWAYS){
585                                 if(tr){
586                                         proto_tree_add_text(tr, tvb, offset, tvb_reported_length_remaining(tvb, offset),
587                                                 "[MALFORMED PACKET or wrong preference settings]");
588                                 }
589                                 col_append_str(pinfo->cinfo, COL_INFO, " [Malformed?]");
590                                 break;
591                         } 
592                         else {
593                                 if (check_col(pinfo->cinfo, COL_INFO)){
594                                         col_append_fstr(pinfo->cinfo, COL_INFO, " IFPPacket#%u",ifp_packet_number);
595                                 }
596                         }
597                 }
598         }
599
600 }
601
602 static void
603 dissect_t38_tcp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
604 {
605         primary_part = TRUE;
606
607         if(t38_tpkt_usage == T38_TPKT_ALWAYS){
608                 dissect_tpkt_encap(tvb,pinfo,tree,t38_tpkt_reassembly,t38_tcp_pdu_handle);
609         } 
610         else if((t38_tpkt_usage == T38_TPKT_NEVER) || (is_tpkt(tvb,1) == -1)){
611                 dissect_t38_tcp_pdu(tvb, pinfo, tree);
612         } 
613         else {
614                 dissect_tpkt_encap(tvb,pinfo,tree,t38_tpkt_reassembly,t38_tcp_pdu_handle);
615         }
616 }
617
618 static void
619 dissect_t38(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
620 {
621         if(pinfo->ipproto == IP_PROTO_TCP)
622         {
623                 dissect_t38_tcp(tvb, pinfo, tree);
624         }
625         else if(pinfo->ipproto == IP_PROTO_UDP)
626         {   
627                 dissect_t38_udp(tvb, pinfo, tree);
628         }
629 }
630
631 /* Look for conversation info and display any setup info found */
632 void 
633 show_setup_info(tvbuff_t *tvb, proto_tree *tree, t38_conv *p_t38_conversation)
634 {
635         proto_tree *t38_setup_tree;
636         proto_item *ti;
637
638         if (!p_t38_conversation || p_t38_conversation->setup_frame_number == 0) {
639                 /* there is no Setup info */
640                 return;
641         }
642
643         ti =  proto_tree_add_string_format(tree, hf_t38_setup, tvb, 0, 0,
644                       "",
645                       "Stream setup by %s (frame %u)",
646                       p_t38_conversation->setup_method,
647                       p_t38_conversation->setup_frame_number);
648     PROTO_ITEM_SET_GENERATED(ti);
649     t38_setup_tree = proto_item_add_subtree(ti, ett_t38_setup);
650     if (t38_setup_tree)
651     {
652                 /* Add details into subtree */
653                 proto_item* item = proto_tree_add_uint(t38_setup_tree, hf_t38_setup_frame,
654                                                                tvb, 0, 0, p_t38_conversation->setup_frame_number);
655                 PROTO_ITEM_SET_GENERATED(item);
656                 item = proto_tree_add_string(t38_setup_tree, hf_t38_setup_method,
657                                                      tvb, 0, 0, p_t38_conversation->setup_method);
658                 PROTO_ITEM_SET_GENERATED(item);
659     }
660 }
661
662
663
664 /* Wireshark Protocol Registration */
665 void
666 proto_register_t38(void)
667 {
668         static hf_register_info hf[] =
669         {
670 #include "packet-t38-hfarr.c"
671                 {   &hf_t38_setup,
672                     { "Stream setup", "t38.setup", FT_STRING, BASE_NONE,
673                     NULL, 0x0, "Stream setup, method and frame number", HFILL }},
674                 {   &hf_t38_setup_frame,
675             { "Stream frame", "t38.setup-frame", FT_FRAMENUM, BASE_NONE,
676             NULL, 0x0, "Frame that set up this stream", HFILL }},
677         {   &hf_t38_setup_method,
678             { "Stream Method", "t38.setup-method", FT_STRING, BASE_NONE,
679             NULL, 0x0, "Method used to set up this stream", HFILL }},
680                 {&hf_t38_fragments,
681                         {"Message fragments", "t38.fragments",
682                         FT_NONE, BASE_NONE, NULL, 0x00, NULL, HFILL } },
683                 {&hf_t38_fragment,
684                         {"Message fragment", "t38.fragment",
685                         FT_FRAMENUM, BASE_NONE, NULL, 0x00, NULL, HFILL } },
686                 {&hf_t38_fragment_overlap,
687                         {"Message fragment overlap", "t38.fragment.overlap",
688                         FT_BOOLEAN, BASE_NONE, NULL, 0x0, NULL, HFILL } },
689                 {&hf_t38_fragment_overlap_conflicts,
690                         {"Message fragment overlapping with conflicting data",
691                         "t38.fragment.overlap.conflicts",
692                         FT_BOOLEAN, BASE_NONE, NULL, 0x0, NULL, HFILL } },
693                 {&hf_t38_fragment_multiple_tails,
694                         {"Message has multiple tail fragments",
695                         "t38.fragment.multiple_tails", 
696                         FT_BOOLEAN, BASE_NONE, NULL, 0x0, NULL, HFILL } },
697                 {&hf_t38_fragment_too_long_fragment,
698                         {"Message fragment too long", "t38.fragment.too_long_fragment",
699                         FT_BOOLEAN, BASE_NONE, NULL, 0x0, NULL, HFILL } },
700                 {&hf_t38_fragment_error,
701                         {"Message defragmentation error", "t38.fragment.error",
702                         FT_FRAMENUM, BASE_NONE, NULL, 0x00, NULL, HFILL } },
703                 {&hf_t38_reassembled_in,
704                         {"Reassembled in", "t38.reassembled.in",
705                         FT_FRAMENUM, BASE_NONE, NULL, 0x00, NULL, HFILL } },
706                 {&hf_t38_reassembled_length,
707                         {"Reassembled T38 length", "t38.reassembled.length",
708                         FT_UINT32, BASE_DEC, NULL, 0x00, NULL, HFILL } },
709         };
710
711         static gint *ett[] =
712         {
713                 &ett_t38,
714 #include "packet-t38-ettarr.c"
715                 &ett_t38_setup,
716                 &ett_data_fragment,
717                 &ett_data_fragments
718         };
719
720         module_t *t38_module;
721
722         proto_t38 = proto_register_protocol("T.38", "T.38", "t38");
723         proto_register_field_array(proto_t38, hf, array_length(hf));
724         proto_register_subtree_array(ett, array_length(ett));
725         register_dissector("t38", dissect_t38, proto_t38);
726
727         /* Init reassemble tables for HDLC */
728         register_init_routine(t38_defragment_init);
729
730         t38_tap = register_tap("t38");
731
732         t38_module = prefs_register_protocol(proto_t38, proto_reg_handoff_t38);
733         prefs_register_bool_preference(t38_module, "use_pre_corrigendum_asn1_specification",
734             "Use the Pre-Corrigendum ASN.1 specification",
735             "Whether the T.38 dissector should decode using the Pre-Corrigendum T.38 "
736                 "ASN.1 specification (1998).",
737             &use_pre_corrigendum_asn1_specification);
738         prefs_register_bool_preference(t38_module, "dissect_possible_rtpv2_packets_as_rtp",
739             "Dissect possible RTP version 2 packets with RTP dissector",
740             "Whether a UDP packet that looks like RTP version 2 packet will "
741                 "be dissected as RTP packet or T.38 packet. If enabled there is a risk that T.38 UDPTL "
742                 "packets with sequence number higher than 32767 may be dissected as RTP.",
743             &dissect_possible_rtpv2_packets_as_rtp);
744         prefs_register_uint_preference(t38_module, "tcp.port",
745                 "T.38 TCP Port",
746                 "Set the TCP port for T.38 messages",
747                 10, &global_t38_tcp_port);
748         prefs_register_uint_preference(t38_module, "udp.port",
749                 "T.38 UDP Port",
750                 "Set the UDP port for T.38 messages",
751                 10, &global_t38_udp_port);      
752         prefs_register_bool_preference(t38_module, "reassembly",
753                 "Reassemble T.38 PDUs over TPKT over TCP",
754                 "Whether the dissector should reassemble T.38 PDUs spanning multiple TCP segments "
755                 "when TPKT is used over TCP. "
756                 "To use this option, you must also enable \"Allow subdissectors to reassemble "
757                 "TCP streams\" in the TCP protocol settings.",
758                 &t38_tpkt_reassembly);
759         prefs_register_enum_preference(t38_module, "tpkt_usage",
760                 "TPKT used over TCP",
761                 "Whether T.38 is used with TPKT for TCP",
762                 (gint *)&t38_tpkt_usage,t38_tpkt_options,FALSE);
763
764         prefs_register_bool_preference(t38_module, "show_setup_info",
765                 "Show stream setup information",
766                 "Where available, show which protocol and frame caused "
767                 "this T.38 stream to be created",
768                 &global_t38_show_setup_info);
769
770 }
771
772 void
773 proto_reg_handoff_t38(void)
774 {
775         static gboolean t38_prefs_initialized = FALSE;
776         static guint tcp_port;
777         static guint udp_port;
778
779         if (!t38_prefs_initialized) {
780                 t38_udp_handle=create_dissector_handle(dissect_t38_udp, proto_t38);
781                 t38_tcp_handle=create_dissector_handle(dissect_t38_tcp, proto_t38);
782                 t38_tcp_pdu_handle=create_dissector_handle(dissect_t38_tcp_pdu, proto_t38);
783                 rtp_handle = find_dissector("rtp");
784                 t30_hdlc_handle = find_dissector("t30.hdlc");
785                 data_handle = find_dissector("data");
786                 t38_prefs_initialized = TRUE;
787         }
788         else {
789                 dissector_delete("tcp.port", tcp_port, t38_tcp_handle);
790                 dissector_delete("udp.port", udp_port, t38_udp_handle);
791         }
792         tcp_port = global_t38_tcp_port;
793         udp_port = global_t38_udp_port;
794
795         dissector_add("tcp.port", tcp_port, t38_tcp_handle);
796         dissector_add("udp.port", udp_port, t38_udp_handle);
797
798 }
799