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