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.24 2000/11/12 21:23:53 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>
42 #include "packet-sdp.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
57 static void process_rtsp_request(tvbuff_t *tvb, int offset, const u_char *data,
58 int linelen, proto_tree *tree);
60 static void process_rtsp_reply(tvbuff_t *tvb, int offset, const u_char *data,
61 int linelen, proto_tree *tree);
69 static const char *rtsp_methods[] = {
70 "DESCRIBE", "ANNOUNCE", "GET_PARAMETER", "OPTIONS",
71 "PAUSE", "PLAY", "RECORD", "REDIRECT", "SETUP",
72 "SET_PARAMETER", "TEARDOWN"
75 #define RTSP_NMETHODS (sizeof rtsp_methods / sizeof rtsp_methods[0])
78 is_rtsp_request_or_reply(const u_char *line, int linelen)
82 /* Is this an RTSP reply? */
83 if (linelen >= 5 && strncasecmp("RTSP/", line, 5) == 0) {
91 * Is this an RTSP request?
92 * Check whether the line begins with one of the RTSP request
95 for (ii = 0; ii < RTSP_NMETHODS; ii++) {
96 size_t len = strlen(rtsp_methods[ii]);
98 strncasecmp(rtsp_methods[ii], line, len) == 0)
105 is_content_sdp(const u_char *line, int linelen)
107 const char *hdr = "Content-Type:";
108 size_t hdrlen = strlen(hdr);
109 const char *type = "application/sdp";
110 size_t typelen = strlen(type);
112 if (linelen < hdrlen || strncasecmp(hdr, line, hdrlen))
117 while (linelen > 0 && (*line == ' ' || *line == '\t')) {
122 if (linelen < typelen || strncasecmp(type, line, typelen))
127 if (linelen > 0 && !isspace(*line))
133 static const char rtsp_transport[] = "Transport:";
134 static const char rtsp_sps[] = "server_port=";
135 static const char rtsp_cps[] = "client_port=";
136 static const char rtsp_rtp[] = "rtp/avp";
139 rtsp_create_conversation(const u_char *trans_begin, const u_char *trans_end)
141 conversation_t *conv;
144 int c_data_port, c_mon_port;
145 int s_data_port, s_mon_port;
147 strncpy(tbuf, trans_begin, trans_end - trans_begin);
148 tbuf[sizeof(tbuf)-1] = 0;
150 tmp = tbuf + strlen(rtsp_transport);
151 while (*tmp && isspace(*tmp))
153 if (strncasecmp(tmp, rtsp_rtp, strlen(rtsp_rtp)) != 0)
156 c_data_port = c_mon_port = 0;
157 s_data_port = s_mon_port = 0;
158 if ((tmp = strstr(tbuf, rtsp_sps))) {
159 tmp += strlen(rtsp_sps);
160 if (sscanf(tmp, "%u-%u", &s_data_port, &s_mon_port) < 1)
161 g_warning("rtsp: failed to parse server_port");
163 if ((tmp = strstr(tbuf, rtsp_cps))) {
164 tmp += strlen(rtsp_cps);
165 if (sscanf(tmp, "%u-%u", &c_data_port, &c_mon_port) < 1)
166 g_warning("rtsp: failed to parse client_port");
168 if (!c_data_port || !s_data_port)
171 conv = conversation_new(&pi.src, &pi.dst, PT_UDP, s_data_port,
173 conversation_set_dissector(conv, dissect_rtp);
175 if (!c_mon_port || !s_mon_port)
178 conv = conversation_new(&pi.src, &pi.dst, PT_UDP, s_mon_port,
180 conversation_set_dissector(conv, dissect_rtcp);
184 dissect_rtsp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
186 proto_tree *rtsp_tree;
187 proto_item *ti = NULL;
191 const u_char *linep, *lineend;
197 CHECK_DISPLAY_AS_DATA(proto_rtsp, tvb, pinfo, tree);
199 pinfo->current_proto = "RTSP";
203 ti = proto_tree_add_item(tree, proto_rtsp, tvb, offset,
204 tvb_length_remaining(tvb, offset), FALSE);
205 rtsp_tree = proto_item_add_subtree(ti, ett_rtsp);
208 if (check_col(pinfo->fd, COL_PROTOCOL))
209 col_add_str(pinfo->fd, COL_PROTOCOL, "RTSP");
210 if (check_col(pinfo->fd, COL_INFO)) {
212 * Put the first line from the buffer into the summary
213 * if it's an RTSP request or reply (but leave out the
215 * Otherwise, just call it a continuation.
217 linelen = tvb_find_line_end(tvb, offset, -1, &next_offset);
218 line = tvb_get_ptr(tvb, offset, linelen);
219 switch (is_rtsp_request_or_reply(line, linelen)) {
223 col_add_str(pinfo->fd, COL_INFO,
224 format_text(line, linelen));
228 col_add_str(pinfo->fd, COL_INFO, "Continuation");
234 * Process the packet data, a line at a time.
236 while (tvb_length_remaining(tvb, offset)) {
238 * Find the end of the line.
240 linelen = tvb_find_line_end(tvb, offset, -1, &next_offset);
243 * Get a buffer that refers to the line.
245 line = tvb_get_ptr(tvb, offset, linelen);
246 lineend = line + linelen;
249 * OK, does it look like an RTSP request or
252 switch (is_rtsp_request_or_reply(line, linelen)) {
255 if (rtsp_tree != NULL)
256 process_rtsp_request(tvb, offset, line, linelen,
261 if (rtsp_tree != NULL)
262 process_rtsp_reply(tvb, offset, line, linelen,
271 * No. Does it look like a blank line (as would
272 * appear at the end of an RTSP request)?
275 goto is_rtsp; /* Yes. */
278 * No. Does it look like a MIME header?
281 while (linep < lineend) {
284 break; /* not printable, not a MIME header */
304 * It's a tspecial, so it's not
305 * part of a token, so it's not
306 * a field name for the beginning
313 * This ends the token; we consider
314 * this to be a MIME header.
316 if (is_content_sdp(line, linelen))
324 * We don't consider this part of an RTSP request or
325 * reply, so we don't display it.
334 proto_tree_add_text(rtsp_tree, tvb, offset,
335 next_offset - offset, "%s",
336 tvb_format_text(tvb, offset, next_offset - offset));
338 if (linelen > strlen(rtsp_transport) &&
339 strncasecmp(line, rtsp_transport,
340 strlen(rtsp_transport)) == 0)
341 rtsp_create_conversation(line, line + linelen);
342 offset = next_offset;
345 datalen = tvb_length_remaining(tvb, offset);
351 * Fix up the top-level item so that it doesn't
352 * include the SDP stuff.
355 proto_item_set_len(ti, offset);
358 * Now creat a tvbuff for the SDP stuff and
361 new_tvb = tvb_new_subset(tvb, offset, -1, -1);
362 dissect_sdp(new_tvb, pinfo, tree);
366 proto_tree_add_text(rtsp_tree, tvb, offset, datalen,
367 "Data (%d bytes)", datalen);
373 process_rtsp_request(tvbuff_t *tvb, int offset, const u_char *data,
374 int linelen, proto_tree *tree)
376 const u_char *lineend = data + linelen;
379 const u_char *url_start;
382 /* Request Methods */
383 for (ii = 0; ii < RTSP_NMETHODS; ii++) {
384 size_t len = strlen(rtsp_methods[ii]);
385 if (linelen >= len && !strncasecmp(rtsp_methods[ii], data, len))
388 if (ii == RTSP_NMETHODS) {
390 * We got here because "is_rtsp_request_or_reply()" returned
391 * RTSP_REQUEST, so we know one of the request methods
392 * matched, so we "can't get here".
394 g_assert_not_reached();
398 proto_tree_add_string_hidden(tree, hf_rtsp_method, tvb, offset,
399 strlen(rtsp_methods[ii]), rtsp_methods[ii]);
403 while (url < lineend && !isspace(*url))
405 while (url < lineend && isspace(*url))
408 while (url < lineend && !isspace(*url))
410 tmp_url = g_malloc(url - url_start + 1);
411 memcpy(tmp_url, url_start, url - url_start);
412 tmp_url[url - url_start] = 0;
413 proto_tree_add_string_hidden(tree, hf_rtsp_url, tvb,
414 offset + (url_start - data), url - url_start, tmp_url);
419 process_rtsp_reply(tvbuff_t *tvb, int offset, const u_char *data,
420 int linelen, proto_tree *tree)
422 const u_char *lineend = data + linelen;
423 const u_char *status = data;
424 const u_char *status_start;
425 unsigned int status_i;
428 while (status < lineend && !isspace(*status))
430 while (status < lineend && isspace(*status))
432 status_start = status;
434 while (status < lineend && isdigit(*status))
435 status_i = status_i * 10 + *status++ - '0';
436 proto_tree_add_uint_hidden(tree, hf_rtsp_status, tvb,
437 offset + (status_start - data),
438 status - status_start, status_i);
442 proto_register_rtsp(void)
444 static gint *ett[] = {
447 static hf_register_info hf[] = {
449 { "Method", "rtsp.method", FT_STRING, BASE_NONE, NULL, 0 }},
451 { "URL", "rtsp.url", FT_STRING, BASE_NONE, NULL, 0 }},
453 { "Status", "rtsp.status", FT_UINT32, BASE_DEC, NULL, 0 }},
456 proto_rtsp = proto_register_protocol("Real Time Streaming Protocol",
458 proto_register_field_array(proto_rtsp, hf, array_length(hf));
459 proto_register_subtree_array(ett, array_length(ett));
463 proto_reg_handoff_rtsp(void)
465 dissector_add("tcp.port", TCP_PORT_RTSP, dissect_rtsp);