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.30 2000/12/27 12:48:25 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 int hf_rtsp_method = -1;
52 static int hf_rtsp_url = -1;
53 static int hf_rtsp_status = -1;
55 #define TCP_PORT_RTSP 554
58 * Takes an array of bytes, assumed to contain a null-terminated
59 * string, as an argument, and returns the length of the string -
60 * i.e., the size of the array, minus 1 for the null terminator.
62 #define STRLEN_CONST(str) (sizeof (str) - 1)
64 static void process_rtsp_request(tvbuff_t *tvb, int offset, const u_char *data,
65 int linelen, proto_tree *tree);
67 static void process_rtsp_reply(tvbuff_t *tvb, int offset, const u_char *data,
68 int linelen, proto_tree *tree);
76 static const char *rtsp_methods[] = {
77 "DESCRIBE", "ANNOUNCE", "GET_PARAMETER", "OPTIONS",
78 "PAUSE", "PLAY", "RECORD", "REDIRECT", "SETUP",
79 "SET_PARAMETER", "TEARDOWN"
82 #define RTSP_NMETHODS (sizeof rtsp_methods / sizeof rtsp_methods[0])
84 static dissector_handle_t sdp_handle;
87 is_rtsp_request_or_reply(const u_char *line, int linelen)
91 /* Is this an RTSP reply? */
92 if (linelen >= 5 && strncasecmp("RTSP/", line, 5) == 0) {
100 * Is this an RTSP request?
101 * Check whether the line begins with one of the RTSP request
104 for (ii = 0; ii < RTSP_NMETHODS; ii++) {
105 size_t len = strlen(rtsp_methods[ii]);
106 if (linelen >= len &&
107 strncasecmp(rtsp_methods[ii], line, len) == 0)
113 static const char rtsp_content_type[] = "Content-Type:";
116 is_content_sdp(const u_char *line, int linelen)
118 static const char type[] = "application/sdp";
119 size_t typelen = STRLEN_CONST(type);
121 line += STRLEN_CONST(rtsp_content_type);
122 linelen -= STRLEN_CONST(rtsp_content_type);
123 while (linelen > 0 && (*line == ' ' || *line == '\t')) {
128 if (linelen < typelen || strncasecmp(type, line, typelen))
133 if (linelen > 0 && !isspace(*line))
139 static const char rtsp_transport[] = "Transport:";
140 static const char rtsp_sps[] = "server_port=";
141 static const char rtsp_cps[] = "client_port=";
142 static const char rtsp_rtp[] = "rtp/avp";
145 rtsp_create_conversation(const u_char *line_begin, int line_len)
147 conversation_t *conv;
150 int c_data_port, c_mon_port;
151 int s_data_port, s_mon_port;
153 if (line_len > sizeof(buf) - 1) {
155 * Don't overflow the buffer.
157 line_len = sizeof(buf) - 1;
159 memcpy(buf, line_begin, line_len);
160 buf[line_len] = '\0';
162 tmp = buf + STRLEN_CONST(rtsp_transport);
163 while (*tmp && isspace(*tmp))
165 if (strncasecmp(tmp, rtsp_rtp, strlen(rtsp_rtp)) != 0)
166 return; /* we don't know this transport */
168 c_data_port = c_mon_port = 0;
169 s_data_port = s_mon_port = 0;
170 if ((tmp = strstr(buf, rtsp_sps))) {
171 tmp += strlen(rtsp_sps);
172 if (sscanf(tmp, "%u-%u", &s_data_port, &s_mon_port) < 1)
173 g_warning("rtsp: failed to parse server_port");
175 if ((tmp = strstr(buf, rtsp_cps))) {
176 tmp += strlen(rtsp_cps);
177 if (sscanf(tmp, "%u-%u", &c_data_port, &c_mon_port) < 1)
178 g_warning("rtsp: failed to parse client_port");
180 if (!c_data_port || !s_data_port)
183 conv = conversation_new(&pi.src, &pi.dst, PT_UDP, s_data_port,
185 conversation_set_dissector(conv, dissect_rtp);
187 if (!c_mon_port || !s_mon_port)
190 conv = conversation_new(&pi.src, &pi.dst, PT_UDP, s_mon_port,
192 conversation_set_dissector(conv, dissect_rtcp);
195 static const char rtsp_content_length[] = "Content-Length:";
198 rtsp_get_content_length(const u_char *line_begin, int line_len)
206 if (line_len > sizeof(buf) - 1) {
208 * Don't overflow the buffer.
210 line_len = sizeof(buf) - 1;
212 memcpy(buf, line_begin, line_len);
213 buf[line_len] = '\0';
215 tmp = buf + STRLEN_CONST(rtsp_content_length);
216 while (*tmp && isspace(*tmp))
218 content_length = strtol(tmp, &p, 10);
220 if (up == tmp || (*up != '\0' && !isspace(*up)))
221 return -1; /* not a valid number */
222 return content_length;
226 dissect_rtspmessage(tvbuff_t *tvb, int offset, packet_info *pinfo,
229 proto_tree *rtsp_tree;
230 proto_item *ti = NULL;
233 const u_char *linep, *lineend;
234 int orig_offset, linelen;
236 gboolean is_mime_header;
240 int reported_datalen;
242 orig_offset = offset;
245 ti = proto_tree_add_item(tree, proto_rtsp, tvb, offset,
246 tvb_length_remaining(tvb, offset), FALSE);
247 rtsp_tree = proto_item_add_subtree(ti, ett_rtsp);
250 if (check_col(pinfo->fd, COL_INFO)) {
252 * Put the first line from the buffer into the summary
253 * if it's an RTSP request or reply (but leave out the
255 * Otherwise, just call it a continuation.
257 linelen = tvb_find_line_end(tvb, offset, -1, &next_offset);
258 line = tvb_get_ptr(tvb, offset, linelen);
259 switch (is_rtsp_request_or_reply(line, linelen)) {
263 col_add_str(pinfo->fd, COL_INFO,
264 format_text(line, linelen));
268 col_set_str(pinfo->fd, COL_INFO, "Continuation");
274 * We haven't yet seen a Content-Length header.
279 * Process the packet data, a line at a time.
281 while (tvb_offset_exists(tvb, offset)) {
283 * We haven't yet concluded that this is a MIME header.
285 is_mime_header = FALSE;
288 * Find the end of the line.
290 linelen = tvb_find_line_end(tvb, offset, -1, &next_offset);
293 * Get a buffer that refers to the line.
295 line = tvb_get_ptr(tvb, offset, linelen);
296 lineend = line + linelen;
299 * OK, does it look like an RTSP request or
302 switch (is_rtsp_request_or_reply(line, linelen)) {
305 if (rtsp_tree != NULL)
306 process_rtsp_request(tvb, offset, line, linelen,
311 if (rtsp_tree != NULL)
312 process_rtsp_reply(tvb, offset, line, linelen,
321 * No. Does it look like a blank line (as would
322 * appear at the end of an RTSP request)?
325 goto is_rtsp; /* Yes. */
328 * No. Does it look like a MIME header?
331 while (linep < lineend) {
334 break; /* not printable, not a MIME header */
354 * It's a tspecial, so it's not
355 * part of a token, so it's not
356 * a field name for the beginning
363 * This ends the token; we consider
364 * this to be a MIME header.
366 is_mime_header = TRUE;
373 * We don't consider this part of an RTSP request or
374 * reply, so we don't display it.
383 proto_tree_add_text(rtsp_tree, tvb, offset,
384 next_offset - offset, "%s",
385 tvb_format_text(tvb, offset, next_offset - offset));
387 if (is_mime_header) {
389 * Process some MIME headers specially.
391 #define MIME_HDR_MATCHES(header) \
392 (linelen > STRLEN_CONST(header) && \
393 strncasecmp(line, (header), STRLEN_CONST(header)) == 0)
395 if (MIME_HDR_MATCHES(rtsp_transport)) {
397 * Based on the port numbers specified
398 * in the Transport: header, set up
399 * a conversation that will be dissected
400 * with the appropriate dissector.
402 rtsp_create_conversation(line, linelen);
403 } else if (MIME_HDR_MATCHES(rtsp_content_type)) {
405 * If the Content-Type: header says this
406 * is SDP, dissect the payload as SDP.
408 if (is_content_sdp(line, linelen))
410 } else if (MIME_HDR_MATCHES(rtsp_content_length)) {
412 * Only the amount specified by the
413 * Content-Length: header should be treated
416 content_length = rtsp_get_content_length(line,
420 offset = next_offset;
424 * If a content length was supplied, the amount of data to be
425 * processed as RTSP payload is the minimum of the content
426 * length and the amount of data remaining in the frame.
428 * If no content length was supplied, the amount of data to be
429 * processed is the amount of data remaining in the frame.
431 datalen = tvb_length_remaining(tvb, offset);
432 if (content_length != -1) {
433 if (datalen > content_length)
434 datalen = content_length;
437 * XXX - for now, if the content length is greater
438 * than the amount of data left in this frame (not
439 * the amount of *captured* data left in the frame
440 * minus the current offset, but the amount of *actual*
441 * data that was reported to be in the frame minus
442 * the current offset), limit it to the amount
443 * of data left in this frame.
445 * If we ever handle data that crosses frame
446 * boundaries, we'll need to remember the actual
449 reported_datalen = tvb_reported_length_remaining(tvb, offset);
450 if (content_length > reported_datalen)
451 content_length = reported_datalen;
456 * There's stuff left over; process it.
462 * Fix up the top-level item so that it doesn't
463 * include the SDP stuff.
466 proto_item_set_len(ti, offset);
469 * Now create a tvbuff for the SDP stuff and
472 * The amount of data to be processed that's
473 * available in the tvbuff is "datalen", which
474 * is the minimum of the amount of data left in
475 * the tvbuff and any specified content length.
477 * The amount of data to be processed that's in
478 * this frame, regardless of whether it was
479 * captured or not, is "content_length",
480 * which, if no content length was specified,
481 * is -1, i.e. "to the end of the frame.
483 new_tvb = tvb_new_subset(tvb, offset, datalen,
485 call_dissector(sdp_handle, new_tvb, pinfo, tree);
487 proto_tree_add_text(rtsp_tree, tvb, offset, datalen,
488 "Data (%d bytes)", datalen);
492 * We've processed "datalen" bytes worth of data
493 * (which may be no data at all); advance the
494 * offset past whatever data we've processed, so they
499 return offset - orig_offset;
503 process_rtsp_request(tvbuff_t *tvb, int offset, const u_char *data,
504 int linelen, proto_tree *tree)
506 const u_char *lineend = data + linelen;
509 const u_char *url_start;
512 /* Request Methods */
513 for (ii = 0; ii < RTSP_NMETHODS; ii++) {
514 size_t len = strlen(rtsp_methods[ii]);
515 if (linelen >= len && !strncasecmp(rtsp_methods[ii], data, len))
518 if (ii == RTSP_NMETHODS) {
520 * We got here because "is_rtsp_request_or_reply()" returned
521 * RTSP_REQUEST, so we know one of the request methods
522 * matched, so we "can't get here".
524 g_assert_not_reached();
528 proto_tree_add_string_hidden(tree, hf_rtsp_method, tvb, offset,
529 strlen(rtsp_methods[ii]), rtsp_methods[ii]);
533 while (url < lineend && !isspace(*url))
535 while (url < lineend && isspace(*url))
538 while (url < lineend && !isspace(*url))
540 tmp_url = g_malloc(url - url_start + 1);
541 memcpy(tmp_url, url_start, url - url_start);
542 tmp_url[url - url_start] = 0;
543 proto_tree_add_string_hidden(tree, hf_rtsp_url, tvb,
544 offset + (url_start - data), url - url_start, tmp_url);
549 process_rtsp_reply(tvbuff_t *tvb, int offset, const u_char *data,
550 int linelen, proto_tree *tree)
552 const u_char *lineend = data + linelen;
553 const u_char *status = data;
554 const u_char *status_start;
555 unsigned int status_i;
558 while (status < lineend && !isspace(*status))
560 while (status < lineend && isspace(*status))
562 status_start = status;
564 while (status < lineend && isdigit(*status))
565 status_i = status_i * 10 + *status++ - '0';
566 proto_tree_add_uint_hidden(tree, hf_rtsp_status, tvb,
567 offset + (status_start - data),
568 status - status_start, status_i);
572 dissect_rtsp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
577 CHECK_DISPLAY_AS_DATA(proto_rtsp, tvb, pinfo, tree);
579 pinfo->current_proto = "RTSP";
581 if (check_col(pinfo->fd, COL_PROTOCOL))
582 col_set_str(pinfo->fd, COL_PROTOCOL, "RTSP");
584 while (tvb_offset_exists(tvb, offset)) {
585 len = dissect_rtspmessage(tvb, offset, pinfo, tree);
591 * OK, we've set the Protocol and Info columns for the
592 * first RTSP message; make the columns non-writable,
593 * so that we don't change it for subsequent RTSP messages.
595 col_set_writable(pinfo->fd, FALSE);
600 proto_register_rtsp(void)
602 static gint *ett[] = {
605 static hf_register_info hf[] = {
607 { "Method", "rtsp.method", FT_STRING, BASE_NONE, NULL, 0 }},
609 { "URL", "rtsp.url", FT_STRING, BASE_NONE, NULL, 0 }},
611 { "Status", "rtsp.status", FT_UINT32, BASE_DEC, NULL, 0 }},
614 proto_rtsp = proto_register_protocol("Real Time Streaming Protocol",
616 proto_register_field_array(proto_rtsp, hf, array_length(hf));
617 proto_register_subtree_array(ett, array_length(ett));
621 proto_reg_handoff_rtsp(void)
623 dissector_add("tcp.port", TCP_PORT_RTSP, dissect_rtsp);
626 * Get a handle for the SDP dissector.
628 sdp_handle = find_dissector("sdp");