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