If a PrincipalName has at least one name-string, put the first of the
[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.29 2000/12/02 06:05:29 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 #include <stdlib.h>
40
41 #include <glib.h>
42 #include "packet.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 /*
58  * Takes an array of bytes, assumed to contain a null-terminated
59  * string, as an argument, and returns the length of the string -
60  * i.e., the size of the array, minus 1 for the null terminator.
61  */
62 #define STRLEN_CONST(str)       (sizeof (str) - 1)
63
64 static void process_rtsp_request(tvbuff_t *tvb, int offset, const u_char *data,
65         int linelen, proto_tree *tree);
66
67 static void process_rtsp_reply(tvbuff_t *tvb, int offset, const u_char *data,
68         int linelen, proto_tree *tree);
69
70 typedef enum {
71         RTSP_REQUEST,
72         RTSP_REPLY,
73         NOT_RTSP
74 } rtsp_type_t;
75
76 static const char *rtsp_methods[] = {
77         "DESCRIBE", "ANNOUNCE", "GET_PARAMETER", "OPTIONS",
78         "PAUSE", "PLAY", "RECORD", "REDIRECT", "SETUP",
79         "SET_PARAMETER", "TEARDOWN"
80 };
81
82 #define RTSP_NMETHODS   (sizeof rtsp_methods / sizeof rtsp_methods[0])
83
84 static dissector_handle_t sdp_handle;
85
86 static rtsp_type_t
87 is_rtsp_request_or_reply(const u_char *line, int linelen)
88 {
89         int             ii;
90
91         /* Is this an RTSP reply? */
92         if (linelen >= 5 && strncasecmp("RTSP/", line, 5) == 0) {
93                 /*
94                  * Yes.
95                  */
96                 return RTSP_REPLY;
97         }
98
99         /*
100          * Is this an RTSP request?
101          * Check whether the line begins with one of the RTSP request
102          * methods.
103          */
104         for (ii = 0; ii < RTSP_NMETHODS; ii++) {
105                 size_t len = strlen(rtsp_methods[ii]);
106                 if (linelen >= len &&
107                     strncasecmp(rtsp_methods[ii], line, len) == 0)
108                         return RTSP_REQUEST;
109         }
110         return NOT_RTSP;
111 }
112
113 static const char rtsp_content_type[] = "Content-Type:";
114
115 static int
116 is_content_sdp(const u_char *line, int linelen)
117 {
118         static const char type[] = "application/sdp";
119         size_t          typelen = STRLEN_CONST(type);
120
121         line += STRLEN_CONST(rtsp_content_type);
122         linelen -= STRLEN_CONST(rtsp_content_type);
123         while (linelen > 0 && (*line == ' ' || *line == '\t')) {
124                 line++;
125                 linelen--;
126         }
127
128         if (linelen < typelen || strncasecmp(type, line, typelen))
129                 return FALSE;
130
131         line += typelen;
132         linelen -= typelen;
133         if (linelen > 0 && !isspace(*line))
134                 return FALSE;
135
136         return TRUE;
137 }
138
139 static const char rtsp_transport[] = "Transport:";
140 static const char rtsp_sps[] = "server_port=";
141 static const char rtsp_cps[] = "client_port=";
142 static const char rtsp_rtp[] = "rtp/avp";
143
144 static void
145 rtsp_create_conversation(const u_char *line_begin, int line_len)
146 {
147         conversation_t  *conv;
148         u_char          buf[256];
149         u_char          *tmp;
150         int             c_data_port, c_mon_port;
151         int             s_data_port, s_mon_port;
152
153         if (line_len > sizeof(buf) - 1) {
154                 /*
155                  * Don't overflow the buffer.
156                  */
157                 line_len = sizeof(buf) - 1;
158         }
159         memcpy(buf, line_begin, line_len);
160         buf[line_len] = '\0';
161
162         tmp = buf + STRLEN_CONST(rtsp_transport);
163         while (*tmp && isspace(*tmp))
164                 tmp++;
165         if (strncasecmp(tmp, rtsp_rtp, strlen(rtsp_rtp)) != 0)
166                 return; /* we don't know this transport */
167
168         c_data_port = c_mon_port = 0;
169         s_data_port = s_mon_port = 0;
170         if ((tmp = strstr(buf, rtsp_sps))) {
171                 tmp += strlen(rtsp_sps);
172                 if (sscanf(tmp, "%u-%u", &s_data_port, &s_mon_port) < 1)
173                         g_warning("rtsp: failed to parse server_port");
174         }
175         if ((tmp = strstr(buf, rtsp_cps))) {
176                 tmp += strlen(rtsp_cps);
177                 if (sscanf(tmp, "%u-%u", &c_data_port, &c_mon_port) < 1)
178                         g_warning("rtsp: failed to parse client_port");
179         }
180         if (!c_data_port || !s_data_port)
181                 return;
182
183         conv = conversation_new(&pi.src, &pi.dst, PT_UDP, s_data_port,
184                 c_data_port, 0, 0);
185         conversation_set_dissector(conv, dissect_rtp);
186
187         if (!c_mon_port || !s_mon_port)
188                 return;
189
190         conv = conversation_new(&pi.src, &pi.dst, PT_UDP, s_mon_port,
191                 c_mon_port, 0, 0);
192         conversation_set_dissector(conv, dissect_rtcp);
193 }
194
195 static const char rtsp_content_length[] = "Content-Length:";
196
197 static int
198 rtsp_get_content_length(const u_char *line_begin, int line_len)
199 {
200         u_char          buf[256];
201         u_char          *tmp;
202         long            content_length;
203         char            *p;
204         u_char          *up;
205
206         if (line_len > sizeof(buf) - 1) {
207                 /*
208                  * Don't overflow the buffer.
209                  */
210                 line_len = sizeof(buf) - 1;
211         }
212         memcpy(buf, line_begin, line_len);
213         buf[line_len] = '\0';
214
215         tmp = buf + STRLEN_CONST(rtsp_content_length);
216         while (*tmp && isspace(*tmp))
217                 tmp++;
218         content_length = strtol(tmp, &p, 10);
219         up = p;
220         if (up == tmp || (*up != '\0' && !isspace(*up)))
221                 return -1;      /* not a valid number */
222         return content_length;
223 }
224
225 static int
226 dissect_rtspmessage(tvbuff_t *tvb, int offset, packet_info *pinfo,
227         proto_tree *tree)
228 {
229         proto_tree      *rtsp_tree;
230         proto_item      *ti = NULL;
231         const u_char    *line;
232         gint            next_offset;
233         const u_char    *linep, *lineend;
234         int             orig_offset, linelen;
235         u_char          c;
236         gboolean        is_mime_header;
237         int             is_sdp = FALSE;
238         int             datalen;
239         int             content_length;
240         int             reported_datalen;
241
242         orig_offset = offset;
243         rtsp_tree = NULL;
244         if (tree) {
245                 ti = proto_tree_add_item(tree, proto_rtsp, tvb, offset,
246                         tvb_length_remaining(tvb, offset), FALSE);
247                 rtsp_tree = proto_item_add_subtree(ti, ett_rtsp);
248         }
249
250         if (check_col(pinfo->fd, COL_INFO)) {
251                 /*
252                  * Put the first line from the buffer into the summary
253                  * if it's an RTSP request or reply (but leave out the
254                  * line terminator).
255                  * Otherwise, just call it a continuation.
256                  */
257                 linelen = tvb_find_line_end(tvb, offset, -1, &next_offset);
258                 line = tvb_get_ptr(tvb, offset, linelen);
259                 switch (is_rtsp_request_or_reply(line, linelen)) {
260
261                 case RTSP_REQUEST:
262                 case RTSP_REPLY:
263                         col_add_str(pinfo->fd, COL_INFO,
264                             format_text(line, linelen));
265                         break;
266
267                 default:
268                         col_set_str(pinfo->fd, COL_INFO, "Continuation");
269                         break;
270                 }
271         }
272
273         /*
274          * We haven't yet seen a Content-Length header.
275          */
276         content_length = -1;
277
278         /*
279          * Process the packet data, a line at a time.
280          */
281         while (tvb_offset_exists(tvb, offset)) {
282                 /*
283                  * We haven't yet concluded that this is a MIME header.
284                  */
285                 is_mime_header = FALSE;
286
287                 /*
288                  * Find the end of the line.
289                  */
290                 linelen = tvb_find_line_end(tvb, offset, -1, &next_offset);
291
292                 /*
293                  * Get a buffer that refers to the line.
294                  */
295                 line = tvb_get_ptr(tvb, offset, linelen);
296                 lineend = line + linelen;
297
298                 /*
299                  * OK, does it look like an RTSP request or
300                  * response?
301                  */
302                 switch (is_rtsp_request_or_reply(line, linelen)) {
303
304                 case RTSP_REQUEST:
305                         if (rtsp_tree != NULL)
306                                 process_rtsp_request(tvb, offset, line, linelen,
307                                     rtsp_tree);
308                         goto is_rtsp;
309
310                 case RTSP_REPLY:
311                         if (rtsp_tree != NULL)
312                                 process_rtsp_reply(tvb, offset, line, linelen,
313                                     rtsp_tree);
314                         goto is_rtsp;
315
316                 case NOT_RTSP:
317                         break;
318                 }
319
320                 /*
321                  * No.  Does it look like a blank line (as would
322                  * appear at the end of an RTSP request)?
323                  */
324                 if (linelen == 0)
325                         goto is_rtsp;   /* Yes. */
326
327                 /*
328                  * No.  Does it look like a MIME header?
329                  */
330                 linep = line;
331                 while (linep < lineend) {
332                         c = *linep++;
333                         if (!isprint(c))
334                                 break;  /* not printable, not a MIME header */
335                         switch (c) {
336
337                         case '(':
338                         case ')':
339                         case '<':
340                         case '>':
341                         case '@':
342                         case ',':
343                         case ';':
344                         case '\\':
345                         case '"':
346                         case '/':
347                         case '[':
348                         case ']':
349                         case '?':
350                         case '=':
351                         case '{':
352                         case '}':
353                                 /*
354                                  * It's a tspecial, so it's not
355                                  * part of a token, so it's not
356                                  * a field name for the beginning
357                                  * of a MIME header.
358                                  */
359                                 goto not_rtsp;
360
361                         case ':':
362                                 /*
363                                  * This ends the token; we consider
364                                  * this to be a MIME header.
365                                  */
366                                 is_mime_header = TRUE;
367                                 goto is_rtsp;
368                         }
369                 }
370
371         not_rtsp:
372                 /*
373                  * We don't consider this part of an RTSP request or
374                  * reply, so we don't display it.
375                  */
376                 break;
377
378         is_rtsp:
379                 /*
380                  * Put this line.
381                  */
382                 if (rtsp_tree) {
383                         proto_tree_add_text(rtsp_tree, tvb, offset,
384                             next_offset - offset, "%s",
385                             tvb_format_text(tvb, offset, next_offset - offset));
386                 }
387                 if (is_mime_header) {
388                         /*
389                          * Process some MIME headers specially.
390                          */
391 #define MIME_HDR_MATCHES(header) \
392         (linelen > STRLEN_CONST(header) && \
393          strncasecmp(line, (header), STRLEN_CONST(header)) == 0)
394
395                         if (MIME_HDR_MATCHES(rtsp_transport)) {
396                                 /*
397                                  * Based on the port numbers specified
398                                  * in the Transport: header, set up
399                                  * a conversation that will be dissected
400                                  * with the appropriate dissector.
401                                  */
402                                 rtsp_create_conversation(line, linelen);
403                         } else if (MIME_HDR_MATCHES(rtsp_content_type)) {
404                                 /*
405                                  * If the Content-Type: header says this
406                                  * is SDP, dissect the payload as SDP.
407                                  */
408                                 if (is_content_sdp(line, linelen))
409                                         is_sdp = TRUE;
410                         } else if (MIME_HDR_MATCHES(rtsp_content_length)) {
411                                 /*
412                                  * Only the amount specified by the
413                                  * Content-Length: header should be treated
414                                  * as payload.
415                                  */
416                                 content_length = rtsp_get_content_length(line,
417                                     linelen);
418                         }
419                 }
420                 offset = next_offset;
421         }
422
423         /*
424          * If a content length was supplied, the amount of data to be
425          * processed as RTSP payload is the minimum of the content
426          * length and the amount of data remaining in the frame.
427          *
428          * If no content length was supplied, the amount of data to be
429          * processed is the amount of data remaining in the frame.
430          */
431         datalen = tvb_length_remaining(tvb, offset);
432         if (content_length != -1) {
433                 if (datalen > content_length)
434                         datalen = content_length;
435
436                 /*
437                  * XXX - for now, if the content length is greater
438                  * than the amount of data left in this frame (not
439                  * the amount of *captured* data left in the frame
440                  * minus the current offset, but the amount of *actual*
441                  * data that was reported to be in the frame minus
442                  * the current offset), limit it to the amount
443                  * of data left in this frame.
444                  *
445                  * If we ever handle data that crosses frame
446                  * boundaries, we'll need to remember the actual
447                  * content length.
448                  */
449                 reported_datalen = tvb_reported_length(tvb) - offset;
450                 if (content_length > reported_datalen)
451                         content_length = reported_datalen;
452         }
453
454         if (datalen > 0) {
455                 /*
456                  * There's stuff left over; process it.
457                  */
458                 if (is_sdp) {
459                         tvbuff_t *new_tvb;
460
461                         /*
462                          * Fix up the top-level item so that it doesn't
463                          * include the SDP stuff.
464                          */
465                         if (ti != NULL)
466                                 proto_item_set_len(ti, offset);
467
468                         /*
469                          * Now create a tvbuff for the SDP stuff and
470                          * dissect it.
471                          *
472                          * The amount of data to be processed that's
473                          * available in the tvbuff is "datalen", which
474                          * is the minimum of the amount of data left in
475                          * the tvbuff and any specified content length.
476                          *
477                          * The amount of data to be processed that's in
478                          * this frame, regardless of whether it was
479                          * captured or not, is "content_length",
480                          * which, if no content length was specified,
481                          * is -1, i.e. "to the end of the frame.
482                          */
483                         new_tvb = tvb_new_subset(tvb, offset, datalen,
484                             content_length);
485                         call_dissector(sdp_handle, new_tvb, pinfo, tree);
486                 } else {
487                         proto_tree_add_text(rtsp_tree, tvb, offset, datalen,
488                             "Data (%d bytes)", datalen);
489                 }
490
491                 /*
492                  * We've processed "datalen" bytes worth of data
493                  * (which may be no data at all); advance the
494                  * offset past whatever data we've processed, so they
495                  * don't process it.
496                  */
497                 offset += datalen;
498         }
499         return offset - orig_offset;
500 }
501
502 static void
503 process_rtsp_request(tvbuff_t *tvb, int offset, const u_char *data,
504         int linelen, proto_tree *tree)
505 {
506         const u_char    *lineend = data + linelen;
507         int             ii;
508         const u_char    *url;
509         const u_char    *url_start;
510         u_char          *tmp_url;
511
512         /* Request Methods */
513         for (ii = 0; ii < RTSP_NMETHODS; ii++) {
514                 size_t len = strlen(rtsp_methods[ii]);
515                 if (linelen >= len && !strncasecmp(rtsp_methods[ii], data, len))
516                         break;
517         }
518         if (ii == RTSP_NMETHODS) {
519                 /*
520                  * We got here because "is_rtsp_request_or_reply()" returned
521                  * RTSP_REQUEST, so we know one of the request methods
522                  * matched, so we "can't get here".
523                  */
524                 g_assert_not_reached();
525         }
526
527         /* Method name */
528         proto_tree_add_string_hidden(tree, hf_rtsp_method, tvb, offset,
529                 strlen(rtsp_methods[ii]), rtsp_methods[ii]);
530
531         /* URL */
532         url = data;
533         while (url < lineend && !isspace(*url))
534                 url++;
535         while (url < lineend && isspace(*url))
536                 url++;
537         url_start = url;
538         while (url < lineend && !isspace(*url))
539                 url++;
540         tmp_url = g_malloc(url - url_start + 1);
541         memcpy(tmp_url, url_start, url - url_start);
542         tmp_url[url - url_start] = 0;
543         proto_tree_add_string_hidden(tree, hf_rtsp_url, tvb,
544                 offset + (url_start - data), url - url_start, tmp_url);
545         g_free(tmp_url);
546 }
547
548 static void
549 process_rtsp_reply(tvbuff_t *tvb, int offset, const u_char *data,
550         int linelen, proto_tree *tree)
551 {
552         const u_char    *lineend = data + linelen;
553         const u_char    *status = data;
554         const u_char    *status_start;
555         unsigned int    status_i;
556
557         /* status code */
558         while (status < lineend && !isspace(*status))
559                 status++;
560         while (status < lineend && isspace(*status))
561                 status++;
562         status_start = status;
563         status_i = 0;
564         while (status < lineend && isdigit(*status))
565                 status_i = status_i * 10 + *status++ - '0';
566         proto_tree_add_uint_hidden(tree, hf_rtsp_status, tvb,
567                 offset + (status_start - data),
568                 status - status_start, status_i);
569 }
570
571 static void
572 dissect_rtsp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
573 {
574         int             offset = 0;
575         int             len;
576
577         CHECK_DISPLAY_AS_DATA(proto_rtsp, tvb, pinfo, tree);
578
579         pinfo->current_proto = "RTSP";
580
581         if (check_col(pinfo->fd, COL_PROTOCOL))
582                 col_set_str(pinfo->fd, COL_PROTOCOL, "RTSP");
583
584         while (tvb_offset_exists(tvb, offset)) {
585                 len = dissect_rtspmessage(tvb, offset, pinfo, tree);
586                 if (len == -1)
587                         break;
588                 offset += len;
589
590                 /*
591                  * OK, we've set the Protocol and Info columns for the
592                  * first RTSP message; make the columns non-writable,
593                  * so that we don't change it for subsequent RTSP messages.
594                  */
595                 col_set_writable(pinfo->fd, FALSE);
596         }
597 }
598
599 void
600 proto_register_rtsp(void)
601 {
602         static gint *ett[] = {
603                 &ett_rtsp,
604         };
605         static hf_register_info hf[] = {
606         { &hf_rtsp_method,
607         { "Method", "rtsp.method", FT_STRING, BASE_NONE, NULL, 0 }},
608         { &hf_rtsp_url,
609         { "URL", "rtsp.url", FT_STRING, BASE_NONE, NULL, 0 }},
610         { &hf_rtsp_status,
611         { "Status", "rtsp.status", FT_UINT32, BASE_DEC, NULL, 0 }},
612         };
613
614         proto_rtsp = proto_register_protocol("Real Time Streaming Protocol",
615                 "rtsp");
616         proto_register_field_array(proto_rtsp, hf, array_length(hf));
617         proto_register_subtree_array(ett, array_length(ett));
618 }
619
620 void
621 proto_reg_handoff_rtsp(void)
622 {
623         dissector_add("tcp.port", TCP_PORT_RTSP, dissect_rtsp);
624
625         /*
626          * Get a handle for the SDP dissector.
627          */
628         sdp_handle = find_dissector("sdp");
629 }