Fix memory leaks involving tvb_get_string[z]().
[obnox/wireshark/wip.git] / epan / dissectors / packet-gopher.c
1 /* packet-gopher.c
2  * Routines for RFC 1436 Gopher protocol dissection
3  * Copyright 2010, Gerald Combs <gerald@wireshark.org>
4  *
5  * $Id$
6  *
7  * Wireshark - Network traffic analyzer
8  * By Gerald Combs <gerald@wireshark.org>
9  * Copyright 1998 Gerald Combs
10  *
11  * Copied from packet-banana.c
12  *
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.
17  *
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.
22  *
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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
26  */
27
28 /*
29  * RFC 1436: http://tools.ietf.org/html/rfc1436
30  * http://en.wikipedia.org/wiki/Gopher_%28protocol%29
31  */
32
33 #ifdef HAVE_CONFIG_H
34 # include "config.h"
35 #endif
36
37 #include <stdlib.h>
38
39 #include <glib.h>
40
41 #include <epan/packet.h>
42 #include <epan/prefs.h>
43
44 /* Initialize the protocol and registered fields */
45 static int proto_gopher = -1;
46 static int hf_gopher_request = -1;
47 static int hf_gopher_dir_item = -1;
48 static int hf_gopher_di_type = -1;
49 static int hf_gopher_di_name = -1;
50 static int hf_gopher_di_selector = -1;
51 static int hf_gopher_di_host = -1;
52 static int hf_gopher_di_port = -1;
53 static int hf_gopher_unknown = -1;
54
55 /* Initialize the subtree pointers */
56 static gint ett_gopher = -1;
57 static gint ett_dir_item = -1;
58
59 static dissector_handle_t gopher_handle;
60
61 /* RFC 1436 section 3.8 */
62 static const value_string item_types[] = {
63     { '+',  "Redundant server" },
64     { '0',  "Text file" },
65     { '1',  "Menu" },
66     { '2',  "CSO phone book entity" },
67     { '3',  "Error" },
68     { '4',  "BinHexed Macintosh file" },
69     { '5',  "DOS binary file" },
70     { '6',  "Uuencoded file" },
71     { '7',  "Index server" },
72     { '8',  "Telnet session" },
73     { '9',  "Binary file" },
74     { 'g',  "GIF file" },
75     { 'h',  "HTML file" },              /* Not in RFC 1436 */
76     { 'i',  "Informational message"},   /* Not in RFC 1436 */
77     { 'I',  "Image file" },
78     { 's',  "Audio file" },             /* Not in RFC 1436 */
79     { 'T',  "Tn3270 session" },
80     { 0, NULL }
81 };
82
83 #define TCP_DEFAULT_RANGE "70"
84
85 static range_t *global_gopher_tcp_range = NULL;
86 static range_t *gopher_tcp_range = NULL;
87
88 /* Returns TRUE if the packet is from a client */
89 static gboolean
90 is_client(packet_info *pinfo) {
91     if (value_is_in_range(gopher_tcp_range, pinfo->destport)) {
92         return TRUE;
93     }
94     return FALSE;
95 }
96
97 /* Name + Tab + Selector + Tab + Host + Tab + Port */
98 #define MAX_DIR_LINE_LEN (70 + 1 + 255 + 1 + 255 + 1 + 5)
99 #define MIN_DIR_LINE_LEN (0 + 1 + 0 + 1 + 1 + 1 + 1)
100 static gboolean
101 find_dir_tokens(tvbuff_t *tvb, gint name_start, gint *sel_start, gint *host_start, gint *port_start, gint *line_len, gint *next_offset) {
102     gint remain;
103
104     if (tvb_length_remaining(tvb, name_start) < MIN_DIR_LINE_LEN)
105         return FALSE;
106
107     if (! (sel_start && host_start && port_start && line_len && next_offset) )
108         return FALSE;
109
110     *line_len = tvb_find_line_end(tvb, name_start, MAX_DIR_LINE_LEN, next_offset, FALSE);
111     if (*line_len < MIN_DIR_LINE_LEN)
112         return FALSE;
113
114     remain = *line_len;
115     *sel_start = tvb_find_guint8(tvb, name_start, remain, '\t') + 1;
116     if (*sel_start < name_start + 1)
117         return FALSE;
118
119     remain -= *sel_start - name_start;
120     *host_start = tvb_find_guint8(tvb, *sel_start, remain, '\t') + 1;
121     if (*host_start < *sel_start + 1)
122         return FALSE;
123
124     remain -= *host_start - *sel_start;
125     *port_start = tvb_find_guint8(tvb, *host_start, remain, '\t') + 1;
126     if (*port_start < *host_start + 1)
127         return FALSE;
128
129     return TRUE;
130 }
131
132 /* Dissect the packets */
133
134 static int
135 dissect_gopher(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree) {
136     proto_item *ti;
137     proto_tree *gopher_tree, *dir_tree = NULL;
138     gboolean client = is_client(pinfo);
139     gint line_len;
140     gchar *request = "[Invalid request]";
141     gboolean is_dir = FALSE;
142     gint offset = 0, next_offset;
143     gint sel_start, host_start, port_start;
144     gchar *name;
145
146     /* Fill in our protocol and info columns */
147     col_set_str(pinfo->cinfo, COL_PROTOCOL, "Gopher");
148
149     if (client) {
150         line_len = tvb_find_line_end(tvb, 0, -1, NULL, FALSE);
151         if (line_len == 0) {
152             request = "[Directory list]";
153         } else if (line_len > 0) {
154             request = tvb_get_ephemeral_string(tvb, 0, line_len);
155         }
156         col_add_fstr(pinfo->cinfo, COL_INFO, "Request: %s", request);
157     } else {
158         col_add_fstr(pinfo->cinfo, COL_INFO, "Response");
159     }
160
161     if (tree) {
162         /* Create display subtree for the protocol */
163         ti = proto_tree_add_item(tree, proto_gopher, tvb, 0, -1, ENC_NA);
164         gopher_tree = proto_item_add_subtree(ti, ett_gopher);
165
166         if (client) {
167             proto_item_append_text(ti, " request: %s", request);
168             proto_tree_add_string(gopher_tree, hf_gopher_request, tvb,
169                                   0, -1, request);
170         } else {
171             proto_item_append_text(ti, " response: ");
172
173             while (find_dir_tokens(tvb, offset + 1, &sel_start, &host_start, &port_start, &line_len, &next_offset)) {
174                 if (!is_dir) { /* First time */
175                     proto_item_append_text(ti, "[Directory list]");
176                     col_append_fstr(pinfo->cinfo, COL_INFO, ": [Directory list]");
177                 }
178
179                 name = tvb_get_string(tvb, offset + 1, sel_start - offset - 2);
180                 ti = proto_tree_add_string(gopher_tree, hf_gopher_dir_item, tvb,
181                                 offset, line_len + 1, name);
182                 g_free(name);
183                 dir_tree = proto_item_add_subtree(ti, ett_dir_item);
184                 proto_tree_add_item(dir_tree, hf_gopher_di_type, tvb, offset, 1, ENC_BIG_ENDIAN);
185                 proto_tree_add_item(dir_tree, hf_gopher_di_name, tvb, offset + 1,
186                                     sel_start - offset - 2, ENC_ASCII|ENC_NA);
187                 proto_tree_add_item(dir_tree, hf_gopher_di_selector, tvb, sel_start,
188                                     host_start - sel_start - 1, ENC_ASCII|ENC_NA);
189                 proto_tree_add_item(dir_tree, hf_gopher_di_host, tvb, host_start,
190                                     port_start - host_start - 1, ENC_ASCII|ENC_NA);
191                 proto_tree_add_item(dir_tree, hf_gopher_di_port, tvb, port_start,
192                                     line_len - (port_start - offset - 1), ENC_ASCII|ENC_NA);
193                 is_dir = TRUE;
194                 offset = next_offset;
195             }
196
197             if (!is_dir) {
198                 proto_item_append_text(ti, "[Unknown]");
199                 proto_tree_add_item(gopher_tree, hf_gopher_unknown, tvb, 0, -1, ENC_ASCII|ENC_NA);
200             }
201         }
202
203     }
204
205     /* Return the amount of data this dissector was able to dissect */
206     return tvb_length(tvb);
207 }
208
209 /* Preference callbacks */
210 static void
211 range_delete_gopher_tcp_callback(guint32 port) {
212       dissector_delete_uint("tcp.port", port, gopher_handle);
213 }
214
215 static void
216 range_add_gopher_tcp_callback(guint32 port) {
217     dissector_add_uint("tcp.port", port, gopher_handle);
218 }
219
220 static void
221 gopher_prefs_apply(void) {
222     range_foreach(gopher_tcp_range, range_delete_gopher_tcp_callback);
223     g_free(gopher_tcp_range);
224     gopher_tcp_range = range_copy(global_gopher_tcp_range);
225     range_foreach(gopher_tcp_range, range_add_gopher_tcp_callback);
226 }
227
228 /* Register the protocol with Wireshark */
229
230 void
231 proto_register_gopher(void)
232 {
233     static hf_register_info hf[] = {
234         { &hf_gopher_request,
235             { "Gopher client request", "gopher.request",
236                 FT_STRING, BASE_NONE, NULL, 0,
237                 NULL, HFILL }
238         },
239
240         { &hf_gopher_dir_item,
241             { "Directory item", "gopher.directory",
242                 FT_STRING, BASE_NONE, NULL, 0,
243                 NULL, HFILL }
244         },
245         { &hf_gopher_di_type,
246             { "Type", "gopher.directory.type",
247                 FT_UINT8, BASE_HEX, VALS(item_types), 0,
248                 NULL, HFILL }
249         },
250         { &hf_gopher_di_name,
251             { "Name", "gopher.directory.name",
252                 FT_STRING, BASE_NONE, NULL, 0,
253                 NULL, HFILL }
254         },
255         { &hf_gopher_di_selector,
256             { "Selector", "gopher.directory.selector",
257                 FT_STRING, BASE_NONE, NULL, 0,
258                 NULL, HFILL }
259         },
260         { &hf_gopher_di_host,
261             { "Host", "gopher.directory.host",
262                 FT_STRING, BASE_NONE, NULL, 0,
263                 NULL, HFILL }
264         },
265         { &hf_gopher_di_port,
266             { "Port", "gopher.directory.port",
267                 FT_STRING, BASE_NONE, NULL, 0,
268                 NULL, HFILL }
269         },
270
271         { &hf_gopher_unknown,
272             { "Unknown Gopher transaction data", "gopher.unknown",
273                 FT_STRING, BASE_NONE, NULL, 0,
274                 NULL, HFILL }
275         }
276     };
277
278     module_t *gopher_module;
279
280     /* Setup protocol subtree array */
281     static gint *ett[] = {
282         &ett_gopher,
283         &ett_dir_item
284     };
285
286     /* Register the protocol name and description */
287     proto_gopher = proto_register_protocol("Gopher",
288         "Gopher", "gopher");
289
290     /* Required function calls to register the header fields and subtrees used */
291     proto_register_field_array(proto_gopher, hf, array_length(hf));
292     proto_register_subtree_array(ett, array_length(ett));
293
294     /* Initialize dissector preferences */
295     gopher_module = prefs_register_protocol(proto_gopher, gopher_prefs_apply);
296
297     range_convert_str(&global_gopher_tcp_range, TCP_DEFAULT_RANGE, 65535);
298     gopher_tcp_range = range_empty();
299     prefs_register_range_preference(gopher_module, "tcp.port", "TCP Ports",
300                                     "TCP Ports range",
301                                     &global_gopher_tcp_range, 65535);
302 }
303
304 void
305 proto_reg_handoff_gopher(void)
306 {
307     gopher_handle = new_create_dissector_handle(dissect_gopher, proto_gopher);
308     gopher_prefs_apply();
309 }
310
311 /*
312  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
313  *
314  * Local variables:
315  * c-basic-offset: 4
316  * tab-width: 8
317  * indent-tabs-mode: nil
318  * End:
319  *
320  * vi: set shiftwidth=4 tabstop=8 expandtab:
321  * :indentSize=4:tabSize=8:noTabs=true:
322  */
323
324