2 * Routines for RFC 1436 Gopher protocol dissection
3 * Copyright 2010, Gerald Combs <gerald@wireshark.org>
7 * Wireshark - Network traffic analyzer
8 * By Gerald Combs <gerald@wireshark.org>
9 * Copyright 1998 Gerald Combs
11 * Copied from packet-banana.c
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.
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.
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.
29 * RFC 1436: http://tools.ietf.org/html/rfc1436
30 * http://en.wikipedia.org/wiki/Gopher_%28protocol%29
41 #include <epan/packet.h>
42 #include <epan/prefs.h>
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;
55 /* Initialize the subtree pointers */
56 static gint ett_gopher = -1;
57 static gint ett_dir_item = -1;
59 static dissector_handle_t gopher_handle;
61 /* RFC 1436 section 3.8 */
62 static const value_string item_types[] = {
63 { '+', "Redundant server" },
66 { '2', "CSO phone book entity" },
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" },
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" },
83 #define TCP_DEFAULT_RANGE "70"
85 static range_t *global_gopher_tcp_range = NULL;
86 static range_t *gopher_tcp_range = NULL;
88 /* Returns TRUE if the packet is from a client */
90 is_client(packet_info *pinfo) {
91 if (value_is_in_range(gopher_tcp_range, pinfo->destport)) {
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)
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) {
104 if (tvb_length_remaining(tvb, name_start) < MIN_DIR_LINE_LEN)
107 if (! (sel_start && host_start && port_start && line_len && next_offset) )
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)
115 *sel_start = tvb_find_guint8(tvb, name_start, remain, '\t') + 1;
116 if (*sel_start < name_start + 1)
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)
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)
132 /* Dissect the packets */
135 dissect_gopher(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree) {
137 proto_tree *gopher_tree, *dir_tree = NULL;
138 gboolean client = is_client(pinfo);
140 gchar *request = "[Invalid request]";
141 gboolean is_dir = FALSE;
142 gint offset = 0, next_offset;
143 gint sel_start, host_start, port_start;
146 /* Fill in our protocol and info columns */
147 col_set_str(pinfo->cinfo, COL_PROTOCOL, "Gopher");
150 line_len = tvb_find_line_end(tvb, 0, -1, NULL, FALSE);
152 request = "[Directory list]";
153 } else if (line_len > 0) {
154 request = tvb_get_ephemeral_string(tvb, 0, line_len);
156 col_add_fstr(pinfo->cinfo, COL_INFO, "Request: %s", request);
158 col_add_fstr(pinfo->cinfo, COL_INFO, "Response");
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);
167 proto_item_append_text(ti, " request: %s", request);
168 proto_tree_add_string(gopher_tree, hf_gopher_request, tvb,
171 proto_item_append_text(ti, " response: ");
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]");
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);
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);
194 offset = next_offset;
198 proto_item_append_text(ti, "[Unknown]");
199 proto_tree_add_item(gopher_tree, hf_gopher_unknown, tvb, 0, -1, ENC_ASCII|ENC_NA);
205 /* Return the amount of data this dissector was able to dissect */
206 return tvb_length(tvb);
209 /* Preference callbacks */
211 range_delete_gopher_tcp_callback(guint32 port) {
212 dissector_delete_uint("tcp.port", port, gopher_handle);
216 range_add_gopher_tcp_callback(guint32 port) {
217 dissector_add_uint("tcp.port", port, gopher_handle);
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);
228 /* Register the protocol with Wireshark */
231 proto_register_gopher(void)
233 static hf_register_info hf[] = {
234 { &hf_gopher_request,
235 { "Gopher client request", "gopher.request",
236 FT_STRING, BASE_NONE, NULL, 0,
240 { &hf_gopher_dir_item,
241 { "Directory item", "gopher.directory",
242 FT_STRING, BASE_NONE, NULL, 0,
245 { &hf_gopher_di_type,
246 { "Type", "gopher.directory.type",
247 FT_UINT8, BASE_HEX, VALS(item_types), 0,
250 { &hf_gopher_di_name,
251 { "Name", "gopher.directory.name",
252 FT_STRING, BASE_NONE, NULL, 0,
255 { &hf_gopher_di_selector,
256 { "Selector", "gopher.directory.selector",
257 FT_STRING, BASE_NONE, NULL, 0,
260 { &hf_gopher_di_host,
261 { "Host", "gopher.directory.host",
262 FT_STRING, BASE_NONE, NULL, 0,
265 { &hf_gopher_di_port,
266 { "Port", "gopher.directory.port",
267 FT_STRING, BASE_NONE, NULL, 0,
271 { &hf_gopher_unknown,
272 { "Unknown Gopher transaction data", "gopher.unknown",
273 FT_STRING, BASE_NONE, NULL, 0,
278 module_t *gopher_module;
280 /* Setup protocol subtree array */
281 static gint *ett[] = {
286 /* Register the protocol name and description */
287 proto_gopher = proto_register_protocol("Gopher",
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));
294 /* Initialize dissector preferences */
295 gopher_module = prefs_register_protocol(proto_gopher, gopher_prefs_apply);
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",
301 &global_gopher_tcp_range, 65535);
305 proto_reg_handoff_gopher(void)
307 gopher_handle = new_create_dissector_handle(dissect_gopher, proto_gopher);
308 gopher_prefs_apply();
312 * Editor modelines - http://www.wireshark.org/tools/modelines.html
317 * indent-tabs-mode: nil
320 * vi: set shiftwidth=4 tabstop=8 expandtab:
321 * :indentSize=4:tabSize=8:noTabs=true: