Update e-mail address for Ed Meaney.
[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.39 2001/06/19 04:46:10 guy Exp $
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
31 #include "config.h"
32
33 #ifdef HAVE_SYS_TYPES_H
34 #include <sys/types.h>
35 #endif
36
37 #include <string.h>
38 #include <ctype.h>
39 #include <stdlib.h>
40
41 #include <glib.h>
42 #include "packet.h"
43 #include "packet-rtp.h"
44 #include "packet-rtcp.h"
45 #include "conversation.h"
46 #include "strutil.h"
47
48 static int proto_rtsp = -1;
49 static gint ett_rtsp = -1;
50
51 static gint ett_rtspframe = -1;
52
53 static int hf_rtsp_method = -1;
54 static int hf_rtsp_url = -1;
55 static int hf_rtsp_status = -1;
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 static int
69 dissect_rtspinterleaved(tvbuff_t *tvb, int offset, packet_info *pinfo,
70         proto_tree *tree)
71 {
72         proto_tree      *rtspframe_tree;
73         proto_item      *ti;
74         int             orig_offset;
75         guint8          rf_start;       /* always RTSP_FRAMEHDR */
76         guint8          rf_chan;        /* interleaved channel id */
77         guint16         rf_len;         /* packet length */
78         gint            framelen;
79         tvbuff_t        *next_tvb;
80
81         orig_offset = offset;
82         rf_start = tvb_get_guint8(tvb, offset);
83         rf_chan = tvb_get_guint8(tvb, offset+1);
84         rf_len = tvb_get_ntohs(tvb, offset+2);
85
86         if (check_col(pinfo->fd, COL_INFO))
87                 col_add_fstr(pinfo->fd, COL_INFO, 
88                         "Interleaved channel 0x%02x, %u bytes",
89                         rf_chan, rf_len);
90
91         if (tree == NULL) {
92                 /*
93                  * We're not building a full protocol tree; all we care
94                  * about is setting the column info.
95                  */
96                 return -1;
97         }
98
99         ti = proto_tree_add_protocol_format(tree, proto_rtsp, tvb, offset, 4,
100                 "RTSP Interleaved Frame, Channel: 0x%02x, %u bytes",
101                 rf_chan, rf_len);
102         rtspframe_tree = proto_item_add_subtree(ti, ett_rtspframe);
103
104         proto_tree_add_text(rtspframe_tree, tvb, offset, 1,
105                 "Magic: 0x%02x",
106                 rf_start);
107         offset += 1;
108
109         proto_tree_add_text(rtspframe_tree, tvb, offset, 1,
110                 "Channel: 0x%02x",
111                 rf_chan);
112         offset += 1;
113
114         proto_tree_add_text(rtspframe_tree, tvb, offset, 2,
115                 "Length: %u bytes",
116                 rf_len);
117         offset += 2;
118
119         /*
120          * We set the actual length of the tvbuff for the interleaved
121          * stuff to the minimum of what's left in the tvbuff and the
122          * length in the header.
123          *
124          * XXX - what if there's nothing left in the tvbuff?
125          * We'd want a BoundsError exception to be thrown, so
126          * that a Short Frame would be reported.
127          */
128         framelen = tvb_length_remaining(tvb, offset);
129         if (framelen > rf_len)
130                 framelen = rf_len;
131         next_tvb = tvb_new_subset(tvb, offset, framelen, rf_len);
132         dissect_data(next_tvb, 0, pinfo, tree);
133         offset += rf_len;
134
135         return offset - orig_offset;
136 }
137
138 static void process_rtsp_request(tvbuff_t *tvb, int offset, const u_char *data,
139         int linelen, proto_tree *tree);
140
141 static void process_rtsp_reply(tvbuff_t *tvb, int offset, const u_char *data,
142         int linelen, proto_tree *tree);
143
144 typedef enum {
145         RTSP_REQUEST,
146         RTSP_REPLY,
147         NOT_RTSP
148 } rtsp_type_t;
149
150 static const char *rtsp_methods[] = {
151         "DESCRIBE", "ANNOUNCE", "GET_PARAMETER", "OPTIONS",
152         "PAUSE", "PLAY", "RECORD", "REDIRECT", "SETUP",
153         "SET_PARAMETER", "TEARDOWN"
154 };
155
156 #define RTSP_NMETHODS   (sizeof rtsp_methods / sizeof rtsp_methods[0])
157
158 static dissector_handle_t sdp_handle;
159
160 static rtsp_type_t
161 is_rtsp_request_or_reply(const u_char *line, int linelen)
162 {
163         int             ii;
164
165         /* Is this an RTSP reply? */
166         if (linelen >= 5 && strncasecmp("RTSP/", line, 5) == 0) {
167                 /*
168                  * Yes.
169                  */
170                 return RTSP_REPLY;
171         }
172
173         /*
174          * Is this an RTSP request?
175          * Check whether the line begins with one of the RTSP request
176          * methods.
177          */
178         for (ii = 0; ii < RTSP_NMETHODS; ii++) {
179                 size_t len = strlen(rtsp_methods[ii]);
180                 if (linelen >= len &&
181                     strncasecmp(rtsp_methods[ii], line, len) == 0)
182                         return RTSP_REQUEST;
183         }
184         return NOT_RTSP;
185 }
186
187 static const char rtsp_content_type[] = "Content-Type:";
188
189 static int
190 is_content_sdp(const u_char *line, int linelen)
191 {
192         static const char type[] = "application/sdp";
193         size_t          typelen = STRLEN_CONST(type);
194
195         line += STRLEN_CONST(rtsp_content_type);
196         linelen -= STRLEN_CONST(rtsp_content_type);
197         while (linelen > 0 && (*line == ' ' || *line == '\t')) {
198                 line++;
199                 linelen--;
200         }
201
202         if (linelen < typelen || strncasecmp(type, line, typelen))
203                 return FALSE;
204
205         line += typelen;
206         linelen -= typelen;
207         if (linelen > 0 && !isspace(*line))
208                 return FALSE;
209
210         return TRUE;
211 }
212
213 static const char rtsp_transport[] = "Transport:";
214 static const char rtsp_sps[] = "server_port=";
215 static const char rtsp_cps[] = "client_port=";
216 static const char rtsp_rtp[] = "rtp/avp";
217
218 static void
219 rtsp_create_conversation(packet_info *pinfo, const u_char *line_begin,
220     int line_len)
221 {
222         conversation_t  *conv;
223         u_char          buf[256];
224         u_char          *tmp;
225         int             c_data_port, c_mon_port;
226         int             s_data_port, s_mon_port;
227
228         if (line_len > sizeof(buf) - 1) {
229                 /*
230                  * Don't overflow the buffer.
231                  */
232                 line_len = sizeof(buf) - 1;
233         }
234         memcpy(buf, line_begin, line_len);
235         buf[line_len] = '\0';
236
237         tmp = buf + STRLEN_CONST(rtsp_transport);
238         while (*tmp && isspace(*tmp))
239                 tmp++;
240         if (strncasecmp(tmp, rtsp_rtp, strlen(rtsp_rtp)) != 0)
241                 return; /* we don't know this transport */
242
243         c_data_port = c_mon_port = 0;
244         s_data_port = s_mon_port = 0;
245         if ((tmp = strstr(buf, rtsp_sps))) {
246                 tmp += strlen(rtsp_sps);
247                 if (sscanf(tmp, "%u-%u", &s_data_port, &s_mon_port) < 1)
248                         g_warning("rtsp: failed to parse server_port");
249         }
250         if ((tmp = strstr(buf, rtsp_cps))) {
251                 tmp += strlen(rtsp_cps);
252                 if (sscanf(tmp, "%u-%u", &c_data_port, &c_mon_port) < 1)
253                         g_warning("rtsp: failed to parse client_port");
254         }
255         if (!c_data_port || !s_data_port)
256                 return;
257
258         conv = conversation_new(&pinfo->src, &pinfo->dst, PT_UDP, s_data_port,
259                 c_data_port, 0, 0);
260         conversation_set_dissector(conv, dissect_rtp);
261
262         if (!c_mon_port || !s_mon_port)
263                 return;
264
265         conv = conversation_new(&pinfo->src, &pinfo->dst, PT_UDP, s_mon_port,
266                 c_mon_port, 0, 0);
267         conversation_set_dissector(conv, dissect_rtcp);
268 }
269
270 static const char rtsp_content_length[] = "Content-Length:";
271
272 static int
273 rtsp_get_content_length(const u_char *line_begin, int line_len)
274 {
275         u_char          buf[256];
276         u_char          *tmp;
277         long            content_length;
278         char            *p;
279         u_char          *up;
280
281         if (line_len > sizeof(buf) - 1) {
282                 /*
283                  * Don't overflow the buffer.
284                  */
285                 line_len = sizeof(buf) - 1;
286         }
287         memcpy(buf, line_begin, line_len);
288         buf[line_len] = '\0';
289
290         tmp = buf + STRLEN_CONST(rtsp_content_length);
291         while (*tmp && isspace(*tmp))
292                 tmp++;
293         content_length = strtol(tmp, &p, 10);
294         up = p;
295         if (up == tmp || (*up != '\0' && !isspace(*up)))
296                 return -1;      /* not a valid number */
297         return content_length;
298 }
299
300 static int
301 dissect_rtspmessage(tvbuff_t *tvb, int offset, packet_info *pinfo,
302         proto_tree *tree)
303 {
304         proto_tree      *rtsp_tree;
305         proto_item      *ti = NULL;
306         const u_char    *line;
307         gint            next_offset;
308         const u_char    *linep, *lineend;
309         int             orig_offset, linelen;
310         u_char          c;
311         gboolean        is_mime_header;
312         int             is_sdp = FALSE;
313         int             datalen;
314         int             content_length;
315         int             reported_datalen;
316
317         orig_offset = offset;
318         rtsp_tree = NULL;
319         if (tree) {
320                 ti = proto_tree_add_item(tree, proto_rtsp, tvb, offset,
321                         tvb_length_remaining(tvb, offset), FALSE);
322                 rtsp_tree = proto_item_add_subtree(ti, ett_rtsp);
323         }
324
325         if (check_col(pinfo->fd, COL_INFO)) {
326                 /*
327                  * Put the first line from the buffer into the summary
328                  * if it's an RTSP request or reply (but leave out the
329                  * line terminator).
330                  * Otherwise, just call it a continuation.
331                  */
332                 linelen = tvb_find_line_end(tvb, offset, -1, &next_offset);
333                 line = tvb_get_ptr(tvb, offset, linelen);
334                 switch (is_rtsp_request_or_reply(line, linelen)) {
335
336                 case RTSP_REQUEST:
337                 case RTSP_REPLY:
338                         col_add_str(pinfo->fd, COL_INFO,
339                             format_text(line, linelen));
340                         break;
341
342                 default:
343                         col_set_str(pinfo->fd, COL_INFO, "Continuation");
344                         break;
345                 }
346         }
347
348         /*
349          * We haven't yet seen a Content-Length header.
350          */
351         content_length = -1;
352
353         /*
354          * Process the packet data, a line at a time.
355          */
356         while (tvb_offset_exists(tvb, offset)) {
357                 /*
358                  * We haven't yet concluded that this is a MIME header.
359                  */
360                 is_mime_header = FALSE;
361
362                 /*
363                  * Find the end of the line.
364                  */
365                 linelen = tvb_find_line_end(tvb, offset, -1, &next_offset);
366
367                 /*
368                  * Get a buffer that refers to the line.
369                  */
370                 line = tvb_get_ptr(tvb, offset, linelen);
371                 lineend = line + linelen;
372
373                 /*
374                  * OK, does it look like an RTSP request or
375                  * response?
376                  */
377                 switch (is_rtsp_request_or_reply(line, linelen)) {
378
379                 case RTSP_REQUEST:
380                         if (rtsp_tree != NULL)
381                                 process_rtsp_request(tvb, offset, line, linelen,
382                                     rtsp_tree);
383                         goto is_rtsp;
384
385                 case RTSP_REPLY:
386                         if (rtsp_tree != NULL)
387                                 process_rtsp_reply(tvb, offset, line, linelen,
388                                     rtsp_tree);
389                         goto is_rtsp;
390
391                 case NOT_RTSP:
392                         break;
393                 }
394
395                 /*
396                  * No.  Does it look like a blank line (as would
397                  * appear at the end of an RTSP request)?
398                  */
399                 if (linelen == 0)
400                         goto is_rtsp;   /* Yes. */
401
402                 /*
403                  * No.  Does it look like a MIME header?
404                  */
405                 linep = line;
406                 while (linep < lineend) {
407                         c = *linep++;
408                         if (!isprint(c))
409                                 break;  /* not printable, not a MIME header */
410                         switch (c) {
411
412                         case '(':
413                         case ')':
414                         case '<':
415                         case '>':
416                         case '@':
417                         case ',':
418                         case ';':
419                         case '\\':
420                         case '"':
421                         case '/':
422                         case '[':
423                         case ']':
424                         case '?':
425                         case '=':
426                         case '{':
427                         case '}':
428                                 /*
429                                  * It's a tspecial, so it's not
430                                  * part of a token, so it's not
431                                  * a field name for the beginning
432                                  * of a MIME header.
433                                  */
434                                 goto not_rtsp;
435
436                         case ':':
437                                 /*
438                                  * This ends the token; we consider
439                                  * this to be a MIME header.
440                                  */
441                                 is_mime_header = TRUE;
442                                 goto is_rtsp;
443
444                         case ' ':
445                         case '\t':
446                                 /*
447                                  * LWS (RFC-2616, 4.2); continue the previous
448                                  * header.
449                                  */
450                                 goto is_rtsp;
451                         }
452                 }
453
454         not_rtsp:
455                 /*
456                  * We don't consider this part of an RTSP request or
457                  * reply, so we don't display it.
458                  */
459                 break;
460
461         is_rtsp:
462                 /*
463                  * Put this line.
464                  */
465                 if (rtsp_tree) {
466                         proto_tree_add_text(rtsp_tree, tvb, offset,
467                             next_offset - offset, "%s",
468                             tvb_format_text(tvb, offset, next_offset - offset));
469                 }
470                 if (is_mime_header) {
471                         /*
472                          * Process some MIME headers specially.
473                          */
474 #define MIME_HDR_MATCHES(header) \
475         (linelen > STRLEN_CONST(header) && \
476          strncasecmp(line, (header), STRLEN_CONST(header)) == 0)
477
478                         if (MIME_HDR_MATCHES(rtsp_transport)) {
479                                 /*
480                                  * Based on the port numbers specified
481                                  * in the Transport: header, set up
482                                  * a conversation that will be dissected
483                                  * with the appropriate dissector.
484                                  */
485                                 rtsp_create_conversation(pinfo, line, linelen);
486                         } else if (MIME_HDR_MATCHES(rtsp_content_type)) {
487                                 /*
488                                  * If the Content-Type: header says this
489                                  * is SDP, dissect the payload as SDP.
490                                  */
491                                 if (is_content_sdp(line, linelen))
492                                         is_sdp = TRUE;
493                         } else if (MIME_HDR_MATCHES(rtsp_content_length)) {
494                                 /*
495                                  * Only the amount specified by the
496                                  * Content-Length: header should be treated
497                                  * as payload.
498                                  */
499                                 content_length = rtsp_get_content_length(line,
500                                     linelen);
501                         }
502                 }
503                 offset = next_offset;
504         }
505
506         /*
507          * If a content length was supplied, the amount of data to be
508          * processed as RTSP payload is the minimum of the content
509          * length and the amount of data remaining in the frame.
510          *
511          * If no content length was supplied, the amount of data to be
512          * processed is the amount of data remaining in the frame.
513          */
514         datalen = tvb_length_remaining(tvb, offset);
515         if (content_length != -1) {
516                 if (datalen > content_length)
517                         datalen = content_length;
518
519                 /*
520                  * XXX - for now, if the content length is greater
521                  * than the amount of data left in this frame (not
522                  * the amount of *captured* data left in the frame
523                  * minus the current offset, but the amount of *actual*
524                  * data that was reported to be in the frame minus
525                  * the current offset), limit it to the amount
526                  * of data left in this frame.
527                  *
528                  * If we ever handle data that crosses frame
529                  * boundaries, we'll need to remember the actual
530                  * content length.
531                  */
532                 reported_datalen = tvb_reported_length_remaining(tvb, offset);
533                 if (content_length > reported_datalen)
534                         content_length = reported_datalen;
535         }
536
537         if (datalen > 0) {
538                 /*
539                  * There's stuff left over; process it.
540                  */
541                 if (is_sdp) {
542                         tvbuff_t *new_tvb;
543
544                         /*
545                          * Fix up the top-level item so that it doesn't
546                          * include the SDP stuff.
547                          */
548                         if (ti != NULL)
549                                 proto_item_set_len(ti, offset);
550
551                         /*
552                          * Now create a tvbuff for the SDP stuff and
553                          * dissect it.
554                          *
555                          * The amount of data to be processed that's
556                          * available in the tvbuff is "datalen", which
557                          * is the minimum of the amount of data left in
558                          * the tvbuff and any specified content length.
559                          *
560                          * The amount of data to be processed that's in
561                          * this frame, regardless of whether it was
562                          * captured or not, is "content_length",
563                          * which, if no content length was specified,
564                          * is -1, i.e. "to the end of the frame.
565                          */
566                         new_tvb = tvb_new_subset(tvb, offset, datalen,
567                             content_length);
568                         call_dissector(sdp_handle, new_tvb, pinfo, tree);
569                 } else {
570                         if (tvb_get_guint8(tvb, offset) == RTSP_FRAMEHDR) {
571                                 /*
572                                  * This is interleaved stuff; don't
573                                  * treat it as raw data - set "datalen"
574                                  * to 0, so we won't skip the offset
575                                  * past it, which will cause our
576                                  * caller to process that stuff itself.
577                                  */
578                                 datalen = 0;
579                         } else {
580                                 proto_tree_add_text(rtsp_tree, tvb, offset,
581                                     datalen, "Data (%d bytes)", datalen);
582                         }
583                 }
584
585                 /*
586                  * We've processed "datalen" bytes worth of data
587                  * (which may be no data at all); advance the
588                  * offset past whatever data we've processed, so they
589                  * don't process it.
590                  */
591                 offset += datalen;
592         }
593         return offset - orig_offset;
594 }
595
596 static void
597 process_rtsp_request(tvbuff_t *tvb, int offset, const u_char *data,
598         int linelen, proto_tree *tree)
599 {
600         const u_char    *lineend = data + linelen;
601         int             ii;
602         const u_char    *url;
603         const u_char    *url_start;
604         u_char          *tmp_url;
605
606         /* Request Methods */
607         for (ii = 0; ii < RTSP_NMETHODS; ii++) {
608                 size_t len = strlen(rtsp_methods[ii]);
609                 if (linelen >= len && !strncasecmp(rtsp_methods[ii], data, len))
610                         break;
611         }
612         if (ii == RTSP_NMETHODS) {
613                 /*
614                  * We got here because "is_rtsp_request_or_reply()" returned
615                  * RTSP_REQUEST, so we know one of the request methods
616                  * matched, so we "can't get here".
617                  */
618                 g_assert_not_reached();
619         }
620
621         /* Method name */
622         proto_tree_add_string_hidden(tree, hf_rtsp_method, tvb, offset,
623                 strlen(rtsp_methods[ii]), rtsp_methods[ii]);
624
625         /* URL */
626         url = data;
627         while (url < lineend && !isspace(*url))
628                 url++;
629         while (url < lineend && isspace(*url))
630                 url++;
631         url_start = url;
632         while (url < lineend && !isspace(*url))
633                 url++;
634         tmp_url = g_malloc(url - url_start + 1);
635         memcpy(tmp_url, url_start, url - url_start);
636         tmp_url[url - url_start] = 0;
637         proto_tree_add_string_hidden(tree, hf_rtsp_url, tvb,
638                 offset + (url_start - data), url - url_start, tmp_url);
639         g_free(tmp_url);
640 }
641
642 static void
643 process_rtsp_reply(tvbuff_t *tvb, int offset, const u_char *data,
644         int linelen, proto_tree *tree)
645 {
646         const u_char    *lineend = data + linelen;
647         const u_char    *status = data;
648         const u_char    *status_start;
649         unsigned int    status_i;
650
651         /* status code */
652         while (status < lineend && !isspace(*status))
653                 status++;
654         while (status < lineend && isspace(*status))
655                 status++;
656         status_start = status;
657         status_i = 0;
658         while (status < lineend && isdigit(*status))
659                 status_i = status_i * 10 + *status++ - '0';
660         proto_tree_add_uint_hidden(tree, hf_rtsp_status, tvb,
661                 offset + (status_start - data),
662                 status - status_start, status_i);
663 }
664
665 static void
666 dissect_rtsp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
667 {
668         int             offset = 0;
669         int             len;
670
671         if (check_col(pinfo->fd, COL_PROTOCOL))
672                 col_set_str(pinfo->fd, COL_PROTOCOL, "RTSP");
673         if (check_col(pinfo->fd, COL_INFO))
674                 col_clear(pinfo->fd, COL_INFO);
675
676         while (tvb_offset_exists(tvb, offset)) {
677                 len = (tvb_get_guint8(tvb, offset) == RTSP_FRAMEHDR)
678                         ? dissect_rtspinterleaved(tvb, offset, pinfo, tree)
679                         : dissect_rtspmessage(tvb, offset, pinfo, tree);
680                 if (len == -1)
681                         break;
682                 offset += len;
683
684                 /*
685                  * OK, we've set the Protocol and Info columns for the
686                  * first RTSP message; make the columns non-writable,
687                  * so that we don't change it for subsequent RTSP messages.
688                  */
689                 col_set_writable(pinfo->fd, FALSE);
690         }
691 }
692
693 void
694 proto_register_rtsp(void)
695 {
696         static gint *ett[] = {
697                 &ett_rtspframe,
698                 &ett_rtsp,
699         };
700         static hf_register_info hf[] = {
701         { &hf_rtsp_method,
702         { "Method", "rtsp.method", FT_STRING, BASE_NONE, NULL, 0, "", HFILL }},
703         { &hf_rtsp_url,
704         { "URL", "rtsp.url", FT_STRING, BASE_NONE, NULL, 0, "", HFILL }},
705         { &hf_rtsp_status,
706         { "Status", "rtsp.status", FT_UINT32, BASE_DEC, NULL, 0, "", HFILL }},
707         };
708
709         proto_rtsp = proto_register_protocol("Real Time Streaming Protocol",
710                 "RTSP", "rtsp");
711         proto_register_field_array(proto_rtsp, hf, array_length(hf));
712         proto_register_subtree_array(ett, array_length(ett));
713 }
714
715 void
716 proto_reg_handoff_rtsp(void)
717 {
718         dissector_add("tcp.port", TCP_PORT_RTSP, dissect_rtsp, proto_rtsp);
719
720         /*
721          * Get a handle for the SDP dissector.
722          */
723         sdp_handle = find_dissector("sdp");
724 }