Support for conversations with "wildcard" destination addresses, from
[obnox/wireshark/wip.git] / packet-rtsp.c
1 /* packet-rtsp.c
2  * Routines for RTSP packet disassembly (RFC 2326)
3  *
4  * Jason Lango <jal@netapp.com>
5  * Liberally copied from packet-http.c, by Guy Harris <guy@alum.mit.edu>
6  *
7  * $Id: packet-rtsp.c,v 1.21 2000/10/21 05:52:22 guy Exp $
8  *
9  * Ethereal - Network traffic analyzer
10  * By Gerald Combs <gerald@zing.org>
11  * Copyright 1998 Gerald Combs
12  *
13  * 
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.
18  * 
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.
23  * 
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.
27  *
28  *
29  */
30
31 #include "config.h"
32
33 #ifdef HAVE_SYS_TYPES_H
34 #include <sys/types.h>
35 #endif
36
37 #include <string.h>
38 #include <ctype.h>
39
40 #include <glib.h>
41 #include "packet.h"
42 #include "packet-sdp.h"
43 #include "packet-rtp.h"
44 #include "packet-rtcp.h"
45 #include "conversation.h"
46 #include "strutil.h"
47
48 static int proto_rtsp = -1;
49 static gint ett_rtsp = -1;
50
51 static int hf_rtsp_method = -1;
52 static int hf_rtsp_url = -1;
53 static int hf_rtsp_status = -1;
54
55 #define TCP_PORT_RTSP                   554
56
57 static int process_rtsp_request_or_reply(const u_char *data, int offset,
58         int linelen, proto_tree *tree);
59
60 static int
61 is_content_sdp(const u_char *line, int linelen)
62 {
63         const char      *hdr = "Content-Type:";
64         size_t          hdrlen = strlen(hdr);
65         const char      *type = "application/sdp";
66         size_t          typelen = strlen(type);
67
68         if (linelen < hdrlen || strncasecmp(hdr, line, hdrlen))
69                 return 0;
70
71         line += hdrlen;
72         linelen -= hdrlen;
73         while (linelen > 0 && (*line == ' ' || *line == '\t')) {
74                 line++;
75                 linelen--;
76         }
77
78         if (linelen < typelen || strncasecmp(type, line, typelen))
79                 return 0;
80
81         line += typelen;
82         linelen -= typelen;
83         if (linelen > 0 && !isspace(*line))
84                 return 0;
85
86         return 1;
87 }
88
89 static const char rtsp_transport[] = "Transport:";
90 static const char rtsp_sps[] = "server_port=";
91 static const char rtsp_cps[] = "client_port=";
92 static const char rtsp_rtp[] = "rtp/avp";
93
94 static void
95 rtsp_create_conversation(const u_char *trans_begin, const u_char *trans_end)
96 {
97         conversation_t  *conv;
98         u_char          tbuf[256];
99         u_char          *tmp;
100         int             c_data_port, c_mon_port;
101         int             s_data_port, s_mon_port;
102
103         strncpy(tbuf, trans_begin, trans_end - trans_begin);
104         tbuf[sizeof(tbuf)-1] = 0;
105
106         tmp = tbuf + strlen(rtsp_transport);
107         while (*tmp && isspace(*tmp))
108                 tmp++;
109         if (strncasecmp(tmp, rtsp_rtp, strlen(rtsp_rtp)) != 0)
110                 return;
111
112         c_data_port = c_mon_port = 0;
113         s_data_port = s_mon_port = 0;
114         if ((tmp = strstr(tbuf, rtsp_sps))) {
115                 tmp += strlen(rtsp_sps);
116                 if (sscanf(tmp, "%u-%u", &s_data_port, &s_mon_port) < 1)
117                         g_warning("rtsp: failed to parse server_port");
118         }
119         if ((tmp = strstr(tbuf, rtsp_cps))) {
120                 tmp += strlen(rtsp_cps);
121                 if (sscanf(tmp, "%u-%u", &c_data_port, &c_mon_port) < 1)
122                         g_warning("rtsp: failed to parse client_port");
123         }
124         if (!c_data_port || !s_data_port)
125                 return;
126
127         conv = conversation_new(&pi.src, &pi.dst, PT_UDP, s_data_port,
128                 c_data_port, 0, 0);
129         conversation_set_dissector(conv, dissect_rtp);
130
131         if (!c_mon_port || !s_mon_port)
132                 return;
133
134         conv = conversation_new(&pi.src, &pi.dst, PT_UDP, s_mon_port,
135                 c_mon_port, 0, 0);
136         conversation_set_dissector(conv, dissect_rtcp);
137 }
138
139 static void dissect_rtsp(const u_char *pd, int offset, frame_data *fd,
140         proto_tree *tree)
141 {
142         proto_tree      *rtsp_tree;
143         proto_item      *ti;
144         const u_char    *data, *dataend;
145         const u_char    *linep, *lineend, *eol;
146         int             linelen;
147         u_char          c;
148         int             is_sdp = 0;
149         int             end_offset;
150
151         OLD_CHECK_DISPLAY_AS_DATA(proto_rtsp, pd, offset, fd, tree);
152
153         data = &pd[offset];
154         dataend = data + END_OF_FRAME;
155         end_offset = offset + END_OF_FRAME;
156
157         rtsp_tree = NULL;
158         if (tree) {
159                 ti = proto_tree_add_item(tree, proto_rtsp, NullTVB, offset, 
160                         END_OF_FRAME, FALSE);
161                 rtsp_tree = proto_item_add_subtree(ti, ett_rtsp);
162         }
163
164         if (check_col(fd, COL_PROTOCOL))
165                 col_add_str(fd, COL_PROTOCOL, "RTSP");
166         if (check_col(fd, COL_INFO)) {
167                 /*
168                  * Put the first line from the buffer into the summary
169                  * if it's an RTSP request or reply. Otherwise, just call 
170                  * it a continuation.
171                  */
172                 lineend = find_line_end(data, dataend, &eol);
173                 linelen = lineend - data;
174                 if (process_rtsp_request_or_reply(data, offset, linelen,
175                                 rtsp_tree)) {
176                         col_add_str(fd, COL_INFO,
177                             format_text(data, eol - data));
178                 } else
179                         col_add_str(fd, COL_INFO, "Continuation");
180         }
181
182         if (offset >= end_offset)
183                 goto bad_len;
184         while (data < dataend) {
185                 /*
186                  * Find the end of the line.
187                  */
188                 lineend = find_line_end(data, dataend, &eol);
189                 linelen = lineend - data;
190
191                 /*
192                  * OK, does it look like an RTSP request or
193                  * response?
194                  */
195                 if (process_rtsp_request_or_reply(data, offset, linelen,
196                                 rtsp_tree))
197                         goto is_rtsp;
198
199                 /*
200                  * No.  Does it look like a blank line (as would
201                  * appear at the end of an RTSP request)?
202                  */
203                 if (linelen == 1) {
204                         if (*data == '\n')
205                                 goto is_rtsp;
206                 }
207                 if (linelen == 2) {
208                         if (strncmp(data, "\r\n", 2) == 0 ||
209                             strncmp(data, "\n\r", 2) == 0)
210                                 goto is_rtsp;
211                 }
212
213                 /*
214                  * No.  Does it look like a MIME header?
215                  */
216                 linep = data;
217                 while (linep < lineend) {
218                         c = *linep++;
219                         if (!isprint(c))
220                                 break;  /* not printable, not a MIME header */
221                         switch (c) {
222
223                         case '(':
224                         case ')':
225                         case '<':
226                         case '>':
227                         case '@':
228                         case ',':
229                         case ';':
230                         case '\\':
231                         case '"':
232                         case '/':
233                         case '[':
234                         case ']':
235                         case '?':
236                         case '=':
237                         case '{':
238                         case '}':
239                                 /*
240                                  * It's a tspecial, so it's not
241                                  * part of a token, so it's not
242                                  * a field name for the beginning
243                                  * of a MIME header.
244                                  */
245                                 goto not_rtsp;
246
247                         case ':':
248                                 /*
249                                  * This ends the token; we consider
250                                  * this to be a MIME header.
251                                  */
252                                 if (is_content_sdp(data, linelen))
253                                         is_sdp = 1;
254                                 goto is_rtsp;
255                         }
256                 }
257
258         not_rtsp:
259                 /*
260                  * We don't consider this part of an RTSP request or
261                  * reply, so we don't display it.
262                  */
263                 break;
264
265         is_rtsp:
266                 /*
267                  * Put this line.
268                  */
269                 if (rtsp_tree) {
270                         proto_tree_add_text(rtsp_tree, NullTVB, offset, linelen, "%s",
271                                 format_text(data, linelen));
272                 }
273                 if (linelen > strlen(rtsp_transport) &&
274                         strncasecmp(data, rtsp_transport,
275                                 strlen(rtsp_transport)) == 0)
276                         rtsp_create_conversation(data, data + linelen);
277                 offset += linelen;
278                 data = lineend;
279         }
280
281         if (is_sdp) {
282                 dissect_sdp(pd, offset, fd, tree);
283                 if (check_col(fd, COL_PROTOCOL))
284                         col_add_str(fd, COL_PROTOCOL, "RTSP/SDP");
285         }
286         else if (data < dataend) {
287                 proto_tree_add_text(rtsp_tree, NullTVB, offset, END_OF_FRAME,
288                     "Data (%d bytes)", END_OF_FRAME);
289         }
290         return;
291
292 bad_len:
293         proto_tree_add_text(rtsp_tree, NullTVB, end_offset, 0,
294                 "Unexpected end of packet");
295 }
296
297 const char *rtsp_methods[] = {
298         "DESCRIBE", "ANNOUNCE", "GET_PARAMETER", "OPTIONS",
299         "PAUSE", "PLAY", "RECORD", "REDIRECT", "SETUP",
300         "SET_PARAMETER", "TEARDOWN"
301 };
302 const int rtsp_nmethods = sizeof(rtsp_methods) / sizeof(*rtsp_methods);
303
304 static int
305 process_rtsp_request_or_reply(const u_char *data, int offset, int linelen,
306         proto_tree *tree)
307 {
308         int             ii;
309         const u_char    *lineend = data + linelen;
310
311         /* Reply */
312         if (linelen >= 5 && !strncasecmp("RTSP/", data, 5)) {
313                 if (tree) {
314                         /* status code */
315                         const u_char *status = data;
316                         const u_char *status_start;
317                         unsigned int status_i = 0;
318                         while (status < lineend && !isspace(*status))
319                                 status++;
320                         while (status < lineend && isspace(*status))
321                                 status++;
322                         status_start = status;
323                         while (status < lineend && isdigit(*status))
324                                 status_i = status_i * 10 + *status++ - '0';
325                         proto_tree_add_uint_hidden(tree, hf_rtsp_status, NullTVB,
326                                 offset + (status_start - data),
327                                 status - status_start, status_i);
328                 }
329                 return TRUE;
330         }
331
332         /* Request Methods */
333         for (ii = 0; ii < rtsp_nmethods; ii++) {
334                 size_t len = strlen(rtsp_methods[ii]);
335                 if (linelen >= len && !strncasecmp(rtsp_methods[ii], data, len))
336                         break;
337         }
338         if (ii == rtsp_nmethods)
339                 return FALSE;
340
341         if (tree) {
342                 const u_char *url;
343                 const u_char *url_start;
344                 u_char *tmp_url;
345
346                 /* method name */
347                 proto_tree_add_string_hidden(tree, hf_rtsp_method, NullTVB, offset,
348                         strlen(rtsp_methods[ii]), rtsp_methods[ii]);
349
350                 /* URL */
351                 url = data;
352                 while (url < lineend && !isspace(*url))
353                         url++;
354                 while (url < lineend && isspace(*url))
355                         url++;
356                 url_start = url;
357                 while (url < lineend && !isspace(*url))
358                         url++;
359                 tmp_url = g_malloc(url - url_start + 1);
360                 memcpy(tmp_url, url_start, url - url_start);
361                 tmp_url[url - url_start] = 0;
362                 proto_tree_add_string_hidden(tree, hf_rtsp_url, NullTVB,
363                         offset + (url_start - data), url - url_start, tmp_url);
364                 g_free(tmp_url);
365         }
366         return TRUE;
367 }
368
369 void
370 proto_register_rtsp(void)
371 {
372         static gint *ett[] = {
373                 &ett_rtsp,
374         };
375         static hf_register_info hf[] = {
376         { &hf_rtsp_method,
377         { "Method", "rtsp.method", FT_STRING, BASE_NONE, NULL, 0 }},
378         { &hf_rtsp_url,
379         { "URL", "rtsp.url", FT_STRING, BASE_NONE, NULL, 0 }},
380         { &hf_rtsp_status,
381         { "Status", "rtsp.status", FT_UINT32, BASE_DEC, NULL, 0 }},
382         };
383
384         proto_rtsp = proto_register_protocol("Real Time Streaming Protocol",
385                 "rtsp");
386         proto_register_field_array(proto_rtsp, hf, array_length(hf));
387         proto_register_subtree_array(ett, array_length(ett));
388 }
389
390 void
391 proto_reg_handoff_rtsp(void)
392 {
393         old_dissector_add("tcp.port", TCP_PORT_RTSP, dissect_rtsp);
394 }