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.45 2001/12/10 00:25:33 guy 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.
30 #ifdef HAVE_SYS_TYPES_H
31 #include <sys/types.h>
40 #include "packet-rtp.h"
41 #include "packet-rtcp.h"
42 #include "conversation.h"
45 static int proto_rtsp = -1;
46 static gint ett_rtsp = -1;
48 static gint ett_rtspframe = -1;
50 static int hf_rtsp_method = -1;
51 static int hf_rtsp_url = -1;
52 static int hf_rtsp_status = -1;
54 static dissector_handle_t sdp_handle;
55 static dissector_handle_t rtp_handle;
56 static dissector_handle_t rtcp_handle;
58 static GMemChunk *rtsp_vals = NULL;
59 #define rtsp_hash_init_count 20
61 #define TCP_PORT_RTSP 554
64 * Takes an array of bytes, assumed to contain a null-terminated
65 * string, as an argument, and returns the length of the string -
66 * i.e., the size of the array, minus 1 for the null terminator.
68 #define STRLEN_CONST(str) (sizeof (str) - 1)
70 #define RTSP_FRAMEHDR ('$')
73 dissector_handle_t dissector;
76 #define RTSP_MAX_INTERLEAVED (8)
79 * Careful about dynamically allocating memory in this structure (say
80 * for dynamically increasing the size of the 'interleaved' array) -
81 * the containing structure is garbage collected and contained
82 * pointers will not be freed.
85 rtsp_interleaved_t interleaved[RTSP_MAX_INTERLEAVED];
86 } rtsp_conversation_data_t;
89 dissect_rtspinterleaved(tvbuff_t *tvb, int offset, packet_info *pinfo,
92 proto_tree *rtspframe_tree;
95 guint8 rf_start; /* always RTSP_FRAMEHDR */
96 guint8 rf_chan; /* interleaved channel id */
97 guint16 rf_len; /* packet length */
100 conversation_t *conv;
101 rtsp_conversation_data_t *data;
102 dissector_handle_t dissector;
104 orig_offset = offset;
105 rf_start = tvb_get_guint8(tvb, offset);
106 rf_chan = tvb_get_guint8(tvb, offset+1);
107 rf_len = tvb_get_ntohs(tvb, offset+2);
109 if (check_col(pinfo->cinfo, COL_INFO))
110 col_add_fstr(pinfo->cinfo, COL_INFO,
111 "Interleaved channel 0x%02x, %u bytes",
116 * We're not building a full protocol tree; all we care
117 * about is setting the column info.
122 ti = proto_tree_add_protocol_format(tree, proto_rtsp, tvb, offset, 4,
123 "RTSP Interleaved Frame, Channel: 0x%02x, %u bytes",
125 rtspframe_tree = proto_item_add_subtree(ti, ett_rtspframe);
127 proto_tree_add_text(rtspframe_tree, tvb, offset, 1,
132 proto_tree_add_text(rtspframe_tree, tvb, offset, 1,
137 proto_tree_add_text(rtspframe_tree, tvb, offset, 2,
143 * We set the actual length of the tvbuff for the interleaved
144 * stuff to the minimum of what's left in the tvbuff and the
145 * length in the header.
147 * XXX - what if there's nothing left in the tvbuff?
148 * We'd want a BoundsError exception to be thrown, so
149 * that a Short Frame would be reported.
151 framelen = tvb_length_remaining(tvb, offset);
152 if (framelen > rf_len)
154 next_tvb = tvb_new_subset(tvb, offset, framelen, rf_len);
156 conv = find_conversation(&pinfo->src, &pinfo->dst, pinfo->ptype,
157 pinfo->srcport, pinfo->destport, 0);
160 (data = conversation_get_proto_data(conv, proto_rtsp)) &&
161 rf_chan < RTSP_MAX_INTERLEAVED &&
162 (dissector = data->interleaved[rf_chan].dissector)) {
163 call_dissector(dissector, next_tvb, pinfo, tree);
165 proto_tree_add_text(rtspframe_tree, tvb, offset, rf_len,
166 "Data (%u bytes)", rf_len);
171 return offset - orig_offset;
174 static void process_rtsp_request(tvbuff_t *tvb, int offset, const u_char *data,
175 size_t linelen, proto_tree *tree);
177 static void process_rtsp_reply(tvbuff_t *tvb, int offset, const u_char *data,
178 size_t linelen, proto_tree *tree);
186 static const char *rtsp_methods[] = {
187 "DESCRIBE", "ANNOUNCE", "GET_PARAMETER", "OPTIONS",
188 "PAUSE", "PLAY", "RECORD", "REDIRECT", "SETUP",
189 "SET_PARAMETER", "TEARDOWN"
192 #define RTSP_NMETHODS (sizeof rtsp_methods / sizeof rtsp_methods[0])
195 is_rtsp_request_or_reply(const u_char *line, size_t linelen)
199 /* Is this an RTSP reply? */
200 if (linelen >= 5 && strncasecmp("RTSP/", line, 5) == 0) {
208 * Is this an RTSP request?
209 * Check whether the line begins with one of the RTSP request
212 for (ii = 0; ii < RTSP_NMETHODS; ii++) {
213 size_t len = strlen(rtsp_methods[ii]);
214 if (linelen >= len &&
215 strncasecmp(rtsp_methods[ii], line, len) == 0)
221 static const char rtsp_content_type[] = "Content-Type:";
224 is_content_sdp(const u_char *line, size_t linelen)
226 static const char type[] = "application/sdp";
227 size_t typelen = STRLEN_CONST(type);
229 line += STRLEN_CONST(rtsp_content_type);
230 linelen -= STRLEN_CONST(rtsp_content_type);
231 while (linelen > 0 && (*line == ' ' || *line == '\t')) {
236 if (linelen < typelen || strncasecmp(type, line, typelen))
241 if (linelen > 0 && !isspace(*line))
247 static const char rtsp_transport[] = "Transport:";
248 static const char rtsp_sps[] = "server_port=";
249 static const char rtsp_cps[] = "client_port=";
250 static const char rtsp_rtp[] = "rtp/";
251 static const char rtsp_inter[] = "interleaved=";
254 rtsp_create_conversation(packet_info *pinfo, const u_char *line_begin,
257 conversation_t *conv;
260 u_int c_data_port, c_mon_port;
261 u_int s_data_port, s_mon_port;
264 if (line_len > sizeof(buf) - 1) {
266 * Don't overflow the buffer.
268 line_len = sizeof(buf) - 1;
270 memcpy(buf, line_begin, line_len);
271 buf[line_len] = '\0';
273 tmp = buf + STRLEN_CONST(rtsp_transport);
274 while (*tmp && isspace(*tmp))
276 if (strncasecmp(tmp, rtsp_rtp, strlen(rtsp_rtp)) != 0) {
277 g_warning("Frame %u: rtsp: unknown transport", pinfo->fd->num);
281 c_data_port = c_mon_port = 0;
282 s_data_port = s_mon_port = 0;
283 if ((tmp = strstr(buf, rtsp_sps))) {
284 tmp += strlen(rtsp_sps);
285 if (sscanf(tmp, "%u-%u", &s_data_port, &s_mon_port) < 1) {
286 g_warning("Frame %u: rtsp: bad server_port",
291 if ((tmp = strstr(buf, rtsp_cps))) {
292 tmp += strlen(rtsp_cps);
293 if (sscanf(tmp, "%u-%u", &c_data_port, &c_mon_port) < 1) {
294 g_warning("Frame %u: rtsp: bad client_port",
300 rtsp_conversation_data_t *data;
301 u_int s_data_chan, s_mon_chan;
305 * Deal with RTSP TCP-interleaved conversations.
307 if ((tmp = strstr(buf, rtsp_inter)) == NULL) {
309 * No interleaved or server_port - probably a
310 * SETUP request, rather than reply.
314 tmp += strlen(rtsp_inter);
315 i = sscanf(tmp, "%u-%u", &s_data_chan, &s_mon_chan);
317 g_warning("Frame %u: rtsp: bad interleaved",
321 conv = find_conversation(&pinfo->src, &pinfo->dst, pinfo->ptype,
322 pinfo->srcport, pinfo->destport, 0);
324 conv = conversation_new(&pinfo->src, &pinfo->dst,
325 pinfo->ptype, pinfo->srcport, pinfo->destport,
328 data = conversation_get_proto_data(conv, proto_rtsp);
330 data = g_mem_chunk_alloc(rtsp_vals);
331 conversation_add_proto_data(conv, proto_rtsp, data);
333 if (s_data_chan < RTSP_MAX_INTERLEAVED) {
334 data->interleaved[s_data_chan].dissector =
337 if (i > 1 && s_mon_chan < RTSP_MAX_INTERLEAVED) {
338 data->interleaved[s_mon_chan].dissector =
345 * We only want to match on the destination address, not the
346 * source address, because the server might send back a packet
347 * from an address other than the address to which its client
348 * sent the packet, so we construct a conversation with no
351 SET_ADDRESS(&null_addr, pinfo->src.type, 0, NULL);
353 conv = conversation_new(&pinfo->dst, &null_addr, PT_UDP, c_data_port,
354 s_data_port, NO_ADDR2 | (!s_data_port ? NO_PORT2 : 0));
355 conversation_set_dissector(conv, rtp_handle);
360 conv = conversation_new(&pinfo->dst, &null_addr, PT_UDP, c_mon_port,
361 s_mon_port, NO_ADDR2 | (!s_mon_port ? NO_PORT2 : 0));
362 conversation_set_dissector(conv, rtcp_handle);
365 static const char rtsp_content_length[] = "Content-Length:";
368 rtsp_get_content_length(const u_char *line_begin, size_t line_len)
376 if (line_len > sizeof(buf) - 1) {
378 * Don't overflow the buffer.
380 line_len = sizeof(buf) - 1;
382 memcpy(buf, line_begin, line_len);
383 buf[line_len] = '\0';
385 tmp = buf + STRLEN_CONST(rtsp_content_length);
386 while (*tmp && isspace(*tmp))
388 content_length = strtol(tmp, &p, 10);
390 if (up == tmp || (*up != '\0' && !isspace(*up)))
391 return -1; /* not a valid number */
392 return content_length;
396 dissect_rtspmessage(tvbuff_t *tvb, int offset, packet_info *pinfo,
399 proto_tree *rtsp_tree;
400 proto_item *ti = NULL;
403 const u_char *linep, *lineend;
407 gboolean is_mime_header;
411 int reported_datalen;
413 orig_offset = offset;
416 ti = proto_tree_add_item(tree, proto_rtsp, tvb, offset,
417 tvb_length_remaining(tvb, offset), FALSE);
418 rtsp_tree = proto_item_add_subtree(ti, ett_rtsp);
421 if (check_col(pinfo->cinfo, COL_INFO)) {
423 * Put the first line from the buffer into the summary
424 * if it's an RTSP request or reply (but leave out the
426 * Otherwise, just call it a continuation.
428 linelen = tvb_find_line_end(tvb, offset, -1, &next_offset);
429 line = tvb_get_ptr(tvb, offset, linelen);
430 switch (is_rtsp_request_or_reply(line, linelen)) {
434 col_add_str(pinfo->cinfo, COL_INFO,
435 format_text(line, linelen));
439 col_set_str(pinfo->cinfo, COL_INFO, "Continuation");
445 * We haven't yet seen a Content-Length header.
450 * Process the packet data, a line at a time.
452 while (tvb_offset_exists(tvb, offset)) {
454 * We haven't yet concluded that this is a MIME header.
456 is_mime_header = FALSE;
459 * Find the end of the line.
461 linelen = tvb_find_line_end(tvb, offset, -1, &next_offset);
464 * Get a buffer that refers to the line.
466 line = tvb_get_ptr(tvb, offset, linelen);
467 lineend = line + linelen;
470 * OK, does it look like an RTSP request or
473 switch (is_rtsp_request_or_reply(line, linelen)) {
476 if (rtsp_tree != NULL)
477 process_rtsp_request(tvb, offset, line, linelen,
482 if (rtsp_tree != NULL)
483 process_rtsp_reply(tvb, offset, line, linelen,
492 * No. Does it look like a blank line (as would
493 * appear at the end of an RTSP request)?
496 goto is_rtsp; /* Yes. */
499 * No. Does it look like a MIME header?
502 while (linep < lineend) {
505 break; /* not printable, not a MIME header */
525 * It's a tspecial, so it's not
526 * part of a token, so it's not
527 * a field name for the beginning
534 * This ends the token; we consider
535 * this to be a MIME header.
537 is_mime_header = TRUE;
543 * LWS (RFC-2616, 4.2); continue the previous
552 * We don't consider this part of an RTSP request or
553 * reply, so we don't display it.
562 proto_tree_add_text(rtsp_tree, tvb, offset,
563 next_offset - offset, "%s",
564 tvb_format_text(tvb, offset, next_offset - offset));
566 if (is_mime_header) {
568 * Process some MIME headers specially.
570 #define MIME_HDR_MATCHES(header) \
571 (linelen > STRLEN_CONST(header) && \
572 strncasecmp(line, (header), STRLEN_CONST(header)) == 0)
574 if (MIME_HDR_MATCHES(rtsp_transport)) {
576 * Based on the port numbers specified
577 * in the Transport: header, set up
578 * a conversation that will be dissected
579 * with the appropriate dissector.
581 rtsp_create_conversation(pinfo, line, linelen);
582 } else if (MIME_HDR_MATCHES(rtsp_content_type)) {
584 * If the Content-Type: header says this
585 * is SDP, dissect the payload as SDP.
587 if (is_content_sdp(line, linelen))
589 } else if (MIME_HDR_MATCHES(rtsp_content_length)) {
591 * Only the amount specified by the
592 * Content-Length: header should be treated
595 content_length = rtsp_get_content_length(line,
599 offset = next_offset;
603 * If a content length was supplied, the amount of data to be
604 * processed as RTSP payload is the minimum of the content
605 * length and the amount of data remaining in the frame.
607 * If no content length was supplied, the amount of data to be
608 * processed is the amount of data remaining in the frame.
610 datalen = tvb_length_remaining(tvb, offset);
611 if (content_length != -1) {
612 if (datalen > content_length)
613 datalen = content_length;
616 * XXX - for now, if the content length is greater
617 * than the amount of data left in this frame (not
618 * the amount of *captured* data left in the frame
619 * minus the current offset, but the amount of *actual*
620 * data that was reported to be in the frame minus
621 * the current offset), limit it to the amount
622 * of data left in this frame.
624 * If we ever handle data that crosses frame
625 * boundaries, we'll need to remember the actual
628 reported_datalen = tvb_reported_length_remaining(tvb, offset);
629 if (content_length > reported_datalen)
630 content_length = reported_datalen;
635 * There's stuff left over; process it.
641 * Fix up the top-level item so that it doesn't
642 * include the SDP stuff.
645 proto_item_set_len(ti, offset);
648 * Now create a tvbuff for the SDP stuff and
651 * The amount of data to be processed that's
652 * available in the tvbuff is "datalen", which
653 * is the minimum of the amount of data left in
654 * the tvbuff and any specified content length.
656 * The amount of data to be processed that's in
657 * this frame, regardless of whether it was
658 * captured or not, is "content_length",
659 * which, if no content length was specified,
660 * is -1, i.e. "to the end of the frame.
662 new_tvb = tvb_new_subset(tvb, offset, datalen,
664 call_dissector(sdp_handle, new_tvb, pinfo, tree);
666 if (tvb_get_guint8(tvb, offset) == RTSP_FRAMEHDR) {
668 * This is interleaved stuff; don't
669 * treat it as raw data - set "datalen"
670 * to 0, so we won't skip the offset
671 * past it, which will cause our
672 * caller to process that stuff itself.
676 proto_tree_add_text(rtsp_tree, tvb, offset,
677 datalen, "Data (%d bytes)", datalen);
682 * We've processed "datalen" bytes worth of data
683 * (which may be no data at all); advance the
684 * offset past whatever data we've processed, so they
689 return offset - orig_offset;
693 process_rtsp_request(tvbuff_t *tvb, int offset, const u_char *data,
694 size_t linelen, proto_tree *tree)
696 const u_char *lineend = data + linelen;
699 const u_char *url_start;
702 /* Request Methods */
703 for (ii = 0; ii < RTSP_NMETHODS; ii++) {
704 size_t len = strlen(rtsp_methods[ii]);
705 if (linelen >= len && !strncasecmp(rtsp_methods[ii], data, len))
708 if (ii == RTSP_NMETHODS) {
710 * We got here because "is_rtsp_request_or_reply()" returned
711 * RTSP_REQUEST, so we know one of the request methods
712 * matched, so we "can't get here".
714 g_assert_not_reached();
718 proto_tree_add_string_hidden(tree, hf_rtsp_method, tvb, offset,
719 strlen(rtsp_methods[ii]), rtsp_methods[ii]);
723 while (url < lineend && !isspace(*url))
725 while (url < lineend && isspace(*url))
728 while (url < lineend && !isspace(*url))
730 tmp_url = g_malloc(url - url_start + 1);
731 memcpy(tmp_url, url_start, url - url_start);
732 tmp_url[url - url_start] = 0;
733 proto_tree_add_string_hidden(tree, hf_rtsp_url, tvb,
734 offset + (url_start - data), url - url_start, tmp_url);
739 process_rtsp_reply(tvbuff_t *tvb, int offset, const u_char *data,
740 size_t linelen, proto_tree *tree)
742 const u_char *lineend = data + linelen;
743 const u_char *status = data;
744 const u_char *status_start;
745 unsigned int status_i;
748 while (status < lineend && !isspace(*status))
750 while (status < lineend && isspace(*status))
752 status_start = status;
754 while (status < lineend && isdigit(*status))
755 status_i = status_i * 10 + *status++ - '0';
756 proto_tree_add_uint_hidden(tree, hf_rtsp_status, tvb,
757 offset + (status_start - data),
758 status - status_start, status_i);
762 dissect_rtsp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
767 if (check_col(pinfo->cinfo, COL_PROTOCOL))
768 col_set_str(pinfo->cinfo, COL_PROTOCOL, "RTSP");
769 if (check_col(pinfo->cinfo, COL_INFO))
770 col_clear(pinfo->cinfo, COL_INFO);
772 while (tvb_offset_exists(tvb, offset)) {
773 len = (tvb_get_guint8(tvb, offset) == RTSP_FRAMEHDR)
774 ? dissect_rtspinterleaved(tvb, offset, pinfo, tree)
775 : dissect_rtspmessage(tvb, offset, pinfo, tree);
781 * OK, we've set the Protocol and Info columns for the
782 * first RTSP message; make the columns non-writable,
783 * so that we don't change it for subsequent RTSP messages.
785 col_set_writable(pinfo->cinfo, FALSE);
792 /* Routine to initialize rtsp protocol before each capture or filter pass. */
793 /* Release any memory if needed. Then setup the memory chunks. */
796 g_mem_chunk_destroy(rtsp_vals);
798 rtsp_vals = g_mem_chunk_new("rtsp_vals",
799 sizeof(rtsp_conversation_data_t),
800 rtsp_hash_init_count * sizeof(rtsp_conversation_data_t),
805 proto_register_rtsp(void)
807 static gint *ett[] = {
811 static hf_register_info hf[] = {
813 { "Method", "rtsp.method", FT_STRING, BASE_NONE, NULL, 0, "", HFILL }},
815 { "URL", "rtsp.url", FT_STRING, BASE_NONE, NULL, 0, "", HFILL }},
817 { "Status", "rtsp.status", FT_UINT32, BASE_DEC, NULL, 0, "", HFILL }},
820 proto_rtsp = proto_register_protocol("Real Time Streaming Protocol",
822 proto_register_field_array(proto_rtsp, hf, array_length(hf));
823 proto_register_subtree_array(ett, array_length(ett));
825 register_init_routine(rtsp_init); /* register re-init routine */
829 proto_reg_handoff_rtsp(void)
831 dissector_handle_t rtsp_handle;
833 rtsp_handle = create_dissector_handle(dissect_rtsp, proto_rtsp);
834 dissector_add("tcp.port", TCP_PORT_RTSP, rtsp_handle);
836 sdp_handle = find_dissector("sdp");
837 rtp_handle = find_dissector("rtp");
838 rtcp_handle = find_dissector("rtcp");