be20461ab22344458c066833224d423b3381b34c
[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 <stdio.h>
59 #include <string.h>
60
61 #include "packet-t38.h"
62 #include <epan/prefs.h>
63 #include <epan/ipproto.h>
64 #include <epan/asn1.h>
65 #include "packet-per.h"
66 #include "packet-tpkt.h"
67 #include <epan/emem.h>
68 #include <epan/strutil.h>
69
70 #define PORT_T38 6004  
71 static guint global_t38_tcp_port = PORT_T38;
72 static guint global_t38_udp_port = PORT_T38;
73
74 static int t38_tap = -1;
75
76 /* dissect using the Pre Corrigendum T.38 ASN.1 specification (1998) */
77 static gboolean use_pre_corrigendum_asn1_specification = TRUE;
78
79 /* dissect packets that looks like RTP version 2 packets as RTP     */
80 /* instead of as T.38. This may result in that some T.38 UPTL       */
81 /* packets with sequence number values higher than 32767 may be     */
82 /* shown as RTP packets.                                            */ 
83 static gboolean dissect_possible_rtpv2_packets_as_rtp = FALSE;
84
85
86 /* Reassembly of T.38 PDUs over TPKT over TCP */
87 static gboolean t38_tpkt_reassembly = TRUE;
88
89 /* Preference setting whether TPKT header is used when sending T.38 over TCP.
90  * The default setting is Maybe where the dissector will look on the first
91  * bytes to try to determine whether TPKT header is used or not. This may not
92  * work so well in some cases. You may want to change the setting to Always or
93  * Newer.
94  */
95 #define T38_TPKT_NEVER 0   /* Assume that there is never a TPKT header    */
96 #define T38_TPKT_ALWAYS 1  /* Assume that there is always a TPKT header   */
97 #define T38_TPKT_MAYBE 2   /* Assume TPKT if first octets are 03-00-xx-xx */
98 static gint t38_tpkt_usage = T38_TPKT_MAYBE;
99
100 static const enum_val_t t38_tpkt_options[] = {
101   {"never", "Never", T38_TPKT_NEVER},
102   {"always", "Always", T38_TPKT_ALWAYS},
103   {"maybe", "Maybe", T38_TPKT_MAYBE},
104   {NULL, NULL, -1}
105 };
106
107
108
109 /* T38 */
110 static dissector_handle_t t38_udp_handle;
111 static dissector_handle_t t38_tcp_handle;
112 static dissector_handle_t t38_tcp_pdu_handle;
113 static dissector_handle_t rtp_handle;
114 static dissector_handle_t t30_hdlc_handle;
115 static dissector_handle_t data_handle;
116
117 static gint32 Type_of_msg_value;
118 static guint32 Data_Field_field_type_value;
119 static guint32 Data_value;
120 static guint32 T30ind_value;
121 static guint32 Data_Field_item_num;
122
123 static int proto_t38 = -1;
124 #include "packet-t38-hf.c"
125
126 /* T38 setup fields */
127 static int hf_t38_setup        = -1;
128 static int hf_t38_setup_frame  = -1;
129 static int hf_t38_setup_method = -1;
130
131 /* T38 Data reassemble fields */
132 static int hf_t38_fragments = -1;
133 static int hf_t38_fragment = -1;
134 static int hf_t38_fragment_overlap = -1;
135 static int hf_t38_fragment_overlap_conflicts = -1;
136 static int hf_t38_fragment_multiple_tails = -1;
137 static int hf_t38_fragment_too_long_fragment = -1;
138 static int hf_t38_fragment_error = -1;
139 static int hf_t38_reassembled_in = -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 static GHashTable *data_reassembled_table = NULL;
154
155 static const fragment_items data_frag_items = {
156         /* Fragment subtrees */
157         &ett_data_fragment,
158         &ett_data_fragments,
159         /* Fragment fields */
160         &hf_t38_fragments,
161         &hf_t38_fragment,
162         &hf_t38_fragment_overlap,
163         &hf_t38_fragment_overlap_conflicts,
164         &hf_t38_fragment_multiple_tails,
165         &hf_t38_fragment_too_long_fragment,
166         &hf_t38_fragment_error,
167         /* Reassembled in field */
168         &hf_t38_reassembled_in,
169         /* Tag */
170         "Data fragments"
171 };
172
173 typedef struct _fragment_key {
174         address src;
175         address dst;
176         guint32 id;
177 } fragment_key;
178
179 static conversation_t *p_conv= NULL;
180 static t38_conv *p_t38_conv = NULL;
181 static t38_conv *p_t38_packet_conv = NULL;
182 static t38_conv_info *p_t38_conv_info = NULL;
183 static t38_conv_info *p_t38_packet_conv_info = NULL;
184
185 /* RTP Version is the first 2 bits of the first octet in the UDP payload*/
186 #define RTP_VERSION(octet)      ((octet) >> 6)
187
188 void proto_reg_handoff_t38(void);
189
190 static void show_setup_info(tvbuff_t *tvb, proto_tree *tree, t38_conv *p_t38_conv);
191 /* Preferences bool to control whether or not setup info should be shown */
192 static gboolean global_t38_show_setup_info = TRUE;
193
194 /* Can tap up to 4 T38 packets within same packet */
195 /* We only tap the primary part, not the redundancy */
196 #define MAX_T38_MESSAGES_IN_PACKET 4
197 static t38_packet_info t38_info_arr[MAX_T38_MESSAGES_IN_PACKET];
198 static int t38_info_current=0;
199 static t38_packet_info *t38_info=NULL;
200
201 static void t38_defragment_init(void)
202 {
203         /* Init reassemble tables */
204         fragment_table_init(&data_fragment_table);
205         reassembled_table_init(&data_reassembled_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_conv;
217         t38_conv* p_conv_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_conv = 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_conv || p_conv->setup_frame != setup_frame_number) {
242                 p_conv = 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_conv, t38_udp_handle);
249
250         /*
251          * Check if the conversation has data associated with it.
252          */
253         p_conv_data = conversation_get_proto_data(p_conv, proto_t38);
254
255         /*
256          * If not, add a new data item.
257          */
258         if ( ! p_conv_data ) {
259                 /* Create conversation data */
260                 p_conv_data = se_alloc(sizeof(t38_conv));
261
262                 conversation_add_proto_data(p_conv, proto_t38, p_conv_data);
263         }
264
265         /*
266          * Update the conversation data.
267          */
268         g_strlcpy(p_conv_data->setup_method, setup_method, MAX_T38_SETUP_METHOD_SIZE);
269         p_conv_data->setup_frame_number = setup_frame_number;
270                 p_conv_data->src_t38_info.reass_ID = 0;
271                 p_conv_data->src_t38_info.reass_start_seqnum = -1;
272                 p_conv_data->src_t38_info.reass_data_type = 0;
273                 p_conv_data->src_t38_info.last_seqnum = -1;
274                 p_conv_data->src_t38_info.packet_lost = 0;
275                 p_conv_data->src_t38_info.burst_lost = 0;
276                 p_conv_data->src_t38_info.time_first_t4_data = 0;
277
278
279                 p_conv_data->dst_t38_info.reass_ID = 0;
280                 p_conv_data->dst_t38_info.reass_start_seqnum = -1;
281                 p_conv_data->dst_t38_info.reass_data_type = 0;
282                 p_conv_data->dst_t38_info.last_seqnum = -1;
283                 p_conv_data->dst_t38_info.packet_lost = 0;
284                 p_conv_data->dst_t38_info.burst_lost = 0;
285                 p_conv_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_src, &pinfo->net_dst,
432                                    pinfo->ptype,
433                                    pinfo->srcport, pinfo->destport, 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         if (check_col(pinfo->cinfo, COL_PROTOCOL)){
516                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "T.38");
517         }
518         if (check_col(pinfo->cinfo, COL_INFO)){
519                 col_clear(pinfo->cinfo, COL_INFO);
520         }
521
522         primary_part = TRUE;
523
524         /* This indicate the item number in the primary part of the T38 message, it is used for the reassemble of T30 packets */
525         Data_Field_item_num = 0;
526
527         it=proto_tree_add_protocol_format(tree, proto_t38, tvb, 0, -1, "ITU-T Recommendation T.38");
528         tr=proto_item_add_subtree(it, ett_t38);
529
530         /* init tap and conv info */
531         init_t38_info_conv(pinfo);
532
533         /* Show Conversation setup info if exists*/
534         if (global_t38_show_setup_info) {
535                 show_setup_info(tvb, tr, p_t38_packet_conv);
536         }
537
538         if (check_col(pinfo->cinfo, COL_INFO)){
539                 col_append_str(pinfo->cinfo, COL_INFO, "UDP: UDPTLPacket ");
540         }
541
542         offset = dissect_UDPTLPacket_PDU(tvb, pinfo, tr);
543
544         if (tvb_length_remaining(tvb,offset)>0){
545                 if (tr){
546                         proto_tree_add_text(tr, tvb, offset, tvb_reported_length_remaining(tvb, offset),
547                                 "[MALFORMED PACKET or wrong preference settings]");
548                 }
549                 if (check_col(pinfo->cinfo, COL_INFO)){
550                         col_append_str(pinfo->cinfo, COL_INFO, " [Malformed?]");
551                 }
552         }
553 }
554
555 static void
556 dissect_t38_tcp_pdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
557 {
558         proto_item *it;
559         proto_tree *tr;
560         guint32 offset=0;
561     tvbuff_t *next_tvb;
562         guint16 ifp_packet_number=1;
563
564         if (check_col(pinfo->cinfo, COL_PROTOCOL)){
565                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "T.38");
566         }
567         if (check_col(pinfo->cinfo, COL_INFO)){
568                 col_clear(pinfo->cinfo, COL_INFO);
569         }
570
571         primary_part = TRUE;
572
573         /* This indicate the item number in the primary part of the T38 message, it is used for the reassemble of T30 packets */
574         Data_Field_item_num = 0;
575
576         it=proto_tree_add_protocol_format(tree, proto_t38, tvb, 0, -1, "ITU-T Recommendation T.38");
577         tr=proto_item_add_subtree(it, ett_t38);
578
579         /* init tap and conv info */
580         init_t38_info_conv(pinfo);
581
582         /* Show Conversation setup info if exists*/
583         if (global_t38_show_setup_info) {
584                 show_setup_info(tvb, tr, p_t38_packet_conv);
585         }
586
587         if (check_col(pinfo->cinfo, COL_INFO)){
588                 col_append_str(pinfo->cinfo, COL_INFO, "TCP: IFPPacket");
589         }
590
591         while(tvb_length_remaining(tvb,offset)>0)
592         {
593                 next_tvb = tvb_new_subset(tvb, offset, -1, -1);
594                 offset += dissect_IFPPacket_PDU(next_tvb, pinfo, tr);
595                 ifp_packet_number++;
596
597                 if(tvb_length_remaining(tvb,offset)>0){
598                         if(t38_tpkt_usage == T38_TPKT_ALWAYS){
599                                 if(tr){
600                                         proto_tree_add_text(tr, tvb, offset, tvb_reported_length_remaining(tvb, offset),
601                                                 "[MALFORMED PACKET or wrong preference settings]");
602                                 }
603                                 if (check_col(pinfo->cinfo, COL_INFO)){
604                                         col_append_str(pinfo->cinfo, COL_INFO, " [Malformed?]");
605                                 }
606                                 break;
607                         } 
608                         else {
609                                 if (check_col(pinfo->cinfo, COL_INFO)){
610                                         col_append_fstr(pinfo->cinfo, COL_INFO, " IFPPacket#%u",ifp_packet_number);
611                                 }
612                         }
613                 }
614         }
615
616 }
617
618 static void
619 dissect_t38_tcp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
620 {
621         primary_part = TRUE;
622
623         if(t38_tpkt_usage == T38_TPKT_ALWAYS){
624                 dissect_tpkt_encap(tvb,pinfo,tree,t38_tpkt_reassembly,t38_tcp_pdu_handle);
625         } 
626         else if((t38_tpkt_usage == T38_TPKT_NEVER) || (is_tpkt(tvb,1) == -1)){
627                 dissect_t38_tcp_pdu(tvb, pinfo, tree);
628         } 
629         else {
630                 dissect_tpkt_encap(tvb,pinfo,tree,t38_tpkt_reassembly,t38_tcp_pdu_handle);
631         }
632 }
633
634 static void
635 dissect_t38(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
636 {
637         if(pinfo->ipproto == IP_PROTO_TCP)
638         {
639                 dissect_t38_tcp(tvb, pinfo, tree);
640         }
641         else if(pinfo->ipproto == IP_PROTO_UDP)
642         {   
643                 dissect_t38_udp(tvb, pinfo, tree);
644         }
645 }
646
647 /* Look for conversation info and display any setup info found */
648 void 
649 show_setup_info(tvbuff_t *tvb, proto_tree *tree, t38_conv *p_t38_conv)
650 {
651         proto_tree *t38_setup_tree;
652         proto_item *ti;
653
654         if (!p_t38_conv || p_t38_conv->setup_frame_number == 0) {
655                 /* there is no Setup info */
656                 return;
657         }
658
659         ti =  proto_tree_add_string_format(tree, hf_t38_setup, tvb, 0, 0,
660                       "",
661                       "Stream setup by %s (frame %u)",
662                       p_t38_conv->setup_method,
663                       p_t38_conv->setup_frame_number);
664     PROTO_ITEM_SET_GENERATED(ti);
665     t38_setup_tree = proto_item_add_subtree(ti, ett_t38_setup);
666     if (t38_setup_tree)
667     {
668                 /* Add details into subtree */
669                 proto_item* item = proto_tree_add_uint(t38_setup_tree, hf_t38_setup_frame,
670                                                                tvb, 0, 0, p_t38_conv->setup_frame_number);
671                 PROTO_ITEM_SET_GENERATED(item);
672                 item = proto_tree_add_string(t38_setup_tree, hf_t38_setup_method,
673                                                      tvb, 0, 0, p_t38_conv->setup_method);
674                 PROTO_ITEM_SET_GENERATED(item);
675     }
676 }
677
678
679
680 /* Wireshark Protocol Registration */
681 void
682 proto_register_t38(void)
683 {
684         static hf_register_info hf[] =
685         {
686 #include "packet-t38-hfarr.c"
687                 {   &hf_t38_setup,
688                     { "Stream setup", "t38.setup", FT_STRING, BASE_NONE,
689                     NULL, 0x0, "Stream setup, method and frame number", HFILL }},
690                 {   &hf_t38_setup_frame,
691             { "Stream frame", "t38.setup-frame", FT_FRAMENUM, BASE_NONE,
692             NULL, 0x0, "Frame that set up this stream", HFILL }},
693         {   &hf_t38_setup_method,
694             { "Stream Method", "t38.setup-method", FT_STRING, BASE_NONE,
695             NULL, 0x0, "Method used to set up this stream", HFILL }},
696                 {&hf_t38_fragments,
697                         {"Message fragments", "t38.fragments",
698                         FT_NONE, BASE_NONE, NULL, 0x00, NULL, HFILL } },
699                 {&hf_t38_fragment,
700                         {"Message fragment", "t38.fragment",
701                         FT_FRAMENUM, BASE_NONE, NULL, 0x00, NULL, HFILL } },
702                 {&hf_t38_fragment_overlap,
703                         {"Message fragment overlap", "t38.fragment.overlap",
704                         FT_BOOLEAN, BASE_NONE, NULL, 0x0, NULL, HFILL } },
705                 {&hf_t38_fragment_overlap_conflicts,
706                         {"Message fragment overlapping with conflicting data",
707                         "t38.fragment.overlap.conflicts",
708                         FT_BOOLEAN, BASE_NONE, NULL, 0x0, NULL, HFILL } },
709                 {&hf_t38_fragment_multiple_tails,
710                         {"Message has multiple tail fragments",
711                         "t38.fragment.multiple_tails", 
712                         FT_BOOLEAN, BASE_NONE, NULL, 0x0, NULL, HFILL } },
713                 {&hf_t38_fragment_too_long_fragment,
714                         {"Message fragment too long", "t38.fragment.too_long_fragment",
715                         FT_BOOLEAN, BASE_NONE, NULL, 0x0, NULL, HFILL } },
716                 {&hf_t38_fragment_error,
717                         {"Message defragmentation error", "t38.fragment.error",
718                         FT_FRAMENUM, BASE_NONE, NULL, 0x00, NULL, HFILL } },
719                 {&hf_t38_reassembled_in,
720                         {"Reassembled in", "t38.reassembled.in",
721                         FT_FRAMENUM, BASE_NONE, NULL, 0x00, NULL, HFILL } },
722         };
723
724         static gint *ett[] =
725         {
726                 &ett_t38,
727 #include "packet-t38-ettarr.c"
728                 &ett_t38_setup,
729                 &ett_data_fragment,
730                 &ett_data_fragments
731         };
732
733         module_t *t38_module;
734
735         proto_t38 = proto_register_protocol("T.38", "T.38", "t38");
736         proto_register_field_array(proto_t38, hf, array_length(hf));
737         proto_register_subtree_array(ett, array_length(ett));
738         register_dissector("t38", dissect_t38, proto_t38);
739
740         /* Init reassemble tables for HDLC */
741         register_init_routine(t38_defragment_init);
742
743         t38_tap = register_tap("t38");
744
745         t38_module = prefs_register_protocol(proto_t38, proto_reg_handoff_t38);
746         prefs_register_bool_preference(t38_module, "use_pre_corrigendum_asn1_specification",
747             "Use the Pre-Corrigendum ASN.1 specification",
748             "Whether the T.38 dissector should decode using the Pre-Corrigendum T.38 "
749                 "ASN.1 specification (1998).",
750             &use_pre_corrigendum_asn1_specification);
751         prefs_register_bool_preference(t38_module, "dissect_possible_rtpv2_packets_as_rtp",
752             "Dissect possible RTP version 2 packets with RTP dissector",
753             "Whether a UDP packet that looks like RTP version 2 packet will "
754                 "be dissected as RTP packet or T.38 packet. If enabled there is a risk that T.38 UDPTL "
755                 "packets with sequence number higher than 32767 may be dissected as RTP.",
756             &dissect_possible_rtpv2_packets_as_rtp);
757         prefs_register_uint_preference(t38_module, "tcp.port",
758                 "T.38 TCP Port",
759                 "Set the TCP port for T.38 messages",
760                 10, &global_t38_tcp_port);
761         prefs_register_uint_preference(t38_module, "udp.port",
762                 "T.38 UDP Port",
763                 "Set the UDP port for T.38 messages",
764                 10, &global_t38_udp_port);      
765         prefs_register_bool_preference(t38_module, "reassembly",
766                 "Reassemble T.38 PDUs over TPKT over TCP",
767                 "Whether the dissector should reassemble T.38 PDUs spanning multiple TCP segments "
768                 "when TPKT is used over TCP. "
769                 "To use this option, you must also enable \"Allow subdissectors to reassemble "
770                 "TCP streams\" in the TCP protocol settings.",
771                 &t38_tpkt_reassembly);
772         prefs_register_enum_preference(t38_module, "tpkt_usage",
773                 "TPKT used over TCP",
774                 "Whether T.38 is used with TPKT for TCP",
775                 (gint *)&t38_tpkt_usage,t38_tpkt_options,FALSE);
776
777         prefs_register_bool_preference(t38_module, "show_setup_info",
778                 "Show stream setup information",
779                 "Where available, show which protocol and frame caused "
780                 "this T.38 stream to be created",
781                 &global_t38_show_setup_info);
782
783 }
784
785 void
786 proto_reg_handoff_t38(void)
787 {
788         static gboolean t38_prefs_initialized = FALSE;
789         static guint tcp_port;
790         static guint udp_port;
791
792         if (!t38_prefs_initialized) {
793                 t38_udp_handle=create_dissector_handle(dissect_t38_udp, proto_t38);
794                 t38_tcp_handle=create_dissector_handle(dissect_t38_tcp, proto_t38);
795                 t38_tcp_pdu_handle=create_dissector_handle(dissect_t38_tcp_pdu, proto_t38);
796                 rtp_handle = find_dissector("rtp");
797                 t30_hdlc_handle = find_dissector("t30.hdlc");
798                 data_handle = find_dissector("data");
799                 t38_prefs_initialized = TRUE;
800         }
801         else {
802                 dissector_delete("tcp.port", tcp_port, t38_tcp_handle);
803                 dissector_delete("udp.port", udp_port, t38_udp_handle);
804         }
805         tcp_port = global_t38_tcp_port;
806         udp_port = global_t38_udp_port;
807
808         dissector_add("tcp.port", tcp_port, t38_tcp_handle);
809         dissector_add("udp.port", udp_port, t38_udp_handle);
810
811 }
812