Call subdissectors even if we're not building a protocol tree.
[obnox/wireshark/wip.git] / packet-rtcp.c
1 /* packet-rtcp.c
2  *
3  * $Id: packet-rtcp.c,v 1.38 2003/11/09 22:55:34 guy Exp $
4  *
5  * Routines for RTCP dissection
6  * RTCP = Real-time Transport Control Protocol
7  *
8  * Copyright 2000, Philips Electronics N.V.
9  * Written by Andreas Sikkema <andreas.sikkema@philips.com>
10  *
11  * Ethereal - Network traffic analyzer
12  * By Gerald Combs <gerald@ethereal.com>
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 RTCP protocol according to Annex A
32  * of ITU-T Recommendation H.225.0 (02/98) and RFC 1889
33  * H.225.0 literally copies RFC 1889, but omitting a few sections.
34  *
35  * RTCP traffic is handled by an uneven UDP portnumber. This can be any
36  * port number, but there is a registered port available, port 5005
37  * See Annex B of ITU-T Recommendation H.225.0, section B.7
38  *
39  */
40
41
42 #ifdef HAVE_CONFIG_H
43 # include "config.h"
44 #endif
45
46 #include <glib.h>
47 #include <epan/packet.h>
48
49 #include <stdio.h>
50 #include <string.h>
51
52 #include "packet-rtcp.h"
53 #if 0
54 #include "packet-ntp.h"
55 #endif
56 #include <epan/conversation.h>
57
58 /* Version is the first 2 bits of the first octet*/
59 #define RTCP_VERSION(octet)     ((octet) >> 6)
60
61 /* Padding is the third bit; no need to shift, because true is any value
62    other than 0! */
63 #define RTCP_PADDING(octet)     ((octet) & 0x20)
64
65 /* Receiver/ Sender count is the 5 last bits  */
66 #define RTCP_COUNT(octet)       ((octet) & 0x1F)
67
68 static const value_string rtcp_version_vals[] =
69 {
70         { 0, "Old VAT Version" },
71         { 1, "First Draft Version" },
72         { 2, "RFC 1889 Version" },
73         { 0, NULL },
74 };
75
76 /* RTCP packet types according to Section A.11.1 */
77 #define RTCP_SR   200
78 #define RTCP_RR   201
79 #define RTCP_SDES 202
80 #define RTCP_BYE  203
81 #define RTCP_APP  204
82 /* Supplemental H.261 specific RTCP packet types according to Section C.3.5 */
83 #define RTCP_FIR  192
84 #define RTCP_NACK 193
85
86 static const value_string rtcp_packet_type_vals[] =
87 {
88         { RTCP_SR,   "Sender Report" },
89         { RTCP_RR,   "Receiver Report" },
90         { RTCP_SDES, "Source description" },
91         { RTCP_BYE,  "Goodbye" },
92         { RTCP_APP,  "Application specific" },
93         { RTCP_FIR,  "Full Intra-frame Request (H.261)" },
94         { RTCP_NACK, "Negative Acknowledgement (H.261)" },
95         { 0,         NULL },
96 };
97
98 /* RTCP SDES types (Section A.11.2) */
99 #define RTCP_SDES_END    0
100 #define RTCP_SDES_CNAME  1
101 #define RTCP_SDES_NAME   2
102 #define RTCP_SDES_EMAIL  3
103 #define RTCP_SDES_PHONE  4
104 #define RTCP_SDES_LOC    5
105 #define RTCP_SDES_TOOL   6
106 #define RTCP_SDES_NOTE   7
107 #define RTCP_SDES_PRIV   8
108
109 static const value_string rtcp_sdes_type_vals[] =
110 {
111         { RTCP_SDES_END,   "END" },
112         { RTCP_SDES_CNAME, "CNAME (user and domain)" },
113         { RTCP_SDES_NAME,  "NAME (common name)" },
114         { RTCP_SDES_EMAIL, "EMAIL (e-mail address)" },
115         { RTCP_SDES_PHONE, "PHONE (phone number)" },
116         { RTCP_SDES_LOC,   "LOC (geographic location)" },
117         { RTCP_SDES_TOOL,  "TOOL (name/version of source app)" },
118         { RTCP_SDES_NOTE,  "NOTE (note about source)" },
119         { RTCP_SDES_PRIV,  "PRIV (private extensions)" },
120         { 0,               NULL },
121 };
122
123 /* RTCP header fields                   */
124 static int proto_rtcp                = -1;
125 static int hf_rtcp_version           = -1;
126 static int hf_rtcp_padding           = -1;
127 static int hf_rtcp_rc                = -1;
128 static int hf_rtcp_sc                = -1;
129 static int hf_rtcp_pt                = -1;
130 static int hf_rtcp_length            = -1;
131 static int hf_rtcp_ssrc_sender       = -1;
132 static int hf_rtcp_ntp               = -1;
133 static int hf_rtcp_rtp_timestamp     = -1;
134 static int hf_rtcp_sender_pkt_cnt    = -1;
135 static int hf_rtcp_sender_oct_cnt    = -1;
136 static int hf_rtcp_ssrc_source       = -1;
137 static int hf_rtcp_ssrc_fraction     = -1;
138 static int hf_rtcp_ssrc_cum_nr       = -1;
139 /* First the 32 bit number, then the split
140  * up 16 bit values */
141 /* These two are added to a subtree */
142 static int hf_rtcp_ssrc_ext_high_seq = -1;
143 static int hf_rtcp_ssrc_high_seq     = -1;
144 static int hf_rtcp_ssrc_high_cycles  = -1;
145 static int hf_rtcp_ssrc_jitter       = -1;
146 static int hf_rtcp_ssrc_lsr          = -1;
147 static int hf_rtcp_ssrc_dlsr         = -1;
148 static int hf_rtcp_ssrc_csrc         = -1;
149 static int hf_rtcp_ssrc_type         = -1;
150 static int hf_rtcp_ssrc_length       = -1;
151 static int hf_rtcp_ssrc_text         = -1;
152 static int hf_rtcp_ssrc_prefix_len   = -1;
153 static int hf_rtcp_ssrc_prefix_string= -1;
154 static int hf_rtcp_subtype           = -1;
155 static int hf_rtcp_name_ascii        = -1;
156 static int hf_rtcp_app_data          = -1;
157 static int hf_rtcp_fsn               = -1;
158 static int hf_rtcp_blp               = -1;
159 static int hf_rtcp_padding_count     = -1;
160 static int hf_rtcp_padding_data      = -1;
161
162 /* RTCP fields defining a sub tree */
163 static gint ett_rtcp           = -1;
164 static gint ett_ssrc           = -1;
165 static gint ett_ssrc_item      = -1;
166 static gint ett_ssrc_ext_high  = -1;
167 static gint ett_sdes           = -1;
168 static gint ett_sdes_item      = -1;
169
170 static address fake_addr;
171 static int heur_init = FALSE;
172
173 static gboolean dissect_rtcp_heur( tvbuff_t *tvb, packet_info *pinfo,
174     proto_tree *tree );
175 static void dissect_rtcp( tvbuff_t *tvb, packet_info *pinfo,
176      proto_tree *tree );
177
178 void rtcp_add_address( packet_info *pinfo, const unsigned char* ip_addr,
179     int prt )
180 {
181         address src_addr;
182         conversation_t* pconv;
183
184         /*
185          * If this isn't the first time this packet has been processed,
186          * we've already done this work, so we don't need to do it
187          * again.
188          */
189         if (pinfo->fd->flags.visited)
190                 return;
191
192         src_addr.type = AT_IPv4;
193         src_addr.len = 4;
194         src_addr.data = ip_addr;
195
196         /*
197          * The first time the function is called let the udp dissector
198          * know that we're interested in traffic
199          */
200         if ( ! heur_init ) {
201                 heur_dissector_add( "udp", dissect_rtcp_heur, proto_rtcp );
202                 heur_init = TRUE;
203         }
204
205         /*
206          * Check if the ip address and port combination is not
207          * already registered
208          */
209         pconv = find_conversation( &src_addr, &fake_addr, PT_UDP, prt, 0, 0 );
210
211         /*
212          * If not, add
213          * XXX - use wildcard address and port B?
214          */
215         if ( ! pconv ) {
216                 pconv = conversation_new( &src_addr, &fake_addr, PT_UDP,
217                     (guint32) prt, (guint32) 0, 0 );
218                 conversation_add_proto_data(pconv, proto_rtcp, NULL);
219         }
220
221 }
222
223 #if 0
224 static void rtcp_init( void )
225 {
226         unsigned char* tmp_data;
227         int i;
228
229         /* Create a fake adddress... */
230         fake_addr.type = AT_IPv4;
231         fake_addr.len = 4;
232
233         tmp_data = g_malloc( fake_addr.len );
234         for ( i = 0; i < fake_addr.len; i++) {
235                 tmp_data[i] = 0;
236         }
237         fake_addr.data = tmp_data;
238 }
239 #endif
240
241 static gboolean
242 dissect_rtcp_heur( tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree )
243 {
244         conversation_t* pconv;
245
246         /* This is a heuristic dissector, which means we get all the UDP
247          * traffic not sent to a known dissector and not claimed by
248          * a heuristic dissector called before us!
249          * So we first check if the frame is really meant for us.
250          */
251         if ( ( pconv = find_conversation( &pinfo->src, &fake_addr, pinfo->ptype,
252             pinfo->srcport, 0, 0 ) ) == NULL ) {
253                 /*
254                  * The source ip:port combination was not what we were
255                  * looking for, check the destination
256                  */
257                 if ( ( pconv = find_conversation( &pinfo->dst, &fake_addr,
258                     pinfo->ptype, pinfo->destport, 0, 0 ) ) == NULL ) {
259                         return FALSE;
260                 }
261         }
262
263
264         /*
265          * An RTCP conversation always has a data item for RTCP.
266          * (Its existence is sufficient to indicate that this is an RTCP
267          * conversation.)
268          */
269         if (conversation_get_proto_data(pconv, proto_rtcp) == NULL)
270                 return FALSE;
271
272         /*
273          * The message is a valid RTCP message!
274          */
275         dissect_rtcp( tvb, pinfo, tree );
276
277         return TRUE;
278 }
279
280
281 static int
282 dissect_rtcp_nack( tvbuff_t *tvb, int offset, proto_tree *tree )
283 {
284         /* Packet type = FIR (H261) */
285         proto_tree_add_uint( tree, hf_rtcp_rc, tvb, offset, 1, tvb_get_guint8( tvb, offset ) );
286         offset++;
287         /* Packet type, 8 bits  = APP */
288         proto_tree_add_item( tree, hf_rtcp_pt, tvb, offset, 1, FALSE );
289         offset++;
290
291         /* Packet length in 32 bit words minus one */
292         proto_tree_add_uint( tree, hf_rtcp_length, tvb, offset, 2, tvb_get_ntohs( tvb, offset ) );
293         offset += 2;
294
295         /* SSRC  */
296         proto_tree_add_uint( tree, hf_rtcp_ssrc_source, tvb, offset, 4, tvb_get_ntohl( tvb, offset ) );
297         offset += 4;
298
299         /* FSN, 16 bits */
300         proto_tree_add_uint( tree, hf_rtcp_fsn, tvb, offset, 2, tvb_get_ntohs( tvb, offset ) );
301         offset += 2;
302
303         /* BLP, 16 bits */
304         proto_tree_add_uint( tree, hf_rtcp_blp, tvb, offset, 2, tvb_get_ntohs( tvb, offset ) );
305         offset += 2;
306
307         return offset;
308 }
309
310 static int
311 dissect_rtcp_fir( tvbuff_t *tvb, int offset, proto_tree *tree )
312 {
313         /* Packet type = FIR (H261) */
314         proto_tree_add_uint( tree, hf_rtcp_rc, tvb, offset, 1, tvb_get_guint8( tvb, offset ) );
315         offset++;
316         /* Packet type, 8 bits  = APP */
317         proto_tree_add_item( tree, hf_rtcp_pt, tvb, offset, 1, FALSE );
318         offset++;
319
320         /* Packet length in 32 bit words minus one */
321         proto_tree_add_uint( tree, hf_rtcp_length, tvb, offset, 2, tvb_get_ntohs( tvb, offset ) );
322         offset += 2;
323
324         /* SSRC  */
325         proto_tree_add_uint( tree, hf_rtcp_ssrc_source, tvb, offset, 4, tvb_get_ntohl( tvb, offset ) );
326         offset += 4;
327
328         return offset;
329 }
330
331 static int
332 dissect_rtcp_app( tvbuff_t *tvb, int offset, proto_tree *tree,
333     unsigned int padding, unsigned int packet_len )
334 {
335         unsigned int counter = 0;
336         char ascii_name[5];
337
338         /* SSRC / CSRC */
339         proto_tree_add_uint( tree, hf_rtcp_ssrc_source, tvb, offset, 4, tvb_get_ntohl( tvb, offset ) );
340         offset += 4;
341         packet_len -= 4;
342
343         /* Name (ASCII) */
344         for( counter = 0; counter < 4; counter++ )
345             ascii_name[ counter ] = tvb_get_guint8( tvb, offset + counter );
346         /* strncpy( ascii_name, pd + offset, 4 ); */
347         ascii_name[4] = '\0';
348         proto_tree_add_string( tree, hf_rtcp_name_ascii, tvb, offset, 4,
349             ascii_name );
350         offset += 4;
351         packet_len -= 4;
352
353         /* Applications specific data */
354         if ( padding ) {
355                 /* If there's padding present, we have to remove that from the data part
356                  * The last octet of the packet contains the length of the padding
357                  */
358                 packet_len -= tvb_get_guint8( tvb, offset + packet_len - 1 );
359         }
360         proto_tree_add_item( tree, hf_rtcp_app_data, tvb, offset, packet_len, FALSE );
361         offset += packet_len;
362
363         return offset;
364 }
365
366 static int
367 dissect_rtcp_bye( tvbuff_t *tvb, int offset, proto_tree *tree,
368     unsigned int count )
369 {
370         unsigned int chunk          = 1;
371         unsigned int reason_length  = 0;
372         unsigned int counter = 0;
373         char* reason_text = NULL;
374
375         while ( chunk <= count ) {
376                 /* source identifier, 32 bits */
377                 proto_tree_add_item( tree, hf_rtcp_ssrc_source, tvb, offset, 4, FALSE);
378                 offset += 4;
379                 chunk++;
380         }
381
382         if ( tvb_reported_length_remaining( tvb, offset ) > 0 ) {
383                 /* Bye reason consists of an 8 bit length l and a string with length l */
384                 reason_length = tvb_get_guint8( tvb, offset );
385                 proto_tree_add_item( tree, hf_rtcp_ssrc_length, tvb, offset, 1, FALSE );
386                 offset++;
387
388                 reason_text = g_malloc( reason_length + 1 );
389                 for ( counter = 0; counter < reason_length; counter++ ) reason_text[ counter ] = tvb_get_guint8( tvb, offset + counter );
390                 /* strncpy( reason_text, pd + offset, reason_length ); */
391                 reason_text[ reason_length ] = '\0';
392                 proto_tree_add_string( tree, hf_rtcp_ssrc_text, tvb, offset, reason_length, reason_text );
393                 g_free( reason_text );
394                 offset += reason_length;
395         }
396
397         return offset;
398
399 }
400
401 static void
402 dissect_rtcp_sdes( tvbuff_t *tvb, int offset, proto_tree *tree,
403     unsigned int count )
404 {
405         unsigned int chunk          = 1;
406         proto_item *sdes_item;
407         proto_tree *sdes_tree;
408         proto_tree *sdes_item_tree;
409         proto_item *ti;
410         int start_offset;
411         int items_start_offset;
412         guint32 ssrc;
413         unsigned int item_len       = 0;
414         unsigned int sdes_type      = 0;
415         unsigned int counter        = 0;
416         unsigned int prefix_len     = 0;
417         char *prefix_string = NULL;
418
419         while ( chunk <= count ) {
420                 /* Create a subtree for this chunk; we don't yet know
421                    the length. */
422                 start_offset = offset;
423
424                 ssrc = tvb_get_ntohl( tvb, offset );
425                 sdes_item = proto_tree_add_text(tree, tvb, offset, -1,
426                     "Chunk %u, SSRC/CSRC %u", chunk, ssrc);
427                 sdes_tree = proto_item_add_subtree( sdes_item, ett_sdes );
428
429                 /* SSRC_n source identifier, 32 bits */
430                 proto_tree_add_uint( sdes_tree, hf_rtcp_ssrc_source, tvb, offset, 4, ssrc );
431                 offset += 4;
432
433                 /* Create a subtree for the SDES items; we don't yet know
434                    the length */
435                 items_start_offset = offset;
436                 ti = proto_tree_add_text(sdes_tree, tvb, offset, -1,
437                     "SDES items" );
438                 sdes_item_tree = proto_item_add_subtree( ti, ett_sdes_item );
439
440                 /*
441                  * Not every message is ended with "null" bytes, so check for
442                  * end of frame instead.
443                  */
444                 while ( ( tvb_reported_length_remaining( tvb, offset ) > 0 )
445                     && ( tvb_get_guint8( tvb, offset ) != RTCP_SDES_END ) ) {
446                         /* ID, 8 bits */
447                         sdes_type = tvb_get_guint8( tvb, offset );
448                         proto_tree_add_item( sdes_item_tree, hf_rtcp_ssrc_type, tvb, offset, 1, FALSE );
449                         offset++;
450
451                         /* Item length, 8 bits */
452                         item_len = tvb_get_guint8( tvb, offset );
453                         proto_tree_add_item( sdes_item_tree, hf_rtcp_ssrc_length, tvb, offset, 1, FALSE );
454                         offset++;
455
456                         if ( sdes_type == RTCP_SDES_PRIV ) {
457                                 /* PRIV adds two items between the SDES length
458                                  * and value - an 8 bit length giving the
459                                  * length of a "prefix string", and the string.
460                                  */
461                                 prefix_len = tvb_get_guint8( tvb, offset );
462                                 proto_tree_add_item( sdes_item_tree, hf_rtcp_ssrc_prefix_len, tvb, offset, 1, FALSE );
463                                 offset++;
464
465                                 prefix_string = g_malloc( prefix_len + 1 );
466                                 for ( counter = 0; counter < prefix_len; counter++ )
467                                         prefix_string[ counter ] =
468                                             tvb_get_guint8( tvb, offset + counter );
469                                 /* strncpy( prefix_string, pd + offset, prefix_len ); */
470                                 prefix_string[ prefix_len ] = '\0';
471                                 proto_tree_add_string( sdes_item_tree, hf_rtcp_ssrc_prefix_string, tvb, offset, prefix_len, prefix_string );
472                                 g_free( prefix_string );
473                                 offset += prefix_len;
474                         }
475                         prefix_string = g_malloc( item_len + 1 );
476                         for ( counter = 0; counter < item_len; counter++ )
477                             prefix_string[ counter ] =
478                                 tvb_get_guint8( tvb, offset + counter );
479                         /* strncpy( prefix_string, pd + offset, item_len ); */
480                         prefix_string[ item_len] = 0;
481                         proto_tree_add_string( sdes_item_tree, hf_rtcp_ssrc_text, tvb, offset, item_len, prefix_string );
482                         g_free( prefix_string );
483                         offset += item_len;
484                 }
485
486                 /* Set the length of the items subtree. */
487                 proto_item_set_len(ti, offset - items_start_offset);
488
489                 /* 32 bits = 4 bytes, so.....
490                  * If offset % 4 != 0, we divide offset by 4, add one and then
491                  * multiply by 4 again to reach the boundary
492                  */
493                 if ( offset % 4 != 0 )
494                         offset = ((offset / 4) + 1 ) * 4;
495
496                 /* Set the length of this chunk. */
497                 proto_item_set_len(sdes_item, offset - start_offset);
498
499                 chunk++;
500         }
501 }
502
503 static int
504 dissect_rtcp_rr( tvbuff_t *tvb, int offset, proto_tree *tree,
505     unsigned int count )
506 {
507         unsigned int counter = 1;
508         proto_tree *ssrc_tree = (proto_tree*) NULL;
509         proto_tree *ssrc_sub_tree = (proto_tree*) NULL;
510         proto_tree *high_sec_tree = (proto_tree*) NULL;
511         proto_item *ti = (proto_item*) NULL;
512         guint8 rr_flt;
513         unsigned int cum_nr = 0;
514
515         while ( counter <= count ) {
516                 /* Create a new subtree for a length of 24 bytes */
517                 ti = proto_tree_add_text(tree, tvb, offset, 24,
518                     "Source %u", counter );
519                 ssrc_tree = proto_item_add_subtree( ti, ett_ssrc );
520
521                 /* SSRC_n source identifier, 32 bits */
522                 proto_tree_add_uint( ssrc_tree, hf_rtcp_ssrc_source, tvb, offset, 4, tvb_get_ntohl( tvb, offset ) );
523                 offset += 4;
524
525                 ti = proto_tree_add_text(ssrc_tree, tvb, offset, 20, "SSRC contents" );
526                 ssrc_sub_tree = proto_item_add_subtree( ti, ett_ssrc_item );
527
528                 /* Fraction lost, 8bits */
529                 rr_flt = tvb_get_guint8( tvb, offset );
530                 proto_tree_add_uint_format( ssrc_sub_tree, hf_rtcp_ssrc_fraction, tvb,
531                     offset, 1, rr_flt, "Fraction lost: %u / 256", rr_flt );
532                 offset++;
533
534                 /* Cumulative number of packets lost, 24 bits */
535                 cum_nr = tvb_get_ntohl( tvb, offset ) >> 8;
536                 proto_tree_add_uint( ssrc_sub_tree, hf_rtcp_ssrc_cum_nr, tvb,
537                     offset, 3, cum_nr );
538                 offset += 3;
539
540                 /* Extended highest sequence nr received, 32 bits
541                  * Just for the sake of it, let's add another subtree
542                  * because this might be a little clearer
543                  */
544                 ti = proto_tree_add_uint( ssrc_tree, hf_rtcp_ssrc_ext_high_seq,
545                     tvb, offset, 4, tvb_get_ntohl( tvb, offset ) );
546                 high_sec_tree = proto_item_add_subtree( ti, ett_ssrc_ext_high );
547                 /* Sequence number cycles */
548                 proto_tree_add_uint( high_sec_tree, hf_rtcp_ssrc_high_cycles,
549                     tvb, offset, 2, tvb_get_ntohs( tvb, offset ) );
550                 offset += 2;
551                 /* highest sequence number received */
552                 proto_tree_add_uint( high_sec_tree, hf_rtcp_ssrc_high_seq,
553                     tvb, offset, 2, tvb_get_ntohs( tvb, offset ) );
554                 offset += 2;
555
556                 /* Interarrival jitter */
557                 proto_tree_add_uint( ssrc_tree, hf_rtcp_ssrc_jitter, tvb,
558                     offset, 4, tvb_get_ntohl( tvb, offset ) );
559                 offset += 4;
560
561                 /* Last SR timestamp */
562                 proto_tree_add_uint( ssrc_tree, hf_rtcp_ssrc_lsr, tvb,
563                     offset, 4, tvb_get_ntohl( tvb, offset ) );
564                 offset += 4;
565
566                 /* Delay since last SR timestamp */
567                 proto_tree_add_uint( ssrc_tree, hf_rtcp_ssrc_dlsr, tvb,
568                     offset, 4, tvb_get_ntohl( tvb, offset ) );
569                 offset += 4;
570                 counter++;
571         }
572
573         return offset;
574 }
575
576 static int
577 dissect_rtcp_sr( tvbuff_t *tvb, int offset, proto_tree *tree,
578     unsigned int count )
579 {
580 #if 0
581         gchar buff[ NTP_TS_SIZE ];
582         char* ptime = tvb_get_ptr( tvb, offset, 8 );
583
584         /* Retreive the NTP timestamp. Using the NTP dissector for this */
585         ntp_fmt_ts( ptime, buff );
586         proto_tree_add_string_format( tree, hf_rtcp_ntp, tvb, offset, 8, ( const char* ) &buff, "NTP timestamp: %s", &buff );
587         free( ptime ); ??????????????????????????????????????????????????????????????????
588         offset += 8;
589 #else
590         /*
591          * XXX - RFC 1889 says this is an NTP timestamp, but that appears
592          * not to be the case.
593          */
594         proto_tree_add_text(tree, tvb, offset, 4, "Timestamp, MSW: %u",
595                 tvb_get_ntohl(tvb, offset));
596         offset += 4;
597         proto_tree_add_text(tree, tvb, offset, 4, "Timestamp, LSW: %u",
598                 tvb_get_ntohl(tvb, offset));
599         offset += 4;
600 #endif
601         /* RTP timestamp, 32 bits */
602         proto_tree_add_uint( tree, hf_rtcp_rtp_timestamp, tvb, offset, 4, tvb_get_ntohl( tvb, offset ) );
603         offset += 4;
604         /* Sender's packet count, 32 bits */
605         proto_tree_add_uint( tree, hf_rtcp_sender_pkt_cnt, tvb, offset, 4, tvb_get_ntohl( tvb, offset ) );
606         offset += 4;
607         /* Sender's octet count, 32 bits */
608         proto_tree_add_uint( tree, hf_rtcp_sender_oct_cnt, tvb, offset, 4, tvb_get_ntohl( tvb, offset ) );
609         offset += 4;
610
611         /* The rest of the packet is equal to the RR packet */
612         if ( count != 0 )
613                 offset = dissect_rtcp_rr( tvb, offset, tree, count );
614
615         return offset;
616 }
617
618 static void
619 dissect_rtcp( tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree )
620 {
621         proto_item *ti           = NULL;
622         proto_tree *rtcp_tree    = NULL;
623         unsigned int temp_byte   = 0;
624         unsigned int padding_set = 0;
625         unsigned int elem_count  = 0;
626         unsigned int packet_type = 0;
627         unsigned int offset      = 0;
628         guint16 packet_length    = 0;
629
630         if ( check_col( pinfo->cinfo, COL_PROTOCOL ) )   {
631                 col_set_str( pinfo->cinfo, COL_PROTOCOL, "RTCP" );
632         }
633
634         if ( check_col( pinfo->cinfo, COL_INFO) ) {
635                 /* The second octet contains the packet type */
636                 /* switch ( pd[ offset + 1 ] ) { */
637                 switch ( tvb_get_guint8( tvb, 1 ) ) {
638                         case RTCP_SR:
639                                 col_set_str( pinfo->cinfo, COL_INFO, "Sender Report");
640                                 break;
641                         case RTCP_RR:
642                                 col_set_str( pinfo->cinfo, COL_INFO, "Receiver Report");
643                                 break;
644                         case RTCP_SDES:
645                                 col_set_str( pinfo->cinfo, COL_INFO, "Source Description");
646                                 break;
647                         case RTCP_BYE:
648                                 col_set_str( pinfo->cinfo, COL_INFO, "Goodbye");
649                                 break;
650                         case RTCP_APP:
651                                 col_set_str( pinfo->cinfo, COL_INFO, "Application defined");
652                                 break;
653                         case RTCP_FIR:
654                                 col_set_str( pinfo->cinfo, COL_INFO, "Full Intra-frame Request (H.261)");
655                                 break;
656                         case RTCP_NACK:
657                                 col_set_str( pinfo->cinfo, COL_INFO, "Negative Acknowledgement (H.261)");
658                                 break;
659                         default:
660                                 col_set_str( pinfo->cinfo, COL_INFO, "Unknown packet type");
661                                 break;
662                 }
663         }
664
665         if ( tree ) {
666
667                 /*
668                  * Check if there are at least 4 bytes left in the frame,
669                  * the last 16 bits of those is the length of the current
670                  * RTCP message. The last compound message contains padding,
671                  * that enables us to break from the while loop.
672                  */
673                 while ( tvb_bytes_exist( tvb, offset, 4) ) {
674                         /*
675                          * First retreive the packet_type
676                          */
677                         packet_type = tvb_get_guint8( tvb, offset + 1 );
678
679                         /*
680                          * Check if it's a valid type
681                          */
682                         if ( ( packet_type < 192 ) || ( packet_type >  204 ) )
683                                 break;
684
685                         /*
686                          * get the packet-length for the complete RTCP packet
687                          */
688                         packet_length = ( tvb_get_ntohs( tvb, offset + 2 ) + 1 ) * 4;
689
690                         ti = proto_tree_add_item(tree, proto_rtcp, tvb, offset, packet_length, FALSE );
691                         rtcp_tree = proto_item_add_subtree( ti, ett_rtcp );
692
693                         temp_byte = tvb_get_guint8( tvb, offset );
694
695                         proto_tree_add_uint( rtcp_tree, hf_rtcp_version, tvb,
696                             offset, 1, temp_byte);
697                         padding_set = RTCP_PADDING( temp_byte );
698                         proto_tree_add_boolean( rtcp_tree, hf_rtcp_padding, tvb,
699                             offset, 1, temp_byte );
700                         elem_count = RTCP_COUNT( temp_byte );
701
702                         switch ( packet_type ) {
703                                 case RTCP_SR:
704                                 case RTCP_RR:
705                                         /* Receiver report count, 5 bits */
706                                         proto_tree_add_uint( rtcp_tree, hf_rtcp_rc, tvb, offset, 1, temp_byte );
707                                         offset++;
708                                         /* Packet type, 8 bits */
709                                         proto_tree_add_item( rtcp_tree, hf_rtcp_pt, tvb, offset, 1, FALSE );
710                                         offset++;
711                                         /* Packet length in 32 bit words MINUS one, 16 bits */
712                                         proto_tree_add_uint( rtcp_tree, hf_rtcp_length, tvb, offset, 2, tvb_get_ntohs( tvb, offset ) );
713                                         offset += 2;
714                                         /* Sender Synchronization source, 32 bits */
715                                         proto_tree_add_uint( rtcp_tree, hf_rtcp_ssrc_sender, tvb, offset, 4, tvb_get_ntohl( tvb, offset ) );
716                                         offset += 4;
717
718                                         if ( packet_type == RTCP_SR ) offset = dissect_rtcp_sr( tvb, offset, rtcp_tree, elem_count );
719                                         else offset = dissect_rtcp_rr( tvb, offset, rtcp_tree, elem_count );
720                                         break;
721                                 case RTCP_SDES:
722                                         /* Source count, 5 bits */
723                                         proto_tree_add_uint( rtcp_tree, hf_rtcp_sc, tvb, offset, 1, temp_byte );
724                                         offset++;
725                                         /* Packet type, 8 bits */
726                                         proto_tree_add_item( rtcp_tree, hf_rtcp_pt, tvb, offset, 1, FALSE );
727                                         offset++;
728                                         /* Packet length in 32 bit words MINUS one, 16 bits */
729                                         proto_tree_add_uint( rtcp_tree, hf_rtcp_length, tvb, offset, 2, tvb_get_ntohs( tvb, offset ) );
730                                         offset += 2;
731                                         dissect_rtcp_sdes( tvb, offset, rtcp_tree, elem_count );
732                                         offset += packet_length - 4;
733                                         break;
734                                 case RTCP_BYE:
735                                         /* Source count, 5 bits */
736                                         proto_tree_add_uint( rtcp_tree, hf_rtcp_sc, tvb, offset, 1, temp_byte );
737                                         offset++;
738                                         /* Packet type, 8 bits */
739                                         proto_tree_add_item( rtcp_tree, hf_rtcp_pt, tvb, offset, 1, FALSE );
740                                         offset++;
741                                         /* Packet length in 32 bit words MINUS one, 16 bits */
742                                         proto_tree_add_uint( rtcp_tree, hf_rtcp_length, tvb, offset, 2, tvb_get_ntohs( tvb, offset ) );
743                                         offset += 2;
744                                         offset = dissect_rtcp_bye( tvb, offset, rtcp_tree, elem_count );
745                                         break;
746                                 case RTCP_APP:
747                                         /* Subtype, 5 bits */
748                                         proto_tree_add_uint( rtcp_tree, hf_rtcp_subtype, tvb, offset, 1, elem_count );
749                                         offset++;
750                                         /* Packet type, 8 bits */
751                                         proto_tree_add_item( rtcp_tree, hf_rtcp_pt, tvb, offset, 1, FALSE );
752                                         offset++;
753                                         /* Packet length in 32 bit words MINUS one, 16 bits */
754                                         proto_tree_add_uint( rtcp_tree, hf_rtcp_length, tvb, offset, 2, tvb_get_ntohs( tvb, offset ) );
755                                         offset += 2;
756                                         offset = dissect_rtcp_app( tvb, offset,
757                                             rtcp_tree, padding_set,
758                                             packet_length - 4 );
759                                         break;
760                                 case RTCP_FIR:
761                                         offset = dissect_rtcp_fir( tvb, offset, rtcp_tree );
762                                         break;
763                                 case RTCP_NACK:
764                                         offset = dissect_rtcp_nack( tvb, offset, rtcp_tree );
765                                         break;
766                                 default:
767                                         /*
768                                          * To prevent endless loops in case of an unknown message type
769                                          * increase offset. Some time the while will end :-)
770                                          */
771                                         offset++;
772                                         break;
773                         }
774                 }
775                 /* If the padding bit is set, the last octet of the
776                  * packet contains the length of the padding
777                  * We only have to check for this at the end of the LAST RTCP message
778                  */
779                 if ( padding_set ) {
780                         /* If everything went according to plan offset should now point to the
781                          * first octet of the padding
782                          */
783                         proto_tree_add_item( rtcp_tree, hf_rtcp_padding_data, tvb, offset, tvb_length_remaining( tvb, offset) - 1, FALSE );
784                         offset += tvb_length_remaining( tvb, offset) - 1;
785                         proto_tree_add_item( rtcp_tree, hf_rtcp_padding_count, tvb, offset, 1, FALSE );
786                 }
787         }
788 }
789
790 void
791 proto_register_rtcp(void)
792 {
793         static hf_register_info hf[] =
794         {
795                 {
796                         &hf_rtcp_version,
797                         {
798                                 "Version",
799                                 "rtcp.version",
800                                 FT_UINT8,
801                                 BASE_DEC,
802                                 VALS(rtcp_version_vals),
803                                 0xC0,
804                                 "", HFILL
805                         }
806                 },
807                 {
808                         &hf_rtcp_padding,
809                         {
810                                 "Padding",
811                                 "rtcp.padding",
812                                 FT_BOOLEAN,
813                                 8,
814                                 NULL,
815                                 0x20,
816                                 "", HFILL
817                         }
818                 },
819                 {
820                         &hf_rtcp_rc,
821                         {
822                                 "Reception report count",
823                                 "rtcp.rc",
824                                 FT_UINT8,
825                                 BASE_DEC,
826                                 NULL,
827                                 0x1F,
828                                 "", HFILL
829                         }
830                 },
831                 {
832                         &hf_rtcp_sc,
833                         {
834                                 "Source count",
835                                 "rtcp.sc",
836                                 FT_UINT8,
837                                 BASE_DEC,
838                                 NULL,
839                                 0x1F,
840                                 "", HFILL
841                         }
842                 },
843                 {
844                         &hf_rtcp_pt,
845                         {
846                                 "Packet type",
847                                 "rtcp.pt",
848                                 FT_UINT8,
849                                 BASE_DEC,
850                                 VALS( rtcp_packet_type_vals ),
851                                 0x0,
852                                 "", HFILL
853                         }
854                 },
855                 {
856                         &hf_rtcp_length,
857                         {
858                                 "Length",
859                                 "rtcp.length",
860                                 FT_UINT16,
861                                 BASE_DEC,
862                                 NULL,
863                                 0x0,
864                                 "", HFILL
865                         }
866                 },
867                 {
868                         &hf_rtcp_ssrc_sender,
869                         {
870                                 "Sender SSRC",
871                                 "rtcp.senderssrc",
872                                 FT_UINT32,
873                                 BASE_DEC,
874                                 NULL,
875                                 0x0,
876                                 "", HFILL
877                         }
878                 },
879                 {
880                         &hf_rtcp_ntp,
881                         {
882                                 "NTP timestamp",
883                                 "rtcp.timestamp.ntp",
884                                 FT_STRING,
885                                 BASE_NONE,
886                                 NULL,
887                                 0x0,
888                                 "", HFILL
889                         }
890                 },
891                 {
892                         &hf_rtcp_rtp_timestamp,
893                         {
894                                 "RTP timestamp",
895                                 "rtcp.timestamp.rtp",
896                                 FT_UINT32,
897                                 BASE_DEC,
898                                 NULL,
899                                 0x0,
900                                 "", HFILL
901                         }
902                 },
903                 {
904                         &hf_rtcp_sender_pkt_cnt,
905                         {
906                                 "Sender's packet count",
907                                 "rtcp.sender.packetcount",
908                                 FT_UINT32,
909                                 BASE_DEC,
910                                 NULL,
911                                 0x0,
912                                 "", HFILL
913                         }
914                 },
915                 {
916                         &hf_rtcp_sender_oct_cnt,
917                         {
918                                 "Sender's octet count",
919                                 "rtcp.sender.octetcount",
920                                 FT_UINT32,
921                                 BASE_DEC,
922                                 NULL,
923                                 0x0,
924                                 "", HFILL
925                         }
926                 },
927                 {
928                         &hf_rtcp_ssrc_source,
929                         {
930                                 "Identifier",
931                                 "rtcp.ssrc.identifier",
932                                 FT_UINT32,
933                                 BASE_DEC,
934                                 NULL,
935                                 0x0,
936                                 "", HFILL
937                         }
938                 },
939                 {
940                         &hf_rtcp_ssrc_fraction,
941                         {
942                                 "Fraction lost",
943                                 "rtcp.ssrc.fraction",
944                                 FT_UINT8,
945                                 BASE_DEC,
946                                 NULL,
947                                 0x0,
948                                 "", HFILL
949                         }
950                 },
951                 {
952                         &hf_rtcp_ssrc_cum_nr,
953                         {
954                                 "Cumulative number of packets lost",
955                                 "rtcp.ssrc.cum_nr",
956                                 FT_UINT32,
957                                 BASE_DEC,
958                                 NULL,
959                                 0x0,
960                                 "", HFILL
961                         }
962                 },
963                 {
964                         &hf_rtcp_ssrc_ext_high_seq,
965                         {
966                                 "Extended highest sequence number received",
967                                 "rtcp.ssrc.ext_high",
968                                 FT_UINT32,
969                                 BASE_DEC,
970                                 NULL,
971                                 0x0,
972                                 "", HFILL
973                         }
974                 },
975                 {
976                         &hf_rtcp_ssrc_high_seq,
977                         {
978                                 "Highest sequence number received",
979                                 "rtcp.ssrc.high_seq",
980                                 FT_UINT16,
981                                 BASE_DEC,
982                                 NULL,
983                                 0x0,
984                                 "", HFILL
985                         }
986                 },
987                 {
988                         &hf_rtcp_ssrc_high_cycles,
989                         {
990                                 "Sequence number cycles count",
991                                 "rtcp.ssrc.high_cycles",
992                                 FT_UINT16,
993                                 BASE_DEC,
994                                 NULL,
995                                 0x0,
996                                 "", HFILL
997                         }
998                 },
999                 {
1000                         &hf_rtcp_ssrc_jitter,
1001                         {
1002                                 "Interarrival jitter",
1003                                 "rtcp.ssrc.jitter",
1004                                 FT_UINT32,
1005                                 BASE_DEC,
1006                                 NULL,
1007                                 0x0,
1008                                 "", HFILL
1009                         }
1010                 },
1011                 {
1012                         &hf_rtcp_ssrc_lsr,
1013                         {
1014                                 "Last SR timestamp",
1015                                 "rtcp.ssrc.lsr",
1016                                 FT_UINT32,
1017                                 BASE_DEC,
1018                                 NULL,
1019                                 0x0,
1020                                 "", HFILL
1021                         }
1022                 },
1023                 {
1024                         &hf_rtcp_ssrc_dlsr,
1025                         {
1026                                 "Delay since last SR timestamp",
1027                                 "rtcp.ssrc.dlsr",
1028                                 FT_UINT32,
1029                                 BASE_DEC,
1030                                 NULL,
1031                                 0x0,
1032                                 "", HFILL
1033                         }
1034                 },
1035                 {
1036                         &hf_rtcp_ssrc_csrc,
1037                         {
1038                                 "SSRC / CSRC identifier",
1039                                 "rtcp.sdes.ssrc_csrc",
1040                                 FT_UINT32,
1041                                 BASE_DEC,
1042                                 NULL,
1043                                 0x0,
1044                                 "", HFILL
1045                         }
1046                 },
1047                 {
1048                         &hf_rtcp_ssrc_type,
1049                         {
1050                                 "Type",
1051                                 "rtcp.sdes.type",
1052                                 FT_UINT8,
1053                                 BASE_DEC,
1054                                 VALS( rtcp_sdes_type_vals ),
1055                                 0x0,
1056                                 "", HFILL
1057                         }
1058                 },
1059                 {
1060                         &hf_rtcp_ssrc_length,
1061                         {
1062                                 "Length",
1063                                 "rtcp.sdes.length",
1064                                 FT_UINT32,
1065                                 BASE_DEC,
1066                                 NULL,
1067                                 0x0,
1068                                 "", HFILL
1069                         }
1070                 },
1071                 {
1072                         &hf_rtcp_ssrc_text,
1073                         {
1074                                 "Text",
1075                                 "rtcp.sdes.text",
1076                                 FT_STRING,
1077                                 BASE_NONE,
1078                                 NULL,
1079                                 0x0,
1080                                 "", HFILL
1081                         }
1082                 },
1083                 {
1084                         &hf_rtcp_ssrc_prefix_len,
1085                         {
1086                                 "Prefix length",
1087                                 "rtcp.sdes.prefix.length",
1088                                 FT_UINT8,
1089                                 BASE_DEC,
1090                                 NULL,
1091                                 0x0,
1092                                 "", HFILL
1093                         }
1094                 },
1095                 {
1096                         &hf_rtcp_ssrc_prefix_string,
1097                         {
1098                                 "Prefix string",
1099                                 "rtcp.sdes.prefix.string",
1100                                 FT_STRING,
1101                                 BASE_NONE,
1102                                 NULL,
1103                                 0x0,
1104                                 "", HFILL
1105                         }
1106                 },
1107                 {
1108                         &hf_rtcp_subtype,
1109                         {
1110                                 "Subtype",
1111                                 "rtcp.app.subtype",
1112                                 FT_UINT8,
1113                                 BASE_DEC,
1114                                 NULL,
1115                                 0x0,
1116                                 "", HFILL
1117                         }
1118                 },
1119                 {
1120                         &hf_rtcp_name_ascii,
1121                         {
1122                                 "Name (ASCII)",
1123                                 "rtcp.app.name",
1124                                 FT_STRING,
1125                                 BASE_NONE,
1126                                 NULL,
1127                                 0x0,
1128                                 "", HFILL
1129                         }
1130                 },
1131                 {
1132                         &hf_rtcp_app_data,
1133                         {
1134                                 "Application specific data",
1135                                 "rtcp.app.data",
1136                                 FT_BYTES,
1137                                 BASE_NONE,
1138                                 NULL,
1139                                 0x0,
1140                                 "", HFILL
1141                         }
1142                 },
1143                 {
1144                         &hf_rtcp_fsn,
1145                         {
1146                                 "First sequence number",
1147                                 "rtcp.nack.fsn",
1148                                 FT_UINT16,
1149                                 BASE_DEC,
1150                                 NULL,
1151                                 0x0,
1152                                 "", HFILL
1153                         }
1154                 },
1155                 {
1156                         &hf_rtcp_blp,
1157                         {
1158                                 "Bitmask of following lost packets",
1159                                 "rtcp.nack.blp",
1160                                 FT_UINT16,
1161                                 BASE_DEC,
1162                                 NULL,
1163                                 0x0,
1164                                 "", HFILL
1165                         }
1166                 },
1167                 {
1168                         &hf_rtcp_padding_count,
1169                         {
1170                                 "Padding count",
1171                                 "rtcp.padding.count",
1172                                 FT_UINT8,
1173                                 BASE_DEC,
1174                                 NULL,
1175                                 0x0,
1176                                 "", HFILL
1177                         }
1178                 },
1179                 {
1180                         &hf_rtcp_padding_data,
1181                         {
1182                                 "Padding data",
1183                                 "rtcp.padding.data",
1184                                 FT_BYTES,
1185                                 BASE_NONE,
1186                                 NULL,
1187                                 0x0,
1188                                 "", HFILL
1189                         }
1190                 },
1191 };
1192
1193         static gint *ett[] =
1194         {
1195                 &ett_rtcp,
1196                 &ett_ssrc,
1197                 &ett_ssrc_item,
1198                 &ett_ssrc_ext_high,
1199                 &ett_sdes,
1200                 &ett_sdes_item,
1201         };
1202
1203
1204         proto_rtcp = proto_register_protocol("Real-time Transport Control Protocol",
1205             "RTCP", "rtcp");
1206         proto_register_field_array(proto_rtcp, hf, array_length(hf));
1207         proto_register_subtree_array(ett, array_length(ett));
1208
1209         register_dissector("rtcp", dissect_rtcp, proto_rtcp);
1210
1211 #if 0
1212         register_init_routine( &rtcp_init );
1213 #endif
1214 }
1215
1216 void
1217 proto_reg_handoff_rtcp(void)
1218 {
1219         dissector_handle_t rtcp_handle;
1220
1221         /*
1222          * Register this dissector as one that can be selected by a
1223          * UDP port number.
1224          */
1225         rtcp_handle = find_dissector("rtcp");
1226         dissector_add_handle("udp.port", rtcp_handle);
1227 }