3 * Routines to modify dissector tables on the fly.
5 * By David Hampton <dhampton@mac.com>
6 * Copyright 2001 David Hampton
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 #include "epan/decode_as.h"
29 #include "epan/packet.h"
30 #include "epan/prefs.h"
31 #include "epan/prefs-int.h"
33 #include "epan/dissectors/packet-dcerpc.h"
35 #include "ui/decode_as_utils.h"
36 #include "ui/simple_dialog.h"
38 #include "wsutil/file_util.h"
39 #include "wsutil/filesystem.h"
40 #include "wsutil/ws_version_info.h"
42 /* XXX - We might want to switch this to a UAT */
45 * A list of dissectors that need to be reset.
47 static GSList *dissector_reset_list = NULL;
50 * Data structure used as user data when iterating dissector handles
52 typedef struct lookup_entry {
53 gchar* dissector_short_name;
54 dissector_handle_t handle;
58 * Data structure for tracking which dissector need to be reset. This
59 * structure is necessary as a hash table entry cannot be removed
60 * while a g_hash_table_foreach walk is in progress.
62 typedef struct dissector_delete_item {
63 /* The name of the dissector table */
64 const gchar *ddi_table_name;
65 /* The type of the selector in that dissector table */
66 ftenum_t ddi_selector_type;
67 /* The selector in the dissector table */
72 } dissector_delete_item_t;
75 * A callback function to changed a dissector_handle if matched
76 * This is used when iterating a dissector table
79 change_dissector_if_matched(gpointer item, gpointer user_data)
81 dissector_handle_t handle = (dissector_handle_t)item;
82 lookup_entry_t * lookup = (lookup_entry_t *)user_data;
83 if (strcmp(lookup->dissector_short_name, dissector_handle_get_short_name(handle)) == 0) {
84 lookup->handle = handle;
89 * A callback function to parse each "decode as" entry in the file and apply the change
91 static prefs_set_pref_e
92 read_set_decode_as_entries(gchar *key, const gchar *value,
94 gboolean return_range_errors _U_)
96 gchar *values[4] = {NULL, NULL, NULL, NULL};
97 gchar delimiter[4] = {',', ',', ',','\0'};
100 dissector_table_t sub_dissectors;
101 prefs_set_pref_e retval = PREFS_SET_OK;
102 gboolean is_valid = FALSE;
104 if (strcmp(key, DECODE_AS_ENTRY) == 0) {
105 /* Parse csv into table, selector, initial, current */
106 for (i = 0; i < 4; i++) {
107 pch = strchr(value, delimiter[i]);
109 for (j = 0; j < i; j++) {
112 return PREFS_SET_SYNTAX_ERR;
114 values[i] = g_strndup(value, pch - value);
117 sub_dissectors = find_dissector_table(values[0]);
118 if (sub_dissectors != NULL) {
119 lookup_entry_t lookup;
120 ftenum_t selector_type;
122 lookup.dissector_short_name = values[3];
123 lookup.handle = NULL;
124 selector_type = dissector_table_get_type(sub_dissectors);
126 g_slist_foreach(dissector_table_get_dissector_handles(sub_dissectors),
127 change_dissector_if_matched, &lookup);
128 if (lookup.handle != NULL || g_ascii_strcasecmp(values[3], DECODE_AS_NONE) == 0) {
133 if (IS_FT_STRING(selector_type)) {
134 dissector_change_string(values[0], values[1], lookup.handle);
139 long_value = strtol(values[1], &p, 0);
140 if (p == values[0] || *p != '\0' || long_value < 0 ||
141 (unsigned long)long_value > UINT_MAX) {
142 retval = PREFS_SET_SYNTAX_ERR;
145 dissector_change_uint(values[0], (guint)long_value, lookup.handle);
149 decode_build_reset_list(g_strdup(values[0]), selector_type,
150 g_strdup(values[1]), NULL, NULL);
153 retval = PREFS_SET_SYNTAX_ERR;
157 retval = PREFS_SET_NO_SUCH_PREF;
160 for (i = 0; i < 4; i++) {
167 load_decode_as_entries(void)
172 if (dissector_reset_list) {
176 daf_path = get_persconffile_path(DECODE_AS_ENTRIES_FILE_NAME, TRUE);
177 if ((daf = ws_fopen(daf_path, "r")) != NULL) {
178 read_prefs_file(daf_path, daf, read_set_decode_as_entries, NULL);
185 decode_build_reset_list (const gchar *table_name, ftenum_t selector_type,
186 gpointer key, gpointer value _U_,
187 gpointer user_data _U_)
189 dissector_delete_item_t *item;
191 item = g_new(dissector_delete_item_t,1);
192 item->ddi_table_name = table_name;
193 item->ddi_selector_type = selector_type;
194 switch (selector_type) {
200 item->ddi_selector.sel_uint = GPOINTER_TO_UINT(key);
207 item->ddi_selector.sel_string = (char *)key;
211 g_assert_not_reached();
213 dissector_reset_list = g_slist_prepend(dissector_reset_list, item);
216 /* clear all settings */
218 decode_clear_all(void)
220 dissector_delete_item_t *item;
223 dissector_all_tables_foreach_changed(decode_build_reset_list, NULL);
225 for (tmp = dissector_reset_list; tmp; tmp = g_slist_next(tmp)) {
226 item = (dissector_delete_item_t *)tmp->data;
227 switch (item->ddi_selector_type) {
233 dissector_reset_uint(item->ddi_table_name,
234 item->ddi_selector.sel_uint);
241 dissector_reset_string(item->ddi_table_name,
242 item->ddi_selector.sel_string);
246 g_assert_not_reached();
250 g_slist_free(dissector_reset_list);
251 dissector_reset_list = NULL;
253 decode_dcerpc_reset_all();
257 decode_as_write_entry (const gchar *table_name, ftenum_t selector_type,
258 gpointer key, gpointer value, gpointer user_data)
260 FILE *da_file = (FILE *)user_data;
261 dissector_handle_t current, initial;
262 const gchar *current_proto_name, *initial_proto_name;
264 current = dtbl_entry_get_handle((dtbl_entry_t *)value);
266 current_proto_name = DECODE_AS_NONE;
268 current_proto_name = dissector_handle_get_short_name(current);
269 initial = dtbl_entry_get_initial_handle((dtbl_entry_t *)value);
271 initial_proto_name = DECODE_AS_NONE;
273 initial_proto_name = dissector_handle_get_short_name(initial);
275 switch (selector_type) {
282 * XXX - write these in decimal, regardless of the base of
283 * the dissector table's selector, as older versions of
284 * Wireshark used atoi() when reading this file, and
285 * failed to handle hex or octal numbers.
287 * That will be fixed in future 1.10 and 1.12 releases,
288 * but pre-1.10 releases are at end-of-life and won't
292 DECODE_AS_ENTRY ": %s,%u,%s,%s\n",
293 table_name, GPOINTER_TO_UINT(key), initial_proto_name,
302 DECODE_AS_ENTRY ": %s,%s,%s,%s\n",
303 table_name, (gchar *)key, initial_proto_name,
308 g_assert_not_reached();
314 save_decode_as_entries(void)
320 if (create_persconffile_dir(&pf_dir_path) == -1) {
321 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
322 "Can't create directory\n\"%s\"\nfor recent file: %s.", pf_dir_path,
328 daf_path = get_persconffile_path(DECODE_AS_ENTRIES_FILE_NAME, TRUE);
329 if ((da_file = ws_fopen(daf_path, "w")) == NULL) {
330 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
331 "Can't open decode_as_entries file\n\"%s\": %s.", daf_path,
337 fputs("# \"Decode As\" entries file for Wireshark " VERSION ".\n"
339 "# This file is regenerated each time \"Decode As\" preferences\n"
340 "# are saved within Wireshark. Making manual changes should be safe,\n"
341 "# however.\n", da_file);
343 dissector_all_tables_foreach_changed(decode_as_write_entry, da_file);
353 * indent-tabs-mode: nil
356 * ex: set shiftwidth=4 tabstop=8 expandtab:
357 * :indentSize=4:tabSize=8:noTabs=true: