Add tvbuff class.
[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.13 2000/05/11 08:15:43 gram 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
47 static int proto_rtsp = -1;
48 static gint ett_rtsp = -1;
49
50 static int hf_rtsp_method = -1;
51 static int hf_rtsp_url = -1;
52 static int hf_rtsp_status = -1;
53
54 #define TCP_PORT_RTSP                   554
55
56 static int process_rtsp_request_or_reply(const u_char *data, int offset,
57         int linelen, proto_tree *tree);
58
59 static int
60 is_content_sdp(const u_char *line, int linelen)
61 {
62         const char      *hdr = "Content-Type:";
63         size_t          hdrlen = strlen(hdr);
64         const char      *type = "application/sdp";
65         size_t          typelen = strlen(type);
66
67         if (linelen < hdrlen || strncasecmp(hdr, line, hdrlen))
68                 return 0;
69
70         line += hdrlen;
71         linelen -= hdrlen;
72         while (linelen > 0 && (*line == ' ' || *line == '\t')) {
73                 line++;
74                 linelen--;
75         }
76
77         if (linelen < typelen || strncasecmp(type, line, typelen))
78                 return 0;
79
80         line += typelen;
81         linelen -= typelen;
82         if (linelen > 0 && !isspace(*line))
83                 return 0;
84
85         return 1;
86 }
87
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";
92
93 static void
94 rtsp_create_conversation(const u_char *trans_begin, const u_char *trans_end)
95 {
96         conversation_t  *conv;
97         u_char          tbuf[256];
98         u_char          *tmp;
99         int             c_data_port, c_mon_port;
100         int             s_data_port, s_mon_port;
101
102         strncpy(tbuf, trans_begin, trans_end - trans_begin);
103         tbuf[sizeof(tbuf)-1] = 0;
104
105         tmp = tbuf + strlen(rtsp_transport);
106         while (*tmp && isspace(*tmp))
107                 tmp++;
108         if (strncasecmp(tmp, rtsp_rtp, strlen(rtsp_rtp)) != 0)
109                 return;
110
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");
117         }
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");
122         }
123         if (!c_data_port || !s_data_port)
124                 return;
125
126         conv = conversation_new(&pi.src, &pi.dst, PT_UDP, s_data_port,
127                 c_data_port, 0);
128         conv->dissector = dissect_rtp;
129
130         if (!c_mon_port || !s_mon_port)
131                 return;
132
133         conv = conversation_new(&pi.src, &pi.dst, PT_UDP, s_mon_port,
134                 c_mon_port, 0);
135         conv->dissector = dissect_rtcp;
136 }
137
138 static void dissect_rtsp(const u_char *pd, int offset, frame_data *fd,
139         proto_tree *tree)
140 {
141         proto_tree      *rtsp_tree;
142         proto_item      *ti;
143         const u_char    *data, *dataend;
144         const u_char    *linep, *lineend, *eol;
145         int             linelen;
146         u_char          c;
147         int             is_sdp = 0;
148         int             end_offset;
149
150         data = &pd[offset];
151         dataend = data + END_OF_FRAME;
152         end_offset = offset + END_OF_FRAME;
153
154         rtsp_tree = NULL;
155         if (tree) {
156                 ti = proto_tree_add_item(tree, proto_rtsp, NullTVB, offset, 
157                         END_OF_FRAME, NULL);
158                 rtsp_tree = proto_item_add_subtree(ti, ett_rtsp);
159         }
160
161         if (check_col(fd, COL_PROTOCOL))
162                 col_add_str(fd, COL_PROTOCOL, "RTSP");
163         if (check_col(fd, COL_INFO)) {
164                 /*
165                  * Put the first line from the buffer into the summary
166                  * if it's an RTSP request or reply. Otherwise, just call 
167                  * it a continuation.
168                  */
169                 lineend = find_line_end(data, dataend, &eol);
170                 linelen = lineend - data;
171                 if (process_rtsp_request_or_reply(data, offset, linelen,
172                                 rtsp_tree))
173                         col_add_str(fd, COL_INFO, format_text(data, linelen));
174                 else
175                         col_add_str(fd, COL_INFO, "Continuation");
176         }
177
178         if (offset >= end_offset)
179                 goto bad_len;
180         while (data < dataend) {
181                 /*
182                  * Find the end of the line.
183                  */
184                 lineend = find_line_end(data, dataend, &eol);
185                 linelen = lineend - data;
186
187                 /*
188                  * OK, does it look like an RTSP request or
189                  * response?
190                  */
191                 if (process_rtsp_request_or_reply(data, offset, linelen,
192                                 rtsp_tree))
193                         goto is_rtsp;
194
195                 /*
196                  * No.  Does it look like a blank line (as would
197                  * appear at the end of an RTSP request)?
198                  */
199                 if (linelen == 1) {
200                         if (*data == '\n')
201                                 goto is_rtsp;
202                 }
203                 if (linelen == 2) {
204                         if (strncmp(data, "\r\n", 2) == 0 ||
205                             strncmp(data, "\n\r", 2) == 0)
206                                 goto is_rtsp;
207                 }
208
209                 /*
210                  * No.  Does it look like a MIME header?
211                  */
212                 linep = data;
213                 while (linep < lineend) {
214                         c = *linep++;
215                         if (!isprint(c))
216                                 break;  /* not printable, not a MIME header */
217                         switch (c) {
218
219                         case '(':
220                         case ')':
221                         case '<':
222                         case '>':
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                                 /*
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
239                                  * of a MIME header.
240                                  */
241                                 goto not_rtsp;
242
243                         case ':':
244                                 /*
245                                  * This ends the token; we consider
246                                  * this to be a MIME header.
247                                  */
248                                 if (is_content_sdp(data, linelen))
249                                         is_sdp = 1;
250                                 goto is_rtsp;
251                         }
252                 }
253
254         not_rtsp:
255                 /*
256                  * We don't consider this part of an RTSP request or
257                  * reply, so we don't display it.
258                  */
259                 break;
260
261         is_rtsp:
262                 /*
263                  * Put this line.
264                  */
265                 if (rtsp_tree) {
266                         proto_tree_add_text(rtsp_tree, NullTVB, offset, linelen, "%s",
267                                 format_text(data, linelen));
268                 }
269                 if (linelen > strlen(rtsp_transport) &&
270                         strncasecmp(data, rtsp_transport,
271                                 strlen(rtsp_transport)) == 0)
272                         rtsp_create_conversation(data, data + linelen);
273                 offset += linelen;
274                 data = lineend;
275         }
276
277         if (is_sdp) {
278                 dissect_sdp(pd, offset, fd, tree);
279                 if (check_col(fd, COL_PROTOCOL))
280                         col_add_str(fd, COL_PROTOCOL, "RTSP/SDP");
281         }
282         else if (data < dataend) {
283                 proto_tree_add_text(rtsp_tree, NullTVB, offset, END_OF_FRAME,
284                     "Data (%d bytes)", END_OF_FRAME);
285         }
286         return;
287
288 bad_len:
289         proto_tree_add_text(rtsp_tree, NullTVB, end_offset, 0,
290                 "Unexpected end of packet");
291 }
292
293 const char *rtsp_methods[] = {
294         "DESCRIBE", "ANNOUNCE", "GET_PARAMETER", "OPTIONS",
295         "PAUSE", "PLAY", "RECORD", "REDIRECT", "SETUP",
296         "SET_PARAMETER", "TEARDOWN"
297 };
298 const int rtsp_nmethods = sizeof(rtsp_methods) / sizeof(*rtsp_methods);
299
300 static int
301 process_rtsp_request_or_reply(const u_char *data, int offset, int linelen,
302         proto_tree *tree)
303 {
304         int             ii;
305         const u_char    *lineend = data + linelen;
306
307         /* Reply */
308         if (linelen >= 5 && !strncasecmp("RTSP/", data, 5)) {
309                 if (tree) {
310                         /* status code */
311                         const u_char *status = data;
312                         const u_char *status_start;
313                         unsigned int status_i = 0;
314                         while (status < lineend && !isspace(*status))
315                                 status++;
316                         while (status < lineend && isspace(*status))
317                                 status++;
318                         status_start = status;
319                         while (status < lineend && isdigit(*status))
320                                 status_i = status_i * 10 + *status++ - '0';
321                         proto_tree_add_item_hidden(tree, hf_rtsp_status, NullTVB,
322                                 offset + (status_start - data),
323                                 status - status_start, status_i);
324                 }
325                 return TRUE;
326         }
327
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))
332                         break;
333         }
334         if (ii == rtsp_nmethods)
335                 return FALSE;
336
337         if (tree) {
338                 const u_char *url;
339                 const u_char *url_start;
340                 u_char *tmp_url;
341
342                 /* method name */
343                 proto_tree_add_item_hidden(tree, hf_rtsp_method, NullTVB, offset,
344                         strlen(rtsp_methods[ii]), rtsp_methods[ii]);
345
346                 /* URL */
347                 url = data;
348                 while (url < lineend && !isspace(*url))
349                         url++;
350                 while (url < lineend && isspace(*url))
351                         url++;
352                 url_start = url;
353                 while (url < lineend && !isspace(*url))
354                         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_item_hidden(tree, hf_rtsp_url, NullTVB,
359                         offset + (url_start - data), url - url_start, tmp_url);
360                 g_free(tmp_url);
361         }
362         return TRUE;
363 }
364
365 void
366 proto_register_rtsp(void)
367 {
368         static gint *ett[] = {
369                 &ett_rtsp,
370         };
371         static hf_register_info hf[] = {
372         { &hf_rtsp_method,
373         { "Method", "rtsp.method", FT_STRING, BASE_NONE, NULL, 0 }},
374         { &hf_rtsp_url,
375         { "URL", "rtsp.url", FT_STRING, BASE_NONE, NULL, 0 }},
376         { &hf_rtsp_status,
377         { "Status", "rtsp.status", FT_UINT32, BASE_DEC, NULL, 0 }},
378         };
379
380         proto_rtsp = proto_register_protocol("Real Time Streaming Protocol",
381                 "rtsp");
382         proto_register_field_array(proto_rtsp, hf, array_length(hf));
383         proto_register_subtree_array(ett, array_length(ett));
384 }
385
386 void
387 proto_reg_handoff_rtsp(void)
388 {
389         dissector_add("tcp.port", TCP_PORT_RTSP, dissect_rtsp);
390 }