3 * Routines for RTP dissection
4 * RTP = Real time Transport Protocol
6 * Copyright 2000, Philips Electronics N.V.
7 * Written by Andreas Sikkema <andreas.sikkema@philips.com>
9 * $Id: packet-rtp.c,v 1.25 2001/09/08 00:43:51 guy Exp $
11 * Ethereal - Network traffic analyzer
12 * By Gerald Combs <gerald@ethereal.com>
13 * Copyright 1998 Gerald Combs
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.
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.
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.
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
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
38 * This doesn't dissect older versions of RTP, such as:
40 * the vat protocol ("version 0") - see
42 * ftp://ftp.ee.lbl.gov/conferencing/vat/alpha-test/vatsrc-4.0b2.tar.gz
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
48 * version 1, as documented in
50 * ftp://gaia.cs.umass.edu/pub/hgschulz/rtp/draft-ietf-avt-rtp-04.txt
61 #ifdef HAVE_SYS_TYPES_H
62 # include <sys/types.h>
65 #ifdef HAVE_NETINET_IN_H
66 # include <netinet/in.h>
72 #include "packet-rtp.h"
73 #include "conversation.h"
75 /* RTP header fields */
76 static int proto_rtp = -1;
77 static int hf_rtp_version = -1;
78 static int hf_rtp_padding = -1;
79 static int hf_rtp_extension = -1;
80 static int hf_rtp_csrc_count = -1;
81 static int hf_rtp_marker = -1;
82 static int hf_rtp_payload_type = -1;
83 static int hf_rtp_seq_nr = -1;
84 static int hf_rtp_timestamp = -1;
85 static int hf_rtp_ssrc = -1;
86 static int hf_rtp_csrc_item = -1;
87 static int hf_rtp_data = -1;
88 static int hf_rtp_padding_data = -1;
89 static int hf_rtp_padding_count= -1;
91 /* RTP header extension fields */
92 static int hf_rtp_prof_define = -1;
93 static int hf_rtp_length = -1;
94 static int hf_rtp_hdr_ext = -1;
96 /* RTP fields defining a sub tree */
97 static gint ett_rtp = -1;
98 static gint ett_csrc_list = -1;
99 static gint ett_hdr_ext = -1;
101 static dissector_handle_t h261_handle;
102 static dissector_handle_t mpeg1_handle;
105 * Fields in the first octet of the RTP header.
108 /* Version is the first 2 bits of the first octet*/
109 #define RTP_VERSION(octet) ((octet) >> 6)
111 /* Padding is the third bit; No need to shift, because true is any value
113 #define RTP_PADDING(octet) ((octet) & 0x20)
115 /* Extension bit is the fourth bit */
116 #define RTP_EXTENSION(octet) ((octet) & 0x10)
118 /* CSRC count is the last four bits */
119 #define RTP_CSRC_COUNT(octet) ((octet) & 0xF)
121 static const value_string rtp_version_vals[] =
123 { 0, "Old VAT Version" },
124 { 1, "First Draft Version" },
125 { 2, "RFC 1889 Version" },
130 * Fields in the second octet of the RTP header.
133 /* Marker is the first bit of the second octet */
134 #define RTP_MARKER(octet) ((octet) & 0x80)
136 /* Payload type is the last 7 bits */
137 #define RTP_PAYLOAD_TYPE(octet) ((octet) & 0x7F)
141 * Table B.2 / H.225.0
149 #define PT_DVI4_8000 5
150 #define PT_DVI4_16000 6
154 #define PT_L16_STEREO 10
155 #define PT_L16_MONO 11
167 static const value_string rtp_payload_type_vals[] =
169 { PT_PCMU, "ITU-T G.711 PCMU" },
170 { PT_1016, "USA Federal Standard FS-1016" },
171 { PT_G721, "ITU-T G.721" },
172 { PT_GSM, "GSM 06.10" },
173 { PT_G723, "ITU-T G.723" },
174 { PT_DVI4_8000, "DVI4 8000 samples/s" },
175 { PT_DVI4_16000, "DVI4 16000 samples/s" },
177 { PT_PCMA, "ITU-T G.711 PCMA" },
178 { PT_G722, "ITU-T G.722" },
179 { PT_L16_STEREO, "16-bit uncompressed audio, stereo" },
180 { PT_L16_MONO, "16-bit uncompressed audio, monaural" },
181 { PT_MPA, "MPEG-I/II Audeo"},
182 { PT_G728, "ITU-T G.728" },
183 { PT_G729, "ITU-T G.729" },
184 { PT_CELB, "Sun CELL-B" },
186 { PT_NV, "'nv' program" },
187 { PT_H261, "ITU-T H.261" },
188 { PT_MPV, "MPEG-I/II Video"},
189 { PT_MP2T, "MPEG-II transport streams"},
190 { PT_H263, "ITU-T H.263" },
194 static address fake_addr;
195 static int heur_init = FALSE;
197 void rtp_add_address( packet_info *pinfo, const unsigned char* ip_addr,
201 conversation_t* pconv;
204 * If this isn't the first time this packet has been processed,
205 * we've already done this work, so we don't need to do it
208 if (pinfo->fd->flags.visited)
211 src_addr.type = AT_IPv4;
213 src_addr.data = ip_addr;
216 * The first time the function is called let the tcp dissector
217 * know that we're interested in traffic
220 heur_dissector_add( "udp", dissect_rtp_heur, proto_rtp );
225 * Check if the ip address an dport combination is not
228 pconv = find_conversation( &src_addr, &fake_addr, PT_UDP, prt, 0, 0 );
232 * XXX - use wildcard address and port B?
235 pconv = conversation_new( &src_addr, &fake_addr, PT_UDP,
236 (guint32) prt, (guint32) 0, 0 );
237 conversation_add_proto_data(pconv, proto_rtp, NULL);
243 static void rtp_init( void )
245 unsigned char* tmp_data;
248 /* Create a fake adddress... */
249 fake_addr.type = AT_IPv4;
252 tmp_data = malloc( fake_addr.len );
253 for ( i = 0; i < fake_addr.len; i++) {
256 fake_addr.data = tmp_data;
261 dissect_rtp_heur( tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree )
263 conversation_t* pconv;
265 if (!proto_is_protocol_enabled(proto_rtp))
266 return FALSE; /* RTP has been disabled */
268 /* This is a heuristic dissector, which means we get all the TCP
269 * traffic not sent to a known dissector and not claimed by
270 * a heuristic dissector called before us!
271 * So we first check if the frame is really meant for us.
273 if ( ( pconv = find_conversation( &pinfo->src, &fake_addr, pinfo->ptype,
274 pinfo->srcport, 0, 0 ) ) == NULL ) {
276 * The source ip:port combination was not what we were
277 * looking for, check the destination
279 if ( ( pconv = find_conversation( &pinfo->dst, &fake_addr,
280 pinfo->ptype, pinfo->destport, 0, 0 ) ) == NULL ) {
286 * An RTP conversation always has a data item for RTP.
287 * (Its existence is sufficient to indicate that this is an RTP
290 if (conversation_get_proto_data(pconv, proto_rtp) == NULL)
293 dissect_rtp( tvb, pinfo, tree );
299 dissect_rtp_data( tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
300 proto_tree *rtp_tree, int offset, unsigned int data_len,
301 unsigned int data_reported_len, unsigned int payload_type )
305 switch( payload_type ) {
307 newtvb = tvb_new_subset( tvb, offset, data_len,
309 call_dissector(h261_handle, newtvb, pinfo, tree);
313 newtvb = tvb_new_subset( tvb, offset, data_len,
315 call_dissector(mpeg1_handle, newtvb, pinfo, tree);
319 proto_tree_add_item( rtp_tree, hf_rtp_data, tvb, offset, data_len, FALSE );
325 dissect_rtp( tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree )
327 proto_item *ti = NULL;
328 proto_tree *rtp_tree = NULL;
329 proto_tree *rtp_csrc_tree = NULL;
331 unsigned int version;
332 gboolean padding_set;
333 gboolean extension_set;
334 unsigned int csrc_count;
336 unsigned int payload_type;
338 unsigned int hdr_extension= 0;
339 unsigned int padding_count;
341 unsigned int offset = 0;
347 CHECK_DISPLAY_AS_DATA(proto_rtp, tvb, pinfo, tree);
349 pinfo->current_proto = "RTP";
351 /* Get the fields in the first octet */
352 octet = tvb_get_guint8( tvb, offset );
353 version = RTP_VERSION( octet );
357 * Unknown or unsupported version.
359 if ( check_col( pinfo->fd, COL_PROTOCOL ) ) {
360 col_set_str( pinfo->fd, COL_PROTOCOL, "RTP" );
363 if ( check_col( pinfo->fd, COL_INFO) ) {
364 col_add_fstr( pinfo->fd, COL_INFO,
365 "Unknown RTP version %u", version);
369 ti = proto_tree_add_item( tree, proto_rtp, tvb, offset, tvb_length_remaining( tvb, offset ), FALSE );
370 rtp_tree = proto_item_add_subtree( ti, ett_rtp );
372 proto_tree_add_uint( rtp_tree, hf_rtp_version, tvb,
373 offset, 1, version );
378 padding_set = RTP_PADDING( octet );
379 extension_set = RTP_EXTENSION( octet );
380 csrc_count = RTP_CSRC_COUNT( octet );
382 /* Get the fields in the second octet */
383 octet = tvb_get_guint8( tvb, offset + 1 );
384 marker_set = RTP_MARKER( octet );
385 payload_type = RTP_PAYLOAD_TYPE( octet );
387 /* Get the subsequent fields */
388 seq_num = tvb_get_ntohs( tvb, offset + 2 );
389 timestamp = tvb_get_ntohl( tvb, offset + 4 );
390 sync_src = tvb_get_ntohl( tvb, offset + 8 );
392 if ( check_col( pinfo->fd, COL_PROTOCOL ) ) {
393 col_set_str( pinfo->fd, COL_PROTOCOL, "RTP" );
396 if ( check_col( pinfo->fd, COL_INFO) ) {
397 col_add_fstr( pinfo->fd, COL_INFO,
398 "Payload type=%s, SSRC=%u, Seq=%u, Time=%u%s",
399 val_to_str( payload_type, rtp_payload_type_vals,
404 marker_set ? ", Mark" : "");
408 ti = proto_tree_add_item( tree, proto_rtp, tvb, offset, tvb_length_remaining( tvb, offset ), FALSE );
409 rtp_tree = proto_item_add_subtree( ti, ett_rtp );
411 proto_tree_add_uint( rtp_tree, hf_rtp_version, tvb,
412 offset, 1, version );
413 proto_tree_add_boolean( rtp_tree, hf_rtp_padding, tvb,
414 offset, 1, padding_set );
415 proto_tree_add_boolean( rtp_tree, hf_rtp_extension, tvb,
416 offset, 1, extension_set );
417 proto_tree_add_uint( rtp_tree, hf_rtp_csrc_count, tvb,
418 offset, 1, csrc_count );
421 proto_tree_add_boolean( rtp_tree, hf_rtp_marker, tvb, offset,
423 proto_tree_add_uint( rtp_tree, hf_rtp_payload_type, tvb,
424 offset, 1, payload_type );
427 /* Sequence number 16 bits (2 octets) */
428 proto_tree_add_uint( rtp_tree, hf_rtp_seq_nr, tvb, offset, 2, seq_num );
431 /* Timestamp 32 bits (4 octets) */
432 proto_tree_add_uint( rtp_tree, hf_rtp_timestamp, tvb, offset, 4, timestamp );
435 /* Synchronization source identifier 32 bits (4 octets) */
436 proto_tree_add_uint( rtp_tree, hf_rtp_ssrc, tvb, offset, 4, sync_src );
440 if ( csrc_count > 0 ) {
441 ti = proto_tree_add_text(rtp_tree, tvb, offset, csrc_count * 4, "Contributing Source identifiers");
442 rtp_csrc_tree = proto_item_add_subtree( ti, ett_csrc_list );
443 for (i = 0; i < csrc_count; i++ ) {
444 csrc_item = tvb_get_ntohl( tvb, offset );
445 proto_tree_add_uint_format( rtp_csrc_tree,
446 hf_rtp_csrc_item, tvb, offset, 4,
454 /* Optional RTP header extension */
455 if ( extension_set ) {
456 /* Defined by profile field is 16 bits (2 octets) */
457 proto_tree_add_uint( rtp_tree, hf_rtp_prof_define, tvb, offset, 2, tvb_get_ntohs( tvb, offset ) );
460 hdr_extension = tvb_get_ntohs( tvb, offset );
461 proto_tree_add_uint( rtp_tree, hf_rtp_length, tvb,
462 offset, 2, hdr_extension);
464 if ( hdr_extension > 0 ) {
465 ti = proto_tree_add_text(rtp_tree, tvb, offset, csrc_count * 4, "Header extensions");
466 /* I'm re-using the old tree variable here
467 from the CSRC list!*/
468 rtp_csrc_tree = proto_item_add_subtree( ti,
470 for (i = 0; i < hdr_extension; i++ ) {
471 proto_tree_add_uint( rtp_csrc_tree, hf_rtp_hdr_ext, tvb, offset, 4, tvb_get_ntohl( tvb, offset ) );
479 * This RTP frame has padding - find it.
481 * The padding count is found in the LAST octet of
482 * the packet; it contains the number of octets
483 * that can be ignored at the end of the packet.
485 if (tvb_length(tvb) < tvb_reported_length(tvb)) {
487 * We don't *have* the last octet of the
488 * packet, so we can't get the padding
491 * Put an indication of that into the
492 * tree, and just put in a raw data
495 proto_tree_add_text(rtp_tree, tvb, 0, 0,
496 "Frame has padding, but not all the frame data was captured");
497 dissect_data(tvb, offset, pinfo, rtp_tree);
501 padding_count = tvb_get_guint8( tvb,
502 tvb_reported_length( tvb ) - 1 );
504 tvb_reported_length_remaining( tvb, offset ) - padding_count;
507 * There's data left over when you take out
508 * the padding; dissect it.
510 dissect_rtp_data( tvb, pinfo, tree, rtp_tree,
516 } else if (data_len < 0) {
518 * The padding count is bigger than the
519 * amount of RTP payload in the packet!
520 * Clip the padding count.
522 * XXX - put an item in the tree to indicate
523 * that the padding count is bogus?
526 tvb_reported_length_remaining(tvb, offset);
528 if (padding_count > 1) {
530 * There's more than one byte of padding;
531 * show all but the last byte as padding
534 proto_tree_add_item( rtp_tree, hf_rtp_padding_data,
535 tvb, offset, padding_count - 1, FALSE );
536 offset += padding_count - 1;
539 * Show the last byte in the PDU as the padding
542 proto_tree_add_item( rtp_tree, hf_rtp_padding_count,
543 tvb, offset, 1, FALSE );
549 dissect_rtp_data( tvb, pinfo, tree, rtp_tree, offset,
550 tvb_length_remaining( tvb, offset ),
551 tvb_reported_length_remaining( tvb, offset ),
558 proto_register_rtp(void)
560 static hf_register_info hf[] =
569 VALS(rtp_version_vals),
601 "Contributing source identifiers count",
623 &hf_rtp_payload_type,
629 VALS(rtp_payload_type_vals),
661 "Synchronization Source identifier",
673 "Defined by profile",
731 &hf_rtp_padding_data,
743 &hf_rtp_padding_count,
764 proto_rtp = proto_register_protocol("Real-Time Transport Protocol",
766 proto_register_field_array(proto_rtp, hf, array_length(hf));
767 proto_register_subtree_array(ett, array_length(ett));
769 register_dissector("rtp", dissect_rtp, proto_rtp);
772 register_init_routine( &rtp_init );
777 proto_reg_handoff_rtp(void)
780 * Get handles for the H.261 and MPEG-1 dissectors.
782 h261_handle = find_dissector("h261");
783 mpeg1_handle = find_dissector("mpeg1");
786 * Register this dissector as one that can be assigned to a
789 conv_dissector_add("udp", dissect_rtp, proto_rtp);