From Graeme Hewson:
[obnox/wireshark/wip.git] / packet-text-media.c
1 /* packet-text-media.c
2  * Routines for text-based media dissection.
3  *
4  * NOTE - The media type is either found in pinfo->match_string
5  *        or in pinfo->private_data.
6  *
7  * (C) Olivier Biot, 2004.
8  *
9  * $Id: packet-text-media.c,v 1.8 2004/05/04 07:24:33 guy Exp $
10  *
11  * Refer to the AUTHORS file or the AUTHORS section in the man page
12  * for contacting the author(s) of this file.
13  *
14  * Ethereal - Network traffic analyzer
15  * By Gerald Combs <gerald@ethereal.com>
16  * Copyright 1998 Gerald Combs
17  *
18  * This program is free software; you can redistribute it and/or
19  * modify it under the terms of the GNU General Public License
20  * as published by the Free Software Foundation; either version 2
21  * of the License, or (at your option) any later version.
22  *
23  * This program is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26  * GNU General Public License for more details.
27  *
28  * You should have received a copy of the GNU General Public License
29  * along with this program; if not, write to the Free Software
30  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
31  */
32
33 /* Edit this file with 4-space tabs */
34
35 #ifdef HAVE_CONFIG_H
36 #include "config.h"
37 #endif
38
39 #include <string.h>
40 #include <ctype.h>
41
42 #include <glib.h>
43 #include <epan/packet.h>
44 #include <epan/strutil.h>
45
46
47 /*
48  * Media dissector for line-based text media like text/plain, message/http.
49  *
50  * TODO - character set and chunked transfer-coding
51  */
52
53 /* Filterable header fields */
54 static gint proto_text_lines = -1;
55
56 /* Subtrees */
57 static gint ett_text_lines = -1;
58
59 /* Dissector handles */
60 static dissector_handle_t text_lines_handle;
61
62 static void
63 dissect_text_lines(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
64 {
65         proto_tree      *subtree;
66         proto_item      *ti;
67         gint            offset = 0, next_offset;
68         gint            len;
69         const char      *data_name;
70
71         data_name = pinfo->match_string;
72         if (! (data_name && data_name[0])) {
73                 /*
74                  * No information from "match_string"
75                  */
76                 data_name = (char *)(pinfo->private_data);
77                 if (! (data_name && data_name[0])) {
78                         /*
79                          * No information from "private_data"
80                          */
81                         data_name = NULL;
82                 }
83         }
84
85         if (data_name && check_col(pinfo->cinfo, COL_INFO))
86                 col_append_sep_fstr(pinfo->cinfo, COL_INFO, " ", "(%s)",
87                                 data_name);
88
89         if (tree) {
90                 ti = proto_tree_add_item(tree, proto_text_lines,
91                                 tvb, 0, -1, FALSE);
92                 if (data_name)
93                         proto_item_append_text(ti, ": %s", data_name);
94                 subtree = proto_item_add_subtree(ti, ett_text_lines);
95                 /* Read the media line by line */
96                 while (tvb_reported_length_remaining(tvb, offset) != 0) {
97                         /*
98                          * XXX - we need to be passed the parameters
99                          * of the content type via "pinfo->private_data",
100                          * so that we know the character set.  We'd
101                          * have to handle that character set, which
102                          * might be a multibyte character set such
103                          * as "iso-10646-ucs-2", or might require other
104                          * special processing.
105                          */
106                         len = tvb_find_line_end(tvb, offset,
107                                         tvb_ensure_length_remaining(tvb, offset),
108                                         &next_offset, FALSE);
109                         if (len == -1)
110                                 break;
111                         proto_tree_add_text(subtree, tvb, offset, next_offset - offset,
112                                         "%s", tvb_format_text(tvb, offset, len));
113                         offset = next_offset;
114                 }
115         }
116 }
117
118 void
119 proto_register_text_lines(void)
120 {
121         static gint *ett[] = {
122                 &ett_text_lines,
123         };
124
125         proto_register_subtree_array(ett, array_length(ett));
126
127         proto_text_lines = proto_register_protocol(
128                         "Line-based text data", /* Long name */
129                         "Line-based text data", /* Short name */
130                         "data-text-lines");             /* Filter name */
131         register_dissector("data-text-lines", dissect_text_lines, proto_text_lines);
132 }
133
134 void
135 proto_reg_handoff_text_lines(void)
136 {
137         text_lines_handle = create_dissector_handle(
138                                         dissect_text_lines, proto_text_lines);
139
140         dissector_add_string("media_type", "text/plain", text_lines_handle);
141         /* W3C line-based textual media */
142         dissector_add_string("media_type", "text/html", text_lines_handle);
143         dissector_add_string("media_type", "text/xml", text_lines_handle);
144         dissector_add_string("media_type", "text/xml-external-parsed-entity", text_lines_handle);
145         dissector_add_string("media_type", "text/css", text_lines_handle);
146         dissector_add_string("media_type", "application/xml", text_lines_handle);
147         dissector_add_string("media_type", "application/xml-external-parsed-entity", text_lines_handle);
148         dissector_add_string("media_type", "application/xml-dtd", text_lines_handle);
149         dissector_add_string("media_type", "application/x-javascript", text_lines_handle);
150         dissector_add_string("media_type", "application/x-www-form-urlencoded", text_lines_handle);
151         dissector_add_string("media_type", "application/x-ns-proxy-autoconfig", text_lines_handle);
152         /* WAP and OMA line-based textual media */
153         dissector_add_string("media_type", "text/vnd.wap.wml", text_lines_handle);
154         dissector_add_string("media_type", "text/vnd.wap.si", text_lines_handle);
155         dissector_add_string("media_type", "text/vnd.wap.sl", text_lines_handle);
156         dissector_add_string("media_type", "text/vnd.wap.co", text_lines_handle);
157         dissector_add_string("media_type", "text/vnd.wap.emn", text_lines_handle);
158         dissector_add_string("media_type", "application/vnd.wv.csp+xml", text_lines_handle);
159         /* Other */
160         dissector_add_string("media_type", "text/vnd.sun.j2me.app-descriptor", text_lines_handle);
161         dissector_add_string("media_type", "application/smil", text_lines_handle);
162 }