From Devin Heitmueller: follow TCP Stream support for showing stream
[obnox/wireshark/wip.git] / packet-rtsp.c
1 /* packet-rtsp.c
2  * Routines for RTSP packet disassembly (RFC 2326)
3  *
4  * Jason Lango <jal@netapp.com>
5  * Liberally copied from packet-http.c, by Guy Harris <guy@alum.mit.edu>
6  *
7  * $Id: packet-rtsp.c,v 1.50 2002/08/28 21:00:30 jmayer Exp $
8  *
9  * Ethereal - Network traffic analyzer
10  * By Gerald Combs <gerald@ethereal.com>
11  * Copyright 1998 Gerald Combs
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
26  */
27
28 #include "config.h"
29
30 #include <string.h>
31 #include <ctype.h>
32 #include <stdlib.h>
33
34 #include <glib.h>
35 #include <epan/packet.h>
36 #include "packet-rtp.h"
37 #include "packet-rtcp.h"
38 #include <epan/conversation.h>
39 #include <epan/strutil.h>
40
41 static int proto_rtsp = -1;
42 static gint ett_rtsp = -1;
43
44 static gint ett_rtspframe = -1;
45
46 static int hf_rtsp_method = -1;
47 static int hf_rtsp_url = -1;
48 static int hf_rtsp_status = -1;
49
50 static dissector_handle_t sdp_handle;
51 static dissector_handle_t rtp_handle;
52 static dissector_handle_t rtcp_handle;
53
54 static GMemChunk *rtsp_vals = NULL;
55 #define rtsp_hash_init_count 20
56
57 #define TCP_PORT_RTSP                   554
58
59 /*
60  * Takes an array of bytes, assumed to contain a null-terminated
61  * string, as an argument, and returns the length of the string -
62  * i.e., the size of the array, minus 1 for the null terminator.
63  */
64 #define STRLEN_CONST(str)       (sizeof (str) - 1)
65
66 #define RTSP_FRAMEHDR   ('$')
67
68 typedef struct {
69         dissector_handle_t              dissector;
70 } rtsp_interleaved_t;
71
72 #define RTSP_MAX_INTERLEAVED            (8)
73
74 /*
75  * Careful about dynamically allocating memory in this structure (say
76  * for dynamically increasing the size of the 'interleaved' array) -
77  * the containing structure is garbage collected and contained
78  * pointers will not be freed.
79  */
80 typedef struct {
81         rtsp_interleaved_t              interleaved[RTSP_MAX_INTERLEAVED];
82 } rtsp_conversation_data_t;
83
84 static int
85 dissect_rtspinterleaved(tvbuff_t *tvb, int offset, packet_info *pinfo,
86         proto_tree *tree)
87 {
88         proto_tree      *rtspframe_tree;
89         proto_item      *ti;
90         int             orig_offset;
91         guint8          rf_start;       /* always RTSP_FRAMEHDR */
92         guint8          rf_chan;        /* interleaved channel id */
93         guint16         rf_len;         /* packet length */
94         gint            framelen;
95         tvbuff_t        *next_tvb;
96         conversation_t  *conv;
97         rtsp_conversation_data_t        *data;
98         dissector_handle_t              dissector;
99
100         orig_offset = offset;
101         rf_start = tvb_get_guint8(tvb, offset);
102         rf_chan = tvb_get_guint8(tvb, offset+1);
103         rf_len = tvb_get_ntohs(tvb, offset+2);
104
105         if (check_col(pinfo->cinfo, COL_INFO))
106                 col_add_fstr(pinfo->cinfo, COL_INFO,
107                         "Interleaved channel 0x%02x, %u bytes",
108                         rf_chan, rf_len);
109
110         if (tree == NULL) {
111                 /*
112                  * We're not building a full protocol tree; all we care
113                  * about is setting the column info.
114                  */
115                 return -1;
116         }
117
118         ti = proto_tree_add_protocol_format(tree, proto_rtsp, tvb, offset, 4,
119                 "RTSP Interleaved Frame, Channel: 0x%02x, %u bytes",
120                 rf_chan, rf_len);
121         rtspframe_tree = proto_item_add_subtree(ti, ett_rtspframe);
122
123         proto_tree_add_text(rtspframe_tree, tvb, offset, 1,
124                 "Magic: 0x%02x",
125                 rf_start);
126         offset += 1;
127
128         proto_tree_add_text(rtspframe_tree, tvb, offset, 1,
129                 "Channel: 0x%02x",
130                 rf_chan);
131         offset += 1;
132
133         proto_tree_add_text(rtspframe_tree, tvb, offset, 2,
134                 "Length: %u bytes",
135                 rf_len);
136         offset += 2;
137
138         /*
139          * We set the actual length of the tvbuff for the interleaved
140          * stuff to the minimum of what's left in the tvbuff and the
141          * length in the header.
142          *
143          * XXX - what if there's nothing left in the tvbuff?
144          * We'd want a BoundsError exception to be thrown, so
145          * that a Short Frame would be reported.
146          */
147         framelen = tvb_length_remaining(tvb, offset);
148         if (framelen > rf_len)
149                 framelen = rf_len;
150         next_tvb = tvb_new_subset(tvb, offset, framelen, rf_len);
151
152         conv = find_conversation(&pinfo->src, &pinfo->dst, pinfo->ptype,
153                 pinfo->srcport, pinfo->destport, 0);
154
155         if (conv &&
156             (data = conversation_get_proto_data(conv, proto_rtsp)) &&
157             rf_chan < RTSP_MAX_INTERLEAVED &&
158             (dissector = data->interleaved[rf_chan].dissector)) {
159                 call_dissector(dissector, next_tvb, pinfo, tree);
160         } else {
161                 proto_tree_add_text(rtspframe_tree, tvb, offset, rf_len,
162                         "Data (%u bytes)", rf_len);
163         }
164
165         offset += rf_len;
166
167         return offset - orig_offset;
168 }
169
170 static void process_rtsp_request(tvbuff_t *tvb, int offset, const guchar *data,
171         size_t linelen, proto_tree *tree);
172
173 static void process_rtsp_reply(tvbuff_t *tvb, int offset, const guchar *data,
174         size_t linelen, proto_tree *tree);
175
176 typedef enum {
177         RTSP_REQUEST,
178         RTSP_REPLY,
179         NOT_RTSP
180 } rtsp_type_t;
181
182 static const char *rtsp_methods[] = {
183         "DESCRIBE", "ANNOUNCE", "GET_PARAMETER", "OPTIONS",
184         "PAUSE", "PLAY", "RECORD", "REDIRECT", "SETUP",
185         "SET_PARAMETER", "TEARDOWN"
186 };
187
188 #define RTSP_NMETHODS   (sizeof rtsp_methods / sizeof rtsp_methods[0])
189
190 static rtsp_type_t
191 is_rtsp_request_or_reply(const guchar *line, size_t linelen)
192 {
193         unsigned        ii;
194
195         /* Is this an RTSP reply? */
196         if (linelen >= 5 && strncasecmp("RTSP/", line, 5) == 0) {
197                 /*
198                  * Yes.
199                  */
200                 return RTSP_REPLY;
201         }
202
203         /*
204          * Is this an RTSP request?
205          * Check whether the line begins with one of the RTSP request
206          * methods.
207          */
208         for (ii = 0; ii < RTSP_NMETHODS; ii++) {
209                 size_t len = strlen(rtsp_methods[ii]);
210                 if (linelen >= len &&
211                     strncasecmp(rtsp_methods[ii], line, len) == 0)
212                         return RTSP_REQUEST;
213         }
214         return NOT_RTSP;
215 }
216
217 static const char rtsp_content_type[] = "Content-Type:";
218
219 static int
220 is_content_sdp(const guchar *line, size_t linelen)
221 {
222         static const char type[] = "application/sdp";
223         size_t          typelen = STRLEN_CONST(type);
224
225         line += STRLEN_CONST(rtsp_content_type);
226         linelen -= STRLEN_CONST(rtsp_content_type);
227         while (linelen > 0 && (*line == ' ' || *line == '\t')) {
228                 line++;
229                 linelen--;
230         }
231
232         if (linelen < typelen || strncasecmp(type, line, typelen))
233                 return FALSE;
234
235         line += typelen;
236         linelen -= typelen;
237         if (linelen > 0 && !isspace(*line))
238                 return FALSE;
239
240         return TRUE;
241 }
242
243 static const char rtsp_transport[] = "Transport:";
244 static const char rtsp_sps[] = "server_port=";
245 static const char rtsp_cps[] = "client_port=";
246 static const char rtsp_rtp[] = "rtp/";
247 static const char rtsp_inter[] = "interleaved=";
248
249 static void
250 rtsp_create_conversation(packet_info *pinfo, const guchar *line_begin,
251         size_t line_len)
252 {
253         conversation_t  *conv;
254         guchar          buf[256];
255         guchar          *tmp;
256         guint           c_data_port, c_mon_port;
257         guint           s_data_port, s_mon_port;
258         address         null_addr;
259
260         if (line_len > sizeof(buf) - 1) {
261                 /*
262                  * Don't overflow the buffer.
263                  */
264                 line_len = sizeof(buf) - 1;
265         }
266         memcpy(buf, line_begin, line_len);
267         buf[line_len] = '\0';
268
269         tmp = buf + STRLEN_CONST(rtsp_transport);
270         while (*tmp && isspace(*tmp))
271                 tmp++;
272         if (strncasecmp(tmp, rtsp_rtp, strlen(rtsp_rtp)) != 0) {
273                 g_warning("Frame %u: rtsp: unknown transport", pinfo->fd->num);
274                 return;
275         }
276
277         c_data_port = c_mon_port = 0;
278         s_data_port = s_mon_port = 0;
279         if ((tmp = strstr(buf, rtsp_sps))) {
280                 tmp += strlen(rtsp_sps);
281                 if (sscanf(tmp, "%u-%u", &s_data_port, &s_mon_port) < 1) {
282                         g_warning("Frame %u: rtsp: bad server_port",
283                                 pinfo->fd->num);
284                         return;
285                 }
286         }
287         if ((tmp = strstr(buf, rtsp_cps))) {
288                 tmp += strlen(rtsp_cps);
289                 if (sscanf(tmp, "%u-%u", &c_data_port, &c_mon_port) < 1) {
290                         g_warning("Frame %u: rtsp: bad client_port",
291                                 pinfo->fd->num);
292                         return;
293                 }
294         }
295         if (!c_data_port) {
296                 rtsp_conversation_data_t        *data;
297                 guint                           s_data_chan, s_mon_chan;
298                 int                             i;
299
300                 /*
301                  * Deal with RTSP TCP-interleaved conversations.
302                  */
303                 if ((tmp = strstr(buf, rtsp_inter)) == NULL) {
304                         /*
305                          * No interleaved or server_port - probably a
306                          * SETUP request, rather than reply.
307                          */
308                         return;
309                 }
310                 tmp += strlen(rtsp_inter);
311                 i = sscanf(tmp, "%u-%u", &s_data_chan, &s_mon_chan);
312                 if (i < 1) {
313                         g_warning("Frame %u: rtsp: bad interleaved",
314                                 pinfo->fd->num);
315                         return;
316                 }
317                 conv = find_conversation(&pinfo->src, &pinfo->dst, pinfo->ptype,
318                         pinfo->srcport, pinfo->destport, 0);
319                 if (!conv) {
320                         conv = conversation_new(&pinfo->src, &pinfo->dst,
321                                 pinfo->ptype, pinfo->srcport, pinfo->destport,
322                                 0);
323                 }
324                 data = conversation_get_proto_data(conv, proto_rtsp);
325                 if (!data) {
326                         data = g_mem_chunk_alloc(rtsp_vals);
327                         conversation_add_proto_data(conv, proto_rtsp, data);
328                 }
329                 if (s_data_chan < RTSP_MAX_INTERLEAVED) {
330                         data->interleaved[s_data_chan].dissector =
331                                 rtp_handle;
332                 }
333                 if (i > 1 && s_mon_chan < RTSP_MAX_INTERLEAVED) {
334                         data->interleaved[s_mon_chan].dissector =
335                                 rtcp_handle;
336                 }
337                 return;
338         }
339
340         /*
341          * We only want to match on the destination address, not the
342          * source address, because the server might send back a packet
343          * from an address other than the address to which its client
344          * sent the packet, so we construct a conversation with no
345          * second address.
346          */
347         SET_ADDRESS(&null_addr, pinfo->src.type, 0, NULL);
348
349         conv = conversation_new(&pinfo->dst, &null_addr, PT_UDP, c_data_port,
350                 s_data_port, NO_ADDR2 | (!s_data_port ? NO_PORT2 : 0));
351         conversation_set_dissector(conv, rtp_handle);
352
353         if (!c_mon_port)
354                 return;
355
356         conv = conversation_new(&pinfo->dst, &null_addr, PT_UDP, c_mon_port,
357                 s_mon_port, NO_ADDR2 | (!s_mon_port ? NO_PORT2 : 0));
358         conversation_set_dissector(conv, rtcp_handle);
359 }
360
361 static const char rtsp_content_length[] = "Content-Length:";
362
363 static int
364 rtsp_get_content_length(const guchar *line_begin, size_t line_len)
365 {
366         guchar          buf[256];
367         guchar          *tmp;
368         long            content_length;
369         char            *p;
370         guchar          *up;
371
372         if (line_len > sizeof(buf) - 1) {
373                 /*
374                  * Don't overflow the buffer.
375                  */
376                 line_len = sizeof(buf) - 1;
377         }
378         memcpy(buf, line_begin, line_len);
379         buf[line_len] = '\0';
380
381         tmp = buf + STRLEN_CONST(rtsp_content_length);
382         while (*tmp && isspace(*tmp))
383                 tmp++;
384         content_length = strtol(tmp, &p, 10);
385         up = p;
386         if (up == tmp || (*up != '\0' && !isspace(*up)))
387                 return -1;      /* not a valid number */
388         return content_length;
389 }
390
391 static int
392 dissect_rtspmessage(tvbuff_t *tvb, int offset, packet_info *pinfo,
393         proto_tree *tree)
394 {
395         proto_tree      *rtsp_tree;
396         proto_item      *ti = NULL;
397         const guchar    *line;
398         gint            next_offset;
399         const guchar    *linep, *lineend;
400         int             orig_offset;
401         size_t          linelen;
402         guchar          c;
403         gboolean        is_mime_header;
404         int             is_sdp = FALSE;
405         int             datalen;
406         int             content_length;
407         int             reported_datalen;
408
409         orig_offset = offset;
410         rtsp_tree = NULL;
411         if (tree) {
412                 ti = proto_tree_add_item(tree, proto_rtsp, tvb, offset, -1,
413                     FALSE);
414                 rtsp_tree = proto_item_add_subtree(ti, ett_rtsp);
415         }
416
417         if (check_col(pinfo->cinfo, COL_INFO)) {
418                 /*
419                  * Put the first line from the buffer into the summary
420                  * if it's an RTSP request or reply (but leave out the
421                  * line terminator).
422                  * Otherwise, just call it a continuation.
423                  */
424                 linelen = tvb_find_line_end(tvb, offset, -1, &next_offset,
425                     FALSE);
426                 line = tvb_get_ptr(tvb, offset, linelen);
427                 switch (is_rtsp_request_or_reply(line, linelen)) {
428
429                 case RTSP_REQUEST:
430                 case RTSP_REPLY:
431                         col_add_str(pinfo->cinfo, COL_INFO,
432                             format_text(line, linelen));
433                         break;
434
435                 default:
436                         col_set_str(pinfo->cinfo, COL_INFO, "Continuation");
437                         break;
438                 }
439         }
440
441         /*
442          * We haven't yet seen a Content-Length header.
443          */
444         content_length = -1;
445
446         /*
447          * Process the packet data, a line at a time.
448          */
449         while (tvb_offset_exists(tvb, offset)) {
450                 /*
451                  * We haven't yet concluded that this is a MIME header.
452                  */
453                 is_mime_header = FALSE;
454
455                 /*
456                  * Find the end of the line.
457                  */
458                 linelen = tvb_find_line_end(tvb, offset, -1, &next_offset,
459                     FALSE);
460
461                 /*
462                  * Get a buffer that refers to the line.
463                  */
464                 line = tvb_get_ptr(tvb, offset, linelen);
465                 lineend = line + linelen;
466
467                 /*
468                  * OK, does it look like an RTSP request or
469                  * response?
470                  */
471                 switch (is_rtsp_request_or_reply(line, linelen)) {
472
473                 case RTSP_REQUEST:
474                         if (rtsp_tree != NULL)
475                                 process_rtsp_request(tvb, offset, line, linelen,
476                                     rtsp_tree);
477                         goto is_rtsp;
478
479                 case RTSP_REPLY:
480                         if (rtsp_tree != NULL)
481                                 process_rtsp_reply(tvb, offset, line, linelen,
482                                     rtsp_tree);
483                         goto is_rtsp;
484
485                 case NOT_RTSP:
486                         break;
487                 }
488
489                 /*
490                  * No.  Does it look like a blank line (as would
491                  * appear at the end of an RTSP request)?
492                  */
493                 if (linelen == 0)
494                         goto is_rtsp;   /* Yes. */
495
496                 /*
497                  * No.  Does it look like a MIME header?
498                  */
499                 linep = line;
500                 while (linep < lineend) {
501                         c = *linep++;
502                         if (!isprint(c))
503                                 break;  /* not printable, not a MIME header */
504                         switch (c) {
505
506                         case '(':
507                         case ')':
508                         case '<':
509                         case '>':
510                         case '@':
511                         case ',':
512                         case ';':
513                         case '\\':
514                         case '"':
515                         case '/':
516                         case '[':
517                         case ']':
518                         case '?':
519                         case '=':
520                         case '{':
521                         case '}':
522                                 /*
523                                  * It's a tspecial, so it's not
524                                  * part of a token, so it's not
525                                  * a field name for the beginning
526                                  * of a MIME header.
527                                  */
528                                 goto not_rtsp;
529
530                         case ':':
531                                 /*
532                                  * This ends the token; we consider
533                                  * this to be a MIME header.
534                                  */
535                                 is_mime_header = TRUE;
536                                 goto is_rtsp;
537
538                         case ' ':
539                         case '\t':
540                                 /*
541                                  * LWS (RFC-2616, 4.2); continue the previous
542                                  * header.
543                                  */
544                                 goto is_rtsp;
545                         }
546                 }
547
548         not_rtsp:
549                 /*
550                  * We don't consider this part of an RTSP request or
551                  * reply, so we don't display it.
552                  */
553                 break;
554
555         is_rtsp:
556                 /*
557                  * Put this line.
558                  */
559                 if (rtsp_tree) {
560                         proto_tree_add_text(rtsp_tree, tvb, offset,
561                             next_offset - offset, "%s",
562                             tvb_format_text(tvb, offset, next_offset - offset));
563                 }
564                 if (is_mime_header) {
565                         /*
566                          * Process some MIME headers specially.
567                          */
568 #define MIME_HDR_MATCHES(header) \
569         (linelen > STRLEN_CONST(header) && \
570          strncasecmp(line, (header), STRLEN_CONST(header)) == 0)
571
572                         if (MIME_HDR_MATCHES(rtsp_transport)) {
573                                 /*
574                                  * Based on the port numbers specified
575                                  * in the Transport: header, set up
576                                  * a conversation that will be dissected
577                                  * with the appropriate dissector.
578                                  */
579                                 rtsp_create_conversation(pinfo, line, linelen);
580                         } else if (MIME_HDR_MATCHES(rtsp_content_type)) {
581                                 /*
582                                  * If the Content-Type: header says this
583                                  * is SDP, dissect the payload as SDP.
584                                  */
585                                 if (is_content_sdp(line, linelen))
586                                         is_sdp = TRUE;
587                         } else if (MIME_HDR_MATCHES(rtsp_content_length)) {
588                                 /*
589                                  * Only the amount specified by the
590                                  * Content-Length: header should be treated
591                                  * as payload.
592                                  */
593                                 content_length = rtsp_get_content_length(line,
594                                     linelen);
595                         }
596                 }
597                 offset = next_offset;
598         }
599
600         /*
601          * If a content length was supplied, the amount of data to be
602          * processed as RTSP payload is the minimum of the content
603          * length and the amount of data remaining in the frame.
604          *
605          * If no content length was supplied, the amount of data to be
606          * processed is the amount of data remaining in the frame.
607          */
608         datalen = tvb_length_remaining(tvb, offset);
609         if (content_length != -1) {
610                 if (datalen > content_length)
611                         datalen = content_length;
612
613                 /*
614                  * XXX - for now, if the content length is greater
615                  * than the amount of data left in this frame (not
616                  * the amount of *captured* data left in the frame
617                  * minus the current offset, but the amount of *actual*
618                  * data that was reported to be in the frame minus
619                  * the current offset), limit it to the amount
620                  * of data left in this frame.
621                  *
622                  * If we ever handle data that crosses frame
623                  * boundaries, we'll need to remember the actual
624                  * content length.
625                  */
626                 reported_datalen = tvb_reported_length_remaining(tvb, offset);
627                 if (content_length > reported_datalen)
628                         content_length = reported_datalen;
629         }
630
631         if (datalen > 0) {
632                 /*
633                  * There's stuff left over; process it.
634                  */
635                 if (is_sdp) {
636                         tvbuff_t *new_tvb;
637
638                         /*
639                          * Fix up the top-level item so that it doesn't
640                          * include the SDP stuff.
641                          */
642                         if (ti != NULL)
643                                 proto_item_set_len(ti, offset);
644
645                         /*
646                          * Now create a tvbuff for the SDP stuff and
647                          * dissect it.
648                          *
649                          * The amount of data to be processed that's
650                          * available in the tvbuff is "datalen", which
651                          * is the minimum of the amount of data left in
652                          * the tvbuff and any specified content length.
653                          *
654                          * The amount of data to be processed that's in
655                          * this frame, regardless of whether it was
656                          * captured or not, is "content_length",
657                          * which, if no content length was specified,
658                          * is -1, i.e. "to the end of the frame.
659                          */
660                         new_tvb = tvb_new_subset(tvb, offset, datalen,
661                             content_length);
662                         call_dissector(sdp_handle, new_tvb, pinfo, tree);
663                 } else {
664                         if (tvb_get_guint8(tvb, offset) == RTSP_FRAMEHDR) {
665                                 /*
666                                  * This is interleaved stuff; don't
667                                  * treat it as raw data - set "datalen"
668                                  * to 0, so we won't skip the offset
669                                  * past it, which will cause our
670                                  * caller to process that stuff itself.
671                                  */
672                                 datalen = 0;
673                         } else {
674                                 proto_tree_add_text(rtsp_tree, tvb, offset,
675                                     datalen, "Data (%d bytes)", datalen);
676                         }
677                 }
678
679                 /*
680                  * We've processed "datalen" bytes worth of data
681                  * (which may be no data at all); advance the
682                  * offset past whatever data we've processed, so they
683                  * don't process it.
684                  */
685                 offset += datalen;
686         }
687         return offset - orig_offset;
688 }
689
690 static void
691 process_rtsp_request(tvbuff_t *tvb, int offset, const guchar *data,
692         size_t linelen, proto_tree *tree)
693 {
694         const guchar    *lineend = data + linelen;
695         unsigned        ii;
696         const guchar    *url;
697         const guchar    *url_start;
698         guchar          *tmp_url;
699
700         /* Request Methods */
701         for (ii = 0; ii < RTSP_NMETHODS; ii++) {
702                 size_t len = strlen(rtsp_methods[ii]);
703                 if (linelen >= len && !strncasecmp(rtsp_methods[ii], data, len))
704                         break;
705         }
706         if (ii == RTSP_NMETHODS) {
707                 /*
708                  * We got here because "is_rtsp_request_or_reply()" returned
709                  * RTSP_REQUEST, so we know one of the request methods
710                  * matched, so we "can't get here".
711                  */
712                 g_assert_not_reached();
713         }
714
715         /* Method name */
716         proto_tree_add_string_hidden(tree, hf_rtsp_method, tvb, offset,
717                 strlen(rtsp_methods[ii]), rtsp_methods[ii]);
718
719         /* URL */
720         url = data;
721         while (url < lineend && !isspace(*url))
722                 url++;
723         while (url < lineend && isspace(*url))
724                 url++;
725         url_start = url;
726         while (url < lineend && !isspace(*url))
727                 url++;
728         tmp_url = g_malloc(url - url_start + 1);
729         memcpy(tmp_url, url_start, url - url_start);
730         tmp_url[url - url_start] = 0;
731         proto_tree_add_string_hidden(tree, hf_rtsp_url, tvb,
732                 offset + (url_start - data), url - url_start, tmp_url);
733         g_free(tmp_url);
734 }
735
736 static void
737 process_rtsp_reply(tvbuff_t *tvb, int offset, const guchar *data,
738         size_t linelen, proto_tree *tree)
739 {
740         const guchar    *lineend = data + linelen;
741         const guchar    *status = data;
742         const guchar    *status_start;
743         unsigned int    status_i;
744
745         /* status code */
746         while (status < lineend && !isspace(*status))
747                 status++;
748         while (status < lineend && isspace(*status))
749                 status++;
750         status_start = status;
751         status_i = 0;
752         while (status < lineend && isdigit(*status))
753                 status_i = status_i * 10 + *status++ - '0';
754         proto_tree_add_uint_hidden(tree, hf_rtsp_status, tvb,
755                 offset + (status_start - data),
756                 status - status_start, status_i);
757 }
758
759 static void
760 dissect_rtsp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
761 {
762         int             offset = 0;
763         int             len;
764
765         if (check_col(pinfo->cinfo, COL_PROTOCOL))
766                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "RTSP");
767         if (check_col(pinfo->cinfo, COL_INFO))
768                 col_clear(pinfo->cinfo, COL_INFO);
769
770         while (tvb_offset_exists(tvb, offset)) {
771                 len = (tvb_get_guint8(tvb, offset) == RTSP_FRAMEHDR)
772                         ? dissect_rtspinterleaved(tvb, offset, pinfo, tree)
773                         : dissect_rtspmessage(tvb, offset, pinfo, tree);
774                 if (len == -1)
775                         break;
776                 offset += len;
777
778                 /*
779                  * OK, we've set the Protocol and Info columns for the
780                  * first RTSP message; make the columns non-writable,
781                  * so that we don't change it for subsequent RTSP messages.
782                  */
783                 col_set_writable(pinfo->cinfo, FALSE);
784         }
785 }
786
787 static void
788 rtsp_init(void)
789 {
790 /* Routine to initialize rtsp protocol before each capture or filter pass. */
791 /* Release any memory if needed.  Then setup the memory chunks.         */
792
793         if (rtsp_vals)
794                 g_mem_chunk_destroy(rtsp_vals);
795
796         rtsp_vals = g_mem_chunk_new("rtsp_vals",
797                 sizeof(rtsp_conversation_data_t),
798                 rtsp_hash_init_count * sizeof(rtsp_conversation_data_t),
799                 G_ALLOC_AND_FREE);
800 }
801
802 void
803 proto_register_rtsp(void)
804 {
805         static gint *ett[] = {
806                 &ett_rtspframe,
807                 &ett_rtsp,
808         };
809         static hf_register_info hf[] = {
810         { &hf_rtsp_method,
811         { "Method", "rtsp.method", FT_STRING, BASE_NONE, NULL, 0, "", HFILL }},
812         { &hf_rtsp_url,
813         { "URL", "rtsp.url", FT_STRING, BASE_NONE, NULL, 0, "", HFILL }},
814         { &hf_rtsp_status,
815         { "Status", "rtsp.status", FT_UINT32, BASE_DEC, NULL, 0, "", HFILL }},
816         };
817
818         proto_rtsp = proto_register_protocol("Real Time Streaming Protocol",
819                 "RTSP", "rtsp");
820         proto_register_field_array(proto_rtsp, hf, array_length(hf));
821         proto_register_subtree_array(ett, array_length(ett));
822
823         register_init_routine(rtsp_init);       /* register re-init routine */
824 }
825
826 void
827 proto_reg_handoff_rtsp(void)
828 {
829         dissector_handle_t rtsp_handle;
830
831         rtsp_handle = create_dissector_handle(dissect_rtsp, proto_rtsp);
832         dissector_add("tcp.port", TCP_PORT_RTSP, rtsp_handle);
833
834         sdp_handle = find_dissector("sdp");
835         rtp_handle = find_dissector("rtp");
836         rtcp_handle = find_dissector("rtcp");
837 }