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