2 * Routines for RTSP packet disassembly (RFC 2326)
4 * Jason Lango <jal@netapp.com>
5 * Liberally copied from packet-http.c, by Guy Harris <guy@alum.mit.edu>
7 * $Id: packet-rtsp.c,v 1.39 2001/06/19 04:46:10 guy Exp $
9 * Ethereal - Network traffic analyzer
10 * By Gerald Combs <gerald@zing.org>
11 * Copyright 1998 Gerald Combs
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.
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.
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.
33 #ifdef HAVE_SYS_TYPES_H
34 #include <sys/types.h>
43 #include "packet-rtp.h"
44 #include "packet-rtcp.h"
45 #include "conversation.h"
48 static int proto_rtsp = -1;
49 static gint ett_rtsp = -1;
51 static gint ett_rtspframe = -1;
53 static int hf_rtsp_method = -1;
54 static int hf_rtsp_url = -1;
55 static int hf_rtsp_status = -1;
57 #define TCP_PORT_RTSP 554
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.
64 #define STRLEN_CONST(str) (sizeof (str) - 1)
66 #define RTSP_FRAMEHDR ('$')
69 dissect_rtspinterleaved(tvbuff_t *tvb, int offset, packet_info *pinfo,
72 proto_tree *rtspframe_tree;
75 guint8 rf_start; /* always RTSP_FRAMEHDR */
76 guint8 rf_chan; /* interleaved channel id */
77 guint16 rf_len; /* packet length */
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);
86 if (check_col(pinfo->fd, COL_INFO))
87 col_add_fstr(pinfo->fd, COL_INFO,
88 "Interleaved channel 0x%02x, %u bytes",
93 * We're not building a full protocol tree; all we care
94 * about is setting the column info.
99 ti = proto_tree_add_protocol_format(tree, proto_rtsp, tvb, offset, 4,
100 "RTSP Interleaved Frame, Channel: 0x%02x, %u bytes",
102 rtspframe_tree = proto_item_add_subtree(ti, ett_rtspframe);
104 proto_tree_add_text(rtspframe_tree, tvb, offset, 1,
109 proto_tree_add_text(rtspframe_tree, tvb, offset, 1,
114 proto_tree_add_text(rtspframe_tree, tvb, offset, 2,
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.
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.
128 framelen = tvb_length_remaining(tvb, offset);
129 if (framelen > rf_len)
131 next_tvb = tvb_new_subset(tvb, offset, framelen, rf_len);
132 dissect_data(next_tvb, 0, pinfo, tree);
135 return offset - orig_offset;
138 static void process_rtsp_request(tvbuff_t *tvb, int offset, const u_char *data,
139 int linelen, proto_tree *tree);
141 static void process_rtsp_reply(tvbuff_t *tvb, int offset, const u_char *data,
142 int linelen, proto_tree *tree);
150 static const char *rtsp_methods[] = {
151 "DESCRIBE", "ANNOUNCE", "GET_PARAMETER", "OPTIONS",
152 "PAUSE", "PLAY", "RECORD", "REDIRECT", "SETUP",
153 "SET_PARAMETER", "TEARDOWN"
156 #define RTSP_NMETHODS (sizeof rtsp_methods / sizeof rtsp_methods[0])
158 static dissector_handle_t sdp_handle;
161 is_rtsp_request_or_reply(const u_char *line, int linelen)
165 /* Is this an RTSP reply? */
166 if (linelen >= 5 && strncasecmp("RTSP/", line, 5) == 0) {
174 * Is this an RTSP request?
175 * Check whether the line begins with one of the RTSP request
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)
187 static const char rtsp_content_type[] = "Content-Type:";
190 is_content_sdp(const u_char *line, int linelen)
192 static const char type[] = "application/sdp";
193 size_t typelen = STRLEN_CONST(type);
195 line += STRLEN_CONST(rtsp_content_type);
196 linelen -= STRLEN_CONST(rtsp_content_type);
197 while (linelen > 0 && (*line == ' ' || *line == '\t')) {
202 if (linelen < typelen || strncasecmp(type, line, typelen))
207 if (linelen > 0 && !isspace(*line))
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";
219 rtsp_create_conversation(packet_info *pinfo, const u_char *line_begin,
222 conversation_t *conv;
225 int c_data_port, c_mon_port;
226 int s_data_port, s_mon_port;
228 if (line_len > sizeof(buf) - 1) {
230 * Don't overflow the buffer.
232 line_len = sizeof(buf) - 1;
234 memcpy(buf, line_begin, line_len);
235 buf[line_len] = '\0';
237 tmp = buf + STRLEN_CONST(rtsp_transport);
238 while (*tmp && isspace(*tmp))
240 if (strncasecmp(tmp, rtsp_rtp, strlen(rtsp_rtp)) != 0)
241 return; /* we don't know this transport */
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");
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");
255 if (!c_data_port || !s_data_port)
258 conv = conversation_new(&pinfo->src, &pinfo->dst, PT_UDP, s_data_port,
260 conversation_set_dissector(conv, dissect_rtp);
262 if (!c_mon_port || !s_mon_port)
265 conv = conversation_new(&pinfo->src, &pinfo->dst, PT_UDP, s_mon_port,
267 conversation_set_dissector(conv, dissect_rtcp);
270 static const char rtsp_content_length[] = "Content-Length:";
273 rtsp_get_content_length(const u_char *line_begin, int line_len)
281 if (line_len > sizeof(buf) - 1) {
283 * Don't overflow the buffer.
285 line_len = sizeof(buf) - 1;
287 memcpy(buf, line_begin, line_len);
288 buf[line_len] = '\0';
290 tmp = buf + STRLEN_CONST(rtsp_content_length);
291 while (*tmp && isspace(*tmp))
293 content_length = strtol(tmp, &p, 10);
295 if (up == tmp || (*up != '\0' && !isspace(*up)))
296 return -1; /* not a valid number */
297 return content_length;
301 dissect_rtspmessage(tvbuff_t *tvb, int offset, packet_info *pinfo,
304 proto_tree *rtsp_tree;
305 proto_item *ti = NULL;
308 const u_char *linep, *lineend;
309 int orig_offset, linelen;
311 gboolean is_mime_header;
315 int reported_datalen;
317 orig_offset = offset;
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);
325 if (check_col(pinfo->fd, COL_INFO)) {
327 * Put the first line from the buffer into the summary
328 * if it's an RTSP request or reply (but leave out the
330 * Otherwise, just call it a continuation.
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)) {
338 col_add_str(pinfo->fd, COL_INFO,
339 format_text(line, linelen));
343 col_set_str(pinfo->fd, COL_INFO, "Continuation");
349 * We haven't yet seen a Content-Length header.
354 * Process the packet data, a line at a time.
356 while (tvb_offset_exists(tvb, offset)) {
358 * We haven't yet concluded that this is a MIME header.
360 is_mime_header = FALSE;
363 * Find the end of the line.
365 linelen = tvb_find_line_end(tvb, offset, -1, &next_offset);
368 * Get a buffer that refers to the line.
370 line = tvb_get_ptr(tvb, offset, linelen);
371 lineend = line + linelen;
374 * OK, does it look like an RTSP request or
377 switch (is_rtsp_request_or_reply(line, linelen)) {
380 if (rtsp_tree != NULL)
381 process_rtsp_request(tvb, offset, line, linelen,
386 if (rtsp_tree != NULL)
387 process_rtsp_reply(tvb, offset, line, linelen,
396 * No. Does it look like a blank line (as would
397 * appear at the end of an RTSP request)?
400 goto is_rtsp; /* Yes. */
403 * No. Does it look like a MIME header?
406 while (linep < lineend) {
409 break; /* not printable, not a MIME header */
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
438 * This ends the token; we consider
439 * this to be a MIME header.
441 is_mime_header = TRUE;
447 * LWS (RFC-2616, 4.2); continue the previous
456 * We don't consider this part of an RTSP request or
457 * reply, so we don't display it.
466 proto_tree_add_text(rtsp_tree, tvb, offset,
467 next_offset - offset, "%s",
468 tvb_format_text(tvb, offset, next_offset - offset));
470 if (is_mime_header) {
472 * Process some MIME headers specially.
474 #define MIME_HDR_MATCHES(header) \
475 (linelen > STRLEN_CONST(header) && \
476 strncasecmp(line, (header), STRLEN_CONST(header)) == 0)
478 if (MIME_HDR_MATCHES(rtsp_transport)) {
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.
485 rtsp_create_conversation(pinfo, line, linelen);
486 } else if (MIME_HDR_MATCHES(rtsp_content_type)) {
488 * If the Content-Type: header says this
489 * is SDP, dissect the payload as SDP.
491 if (is_content_sdp(line, linelen))
493 } else if (MIME_HDR_MATCHES(rtsp_content_length)) {
495 * Only the amount specified by the
496 * Content-Length: header should be treated
499 content_length = rtsp_get_content_length(line,
503 offset = next_offset;
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.
511 * If no content length was supplied, the amount of data to be
512 * processed is the amount of data remaining in the frame.
514 datalen = tvb_length_remaining(tvb, offset);
515 if (content_length != -1) {
516 if (datalen > content_length)
517 datalen = content_length;
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.
528 * If we ever handle data that crosses frame
529 * boundaries, we'll need to remember the actual
532 reported_datalen = tvb_reported_length_remaining(tvb, offset);
533 if (content_length > reported_datalen)
534 content_length = reported_datalen;
539 * There's stuff left over; process it.
545 * Fix up the top-level item so that it doesn't
546 * include the SDP stuff.
549 proto_item_set_len(ti, offset);
552 * Now create a tvbuff for the SDP stuff and
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.
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.
566 new_tvb = tvb_new_subset(tvb, offset, datalen,
568 call_dissector(sdp_handle, new_tvb, pinfo, tree);
570 if (tvb_get_guint8(tvb, offset) == RTSP_FRAMEHDR) {
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.
580 proto_tree_add_text(rtsp_tree, tvb, offset,
581 datalen, "Data (%d bytes)", datalen);
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
593 return offset - orig_offset;
597 process_rtsp_request(tvbuff_t *tvb, int offset, const u_char *data,
598 int linelen, proto_tree *tree)
600 const u_char *lineend = data + linelen;
603 const u_char *url_start;
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))
612 if (ii == RTSP_NMETHODS) {
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".
618 g_assert_not_reached();
622 proto_tree_add_string_hidden(tree, hf_rtsp_method, tvb, offset,
623 strlen(rtsp_methods[ii]), rtsp_methods[ii]);
627 while (url < lineend && !isspace(*url))
629 while (url < lineend && isspace(*url))
632 while (url < lineend && !isspace(*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);
643 process_rtsp_reply(tvbuff_t *tvb, int offset, const u_char *data,
644 int linelen, proto_tree *tree)
646 const u_char *lineend = data + linelen;
647 const u_char *status = data;
648 const u_char *status_start;
649 unsigned int status_i;
652 while (status < lineend && !isspace(*status))
654 while (status < lineend && isspace(*status))
656 status_start = status;
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);
666 dissect_rtsp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
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);
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);
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.
689 col_set_writable(pinfo->fd, FALSE);
694 proto_register_rtsp(void)
696 static gint *ett[] = {
700 static hf_register_info hf[] = {
702 { "Method", "rtsp.method", FT_STRING, BASE_NONE, NULL, 0, "", HFILL }},
704 { "URL", "rtsp.url", FT_STRING, BASE_NONE, NULL, 0, "", HFILL }},
706 { "Status", "rtsp.status", FT_UINT32, BASE_DEC, NULL, 0, "", HFILL }},
709 proto_rtsp = proto_register_protocol("Real Time Streaming Protocol",
711 proto_register_field_array(proto_rtsp, hf, array_length(hf));
712 proto_register_subtree_array(ett, array_length(ett));
716 proto_reg_handoff_rtsp(void)
718 dissector_add("tcp.port", TCP_PORT_RTSP, dissect_rtsp, proto_rtsp);
721 * Get a handle for the SDP dissector.
723 sdp_handle = find_dissector("sdp");