From Lars Ruoff:
[obnox/wireshark/wip.git] / epan / dissectors / packet-rtp.c
1 /* packet-rtp.c
2  *
3  * Routines for RTP dissection
4  * RTP = Real time Transport Protocol
5  *
6  * Copyright 2000, Philips Electronics N.V.
7  * Written by Andreas Sikkema <h323@ramdyne.nl>
8  *
9  * $Id$
10  *
11  * Wireshark - Network traffic analyzer
12  * By Gerald Combs <gerald@wireshark.org>
13  * Copyright 1998 Gerald Combs
14  *
15  * This program is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU General Public License
17  * as published by the Free Software Foundation; either version 2
18  * of the License, or (at your option) any later version.
19  *
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, write to the Free Software
27  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
28  */
29
30 /*
31  * This dissector tries to dissect the RTP protocol according to Annex A
32  * of ITU-T Recommendation H.225.0 (02/98) or RFC 1889
33  *
34  * RTP traffic is handled by an even UDP portnumber. This can be any
35  * port number, but there is a registered port available, port 5004
36  * See Annex B of ITU-T Recommendation H.225.0, section B.7
37  *
38  * This doesn't dissect older versions of RTP, such as:
39  *
40  *    the vat protocol ("version 0") - see
41  *
42  *      ftp://ftp.ee.lbl.gov/conferencing/vat/alpha-test/vatsrc-4.0b2.tar.gz
43  *
44  *    and look in "session-vat.cc" if you want to write a dissector
45  *    (have fun - there aren't any nice header files showing the packet
46  *    format);
47  *
48  *    version 1, as documented in
49  *
50  *      ftp://gaia.cs.umass.edu/pub/hgschulz/rtp/draft-ietf-avt-rtp-04.txt
51  *
52  * It also dissects PacketCable CCC-encapsulated RTP data, as described in
53  * chapter 5 of the PacketCable Electronic Surveillance Specification:
54  *
55  *   http://www.packetcable.com/downloads/specs/PKT-SP-ESP1.5-I01-050128.pdf
56  */
57
58
59 #ifdef HAVE_CONFIG_H
60 # include "config.h"
61 #endif
62
63 #include <glib.h>
64 #include <epan/packet.h>
65
66 #include <stdio.h>
67 #include <string.h>
68
69 #include "packet-rtp.h"
70 #include <epan/rtp_pt.h>
71 #include "packet-ntp.h"
72 #include <epan/conversation.h>
73 #include <epan/reassemble.h>
74 #include <epan/tap.h>
75
76 #include <epan/prefs.h>
77 #include <epan/emem.h>
78
79 #include "log.h"
80
81 /* uncomment this to enable debugging of fragment reassembly */
82 /* #define DEBUG_FRAGMENTS   1 */
83
84 typedef struct _rfc2198_hdr {
85   guint8 pt;
86   int offset;
87   int len;
88   struct _rfc2198_hdr *next;
89 } rfc2198_hdr;
90
91 /* we have one of these for each pdu which spans more than one segment
92  */
93 typedef struct _rtp_multisegment_pdu {
94         /* the seqno of the segment where the pdu starts */
95         guint32 startseq;
96
97         /* the seqno of the segment where the pdu ends */
98         guint32 endseq;
99 } rtp_multisegment_pdu;
100
101 typedef struct  _rtp_private_conv_info {
102         /* This tree is indexed by sequence number and keeps track of all
103          * all pdus spanning multiple segments for this flow.
104          */
105         emem_tree_t *multisegment_pdus;
106 } rtp_private_conv_info;
107
108 static GHashTable *fragment_table = NULL;
109 static GHashTable * fid_table = NULL;
110
111 static int hf_rtp_fragments = -1;
112 static int hf_rtp_fragment = -1;
113 static int hf_rtp_fragment_overlap = -1;
114 static int hf_rtp_fragment_overlap_conflict = -1;
115 static int hf_rtp_fragment_multiple_tails = -1;
116 static int hf_rtp_fragment_too_long_fragment = -1;
117 static int hf_rtp_fragment_error = -1;
118 static int hf_rtp_reassembled_in = -1;
119
120 static gint ett_rtp_fragment = -1;
121 static gint ett_rtp_fragments = -1;
122
123 static const fragment_items rtp_fragment_items = {
124   &ett_rtp_fragment,
125   &ett_rtp_fragments,
126   &hf_rtp_fragments,
127   &hf_rtp_fragment,
128   &hf_rtp_fragment_overlap,
129   &hf_rtp_fragment_overlap_conflict,
130   &hf_rtp_fragment_multiple_tails,
131   &hf_rtp_fragment_too_long_fragment,
132   &hf_rtp_fragment_error,
133   &hf_rtp_reassembled_in,
134   "RTP fragments"
135 };
136
137 static dissector_handle_t rtp_handle;
138 static dissector_handle_t rtp_rfc2198_handle;
139 static dissector_handle_t stun_handle;
140 static dissector_handle_t t38_handle;
141
142 static dissector_handle_t pkt_ccc_handle;
143
144 static int rtp_tap = -1;
145
146 static dissector_table_t rtp_pt_dissector_table;
147 static dissector_table_t rtp_dyn_pt_dissector_table;
148
149 /* RTP header fields             */
150 static int proto_rtp           = -1;
151 static int hf_rtp_version      = -1;
152 static int hf_rtp_padding      = -1;
153 static int hf_rtp_extension    = -1;
154 static int hf_rtp_csrc_count   = -1;
155 static int hf_rtp_marker       = -1;
156 static int hf_rtp_payload_type = -1;
157 static int hf_rtp_seq_nr       = -1;
158 static int hf_rtp_ext_seq_nr   = -1;
159 static int hf_rtp_timestamp    = -1;
160 static int hf_rtp_ssrc         = -1;
161 static int hf_rtp_csrc_item    = -1;
162 static int hf_rtp_data         = -1;
163 static int hf_rtp_padding_data = -1;
164 static int hf_rtp_padding_count= -1;
165 static int hf_rtp_rfc2198_follow= -1;
166 static int hf_rtp_rfc2198_tm_off= -1;
167 static int hf_rtp_rfc2198_bl_len= -1;
168
169 /* RTP header extension fields   */
170 static int hf_rtp_prof_define  = -1;
171 static int hf_rtp_length       = -1;
172 static int hf_rtp_hdr_ext      = -1;
173
174 /* RTP setup fields */
175 static int hf_rtp_setup        = -1;
176 static int hf_rtp_setup_frame  = -1;
177 static int hf_rtp_setup_method = -1;
178
179 /* RTP fields defining a sub tree */
180 static gint ett_rtp       = -1;
181 static gint ett_csrc_list = -1;
182 static gint ett_hdr_ext   = -1;
183 static gint ett_rtp_setup = -1;
184 static gint ett_rtp_rfc2198 = -1;
185 static gint ett_rtp_rfc2198_hdr = -1;
186
187
188 /* PacketCable CCC header fields */
189 static int proto_pkt_ccc       = -1;
190 static int hf_pkt_ccc_id       = -1;
191 static int hf_pkt_ccc_ts       = -1;
192
193 /* PacketCable CCC field defining a sub tree */
194 static gint ett_pkt_ccc = -1;
195
196 /* PacketCable CCC port preference */
197 static gboolean global_pkt_ccc_udp_port = 0;
198
199
200 #define RTP0_INVALID 0
201 #define RTP0_STUN    1
202 #define RTP0_T38     2
203
204 static enum_val_t rtp_version0_types[] = {
205         { "invalid", "Invalid RTP packets", RTP0_INVALID },
206         { "stun", "STUN packets", RTP0_STUN },
207         { "t38", "T.38 packets", RTP0_T38 },
208         { NULL, NULL, 0 }
209 };
210 static guint global_rtp_version0_type = 0;
211
212 static dissector_handle_t data_handle;
213
214 /* Forward declaration we need below */
215 void proto_reg_handoff_rtp(void);
216
217 static gboolean dissect_rtp_heur( tvbuff_t *tvb, packet_info *pinfo,
218     proto_tree *tree );
219 static void dissect_rtp( tvbuff_t *tvb, packet_info *pinfo,
220     proto_tree *tree );
221 static void show_setup_info(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
222 static void get_conv_info(packet_info *pinfo, struct _rtp_info *rtp_info);
223
224 /* Preferences bool to control whether or not setup info should be shown */
225 static gboolean global_rtp_show_setup_info = TRUE;
226
227 /* Try heuristic RTP decode */
228 static gboolean global_rtp_heur = FALSE;
229
230 /* desegment RTP streams */
231 static gboolean desegment_rtp = TRUE;
232
233 /* RFC2198 Redundant Audio Data */
234 static guint rtp_rfc2198_pt = 99;
235 static guint rtp_saved_rfc2198_pt = 0;
236
237 /*
238  * Fields in the first octet of the RTP header.
239  */
240
241 /* Version is the first 2 bits of the first octet*/
242 #define RTP_VERSION(octet)      ((octet) >> 6)
243
244 /* Padding is the third bit; No need to shift, because true is any value
245    other than 0! */
246 #define RTP_PADDING(octet)      ((octet) & 0x20)
247
248 /* Extension bit is the fourth bit */
249 #define RTP_EXTENSION(octet)    ((octet) & 0x10)
250
251 /* CSRC count is the last four bits */
252 #define RTP_CSRC_COUNT(octet)   ((octet) & 0xF)
253
254 static const value_string rtp_version_vals[] =
255 {
256         { 0, "Old VAT Version" },
257         { 1, "First Draft Version" },
258         { 2, "RFC 1889 Version" },
259         { 0, NULL },
260 };
261
262 /*
263  * Fields in the second octet of the RTP header.
264  */
265
266 /* Marker is the first bit of the second octet */
267 #define RTP_MARKER(octet)       ((octet) & 0x80)
268
269 /* Payload type is the last 7 bits */
270 #define RTP_PAYLOAD_TYPE(octet) ((octet) & 0x7F)
271
272 const value_string rtp_payload_type_vals[] =
273 {
274         { PT_PCMU,      "ITU-T G.711 PCMU" },
275         { PT_1016,      "USA Federal Standard FS-1016" },
276         { PT_G721,      "ITU-T G.721" },
277         { PT_GSM,       "GSM 06.10" },
278         { PT_G723,      "ITU-T G.723" },
279         { PT_DVI4_8000, "DVI4 8000 samples/s" },
280         { PT_DVI4_16000, "DVI4 16000 samples/s" },
281         { PT_LPC,       "Experimental linear predictive encoding from Xerox PARC" },
282         { PT_PCMA,      "ITU-T G.711 PCMA" },
283         { PT_G722,      "ITU-T G.722" },
284         { PT_L16_STEREO, "16-bit uncompressed audio, stereo" },
285         { PT_L16_MONO,  "16-bit uncompressed audio, monaural" },
286         { PT_QCELP,     "Qualcomm Code Excited Linear Predictive coding" },
287         { PT_CN,        "Comfort noise" },
288         { PT_MPA,       "MPEG-I/II Audio"},
289         { PT_G728,      "ITU-T G.728" },
290         { PT_DVI4_11025, "DVI4 11025 samples/s" },
291         { PT_DVI4_22050, "DVI4 22050 samples/s" },
292         { PT_G729,      "ITU-T G.729" },
293         { PT_CN_OLD,    "Comfort noise (old)" },
294         { PT_CELB,      "Sun CellB video encoding" },
295         { PT_JPEG,      "JPEG-compressed video" },
296         { PT_NV,        "'nv' program" },
297         { PT_H261,      "ITU-T H.261" },
298         { PT_MPV,       "MPEG-I/II Video"},
299         { PT_MP2T,      "MPEG-II transport streams"},
300         { PT_H263,      "ITU-T H.263" },
301         { 0,            NULL },
302 };
303
304 const value_string rtp_payload_type_short_vals[] =
305 {
306        { PT_PCMU,      "g711U" },
307        { PT_1016,      "fs-1016" },
308        { PT_G721,      "g721" },
309        { PT_GSM,       "GSM" },
310        { PT_G723,      "g723" },
311        { PT_DVI4_8000, "DVI4 8k" },
312        { PT_DVI4_16000, "DVI4 16k" },
313        { PT_LPC,       "Exp. from Xerox PARC" },
314        { PT_PCMA,      "g711A" },
315        { PT_G722,      "g722" },
316        { PT_L16_STEREO, "16-bit audio, stereo" },
317        { PT_L16_MONO,  "16-bit audio, monaural" },
318        { PT_QCELP,     "Qualcomm" },
319        { PT_CN,        "CN" },
320        { PT_MPA,       "MPEG-I/II Audio"},
321        { PT_G728,      "g728" },
322        { PT_DVI4_11025, "DVI4 11k" },
323        { PT_DVI4_22050, "DVI4 22k" },
324        { PT_G729,      "g729" },
325        { PT_CN_OLD,    "CN(old)" },
326        { PT_CELB,      "CellB" },
327        { PT_JPEG,      "JPEG" },
328        { PT_NV,        "NV" },
329        { PT_H261,      "h261" },
330        { PT_MPV,       "MPEG-I/II Video"},
331        { PT_MP2T,      "MPEG-II streams"},
332        { PT_H263,      "h263" },
333        { 0,            NULL },
334 };
335
336
337 /* initialisation routine */
338 static void rtp_fragment_init(void)
339 {
340         fragment_table_init(&fragment_table);
341         fid_table = g_hash_table_new(g_direct_hash, g_direct_equal);
342 }
343
344 void
345 rtp_free_hash_dyn_payload(GHashTable *rtp_dyn_payload)
346 {
347         if (rtp_dyn_payload == NULL) return;
348         g_hash_table_destroy(rtp_dyn_payload);
349         rtp_dyn_payload = NULL;
350 }
351
352 /* Set up an RTP conversation */
353 void rtp_add_address(packet_info *pinfo,
354                      address *addr, int port,
355                      int other_port,
356                      const gchar *setup_method, guint32 setup_frame_number, GHashTable *rtp_dyn_payload)
357 {
358         address null_addr;
359         conversation_t* p_conv;
360         struct _rtp_conversation_info *p_conv_data = NULL;
361
362         /*
363          * If this isn't the first time this packet has been processed,
364          * we've already done this work, so we don't need to do it
365          * again.
366          */
367         if (pinfo->fd->flags.visited)
368         {
369                 return;
370         }
371
372         SET_ADDRESS(&null_addr, AT_NONE, 0, NULL);
373
374         /*
375          * Check if the ip address and port combination is not
376          * already registered as a conversation.
377          */
378         p_conv = find_conversation( setup_frame_number, addr, &null_addr, PT_UDP, port, other_port,
379                                 NO_ADDR_B | (!other_port ? NO_PORT_B : 0));
380
381         /*
382          * If not, create a new conversation.
383          */
384         if ( !p_conv || p_conv->setup_frame != setup_frame_number) {
385                 p_conv = conversation_new( setup_frame_number, addr, &null_addr, PT_UDP,
386                                            (guint32)port, (guint32)other_port,
387                                                                    NO_ADDR2 | (!other_port ? NO_PORT2 : 0));
388         }
389
390         /* Set dissector */
391         conversation_set_dissector(p_conv, rtp_handle);
392
393         /*
394          * Check if the conversation has data associated with it.
395          */
396         p_conv_data = conversation_get_proto_data(p_conv, proto_rtp);
397
398         /*
399          * If not, add a new data item.
400          */
401         if ( ! p_conv_data ) {
402                 /* Create conversation data */
403                 p_conv_data = se_alloc(sizeof(struct _rtp_conversation_info));
404                 p_conv_data->rtp_dyn_payload = NULL;
405
406                 /* start this at 0x10000 so that we cope gracefully with the
407                  * first few packets being out of order (hence 0,65535,1,2,...)
408                  */
409                 p_conv_data->extended_seqno = 0x10000;
410                 p_conv_data->rtp_conv_info = se_alloc(sizeof(rtp_private_conv_info));
411                 p_conv_data->rtp_conv_info->multisegment_pdus = se_tree_create(EMEM_TREE_TYPE_RED_BLACK,"rtp_ms_pdus");
412                 conversation_add_proto_data(p_conv, proto_rtp, p_conv_data);
413         }
414
415         /*
416          * Update the conversation data.
417          */
418         /* Free the hash if already exists */
419         rtp_free_hash_dyn_payload(p_conv_data->rtp_dyn_payload);
420
421         strncpy(p_conv_data->method, setup_method, MAX_RTP_SETUP_METHOD_SIZE);
422         p_conv_data->method[MAX_RTP_SETUP_METHOD_SIZE] = '\0';
423         p_conv_data->frame_number = setup_frame_number;
424         p_conv_data->rtp_dyn_payload = rtp_dyn_payload;
425 }
426
427 static gboolean
428 dissect_rtp_heur( tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree )
429 {
430         guint8      octet1, octet2;
431         unsigned int version;
432         unsigned int payload_type;
433         unsigned int offset = 0;
434
435         /* This is a heuristic dissector, which means we get all the UDP
436          * traffic not sent to a known dissector and not claimed by
437          * a heuristic dissector called before us!
438          */
439
440         if (! global_rtp_heur)
441                 return FALSE;
442
443         /* Get the fields in the first octet */
444         octet1 = tvb_get_guint8( tvb, offset );
445         version = RTP_VERSION( octet1 );
446
447         if (version == 0) {
448                 switch (global_rtp_version0_type) {
449                 case RTP0_STUN:
450                         call_dissector(stun_handle, tvb, pinfo, tree);
451                         return TRUE;
452
453                 case RTP0_T38:
454                         call_dissector(t38_handle, tvb, pinfo, tree);
455                         return TRUE;
456
457                 case RTP0_INVALID:
458                 default:
459                         return FALSE; /* Unknown or unsupported version */
460                 }
461         } else if (version != 2) {
462                 /* Unknown or unsupported version */
463                 return FALSE;
464         }
465
466         /* Get the fields in the second octet */
467         octet2 = tvb_get_guint8( tvb, offset + 1 );
468         payload_type = RTP_PAYLOAD_TYPE( octet2 );
469         /*      if (payload_type == PT_PCMU ||
470          *                   payload_type == PT_PCMA)
471          *           payload_type == PT_G729)
472          *       */
473         if (payload_type <= PT_H263) {
474                 dissect_rtp( tvb, pinfo, tree );
475                 return TRUE;
476         }
477         else {
478                 return FALSE;
479         }
480 }
481
482 /*
483  * Process the payload of the RTP packet, hand it to the subdissector
484  */
485 static void
486 process_rtp_payload(tvbuff_t *newtvb, packet_info *pinfo, proto_tree *tree,
487     proto_tree *rtp_tree,
488     unsigned int payload_type)
489 {
490         struct _rtp_conversation_info *p_conv_data = NULL;
491         gboolean found_match = FALSE;
492
493         /* if the payload type is dynamic (96 to 127), we check if the conv is set and we look for the pt definition */
494         if ( (payload_type >=96) && (payload_type <=127) ) {
495                 p_conv_data = p_get_proto_data(pinfo->fd, proto_rtp);
496                 if (p_conv_data && p_conv_data->rtp_dyn_payload) {
497                         gchar *payload_type_str = NULL;
498                         payload_type_str = g_hash_table_lookup(p_conv_data->rtp_dyn_payload, &payload_type);
499                         if (payload_type_str){
500                                 found_match = dissector_try_string(rtp_dyn_pt_dissector_table,
501                                                                                                         payload_type_str, newtvb, pinfo, tree);
502                                 /* If payload type string set from conversation and
503                                  * no matching dissector found it's probably because no subdissector
504                                  * exists. Don't call the dissectors based on payload number
505                                  * as that'd probably be the wrong dissector in this case.
506                                  * Just add it as data.
507                                  */
508                                 if(found_match==FALSE)
509                                         proto_tree_add_item( rtp_tree, hf_rtp_data, newtvb, 0, -1, FALSE );
510                                 return;
511                         }
512
513                 }
514         }
515
516         /* if we don't found, it is static OR could be set static from the preferences */
517         if (!dissector_try_port(rtp_pt_dissector_table, payload_type, newtvb, pinfo, tree))
518                 proto_tree_add_item( rtp_tree, hf_rtp_data, newtvb, 0, -1, FALSE );
519
520 }
521
522 /* Rtp payload reassembly
523  *
524  * This handles the reassembly of PDUs for higher-level protocols.
525  *
526  * We're a bit limited on how we can cope with out-of-order packets, because
527  * we don't have any idea of where the datagram boundaries are. So if we see
528  * packets A, C, B (all of which comprise a single datagram), we cannot know
529  * that C should be added to the same datagram as A, until we come to B (which
530  * may or may not actually be present...).
531  *
532  * What we end up doing in this case is passing A+B to the subdissector as one
533  * datagram, and make out that a new one starts on C.
534  */
535 static void
536 dissect_rtp_data( tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
537     proto_tree *rtp_tree, int offset, unsigned int data_len,
538     unsigned int data_reported_len,
539         unsigned int payload_type )
540 {
541         tvbuff_t *newtvb;
542         struct _rtp_conversation_info *p_conv_data= NULL;
543         gboolean must_desegment = FALSE;
544         rtp_private_conv_info *finfo = NULL;
545         rtp_multisegment_pdu *msp = NULL;
546         guint32 seqno;
547
548         /* Retrieve RTPs idea of a converation */
549         p_conv_data = p_get_proto_data(pinfo->fd, proto_rtp);
550
551         if(p_conv_data != NULL) 
552                 finfo = p_conv_data->rtp_conv_info;
553
554         if(finfo == NULL || !desegment_rtp) {
555                 /* Hand the whole lot off to the subdissector */
556                 newtvb=tvb_new_subset(tvb,offset,data_len,data_reported_len);
557                 process_rtp_payload(newtvb, pinfo, tree, rtp_tree, payload_type);
558                 return;
559         }
560
561         seqno = p_conv_data->extended_seqno;
562
563         pinfo->can_desegment = 2;
564         pinfo->desegment_offset = 0;
565         pinfo->desegment_len = 0;
566
567 #ifdef DEBUG_FRAGMENTS
568         g_debug("%d: RTP Part of convo %d(%p); seqno %d",
569                 pinfo->fd->num,
570                 p_conv_data->frame_number, p_conv_data,
571                 seqno
572                 );
573 #endif
574
575         /* look for a pdu which we might be extending */
576         msp = (rtp_multisegment_pdu *)se_tree_lookup32_le(finfo->multisegment_pdus,seqno-1);
577
578         if(msp && msp->startseq < seqno && msp->endseq >= seqno) {
579                 guint32 fid = msp->startseq;
580                 fragment_data *fd_head;
581                 
582 #ifdef DEBUG_FRAGMENTS
583                 g_debug("\tContinues fragment %d", fid);
584 #endif
585
586                 /* we always assume the datagram is complete; if this is the
587                  * first pass, that's our best guess, and if it's not, what we
588                  * say gets ignored anyway.
589                  */
590                 fd_head = fragment_add_seq(tvb, offset, pinfo, fid, fragment_table,
591                                            seqno-msp->startseq, data_len, FALSE);
592
593                 newtvb = process_reassembled_data(tvb,offset, pinfo, "Reassembled RTP", fd_head,
594                                                   &rtp_fragment_items, NULL, tree);
595
596 #ifdef DEBUG_FRAGMENTS
597                 g_debug("\tFragment Coalesced; fd_head=%p, newtvb=%p (len %d)",fd_head, newtvb,
598                         newtvb?tvb_reported_length(newtvb):0);
599 #endif
600
601                 if(newtvb != NULL) {
602                         /* Hand off to the subdissector */
603                         process_rtp_payload(newtvb, pinfo, tree, rtp_tree, payload_type);
604             
605                         /*
606                          * Check to see if there were any complete fragments within the chunk
607                          */
608                         if( pinfo->desegment_len && pinfo->desegment_offset == 0 )
609                         {
610 #ifdef DEBUG_FRAGMENTS
611                                 g_debug("\tNo complete pdus in payload" );
612 #endif
613                                 /* Mark the fragments and not complete yet */
614                                 fragment_set_partial_reassembly(pinfo, fid, fragment_table);
615                                         
616                                 /* we must need another segment */
617                                 msp->endseq = MIN(msp->endseq,seqno) + 1;
618                         }
619                         else 
620                         {
621                                 /*
622                                  * Data was dissected so add the protocol tree to the display
623                                  */
624                                 proto_item *rtp_tree_item, *frag_tree_item;
625                                 /* this nargery is to insert the fragment tree into the main tree
626                                  * between the RTP protocol entry and the subdissector entry */
627                                 show_fragment_tree(fd_head, &rtp_fragment_items, tree, pinfo, newtvb, &frag_tree_item);
628                                 rtp_tree_item = proto_item_get_parent( proto_tree_get_parent( rtp_tree ));
629                                 if( frag_tree_item && rtp_tree_item )
630                                         proto_tree_move_item( tree, rtp_tree_item, frag_tree_item );
631
632             
633                                 if(pinfo->desegment_len) 
634                                 {
635                                         /* the higher-level dissector has asked for some more data - ie,
636                                            the end of this segment does not coincide with the end of a
637                                            higher-level PDU. */
638                                         must_desegment = TRUE;
639                                 }
640                         }       
641                 
642                 } 
643         
644         } 
645         else
646         {
647                 /*
648                  * The segment is not the continuation of a fragmented segment
649                  * so process it as normal
650                  */               
651 #ifdef DEBUG_FRAGMENTS
652                 g_debug("\tRTP non-fragment payload");
653 #endif
654                 newtvb = tvb_new_subset( tvb, offset, data_len, data_reported_len );
655         
656                 /* Hand off to the subdissector */
657                 process_rtp_payload(newtvb, pinfo, tree, rtp_tree, payload_type);
658         
659                 if(pinfo->desegment_len) {
660                         /* the higher-level dissector has asked for some more data - ie,
661                            the end of this segment does not coincide with the end of a
662                            higher-level PDU. */
663                         must_desegment = TRUE;
664                 }
665         }
666         
667         /* 
668          * There were bytes left over that the higher protocol couldn't dissect so save them
669          */
670         if(must_desegment)
671         {
672                 guint32 deseg_offset = pinfo->desegment_offset;
673                 guint32 frag_len = tvb_reported_length_remaining(newtvb, deseg_offset);
674                 fragment_data *fd_head = NULL;
675     
676 #ifdef DEBUG_FRAGMENTS
677                 g_debug("\tRTP Must Desegment: tvb_len=%d ds_len=%d %d frag_len=%d ds_off=%d",
678                         tvb_reported_length(newtvb),
679                         pinfo->desegment_len,
680                         pinfo->fd->flags.visited,
681                         frag_len,
682                         deseg_offset); 
683 #endif
684                 /* allocate a new msp for this pdu */
685                 msp = se_alloc(sizeof(rtp_multisegment_pdu));
686                 msp->startseq = seqno;
687                 msp->endseq = seqno+1;
688                 se_tree_insert32(finfo->multisegment_pdus,seqno,msp);
689                         
690                 /*
691                  * Add the fragment to the fragment table
692                  */    
693                 fd_head = fragment_add_seq(newtvb,deseg_offset, pinfo, seqno, fragment_table, 0, frag_len,
694                                            TRUE );
695
696                 if(fd_head != NULL)
697                 {
698                         if( fd_head->reassembled_in != 0 && !(fd_head->flags & FD_PARTIAL_REASSEMBLY) ) 
699                         {
700                                 proto_item *rtp_tree_item;
701                                 rtp_tree_item = proto_tree_add_uint( tree, hf_rtp_reassembled_in,
702                                                                      newtvb, deseg_offset, tvb_reported_length_remaining(newtvb,deseg_offset),
703                                                                      fd_head->reassembled_in);
704                                 PROTO_ITEM_SET_GENERATED(rtp_tree_item);          
705 #ifdef DEBUG_FRAGMENTS
706                                 g_debug("\tReassembled in %d", fd_head->reassembled_in);
707 #endif
708                         }         
709                         else 
710                         {
711 #ifdef DEBUG_FRAGMENTS
712                                 g_debug("\tUnfinished fragment");
713 #endif
714                                 /* this fragment is never reassembled */
715                                 proto_tree_add_text( tree, tvb, deseg_offset, -1,"RTP fragment, unfinished");
716                         }       
717                 }
718                 else
719                 {
720                         /* 
721                          * This fragment was the first fragment in a new entry in the
722                          * frag_table; we don't yet know where it is reassembled
723                          */      
724 #ifdef DEBUG_FRAGMENTS
725                         g_debug("\tnew pdu");
726 #endif
727                 }
728                         
729                 if( pinfo->desegment_offset == 0 ) 
730                 {
731                         if (check_col(pinfo->cinfo, COL_PROTOCOL))
732                         {
733                                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "RTP");
734                         }
735                         if (check_col(pinfo->cinfo, COL_INFO))
736                         {
737                                 col_set_str(pinfo->cinfo, COL_INFO, "[RTP segment of a reassembled PDU]");
738                         }
739                 }
740         }
741
742
743
744         pinfo->can_desegment = 0;
745         pinfo->desegment_offset = 0;
746         pinfo->desegment_len = 0;
747 }
748
749
750
751 static void
752 dissect_rtp_rfc2198(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree )
753 {
754         int offset = 0;
755         guint8 octet1;
756         int cnt;
757         gboolean hdr_follow = TRUE;
758         proto_item *ti = NULL;
759         proto_tree *rfc2198_tree = NULL;
760         proto_tree *rfc2198_hdr_tree = NULL;
761         rfc2198_hdr *hdr_last, *hdr_new;
762         rfc2198_hdr *hdr_chain = NULL;
763
764         /* Add try to RFC2198 data */
765         ti = proto_tree_add_text(tree, tvb, offset, -1, "RFC2198: Redundant Audio Data");
766         rfc2198_tree = proto_item_add_subtree(ti, ett_rtp_rfc2198);
767
768         hdr_last = NULL;
769         cnt = 0;
770         while (hdr_follow) {
771                 cnt++;
772
773                 /* Allocate and fill in header */
774                 hdr_new = ep_alloc(sizeof(rfc2198_hdr));
775                 hdr_new->next = NULL;
776                 octet1 = tvb_get_guint8(tvb, offset);
777                 hdr_new->pt = RTP_PAYLOAD_TYPE(octet1);
778                 hdr_follow = (octet1 & 0x80);
779
780                 /* Add a subtree for this header and add items */
781                 ti = proto_tree_add_text(rfc2198_tree, tvb, offset, (hdr_follow)?4:1, "Header %u", cnt);
782                 rfc2198_hdr_tree = proto_item_add_subtree(ti, ett_rtp_rfc2198_hdr);
783                 proto_tree_add_item(rfc2198_hdr_tree, hf_rtp_rfc2198_follow, tvb, offset, 1, FALSE );
784                 proto_tree_add_item(rfc2198_hdr_tree, hf_rtp_payload_type, tvb, offset, 1, FALSE );
785                 proto_item_append_text(ti, ": PT=%s", val_to_str(hdr_new->pt, rtp_payload_type_vals, "Unknown (%u)"));
786                 offset += 1;
787
788                 /* Timestamp offset and block length don't apply to last header */
789                 if (hdr_follow) {
790                         proto_tree_add_item(rfc2198_hdr_tree, hf_rtp_rfc2198_tm_off, tvb, offset, 2, FALSE );
791                         proto_tree_add_item(rfc2198_hdr_tree, hf_rtp_rfc2198_bl_len, tvb, offset + 1, 2, FALSE );
792                         hdr_new->len = tvb_get_ntohs(tvb, offset + 1) & 0x03FF;
793                         proto_item_append_text(ti, ", len=%u", hdr_new->len);
794                         offset += 3;
795                 } else {
796                         hdr_new->len = -1;
797                         hdr_follow = FALSE;
798                 }
799
800                 if (hdr_last) {
801                         hdr_last->next = hdr_new;
802                 } else {
803                         hdr_chain = hdr_new;
804                 }
805                 hdr_last = hdr_new;
806         }
807
808         /* Dissect each data block according to the header info */
809         hdr_last = hdr_chain;
810         while (hdr_last) {
811                 hdr_last->offset = offset;
812                 if (!hdr_last->next) {
813                         hdr_last->len = tvb_reported_length_remaining(tvb, offset);
814                 }
815                 dissect_rtp_data(tvb, pinfo, tree, rfc2198_tree, hdr_last->offset, hdr_last->len, hdr_last->len, hdr_last->pt);
816                 offset += hdr_last->len;
817                 hdr_last = hdr_last->next;
818         }
819 }
820
821 static void
822 dissect_rtp( tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree )
823 {
824         proto_item *ti            = NULL;
825         proto_tree *rtp_tree      = NULL;
826         proto_tree *rtp_csrc_tree = NULL;
827         guint8      octet1, octet2;
828         unsigned int version;
829         gboolean    padding_set;
830         gboolean    extension_set;
831         unsigned int csrc_count;
832         gboolean    marker_set;
833         unsigned int payload_type;
834         gchar *payload_type_str = NULL;
835         unsigned int i            = 0;
836         unsigned int hdr_extension= 0;
837         unsigned int padding_count;
838         gint        length, reported_length;
839         int         data_len;
840         unsigned int offset = 0;
841         guint16     seq_num;
842         guint32     timestamp;
843         guint32     sync_src;
844         guint32     csrc_item;
845         struct _rtp_conversation_info *p_conv_data = NULL;
846
847         /* Can tap up to 4 RTP packets within same packet */
848         static struct _rtp_info rtp_info_arr[4];
849         static int rtp_info_current=0;
850         struct _rtp_info *rtp_info;
851
852         rtp_info_current++;
853         if (rtp_info_current==4) {
854                 rtp_info_current=0;
855         }
856         rtp_info = &rtp_info_arr[rtp_info_current];
857
858         /* Get the fields in the first octet */
859         octet1 = tvb_get_guint8( tvb, offset );
860         version = RTP_VERSION( octet1 );
861
862         if (version == 0) {
863                 switch (global_rtp_version0_type) {
864                 case RTP0_STUN:
865                         call_dissector(stun_handle, tvb, pinfo, tree);
866                         return;
867
868                 case RTP0_T38:
869                         call_dissector(t38_handle, tvb, pinfo, tree);
870                         return;
871
872                 case RTP0_INVALID:
873                 default:
874                         ; /* Unknown or unsupported version (let it fall through */
875                 }
876         }
877
878         /* fill in the rtp_info structure */
879         rtp_info->info_version = version;
880         if (version != 2) {
881                 /*
882                  * Unknown or unsupported version.
883                  */
884                 if ( check_col( pinfo->cinfo, COL_PROTOCOL ) )   {
885                         col_set_str( pinfo->cinfo, COL_PROTOCOL, "RTP" );
886                 }
887
888                 if ( check_col( pinfo->cinfo, COL_INFO) ) {
889                         col_add_fstr( pinfo->cinfo, COL_INFO,
890                             "Unknown RTP version %u", version);
891                 }
892
893                 if ( tree ) {
894                         ti = proto_tree_add_item( tree, proto_rtp, tvb, offset, -1, FALSE );
895                         rtp_tree = proto_item_add_subtree( ti, ett_rtp );
896
897                         proto_tree_add_uint( rtp_tree, hf_rtp_version, tvb,
898                             offset, 1, octet1);
899                 }
900                 return;
901         }
902
903         padding_set = RTP_PADDING( octet1 );
904         extension_set = RTP_EXTENSION( octet1 );
905         csrc_count = RTP_CSRC_COUNT( octet1 );
906
907         /* Get the fields in the second octet */
908         octet2 = tvb_get_guint8( tvb, offset + 1 );
909         marker_set = RTP_MARKER( octet2 );
910         payload_type = RTP_PAYLOAD_TYPE( octet2 );
911
912         /* Get the subsequent fields */
913         seq_num = tvb_get_ntohs( tvb, offset + 2 );
914         timestamp = tvb_get_ntohl( tvb, offset + 4 );
915         sync_src = tvb_get_ntohl( tvb, offset + 8 );
916
917         /* fill in the rtp_info structure */
918         rtp_info->info_padding_set = padding_set;
919         rtp_info->info_padding_count = 0;
920         rtp_info->info_marker_set = marker_set;
921         rtp_info->info_payload_type = payload_type;
922         rtp_info->info_seq_num = seq_num;
923         rtp_info->info_timestamp = timestamp;
924         rtp_info->info_sync_src = sync_src;
925         rtp_info->info_setup_frame_num = 0;
926         rtp_info->info_payload_type_str = NULL;
927
928         /*
929          * Do we have all the data?
930          */
931         length = tvb_length_remaining(tvb, offset);
932         reported_length = tvb_reported_length_remaining(tvb, offset);
933         if (reported_length >= 0 && length >= reported_length) {
934                 /*
935                  * Yes.
936                  */
937                 rtp_info->info_all_data_present = TRUE;
938                 rtp_info->info_data_len = reported_length;
939
940                 /*
941                  * Save the pointer to raw rtp data (header + payload incl.
942                  * padding).
943                  * That should be safe because the "epan_dissect_t"
944                  * constructed for the packet has not yet been freed when
945                  * the taps are called.
946                  * (Destroying the "epan_dissect_t" will end up freeing
947                  * all the tvbuffs and hence invalidating pointers to
948                  * their data.)
949                  * See "add_packet_to_packet_list()" for details.
950                  */
951                 rtp_info->info_data = tvb_get_ptr(tvb, 0, -1);
952         } else {
953                 /*
954                  * No - packet was cut short at capture time.
955                  */
956                 rtp_info->info_all_data_present = FALSE;
957                 rtp_info->info_data_len = 0;
958                 rtp_info->info_data = NULL;
959         }
960
961         /* Look for conv and add to the frame if found */
962         get_conv_info(pinfo, rtp_info);
963         p_conv_data = p_get_proto_data(pinfo->fd, proto_rtp);
964
965         if ( check_col( pinfo->cinfo, COL_PROTOCOL ) )   {
966                 col_set_str( pinfo->cinfo, COL_PROTOCOL, "RTP" );
967         }
968
969         /* if it is dynamic payload, let use the conv data to see if it is defined */
970         if ( (payload_type>95) && (payload_type<128) ) {
971                 if (p_conv_data && p_conv_data->rtp_dyn_payload){
972                         payload_type_str = g_hash_table_lookup(p_conv_data->rtp_dyn_payload, &payload_type);
973                         rtp_info->info_payload_type_str = payload_type_str;
974                 }
975         }
976
977         if ( check_col( pinfo->cinfo, COL_INFO) ) {
978                 col_add_fstr( pinfo->cinfo, COL_INFO,
979                     "PT=%s, SSRC=0x%X, Seq=%u, Time=%u%s",
980                         payload_type_str ? payload_type_str : val_to_str( payload_type, rtp_payload_type_vals,"Unknown (%u)" ),
981                     sync_src,
982                     seq_num,
983                     timestamp,
984                     marker_set ? ", Mark " : " ");
985         }
986
987
988         if ( tree ) {
989                 proto_tree *item;
990                 /* Create RTP protocol tree */
991                 ti = proto_tree_add_item(tree, proto_rtp, tvb, offset, -1, FALSE );
992                 rtp_tree = proto_item_add_subtree(ti, ett_rtp );
993
994                 /* Conversation setup info */
995                 if (global_rtp_show_setup_info)
996                 {
997                         show_setup_info(tvb, pinfo, rtp_tree);
998                 }
999
1000                 proto_tree_add_uint( rtp_tree, hf_rtp_version, tvb,
1001                     offset, 1, octet1 );
1002                 proto_tree_add_boolean( rtp_tree, hf_rtp_padding, tvb,
1003                     offset, 1, octet1 );
1004                 proto_tree_add_boolean( rtp_tree, hf_rtp_extension, tvb,
1005                     offset, 1, octet1 );
1006                 proto_tree_add_uint( rtp_tree, hf_rtp_csrc_count, tvb,
1007                     offset, 1, octet1 );
1008                 offset++;
1009
1010                 proto_tree_add_boolean( rtp_tree, hf_rtp_marker, tvb, offset,
1011                     1, octet2 );
1012
1013                 item = proto_tree_add_uint_format( rtp_tree, hf_rtp_payload_type, tvb,
1014                     offset, 1, octet2, "Payload type: %s (%u)",
1015                         payload_type_str ? payload_type_str : val_to_str( payload_type, rtp_payload_type_vals,"Unknown"),
1016                         payload_type);
1017
1018                 offset++;
1019
1020                 /* Sequence number 16 bits (2 octets) */
1021                 proto_tree_add_uint( rtp_tree, hf_rtp_seq_nr, tvb, offset, 2, seq_num );
1022                 if(p_conv_data != NULL) {
1023                         item = proto_tree_add_uint( rtp_tree, hf_rtp_ext_seq_nr, tvb, offset, 2, p_conv_data->extended_seqno );
1024                         PROTO_ITEM_SET_GENERATED(item);
1025                 }
1026                 offset += 2;
1027
1028                 /* Timestamp 32 bits (4 octets) */
1029                 proto_tree_add_uint( rtp_tree, hf_rtp_timestamp, tvb, offset, 4, timestamp );
1030                 offset += 4;
1031
1032                 /* Synchronization source identifier 32 bits (4 octets) */
1033                 proto_tree_add_uint( rtp_tree, hf_rtp_ssrc, tvb, offset, 4, sync_src );
1034                 offset += 4;
1035         } else {
1036                 offset += 12;
1037         }
1038         /* CSRC list*/
1039         if ( csrc_count > 0 ) {
1040                 if ( tree ) {
1041                         ti = proto_tree_add_text(rtp_tree, tvb, offset, csrc_count * 4, "Contributing Source identifiers");
1042                         rtp_csrc_tree = proto_item_add_subtree( ti, ett_csrc_list );
1043                 }
1044                 for (i = 0; i < csrc_count; i++ ) {
1045                         csrc_item = tvb_get_ntohl( tvb, offset );
1046                         if ( tree ) proto_tree_add_uint_format( rtp_csrc_tree,
1047                             hf_rtp_csrc_item, tvb, offset, 4,
1048                             csrc_item,
1049                             "CSRC item %d: 0x%X",
1050                             i, csrc_item );
1051                         offset += 4;
1052                 }
1053         }
1054
1055         /* Optional RTP header extension */
1056         if ( extension_set ) {
1057                 /* Defined by profile field is 16 bits (2 octets) */
1058                 if ( tree ) proto_tree_add_uint( rtp_tree, hf_rtp_prof_define, tvb, offset, 2, tvb_get_ntohs( tvb, offset ) );
1059                 offset += 2;
1060
1061                 hdr_extension = tvb_get_ntohs( tvb, offset );
1062                 if ( tree ) proto_tree_add_uint( rtp_tree, hf_rtp_length, tvb,
1063                     offset, 2, hdr_extension);
1064                 offset += 2;
1065                 if ( hdr_extension > 0 ) {
1066                         if ( tree ) {
1067                                 ti = proto_tree_add_text(rtp_tree, tvb, offset, hdr_extension * 4, "Header extensions");
1068                                 /* I'm re-using the old tree variable here
1069                                    from the CSRC list!*/
1070                                 rtp_csrc_tree = proto_item_add_subtree( ti,
1071                                     ett_hdr_ext );
1072                         }
1073                         for (i = 0; i < hdr_extension; i++ ) {
1074                                 if ( tree ) proto_tree_add_uint( rtp_csrc_tree, hf_rtp_hdr_ext, tvb, offset, 4, tvb_get_ntohl( tvb, offset ) );
1075                                 offset += 4;
1076                         }
1077                 }
1078         }
1079
1080         if ( padding_set ) {
1081                 /*
1082                  * This RTP frame has padding - find it.
1083                  *
1084                  * The padding count is found in the LAST octet of
1085                  * the packet; it contains the number of octets
1086                  * that can be ignored at the end of the packet.
1087                  */
1088                 if (tvb_length(tvb) < tvb_reported_length(tvb)) {
1089                         /*
1090                          * We don't *have* the last octet of the
1091                          * packet, so we can't get the padding
1092                          * count.
1093                          *
1094                          * Put an indication of that into the
1095                          * tree, and just put in a raw data
1096                          * item.
1097                          */
1098                         if ( tree ) proto_tree_add_text(rtp_tree, tvb, 0, 0,
1099                             "Frame has padding, but not all the frame data was captured");
1100                         call_dissector(data_handle,
1101                             tvb_new_subset(tvb, offset, -1, -1),
1102                             pinfo, rtp_tree);
1103                         return;
1104                 }
1105
1106                 padding_count = tvb_get_guint8( tvb,
1107                     tvb_reported_length( tvb ) - 1 );
1108                 data_len =
1109                     tvb_reported_length_remaining( tvb, offset ) - padding_count;
1110
1111                 rtp_info->info_payload_offset = offset;
1112                 rtp_info->info_payload_len = tvb_length_remaining(tvb, offset);
1113                 rtp_info->info_padding_count = padding_count;
1114
1115                 if (data_len > 0) {
1116                         /*
1117                          * There's data left over when you take out
1118                          * the padding; dissect it.
1119                          */
1120                         dissect_rtp_data( tvb, pinfo, tree, rtp_tree,
1121                             offset,
1122                             data_len,
1123                             data_len,
1124                             payload_type );
1125                         offset += data_len;
1126                 } else if (data_len < 0) {
1127                         /*
1128                          * The padding count is bigger than the
1129                          * amount of RTP payload in the packet!
1130                          * Clip the padding count.
1131                          *
1132                          * XXX - put an item in the tree to indicate
1133                          * that the padding count is bogus?
1134                          */
1135                         padding_count =
1136                             tvb_reported_length_remaining(tvb, offset);
1137                 }
1138                 if (padding_count > 1) {
1139                         /*
1140                          * There's more than one byte of padding;
1141                          * show all but the last byte as padding
1142                          * data.
1143                          */
1144                         if ( tree ) proto_tree_add_item( rtp_tree, hf_rtp_padding_data,
1145                             tvb, offset, padding_count - 1, FALSE );
1146                         offset += padding_count - 1;
1147                 }
1148                 /*
1149                  * Show the last byte in the PDU as the padding
1150                  * count.
1151                  */
1152                 if ( tree ) proto_tree_add_item( rtp_tree, hf_rtp_padding_count,
1153                     tvb, offset, 1, FALSE );
1154         }
1155         else {
1156                 /*
1157                  * No padding.
1158                  */
1159                 dissect_rtp_data( tvb, pinfo, tree, rtp_tree, offset,
1160                     tvb_length_remaining( tvb, offset ),
1161                     tvb_reported_length_remaining( tvb, offset ),
1162                     payload_type );
1163                 rtp_info->info_payload_offset = offset;
1164                 rtp_info->info_payload_len = tvb_length_remaining(tvb, offset);
1165         }
1166         if (!pinfo->in_error_pkt)
1167                 tap_queue_packet(rtp_tap, pinfo, rtp_info);
1168 }
1169
1170
1171 /* calculate the extended sequence number - top 16 bits of the previous sequence number,
1172  * plus our own; then correct for wrapping */
1173 static guint32 calculate_extended_seqno(guint32 previous_seqno, guint16 raw_seqno)
1174 {
1175         guint32 seqno = (previous_seqno & 0xffff0000) | raw_seqno;
1176         if(seqno + 0x8000 < previous_seqno) {
1177                 seqno += 0x10000;
1178         } else if(previous_seqno + 0x8000 < seqno) {
1179                 /* we got an out-of-order packet which happened to go backwards over the
1180                  * wrap boundary */
1181                 seqno -= 0x10000;
1182         }
1183         return seqno;
1184 }
1185
1186 /* Look for conversation info */
1187 static void get_conv_info(packet_info *pinfo, struct _rtp_info *rtp_info)
1188 {
1189         /* Conversation and current data */
1190         conversation_t *p_conv = NULL;
1191         struct _rtp_conversation_info *p_conv_data = NULL;
1192
1193         /* Use existing packet info if available */
1194         p_conv_data = p_get_proto_data(pinfo->fd, proto_rtp);
1195
1196         if (!p_conv_data)
1197         {
1198                 /* First time, get info from conversation */
1199                 p_conv = find_conversation(pinfo->fd->num, &pinfo->net_dst, &pinfo->net_src,
1200                                            pinfo->ptype,
1201                                            pinfo->destport, pinfo->srcport, NO_ADDR_B);
1202                 if (p_conv)
1203                 {
1204                         /* Create space for packet info */
1205                         struct _rtp_conversation_info *p_conv_packet_data;
1206                         p_conv_data = conversation_get_proto_data(p_conv, proto_rtp);
1207
1208                         if (p_conv_data) {
1209                                 guint32 seqno;
1210
1211                                 /* Save this conversation info into packet info */
1212                                 p_conv_packet_data = se_alloc(sizeof(struct _rtp_conversation_info));
1213                                 g_snprintf(p_conv_packet_data->method, MAX_RTP_SETUP_METHOD_SIZE, "%s", p_conv_data->method);
1214                                 p_conv_packet_data->method[MAX_RTP_SETUP_METHOD_SIZE]=0;
1215                                 p_conv_packet_data->frame_number = p_conv_data->frame_number;
1216                                 p_conv_packet_data->rtp_dyn_payload = p_conv_data->rtp_dyn_payload;
1217                                 p_conv_packet_data->rtp_conv_info = p_conv_data->rtp_conv_info;
1218                                 p_add_proto_data(pinfo->fd, proto_rtp, p_conv_packet_data);
1219
1220                                 /* calculate extended sequence number */
1221                                 seqno = calculate_extended_seqno(p_conv_data->extended_seqno,
1222                                                                  rtp_info->info_seq_num);
1223
1224                                 p_conv_packet_data->extended_seqno = seqno;
1225                                 p_conv_data->extended_seqno = seqno;
1226                         }
1227                 }
1228         }
1229         if (p_conv_data) rtp_info->info_setup_frame_num = p_conv_data->frame_number;
1230 }
1231
1232
1233 /* Display setup info */
1234 static void show_setup_info(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
1235 {
1236         /* Conversation and current data */
1237         struct _rtp_conversation_info *p_conv_data = NULL;
1238                 proto_tree *rtp_setup_tree;
1239         proto_item *ti;
1240
1241         /* Use existing packet info if available */
1242         p_conv_data = p_get_proto_data(pinfo->fd, proto_rtp);
1243
1244         if (!p_conv_data) return;
1245
1246         /* Create setup info subtree with summary info. */
1247         ti =  proto_tree_add_string_format(tree, hf_rtp_setup, tvb, 0, 0,
1248                                                                "",
1249                                                                "Stream setup by %s (frame %u)",
1250                                                                p_conv_data->method,
1251                                                                p_conv_data->frame_number);
1252                 PROTO_ITEM_SET_GENERATED(ti);
1253                 rtp_setup_tree = proto_item_add_subtree(ti, ett_rtp_setup);
1254                 if (rtp_setup_tree)
1255                 {
1256                         /* Add details into subtree */
1257                         proto_item* item = proto_tree_add_uint(rtp_setup_tree, hf_rtp_setup_frame,
1258                                                                tvb, 0, 0, p_conv_data->frame_number);
1259                         PROTO_ITEM_SET_GENERATED(item);
1260                         item = proto_tree_add_string(rtp_setup_tree, hf_rtp_setup_method,
1261                                                      tvb, 0, 0, p_conv_data->method);
1262                         PROTO_ITEM_SET_GENERATED(item);
1263                 }
1264 }
1265
1266 /* Dissect PacketCable CCC header */
1267
1268 static void
1269 dissect_pkt_ccc(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
1270 {
1271         proto_item *ti            = NULL;
1272         proto_tree *pkt_ccc_tree      = NULL;
1273         const guint8 *ptime = tvb_get_ptr(tvb, 4, 8);
1274
1275         if ( tree ) {
1276                 ti = proto_tree_add_item(tree, proto_pkt_ccc, tvb, 0, 12, FALSE);
1277                 pkt_ccc_tree = proto_item_add_subtree(ti, ett_pkt_ccc);
1278
1279                 proto_tree_add_item(pkt_ccc_tree, hf_pkt_ccc_id, tvb, 0, 4, FALSE);
1280                 proto_tree_add_bytes_format(pkt_ccc_tree, hf_pkt_ccc_ts, tvb,
1281                     4, 8, "NTP timestamp: %s", ntp_fmt_ts(ptime));
1282         }
1283
1284         dissect_rtp(tvb, pinfo, tree);
1285 }
1286
1287
1288 /* Register PacketCable CCC */
1289
1290 void
1291 proto_register_pkt_ccc(void)
1292 {
1293         static hf_register_info hf[] =
1294         {
1295                 {
1296                         &hf_pkt_ccc_id,
1297                         {
1298                                 "PacketCable CCC Identifier",
1299                                 "pkt_ccc.ccc_id",
1300                                 FT_UINT32,
1301                                 BASE_DEC,
1302                                 NULL,
1303                                 0x0,
1304                                 "CCC_ID", HFILL
1305                         }
1306                 },
1307                 {
1308                         &hf_pkt_ccc_ts,
1309                         {
1310                                 "PacketCable CCC Timestamp",
1311                                 "pkt_ccc.ts",
1312                                 FT_BYTES,
1313                                 BASE_NONE,
1314                                 NULL,
1315                                 0x0,
1316                                 "Timestamp", HFILL
1317                         }
1318                 },
1319
1320         };
1321
1322         static gint *ett[] =
1323         {
1324                 &ett_pkt_ccc,
1325         };
1326
1327         module_t *pkt_ccc_module;
1328
1329
1330         proto_pkt_ccc = proto_register_protocol("PacketCable Call Content Connection",
1331             "PKT CCC", "pkt_ccc");
1332         proto_register_field_array(proto_pkt_ccc, hf, array_length(hf));
1333         proto_register_subtree_array(ett, array_length(ett));
1334
1335         register_dissector("pkt_ccc", dissect_pkt_ccc, proto_pkt_ccc);
1336
1337         pkt_ccc_module = prefs_register_protocol(proto_pkt_ccc, NULL);
1338
1339         prefs_register_uint_preference(pkt_ccc_module, "udp_port",
1340                                        "UDP port",
1341                                        "Decode packets on this UDP port as PacketCable CCC",
1342                                        10, &global_pkt_ccc_udp_port);
1343 }
1344
1345 void
1346 proto_reg_handoff_pkt_ccc(void)
1347 {
1348         /*
1349          * Register this dissector as one that can be selected by a
1350          * UDP port number.
1351          */
1352         pkt_ccc_handle = find_dissector("pkt_ccc");
1353         dissector_add_handle("udp.port", pkt_ccc_handle);
1354 }
1355
1356 /* Register RTP */
1357
1358 void
1359 proto_register_rtp(void)
1360 {
1361         static hf_register_info hf[] =
1362         {
1363                 {
1364                         &hf_rtp_version,
1365                         {
1366                                 "Version",
1367                                 "rtp.version",
1368                                 FT_UINT8,
1369                                 BASE_DEC,
1370                                 VALS(rtp_version_vals),
1371                                 0xC0,
1372                                 "", HFILL
1373                         }
1374                 },
1375                 {
1376                         &hf_rtp_padding,
1377                         {
1378                                 "Padding",
1379                                 "rtp.padding",
1380                                 FT_BOOLEAN,
1381                                 8,
1382                                 NULL,
1383                                 0x20,
1384                                 "", HFILL
1385                         }
1386                 },
1387                 {
1388                         &hf_rtp_extension,
1389                         {
1390                                 "Extension",
1391                                 "rtp.ext",
1392                                 FT_BOOLEAN,
1393                                 8,
1394                                 NULL,
1395                                 0x10,
1396                                 "", HFILL
1397                         }
1398                 },
1399                 {
1400                         &hf_rtp_csrc_count,
1401                         {
1402                                 "Contributing source identifiers count",
1403                                 "rtp.cc",
1404                                 FT_UINT8,
1405                                 BASE_DEC,
1406                                 NULL,
1407                                 0x0F,
1408                                 "", HFILL
1409                         }
1410                 },
1411                 {
1412                         &hf_rtp_marker,
1413                         {
1414                                 "Marker",
1415                                 "rtp.marker",
1416                                 FT_BOOLEAN,
1417                                 8,
1418                                 NULL,
1419                                 0x80,
1420                                 "", HFILL
1421                         }
1422                 },
1423                 {
1424                         &hf_rtp_payload_type,
1425                         {
1426                                 "Payload type",
1427                                 "rtp.p_type",
1428                                 FT_UINT8,
1429                                 BASE_DEC,
1430                                 NULL,
1431                                 0x7F,
1432                                 "", HFILL
1433                         }
1434                 },
1435                 {
1436                         &hf_rtp_seq_nr,
1437                         {
1438                                 "Sequence number",
1439                                 "rtp.seq",
1440                                 FT_UINT16,
1441                                 BASE_DEC,
1442                                 NULL,
1443                                 0x0,
1444                                 "", HFILL
1445                         }
1446                 },
1447                 {
1448                         &hf_rtp_ext_seq_nr,
1449                         {
1450                                 "Extended sequence number",
1451                                 "rtp.extseq",
1452                                 FT_UINT32,
1453                                 BASE_DEC,
1454                                 NULL,
1455                                 0x0,
1456                                 "", HFILL
1457                         }
1458                 },
1459                 {
1460                         &hf_rtp_timestamp,
1461                         {
1462                                 "Timestamp",
1463                                 "rtp.timestamp",
1464                                 FT_UINT32,
1465                                 BASE_DEC,
1466                                 NULL,
1467                                 0x0,
1468                                 "", HFILL
1469                         }
1470                 },
1471                 {
1472                         &hf_rtp_ssrc,
1473                         {
1474                                 "Synchronization Source identifier",
1475                                 "rtp.ssrc",
1476                                 FT_UINT32,
1477                                 BASE_HEX_DEC,
1478                                 NULL,
1479                                 0x0,
1480                                 "", HFILL
1481                         }
1482                 },
1483                 {
1484                         &hf_rtp_prof_define,
1485                         {
1486                                 "Defined by profile",
1487                                 "rtp.ext.profile",
1488                                 FT_UINT16,
1489                                 BASE_DEC,
1490                                 NULL,
1491                                 0x0,
1492                                 "", HFILL
1493                         }
1494                 },
1495                 {
1496                         &hf_rtp_length,
1497                         {
1498                                 "Extension length",
1499                                 "rtp.ext.len",
1500                                 FT_UINT16,
1501                                 BASE_DEC,
1502                                 NULL,
1503                                 0x0,
1504                                 "", HFILL
1505                         }
1506                 },
1507                 {
1508                         &hf_rtp_csrc_item,
1509                         {
1510                                 "CSRC item",
1511                                 "rtp.csrc.item",
1512                                 FT_UINT32,
1513                                 BASE_HEX_DEC,
1514                                 NULL,
1515                                 0x0,
1516                                 "", HFILL
1517                         }
1518                 },
1519                 {
1520                         &hf_rtp_hdr_ext,
1521                         {
1522                                 "Header extension",
1523                                 "rtp.hdr_ext",
1524                                 FT_UINT32,
1525                                 BASE_DEC,
1526                                 NULL,
1527                                 0x0,
1528                                 "", HFILL
1529                         }
1530                 },
1531                 {
1532                         &hf_rtp_data,
1533                         {
1534                                 "Payload",
1535                                 "rtp.payload",
1536                                 FT_BYTES,
1537                                 BASE_HEX,
1538                                 NULL,
1539                                 0x0,
1540                                 "", HFILL
1541                         }
1542                 },
1543                 {
1544                         &hf_rtp_padding_data,
1545                         {
1546                                 "Padding data",
1547                                 "rtp.padding.data",
1548                                 FT_BYTES,
1549                                 BASE_HEX,
1550                                 NULL,
1551                                 0x0,
1552                                 "", HFILL
1553                         }
1554                 },
1555                 {
1556                         &hf_rtp_padding_count,
1557                         {
1558                                 "Padding count",
1559                                 "rtp.padding.count",
1560                                 FT_UINT8,
1561                                 BASE_DEC,
1562                                 NULL,
1563                                 0x0,
1564                                 "", HFILL
1565                         }
1566                 },
1567                 {
1568                         &hf_rtp_setup,
1569                         {
1570                                 "Stream setup",
1571                                 "rtp.setup",
1572                                 FT_STRING,
1573                                 BASE_NONE,
1574                                 NULL,
1575                                 0x0,
1576                                 "Stream setup, method and frame number", HFILL
1577                         }
1578                 },
1579                 {
1580                         &hf_rtp_setup_frame,
1581                         {
1582                                 "Setup frame",
1583                                 "rtp.setup-frame",
1584                                 FT_FRAMENUM,
1585                                 BASE_NONE,
1586                                 NULL,
1587                                 0x0,
1588                                 "Frame that set up this stream", HFILL
1589                         }
1590                 },
1591                 {
1592                         &hf_rtp_setup_method,
1593                         {
1594                                 "Setup Method",
1595                                 "rtp.setup-method",
1596                                 FT_STRING,
1597                                 BASE_NONE,
1598                                 NULL,
1599                                 0x0,
1600                                 "Method used to set up this stream", HFILL
1601                         }
1602                 },
1603                 {
1604                         &hf_rtp_rfc2198_follow,
1605                         {
1606                                 "Follow",
1607                                 "rtp.follow",
1608                                 FT_BOOLEAN,
1609                                 8,
1610                                 TFS(&flags_set_truth),
1611                                 0x80,
1612                                 "Next header follows", HFILL
1613                         }
1614                 },
1615                 {
1616                         &hf_rtp_rfc2198_tm_off,
1617                         {
1618                                 "Timestamp offset",
1619                                 "rtp.timestamp-offset",
1620                                 FT_UINT16,
1621                                 BASE_DEC,
1622                                 NULL,
1623                                 0xFFFC,
1624                                 "Timestamp Offset", HFILL
1625                         }
1626                 },
1627                 {
1628                         &hf_rtp_rfc2198_bl_len,
1629                         {
1630                                 "Block length",
1631                                 "rtp.block-length",
1632                                 FT_UINT16,
1633                                 BASE_DEC,
1634                                 NULL,
1635                                 0x03FF,
1636                                 "Block Length", HFILL
1637                         }
1638                 },
1639         
1640                 /* reassembly stuff */
1641                 {&hf_rtp_fragments,
1642                  {"RTP Fragments", "rtp.fragments", FT_NONE, BASE_NONE, NULL, 0x0,
1643                   "RTP Fragments", HFILL }
1644                 },
1645
1646                 {&hf_rtp_fragment,
1647                  {"RTP Fragment data", "rtp.fragment", FT_FRAMENUM, BASE_NONE, NULL, 0x0,
1648                   "RTP Fragment data", HFILL }
1649                 },
1650
1651                 {&hf_rtp_fragment_overlap,
1652                  {"Fragment overlap", "rtp.fragment.overlap", FT_BOOLEAN, BASE_NONE,
1653                   NULL, 0x0, "Fragment overlaps with other fragments", HFILL }
1654                 },
1655
1656                 {&hf_rtp_fragment_overlap_conflict,
1657                  {"Conflicting data in fragment overlap", "rtp.fragment.overlap.conflict",
1658                   FT_BOOLEAN, BASE_NONE, NULL, 0x0,
1659                   "Overlapping fragments contained conflicting data", HFILL }
1660                 },
1661
1662                 {&hf_rtp_fragment_multiple_tails,
1663                  {"Multiple tail fragments found", "rtp.fragment.multipletails",
1664                   FT_BOOLEAN, BASE_NONE, NULL, 0x0,
1665                   "Several tails were found when defragmenting the packet", HFILL }
1666                 },
1667
1668                 {&hf_rtp_fragment_too_long_fragment,
1669                  {"Fragment too long", "rtp.fragment.toolongfragment",
1670                   FT_BOOLEAN, BASE_NONE, NULL, 0x0,
1671                   "Fragment contained data past end of packet", HFILL }
1672                 },
1673
1674                 {&hf_rtp_fragment_error,
1675                  {"Defragmentation error", "rtp.fragment.error",
1676                   FT_FRAMENUM, BASE_NONE, NULL, 0x0,
1677                   "Defragmentation error due to illegal fragments", HFILL }
1678                 },
1679
1680                 {&hf_rtp_reassembled_in,
1681                  {"RTP fragment, reassembled in frame", "rtp.reassembled_in",
1682                   FT_FRAMENUM, BASE_NONE, NULL, 0x0,
1683                   "This RTP packet is reassembled in this frame", HFILL }
1684                 }
1685
1686         };
1687
1688         static gint *ett[] =
1689         {
1690                 &ett_rtp,
1691                 &ett_csrc_list,
1692                 &ett_hdr_ext,
1693                 &ett_rtp_setup,
1694                 &ett_rtp_rfc2198,
1695                 &ett_rtp_rfc2198_hdr,
1696                 &ett_rtp_fragment,
1697                 &ett_rtp_fragments
1698         };
1699
1700         module_t *rtp_module;
1701
1702
1703         proto_rtp = proto_register_protocol("Real-Time Transport Protocol",
1704             "RTP", "rtp");
1705         proto_register_field_array(proto_rtp, hf, array_length(hf));
1706         proto_register_subtree_array(ett, array_length(ett));
1707
1708         register_dissector("rtp", dissect_rtp, proto_rtp);
1709         register_dissector("rtp.rfc2198", dissect_rtp_rfc2198, proto_rtp);
1710
1711         rtp_tap = register_tap("rtp");
1712
1713         rtp_pt_dissector_table = register_dissector_table("rtp.pt",
1714                                                           "RTP payload type", FT_UINT8, BASE_DEC);
1715         rtp_dyn_pt_dissector_table = register_dissector_table("rtp_dyn_payload_type",
1716                                                                                                     "Dynamic RTP payload type", FT_STRING, BASE_NONE);
1717
1718
1719         rtp_module = prefs_register_protocol(proto_rtp, proto_reg_handoff_rtp);
1720
1721         prefs_register_bool_preference(rtp_module, "show_setup_info",
1722                                        "Show stream setup information",
1723                                        "Where available, show which protocol and frame caused "
1724                                        "this RTP stream to be created",
1725                                        &global_rtp_show_setup_info);
1726
1727         prefs_register_bool_preference(rtp_module, "heuristic_rtp",
1728                                        "Try to decode RTP outside of conversations",
1729                                        "If call control SIP/H323/RTSP/.. messages are missing in the trace, "
1730                                        "RTP isn't decoded without this",
1731                                        &global_rtp_heur);
1732
1733         prefs_register_bool_preference(rtp_module, "desegment_rtp_streams",
1734                                        "Allow subdissector to reassemble RTP streams",
1735                                        "Whether subdissector can request RTP streams to be reassembled",
1736                                        &desegment_rtp);
1737
1738         prefs_register_enum_preference(rtp_module, "version0_type",
1739                                        "Treat RTP version 0 packets as",
1740                                        "If an RTP version 0 packet is encountered, it can be treated as an invalid packet, a STUN packet, or a T.38 packet",
1741                                        &global_rtp_version0_type,
1742                                        rtp_version0_types, FALSE);
1743     prefs_register_uint_preference(rtp_module,
1744                                    "rfc2198_payload_type", "Payload Type for RFC2198",
1745                                    "Payload Type for RFC2198 Redundant Audio Data",
1746                                    10,
1747                                    &rtp_rfc2198_pt);
1748     
1749         register_init_routine(rtp_fragment_init);
1750 }
1751
1752 void
1753 proto_reg_handoff_rtp(void)
1754 {
1755         static gboolean rtp_prefs_initialized = FALSE;
1756
1757         data_handle = find_dissector("data");
1758         stun_handle = find_dissector("stun");
1759         t38_handle = find_dissector("t38");
1760         /*
1761          * Register this dissector as one that can be selected by a
1762          * UDP port number.
1763          */
1764         rtp_handle = find_dissector("rtp");
1765         rtp_rfc2198_handle = find_dissector("rtp.rfc2198");
1766
1767         dissector_add_handle("udp.port", rtp_handle);
1768
1769         if (rtp_prefs_initialized) {
1770                 dissector_delete("rtp.pt", rtp_saved_rfc2198_pt, rtp_rfc2198_handle);
1771         } else {
1772                 rtp_prefs_initialized = TRUE;
1773         }
1774         rtp_saved_rfc2198_pt = rtp_rfc2198_pt;
1775         dissector_add("rtp.pt", rtp_saved_rfc2198_pt, rtp_rfc2198_handle);
1776
1777         heur_dissector_add( "udp", dissect_rtp_heur, proto_rtp);
1778 }
1779
1780 /*
1781  * Local Variables:
1782  * c-basic-offset: 8
1783  * indent-tabs-mode: t
1784  * tab-width: 8
1785  * End:
1786  */