ce66fd2ecfd6aa52775863a53363dc73d1f71c9f
[jlayton/wireshark.git] / ui / decode_as_utils.c
1 /* decode_as_utils.c
2  *
3  * Routines to modify dissector tables on the fly.
4  *
5  * By David Hampton <dhampton@mac.com>
6  * Copyright 2001 David Hampton
7  *
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.
12  *
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.
17  *
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.
21  */
22 #include "config.h"
23
24 #include <stdlib.h>
25
26 #include <errno.h>
27
28 #include "epan/decode_as.h"
29 #include "epan/packet.h"
30 #include "epan/prefs.h"
31 #include "epan/prefs-int.h"
32
33 #include "epan/dissectors/packet-dcerpc.h"
34
35 #include "ui/decode_as_utils.h"
36 #include "ui/simple_dialog.h"
37
38 #include "wsutil/file_util.h"
39 #include "wsutil/filesystem.h"
40 #include "ws_version_info.h"
41
42 /* XXX - We might want to switch this to a UAT */
43
44 /*
45  * A list of dissectors that need to be reset.
46  */
47 static GSList *dissector_reset_list = NULL;
48
49 /*
50  * Data structure used as user data when iterating dissector handles
51  */
52 typedef struct lookup_entry {
53     gchar*             dissector_short_name;
54     dissector_handle_t handle;
55 } lookup_entry_t;
56
57 /*
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.
61  */
62 typedef struct dissector_delete_item {
63     /* The name of the dissector table */
64     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 */
68     union {
69         guint   sel_uint;
70         char    *sel_string;
71     } ddi_selector;
72 } dissector_delete_item_t;
73
74 /*
75  * A callback function to changed a dissector_handle if matched
76  * This is used when iterating a dissector table
77  */
78 static void
79 change_dissector_if_matched(gpointer item, gpointer user_data)
80 {
81     dissector_handle_t handle = (dissector_handle_t)item;
82     lookup_entry_t * lookup = (lookup_entry_t *)user_data;
83     const gchar *proto_short_name = dissector_handle_get_short_name(handle);
84     if (proto_short_name && strcmp(lookup->dissector_short_name, proto_short_name) == 0) {
85         lookup->handle = handle;
86     }
87 }
88
89 /*
90  * A callback function to parse each "decode as" entry in the file and apply the change
91  */
92 static prefs_set_pref_e
93 read_set_decode_as_entries(gchar *key, const gchar *value,
94                            void *user_data _U_,
95                            gboolean return_range_errors _U_)
96 {
97     gchar *values[4] = {NULL, NULL, NULL, NULL};
98     gchar delimiter[4] = {',', ',', ',','\0'};
99     gchar *pch;
100     guint i, j;
101     dissector_table_t sub_dissectors;
102     prefs_set_pref_e retval = PREFS_SET_OK;
103     gboolean is_valid = FALSE;
104
105     if (strcmp(key, DECODE_AS_ENTRY) == 0) {
106         /* Parse csv into table, selector, initial, current */
107         for (i = 0; i < 4; i++) {
108             pch = strchr(value, delimiter[i]);
109             if (pch == NULL) {
110                 for (j = 0; j < i; j++) {
111                     g_free(values[j]);
112                 }
113                 return PREFS_SET_SYNTAX_ERR;
114             }
115             values[i] = g_strndup(value, pch - value);
116             value = pch + 1;
117         }
118         sub_dissectors = find_dissector_table(values[0]);
119         if (sub_dissectors != NULL) {
120             lookup_entry_t lookup;
121             ftenum_t selector_type;
122
123             lookup.dissector_short_name = values[3];
124             lookup.handle = NULL;
125             selector_type = dissector_table_get_type(sub_dissectors);
126
127             g_slist_foreach(dissector_table_get_dissector_handles(sub_dissectors),
128                     change_dissector_if_matched, &lookup);
129             if (lookup.handle != NULL || g_ascii_strcasecmp(values[3], DECODE_AS_NONE) == 0) {
130                 is_valid = TRUE;
131             }
132
133             if (is_valid) {
134                 if (IS_FT_STRING(selector_type)) {
135                     dissector_change_string(values[0], values[1], lookup.handle);
136                 } else {
137                     char *p;
138                     long long_value;
139
140                     long_value = strtol(values[1], &p, 0);
141                     if (p == values[0] || *p != '\0' || long_value < 0 ||
142                           (unsigned long)long_value > UINT_MAX) {
143                         retval = PREFS_SET_SYNTAX_ERR;
144                         is_valid = FALSE;
145                     } else
146                         dissector_change_uint(values[0], (guint)long_value, lookup.handle);
147                 }
148             }
149             if (is_valid) {
150                 decode_build_reset_list(values[0], selector_type, values[1], NULL, NULL);
151             }
152         } else {
153             retval = PREFS_SET_SYNTAX_ERR;
154         }
155
156     } else {
157         retval = PREFS_SET_NO_SUCH_PREF;
158     }
159
160     for (i = 0; i < 4; i++) {
161         g_free(values[i]);
162     }
163     return retval;
164 }
165
166 void
167 load_decode_as_entries(void)
168 {
169     char   *daf_path;
170     FILE   *daf;
171
172     if (dissector_reset_list) {
173         decode_clear_all();
174     }
175
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);
179         fclose(daf);
180     }
181     g_free(daf_path);
182 }
183
184 void
185 decode_build_reset_list (const gchar *table_name, ftenum_t selector_type,
186                          gpointer key, gpointer value _U_,
187                          gpointer user_data _U_)
188 {
189     dissector_delete_item_t *item;
190
191     item = g_new(dissector_delete_item_t,1);
192     item->ddi_table_name = g_strdup(table_name);
193     item->ddi_selector_type = selector_type;
194     switch (selector_type) {
195
196     case FT_UINT8:
197     case FT_UINT16:
198     case FT_UINT24:
199     case FT_UINT32:
200         item->ddi_selector.sel_uint = GPOINTER_TO_UINT(key);
201         break;
202
203     case FT_STRING:
204     case FT_STRINGZ:
205     case FT_UINT_STRING:
206     case FT_STRINGZPAD:
207         item->ddi_selector.sel_string = g_strdup((char *)key);
208         break;
209
210     default:
211         g_assert_not_reached();
212     }
213     dissector_reset_list = g_slist_prepend(dissector_reset_list, item);
214 }
215
216 /* clear all settings */
217 void
218 decode_clear_all(void)
219 {
220     dissector_delete_item_t *item;
221     GSList *tmp;
222
223     dissector_all_tables_foreach_changed(decode_build_reset_list, NULL);
224
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) {
228
229         case FT_UINT8:
230         case FT_UINT16:
231         case FT_UINT24:
232         case FT_UINT32:
233             dissector_reset_uint(item->ddi_table_name,
234                                  item->ddi_selector.sel_uint);
235             break;
236
237         case FT_STRING:
238         case FT_STRINGZ:
239         case FT_UINT_STRING:
240         case FT_STRINGZPAD:
241             dissector_reset_string(item->ddi_table_name,
242                                    item->ddi_selector.sel_string);
243             g_free(item->ddi_selector.sel_string);
244             break;
245
246         default:
247             g_assert_not_reached();
248         }
249         g_free(item->ddi_table_name);
250         g_free(item);
251     }
252     g_slist_free(dissector_reset_list);
253     dissector_reset_list = NULL;
254
255     decode_dcerpc_reset_all();
256 }
257
258 static void
259 decode_as_write_entry (const gchar *table_name, ftenum_t selector_type,
260                        gpointer key, gpointer value, gpointer user_data)
261 {
262     FILE *da_file = (FILE *)user_data;
263     dissector_handle_t current, initial;
264     const gchar *current_proto_name, *initial_proto_name;
265
266     current = dtbl_entry_get_handle((dtbl_entry_t *)value);
267     if (current == NULL)
268         current_proto_name = DECODE_AS_NONE;
269     else
270         current_proto_name = dissector_handle_get_short_name(current);
271     initial = dtbl_entry_get_initial_handle((dtbl_entry_t *)value);
272     if (initial == NULL)
273         initial_proto_name = DECODE_AS_NONE;
274     else
275         initial_proto_name = dissector_handle_get_short_name(initial);
276
277     switch (selector_type) {
278
279     case FT_UINT8:
280     case FT_UINT16:
281     case FT_UINT24:
282     case FT_UINT32:
283         /*
284          * XXX - write these in decimal, regardless of the base of
285          * the dissector table's selector, as older versions of
286          * Wireshark used atoi() when reading this file, and
287          * failed to handle hex or octal numbers.
288          *
289          * That will be fixed in future 1.10 and 1.12 releases,
290          * but pre-1.10 releases are at end-of-life and won't
291          * be fixed.
292          */
293         fprintf (da_file,
294                  DECODE_AS_ENTRY ": %s,%u,%s,%s\n",
295                  table_name, GPOINTER_TO_UINT(key), initial_proto_name,
296                  current_proto_name);
297         break;
298
299     case FT_STRING:
300     case FT_STRINGZ:
301     case FT_UINT_STRING:
302     case FT_STRINGZPAD:
303         fprintf (da_file,
304                  DECODE_AS_ENTRY ": %s,%s,%s,%s\n",
305                  table_name, (gchar *)key, initial_proto_name,
306                  current_proto_name);
307         break;
308
309     default:
310         g_assert_not_reached();
311         break;
312     }
313 }
314
315 int
316 save_decode_as_entries(gchar** err)
317 {
318     char *pf_dir_path;
319     char *daf_path;
320     FILE *da_file;
321
322     if (create_persconffile_dir(&pf_dir_path) == -1) {
323         *err = g_strdup_printf("Can't create directory\n\"%s\"\nfor recent file: %s.",
324                                 pf_dir_path, g_strerror(errno));
325         g_free(pf_dir_path);
326         return -1;
327     }
328
329     daf_path = get_persconffile_path(DECODE_AS_ENTRIES_FILE_NAME, TRUE);
330     if ((da_file = ws_fopen(daf_path, "w")) == NULL) {
331         *err = g_strdup_printf("Can't open decode_as_entries file\n\"%s\": %s.",
332                                 daf_path, g_strerror(errno));
333         g_free(daf_path);
334         return -1;
335     }
336
337     fputs("# \"Decode As\" entries file for Wireshark " VERSION ".\n"
338         "#\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);
342
343     dissector_all_tables_foreach_changed(decode_as_write_entry, da_file);
344     fclose(da_file);
345     return 0;
346 }
347
348 /*
349  * Editor modelines
350  *
351  * Local Variables:
352  * c-basic-offset: 4
353  * tab-width: 8
354  * indent-tabs-mode: nil
355  * End:
356  *
357  * ex: set shiftwidth=4 tabstop=8 expandtab:
358  * :indentSize=4:tabSize=8:noTabs=true:
359  */