Remove unneeded includes from ui folder
[metze/wireshark/wip.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 "wsutil/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     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 */
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     if (strcmp(lookup->dissector_short_name, dissector_handle_get_short_name(handle)) == 0) {
84         lookup->handle = handle;
85     }
86 }
87
88 /*
89  * A callback function to parse each "decode as" entry in the file and apply the change
90  */
91 static prefs_set_pref_e
92 read_set_decode_as_entries(gchar *key, const gchar *value,
93                            void *user_data _U_,
94                            gboolean return_range_errors _U_)
95 {
96     gchar *values[4] = {NULL, NULL, NULL, NULL};
97     gchar delimiter[4] = {',', ',', ',','\0'};
98     gchar *pch;
99     guint i, j;
100     dissector_table_t sub_dissectors;
101     prefs_set_pref_e retval = PREFS_SET_OK;
102     gboolean is_valid = FALSE;
103
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]);
108             if (pch == NULL) {
109                 for (j = 0; j < i; j++) {
110                     g_free(values[j]);
111                 }
112                 return PREFS_SET_SYNTAX_ERR;
113             }
114             values[i] = g_strndup(value, pch - value);
115             value = pch + 1;
116         }
117         sub_dissectors = find_dissector_table(values[0]);
118         if (sub_dissectors != NULL) {
119             lookup_entry_t lookup;
120             ftenum_t selector_type;
121
122             lookup.dissector_short_name = values[3];
123             lookup.handle = NULL;
124             selector_type = dissector_table_get_type(sub_dissectors);
125
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) {
129                 is_valid = TRUE;
130             }
131
132             if (is_valid) {
133                 if (IS_FT_STRING(selector_type)) {
134                     dissector_change_string(values[0], values[1], lookup.handle);
135                 } else {
136                     char *p;
137                     long long_value;
138
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;
143                         is_valid = FALSE;
144                     } else
145                         dissector_change_uint(values[0], (guint)long_value, lookup.handle);
146                 }
147             }
148             if (is_valid) {
149                 decode_build_reset_list(g_strdup(values[0]), selector_type,
150                         g_strdup(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 = 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 = (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             break;
244
245         default:
246             g_assert_not_reached();
247         }
248         g_free(item);
249     }
250     g_slist_free(dissector_reset_list);
251     dissector_reset_list = NULL;
252
253     decode_dcerpc_reset_all();
254 }
255
256 static void
257 decode_as_write_entry (const gchar *table_name, ftenum_t selector_type,
258                        gpointer key, gpointer value, gpointer user_data)
259 {
260     FILE *da_file = (FILE *)user_data;
261     dissector_handle_t current, initial;
262     const gchar *current_proto_name, *initial_proto_name;
263
264     current = dtbl_entry_get_handle((dtbl_entry_t *)value);
265     if (current == NULL)
266         current_proto_name = DECODE_AS_NONE;
267     else
268         current_proto_name = dissector_handle_get_short_name(current);
269     initial = dtbl_entry_get_initial_handle((dtbl_entry_t *)value);
270     if (initial == NULL)
271         initial_proto_name = DECODE_AS_NONE;
272     else
273         initial_proto_name = dissector_handle_get_short_name(initial);
274
275     switch (selector_type) {
276
277     case FT_UINT8:
278     case FT_UINT16:
279     case FT_UINT24:
280     case FT_UINT32:
281         /*
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.
286          *
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
289          * be fixed.
290          */
291         fprintf (da_file,
292                  DECODE_AS_ENTRY ": %s,%u,%s,%s\n",
293                  table_name, GPOINTER_TO_UINT(key), initial_proto_name,
294                  current_proto_name);
295         break;
296
297     case FT_STRING:
298     case FT_STRINGZ:
299     case FT_UINT_STRING:
300     case FT_STRINGZPAD:
301         fprintf (da_file,
302                  DECODE_AS_ENTRY ": %s,%s,%s,%s\n",
303                  table_name, (gchar *)key, initial_proto_name,
304                  current_proto_name);
305         break;
306
307     default:
308         g_assert_not_reached();
309         break;
310     }
311 }
312
313 void
314 save_decode_as_entries(void)
315 {
316     char *pf_dir_path;
317     char *daf_path;
318     FILE *da_file;
319
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,
323                 g_strerror(errno));
324         g_free(pf_dir_path);
325         return;
326     }
327
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,
332             g_strerror(errno));
333         g_free(daf_path);
334         return;
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 }
346
347 /*
348  * Editor modelines
349  *
350  * Local Variables:
351  * c-basic-offset: 4
352  * tab-width: 8
353  * indent-tabs-mode: nil
354  * End:
355  *
356  * ex: set shiftwidth=4 tabstop=8 expandtab:
357  * :indentSize=4:tabSize=8:noTabs=true:
358  */