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.14 2000/05/31 05:07:37 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"
47 static int proto_rtsp = -1;
48 static gint ett_rtsp = -1;
50 static int hf_rtsp_method = -1;
51 static int hf_rtsp_url = -1;
52 static int hf_rtsp_status = -1;
54 #define TCP_PORT_RTSP 554
56 static int process_rtsp_request_or_reply(const u_char *data, int offset,
57 int linelen, proto_tree *tree);
60 is_content_sdp(const u_char *line, int linelen)
62 const char *hdr = "Content-Type:";
63 size_t hdrlen = strlen(hdr);
64 const char *type = "application/sdp";
65 size_t typelen = strlen(type);
67 if (linelen < hdrlen || strncasecmp(hdr, line, hdrlen))
72 while (linelen > 0 && (*line == ' ' || *line == '\t')) {
77 if (linelen < typelen || strncasecmp(type, line, typelen))
82 if (linelen > 0 && !isspace(*line))
88 static const char rtsp_transport[] = "Transport:";
89 static const char rtsp_sps[] = "server_port=";
90 static const char rtsp_cps[] = "client_port=";
91 static const char rtsp_rtp[] = "rtp/avp";
94 rtsp_create_conversation(const u_char *trans_begin, const u_char *trans_end)
99 int c_data_port, c_mon_port;
100 int s_data_port, s_mon_port;
102 strncpy(tbuf, trans_begin, trans_end - trans_begin);
103 tbuf[sizeof(tbuf)-1] = 0;
105 tmp = tbuf + strlen(rtsp_transport);
106 while (*tmp && isspace(*tmp))
108 if (strncasecmp(tmp, rtsp_rtp, strlen(rtsp_rtp)) != 0)
111 c_data_port = c_mon_port = 0;
112 s_data_port = s_mon_port = 0;
113 if ((tmp = strstr(tbuf, rtsp_sps))) {
114 tmp += strlen(rtsp_sps);
115 if (sscanf(tmp, "%u-%u", &s_data_port, &s_mon_port) < 1)
116 g_warning("rtsp: failed to parse server_port");
118 if ((tmp = strstr(tbuf, rtsp_cps))) {
119 tmp += strlen(rtsp_cps);
120 if (sscanf(tmp, "%u-%u", &c_data_port, &c_mon_port) < 1)
121 g_warning("rtsp: failed to parse client_port");
123 if (!c_data_port || !s_data_port)
126 conv = conversation_new(&pi.src, &pi.dst, PT_UDP, s_data_port,
128 conv->dissector = dissect_rtp;
130 if (!c_mon_port || !s_mon_port)
133 conv = conversation_new(&pi.src, &pi.dst, PT_UDP, s_mon_port,
135 conv->dissector = dissect_rtcp;
138 static void dissect_rtsp(const u_char *pd, int offset, frame_data *fd,
141 proto_tree *rtsp_tree;
143 const u_char *data, *dataend;
144 const u_char *linep, *lineend, *eol;
151 dataend = data + END_OF_FRAME;
152 end_offset = offset + END_OF_FRAME;
156 ti = proto_tree_add_item(tree, proto_rtsp, NullTVB, offset,
157 END_OF_FRAME, FALSE);
158 rtsp_tree = proto_item_add_subtree(ti, ett_rtsp);
161 if (check_col(fd, COL_PROTOCOL))
162 col_add_str(fd, COL_PROTOCOL, "RTSP");
163 if (check_col(fd, COL_INFO)) {
165 * Put the first line from the buffer into the summary
166 * if it's an RTSP request or reply. Otherwise, just call
169 lineend = find_line_end(data, dataend, &eol);
170 linelen = lineend - data;
171 if (process_rtsp_request_or_reply(data, offset, linelen,
173 col_add_str(fd, COL_INFO, format_text(data, linelen));
175 col_add_str(fd, COL_INFO, "Continuation");
178 if (offset >= end_offset)
180 while (data < dataend) {
182 * Find the end of the line.
184 lineend = find_line_end(data, dataend, &eol);
185 linelen = lineend - data;
188 * OK, does it look like an RTSP request or
191 if (process_rtsp_request_or_reply(data, offset, linelen,
196 * No. Does it look like a blank line (as would
197 * appear at the end of an RTSP request)?
204 if (strncmp(data, "\r\n", 2) == 0 ||
205 strncmp(data, "\n\r", 2) == 0)
210 * No. Does it look like a MIME header?
213 while (linep < lineend) {
216 break; /* not printable, not a MIME header */
236 * It's a tspecial, so it's not
237 * part of a token, so it's not
238 * a field name for the beginning
245 * This ends the token; we consider
246 * this to be a MIME header.
248 if (is_content_sdp(data, linelen))
256 * We don't consider this part of an RTSP request or
257 * reply, so we don't display it.
266 proto_tree_add_text(rtsp_tree, NullTVB, offset, linelen, "%s",
267 format_text(data, linelen));
269 if (linelen > strlen(rtsp_transport) &&
270 strncasecmp(data, rtsp_transport,
271 strlen(rtsp_transport)) == 0)
272 rtsp_create_conversation(data, data + linelen);
278 dissect_sdp(pd, offset, fd, tree);
279 if (check_col(fd, COL_PROTOCOL))
280 col_add_str(fd, COL_PROTOCOL, "RTSP/SDP");
282 else if (data < dataend) {
283 proto_tree_add_text(rtsp_tree, NullTVB, offset, END_OF_FRAME,
284 "Data (%d bytes)", END_OF_FRAME);
289 proto_tree_add_text(rtsp_tree, NullTVB, end_offset, 0,
290 "Unexpected end of packet");
293 const char *rtsp_methods[] = {
294 "DESCRIBE", "ANNOUNCE", "GET_PARAMETER", "OPTIONS",
295 "PAUSE", "PLAY", "RECORD", "REDIRECT", "SETUP",
296 "SET_PARAMETER", "TEARDOWN"
298 const int rtsp_nmethods = sizeof(rtsp_methods) / sizeof(*rtsp_methods);
301 process_rtsp_request_or_reply(const u_char *data, int offset, int linelen,
305 const u_char *lineend = data + linelen;
308 if (linelen >= 5 && !strncasecmp("RTSP/", data, 5)) {
311 const u_char *status = data;
312 const u_char *status_start;
313 unsigned int status_i = 0;
314 while (status < lineend && !isspace(*status))
316 while (status < lineend && isspace(*status))
318 status_start = status;
319 while (status < lineend && isdigit(*status))
320 status_i = status_i * 10 + *status++ - '0';
321 proto_tree_add_uint_hidden(tree, hf_rtsp_status, NullTVB,
322 offset + (status_start - data),
323 status - status_start, status_i);
328 /* Request Methods */
329 for (ii = 0; ii < rtsp_nmethods; ii++) {
330 size_t len = strlen(rtsp_methods[ii]);
331 if (linelen >= len && !strncasecmp(rtsp_methods[ii], data, len))
334 if (ii == rtsp_nmethods)
339 const u_char *url_start;
343 proto_tree_add_string_hidden(tree, hf_rtsp_method, NullTVB, offset,
344 strlen(rtsp_methods[ii]), rtsp_methods[ii]);
348 while (url < lineend && !isspace(*url))
350 while (url < lineend && isspace(*url))
353 while (url < lineend && !isspace(*url))
355 tmp_url = g_malloc(url - url_start + 1);
356 memcpy(tmp_url, url_start, url - url_start);
357 tmp_url[url - url_start] = 0;
358 proto_tree_add_string_hidden(tree, hf_rtsp_url, NullTVB,
359 offset + (url_start - data), url - url_start, tmp_url);
366 proto_register_rtsp(void)
368 static gint *ett[] = {
371 static hf_register_info hf[] = {
373 { "Method", "rtsp.method", FT_STRING, BASE_NONE, NULL, 0 }},
375 { "URL", "rtsp.url", FT_STRING, BASE_NONE, NULL, 0 }},
377 { "Status", "rtsp.status", FT_UINT32, BASE_DEC, NULL, 0 }},
380 proto_rtsp = proto_register_protocol("Real Time Streaming Protocol",
382 proto_register_field_array(proto_rtsp, hf, array_length(hf));
383 proto_register_subtree_array(ett, array_length(ett));
387 proto_reg_handoff_rtsp(void)
389 dissector_add("tcp.port", TCP_PORT_RTSP, dissect_rtsp);