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.26 2000/11/15 07:07:43 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-rtp.h"
43 #include "packet-rtcp.h"
44 #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 void process_rtsp_request(tvbuff_t *tvb, int offset, const u_char *data,
57 int linelen, proto_tree *tree);
59 static void process_rtsp_reply(tvbuff_t *tvb, int offset, const u_char *data,
60 int linelen, proto_tree *tree);
68 static const char *rtsp_methods[] = {
69 "DESCRIBE", "ANNOUNCE", "GET_PARAMETER", "OPTIONS",
70 "PAUSE", "PLAY", "RECORD", "REDIRECT", "SETUP",
71 "SET_PARAMETER", "TEARDOWN"
74 #define RTSP_NMETHODS (sizeof rtsp_methods / sizeof rtsp_methods[0])
76 static dissector_handle_t sdp_handle;
79 is_rtsp_request_or_reply(const u_char *line, int linelen)
83 /* Is this an RTSP reply? */
84 if (linelen >= 5 && strncasecmp("RTSP/", line, 5) == 0) {
92 * Is this an RTSP request?
93 * Check whether the line begins with one of the RTSP request
96 for (ii = 0; ii < RTSP_NMETHODS; ii++) {
97 size_t len = strlen(rtsp_methods[ii]);
99 strncasecmp(rtsp_methods[ii], line, len) == 0)
106 is_content_sdp(const u_char *line, int linelen)
108 const char *hdr = "Content-Type:";
109 size_t hdrlen = strlen(hdr);
110 const char *type = "application/sdp";
111 size_t typelen = strlen(type);
113 if (linelen < hdrlen || strncasecmp(hdr, line, hdrlen))
118 while (linelen > 0 && (*line == ' ' || *line == '\t')) {
123 if (linelen < typelen || strncasecmp(type, line, typelen))
128 if (linelen > 0 && !isspace(*line))
134 static const char rtsp_transport[] = "Transport:";
135 static const char rtsp_sps[] = "server_port=";
136 static const char rtsp_cps[] = "client_port=";
137 static const char rtsp_rtp[] = "rtp/avp";
140 rtsp_create_conversation(const u_char *trans_begin, const u_char *trans_end)
142 conversation_t *conv;
145 int c_data_port, c_mon_port;
146 int s_data_port, s_mon_port;
148 strncpy(tbuf, trans_begin, trans_end - trans_begin);
149 tbuf[sizeof(tbuf)-1] = 0;
151 tmp = tbuf + strlen(rtsp_transport);
152 while (*tmp && isspace(*tmp))
154 if (strncasecmp(tmp, rtsp_rtp, strlen(rtsp_rtp)) != 0)
157 c_data_port = c_mon_port = 0;
158 s_data_port = s_mon_port = 0;
159 if ((tmp = strstr(tbuf, rtsp_sps))) {
160 tmp += strlen(rtsp_sps);
161 if (sscanf(tmp, "%u-%u", &s_data_port, &s_mon_port) < 1)
162 g_warning("rtsp: failed to parse server_port");
164 if ((tmp = strstr(tbuf, rtsp_cps))) {
165 tmp += strlen(rtsp_cps);
166 if (sscanf(tmp, "%u-%u", &c_data_port, &c_mon_port) < 1)
167 g_warning("rtsp: failed to parse client_port");
169 if (!c_data_port || !s_data_port)
172 conv = conversation_new(&pi.src, &pi.dst, PT_UDP, s_data_port,
174 conversation_set_dissector(conv, dissect_rtp);
176 if (!c_mon_port || !s_mon_port)
179 conv = conversation_new(&pi.src, &pi.dst, PT_UDP, s_mon_port,
181 conversation_set_dissector(conv, dissect_rtcp);
185 dissect_rtsp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
187 proto_tree *rtsp_tree;
188 proto_item *ti = NULL;
192 const u_char *linep, *lineend;
198 CHECK_DISPLAY_AS_DATA(proto_rtsp, tvb, pinfo, tree);
200 pinfo->current_proto = "RTSP";
204 ti = proto_tree_add_item(tree, proto_rtsp, tvb, offset,
205 tvb_length_remaining(tvb, offset), FALSE);
206 rtsp_tree = proto_item_add_subtree(ti, ett_rtsp);
209 if (check_col(pinfo->fd, COL_PROTOCOL))
210 col_add_str(pinfo->fd, COL_PROTOCOL, "RTSP");
211 if (check_col(pinfo->fd, COL_INFO)) {
213 * Put the first line from the buffer into the summary
214 * if it's an RTSP request or reply (but leave out the
216 * Otherwise, just call it a continuation.
218 linelen = tvb_find_line_end(tvb, offset, -1, &next_offset);
219 line = tvb_get_ptr(tvb, offset, linelen);
220 switch (is_rtsp_request_or_reply(line, linelen)) {
224 col_add_str(pinfo->fd, COL_INFO,
225 format_text(line, linelen));
229 col_add_str(pinfo->fd, COL_INFO, "Continuation");
235 * Process the packet data, a line at a time.
237 while (tvb_offset_exists(tvb, offset)) {
239 * Find the end of the line.
241 linelen = tvb_find_line_end(tvb, offset, -1, &next_offset);
244 * Get a buffer that refers to the line.
246 line = tvb_get_ptr(tvb, offset, linelen);
247 lineend = line + linelen;
250 * OK, does it look like an RTSP request or
253 switch (is_rtsp_request_or_reply(line, linelen)) {
256 if (rtsp_tree != NULL)
257 process_rtsp_request(tvb, offset, line, linelen,
262 if (rtsp_tree != NULL)
263 process_rtsp_reply(tvb, offset, line, linelen,
272 * No. Does it look like a blank line (as would
273 * appear at the end of an RTSP request)?
276 goto is_rtsp; /* Yes. */
279 * No. Does it look like a MIME header?
282 while (linep < lineend) {
285 break; /* not printable, not a MIME header */
305 * It's a tspecial, so it's not
306 * part of a token, so it's not
307 * a field name for the beginning
314 * This ends the token; we consider
315 * this to be a MIME header.
317 if (is_content_sdp(line, linelen))
325 * We don't consider this part of an RTSP request or
326 * reply, so we don't display it.
335 proto_tree_add_text(rtsp_tree, tvb, offset,
336 next_offset - offset, "%s",
337 tvb_format_text(tvb, offset, next_offset - offset));
339 if (linelen > strlen(rtsp_transport) &&
340 strncasecmp(line, rtsp_transport,
341 strlen(rtsp_transport)) == 0)
342 rtsp_create_conversation(line, line + linelen);
343 offset = next_offset;
346 datalen = tvb_length_remaining(tvb, offset);
352 * Fix up the top-level item so that it doesn't
353 * include the SDP stuff.
356 proto_item_set_len(ti, offset);
359 * Now creat a tvbuff for the SDP stuff and
362 new_tvb = tvb_new_subset(tvb, offset, -1, -1);
363 call_dissector(sdp_handle, new_tvb, pinfo, tree);
367 proto_tree_add_text(rtsp_tree, tvb, offset, datalen,
368 "Data (%d bytes)", datalen);
374 process_rtsp_request(tvbuff_t *tvb, int offset, const u_char *data,
375 int linelen, proto_tree *tree)
377 const u_char *lineend = data + linelen;
380 const u_char *url_start;
383 /* Request Methods */
384 for (ii = 0; ii < RTSP_NMETHODS; ii++) {
385 size_t len = strlen(rtsp_methods[ii]);
386 if (linelen >= len && !strncasecmp(rtsp_methods[ii], data, len))
389 if (ii == RTSP_NMETHODS) {
391 * We got here because "is_rtsp_request_or_reply()" returned
392 * RTSP_REQUEST, so we know one of the request methods
393 * matched, so we "can't get here".
395 g_assert_not_reached();
399 proto_tree_add_string_hidden(tree, hf_rtsp_method, tvb, offset,
400 strlen(rtsp_methods[ii]), rtsp_methods[ii]);
404 while (url < lineend && !isspace(*url))
406 while (url < lineend && isspace(*url))
409 while (url < lineend && !isspace(*url))
411 tmp_url = g_malloc(url - url_start + 1);
412 memcpy(tmp_url, url_start, url - url_start);
413 tmp_url[url - url_start] = 0;
414 proto_tree_add_string_hidden(tree, hf_rtsp_url, tvb,
415 offset + (url_start - data), url - url_start, tmp_url);
420 process_rtsp_reply(tvbuff_t *tvb, int offset, const u_char *data,
421 int linelen, proto_tree *tree)
423 const u_char *lineend = data + linelen;
424 const u_char *status = data;
425 const u_char *status_start;
426 unsigned int status_i;
429 while (status < lineend && !isspace(*status))
431 while (status < lineend && isspace(*status))
433 status_start = status;
435 while (status < lineend && isdigit(*status))
436 status_i = status_i * 10 + *status++ - '0';
437 proto_tree_add_uint_hidden(tree, hf_rtsp_status, tvb,
438 offset + (status_start - data),
439 status - status_start, status_i);
443 proto_register_rtsp(void)
445 static gint *ett[] = {
448 static hf_register_info hf[] = {
450 { "Method", "rtsp.method", FT_STRING, BASE_NONE, NULL, 0 }},
452 { "URL", "rtsp.url", FT_STRING, BASE_NONE, NULL, 0 }},
454 { "Status", "rtsp.status", FT_UINT32, BASE_DEC, NULL, 0 }},
457 proto_rtsp = proto_register_protocol("Real Time Streaming Protocol",
459 proto_register_field_array(proto_rtsp, hf, array_length(hf));
460 proto_register_subtree_array(ett, array_length(ett));
464 proto_reg_handoff_rtsp(void)
466 dissector_add("tcp.port", TCP_PORT_RTSP, dissect_rtsp);
469 * Get a handle for the SDP dissector.
471 sdp_handle = find_dissector("sdp");