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.50 2002/08/28 21:00:30 jmayer Exp $
9 * Ethereal - Network traffic analyzer
10 * By Gerald Combs <gerald@ethereal.com>
11 * Copyright 1998 Gerald Combs
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
35 #include <epan/packet.h>
36 #include "packet-rtp.h"
37 #include "packet-rtcp.h"
38 #include <epan/conversation.h>
39 #include <epan/strutil.h>
41 static int proto_rtsp = -1;
42 static gint ett_rtsp = -1;
44 static gint ett_rtspframe = -1;
46 static int hf_rtsp_method = -1;
47 static int hf_rtsp_url = -1;
48 static int hf_rtsp_status = -1;
50 static dissector_handle_t sdp_handle;
51 static dissector_handle_t rtp_handle;
52 static dissector_handle_t rtcp_handle;
54 static GMemChunk *rtsp_vals = NULL;
55 #define rtsp_hash_init_count 20
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 dissector_handle_t dissector;
72 #define RTSP_MAX_INTERLEAVED (8)
75 * Careful about dynamically allocating memory in this structure (say
76 * for dynamically increasing the size of the 'interleaved' array) -
77 * the containing structure is garbage collected and contained
78 * pointers will not be freed.
81 rtsp_interleaved_t interleaved[RTSP_MAX_INTERLEAVED];
82 } rtsp_conversation_data_t;
85 dissect_rtspinterleaved(tvbuff_t *tvb, int offset, packet_info *pinfo,
88 proto_tree *rtspframe_tree;
91 guint8 rf_start; /* always RTSP_FRAMEHDR */
92 guint8 rf_chan; /* interleaved channel id */
93 guint16 rf_len; /* packet length */
97 rtsp_conversation_data_t *data;
98 dissector_handle_t dissector;
100 orig_offset = offset;
101 rf_start = tvb_get_guint8(tvb, offset);
102 rf_chan = tvb_get_guint8(tvb, offset+1);
103 rf_len = tvb_get_ntohs(tvb, offset+2);
105 if (check_col(pinfo->cinfo, COL_INFO))
106 col_add_fstr(pinfo->cinfo, COL_INFO,
107 "Interleaved channel 0x%02x, %u bytes",
112 * We're not building a full protocol tree; all we care
113 * about is setting the column info.
118 ti = proto_tree_add_protocol_format(tree, proto_rtsp, tvb, offset, 4,
119 "RTSP Interleaved Frame, Channel: 0x%02x, %u bytes",
121 rtspframe_tree = proto_item_add_subtree(ti, ett_rtspframe);
123 proto_tree_add_text(rtspframe_tree, tvb, offset, 1,
128 proto_tree_add_text(rtspframe_tree, tvb, offset, 1,
133 proto_tree_add_text(rtspframe_tree, tvb, offset, 2,
139 * We set the actual length of the tvbuff for the interleaved
140 * stuff to the minimum of what's left in the tvbuff and the
141 * length in the header.
143 * XXX - what if there's nothing left in the tvbuff?
144 * We'd want a BoundsError exception to be thrown, so
145 * that a Short Frame would be reported.
147 framelen = tvb_length_remaining(tvb, offset);
148 if (framelen > rf_len)
150 next_tvb = tvb_new_subset(tvb, offset, framelen, rf_len);
152 conv = find_conversation(&pinfo->src, &pinfo->dst, pinfo->ptype,
153 pinfo->srcport, pinfo->destport, 0);
156 (data = conversation_get_proto_data(conv, proto_rtsp)) &&
157 rf_chan < RTSP_MAX_INTERLEAVED &&
158 (dissector = data->interleaved[rf_chan].dissector)) {
159 call_dissector(dissector, next_tvb, pinfo, tree);
161 proto_tree_add_text(rtspframe_tree, tvb, offset, rf_len,
162 "Data (%u bytes)", rf_len);
167 return offset - orig_offset;
170 static void process_rtsp_request(tvbuff_t *tvb, int offset, const guchar *data,
171 size_t linelen, proto_tree *tree);
173 static void process_rtsp_reply(tvbuff_t *tvb, int offset, const guchar *data,
174 size_t linelen, proto_tree *tree);
182 static const char *rtsp_methods[] = {
183 "DESCRIBE", "ANNOUNCE", "GET_PARAMETER", "OPTIONS",
184 "PAUSE", "PLAY", "RECORD", "REDIRECT", "SETUP",
185 "SET_PARAMETER", "TEARDOWN"
188 #define RTSP_NMETHODS (sizeof rtsp_methods / sizeof rtsp_methods[0])
191 is_rtsp_request_or_reply(const guchar *line, size_t linelen)
195 /* Is this an RTSP reply? */
196 if (linelen >= 5 && strncasecmp("RTSP/", line, 5) == 0) {
204 * Is this an RTSP request?
205 * Check whether the line begins with one of the RTSP request
208 for (ii = 0; ii < RTSP_NMETHODS; ii++) {
209 size_t len = strlen(rtsp_methods[ii]);
210 if (linelen >= len &&
211 strncasecmp(rtsp_methods[ii], line, len) == 0)
217 static const char rtsp_content_type[] = "Content-Type:";
220 is_content_sdp(const guchar *line, size_t linelen)
222 static const char type[] = "application/sdp";
223 size_t typelen = STRLEN_CONST(type);
225 line += STRLEN_CONST(rtsp_content_type);
226 linelen -= STRLEN_CONST(rtsp_content_type);
227 while (linelen > 0 && (*line == ' ' || *line == '\t')) {
232 if (linelen < typelen || strncasecmp(type, line, typelen))
237 if (linelen > 0 && !isspace(*line))
243 static const char rtsp_transport[] = "Transport:";
244 static const char rtsp_sps[] = "server_port=";
245 static const char rtsp_cps[] = "client_port=";
246 static const char rtsp_rtp[] = "rtp/";
247 static const char rtsp_inter[] = "interleaved=";
250 rtsp_create_conversation(packet_info *pinfo, const guchar *line_begin,
253 conversation_t *conv;
256 guint c_data_port, c_mon_port;
257 guint s_data_port, s_mon_port;
260 if (line_len > sizeof(buf) - 1) {
262 * Don't overflow the buffer.
264 line_len = sizeof(buf) - 1;
266 memcpy(buf, line_begin, line_len);
267 buf[line_len] = '\0';
269 tmp = buf + STRLEN_CONST(rtsp_transport);
270 while (*tmp && isspace(*tmp))
272 if (strncasecmp(tmp, rtsp_rtp, strlen(rtsp_rtp)) != 0) {
273 g_warning("Frame %u: rtsp: unknown transport", pinfo->fd->num);
277 c_data_port = c_mon_port = 0;
278 s_data_port = s_mon_port = 0;
279 if ((tmp = strstr(buf, rtsp_sps))) {
280 tmp += strlen(rtsp_sps);
281 if (sscanf(tmp, "%u-%u", &s_data_port, &s_mon_port) < 1) {
282 g_warning("Frame %u: rtsp: bad server_port",
287 if ((tmp = strstr(buf, rtsp_cps))) {
288 tmp += strlen(rtsp_cps);
289 if (sscanf(tmp, "%u-%u", &c_data_port, &c_mon_port) < 1) {
290 g_warning("Frame %u: rtsp: bad client_port",
296 rtsp_conversation_data_t *data;
297 guint s_data_chan, s_mon_chan;
301 * Deal with RTSP TCP-interleaved conversations.
303 if ((tmp = strstr(buf, rtsp_inter)) == NULL) {
305 * No interleaved or server_port - probably a
306 * SETUP request, rather than reply.
310 tmp += strlen(rtsp_inter);
311 i = sscanf(tmp, "%u-%u", &s_data_chan, &s_mon_chan);
313 g_warning("Frame %u: rtsp: bad interleaved",
317 conv = find_conversation(&pinfo->src, &pinfo->dst, pinfo->ptype,
318 pinfo->srcport, pinfo->destport, 0);
320 conv = conversation_new(&pinfo->src, &pinfo->dst,
321 pinfo->ptype, pinfo->srcport, pinfo->destport,
324 data = conversation_get_proto_data(conv, proto_rtsp);
326 data = g_mem_chunk_alloc(rtsp_vals);
327 conversation_add_proto_data(conv, proto_rtsp, data);
329 if (s_data_chan < RTSP_MAX_INTERLEAVED) {
330 data->interleaved[s_data_chan].dissector =
333 if (i > 1 && s_mon_chan < RTSP_MAX_INTERLEAVED) {
334 data->interleaved[s_mon_chan].dissector =
341 * We only want to match on the destination address, not the
342 * source address, because the server might send back a packet
343 * from an address other than the address to which its client
344 * sent the packet, so we construct a conversation with no
347 SET_ADDRESS(&null_addr, pinfo->src.type, 0, NULL);
349 conv = conversation_new(&pinfo->dst, &null_addr, PT_UDP, c_data_port,
350 s_data_port, NO_ADDR2 | (!s_data_port ? NO_PORT2 : 0));
351 conversation_set_dissector(conv, rtp_handle);
356 conv = conversation_new(&pinfo->dst, &null_addr, PT_UDP, c_mon_port,
357 s_mon_port, NO_ADDR2 | (!s_mon_port ? NO_PORT2 : 0));
358 conversation_set_dissector(conv, rtcp_handle);
361 static const char rtsp_content_length[] = "Content-Length:";
364 rtsp_get_content_length(const guchar *line_begin, size_t line_len)
372 if (line_len > sizeof(buf) - 1) {
374 * Don't overflow the buffer.
376 line_len = sizeof(buf) - 1;
378 memcpy(buf, line_begin, line_len);
379 buf[line_len] = '\0';
381 tmp = buf + STRLEN_CONST(rtsp_content_length);
382 while (*tmp && isspace(*tmp))
384 content_length = strtol(tmp, &p, 10);
386 if (up == tmp || (*up != '\0' && !isspace(*up)))
387 return -1; /* not a valid number */
388 return content_length;
392 dissect_rtspmessage(tvbuff_t *tvb, int offset, packet_info *pinfo,
395 proto_tree *rtsp_tree;
396 proto_item *ti = NULL;
399 const guchar *linep, *lineend;
403 gboolean is_mime_header;
407 int reported_datalen;
409 orig_offset = offset;
412 ti = proto_tree_add_item(tree, proto_rtsp, tvb, offset, -1,
414 rtsp_tree = proto_item_add_subtree(ti, ett_rtsp);
417 if (check_col(pinfo->cinfo, COL_INFO)) {
419 * Put the first line from the buffer into the summary
420 * if it's an RTSP request or reply (but leave out the
422 * Otherwise, just call it a continuation.
424 linelen = tvb_find_line_end(tvb, offset, -1, &next_offset,
426 line = tvb_get_ptr(tvb, offset, linelen);
427 switch (is_rtsp_request_or_reply(line, linelen)) {
431 col_add_str(pinfo->cinfo, COL_INFO,
432 format_text(line, linelen));
436 col_set_str(pinfo->cinfo, COL_INFO, "Continuation");
442 * We haven't yet seen a Content-Length header.
447 * Process the packet data, a line at a time.
449 while (tvb_offset_exists(tvb, offset)) {
451 * We haven't yet concluded that this is a MIME header.
453 is_mime_header = FALSE;
456 * Find the end of the line.
458 linelen = tvb_find_line_end(tvb, offset, -1, &next_offset,
462 * Get a buffer that refers to the line.
464 line = tvb_get_ptr(tvb, offset, linelen);
465 lineend = line + linelen;
468 * OK, does it look like an RTSP request or
471 switch (is_rtsp_request_or_reply(line, linelen)) {
474 if (rtsp_tree != NULL)
475 process_rtsp_request(tvb, offset, line, linelen,
480 if (rtsp_tree != NULL)
481 process_rtsp_reply(tvb, offset, line, linelen,
490 * No. Does it look like a blank line (as would
491 * appear at the end of an RTSP request)?
494 goto is_rtsp; /* Yes. */
497 * No. Does it look like a MIME header?
500 while (linep < lineend) {
503 break; /* not printable, not a MIME header */
523 * It's a tspecial, so it's not
524 * part of a token, so it's not
525 * a field name for the beginning
532 * This ends the token; we consider
533 * this to be a MIME header.
535 is_mime_header = TRUE;
541 * LWS (RFC-2616, 4.2); continue the previous
550 * We don't consider this part of an RTSP request or
551 * reply, so we don't display it.
560 proto_tree_add_text(rtsp_tree, tvb, offset,
561 next_offset - offset, "%s",
562 tvb_format_text(tvb, offset, next_offset - offset));
564 if (is_mime_header) {
566 * Process some MIME headers specially.
568 #define MIME_HDR_MATCHES(header) \
569 (linelen > STRLEN_CONST(header) && \
570 strncasecmp(line, (header), STRLEN_CONST(header)) == 0)
572 if (MIME_HDR_MATCHES(rtsp_transport)) {
574 * Based on the port numbers specified
575 * in the Transport: header, set up
576 * a conversation that will be dissected
577 * with the appropriate dissector.
579 rtsp_create_conversation(pinfo, line, linelen);
580 } else if (MIME_HDR_MATCHES(rtsp_content_type)) {
582 * If the Content-Type: header says this
583 * is SDP, dissect the payload as SDP.
585 if (is_content_sdp(line, linelen))
587 } else if (MIME_HDR_MATCHES(rtsp_content_length)) {
589 * Only the amount specified by the
590 * Content-Length: header should be treated
593 content_length = rtsp_get_content_length(line,
597 offset = next_offset;
601 * If a content length was supplied, the amount of data to be
602 * processed as RTSP payload is the minimum of the content
603 * length and the amount of data remaining in the frame.
605 * If no content length was supplied, the amount of data to be
606 * processed is the amount of data remaining in the frame.
608 datalen = tvb_length_remaining(tvb, offset);
609 if (content_length != -1) {
610 if (datalen > content_length)
611 datalen = content_length;
614 * XXX - for now, if the content length is greater
615 * than the amount of data left in this frame (not
616 * the amount of *captured* data left in the frame
617 * minus the current offset, but the amount of *actual*
618 * data that was reported to be in the frame minus
619 * the current offset), limit it to the amount
620 * of data left in this frame.
622 * If we ever handle data that crosses frame
623 * boundaries, we'll need to remember the actual
626 reported_datalen = tvb_reported_length_remaining(tvb, offset);
627 if (content_length > reported_datalen)
628 content_length = reported_datalen;
633 * There's stuff left over; process it.
639 * Fix up the top-level item so that it doesn't
640 * include the SDP stuff.
643 proto_item_set_len(ti, offset);
646 * Now create a tvbuff for the SDP stuff and
649 * The amount of data to be processed that's
650 * available in the tvbuff is "datalen", which
651 * is the minimum of the amount of data left in
652 * the tvbuff and any specified content length.
654 * The amount of data to be processed that's in
655 * this frame, regardless of whether it was
656 * captured or not, is "content_length",
657 * which, if no content length was specified,
658 * is -1, i.e. "to the end of the frame.
660 new_tvb = tvb_new_subset(tvb, offset, datalen,
662 call_dissector(sdp_handle, new_tvb, pinfo, tree);
664 if (tvb_get_guint8(tvb, offset) == RTSP_FRAMEHDR) {
666 * This is interleaved stuff; don't
667 * treat it as raw data - set "datalen"
668 * to 0, so we won't skip the offset
669 * past it, which will cause our
670 * caller to process that stuff itself.
674 proto_tree_add_text(rtsp_tree, tvb, offset,
675 datalen, "Data (%d bytes)", datalen);
680 * We've processed "datalen" bytes worth of data
681 * (which may be no data at all); advance the
682 * offset past whatever data we've processed, so they
687 return offset - orig_offset;
691 process_rtsp_request(tvbuff_t *tvb, int offset, const guchar *data,
692 size_t linelen, proto_tree *tree)
694 const guchar *lineend = data + linelen;
697 const guchar *url_start;
700 /* Request Methods */
701 for (ii = 0; ii < RTSP_NMETHODS; ii++) {
702 size_t len = strlen(rtsp_methods[ii]);
703 if (linelen >= len && !strncasecmp(rtsp_methods[ii], data, len))
706 if (ii == RTSP_NMETHODS) {
708 * We got here because "is_rtsp_request_or_reply()" returned
709 * RTSP_REQUEST, so we know one of the request methods
710 * matched, so we "can't get here".
712 g_assert_not_reached();
716 proto_tree_add_string_hidden(tree, hf_rtsp_method, tvb, offset,
717 strlen(rtsp_methods[ii]), rtsp_methods[ii]);
721 while (url < lineend && !isspace(*url))
723 while (url < lineend && isspace(*url))
726 while (url < lineend && !isspace(*url))
728 tmp_url = g_malloc(url - url_start + 1);
729 memcpy(tmp_url, url_start, url - url_start);
730 tmp_url[url - url_start] = 0;
731 proto_tree_add_string_hidden(tree, hf_rtsp_url, tvb,
732 offset + (url_start - data), url - url_start, tmp_url);
737 process_rtsp_reply(tvbuff_t *tvb, int offset, const guchar *data,
738 size_t linelen, proto_tree *tree)
740 const guchar *lineend = data + linelen;
741 const guchar *status = data;
742 const guchar *status_start;
743 unsigned int status_i;
746 while (status < lineend && !isspace(*status))
748 while (status < lineend && isspace(*status))
750 status_start = status;
752 while (status < lineend && isdigit(*status))
753 status_i = status_i * 10 + *status++ - '0';
754 proto_tree_add_uint_hidden(tree, hf_rtsp_status, tvb,
755 offset + (status_start - data),
756 status - status_start, status_i);
760 dissect_rtsp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
765 if (check_col(pinfo->cinfo, COL_PROTOCOL))
766 col_set_str(pinfo->cinfo, COL_PROTOCOL, "RTSP");
767 if (check_col(pinfo->cinfo, COL_INFO))
768 col_clear(pinfo->cinfo, COL_INFO);
770 while (tvb_offset_exists(tvb, offset)) {
771 len = (tvb_get_guint8(tvb, offset) == RTSP_FRAMEHDR)
772 ? dissect_rtspinterleaved(tvb, offset, pinfo, tree)
773 : dissect_rtspmessage(tvb, offset, pinfo, tree);
779 * OK, we've set the Protocol and Info columns for the
780 * first RTSP message; make the columns non-writable,
781 * so that we don't change it for subsequent RTSP messages.
783 col_set_writable(pinfo->cinfo, FALSE);
790 /* Routine to initialize rtsp protocol before each capture or filter pass. */
791 /* Release any memory if needed. Then setup the memory chunks. */
794 g_mem_chunk_destroy(rtsp_vals);
796 rtsp_vals = g_mem_chunk_new("rtsp_vals",
797 sizeof(rtsp_conversation_data_t),
798 rtsp_hash_init_count * sizeof(rtsp_conversation_data_t),
803 proto_register_rtsp(void)
805 static gint *ett[] = {
809 static hf_register_info hf[] = {
811 { "Method", "rtsp.method", FT_STRING, BASE_NONE, NULL, 0, "", HFILL }},
813 { "URL", "rtsp.url", FT_STRING, BASE_NONE, NULL, 0, "", HFILL }},
815 { "Status", "rtsp.status", FT_UINT32, BASE_DEC, NULL, 0, "", HFILL }},
818 proto_rtsp = proto_register_protocol("Real Time Streaming Protocol",
820 proto_register_field_array(proto_rtsp, hf, array_length(hf));
821 proto_register_subtree_array(ett, array_length(ett));
823 register_init_routine(rtsp_init); /* register re-init routine */
827 proto_reg_handoff_rtsp(void)
829 dissector_handle_t rtsp_handle;
831 rtsp_handle = create_dissector_handle(dissect_rtsp, proto_rtsp);
832 dissector_add("tcp.port", TCP_PORT_RTSP, rtsp_handle);
834 sdp_handle = find_dissector("sdp");
835 rtp_handle = find_dissector("rtp");
836 rtcp_handle = find_dissector("rtcp");