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