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