2010-06-13 20:30:43 +00:00
|
|
|
/* packet-gopher.c
|
|
|
|
* Routines for RFC 1436 Gopher protocol dissection
|
|
|
|
* Copyright 2010, Gerald Combs <gerald@wireshark.org>
|
|
|
|
*
|
|
|
|
* Wireshark - Network traffic analyzer
|
|
|
|
* By Gerald Combs <gerald@wireshark.org>
|
|
|
|
* Copyright 1998 Gerald Combs
|
|
|
|
*
|
|
|
|
* Copied from packet-banana.c
|
|
|
|
*
|
2018-02-12 11:23:27 +00:00
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later
|
2010-06-13 20:30:43 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* RFC 1436: http://tools.ietf.org/html/rfc1436
|
|
|
|
* http://en.wikipedia.org/wiki/Gopher_%28protocol%29
|
|
|
|
*/
|
|
|
|
|
2012-09-20 02:03:38 +00:00
|
|
|
#include "config.h"
|
2010-06-13 20:30:43 +00:00
|
|
|
|
|
|
|
#include <epan/packet.h>
|
|
|
|
#include <epan/prefs.h>
|
|
|
|
|
2013-12-15 23:44:12 +00:00
|
|
|
void proto_register_gopher(void);
|
|
|
|
void proto_reg_handoff_gopher(void);
|
|
|
|
|
2010-06-13 20:30:43 +00:00
|
|
|
/* Initialize the protocol and registered fields */
|
|
|
|
static int proto_gopher = -1;
|
|
|
|
static int hf_gopher_request = -1;
|
|
|
|
static int hf_gopher_dir_item = -1;
|
|
|
|
static int hf_gopher_di_type = -1;
|
|
|
|
static int hf_gopher_di_name = -1;
|
|
|
|
static int hf_gopher_di_selector = -1;
|
|
|
|
static int hf_gopher_di_host = -1;
|
|
|
|
static int hf_gopher_di_port = -1;
|
|
|
|
static int hf_gopher_unknown = -1;
|
|
|
|
|
|
|
|
/* Initialize the subtree pointers */
|
|
|
|
static gint ett_gopher = -1;
|
|
|
|
static gint ett_dir_item = -1;
|
|
|
|
|
2010-10-11 19:00:55 +00:00
|
|
|
static dissector_handle_t gopher_handle;
|
2010-06-13 20:30:43 +00:00
|
|
|
|
|
|
|
/* RFC 1436 section 3.8 */
|
|
|
|
static const value_string item_types[] = {
|
2010-10-11 19:00:55 +00:00
|
|
|
{ '+', "Redundant server" },
|
|
|
|
{ '0', "Text file" },
|
|
|
|
{ '1', "Menu" },
|
|
|
|
{ '2', "CSO phone book entity" },
|
|
|
|
{ '3', "Error" },
|
|
|
|
{ '4', "BinHexed Macintosh file" },
|
|
|
|
{ '5', "DOS binary file" },
|
|
|
|
{ '6', "Uuencoded file" },
|
|
|
|
{ '7', "Index server" },
|
|
|
|
{ '8', "Telnet session" },
|
|
|
|
{ '9', "Binary file" },
|
|
|
|
{ 'g', "GIF file" },
|
|
|
|
{ 'h', "HTML file" }, /* Not in RFC 1436 */
|
|
|
|
{ 'i', "Informational message"}, /* Not in RFC 1436 */
|
|
|
|
{ 'I', "Image file" },
|
|
|
|
{ 's', "Audio file" }, /* Not in RFC 1436 */
|
|
|
|
{ 'T', "Tn3270 session" },
|
|
|
|
{ 0, NULL }
|
2010-06-13 20:30:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#define TCP_DEFAULT_RANGE "70"
|
|
|
|
|
|
|
|
static range_t *gopher_tcp_range = NULL;
|
|
|
|
|
|
|
|
/* Returns TRUE if the packet is from a client */
|
|
|
|
static gboolean
|
|
|
|
is_client(packet_info *pinfo) {
|
2018-11-30 19:26:29 +00:00
|
|
|
return value_is_in_range(gopher_tcp_range, pinfo->destport);
|
2010-06-13 20:30:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Name + Tab + Selector + Tab + Host + Tab + Port */
|
|
|
|
#define MAX_DIR_LINE_LEN (70 + 1 + 255 + 1 + 255 + 1 + 5)
|
|
|
|
#define MIN_DIR_LINE_LEN (0 + 1 + 0 + 1 + 1 + 1 + 1)
|
|
|
|
static gboolean
|
|
|
|
find_dir_tokens(tvbuff_t *tvb, gint name_start, gint *sel_start, gint *host_start, gint *port_start, gint *line_len, gint *next_offset) {
|
2010-10-11 19:00:55 +00:00
|
|
|
gint remain;
|
2010-06-13 20:30:43 +00:00
|
|
|
|
2015-06-24 04:30:15 +00:00
|
|
|
if (tvb_captured_length_remaining(tvb, name_start) < MIN_DIR_LINE_LEN)
|
2010-10-11 19:00:55 +00:00
|
|
|
return FALSE;
|
2010-06-13 20:30:43 +00:00
|
|
|
|
2010-10-11 19:00:55 +00:00
|
|
|
if (! (sel_start && host_start && port_start && line_len && next_offset) )
|
|
|
|
return FALSE;
|
2010-06-13 20:30:43 +00:00
|
|
|
|
2010-10-11 19:00:55 +00:00
|
|
|
*line_len = tvb_find_line_end(tvb, name_start, MAX_DIR_LINE_LEN, next_offset, FALSE);
|
|
|
|
if (*line_len < MIN_DIR_LINE_LEN)
|
|
|
|
return FALSE;
|
2010-06-13 20:30:43 +00:00
|
|
|
|
2010-10-11 19:00:55 +00:00
|
|
|
remain = *line_len;
|
|
|
|
*sel_start = tvb_find_guint8(tvb, name_start, remain, '\t') + 1;
|
|
|
|
if (*sel_start < name_start + 1)
|
|
|
|
return FALSE;
|
2010-06-13 20:30:43 +00:00
|
|
|
|
2010-10-11 19:00:55 +00:00
|
|
|
remain -= *sel_start - name_start;
|
|
|
|
*host_start = tvb_find_guint8(tvb, *sel_start, remain, '\t') + 1;
|
|
|
|
if (*host_start < *sel_start + 1)
|
|
|
|
return FALSE;
|
2010-06-13 20:30:43 +00:00
|
|
|
|
2010-10-11 19:00:55 +00:00
|
|
|
remain -= *host_start - *sel_start;
|
|
|
|
*port_start = tvb_find_guint8(tvb, *host_start, remain, '\t') + 1;
|
|
|
|
if (*port_start < *host_start + 1)
|
|
|
|
return FALSE;
|
2010-06-13 20:30:43 +00:00
|
|
|
|
2010-10-11 19:00:55 +00:00
|
|
|
return TRUE;
|
2010-06-13 20:30:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Dissect the packets */
|
|
|
|
|
|
|
|
static int
|
2012-09-10 21:40:21 +00:00
|
|
|
dissect_gopher(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_) {
|
2010-06-13 20:30:43 +00:00
|
|
|
proto_item *ti;
|
2010-10-11 19:00:55 +00:00
|
|
|
proto_tree *gopher_tree, *dir_tree = NULL;
|
|
|
|
gboolean client = is_client(pinfo);
|
|
|
|
gint line_len;
|
2012-04-09 17:53:04 +00:00
|
|
|
const gchar *request = "[Invalid request]";
|
2010-10-11 19:00:55 +00:00
|
|
|
gboolean is_dir = FALSE;
|
|
|
|
gint offset = 0, next_offset;
|
|
|
|
gint sel_start, host_start, port_start;
|
|
|
|
gchar *name;
|
|
|
|
|
2010-06-13 20:30:43 +00:00
|
|
|
/* Fill in our protocol and info columns */
|
|
|
|
col_set_str(pinfo->cinfo, COL_PROTOCOL, "Gopher");
|
|
|
|
|
2010-10-11 19:00:55 +00:00
|
|
|
if (client) {
|
|
|
|
line_len = tvb_find_line_end(tvb, 0, -1, NULL, FALSE);
|
|
|
|
if (line_len == 0) {
|
|
|
|
request = "[Directory list]";
|
|
|
|
} else if (line_len > 0) {
|
2014-06-17 15:30:58 +00:00
|
|
|
request = tvb_get_string_enc(wmem_packet_scope(), tvb, 0, line_len, ENC_ASCII);
|
2010-10-11 19:00:55 +00:00
|
|
|
}
|
|
|
|
col_add_fstr(pinfo->cinfo, COL_INFO, "Request: %s", request);
|
|
|
|
} else {
|
|
|
|
col_add_fstr(pinfo->cinfo, COL_INFO, "Response");
|
|
|
|
}
|
2010-06-13 20:30:43 +00:00
|
|
|
|
|
|
|
if (tree) {
|
|
|
|
/* Create display subtree for the protocol */
|
2011-10-21 02:10:19 +00:00
|
|
|
ti = proto_tree_add_item(tree, proto_gopher, tvb, 0, -1, ENC_NA);
|
2010-10-11 19:00:55 +00:00
|
|
|
gopher_tree = proto_item_add_subtree(ti, ett_gopher);
|
|
|
|
|
|
|
|
if (client) {
|
|
|
|
proto_item_append_text(ti, " request: %s", request);
|
|
|
|
proto_tree_add_string(gopher_tree, hf_gopher_request, tvb,
|
|
|
|
0, -1, request);
|
|
|
|
} else {
|
|
|
|
proto_item_append_text(ti, " response: ");
|
|
|
|
|
|
|
|
while (find_dir_tokens(tvb, offset + 1, &sel_start, &host_start, &port_start, &line_len, &next_offset)) {
|
|
|
|
if (!is_dir) { /* First time */
|
|
|
|
proto_item_append_text(ti, "[Directory list]");
|
2013-10-29 14:09:20 +00:00
|
|
|
col_append_str(pinfo->cinfo, COL_INFO, ": [Directory list]");
|
2010-10-11 19:00:55 +00:00
|
|
|
}
|
|
|
|
|
2014-06-17 15:30:58 +00:00
|
|
|
name = tvb_get_string_enc(wmem_packet_scope(), tvb, offset + 1, sel_start - offset - 2, ENC_ASCII);
|
2010-10-11 19:00:55 +00:00
|
|
|
ti = proto_tree_add_string(gopher_tree, hf_gopher_dir_item, tvb,
|
|
|
|
offset, line_len + 1, name);
|
|
|
|
dir_tree = proto_item_add_subtree(ti, ett_dir_item);
|
2016-09-19 01:48:50 +00:00
|
|
|
proto_tree_add_item(dir_tree, hf_gopher_di_type, tvb, offset, 1, ENC_ASCII|ENC_NA);
|
2010-10-11 19:00:55 +00:00
|
|
|
proto_tree_add_item(dir_tree, hf_gopher_di_name, tvb, offset + 1,
|
2011-10-15 18:46:26 +00:00
|
|
|
sel_start - offset - 2, ENC_ASCII|ENC_NA);
|
2010-10-11 19:00:55 +00:00
|
|
|
proto_tree_add_item(dir_tree, hf_gopher_di_selector, tvb, sel_start,
|
2011-10-15 18:46:26 +00:00
|
|
|
host_start - sel_start - 1, ENC_ASCII|ENC_NA);
|
2010-10-11 19:00:55 +00:00
|
|
|
proto_tree_add_item(dir_tree, hf_gopher_di_host, tvb, host_start,
|
2011-10-15 18:46:26 +00:00
|
|
|
port_start - host_start - 1, ENC_ASCII|ENC_NA);
|
2010-10-11 19:00:55 +00:00
|
|
|
proto_tree_add_item(dir_tree, hf_gopher_di_port, tvb, port_start,
|
2011-10-15 18:46:26 +00:00
|
|
|
line_len - (port_start - offset - 1), ENC_ASCII|ENC_NA);
|
2010-10-11 19:00:55 +00:00
|
|
|
is_dir = TRUE;
|
|
|
|
offset = next_offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_dir) {
|
|
|
|
proto_item_append_text(ti, "[Unknown]");
|
2011-10-15 18:46:26 +00:00
|
|
|
proto_tree_add_item(gopher_tree, hf_gopher_unknown, tvb, 0, -1, ENC_ASCII|ENC_NA);
|
2010-10-11 19:00:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-13 20:30:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the amount of data this dissector was able to dissect */
|
2015-06-23 18:24:48 +00:00
|
|
|
return tvb_captured_length(tvb);
|
2010-06-13 20:30:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Preference callbacks */
|
|
|
|
static void
|
|
|
|
gopher_prefs_apply(void) {
|
2016-10-07 20:25:01 +00:00
|
|
|
|
2016-11-18 02:45:28 +00:00
|
|
|
gopher_tcp_range = prefs_get_range_value("gopher", "tcp.port");
|
2010-06-13 20:30:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Register the protocol with Wireshark */
|
|
|
|
|
|
|
|
void
|
|
|
|
proto_register_gopher(void)
|
|
|
|
{
|
|
|
|
static hf_register_info hf[] = {
|
|
|
|
{ &hf_gopher_request,
|
|
|
|
{ "Gopher client request", "gopher.request",
|
|
|
|
FT_STRING, BASE_NONE, NULL, 0,
|
|
|
|
NULL, HFILL }
|
|
|
|
},
|
|
|
|
|
|
|
|
{ &hf_gopher_dir_item,
|
|
|
|
{ "Directory item", "gopher.directory",
|
|
|
|
FT_STRING, BASE_NONE, NULL, 0,
|
|
|
|
NULL, HFILL }
|
|
|
|
},
|
|
|
|
{ &hf_gopher_di_type,
|
|
|
|
{ "Type", "gopher.directory.type",
|
2016-09-19 01:48:50 +00:00
|
|
|
FT_CHAR, BASE_HEX, VALS(item_types), 0,
|
2010-06-13 20:30:43 +00:00
|
|
|
NULL, HFILL }
|
|
|
|
},
|
|
|
|
{ &hf_gopher_di_name,
|
|
|
|
{ "Name", "gopher.directory.name",
|
|
|
|
FT_STRING, BASE_NONE, NULL, 0,
|
|
|
|
NULL, HFILL }
|
|
|
|
},
|
|
|
|
{ &hf_gopher_di_selector,
|
|
|
|
{ "Selector", "gopher.directory.selector",
|
|
|
|
FT_STRING, BASE_NONE, NULL, 0,
|
|
|
|
NULL, HFILL }
|
|
|
|
},
|
|
|
|
{ &hf_gopher_di_host,
|
|
|
|
{ "Host", "gopher.directory.host",
|
|
|
|
FT_STRING, BASE_NONE, NULL, 0,
|
|
|
|
NULL, HFILL }
|
|
|
|
},
|
|
|
|
{ &hf_gopher_di_port,
|
|
|
|
{ "Port", "gopher.directory.port",
|
|
|
|
FT_STRING, BASE_NONE, NULL, 0,
|
|
|
|
NULL, HFILL }
|
|
|
|
},
|
|
|
|
|
|
|
|
{ &hf_gopher_unknown,
|
|
|
|
{ "Unknown Gopher transaction data", "gopher.unknown",
|
|
|
|
FT_STRING, BASE_NONE, NULL, 0,
|
|
|
|
NULL, HFILL }
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Setup protocol subtree array */
|
|
|
|
static gint *ett[] = {
|
|
|
|
&ett_gopher,
|
|
|
|
&ett_dir_item
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Register the protocol name and description */
|
2016-10-07 20:25:01 +00:00
|
|
|
proto_gopher = proto_register_protocol("Gopher", "Gopher", "gopher");
|
2010-06-13 20:30:43 +00:00
|
|
|
|
|
|
|
/* Required function calls to register the header fields and subtrees used */
|
|
|
|
proto_register_field_array(proto_gopher, hf, array_length(hf));
|
|
|
|
proto_register_subtree_array(ett, array_length(ett));
|
|
|
|
|
2016-10-07 20:25:01 +00:00
|
|
|
/* Preferences for this module are generated when registering with
|
|
|
|
dissector tables and need the callback function to get the
|
|
|
|
port range values
|
|
|
|
*/
|
|
|
|
prefs_register_protocol(proto_gopher, gopher_prefs_apply);
|
2010-06-13 20:30:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
proto_reg_handoff_gopher(void)
|
|
|
|
{
|
2015-12-09 03:49:44 +00:00
|
|
|
gopher_handle = create_dissector_handle(dissect_gopher, proto_gopher);
|
2016-10-07 20:25:01 +00:00
|
|
|
dissector_add_uint_range_with_preference("tcp.port", TCP_DEFAULT_RANGE, gopher_handle);
|
2018-11-30 19:26:29 +00:00
|
|
|
gopher_prefs_apply();
|
2010-06-13 20:30:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2019-07-26 18:43:17 +00:00
|
|
|
* Editor modelines - https://www.wireshark.org/tools/modelines.html
|
2010-06-13 20:30:43 +00:00
|
|
|
*
|
|
|
|
* Local variables:
|
|
|
|
* c-basic-offset: 4
|
2010-10-11 19:00:55 +00:00
|
|
|
* tab-width: 8
|
|
|
|
* indent-tabs-mode: nil
|
2010-06-13 20:30:43 +00:00
|
|
|
* End:
|
|
|
|
*
|
2011-09-21 16:28:53 +00:00
|
|
|
* vi: set shiftwidth=4 tabstop=8 expandtab:
|
2010-10-11 19:00:55 +00:00
|
|
|
* :indentSize=4:tabSize=8:noTabs=true:
|
2010-06-13 20:30:43 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|