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