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