regen pidl all: rm epan/dissectors/pidl/*-stamp; pushd epan/dissectors/pidl/ && make...
[wireshark-sm.git] / epan / prefs.c
blob44feca8fbd708c17faacfcff0935c6f2a572d8ae
1 /* prefs.c
2 * Routines for handling preferences
4 * Wireshark - Network traffic analyzer
5 * By Gerald Combs <gerald@wireshark.org>
6 * Copyright 1998 Gerald Combs
8 * SPDX-License-Identifier: GPL-2.0-or-later
9 */
11 #include "config.h"
12 #define WS_LOG_DOMAIN LOG_DOMAIN_EPAN
14 #include "ws_diag_control.h"
16 #include <stdlib.h>
17 #include <string.h>
18 #include <errno.h>
19 #ifdef _WIN32
20 #include <windows.h>
21 #endif
23 #include <glib.h>
25 #include <stdio.h>
26 #include <wsutil/application_flavor.h>
27 #include <wsutil/filesystem.h>
28 #include <epan/addr_resolv.h>
29 #include <epan/oids.h>
30 #include <epan/maxmind_db.h>
31 #include <epan/packet.h>
32 #include <epan/prefs.h>
33 #include <epan/proto.h>
34 #include <epan/strutil.h>
35 #include <epan/column.h>
36 #include <epan/decode_as.h>
37 #include <ui/capture_opts.h>
38 #include <wsutil/file_util.h>
39 #include <wsutil/report_message.h>
40 #include <wsutil/wslog.h>
41 #include <wsutil/ws_assert.h>
42 #include <wsutil/array.h>
44 #include <epan/prefs-int.h>
45 #include <epan/uat-int.h>
47 #include "epan/filter_expressions.h"
49 #include "epan/wmem_scopes.h"
50 #include <epan/stats_tree.h>
52 #define REG_HKCU_WIRESHARK_KEY "Software\\Wireshark"
55 * Module alias.
57 typedef struct pref_module_alias {
58 const char *name; /**< name of module alias */
59 module_t *module; /**< module for which it's an alias */
60 } module_alias_t;
62 /* Internal functions */
63 static module_t *find_subtree(module_t *parent, const char *tilte);
64 static module_t *prefs_register_module_or_subtree(module_t *parent,
65 const char *name, const char *title, const char *description, const char *help,
66 bool is_subtree, void (*apply_cb)(void), bool use_gui);
67 static void prefs_register_modules(void);
68 static module_t *prefs_find_module_alias(const char *name);
69 static prefs_set_pref_e set_pref(char*, const char*, void *, bool);
70 static void free_col_info(GList *);
71 static void pre_init_prefs(void);
72 static bool prefs_is_column_visible(const char *cols_hidden, int col);
73 static bool prefs_is_column_fmt_visible(const char *cols_hidden, fmt_data *cfmt);
74 static unsigned prefs_module_list_foreach(wmem_tree_t *module_list, module_cb callback,
75 void *user_data, bool skip_obsolete);
76 static int find_val_for_string(const char *needle, const enum_val_t *haystack, int default_value);
78 #define IS_PREF_OBSOLETE(p) ((p) & PREF_OBSOLETE)
79 #define SET_PREF_OBSOLETE(p) ((p) |= PREF_OBSOLETE)
80 #define RESET_PREF_OBSOLETE(p) ((p) &= ~PREF_OBSOLETE)
82 #define PF_NAME "preferences"
83 #define OLD_GPF_NAME "wireshark.conf" /* old name for global preferences file */
85 static bool prefs_initialized;
86 static char *gpf_path;
87 static char *cols_hidden_list;
88 static char *cols_hidden_fmt_list;
89 static bool gui_theme_is_dark;
92 * XXX - variables to allow us to attempt to interpret the first
93 * "mgcp.{tcp,udp}.port" in a preferences file as
94 * "mgcp.{tcp,udp}.gateway_port" and the second as
95 * "mgcp.{tcp,udp}.callagent_port".
97 static int mgcp_tcp_port_count;
98 static int mgcp_udp_port_count;
100 e_prefs prefs;
102 static const enum_val_t gui_console_open_type[] = {
103 {"NEVER", "NEVER", LOG_CONSOLE_OPEN_NEVER},
104 {"AUTOMATIC", "AUTOMATIC", LOG_CONSOLE_OPEN_AUTO},
105 {"ALWAYS", "ALWAYS", LOG_CONSOLE_OPEN_ALWAYS},
106 {NULL, NULL, -1}
109 static const enum_val_t gui_version_placement_type[] = {
110 {"WELCOME", "WELCOME", version_welcome_only},
111 {"TITLE", "TITLE", version_title_only},
112 {"BOTH", "BOTH", version_both},
113 {"NEITHER", "NEITHER", version_neither},
114 {NULL, NULL, -1}
117 static const enum_val_t gui_fileopen_style[] = {
118 {"LAST_OPENED", "LAST_OPENED", FO_STYLE_LAST_OPENED},
119 {"SPECIFIED", "SPECIFIED", FO_STYLE_SPECIFIED},
120 {"CWD", "CWD", FO_STYLE_CWD},
121 {NULL, NULL, -1}
124 static const enum_val_t gui_toolbar_style[] = {
125 {"ICONS", "ICONS", 0},
126 {"TEXT", "TEXT", 1},
127 {"BOTH", "BOTH", 2},
128 {NULL, NULL, -1}
131 static const enum_val_t gui_layout_content[] = {
132 {"NONE", "NONE", 0},
133 {"PLIST", "PLIST", 1},
134 {"PDETAILS", "PDETAILS", 2},
135 {"PBYTES", "PBYTES", 3},
136 {"PDIAGRAM", "PDIAGRAM", 4},
137 {NULL, NULL, -1}
140 static const enum_val_t gui_packet_dialog_layout[] = {
141 {"vertical", "Vertical (Stacked)", layout_vertical},
142 {"horizontal", "Horizontal (Side-by-side)", layout_horizontal},
143 {NULL, NULL, -1}
146 static const enum_val_t gui_update_channel[] = {
147 {"DEVELOPMENT", "DEVELOPMENT", UPDATE_CHANNEL_DEVELOPMENT},
148 {"STABLE", "STABLE", UPDATE_CHANNEL_STABLE},
149 {NULL, NULL, -1}
152 static const enum_val_t gui_selection_style[] = {
153 {"DEFAULT", "DEFAULT", COLOR_STYLE_DEFAULT},
154 {"FLAT", "FLAT", COLOR_STYLE_FLAT},
155 {"GRADIENT", "GRADIENT", COLOR_STYLE_GRADIENT},
156 {NULL, NULL, -1}
159 static const enum_val_t gui_color_scheme[] = {
160 {"system", "System Default", COLOR_SCHEME_DEFAULT},
161 {"light", "Light Mode", COLOR_SCHEME_LIGHT},
162 {"dark", "Dark Mode", COLOR_SCHEME_DARK},
163 {NULL, NULL, -1}
166 static const enum_val_t gui_packet_list_copy_format_options_for_keyboard_shortcut[] = {
167 {"TEXT", "Text", COPY_FORMAT_TEXT},
168 {"CSV", "CSV", COPY_FORMAT_CSV},
169 {"YAML", "YAML", COPY_FORMAT_YAML},
170 {"HTML", "HTML", COPY_FORMAT_HTML},
171 {NULL, NULL, -1}
174 /* None : Historical behavior, no deinterlacing */
175 #define CONV_DEINT_CHOICE_NONE 0
176 /* MI : MAC & Interface */
177 #define CONV_DEINT_CHOICE_MI CONV_DEINT_KEY_MAC + CONV_DEINT_KEY_INTERFACE
178 /* VM : VLAN & MAC */
179 #define CONV_DEINT_CHOICE_VM CONV_DEINT_KEY_VLAN + CONV_DEINT_KEY_MAC
180 /* VMI : VLAN & MAC & Interface */
181 #define CONV_DEINT_CHOICE_VMI CONV_DEINT_KEY_VLAN + CONV_DEINT_KEY_MAC + CONV_DEINT_KEY_INTERFACE
183 static const enum_val_t conv_deint_options[] = {
184 {"NONE", "NONE", CONV_DEINT_CHOICE_NONE},
185 {".MI", ".MI", CONV_DEINT_CHOICE_MI },
186 {"VM.", "VM.", CONV_DEINT_CHOICE_VM },
187 {"VMI", "VMI", CONV_DEINT_CHOICE_VMI },
188 {NULL, NULL, -1}
191 static const enum_val_t abs_time_format_options[] = {
192 {"NEVER", "Never", ABS_TIME_ASCII_NEVER},
193 {"TREE", "Protocol tree only", ABS_TIME_ASCII_TREE},
194 {"COLUMN", "Protocol tree and columns", ABS_TIME_ASCII_COLUMN},
195 {"ALWAYS", "Always", ABS_TIME_ASCII_ALWAYS},
196 {NULL, NULL, -1}
199 #if defined(HAVE_PCAP_CREATE)
200 /* Can set monitor mode and buffer size. */
201 static int num_capture_cols = 7;
202 static const char *capture_cols[7] = {
203 "INTERFACE",
204 "LINK",
205 "PMODE",
206 "SNAPLEN",
207 "MONITOR",
208 "BUFFER",
209 "FILTER"
211 #define CAPTURE_COL_TYPE_DESCRIPTION \
212 "Possible values: INTERFACE, LINK, PMODE, SNAPLEN, MONITOR, BUFFER, FILTER\n"
213 #elif defined(CAN_SET_CAPTURE_BUFFER_SIZE)
214 /* Can set buffer size but not monitor mode. */
215 static int num_capture_cols = 6;
216 static const char *capture_cols[6] = {
217 "INTERFACE",
218 "LINK",
219 "PMODE",
220 "SNAPLEN",
221 "BUFFER",
222 "FILTER"
224 #define CAPTURE_COL_TYPE_DESCRIPTION \
225 "Possible values: INTERFACE, LINK, PMODE, SNAPLEN, BUFFER, FILTER\n"
226 #else
227 /* Can neither set buffer size nor monitor mode. */
228 static int num_capture_cols = 5;
229 static const char *capture_cols[5] = {
230 "INTERFACE",
231 "LINK",
232 "PMODE",
233 "SNAPLEN",
234 "FILTER"
236 #define CAPTURE_COL_TYPE_DESCRIPTION \
237 "Possible values: INTERFACE, LINK, PMODE, SNAPLEN, FILTER\n"
238 #endif
240 static const enum_val_t gui_packet_list_elide_mode[] = {
241 {"LEFT", "LEFT", ELIDE_LEFT},
242 {"RIGHT", "RIGHT", ELIDE_RIGHT},
243 {"MIDDLE", "MIDDLE", ELIDE_MIDDLE},
244 {"NONE", "NONE", ELIDE_NONE},
245 {NULL, NULL, -1}
248 /** Struct to hold preference data */
249 struct preference {
250 const char *name; /**< name of preference */
251 const char *title; /**< title to use in GUI */
252 const char *description; /**< human-readable description of preference */
253 int ordinal; /**< ordinal number of this preference */
254 int type; /**< type of that preference */
255 unsigned int effect_flags; /**< Flags of types effected by preference (PREF_TYPE_DISSECTION, PREF_EFFECT_CAPTURE, etc).
256 Flags must be non-zero to ensure saving to disk */
257 union { /* The Qt preference code assumes that these will all be pointers (and unique) */
258 unsigned *uint;
259 bool *boolp;
260 int *enump;
261 char **string;
262 range_t **range;
263 struct epan_uat* uat;
264 color_t *colorp;
265 GList** list;
266 } varp; /**< pointer to variable storing the value */
267 union {
268 unsigned uint;
269 bool boolval;
270 int enumval;
271 char *string;
272 range_t *range;
273 color_t color;
274 GList* list;
275 } stashed_val; /**< original value, when editing from the GUI */
276 union {
277 unsigned uint;
278 bool boolval;
279 int enumval;
280 char *string;
281 range_t *range;
282 color_t color;
283 GList* list;
284 } default_val; /**< the default value of the preference */
285 union {
286 unsigned base; /**< input/output base, for PREF_UINT */
287 uint32_t max_value; /**< maximum value of a range */
288 struct {
289 const enum_val_t *enumvals; /**< list of name & values */
290 bool radio_buttons; /**< true if it should be shown as
291 radio buttons rather than as an
292 option menu or combo box in
293 the preferences tab */
294 } enum_info; /**< for PREF_ENUM */
295 } info; /**< display/text file information */
296 struct pref_custom_cbs custom_cbs; /**< for PREF_CUSTOM */
297 const char *dissector_table; /**< for PREF_DECODE_AS_RANGE */
298 const char *dissector_desc; /**< for PREF_DECODE_AS_RANGE */
301 const char* prefs_get_description(pref_t *pref)
303 return pref->description;
306 const char* prefs_get_title(pref_t *pref)
308 return pref->title;
311 int prefs_get_type(pref_t *pref)
313 return pref->type;
316 const char* prefs_get_name(pref_t *pref)
318 return pref->name;
321 uint32_t prefs_get_max_value(pref_t *pref)
323 return pref->info.max_value;
326 const char* prefs_get_dissector_table(pref_t *pref)
328 return pref->dissector_table;
331 static const char* prefs_get_dissector_description(pref_t *pref)
333 return pref->dissector_desc;
337 * List of all modules with preference settings.
339 static wmem_tree_t *prefs_modules;
342 * List of all modules that should show up at the top level of the
343 * tree in the preference dialog box.
345 static wmem_tree_t *prefs_top_level_modules;
348 * List of aliases for modules.
350 static wmem_tree_t *prefs_module_aliases;
352 /** Sets up memory used by proto routines. Called at program startup */
353 void
354 prefs_init(void)
356 memset(&prefs, 0, sizeof(prefs));
357 prefs_modules = wmem_tree_new(wmem_epan_scope());
358 prefs_top_level_modules = wmem_tree_new(wmem_epan_scope());
359 prefs_module_aliases = wmem_tree_new(wmem_epan_scope());
363 * Free the strings for a string-like preference.
365 static void
366 free_string_like_preference(pref_t *pref)
368 g_free(*pref->varp.string);
369 *pref->varp.string = NULL;
370 g_free(pref->default_val.string);
371 pref->default_val.string = NULL;
374 static void
375 free_pref(void *data, void *user_data _U_)
377 pref_t *pref = (pref_t *)data;
378 int type = pref->type;
380 /* we reset the PREF_OBSOLETE bit in order to allow the original preference to be freed */
381 RESET_PREF_OBSOLETE(type);
383 switch (type) {
384 case PREF_BOOL:
385 case PREF_ENUM:
386 case PREF_UINT:
387 case PREF_STATIC_TEXT:
388 case PREF_UAT:
389 case PREF_COLOR:
390 break;
391 case PREF_STRING:
392 case PREF_SAVE_FILENAME:
393 case PREF_OPEN_FILENAME:
394 case PREF_DIRNAME:
395 case PREF_PASSWORD:
396 case PREF_DISSECTOR:
397 free_string_like_preference(pref);
398 break;
399 case PREF_RANGE:
400 case PREF_DECODE_AS_RANGE:
401 wmem_free(wmem_epan_scope(), *pref->varp.range);
402 *pref->varp.range = NULL;
403 wmem_free(wmem_epan_scope(), pref->default_val.range);
404 pref->default_val.range = NULL;
405 break;
406 case PREF_CUSTOM:
407 if (strcmp(pref->name, "columns") == 0)
408 pref->stashed_val.boolval = true;
409 pref->custom_cbs.free_cb(pref);
410 break;
411 /* non-generic preferences */
412 case PREF_PROTO_TCP_SNDAMB_ENUM:
413 break;
416 g_free(pref);
419 static unsigned
420 free_module_prefs(module_t *module, void *data _U_)
422 if (module->prefs) {
423 g_list_foreach(module->prefs, free_pref, NULL);
424 g_list_free(module->prefs);
426 module->prefs = NULL;
427 module->numprefs = 0;
428 if (module->submodules) {
429 prefs_module_list_foreach(module->submodules, free_module_prefs, NULL, false);
431 /* We don't free the actual module: its submodules pointer points to
432 a wmem_tree and the module itself is stored in a wmem_tree
435 return 0;
438 /** Frees memory used by proto routines. Called at program shutdown */
439 void
440 prefs_cleanup(void)
442 /* This isn't strictly necessary since we're exiting anyway, but let's
443 * do what clean up we can.
445 prefs_module_list_foreach(prefs_modules, free_module_prefs, NULL, false);
447 /* Clean the uats */
448 uat_cleanup();
450 /* Shut down mmdbresolve */
451 maxmind_db_pref_cleanup();
453 g_free(prefs.saved_at_version);
454 g_free(gpf_path);
455 gpf_path = NULL;
458 void prefs_set_gui_theme_is_dark(bool is_dark)
460 gui_theme_is_dark = is_dark;
464 * Register a module that will have preferences.
465 * Specify the module under which to register it or NULL to register it
466 * at the top level, the name used for the module in the preferences file,
467 * the title used in the tab for it in a preferences dialog box, and a
468 * routine to call back when we apply the preferences.
470 static module_t *
471 prefs_register_module(module_t *parent, const char *name, const char *title,
472 const char *description, const char *help, void (*apply_cb)(void),
473 const bool use_gui)
475 return prefs_register_module_or_subtree(parent, name, title, description, help,
476 false, apply_cb, use_gui);
479 static void
480 prefs_deregister_module(module_t *parent, const char *name, const char *title)
482 /* Remove this module from the list of all modules */
483 module_t *module = (module_t *)wmem_tree_remove_string(prefs_modules, name, WMEM_TREE_STRING_NOCASE);
485 if (!module)
486 return;
488 if (parent == NULL) {
489 /* Remove from top */
490 wmem_tree_remove_string(prefs_top_level_modules, title, WMEM_TREE_STRING_NOCASE);
491 } else if (parent->submodules) {
492 /* Remove from parent */
493 wmem_tree_remove_string(parent->submodules, title, WMEM_TREE_STRING_NOCASE);
496 free_module_prefs(module, NULL);
497 wmem_free(wmem_epan_scope(), module);
501 * Register a subtree that will have modules under it.
502 * Specify the module under which to register it or NULL to register it
503 * at the top level and the title used in the tab for it in a preferences
504 * dialog box.
506 static module_t *
507 prefs_register_subtree(module_t *parent, const char *title, const char *description,
508 void (*apply_cb)(void))
510 return prefs_register_module_or_subtree(parent, NULL, title, description, NULL,
511 true, apply_cb,
512 parent ? parent->use_gui : false);
515 static module_t *
516 prefs_register_module_or_subtree(module_t *parent, const char *name,
517 const char *title, const char *description,
518 const char *help,
519 bool is_subtree, void (*apply_cb)(void),
520 bool use_gui)
522 module_t *module;
524 /* this module may have been created as a subtree item previously */
525 if ((module = find_subtree(parent, title))) {
526 /* the module is currently a subtree */
527 module->name = name;
528 module->apply_cb = apply_cb;
529 module->description = description;
530 module->help = help;
532 if (prefs_find_module(name) == NULL) {
533 wmem_tree_insert_string(prefs_modules, name, module,
534 WMEM_TREE_STRING_NOCASE);
537 return module;
540 module = wmem_new(wmem_epan_scope(), module_t);
541 module->name = name;
542 module->title = title;
543 module->description = description;
544 module->help = help;
545 module->apply_cb = apply_cb;
546 module->prefs = NULL; /* no preferences, to start */
547 module->parent = parent;
548 module->submodules = NULL; /* no submodules, to start */
549 module->numprefs = 0;
550 module->prefs_changed_flags = 0;
551 module->obsolete = false;
552 module->use_gui = use_gui;
553 /* A module's preferences affects dissection unless otherwise told */
554 module->effect_flags = PREF_EFFECT_DISSECTION;
557 * Do we have a module name?
559 if (name != NULL) {
561 /* Accept any letter case to conform with protocol names. ASN1 protocols
562 * don't use lower case names, so we can't require lower case. */
563 if (module_check_valid_name(name, false) != '\0') {
564 ws_error("Preference module \"%s\" contains invalid characters", name);
568 * Make sure there's not already a module with that
569 * name. Crash if there is, as that's an error in the
570 * code, and the code has to be fixed not to register
571 * more than one module with the same name.
573 * We search the list of all modules; the subtree stuff
574 * doesn't require preferences in subtrees to have names
575 * that reflect the subtree they're in (that would require
576 * protocol preferences to have a bogus "protocol.", or
577 * something such as that, to be added to all their names).
579 if (prefs_find_module(name) != NULL)
580 ws_error("Preference module \"%s\" is being registered twice", name);
583 * Insert this module in the list of all modules.
585 wmem_tree_insert_string(prefs_modules, name, module, WMEM_TREE_STRING_NOCASE);
586 } else {
588 * This has no name, just a title; check to make sure it's a
589 * subtree, and crash if it's not.
591 if (!is_subtree)
592 ws_error("Preferences module with no name is being registered at the top level");
596 * Insert this module into the appropriate place in the display
597 * tree.
599 if (parent == NULL) {
601 * It goes at the top.
603 wmem_tree_insert_string(prefs_top_level_modules, title, module, WMEM_TREE_STRING_NOCASE);
604 } else {
606 * It goes into the list for this module.
609 if (parent->submodules == NULL)
610 parent->submodules = wmem_tree_new(wmem_epan_scope());
612 wmem_tree_insert_string(parent->submodules, title, module, WMEM_TREE_STRING_NOCASE);
615 return module;
618 void
619 prefs_register_module_alias(const char *name, module_t *module)
621 module_alias_t *alias;
624 * Accept any name that can occur in protocol names. We allow upper-case
625 * letters, to handle the Diameter dissector having used "Diameter" rather
626 * than "diameter" as its preference module name in the past.
628 * Crash if the name is invalid, as that's an error in the code, but the name
629 * can be used on the command line, and shouldn't require quoting, etc.
631 if (module_check_valid_name(name, false) != '\0') {
632 ws_error("Preference module alias \"%s\" contains invalid characters", name);
636 * Make sure there's not already an alias with that
637 * name. Crash if there is, as that's an error in the
638 * code, and the code has to be fixed not to register
639 * more than one alias with the same name.
641 * We search the list of all aliases.
643 if (prefs_find_module_alias(name) != NULL)
644 ws_error("Preference module alias \"%s\" is being registered twice", name);
646 alias = wmem_new(wmem_epan_scope(), module_alias_t);
647 alias->name = name;
648 alias->module = module;
651 * Insert this module in the list of all modules.
653 wmem_tree_insert_string(prefs_module_aliases, name, alias, WMEM_TREE_STRING_NOCASE);
657 * Register that a protocol has preferences.
659 module_t *protocols_module;
661 module_t *
662 prefs_register_protocol(int id, void (*apply_cb)(void))
664 protocol_t *protocol;
667 * Have we yet created the "Protocols" subtree?
669 if (protocols_module == NULL) {
671 * No. Register Protocols subtree as well as any preferences
672 * for non-dissector modules.
674 pre_init_prefs();
675 prefs_register_modules();
677 protocol = find_protocol_by_id(id);
678 if (protocol == NULL)
679 ws_error("Protocol preferences being registered with an invalid protocol ID");
680 return prefs_register_module(protocols_module,
681 proto_get_protocol_filter_name(id),
682 proto_get_protocol_short_name(protocol),
683 proto_get_protocol_name(id), NULL, apply_cb, true);
686 void
687 prefs_deregister_protocol (int id)
689 protocol_t *protocol = find_protocol_by_id(id);
690 if (protocol == NULL)
691 ws_error("Protocol preferences being de-registered with an invalid protocol ID");
692 prefs_deregister_module (protocols_module,
693 proto_get_protocol_filter_name(id),
694 proto_get_protocol_short_name(protocol));
697 module_t *
698 prefs_register_protocol_subtree(const char *subtree, int id, void (*apply_cb)(void))
700 protocol_t *protocol;
701 module_t *subtree_module;
702 module_t *new_module;
703 char *sep = NULL, *ptr = NULL, *orig = NULL;
706 * Have we yet created the "Protocols" subtree?
707 * XXX - can we just do this by registering Protocols/{subtree}?
708 * If not, why not?
710 if (protocols_module == NULL) {
712 * No. Register Protocols subtree as well as any preferences
713 * for non-dissector modules.
715 pre_init_prefs();
716 prefs_register_modules();
719 subtree_module = protocols_module;
721 if (subtree) {
722 /* take a copy of the buffer, orig keeps a base pointer while ptr
723 * walks through the string */
724 orig = ptr = g_strdup(subtree);
726 while (ptr && *ptr) {
728 if ((sep = strchr(ptr, '/')))
729 *sep++ = '\0';
731 if (!(new_module = find_subtree(subtree_module, ptr))) {
733 * There's no such module; create it, with the description
734 * being the name (if it's later registered explicitly
735 * with a description, that will override it).
737 ptr = wmem_strdup(wmem_epan_scope(), ptr);
738 new_module = prefs_register_subtree(subtree_module, ptr, ptr, NULL);
741 subtree_module = new_module;
742 ptr = sep;
746 g_free(orig);
749 protocol = find_protocol_by_id(id);
750 if (protocol == NULL)
751 ws_error("Protocol subtree being registered with an invalid protocol ID");
752 return prefs_register_module(subtree_module,
753 proto_get_protocol_filter_name(id),
754 proto_get_protocol_short_name(protocol),
755 proto_get_protocol_name(id), NULL, apply_cb, true);
760 * Register that a protocol used to have preferences but no longer does,
761 * by creating an "obsolete" module for it.
763 module_t *
764 prefs_register_protocol_obsolete(int id)
766 module_t *module;
767 protocol_t *protocol;
770 * Have we yet created the "Protocols" subtree?
772 if (protocols_module == NULL) {
774 * No. Register Protocols subtree as well as any preferences
775 * for non-dissector modules.
777 pre_init_prefs();
778 prefs_register_modules();
780 protocol = find_protocol_by_id(id);
781 if (protocol == NULL)
782 ws_error("Protocol being registered with an invalid protocol ID");
783 module = prefs_register_module(protocols_module,
784 proto_get_protocol_filter_name(id),
785 proto_get_protocol_short_name(protocol),
786 proto_get_protocol_name(id), NULL, NULL, true);
787 module->obsolete = true;
788 return module;
792 * Register that a statistical tap has preferences.
794 * "name" is a name for the tap to use on the command line with "-o"
795 * and in preference files.
797 * "title" is a short human-readable name for the tap.
799 * "description" is a longer human-readable description of the tap.
801 module_t *stats_module;
803 module_t *
804 prefs_register_stat(const char *name, const char *title,
805 const char *description, void (*apply_cb)(void))
808 * Have we yet created the "Statistics" subtree?
810 if (stats_module == NULL) {
812 * No. Register Statistics subtree as well as any preferences
813 * for non-dissector modules.
815 pre_init_prefs();
816 prefs_register_modules();
819 return prefs_register_module(stats_module, name, title, description, NULL,
820 apply_cb, true);
824 * Register that a codec has preferences.
826 * "name" is a name for the codec to use on the command line with "-o"
827 * and in preference files.
829 * "title" is a short human-readable name for the codec.
831 * "description" is a longer human-readable description of the codec.
833 module_t *codecs_module;
835 module_t *
836 prefs_register_codec(const char *name, const char *title,
837 const char *description, void (*apply_cb)(void))
840 * Have we yet created the "Codecs" subtree?
842 if (codecs_module == NULL) {
844 * No. Register Codecs subtree as well as any preferences
845 * for non-dissector modules.
847 pre_init_prefs();
848 prefs_register_modules();
851 return prefs_register_module(codecs_module, name, title, description, NULL,
852 apply_cb, true);
855 module_t *
856 prefs_find_module(const char *name)
858 return (module_t *)wmem_tree_lookup_string(prefs_modules, name, WMEM_TREE_STRING_NOCASE);
861 static module_t *
862 find_subtree(module_t *parent, const char *name)
864 return (module_t *)wmem_tree_lookup_string(parent ? parent->submodules : prefs_top_level_modules, name, WMEM_TREE_STRING_NOCASE);
868 * Call a callback function, with a specified argument, for each module
869 * in a list of modules. If the list is NULL, searches the top-level
870 * list in the display tree of modules. If any callback returns a
871 * non-zero value, we stop and return that value, otherwise we
872 * return 0.
874 * Normally "obsolete" modules are ignored; their sole purpose is to allow old
875 * preferences for dissectors that no longer have preferences to be
876 * silently ignored in preference files. Does not ignore subtrees,
877 * as this can be used when walking the display tree of modules.
880 typedef struct {
881 module_cb callback;
882 void *user_data;
883 unsigned ret;
884 bool skip_obsolete;
885 } call_foreach_t;
887 static bool
888 call_foreach_cb(const void *key _U_, void *value, void *data)
890 module_t *module = (module_t*)value;
891 call_foreach_t *call_data = (call_foreach_t*)data;
893 if (!call_data->skip_obsolete || !module->obsolete)
894 call_data->ret = (*call_data->callback)(module, call_data->user_data);
896 return (call_data->ret != 0);
899 static unsigned
900 prefs_module_list_foreach(wmem_tree_t *module_list, module_cb callback,
901 void *user_data, bool skip_obsolete)
903 call_foreach_t call_data;
905 if (module_list == NULL)
906 module_list = prefs_top_level_modules;
908 call_data.callback = callback;
909 call_data.user_data = user_data;
910 call_data.ret = 0;
911 call_data.skip_obsolete = skip_obsolete;
912 wmem_tree_foreach(module_list, call_foreach_cb, &call_data);
913 return call_data.ret;
917 * Returns true if module has any submodules
919 bool
920 prefs_module_has_submodules(module_t *module)
922 if (module->submodules == NULL) {
923 return false;
926 if (wmem_tree_is_empty(module->submodules)) {
927 return false;
930 return true;
934 * Call a callback function, with a specified argument, for each module
935 * in the list of all modules. (This list does not include subtrees.)
937 * Ignores "obsolete" modules; their sole purpose is to allow old
938 * preferences for dissectors that no longer have preferences to be
939 * silently ignored in preference files.
941 unsigned
942 prefs_modules_foreach(module_cb callback, void *user_data)
944 return prefs_module_list_foreach(prefs_modules, callback, user_data, true);
948 * Call a callback function, with a specified argument, for each submodule
949 * of specified modules. If the module is NULL, goes through the top-level
950 * list in the display tree of modules.
952 * Ignores "obsolete" modules; their sole purpose is to allow old
953 * preferences for dissectors that no longer have preferences to be
954 * silently ignored in preference files. Does not ignore subtrees,
955 * as this can be used when walking the display tree of modules.
957 unsigned
958 prefs_modules_foreach_submodules(module_t *module, module_cb callback,
959 void *user_data)
961 return prefs_module_list_foreach((module)?module->submodules:prefs_top_level_modules, callback, user_data, true);
964 static bool
965 call_apply_cb(const void *key _U_, void *value, void *data _U_)
967 module_t *module = (module_t *)value;
969 if (module->obsolete)
970 return false;
971 if (module->prefs_changed_flags) {
972 if (module->apply_cb != NULL)
973 (*module->apply_cb)();
974 module->prefs_changed_flags = 0;
976 if (module->submodules)
977 wmem_tree_foreach(module->submodules, call_apply_cb, NULL);
978 return false;
982 * Call the "apply" callback function for each module if any of its
983 * preferences have changed, and then clear the flag saying its
984 * preferences have changed, as the module has been notified of that
985 * fact.
987 void
988 prefs_apply_all(void)
990 wmem_tree_foreach(prefs_modules, call_apply_cb, NULL);
994 * Call the "apply" callback function for a specific module if any of
995 * its preferences have changed, and then clear the flag saying its
996 * preferences have changed, as the module has been notified of that
997 * fact.
999 void
1000 prefs_apply(module_t *module)
1002 if (module && module->prefs_changed_flags)
1003 call_apply_cb(NULL, module, NULL);
1006 static module_t *
1007 prefs_find_module_alias(const char *name)
1009 module_alias_t *alias;
1011 alias = (module_alias_t *)wmem_tree_lookup_string(prefs_module_aliases, name, WMEM_TREE_STRING_NOCASE);
1012 if (alias == NULL)
1013 return NULL;
1014 return alias->module;
1018 * Register a preference in a module's list of preferences.
1019 * If it has a title, give it an ordinal number; otherwise, it's a
1020 * preference that won't show up in the UI, so it shouldn't get an
1021 * ordinal number (the ordinal should be the ordinal in the set of
1022 * *visible* preferences).
1024 static pref_t *
1025 register_preference(module_t *module, const char *name, const char *title,
1026 const char *description, int type)
1028 pref_t *preference;
1029 const char *p;
1030 const char *name_prefix = (module->name != NULL) ? module->name : module->parent->name;
1032 preference = g_new(pref_t,1);
1033 preference->name = name;
1034 preference->title = title;
1035 preference->description = description;
1036 preference->type = type;
1037 /* Default to module's preference effects */
1038 preference->effect_flags = module->effect_flags;
1040 if (title != NULL)
1041 preference->ordinal = module->numprefs;
1042 else
1043 preference->ordinal = -1; /* no ordinal for you */
1046 * Make sure that only lower-case ASCII letters, numbers,
1047 * underscores, and dots appear in the preference name.
1049 * Crash if there is, as that's an error in the code;
1050 * you can make the title and description nice strings
1051 * with capitalization, white space, punctuation, etc.,
1052 * but the name can be used on the command line,
1053 * and shouldn't require quoting, shifting, etc.
1055 for (p = name; *p != '\0'; p++)
1056 if (!(g_ascii_islower(*p) || g_ascii_isdigit(*p) || *p == '_' || *p == '.'))
1057 ws_error("Preference \"%s.%s\" contains invalid characters", module->name, name);
1060 * Make sure there's not already a preference with that
1061 * name. Crash if there is, as that's an error in the
1062 * code, and the code has to be fixed not to register
1063 * more than one preference with the same name.
1065 if (prefs_find_preference(module, name) != NULL)
1066 ws_error("Preference %s has already been registered", name);
1068 if ((!IS_PREF_OBSOLETE(type)) &&
1069 /* Don't compare if it's a subtree */
1070 (module->name != NULL)) {
1072 * Make sure the preference name doesn't begin with the
1073 * module name, as that's redundant and Just Silly.
1075 if (!((strncmp(name, module->name, strlen(module->name)) != 0) ||
1076 (((name[strlen(module->name)]) != '.') && ((name[strlen(module->name)]) != '_'))))
1077 ws_error("Preference %s begins with the module name", name);
1080 /* The title shows up in the preferences dialog. Make sure it's UI-friendly. */
1081 if (preference->title) {
1082 const char *cur_char;
1083 if (preference->type != PREF_STATIC_TEXT && g_utf8_strlen(preference->title, -1) > 80) { // Arbitrary.
1084 ws_error("Title for preference %s.%s is too long: %s", name_prefix, preference->name, preference->title);
1087 if (!g_utf8_validate(preference->title, -1, NULL)) {
1088 ws_error("Title for preference %s.%s isn't valid UTF-8.", name_prefix, preference->name);
1091 for (cur_char = preference->title; *cur_char; cur_char = g_utf8_next_char(cur_char)) {
1092 if (!g_unichar_isprint(g_utf8_get_char(cur_char))) {
1093 ws_error("Title for preference %s.%s isn't printable UTF-8.", name_prefix, preference->name);
1098 if (preference->description) {
1099 if (!g_utf8_validate(preference->description, -1, NULL)) {
1100 ws_error("Description for preference %s.%s isn't valid UTF-8.", name_prefix, preference->name);
1105 * We passed all of our checks. Add the preference.
1107 module->prefs = g_list_append(module->prefs, preference);
1108 if (title != NULL)
1109 module->numprefs++;
1111 return preference;
1115 * Find a preference in a module's list of preferences, given the module
1116 * and the preference's name.
1118 typedef struct {
1119 GList *list_entry;
1120 const char *name;
1121 module_t *submodule;
1122 } find_pref_arg_t;
1124 static int
1125 preference_match(const void *a, const void *b)
1127 const pref_t *pref = (const pref_t *)a;
1128 const char *name = (const char *)b;
1130 return strcmp(name, pref->name);
1133 static bool
1134 module_find_pref_cb(const void *key _U_, void *value, void *data)
1136 find_pref_arg_t* arg = (find_pref_arg_t*)data;
1137 GList *list_entry;
1138 module_t *module = (module_t *)value;
1140 if (module == NULL)
1141 return false;
1143 list_entry = g_list_find_custom(module->prefs, arg->name,
1144 preference_match);
1146 if (list_entry == NULL)
1147 return false;
1149 arg->list_entry = list_entry;
1150 arg->submodule = module;
1151 return true;
1154 /* Tries to find a preference, setting containing_module to the (sub)module
1155 * holding this preference. */
1156 static pref_t *
1157 prefs_find_preference_with_submodule(module_t *module, const char *name,
1158 module_t **containing_module)
1160 find_pref_arg_t arg;
1161 GList *list_entry;
1163 if (module == NULL)
1164 return NULL; /* invalid parameters */
1166 list_entry = g_list_find_custom(module->prefs, name,
1167 preference_match);
1168 arg.submodule = NULL;
1170 if (list_entry == NULL)
1172 arg.list_entry = NULL;
1173 if (module->submodules != NULL)
1175 arg.name = name;
1176 wmem_tree_foreach(module->submodules, module_find_pref_cb, &arg);
1179 list_entry = arg.list_entry;
1182 if (list_entry == NULL)
1183 return NULL; /* no such preference */
1185 if (containing_module)
1186 *containing_module = arg.submodule ? arg.submodule : module;
1188 return (pref_t *) list_entry->data;
1191 pref_t *
1192 prefs_find_preference(module_t *module, const char *name)
1194 return prefs_find_preference_with_submodule(module, name, NULL);
1198 * Returns true if the given protocol has registered preferences
1200 bool
1201 prefs_is_registered_protocol(const char *name)
1203 module_t *m = prefs_find_module(name);
1205 return (m != NULL && !m->obsolete);
1209 * Returns the module title of a registered protocol
1211 const char *
1212 prefs_get_title_by_name(const char *name)
1214 module_t *m = prefs_find_module(name);
1216 return (m != NULL && !m->obsolete) ? m->title : NULL;
1220 * Register a preference with an unsigned integral value.
1222 void
1223 prefs_register_uint_preference(module_t *module, const char *name,
1224 const char *title, const char *description,
1225 unsigned base, unsigned *var)
1227 pref_t *preference;
1229 preference = register_preference(module, name, title, description,
1230 PREF_UINT);
1231 preference->varp.uint = var;
1232 preference->default_val.uint = *var;
1233 ws_assert(base > 0 && base != 1 && base < 37);
1234 preference->info.base = base;
1238 * XXX Add a prefs_register_{uint16|port}_preference which sets max_value?
1243 * Register a "custom" preference with a unsigned integral value.
1244 * XXX - This should be temporary until we can find a better way
1245 * to do "custom" preferences
1247 static void
1248 prefs_register_uint_custom_preference(module_t *module, const char *name,
1249 const char *title, const char *description,
1250 struct pref_custom_cbs* custom_cbs, unsigned *var)
1252 pref_t *preference;
1254 preference = register_preference(module, name, title, description,
1255 PREF_CUSTOM);
1257 preference->custom_cbs = *custom_cbs;
1258 preference->varp.uint = var;
1259 preference->default_val.uint = *var;
1263 * Register a preference with an Boolean value.
1265 void
1266 prefs_register_bool_preference(module_t *module, const char *name,
1267 const char *title, const char *description,
1268 bool *var)
1270 pref_t *preference;
1272 preference = register_preference(module, name, title, description,
1273 PREF_BOOL);
1274 preference->varp.boolp = var;
1275 preference->default_val.boolval = *var;
1278 unsigned int prefs_set_bool_value(pref_t *pref, bool value, pref_source_t source)
1280 unsigned int changed = 0;
1282 switch (source)
1284 case pref_default:
1285 if (pref->default_val.boolval != value) {
1286 pref->default_val.boolval = value;
1287 changed = prefs_get_effect_flags(pref);
1289 break;
1290 case pref_stashed:
1291 if (pref->stashed_val.boolval != value) {
1292 pref->stashed_val.boolval = value;
1293 changed = prefs_get_effect_flags(pref);
1295 break;
1296 case pref_current:
1297 if (*pref->varp.boolp != value) {
1298 *pref->varp.boolp = value;
1299 changed = prefs_get_effect_flags(pref);
1301 break;
1302 default:
1303 ws_assert_not_reached();
1304 break;
1307 return changed;
1310 void prefs_invert_bool_value(pref_t *pref, pref_source_t source)
1312 switch (source)
1314 case pref_default:
1315 pref->default_val.boolval = !pref->default_val.boolval;
1316 break;
1317 case pref_stashed:
1318 pref->stashed_val.boolval = !pref->stashed_val.boolval;
1319 break;
1320 case pref_current:
1321 *pref->varp.boolp = !(*pref->varp.boolp);
1322 break;
1323 default:
1324 ws_assert_not_reached();
1325 break;
1329 bool prefs_get_bool_value(pref_t *pref, pref_source_t source)
1331 switch (source)
1333 case pref_default:
1334 return pref->default_val.boolval;
1335 case pref_stashed:
1336 return pref->stashed_val.boolval;
1337 case pref_current:
1338 return *pref->varp.boolp;
1339 default:
1340 ws_assert_not_reached();
1341 break;
1344 return false;
1348 * Register a preference with an enumerated value.
1351 * XXX Should we get rid of the radio_buttons parameter and make that
1352 * behavior automatic depending on the number of items?
1354 void
1355 prefs_register_enum_preference(module_t *module, const char *name,
1356 const char *title, const char *description,
1357 int *var, const enum_val_t *enumvals,
1358 bool radio_buttons)
1360 pref_t *preference;
1362 /* Validate that the "name one would use on the command line for the value"
1363 * doesn't require quoting, etc. It's all treated case-insensitively so we
1364 * don't care about upper vs lower case.
1366 for (size_t i = 0; enumvals[i].name != NULL; i++) {
1367 for (const char *p = enumvals[i].name; *p != '\0'; p++)
1368 if (!(g_ascii_isalnum(*p) || *p == '_' || *p == '.' || *p == '-'))
1369 ws_error("Preference \"%s.%s\" enum value name \"%s\" contains invalid characters",
1370 module->name, name, enumvals[i].name);
1374 preference = register_preference(module, name, title, description,
1375 PREF_ENUM);
1376 preference->varp.enump = var;
1377 preference->default_val.enumval = *var;
1378 preference->info.enum_info.enumvals = enumvals;
1379 preference->info.enum_info.radio_buttons = radio_buttons;
1382 unsigned int prefs_set_enum_value(pref_t *pref, int value, pref_source_t source)
1384 unsigned int changed = 0;
1386 switch (source)
1388 case pref_default:
1389 if (pref->default_val.enumval != value) {
1390 pref->default_val.enumval = value;
1391 changed = prefs_get_effect_flags(pref);
1393 break;
1394 case pref_stashed:
1395 if (pref->stashed_val.enumval != value) {
1396 pref->stashed_val.enumval = value;
1397 changed = prefs_get_effect_flags(pref);
1399 break;
1400 case pref_current:
1401 if (*pref->varp.enump != value) {
1402 *pref->varp.enump = value;
1403 changed = prefs_get_effect_flags(pref);
1405 break;
1406 default:
1407 ws_assert_not_reached();
1408 break;
1411 return changed;
1414 unsigned int prefs_set_enum_string_value(pref_t *pref, const char *value, pref_source_t source)
1416 int enum_val = find_val_for_string(value, pref->info.enum_info.enumvals, *pref->varp.enump);
1418 return prefs_set_enum_value(pref, enum_val, source);
1421 int prefs_get_enum_value(pref_t *pref, pref_source_t source)
1423 switch (source)
1425 case pref_default:
1426 return pref->default_val.enumval;
1427 case pref_stashed:
1428 return pref->stashed_val.enumval;
1429 case pref_current:
1430 return *pref->varp.enump;
1431 default:
1432 ws_assert_not_reached();
1433 break;
1436 return 0;
1439 const enum_val_t* prefs_get_enumvals(pref_t *pref)
1441 return pref->info.enum_info.enumvals;
1444 bool prefs_get_enum_radiobuttons(pref_t *pref)
1446 return pref->info.enum_info.radio_buttons;
1450 * For use by UI code that sets preferences.
1452 unsigned int
1453 prefs_set_custom_value(pref_t *pref, const char *value, pref_source_t source _U_)
1455 /* XXX - support pref source for custom preferences */
1456 unsigned int changed = 0;
1457 pref->custom_cbs.set_cb(pref, value, &changed);
1458 return changed;
1461 static void
1462 register_string_like_preference(module_t *module, const char *name,
1463 const char *title, const char *description,
1464 char **var, int type,
1465 struct pref_custom_cbs* custom_cbs,
1466 bool free_tmp)
1468 pref_t *pref;
1469 char *tmp;
1471 pref = register_preference(module, name, title, description, type);
1474 * String preference values should be non-null (as you can't
1475 * keep them null after using the preferences GUI, you can at best
1476 * have them be null strings) and freeable (as we free them
1477 * if we change them).
1479 * If the value is a null pointer, make it a copy of a null
1480 * string, otherwise make it a copy of the value.
1482 tmp = *var;
1483 if (*var == NULL) {
1484 *var = g_strdup("");
1485 } else {
1486 *var = g_strdup(*var);
1488 if (free_tmp) {
1489 g_free(tmp);
1491 pref->varp.string = var;
1492 pref->default_val.string = g_strdup(*var);
1493 pref->stashed_val.string = NULL;
1494 if (type == PREF_CUSTOM) {
1495 ws_assert(custom_cbs);
1496 pref->custom_cbs = *custom_cbs;
1501 * Assign to a string preference.
1503 static void
1504 pref_set_string_like_pref_value(pref_t *pref, const char *value)
1506 DIAG_OFF(cast-qual)
1507 g_free((void *)*pref->varp.string);
1508 DIAG_ON(cast-qual)
1509 *pref->varp.string = g_strdup(value);
1513 * For use by UI code that sets preferences.
1515 unsigned int
1516 prefs_set_string_value(pref_t *pref, const char* value, pref_source_t source)
1518 unsigned int changed = 0;
1520 switch (source)
1522 case pref_default:
1523 if (*pref->default_val.string) {
1524 if (strcmp(pref->default_val.string, value) != 0) {
1525 changed = prefs_get_effect_flags(pref);
1526 g_free(pref->default_val.string);
1527 pref->default_val.string = g_strdup(value);
1529 } else if (value) {
1530 pref->default_val.string = g_strdup(value);
1532 break;
1533 case pref_stashed:
1534 if (pref->stashed_val.string) {
1535 if (strcmp(pref->stashed_val.string, value) != 0) {
1536 changed = prefs_get_effect_flags(pref);
1537 g_free(pref->stashed_val.string);
1538 pref->stashed_val.string = g_strdup(value);
1540 } else if (value) {
1541 pref->stashed_val.string = g_strdup(value);
1543 break;
1544 case pref_current:
1545 if (*pref->varp.string) {
1546 if (strcmp(*pref->varp.string, value) != 0) {
1547 changed = prefs_get_effect_flags(pref);
1548 pref_set_string_like_pref_value(pref, value);
1550 } else if (value) {
1551 pref_set_string_like_pref_value(pref, value);
1553 break;
1554 default:
1555 ws_assert_not_reached();
1556 break;
1559 return changed;
1562 char* prefs_get_string_value(pref_t *pref, pref_source_t source)
1564 switch (source)
1566 case pref_default:
1567 return pref->default_val.string;
1568 case pref_stashed:
1569 return pref->stashed_val.string;
1570 case pref_current:
1571 return *pref->varp.string;
1572 default:
1573 ws_assert_not_reached();
1574 break;
1577 return NULL;
1581 * Reset the value of a string-like preference.
1583 static void
1584 reset_string_like_preference(pref_t *pref)
1586 g_free(*pref->varp.string);
1587 *pref->varp.string = g_strdup(pref->default_val.string);
1591 * Register a preference with a character-string value.
1593 void
1594 prefs_register_string_preference(module_t *module, const char *name,
1595 const char *title, const char *description,
1596 const char **var)
1598 DIAG_OFF(cast-qual)
1599 register_string_like_preference(module, name, title, description,
1600 (char **)var, PREF_STRING, NULL, false);
1601 DIAG_ON(cast-qual)
1605 * Register a preference with a file name (string) value.
1607 void
1608 prefs_register_filename_preference(module_t *module, const char *name,
1609 const char *title, const char *description,
1610 const char **var, bool for_writing)
1612 DIAG_OFF(cast-qual)
1613 register_string_like_preference(module, name, title, description, (char **)var,
1614 for_writing ? PREF_SAVE_FILENAME : PREF_OPEN_FILENAME, NULL, false);
1615 DIAG_ON(cast-qual)
1619 * Register a preference with a directory name (string) value.
1621 void
1622 prefs_register_directory_preference(module_t *module, const char *name,
1623 const char *title, const char *description,
1624 const char **var)
1626 DIAG_OFF(cast-qual)
1627 register_string_like_preference(module, name, title, description,
1628 (char **)var, PREF_DIRNAME, NULL, false);
1629 DIAG_ON(cast-qual)
1632 /* Refactoring to handle both PREF_RANGE and PREF_DECODE_AS_RANGE */
1633 static pref_t*
1634 prefs_register_range_preference_common(module_t *module, const char *name,
1635 const char *title, const char *description,
1636 range_t **var, uint32_t max_value, int type)
1638 pref_t *preference;
1640 preference = register_preference(module, name, title, description, type);
1641 preference->info.max_value = max_value;
1644 * Range preference values should be non-null (as you can't
1645 * keep them null after using the preferences GUI, you can at best
1646 * have them be empty ranges) and freeable (as we free them
1647 * if we change them).
1649 * If the value is a null pointer, make it an empty range.
1651 if (*var == NULL)
1652 *var = range_empty(wmem_epan_scope());
1653 preference->varp.range = var;
1654 preference->default_val.range = range_copy(wmem_epan_scope(), *var);
1655 preference->stashed_val.range = NULL;
1657 return preference;
1661 * Register a preference with a ranged value.
1663 void
1664 prefs_register_range_preference(module_t *module, const char *name,
1665 const char *title, const char *description,
1666 range_t **var, uint32_t max_value)
1668 prefs_register_range_preference_common(module, name, title,
1669 description, var, max_value, PREF_RANGE);
1672 bool
1673 prefs_set_range_value_work(pref_t *pref, const char *value,
1674 bool return_range_errors, unsigned int *changed_flags)
1676 range_t *newrange;
1678 if (range_convert_str_work(wmem_epan_scope(), &newrange, value, pref->info.max_value,
1679 return_range_errors) != CVT_NO_ERROR) {
1680 return false; /* number was bad */
1683 if (!ranges_are_equal(*pref->varp.range, newrange)) {
1684 *changed_flags |= prefs_get_effect_flags(pref);
1685 wmem_free(wmem_epan_scope(), *pref->varp.range);
1686 *pref->varp.range = newrange;
1687 } else {
1688 wmem_free(wmem_epan_scope(), newrange);
1690 return true;
1694 * For use by UI code that sets preferences.
1696 unsigned int
1697 prefs_set_stashed_range_value(pref_t *pref, const char *value)
1699 range_t *newrange;
1701 if (range_convert_str_work(wmem_epan_scope(), &newrange, value, pref->info.max_value,
1702 true) != CVT_NO_ERROR) {
1703 return 0; /* number was bad */
1706 if (!ranges_are_equal(pref->stashed_val.range, newrange)) {
1707 wmem_free(wmem_epan_scope(), pref->stashed_val.range);
1708 pref->stashed_val.range = newrange;
1709 } else {
1710 wmem_free(wmem_epan_scope(), newrange);
1712 return prefs_get_effect_flags(pref);
1716 bool prefs_add_list_value(pref_t *pref, void* value, pref_source_t source)
1718 switch (source)
1720 case pref_default:
1721 pref->default_val.list = g_list_prepend(pref->default_val.list, value);
1722 break;
1723 case pref_stashed:
1724 pref->stashed_val.list = g_list_prepend(pref->stashed_val.list, value);
1725 break;
1726 case pref_current:
1727 *pref->varp.list = g_list_prepend(*pref->varp.list, value);
1728 break;
1729 default:
1730 ws_assert_not_reached();
1731 break;
1734 return true;
1737 GList* prefs_get_list_value(pref_t *pref, pref_source_t source)
1739 switch (source)
1741 case pref_default:
1742 return pref->default_val.list;
1743 case pref_stashed:
1744 return pref->stashed_val.list;
1745 case pref_current:
1746 return *pref->varp.list;
1747 default:
1748 ws_assert_not_reached();
1749 break;
1752 return NULL;
1755 bool prefs_set_range_value(pref_t *pref, range_t *value, pref_source_t source)
1757 bool changed = false;
1759 switch (source)
1761 case pref_default:
1762 if (!ranges_are_equal(pref->default_val.range, value)) {
1763 wmem_free(wmem_epan_scope(), pref->default_val.range);
1764 pref->default_val.range = range_copy(wmem_epan_scope(), value);
1765 changed = true;
1767 break;
1768 case pref_stashed:
1769 if (!ranges_are_equal(pref->stashed_val.range, value)) {
1770 wmem_free(wmem_epan_scope(), pref->stashed_val.range);
1771 pref->stashed_val.range = range_copy(wmem_epan_scope(), value);
1772 changed = true;
1774 break;
1775 case pref_current:
1776 if (!ranges_are_equal(*pref->varp.range, value)) {
1777 wmem_free(wmem_epan_scope(), *pref->varp.range);
1778 *pref->varp.range = range_copy(wmem_epan_scope(), value);
1779 changed = true;
1781 break;
1782 default:
1783 ws_assert_not_reached();
1784 break;
1787 return changed;
1790 range_t* prefs_get_range_value_real(pref_t *pref, pref_source_t source)
1792 switch (source)
1794 case pref_default:
1795 return pref->default_val.range;
1796 case pref_stashed:
1797 return pref->stashed_val.range;
1798 case pref_current:
1799 return *pref->varp.range;
1800 default:
1801 ws_assert_not_reached();
1802 break;
1805 return NULL;
1808 range_t* prefs_get_range_value(const char *module_name, const char* pref_name)
1810 pref_t *pref = prefs_find_preference(prefs_find_module(module_name), pref_name);
1811 if (pref == NULL) {
1812 return NULL;
1814 return prefs_get_range_value_real(pref, pref_current);
1817 void
1818 prefs_range_add_value(pref_t *pref, uint32_t val)
1820 range_add_value(wmem_epan_scope(), pref->varp.range, val);
1823 void
1824 prefs_range_remove_value(pref_t *pref, uint32_t val)
1826 range_remove_value(wmem_epan_scope(), pref->varp.range, val);
1830 * Register a static text 'preference'. It can be used to add explanatory
1831 * text inline with other preferences in the GUI.
1832 * Note: Static preferences are not saved to the preferences file.
1834 void
1835 prefs_register_static_text_preference(module_t *module, const char *name,
1836 const char *title,
1837 const char *description)
1839 register_preference(module, name, title, description, PREF_STATIC_TEXT);
1843 * Register a uat 'preference'. It adds a button that opens the uat's window in the
1844 * preferences tab of the module.
1846 extern void
1847 prefs_register_uat_preference(module_t *module, const char *name,
1848 const char *title, const char *description,
1849 uat_t* uat)
1852 pref_t* preference = register_preference(module, name, title, description, PREF_UAT);
1854 preference->varp.uat = uat;
1858 * Register a uat 'preference' for QT only. It adds a button that opens the uat's window in the
1859 * preferences tab of the module.
1861 extern void
1862 prefs_register_uat_preference_qt(module_t *module, const char *name,
1863 const char *title, const char *description,
1864 uat_t* uat)
1867 pref_t* preference = register_preference(module, name, title, description, PREF_UAT);
1869 preference->varp.uat = uat;
1872 struct epan_uat* prefs_get_uat_value(pref_t *pref)
1874 return pref->varp.uat;
1878 * Register a color preference.
1880 void
1881 prefs_register_color_preference(module_t *module, const char *name,
1882 const char *title, const char *description,
1883 color_t *color)
1885 pref_t* preference = register_preference(module, name, title, description, PREF_COLOR);
1887 preference->varp.colorp = color;
1888 preference->default_val.color = *color;
1891 bool prefs_set_color_value(pref_t *pref, color_t value, pref_source_t source)
1893 bool changed = false;
1895 switch (source)
1897 case pref_default:
1898 if ((pref->default_val.color.red != value.red) ||
1899 (pref->default_val.color.green != value.green) ||
1900 (pref->default_val.color.blue != value.blue)) {
1901 changed = true;
1902 pref->default_val.color = value;
1904 break;
1905 case pref_stashed:
1906 if ((pref->stashed_val.color.red != value.red) ||
1907 (pref->stashed_val.color.green != value.green) ||
1908 (pref->stashed_val.color.blue != value.blue)) {
1909 changed = true;
1910 pref->stashed_val.color = value;
1912 break;
1913 case pref_current:
1914 if ((pref->varp.colorp->red != value.red) ||
1915 (pref->varp.colorp->green != value.green) ||
1916 (pref->varp.colorp->blue != value.blue)) {
1917 changed = true;
1918 *pref->varp.colorp = value;
1920 break;
1921 default:
1922 ws_assert_not_reached();
1923 break;
1926 return changed;
1929 color_t* prefs_get_color_value(pref_t *pref, pref_source_t source)
1931 switch (source)
1933 case pref_default:
1934 return &pref->default_val.color;
1935 case pref_stashed:
1936 return &pref->stashed_val.color;
1937 case pref_current:
1938 return pref->varp.colorp;
1939 default:
1940 ws_assert_not_reached();
1941 break;
1944 return NULL;
1948 * Register a "custom" preference with a list.
1949 * XXX - This should be temporary until we can find a better way
1950 * to do "custom" preferences
1952 typedef void (*pref_custom_list_init_cb) (pref_t* pref, GList** value);
1954 static void
1955 prefs_register_list_custom_preference(module_t *module, const char *name,
1956 const char *title, const char *description,
1957 struct pref_custom_cbs* custom_cbs,
1958 pref_custom_list_init_cb init_cb,
1959 GList** list)
1961 pref_t* preference = register_preference(module, name, title, description, PREF_CUSTOM);
1963 preference->custom_cbs = *custom_cbs;
1964 init_cb(preference, list);
1968 * Register a custom preference.
1970 void
1971 prefs_register_custom_preference(module_t *module, const char *name,
1972 const char *title, const char *description,
1973 struct pref_custom_cbs* custom_cbs,
1974 void **custom_data _U_)
1976 pref_t* preference = register_preference(module, name, title, description, PREF_CUSTOM);
1978 preference->custom_cbs = *custom_cbs;
1979 /* XXX - wait until we can handle void** pointers
1980 preference->custom_cbs.init_cb(preference, custom_data);
1985 * Register a dedicated TCP preference for SEQ analysis overriding.
1986 * This is similar to the data structure from enum preference, except
1987 * that when a preference dialog is used, the stashed value is the list
1988 * of frame data pointers whose sequence analysis override will be set
1989 * to the current value if the dialog is accepted.
1991 * We don't need to read or write the value from the preferences file
1992 * (or command line), because the override is reset to the default (0)
1993 * for each frame when a new capture file is loaded.
1995 void
1996 prefs_register_custom_preference_TCP_Analysis(module_t *module, const char *name,
1997 const char *title, const char *description,
1998 int *var, const enum_val_t *enumvals,
1999 bool radio_buttons)
2001 pref_t *preference;
2003 preference = register_preference(module, name, title, description,
2004 PREF_PROTO_TCP_SNDAMB_ENUM);
2005 preference->varp.enump = var;
2006 preference->default_val.enumval = *var;
2007 preference->stashed_val.list = NULL;
2008 preference->info.enum_info.enumvals = enumvals;
2009 preference->info.enum_info.radio_buttons = radio_buttons;
2013 * Register a (internal) "Decode As" preference with a ranged value.
2015 void prefs_register_decode_as_range_preference(module_t *module, const char *name,
2016 const char *title, const char *description, range_t **var,
2017 uint32_t max_value, const char *dissector_table, const char *dissector_description)
2019 pref_t *preference;
2021 preference = prefs_register_range_preference_common(module, name, title,
2022 description, var, max_value, PREF_DECODE_AS_RANGE);
2023 preference->dissector_desc = dissector_description;
2024 preference->dissector_table = dissector_table;
2028 * Register a preference with password value.
2030 void
2031 prefs_register_password_preference(module_t *module, const char *name,
2032 const char *title, const char *description,
2033 const char **var)
2035 DIAG_OFF(cast-qual)
2036 register_string_like_preference(module, name, title, description,
2037 (char **)var, PREF_PASSWORD, NULL, false);
2038 DIAG_ON(cast-qual)
2042 * Register a preference with a dissector name.
2044 void
2045 prefs_register_dissector_preference(module_t *module, const char *name,
2046 const char *title, const char *description,
2047 const char **var)
2049 DIAG_OFF(cast-qual)
2050 register_string_like_preference(module, name, title, description,
2051 (char **)var, PREF_DISSECTOR, NULL, false);
2052 DIAG_ON(cast-qual)
2055 bool prefs_add_decode_as_value(pref_t *pref, unsigned value, bool replace)
2057 switch(pref->type)
2059 case PREF_DECODE_AS_RANGE:
2060 if (replace)
2062 /* If range has single value, replace it */
2063 if (((*pref->varp.range)->nranges == 1) &&
2064 ((*pref->varp.range)->ranges[0].low == (*pref->varp.range)->ranges[0].high)) {
2065 wmem_free(wmem_epan_scope(), *pref->varp.range);
2066 *pref->varp.range = range_empty(wmem_epan_scope());
2070 prefs_range_add_value(pref, value);
2071 break;
2072 default:
2073 /* XXX - Worth asserting over? */
2074 break;
2077 return true;
2080 bool prefs_remove_decode_as_value(pref_t *pref, unsigned value, bool set_default _U_)
2082 switch(pref->type)
2084 case PREF_DECODE_AS_RANGE:
2085 /* XXX - We could set to the default if the value is the only one
2086 * in the range.
2088 prefs_range_remove_value(pref, value);
2089 break;
2090 default:
2091 break;
2094 return true;
2098 * Register a preference that used to be supported but no longer is.
2100 void
2101 prefs_register_obsolete_preference(module_t *module, const char *name)
2103 register_preference(module, name, NULL, NULL, PREF_OBSOLETE);
2106 void
2107 prefs_set_preference_effect_fields(module_t *module, const char *name)
2109 pref_t * pref = prefs_find_preference(module, name);
2110 if (pref) {
2111 prefs_set_effect_flags(pref, prefs_get_effect_flags(pref) | PREF_EFFECT_FIELDS);
2116 * Check to see if a preference is obsolete.
2118 extern bool
2119 prefs_get_preference_obsolete(pref_t *pref)
2121 if (pref)
2122 return (IS_PREF_OBSOLETE(pref->type) ? true : false);
2124 return true;
2128 * Make a preference obsolete.
2130 extern prefs_set_pref_e
2131 prefs_set_preference_obsolete(pref_t *pref)
2133 if (pref) {
2134 SET_PREF_OBSOLETE(pref->type);
2135 return PREFS_SET_OK;
2137 return PREFS_SET_NO_SUCH_PREF;
2140 unsigned
2141 pref_stash(pref_t *pref, void *unused _U_)
2143 switch (pref->type) {
2145 case PREF_UINT:
2146 pref->stashed_val.uint = *pref->varp.uint;
2147 break;
2149 case PREF_BOOL:
2150 pref->stashed_val.boolval = *pref->varp.boolp;
2151 break;
2153 case PREF_ENUM:
2154 pref->stashed_val.enumval = *pref->varp.enump;
2155 break;
2157 case PREF_STRING:
2158 case PREF_SAVE_FILENAME:
2159 case PREF_OPEN_FILENAME:
2160 case PREF_DIRNAME:
2161 case PREF_PASSWORD:
2162 case PREF_DISSECTOR:
2163 g_free(pref->stashed_val.string);
2164 pref->stashed_val.string = g_strdup(*pref->varp.string);
2165 break;
2167 case PREF_DECODE_AS_RANGE:
2168 case PREF_RANGE:
2169 wmem_free(wmem_epan_scope(), pref->stashed_val.range);
2170 pref->stashed_val.range = range_copy(wmem_epan_scope(), *pref->varp.range);
2171 break;
2173 case PREF_COLOR:
2174 pref->stashed_val.color = *pref->varp.colorp;
2175 break;
2177 case PREF_STATIC_TEXT:
2178 case PREF_UAT:
2179 case PREF_CUSTOM:
2180 case PREF_PROTO_TCP_SNDAMB_ENUM:
2181 break;
2183 case PREF_OBSOLETE:
2184 ws_assert_not_reached();
2185 break;
2187 return 0;
2190 unsigned
2191 pref_unstash(pref_t *pref, void *unstash_data_p)
2193 pref_unstash_data_t *unstash_data = (pref_unstash_data_t *)unstash_data_p;
2194 dissector_table_t sub_dissectors = NULL;
2195 dissector_handle_t handle = NULL;
2197 /* Revert the preference to its saved value. */
2198 switch (pref->type) {
2200 case PREF_UINT:
2201 if (*pref->varp.uint != pref->stashed_val.uint) {
2202 unstash_data->module->prefs_changed_flags |= prefs_get_effect_flags(pref);
2203 *pref->varp.uint = pref->stashed_val.uint;
2205 break;
2207 case PREF_BOOL:
2208 if (*pref->varp.boolp != pref->stashed_val.boolval) {
2209 unstash_data->module->prefs_changed_flags |= prefs_get_effect_flags(pref);
2210 *pref->varp.boolp = pref->stashed_val.boolval;
2212 break;
2214 case PREF_ENUM:
2215 if (*pref->varp.enump != pref->stashed_val.enumval) {
2216 unstash_data->module->prefs_changed_flags |= prefs_get_effect_flags(pref);
2217 *pref->varp.enump = pref->stashed_val.enumval;
2219 break;
2221 case PREF_PROTO_TCP_SNDAMB_ENUM:
2223 /* The preference dialogs are modal so the frame_data pointers should
2224 * still be valid; otherwise we could store the frame numbers to
2225 * change.
2227 frame_data *fdata;
2228 for (GList* elem = pref->stashed_val.list; elem != NULL; elem = elem->next) {
2229 fdata = (frame_data*)elem->data;
2230 if (fdata->tcp_snd_manual_analysis != *pref->varp.enump) {
2231 unstash_data->module->prefs_changed_flags |= prefs_get_effect_flags(pref);
2232 fdata->tcp_snd_manual_analysis = *pref->varp.enump;
2235 break;
2237 case PREF_STRING:
2238 case PREF_SAVE_FILENAME:
2239 case PREF_OPEN_FILENAME:
2240 case PREF_DIRNAME:
2241 case PREF_PASSWORD:
2242 case PREF_DISSECTOR:
2243 if (strcmp(*pref->varp.string, pref->stashed_val.string) != 0) {
2244 unstash_data->module->prefs_changed_flags |= prefs_get_effect_flags(pref);
2245 g_free(*pref->varp.string);
2246 *pref->varp.string = g_strdup(pref->stashed_val.string);
2248 break;
2250 case PREF_DECODE_AS_RANGE:
2252 const char* table_name = prefs_get_dissector_table(pref);
2253 if (!ranges_are_equal(*pref->varp.range, pref->stashed_val.range)) {
2254 uint32_t i, j;
2255 unstash_data->module->prefs_changed_flags |= prefs_get_effect_flags(pref);
2257 if (unstash_data->handle_decode_as) {
2258 sub_dissectors = find_dissector_table(table_name);
2259 if (sub_dissectors != NULL) {
2260 const char *handle_desc = prefs_get_dissector_description(pref);
2261 // It should perhaps be possible to get this via dissector name.
2262 handle = dissector_table_get_dissector_handle(sub_dissectors, handle_desc);
2263 if (handle != NULL) {
2264 /* Set the current handle to NULL for all the old values
2265 * in the dissector table. If there isn't an initial
2266 * handle, this actually deletes the entry. (If there
2267 * is an initial entry, keep it around so that the
2268 * user can see the original value.)
2270 * XXX - If there's an initial handle which is not this,
2271 * reset it instead? At least this leaves the initial
2272 * handle visible in the Decode As table.
2274 for (i = 0; i < (*pref->varp.range)->nranges; i++) {
2275 for (j = (*pref->varp.range)->ranges[i].low; j < (*pref->varp.range)->ranges[i].high; j++) {
2276 dissector_change_uint(table_name, j, NULL);
2277 decode_build_reset_list(table_name, dissector_table_get_type(sub_dissectors), GUINT_TO_POINTER(j), NULL, NULL);
2280 dissector_change_uint(table_name, (*pref->varp.range)->ranges[i].high, NULL);
2281 decode_build_reset_list(table_name, dissector_table_get_type(sub_dissectors), GUINT_TO_POINTER((*pref->varp.range)->ranges[i].high), NULL, NULL);
2287 wmem_free(wmem_epan_scope(), *pref->varp.range);
2288 *pref->varp.range = range_copy(wmem_epan_scope(), pref->stashed_val.range);
2290 if (unstash_data->handle_decode_as) {
2291 if ((sub_dissectors != NULL) && (handle != NULL)) {
2293 /* Add new values to the dissector table */
2294 for (i = 0; i < (*pref->varp.range)->nranges; i++) {
2296 for (j = (*pref->varp.range)->ranges[i].low; j < (*pref->varp.range)->ranges[i].high; j++) {
2297 dissector_change_uint(table_name, j, handle);
2298 decode_build_reset_list(table_name, dissector_table_get_type(sub_dissectors), GUINT_TO_POINTER(j), NULL, NULL);
2301 dissector_change_uint(table_name, (*pref->varp.range)->ranges[i].high, handle);
2302 decode_build_reset_list(table_name, dissector_table_get_type(sub_dissectors), GUINT_TO_POINTER((*pref->varp.range)->ranges[i].high), NULL, NULL);
2307 break;
2309 case PREF_RANGE:
2310 if (!ranges_are_equal(*pref->varp.range, pref->stashed_val.range)) {
2311 unstash_data->module->prefs_changed_flags |= prefs_get_effect_flags(pref);
2312 wmem_free(wmem_epan_scope(), *pref->varp.range);
2313 *pref->varp.range = range_copy(wmem_epan_scope(), pref->stashed_val.range);
2315 break;
2317 case PREF_COLOR:
2318 if ((pref->varp.colorp->blue != pref->stashed_val.color.blue) ||
2319 (pref->varp.colorp->red != pref->stashed_val.color.red) ||
2320 (pref->varp.colorp->green != pref->stashed_val.color.green)) {
2321 unstash_data->module->prefs_changed_flags |= prefs_get_effect_flags(pref);
2322 *pref->varp.colorp = pref->stashed_val.color;
2324 break;
2326 case PREF_STATIC_TEXT:
2327 case PREF_UAT:
2328 case PREF_CUSTOM:
2329 break;
2331 case PREF_OBSOLETE:
2332 ws_assert_not_reached();
2333 break;
2335 return 0;
2338 void
2339 reset_stashed_pref(pref_t *pref) {
2340 switch (pref->type) {
2342 case PREF_UINT:
2343 pref->stashed_val.uint = pref->default_val.uint;
2344 break;
2346 case PREF_BOOL:
2347 pref->stashed_val.boolval = pref->default_val.boolval;
2348 break;
2350 case PREF_ENUM:
2351 pref->stashed_val.enumval = pref->default_val.enumval;
2352 break;
2354 case PREF_STRING:
2355 case PREF_SAVE_FILENAME:
2356 case PREF_OPEN_FILENAME:
2357 case PREF_DIRNAME:
2358 case PREF_PASSWORD:
2359 case PREF_DISSECTOR:
2360 g_free(pref->stashed_val.string);
2361 pref->stashed_val.string = g_strdup(pref->default_val.string);
2362 break;
2364 case PREF_DECODE_AS_RANGE:
2365 case PREF_RANGE:
2366 wmem_free(wmem_epan_scope(), pref->stashed_val.range);
2367 pref->stashed_val.range = range_copy(wmem_epan_scope(), pref->default_val.range);
2368 break;
2370 case PREF_PROTO_TCP_SNDAMB_ENUM:
2371 if (pref->stashed_val.list != NULL) {
2372 g_list_free(pref->stashed_val.list);
2373 pref->stashed_val.list = NULL;
2375 break;
2377 case PREF_COLOR:
2378 memcpy(&pref->stashed_val.color, &pref->default_val.color, sizeof(color_t));
2379 break;
2381 case PREF_STATIC_TEXT:
2382 case PREF_UAT:
2383 case PREF_CUSTOM:
2384 break;
2386 case PREF_OBSOLETE:
2387 ws_assert_not_reached();
2388 break;
2392 unsigned
2393 pref_clean_stash(pref_t *pref, void *unused _U_)
2395 switch (pref->type) {
2397 case PREF_UINT:
2398 break;
2400 case PREF_BOOL:
2401 break;
2403 case PREF_ENUM:
2404 break;
2406 case PREF_STRING:
2407 case PREF_SAVE_FILENAME:
2408 case PREF_OPEN_FILENAME:
2409 case PREF_DIRNAME:
2410 case PREF_PASSWORD:
2411 case PREF_DISSECTOR:
2412 if (pref->stashed_val.string != NULL) {
2413 g_free(pref->stashed_val.string);
2414 pref->stashed_val.string = NULL;
2416 break;
2418 case PREF_DECODE_AS_RANGE:
2419 case PREF_RANGE:
2420 if (pref->stashed_val.range != NULL) {
2421 wmem_free(wmem_epan_scope(), pref->stashed_val.range);
2422 pref->stashed_val.range = NULL;
2424 break;
2426 case PREF_STATIC_TEXT:
2427 case PREF_UAT:
2428 case PREF_COLOR:
2429 case PREF_CUSTOM:
2430 break;
2432 case PREF_PROTO_TCP_SNDAMB_ENUM:
2433 if (pref->stashed_val.list != NULL) {
2434 g_list_free(pref->stashed_val.list);
2435 pref->stashed_val.list = NULL;
2437 break;
2439 case PREF_OBSOLETE:
2440 ws_assert_not_reached();
2441 break;
2443 return 0;
2447 * Call a callback function, with a specified argument, for each preference
2448 * in a given module.
2450 * If any of the callbacks return a non-zero value, stop and return that
2451 * value, otherwise return 0.
2453 unsigned
2454 prefs_pref_foreach(module_t *module, pref_cb callback, void *user_data)
2456 GList *elem;
2457 pref_t *pref;
2458 unsigned ret;
2460 for (elem = g_list_first(module->prefs); elem != NULL; elem = g_list_next(elem)) {
2461 pref = (pref_t *)elem->data;
2462 if (IS_PREF_OBSOLETE(pref->type)) {
2464 * This preference is no longer supported; it's
2465 * not a real preference, so we don't call the
2466 * callback for it (i.e., we treat it as if it
2467 * weren't found in the list of preferences,
2468 * and we weren't called in the first place).
2470 continue;
2473 ret = (*callback)(pref, user_data);
2474 if (ret != 0)
2475 return ret;
2477 return 0;
2480 static const enum_val_t st_sort_col_vals[] = {
2481 { "name", "Node name (topic/item)", ST_SORT_COL_NAME },
2482 { "count", "Item count", ST_SORT_COL_COUNT },
2483 { "average", "Average value of the node", ST_SORT_COL_AVG },
2484 { "min", "Minimum value of the node", ST_SORT_COL_MIN },
2485 { "max", "Maximum value of the node", ST_SORT_COL_MAX },
2486 { "burst", "Burst rate of the node", ST_SORT_COL_BURSTRATE },
2487 { NULL, NULL, 0 }
2490 static void
2491 stats_callback(void)
2493 /* Test for a sane tap update interval */
2494 if (prefs.tap_update_interval < 100 || prefs.tap_update_interval > 10000)
2495 prefs.tap_update_interval = TAP_UPDATE_DEFAULT_INTERVAL;
2497 /* burst resolution can't be less than 1 (ms) */
2498 if (prefs.st_burst_resolution < 1) {
2499 prefs.st_burst_resolution = 1;
2501 else if (prefs.st_burst_resolution > ST_MAX_BURSTRES) {
2502 prefs.st_burst_resolution = ST_MAX_BURSTRES;
2504 /* make sure burst window value makes sense */
2505 if (prefs.st_burst_windowlen < prefs.st_burst_resolution) {
2506 prefs.st_burst_windowlen = prefs.st_burst_resolution;
2508 /* round burst window down to multiple of resolution */
2509 prefs.st_burst_windowlen -= prefs.st_burst_windowlen%prefs.st_burst_resolution;
2510 if ((prefs.st_burst_windowlen/prefs.st_burst_resolution) > ST_MAX_BURSTBUCKETS) {
2511 prefs.st_burst_windowlen = prefs.st_burst_resolution*ST_MAX_BURSTBUCKETS;
2515 static void
2516 gui_callback(void)
2518 /* Ensure there is at least one file count */
2519 if (prefs.gui_recent_files_count_max == 0)
2520 prefs.gui_recent_files_count_max = 10;
2522 /* Ensure there is at least one display filter entry */
2523 if (prefs.gui_recent_df_entries_max == 0)
2524 prefs.gui_recent_df_entries_max = 10;
2526 /* number of decimal places should be between 2 and 10 */
2527 if (prefs.gui_decimal_places1 < 2) {
2528 prefs.gui_decimal_places1 = 2;
2529 } else if (prefs.gui_decimal_places1 > 10) {
2530 prefs.gui_decimal_places1 = 10;
2532 /* number of decimal places should be between 2 and 10 */
2533 if (prefs.gui_decimal_places2 < 2) {
2534 prefs.gui_decimal_places2 = 2;
2535 } else if (prefs.gui_decimal_places2 > 10) {
2536 prefs.gui_decimal_places2 = 10;
2538 /* number of decimal places should be between 2 and 10 */
2539 if (prefs.gui_decimal_places3 < 2) {
2540 prefs.gui_decimal_places3 = 2;
2541 } else if (prefs.gui_decimal_places3 > 10) {
2542 prefs.gui_decimal_places3 = 10;
2546 static void
2547 gui_layout_callback(void)
2549 if (prefs.gui_layout_type == layout_unused ||
2550 prefs.gui_layout_type >= layout_type_max) {
2551 /* XXX - report an error? It's not a syntax error - we'd need to
2552 add a way of reporting a *semantic* error. */
2553 prefs.gui_layout_type = layout_type_2;
2557 /******************************************************
2558 * All custom preference function callbacks
2559 ******************************************************/
2560 static void custom_pref_no_cb(pref_t* pref _U_) {}
2563 * Column preference functions
2565 #define PRS_COL_HIDDEN_FMT "column.hidden"
2566 #define PRS_COL_HIDDEN "column.hide"
2567 #define PRS_COL_FMT "column.format"
2568 #define PRS_COL_NUM "column.number"
2569 static module_t *gui_column_module;
2571 static prefs_set_pref_e
2572 column_hidden_set_cb(pref_t* pref, const char* value, unsigned int* changed_flags)
2574 GList *clp;
2575 fmt_data *cfmt;
2576 pref_t *format_pref;
2579 * Prefer the new preference to the old format-based preference if we've
2580 * read it. (We probably could just compare the string to NULL and "".)
2582 prefs.cols_hide_new = true;
2584 (*changed_flags) |= prefs_set_string_value(pref, value, pref_current);
2587 * Set the "visible" flag for the existing columns; we need to
2588 * do this if we set PRS_COL_HIDDEN but don't set PRS_COL_FMT
2589 * after setting it (which might be the case if, for example, we
2590 * set PRS_COL_HIDDEN on the command line).
2592 format_pref = prefs_find_preference(gui_column_module, PRS_COL_FMT);
2593 clp = (format_pref) ? *format_pref->varp.list : NULL;
2594 int cidx = 1;
2595 while (clp) {
2596 cfmt = (fmt_data *)clp->data;
2597 cfmt->visible = prefs_is_column_visible(*pref->varp.string, cidx);
2598 cidx++;
2599 clp = clp->next;
2602 return PREFS_SET_OK;
2605 static const char *
2606 column_hidden_type_name_cb(void)
2608 return "Packet list hidden columns";
2611 static char *
2612 column_hidden_type_description_cb(void)
2614 return g_strdup("List all column indices (1-indexed) to hide in the packet list.");
2617 static char *
2618 column_hidden_to_str_cb(pref_t* pref, bool default_val)
2620 GString *cols_hidden;
2621 GList *clp;
2622 fmt_data *cfmt;
2623 pref_t *format_pref;
2624 int cidx = 1;
2626 if (default_val)
2627 return g_strdup(pref->default_val.string);
2629 cols_hidden = g_string_new("");
2630 format_pref = prefs_find_preference(gui_column_module, PRS_COL_FMT);
2631 clp = (format_pref) ? *format_pref->varp.list : NULL;
2632 while (clp) {
2633 cfmt = (fmt_data *) clp->data;
2634 if (!cfmt->visible) {
2635 if (cols_hidden->len)
2636 g_string_append (cols_hidden, ",");
2637 g_string_append_printf (cols_hidden, "%i", cidx);
2639 clp = clp->next;
2640 cidx++;
2643 return g_string_free (cols_hidden, FALSE);
2646 static bool
2647 column_hidden_is_default_cb(pref_t* pref)
2649 char *cur_hidden_str = column_hidden_to_str_cb(pref, false);
2650 bool is_default = g_strcmp0(cur_hidden_str, pref->default_val.string) == 0;
2652 g_free(cur_hidden_str);
2653 return is_default;
2656 static prefs_set_pref_e
2657 column_hidden_fmt_set_cb(pref_t* pref, const char* value, unsigned int* changed_flags)
2659 GList *clp;
2660 fmt_data *cfmt;
2661 pref_t *format_pref;
2663 (*changed_flags) |= prefs_set_string_value(pref, value, pref_current);
2666 * Set the "visible" flag for the existing columns; we need to
2667 * do this if we set PRS_COL_HIDDEN_FMT but don't set PRS_COL_FMT
2668 * after setting it (which might be the case if, for example, we
2669 * set PRS_COL_HIDDEN_FMT on the command line; it shouldn't happen
2670 * when reading the configuration file because we write (both of)
2671 * the hidden column prefs before the column format prefs.)
2673 format_pref = prefs_find_preference(gui_column_module, PRS_COL_FMT);
2674 clp = (format_pref) ? *format_pref->varp.list : NULL;
2675 while (clp) {
2676 cfmt = (fmt_data *)clp->data;
2677 cfmt->visible = prefs_is_column_fmt_visible(*pref->varp.string, cfmt);
2678 clp = clp->next;
2681 return PREFS_SET_OK;
2684 static const char *
2685 column_hidden_fmt_type_name_cb(void)
2687 return "Packet list hidden column formats (deprecated)";
2690 static char *
2691 column_hidden_fmt_type_description_cb(void)
2693 return g_strdup("List all column formats to hide in the packet list. Deprecated in favor of the index-based preference.");
2696 static char *
2697 column_hidden_fmt_to_str_cb(pref_t* pref, bool default_val)
2699 GString *cols_hidden;
2700 GList *clp;
2701 fmt_data *cfmt;
2702 pref_t *format_pref;
2704 if (default_val)
2705 return g_strdup(pref->default_val.string);
2707 cols_hidden = g_string_new("");
2708 format_pref = prefs_find_preference(gui_column_module, PRS_COL_FMT);
2709 clp = (format_pref) ? *format_pref->varp.list : NULL;
2710 while (clp) {
2711 char *prefs_fmt;
2712 cfmt = (fmt_data *) clp->data;
2713 if (!cfmt->visible) {
2714 if (cols_hidden->len)
2715 g_string_append (cols_hidden, ",");
2716 prefs_fmt = column_fmt_data_to_str(cfmt);
2717 g_string_append(cols_hidden, prefs_fmt);
2718 g_free(prefs_fmt);
2720 clp = clp->next;
2723 return g_string_free (cols_hidden, FALSE);
2726 static bool
2727 column_hidden_fmt_is_default_cb(pref_t* pref)
2729 char *cur_hidden_str = column_hidden_fmt_to_str_cb(pref, false);
2730 bool is_default = g_strcmp0(cur_hidden_str, pref->default_val.string) == 0;
2732 g_free(cur_hidden_str);
2733 return is_default;
2736 /* Number of columns "preference". This is only used internally and is not written to the
2737 * preference file
2739 static void
2740 column_num_reset_cb(pref_t* pref)
2742 *pref->varp.uint = pref->default_val.uint;
2745 static prefs_set_pref_e
2746 column_num_set_cb(pref_t* pref _U_, const char* value _U_, unsigned int* changed_flags _U_)
2748 /* Don't write this to the preferences file */
2749 return PREFS_SET_OK;
2752 static const char *
2753 column_num_type_name_cb(void)
2755 return NULL;
2758 static char *
2759 column_num_type_description_cb(void)
2761 return g_strdup("");
2764 static bool
2765 column_num_is_default_cb(pref_t* pref _U_)
2767 return true;
2770 static char *
2771 column_num_to_str_cb(pref_t* pref _U_, bool default_val _U_)
2773 return g_strdup("");
2777 * Column format custom preference functions
2779 static void
2780 column_format_init_cb(pref_t* pref, GList** value)
2782 fmt_data *src_cfmt, *dest_cfmt;
2783 GList *entry;
2785 pref->varp.list = value;
2787 pref->default_val.list = NULL;
2788 for (entry = *pref->varp.list; entry != NULL; entry = g_list_next(entry)) {
2789 src_cfmt = (fmt_data *)entry->data;
2790 dest_cfmt = g_new(fmt_data,1);
2791 dest_cfmt->title = g_strdup(src_cfmt->title);
2792 dest_cfmt->fmt = src_cfmt->fmt;
2793 if (src_cfmt->custom_fields) {
2794 dest_cfmt->custom_fields = g_strdup(src_cfmt->custom_fields);
2795 dest_cfmt->custom_occurrence = src_cfmt->custom_occurrence;
2796 } else {
2797 dest_cfmt->custom_fields = NULL;
2798 dest_cfmt->custom_occurrence = 0;
2800 dest_cfmt->visible = src_cfmt->visible;
2801 dest_cfmt->display = src_cfmt->display;
2802 pref->default_val.list = g_list_append(pref->default_val.list, dest_cfmt);
2805 column_register_fields();
2808 static void
2809 column_format_free_cb(pref_t* pref)
2811 free_col_info(*pref->varp.list);
2812 free_col_info(pref->default_val.list);
2815 static void
2816 column_format_reset_cb(pref_t* pref)
2818 fmt_data *src_cfmt, *dest_cfmt;
2819 GList *entry;
2820 pref_t *col_num_pref;
2822 free_col_info(*pref->varp.list);
2823 *pref->varp.list = NULL;
2825 for (entry = pref->default_val.list; entry != NULL; entry = g_list_next(entry)) {
2826 src_cfmt = (fmt_data *)entry->data;
2827 dest_cfmt = g_new(fmt_data,1);
2828 dest_cfmt->title = g_strdup(src_cfmt->title);
2829 dest_cfmt->fmt = src_cfmt->fmt;
2830 if (src_cfmt->custom_fields) {
2831 dest_cfmt->custom_fields = g_strdup(src_cfmt->custom_fields);
2832 dest_cfmt->custom_occurrence = src_cfmt->custom_occurrence;
2833 } else {
2834 dest_cfmt->custom_fields = NULL;
2835 dest_cfmt->custom_occurrence = 0;
2837 dest_cfmt->visible = src_cfmt->visible;
2838 dest_cfmt->display = src_cfmt->display;
2839 *pref->varp.list = g_list_append(*pref->varp.list, dest_cfmt);
2842 col_num_pref = prefs_find_preference(gui_column_module, PRS_COL_NUM);
2843 ws_assert(col_num_pref != NULL); /* Should never happen */
2844 column_num_reset_cb(col_num_pref);
2847 static prefs_set_pref_e
2848 column_format_set_cb(pref_t* pref, const char* value, unsigned int* changed_flags _U_)
2850 GList *col_l, *col_l_elt;
2851 fmt_data *cfmt;
2852 int llen;
2853 pref_t *hidden_pref, *col_num_pref;
2855 col_l = prefs_get_string_list(value);
2856 if (col_l == NULL)
2857 return PREFS_SET_SYNTAX_ERR;
2858 if ((g_list_length(col_l) % 2) != 0) {
2859 /* A title didn't have a matching format. */
2860 prefs_clear_string_list(col_l);
2861 return PREFS_SET_SYNTAX_ERR;
2863 /* Check to make sure all column formats are valid. */
2864 col_l_elt = g_list_first(col_l);
2865 while (col_l_elt) {
2866 fmt_data cfmt_check;
2868 /* Go past the title. */
2869 col_l_elt = col_l_elt->next;
2871 /* Some predefined columns have been migrated to use custom columns.
2872 * We'll convert these silently here */
2873 try_convert_to_custom_column((char **)&col_l_elt->data);
2875 /* Parse the format to see if it's valid. */
2876 if (!parse_column_format(&cfmt_check, (char *)col_l_elt->data)) {
2877 /* It's not a valid column format. */
2878 prefs_clear_string_list(col_l);
2879 return PREFS_SET_SYNTAX_ERR;
2881 if (cfmt_check.fmt == COL_CUSTOM) {
2882 /* We don't need the custom column field on this pass. */
2883 g_free(cfmt_check.custom_fields);
2886 /* Go past the format. */
2887 col_l_elt = col_l_elt->next;
2890 /* They're all valid; process them. */
2891 free_col_info(*pref->varp.list);
2892 *pref->varp.list = NULL;
2893 if (prefs.cols_hide_new) {
2894 hidden_pref = prefs_find_preference(gui_column_module, PRS_COL_HIDDEN);
2895 } else {
2896 hidden_pref = prefs_find_preference(gui_column_module, PRS_COL_HIDDEN_FMT);
2898 ws_assert(hidden_pref != NULL); /* Should never happen */
2899 col_num_pref = prefs_find_preference(gui_column_module, PRS_COL_NUM);
2900 ws_assert(col_num_pref != NULL); /* Should never happen */
2901 llen = g_list_length(col_l);
2902 *col_num_pref->varp.uint = llen / 2;
2903 col_l_elt = g_list_first(col_l);
2904 int cidx = 1;
2905 while (col_l_elt) {
2906 cfmt = g_new(fmt_data,1);
2907 cfmt->title = g_strdup((char *)col_l_elt->data);
2908 col_l_elt = col_l_elt->next;
2909 parse_column_format(cfmt, (char *)col_l_elt->data);
2910 if (prefs.cols_hide_new) {
2911 cfmt->visible = prefs_is_column_visible(*hidden_pref->varp.string, cidx);
2912 } else {
2913 cfmt->visible = prefs_is_column_fmt_visible(*hidden_pref->varp.string, cfmt);
2915 col_l_elt = col_l_elt->next;
2916 *pref->varp.list = g_list_append(*pref->varp.list, cfmt);
2917 cidx++;
2920 prefs_clear_string_list(col_l);
2921 free_string_like_preference(hidden_pref);
2922 column_register_fields();
2923 return PREFS_SET_OK;
2927 static const char *
2928 column_format_type_name_cb(void)
2930 return "Packet list column format";
2933 static char *
2934 column_format_type_description_cb(void)
2936 return g_strdup("Each pair of strings consists of a column title and its format");
2939 static bool
2940 column_format_is_default_cb(pref_t* pref)
2942 GList *clp = *pref->varp.list,
2943 *pref_col = g_list_first(clp),
2944 *def_col = g_list_first(pref->default_val.list);
2945 fmt_data *cfmt, *def_cfmt;
2946 bool is_default = true;
2947 pref_t *col_num_pref;
2949 /* See if the column data has changed from the default */
2950 col_num_pref = prefs_find_preference(gui_column_module, PRS_COL_NUM);
2951 if (col_num_pref && *col_num_pref->varp.uint != col_num_pref->default_val.uint) {
2952 is_default = false;
2953 } else {
2954 while (pref_col && def_col) {
2955 cfmt = (fmt_data *) pref_col->data;
2956 def_cfmt = (fmt_data *) def_col->data;
2957 if ((g_strcmp0(cfmt->title, def_cfmt->title) != 0) ||
2958 (cfmt->fmt != def_cfmt->fmt) ||
2959 (((cfmt->fmt == COL_CUSTOM) && (cfmt->custom_fields)) &&
2960 ((g_strcmp0(cfmt->custom_fields, def_cfmt->custom_fields) != 0) ||
2961 (cfmt->display != def_cfmt->display)))) {
2962 is_default = false;
2963 break;
2966 pref_col = pref_col->next;
2967 def_col = def_col->next;
2971 return is_default;
2974 static char *
2975 column_format_to_str_cb(pref_t* pref, bool default_val)
2977 GList *pref_l = default_val ? pref->default_val.list : *pref->varp.list;
2978 GList *clp = g_list_first(pref_l);
2979 GList *col_l;
2980 fmt_data *cfmt;
2981 char *column_format_str;
2983 col_l = NULL;
2984 while (clp) {
2985 cfmt = (fmt_data *) clp->data;
2986 col_l = g_list_append(col_l, g_strdup(cfmt->title));
2987 col_l = g_list_append(col_l, column_fmt_data_to_str(cfmt));
2988 clp = clp->next;
2991 column_format_str = join_string_list(col_l);
2992 prefs_clear_string_list(col_l);
2993 return column_format_str;
2997 /****** Capture column custom preference functions ******/
2999 /* This routine is only called when Wireshark is started, NOT when another profile is selected.
3000 Copy the pref->capture_columns list (just loaded with the capture_cols[] struct values)
3001 to prefs->default_val.list.
3003 static void
3004 capture_column_init_cb(pref_t* pref, GList** capture_cols_values)
3006 GList *ccv_list = *capture_cols_values,
3007 *dlist = NULL;
3009 /* */
3010 while (ccv_list) {
3011 dlist = g_list_append(dlist, g_strdup((char *)ccv_list->data));
3012 ccv_list = ccv_list->next;
3015 pref->default_val.list = dlist;
3016 pref->varp.list = &prefs.capture_columns;
3017 pref->stashed_val.boolval = false;
3020 /* Free the prefs->capture_columns list strings and remove the list entries.
3021 Note that since pref->varp.list points to &prefs.capture_columns, it is
3022 also freed.
3024 static void
3025 capture_column_free_cb(pref_t* pref)
3027 prefs_clear_string_list(prefs.capture_columns);
3028 prefs.capture_columns = NULL;
3030 if (pref->stashed_val.boolval == true) {
3031 prefs_clear_string_list(pref->default_val.list);
3032 pref->default_val.list = NULL;
3036 /* Copy pref->default_val.list to *pref->varp.list.
3038 static void
3039 capture_column_reset_cb(pref_t* pref)
3041 GList *vlist = NULL, *dlist;
3043 /* Free the column name strings and remove the links from *pref->varp.list */
3044 prefs_clear_string_list(*pref->varp.list);
3046 for (dlist = pref->default_val.list; dlist != NULL; dlist = g_list_next(dlist)) {
3047 vlist = g_list_append(vlist, g_strdup((char *)dlist->data));
3049 *pref->varp.list = vlist;
3052 static prefs_set_pref_e
3053 capture_column_set_cb(pref_t* pref, const char* value, unsigned int* changed_flags _U_)
3055 GList *col_l = prefs_get_string_list(value);
3056 GList *col_l_elt;
3057 char *col_name;
3058 int i;
3060 if (col_l == NULL)
3061 return PREFS_SET_SYNTAX_ERR;
3063 capture_column_free_cb(pref);
3065 /* If value (the list of capture.columns read from preferences) is empty, set capture.columns
3066 to the full list of valid capture column names. */
3067 col_l_elt = g_list_first(col_l);
3068 if (!(*(char *)col_l_elt->data)) {
3069 for (i = 0; i < num_capture_cols; i++) {
3070 col_name = g_strdup(capture_cols[i]);
3071 prefs.capture_columns = g_list_append(prefs.capture_columns, col_name);
3075 /* Verify that all the column names are valid. If not, use the entire list of valid columns.
3077 while (col_l_elt) {
3078 bool found_match = false;
3079 col_name = (char *)col_l_elt->data;
3081 for (i = 0; i < num_capture_cols; i++) {
3082 if (strcmp(col_name, capture_cols[i])==0) {
3083 found_match = true;
3084 break;
3087 if (!found_match) {
3088 /* One or more cols are invalid so use the entire list of valid cols. */
3089 for (i = 0; i < num_capture_cols; i++) {
3090 col_name = g_strdup(capture_cols[i]);
3091 prefs.capture_columns = g_list_append(prefs.capture_columns, col_name);
3093 pref->varp.list = &prefs.capture_columns;
3094 prefs_clear_string_list(col_l);
3095 return PREFS_SET_SYNTAX_ERR;
3097 col_l_elt = col_l_elt->next;
3100 col_l_elt = g_list_first(col_l);
3101 while (col_l_elt) {
3102 col_name = (char *)col_l_elt->data;
3103 prefs.capture_columns = g_list_append(prefs.capture_columns, col_name);
3104 col_l_elt = col_l_elt->next;
3106 pref->varp.list = &prefs.capture_columns;
3107 g_list_free(col_l);
3108 return PREFS_SET_OK;
3112 static const char *
3113 capture_column_type_name_cb(void)
3115 return "Column list";
3118 static char *
3119 capture_column_type_description_cb(void)
3121 return g_strdup(
3122 "List of columns to be displayed in the capture options dialog.\n"
3123 CAPTURE_COL_TYPE_DESCRIPTION);
3126 static bool
3127 capture_column_is_default_cb(pref_t* pref)
3129 GList *pref_col = g_list_first(prefs.capture_columns),
3130 *def_col = g_list_first(pref->default_val.list);
3131 bool is_default = true;
3133 /* See if the column data has changed from the default */
3134 while (pref_col && def_col) {
3135 if (strcmp((char *)pref_col->data, (char *)def_col->data) != 0) {
3136 is_default = false;
3137 break;
3139 pref_col = pref_col->next;
3140 def_col = def_col->next;
3143 /* Ensure the same column count */
3144 if (((pref_col == NULL) && (def_col != NULL)) ||
3145 ((pref_col != NULL) && (def_col == NULL)))
3146 is_default = false;
3148 return is_default;
3151 static char *
3152 capture_column_to_str_cb(pref_t* pref, bool default_val)
3155 GList *pref_l = default_val ? pref->default_val.list : prefs.capture_columns;
3156 GList *clp = g_list_first(pref_l);
3157 GList *col_l = NULL;
3158 char *col;
3159 char *capture_column_str;
3161 while (clp) {
3162 col = (char *) clp->data;
3163 col_l = g_list_append(col_l, g_strdup(col));
3164 clp = clp->next;
3167 capture_column_str = join_string_list(col_l);
3168 prefs_clear_string_list(col_l);
3169 return capture_column_str;
3172 static prefs_set_pref_e
3173 colorized_frame_set_cb(pref_t* pref, const char* value, unsigned int* changed_flags)
3175 (*changed_flags) |= prefs_set_string_value(pref, value, pref_current);
3176 return PREFS_SET_OK;
3179 static const char *
3180 colorized_frame_type_name_cb(void)
3182 /* Don't write the colors of the 10 easy-access-colorfilters to the preferences
3183 * file until the colors can be changed in the GUI. Currently this is not really
3184 * possible since the STOCK-icons for these colors are hardcoded.
3186 * XXX Find a way to change the colors of the STOCK-icons on the fly and then
3187 * add these 10 colors to the list of colors that can be changed through
3188 * the preferences.
3191 return NULL;
3194 static char *
3195 colorized_frame_type_description_cb(void)
3197 return g_strdup("");
3200 static bool
3201 colorized_frame_is_default_cb(pref_t* pref _U_)
3203 return true;
3206 static char *
3207 colorized_frame_to_str_cb(pref_t* pref _U_, bool default_val _U_)
3209 return g_strdup("");
3213 * Register all non-dissector modules' preferences.
3215 static module_t *gui_module;
3216 static module_t *gui_color_module;
3217 static module_t *nameres_module;
3219 static void
3220 prefs_register_modules(void)
3222 module_t *printing, *capture_module, *console_module,
3223 *gui_layout_module, *gui_font_module;
3224 module_t *extcap_module;
3225 unsigned int layout_gui_flags;
3226 struct pref_custom_cbs custom_cbs;
3228 if (protocols_module != NULL) {
3229 /* Already setup preferences */
3230 return;
3233 /* GUI
3234 * These are "simple" GUI preferences that can be read/written using the
3235 * preference module API. These preferences still use their own
3236 * configuration screens for access, but this cuts down on the
3237 * preference "string compare list" in set_pref()
3239 extcap_module = prefs_register_module(NULL, "extcap", "Extcap Utilities",
3240 "Extcap Utilities", NULL, NULL, false);
3242 /* Setting default value to true */
3243 prefs.extcap_save_on_start = true;
3244 prefs_register_bool_preference(extcap_module, "gui_save_on_start",
3245 "Save arguments on start of capture",
3246 "Save arguments on start of capture",
3247 &prefs.extcap_save_on_start);
3249 /* GUI
3250 * These are "simple" GUI preferences that can be read/written using the
3251 * preference module API. These preferences still use their own
3252 * configuration screens for access, but this cuts down on the
3253 * preference "string compare list" in set_pref()
3255 gui_module = prefs_register_module(NULL, "gui", "User Interface",
3256 "User Interface", NULL, &gui_callback, false);
3258 * The GUI preferences don't affect dissection in general.
3259 * Any changes are signaled in other ways, so PREF_EFFECT_GUI doesn't
3260 * explicitly do anything, but wslua_set_preference expects *some*
3261 * effect flag to be set if the preference was changed.
3262 * We have to do this again for all the submodules (except for the
3263 * layout submodule, which has its own effect flag).
3265 unsigned gui_effect_flags = prefs_get_module_effect_flags(gui_module);
3266 gui_effect_flags |= PREF_EFFECT_GUI;
3267 gui_effect_flags &= (~PREF_EFFECT_DISSECTION);
3268 prefs_set_module_effect_flags(gui_module, gui_effect_flags);
3271 * gui.console_open is stored in the registry in addition to the
3272 * preferences file. It is also read independently by ws_log_init()
3273 * for early log initialization of the console.
3275 prefs_register_enum_preference(gui_module, "console_open",
3276 "Open a console window",
3277 "Open a console window (Windows only)",
3278 (int *)&ws_log_console_open, gui_console_open_type, false);
3280 prefs_register_obsolete_preference(gui_module, "scrollbar_on_right");
3281 prefs_register_obsolete_preference(gui_module, "packet_list_sel_browse");
3282 prefs_register_obsolete_preference(gui_module, "protocol_tree_sel_browse");
3283 prefs_register_obsolete_preference(gui_module, "tree_view_altern_colors");
3284 prefs_register_obsolete_preference(gui_module, "expert_composite_eyecandy");
3285 prefs_register_obsolete_preference(gui_module, "filter_toolbar_show_in_statusbar");
3287 prefs_register_bool_preference(gui_module, "restore_filter_after_following_stream",
3288 "Restore current display filter after following a stream",
3289 "Restore current display filter after following a stream?",
3290 &prefs.restore_filter_after_following_stream);
3292 prefs_register_obsolete_preference(gui_module, "protocol_tree_line_style");
3294 prefs_register_obsolete_preference(gui_module, "protocol_tree_expander_style");
3296 prefs_register_obsolete_preference(gui_module, "hex_dump_highlight_style");
3298 prefs_register_obsolete_preference(gui_module, "packet_editor.enabled");
3300 gui_column_module = prefs_register_subtree(gui_module, "Columns", "Columns", NULL);
3301 prefs_set_module_effect_flags(gui_column_module, gui_effect_flags);
3302 /* For reading older preference files with "column." preferences */
3303 prefs_register_module_alias("column", gui_column_module);
3306 custom_cbs.free_cb = free_string_like_preference;
3307 custom_cbs.reset_cb = reset_string_like_preference;
3308 custom_cbs.set_cb = column_hidden_set_cb;
3309 custom_cbs.type_name_cb = column_hidden_type_name_cb;
3310 custom_cbs.type_description_cb = column_hidden_type_description_cb;
3311 custom_cbs.is_default_cb = column_hidden_is_default_cb;
3312 custom_cbs.to_str_cb = column_hidden_to_str_cb;
3313 register_string_like_preference(gui_column_module, PRS_COL_HIDDEN, "Packet list hidden columns",
3314 "List all column indices (1-indexed) to hide in the packet list",
3315 &cols_hidden_list, PREF_CUSTOM, &custom_cbs, false);
3317 custom_cbs.set_cb = column_hidden_fmt_set_cb;
3318 custom_cbs.type_name_cb = column_hidden_fmt_type_name_cb;
3319 custom_cbs.type_description_cb = column_hidden_fmt_type_description_cb;
3320 custom_cbs.is_default_cb = column_hidden_fmt_is_default_cb;
3321 custom_cbs.to_str_cb = column_hidden_fmt_to_str_cb;
3323 register_string_like_preference(gui_column_module, PRS_COL_HIDDEN_FMT, "Packet list hidden column formats (deprecated)",
3324 "List all column formats to hide in the packet list; deprecated in favor of the index-based preference",
3325 &cols_hidden_fmt_list, PREF_CUSTOM, &custom_cbs, false);
3327 custom_cbs.free_cb = column_format_free_cb;
3328 custom_cbs.reset_cb = column_format_reset_cb;
3329 custom_cbs.set_cb = column_format_set_cb;
3330 custom_cbs.type_name_cb = column_format_type_name_cb;
3331 custom_cbs.type_description_cb = column_format_type_description_cb;
3332 custom_cbs.is_default_cb = column_format_is_default_cb;
3333 custom_cbs.to_str_cb = column_format_to_str_cb;
3335 prefs_register_list_custom_preference(gui_column_module, PRS_COL_FMT, "Packet list column format",
3336 "Each pair of strings consists of a column title and its format", &custom_cbs,
3337 column_format_init_cb, &prefs.col_list);
3339 /* Number of columns. This is only used internally and is not written to the
3340 * preference file
3342 custom_cbs.free_cb = custom_pref_no_cb;
3343 custom_cbs.reset_cb = column_num_reset_cb;
3344 custom_cbs.set_cb = column_num_set_cb;
3345 custom_cbs.type_name_cb = column_num_type_name_cb;
3346 custom_cbs.type_description_cb = column_num_type_description_cb;
3347 custom_cbs.is_default_cb = column_num_is_default_cb;
3348 custom_cbs.to_str_cb = column_num_to_str_cb;
3349 prefs_register_uint_custom_preference(gui_column_module, PRS_COL_NUM, "Number of columns",
3350 "Number of columns in col_list", &custom_cbs, &prefs.num_cols);
3352 /* User Interface : Font */
3353 gui_font_module = prefs_register_subtree(gui_module, "Font", "Font", NULL);
3354 prefs_set_module_effect_flags(gui_font_module, gui_effect_flags);
3356 prefs_register_obsolete_preference(gui_font_module, "font_name");
3358 prefs_register_obsolete_preference(gui_font_module, "gtk2.font_name");
3360 register_string_like_preference(gui_font_module, "qt.font_name", "Font name",
3361 "Font name for packet list, protocol tree, and hex dump panes. (Qt)",
3362 &prefs.gui_font_name, PREF_STRING, NULL, true);
3364 /* User Interface : Colors */
3365 gui_color_module = prefs_register_subtree(gui_module, "Colors", "Colors", NULL);
3366 unsigned gui_color_effect_flags = gui_effect_flags | PREF_EFFECT_GUI_COLOR;
3367 prefs_set_module_effect_flags(gui_color_module, gui_color_effect_flags);
3369 prefs_register_enum_preference(gui_color_module, "color_scheme", "Color scheme", "Color scheme",
3370 &prefs.gui_color_scheme, gui_color_scheme, false);
3372 prefs_register_color_preference(gui_color_module, "active_frame.fg", "Foreground color for an active selected item",
3373 "Foreground color for an active selected item", &prefs.gui_active_fg);
3375 prefs_register_color_preference(gui_color_module, "active_frame.bg", "Background color for an active selected item",
3376 "Background color for an active selected item", &prefs.gui_active_bg);
3378 prefs_register_enum_preference(gui_color_module, "active_frame.style", "Color style for an active selected item",
3379 "Color style for an active selected item", &prefs.gui_active_style, gui_selection_style, false);
3381 prefs_register_color_preference(gui_color_module, "inactive_frame.fg", "Foreground color for an inactive selected item",
3382 "Foreground color for an inactive selected item", &prefs.gui_inactive_fg);
3384 prefs_register_color_preference(gui_color_module, "inactive_frame.bg", "Background color for an inactive selected item",
3385 "Background color for an inactive selected item", &prefs.gui_inactive_bg);
3387 prefs_register_enum_preference(gui_color_module, "inactive_frame.style", "Color style for an inactive selected item",
3388 "Color style for an inactive selected item", &prefs.gui_inactive_style, gui_selection_style, false);
3390 prefs_register_color_preference(gui_color_module, "marked_frame.fg", "Color preferences for a marked frame",
3391 "Color preferences for a marked frame", &prefs.gui_marked_fg);
3393 prefs_register_color_preference(gui_color_module, "marked_frame.bg", "Color preferences for a marked frame",
3394 "Color preferences for a marked frame", &prefs.gui_marked_bg);
3396 prefs_register_color_preference(gui_color_module, "ignored_frame.fg", "Color preferences for a ignored frame",
3397 "Color preferences for a ignored frame", &prefs.gui_ignored_fg);
3399 prefs_register_color_preference(gui_color_module, "ignored_frame.bg", "Color preferences for a ignored frame",
3400 "Color preferences for a ignored frame", &prefs.gui_ignored_bg);
3402 prefs_register_color_preference(gui_color_module, "stream.client.fg", "TCP stream window color preference",
3403 "TCP stream window color preference", &prefs.st_client_fg);
3405 prefs_register_color_preference(gui_color_module, "stream.client.bg", "TCP stream window color preference",
3406 "TCP stream window color preference", &prefs.st_client_bg);
3408 prefs_register_color_preference(gui_color_module, "stream.server.fg", "TCP stream window color preference",
3409 "TCP stream window color preference", &prefs.st_server_fg);
3411 prefs_register_color_preference(gui_color_module, "stream.server.bg", "TCP stream window color preference",
3412 "TCP stream window color preference", &prefs.st_server_bg);
3414 custom_cbs.free_cb = free_string_like_preference;
3415 custom_cbs.reset_cb = reset_string_like_preference;
3416 custom_cbs.set_cb = colorized_frame_set_cb;
3417 custom_cbs.type_name_cb = colorized_frame_type_name_cb;
3418 custom_cbs.type_description_cb = colorized_frame_type_description_cb;
3419 custom_cbs.is_default_cb = colorized_frame_is_default_cb;
3420 custom_cbs.to_str_cb = colorized_frame_to_str_cb;
3421 register_string_like_preference(gui_column_module, "colorized_frame.fg", "Colorized Foreground",
3422 "Filter Colorized Foreground",
3423 &prefs.gui_colorized_fg, PREF_CUSTOM, &custom_cbs, true);
3425 custom_cbs.free_cb = free_string_like_preference;
3426 custom_cbs.reset_cb = reset_string_like_preference;
3427 custom_cbs.set_cb = colorized_frame_set_cb;
3428 custom_cbs.type_name_cb = colorized_frame_type_name_cb;
3429 custom_cbs.type_description_cb = colorized_frame_type_description_cb;
3430 custom_cbs.is_default_cb = colorized_frame_is_default_cb;
3431 custom_cbs.to_str_cb = colorized_frame_to_str_cb;
3432 register_string_like_preference(gui_column_module, "colorized_frame.bg", "Colorized Background",
3433 "Filter Colorized Background",
3434 &prefs.gui_colorized_bg, PREF_CUSTOM, &custom_cbs, true);
3436 prefs_register_color_preference(gui_color_module, "color_filter_bg.valid", "Valid color filter background",
3437 "Valid color filter background", &prefs.gui_text_valid);
3439 prefs_register_color_preference(gui_color_module, "color_filter_bg.invalid", "Invalid color filter background",
3440 "Invalid color filter background", &prefs.gui_text_invalid);
3442 prefs_register_color_preference(gui_color_module, "color_filter_bg.deprecated", "Deprecated color filter background",
3443 "Deprecated color filter background", &prefs.gui_text_deprecated);
3445 prefs_register_enum_preference(gui_module, "fileopen.style",
3446 "Where to start the File Open dialog box",
3447 "Where to start the File Open dialog box",
3448 &prefs.gui_fileopen_style, gui_fileopen_style, false);
3450 prefs_register_uint_preference(gui_module, "recent_files_count.max",
3451 "The max. number of items in the open recent files list",
3452 "The max. number of items in the open recent files list",
3454 &prefs.gui_recent_files_count_max);
3456 prefs_register_uint_preference(gui_module, "recent_display_filter_entries.max",
3457 "The max. number of entries in the display filter list",
3458 "The max. number of entries in the display filter list",
3460 &prefs.gui_recent_df_entries_max);
3462 register_string_like_preference(gui_module, "fileopen.dir", "Start Directory",
3463 "Directory to start in when opening File Open dialog.",
3464 &prefs.gui_fileopen_dir, PREF_DIRNAME, NULL, true);
3466 prefs_register_obsolete_preference(gui_module, "fileopen.remembered_dir");
3468 prefs_register_uint_preference(gui_module, "fileopen.preview",
3469 "The preview timeout in the File Open dialog",
3470 "The preview timeout in the File Open dialog",
3472 &prefs.gui_fileopen_preview);
3474 register_string_like_preference(gui_module, "tlskeylog_command", "Program to launch with TLS Keylog",
3475 "Program path or command line to launch with SSLKEYLOGFILE",
3476 &prefs.gui_tlskeylog_command, PREF_STRING, NULL, true);
3478 prefs_register_bool_preference(gui_module, "ask_unsaved",
3479 "Ask to save unsaved capture files",
3480 "Ask to save unsaved capture files?",
3481 &prefs.gui_ask_unsaved);
3483 prefs_register_bool_preference(gui_module, "autocomplete_filter",
3484 "Display autocompletion for filter text",
3485 "Display an autocomplete suggestion for display and capture filter controls",
3486 &prefs.gui_autocomplete_filter);
3488 prefs_register_bool_preference(gui_module, "find_wrap",
3489 "Wrap to beginning/end of file during search",
3490 "Wrap to beginning/end of file during search?",
3491 &prefs.gui_find_wrap);
3493 prefs_register_obsolete_preference(gui_module, "use_pref_save");
3495 prefs_register_bool_preference(gui_module, "geometry.save.position",
3496 "Save window position at exit",
3497 "Save window position at exit?",
3498 &prefs.gui_geometry_save_position);
3500 prefs_register_bool_preference(gui_module, "geometry.save.size",
3501 "Save window size at exit",
3502 "Save window size at exit?",
3503 &prefs.gui_geometry_save_size);
3505 prefs_register_bool_preference(gui_module, "geometry.save.maximized",
3506 "Save window maximized state at exit",
3507 "Save window maximized state at exit?",
3508 &prefs.gui_geometry_save_maximized);
3510 prefs_register_obsolete_preference(gui_module, "macosx_style");
3512 prefs_register_obsolete_preference(gui_module, "geometry.main.x");
3513 prefs_register_obsolete_preference(gui_module, "geometry.main.y");
3514 prefs_register_obsolete_preference(gui_module, "geometry.main.width");
3515 prefs_register_obsolete_preference(gui_module, "geometry.main.height");
3516 prefs_register_obsolete_preference(gui_module, "toolbar_main_show");
3518 prefs_register_enum_preference(gui_module, "toolbar_main_style",
3519 "Main Toolbar style",
3520 "Main Toolbar style",
3521 &prefs.gui_toolbar_main_style, gui_toolbar_style, false);
3523 prefs_register_obsolete_preference(gui_module, "toolbar_filter_style");
3524 prefs_register_obsolete_preference(gui_module, "webbrowser");
3526 prefs_register_bool_preference(gui_module, "update.enabled",
3527 "Check for updates",
3528 "Check for updates (Windows and macOS only)",
3529 &prefs.gui_update_enabled);
3531 prefs_register_enum_preference(gui_module, "update.channel",
3532 "Update channel",
3533 "The type of update to fetch. You should probably leave this set to STABLE.",
3534 (int*)(void*)(&prefs.gui_update_channel), gui_update_channel, false);
3536 prefs_register_uint_preference(gui_module, "update.interval",
3537 "How often to check for software updates",
3538 "How often to check for software updates in seconds",
3540 &prefs.gui_update_interval);
3542 prefs_register_uint_preference(gui_module, "debounce.timer",
3543 "How long to wait before processing computationally intensive user input",
3544 "How long to wait (in milliseconds) before processing "
3545 "computationally intensive user input. "
3546 "If you type quickly, consider lowering the value for a 'snappier' "
3547 "experience. "
3548 "If you type slowly, consider increasing the value to avoid performance issues. "
3549 "This is currently used to delay searches in View -> Internals -> Supported Protocols "
3550 "and Preferences -> Advanced menu.",
3552 &prefs.gui_debounce_timer);
3554 register_string_like_preference(gui_module, "window_title", "Custom window title",
3555 "Custom window title to be appended to the existing title\n"
3556 "%C = capture comment from command line\n"
3557 "%F = file path of the capture file\n"
3558 "%P = profile name\n"
3559 "%S = a conditional separator (\" - \") that only shows when surrounded by variables with values or static text\n"
3560 "%V = version info",
3561 &prefs.gui_window_title, PREF_STRING, NULL, true);
3563 register_string_like_preference(gui_module, "prepend_window_title", "Custom window title prefix",
3564 "Custom window title to be prepended to the existing title\n"
3565 "%C = capture comment from command line\n"
3566 "%F = file path of the capture file\n"
3567 "%P = profile name\n"
3568 "%S = a conditional separator (\" - \") that only shows when surrounded by variables with values or static text\n"
3569 "%V = version info",
3570 &prefs.gui_prepend_window_title, PREF_STRING, NULL, true);
3572 register_string_like_preference(gui_module, "start_title", "Custom start page title",
3573 "Custom start page title",
3574 &prefs.gui_start_title, PREF_STRING, NULL, true);
3576 prefs_register_enum_preference(gui_module, "version_placement",
3577 "Show version in the start page and/or main screen's title bar",
3578 "Show version in the start page and/or main screen's title bar",
3579 (int*)(void*)(&prefs.gui_version_placement), gui_version_placement_type, false);
3581 prefs_register_obsolete_preference(gui_module, "auto_scroll_on_expand");
3582 prefs_register_obsolete_preference(gui_module, "auto_scroll_percentage");
3584 prefs_register_uint_preference(gui_module, "max_export_objects",
3585 "Maximum number of exported objects",
3586 "The maximum number of objects that can be exported",
3588 &prefs.gui_max_export_objects);
3589 prefs_register_uint_preference(gui_module, "max_tree_items",
3590 "Maximum number of tree items",
3591 "The maximum number of items that can be added to the dissection tree (Increase with caution)",
3593 &prefs.gui_max_tree_items);
3595 * Used independently by proto_tree_add_node, call_dissector*, dissector_try_heuristic,
3596 * and increment_dissection_depth.
3598 prefs_register_uint_preference(gui_module, "max_tree_depth",
3599 "Maximum dissection depth",
3600 "The maximum depth for dissection tree and protocol layer checks. (Increase with caution)",
3602 &prefs.gui_max_tree_depth);
3604 prefs_register_bool_preference(gui_module, "welcome_page.show_recent",
3605 "Show recent files on the welcome page",
3606 "This will enable or disable the 'Open' list on the welcome page.",
3607 &prefs.gui_welcome_page_show_recent);
3609 /* User Interface : Layout */
3610 gui_layout_module = prefs_register_subtree(gui_module, "Layout", "Layout", gui_layout_callback);
3611 /* Adjust the preference effects of layout GUI for better handling of preferences at Wireshark (GUI) level */
3612 layout_gui_flags = prefs_get_module_effect_flags(gui_layout_module);
3613 layout_gui_flags |= PREF_EFFECT_GUI_LAYOUT;
3614 layout_gui_flags &= (~PREF_EFFECT_DISSECTION);
3616 prefs_register_uint_preference(gui_layout_module, "layout_type",
3617 "Layout type",
3618 "Layout type (1-6)",
3620 (unsigned*)(void*)(&prefs.gui_layout_type));
3621 prefs_set_effect_flags_by_name(gui_layout_module, "layout_type", layout_gui_flags);
3623 prefs_register_enum_preference(gui_layout_module, "layout_content_1",
3624 "Layout content of the pane 1",
3625 "Layout content of the pane 1",
3626 (int*)(void*)(&prefs.gui_layout_content_1), gui_layout_content, false);
3627 prefs_set_effect_flags_by_name(gui_layout_module, "layout_content_1", layout_gui_flags);
3629 prefs_register_enum_preference(gui_layout_module, "layout_content_2",
3630 "Layout content of the pane 2",
3631 "Layout content of the pane 2",
3632 (int*)(void*)(&prefs.gui_layout_content_2), gui_layout_content, false);
3633 prefs_set_effect_flags_by_name(gui_layout_module, "layout_content_2", layout_gui_flags);
3635 prefs_register_enum_preference(gui_layout_module, "layout_content_3",
3636 "Layout content of the pane 3",
3637 "Layout content of the pane 3",
3638 (int*)(void*)(&prefs.gui_layout_content_3), gui_layout_content, false);
3639 prefs_set_effect_flags_by_name(gui_layout_module, "layout_content_3", layout_gui_flags);
3641 prefs_register_bool_preference(gui_layout_module, "packet_list_separator.enabled",
3642 "Enable Packet List Separator",
3643 "Enable Packet List Separator",
3644 &prefs.gui_packet_list_separator);
3646 prefs_register_bool_preference(gui_layout_module, "packet_header_column_definition.enabled",
3647 "Show column definition in packet list header",
3648 "Show column definition in packet list header",
3649 &prefs.gui_packet_header_column_definition);
3651 /* packet_list_hover_style affects the colors, not the layout.
3652 * It's in the layout module to group it with the other packet list
3653 * preferences for the user's benefit with the dialog.
3655 prefs_register_bool_preference(gui_layout_module, "packet_list_hover_style.enabled",
3656 "Enable Packet List mouse-over colorization",
3657 "Enable Packet List mouse-over colorization",
3658 &prefs.gui_packet_list_hover_style);
3659 prefs_set_effect_flags_by_name(gui_layout_module, "packet_list_hover_style.enabled", gui_color_effect_flags);
3661 prefs_register_bool_preference(gui_layout_module, "show_selected_packet.enabled",
3662 "Show selected packet in the Status Bar",
3663 "Show selected packet in the Status Bar",
3664 &prefs.gui_show_selected_packet);
3666 prefs_register_bool_preference(gui_layout_module, "show_file_load_time.enabled",
3667 "Show file load time in the Status Bar",
3668 "Show file load time in the Status Bar",
3669 &prefs.gui_show_file_load_time);
3671 prefs_register_enum_preference(gui_layout_module, "packet_dialog_layout",
3672 "Packet Dialog layout",
3673 "Packet Dialog layout",
3674 (unsigned*)(void*)(&prefs.gui_packet_dialog_layout), gui_packet_dialog_layout, false);
3676 prefs_register_enum_preference(gui_module, "packet_list_elide_mode",
3677 "Elide mode",
3678 "The position of \"...\" (ellipsis) in packet list text.",
3679 (int*)(void*)(&prefs.gui_packet_list_elide_mode), gui_packet_list_elide_mode, false);
3680 prefs_register_uint_preference(gui_module, "decimal_places1",
3681 "Count of decimal places for values of type 1",
3682 "Sets the count of decimal places for values of type 1."
3683 "Type 1 values are defined by authors."
3684 "Value can be in range 2 to 10.",
3685 10,&prefs.gui_decimal_places1);
3687 prefs_register_uint_preference(gui_module, "decimal_places2",
3688 "Count of decimal places for values of type 2",
3689 "Sets the count of decimal places for values of type 2."
3690 "Type 2 values are defined by authors."
3691 "Value can be in range 2 to 10.",
3692 10,&prefs.gui_decimal_places2);
3694 prefs_register_uint_preference(gui_module, "decimal_places3",
3695 "Count of decimal places for values of type 3",
3696 "Sets the count of decimal places for values of type 3."
3697 "Type 3 values are defined by authors."
3698 "Value can be in range 2 to 10.",
3699 10,&prefs.gui_decimal_places3);
3701 prefs_register_bool_preference(gui_module, "rtp_player_use_disk1",
3702 "RTP Player saves temporary data to disk",
3703 "If set to true, RTP Player saves temporary data to "
3704 "temp files on disk. If not set, it uses memory."
3705 "Every stream uses one file therefore you might touch "
3706 "OS limit for count of opened files."
3707 "When ui.rtp_player_use_disk2 is set to true too, it uses "
3708 " two files per RTP stream together."
3709 ,&prefs.gui_rtp_player_use_disk1);
3711 prefs_register_bool_preference(gui_module, "rtp_player_use_disk2",
3712 "RTP Player saves temporary dictionary for data to disk",
3713 "If set to true, RTP Player saves temporary dictionary to "
3714 "temp files on disk. If not set, it uses memory."
3715 "Every stream uses one file therefore you might touch "
3716 "OS limit for count of opened files."
3717 "When ui.rtp_player_use_disk1 is set to true too, it uses "
3718 " two files per RTP stream."
3719 ,&prefs.gui_rtp_player_use_disk2);
3721 prefs_register_enum_preference(gui_layout_module, "gui_packet_list_copy_format_options_for_keyboard_shortcut",
3722 "Allows text to be copied with selected format",
3723 "Allows text to be copied with selected format when copied via keyboard",
3724 (int*)(void*)(&prefs.gui_packet_list_copy_format_options_for_keyboard_shortcut),
3725 gui_packet_list_copy_format_options_for_keyboard_shortcut, false);
3727 prefs_register_bool_preference(gui_layout_module, "gui_packet_list_copy_text_with_aligned_columns",
3728 "Allows text to be copied with aligned columns",
3729 "Allows text to be copied with aligned columns when copied via menu or keyboard",
3730 &prefs.gui_packet_list_copy_text_with_aligned_columns);
3732 prefs_register_bool_preference(gui_layout_module, "packet_list_show_related",
3733 "Show Related Packets",
3734 "Show related packet indicators in the first column",
3735 &prefs.gui_packet_list_show_related);
3737 prefs_register_bool_preference(gui_layout_module, "packet_list_show_minimap",
3738 "Enable Intelligent Scroll Bar",
3739 "Show the intelligent scroll bar (a minimap of packet list colors in the scrollbar)",
3740 &prefs.gui_packet_list_show_minimap);
3742 prefs_register_bool_preference(gui_module, "packet_list_is_sortable",
3743 "Allow packet list to be sortable",
3744 "To prevent sorting by mistake (which can take some time to calculate), it can be disabled",
3745 &prefs.gui_packet_list_sortable);
3747 prefs_register_uint_preference(gui_module, "packet_list_cached_rows_max",
3748 "Maximum cached rows",
3749 "Maximum number of rows that can be sorted by columns that require dissection. Increasing this increases memory consumption by caching column text",
3751 &prefs.gui_packet_list_cached_rows_max);
3753 prefs_register_bool_preference(gui_module, "interfaces_show_hidden",
3754 "Show hidden interfaces",
3755 "Show all interfaces, including interfaces marked as hidden",
3756 &prefs.gui_interfaces_show_hidden);
3758 prefs_register_bool_preference(gui_module, "interfaces_remote_display",
3759 "Show Remote interfaces",
3760 "Show remote interfaces in the interface selection",
3761 &prefs.gui_interfaces_remote_display);
3763 register_string_like_preference(gui_module, "interfaces_hidden_types", "Hide interface types in list",
3764 "Hide the given interface types in the startup list.\n"
3765 "A comma-separated string of interface type values (e.g. 5,9).\n"
3766 "0 = Wired,\n"
3767 "1 = AirPCAP,\n"
3768 "2 = Pipe,\n"
3769 "3 = STDIN,\n"
3770 "4 = Bluetooth,\n"
3771 "5 = Wireless,\n"
3772 "6 = Dial-Up,\n"
3773 "7 = USB,\n"
3774 "8 = External Capture,\n"
3775 "9 = Virtual",
3776 &prefs.gui_interfaces_hide_types, PREF_STRING, NULL, true);
3778 prefs_register_bool_preference(gui_module, "io_graph_automatic_update",
3779 "Enables automatic updates for IO Graph",
3780 "Enables automatic updates for IO Graph",
3781 &prefs.gui_io_graph_automatic_update);
3783 prefs_register_bool_preference(gui_module, "io_graph_enable_legend",
3784 "Enables the legend of IO Graph",
3785 "Enables the legend of IO Graph",
3786 &prefs.gui_io_graph_enable_legend);
3788 prefs_register_bool_preference(gui_module, "show_byteview_in_dialog",
3789 "Show the byte view in the packet details dialog",
3790 "Show the byte view in the packet details dialog",
3791 &prefs.gui_packet_details_show_byteview);
3793 /* Console
3794 * These are preferences that can be read/written using the
3795 * preference module API. These preferences still use their own
3796 * configuration screens for access, but this cuts down on the
3797 * preference "string compare list" in set_pref()
3799 console_module = prefs_register_module(NULL, "console", "Console",
3800 "Console logging and debugging output", NULL, NULL, false);
3802 prefs_register_obsolete_preference(console_module, "log.level");
3804 prefs_register_bool_preference(console_module, "incomplete_dissectors_check_debug",
3805 "Print debug line for incomplete dissectors",
3806 "Look for dissectors that left some bytes undecoded (debug)",
3807 &prefs.incomplete_dissectors_check_debug);
3809 /* Display filter Expressions
3810 * This used to be an array of individual fields that has now been
3811 * converted to a UAT. Just make it part of the GUI category even
3812 * though the name of the preference will never be seen in preference
3813 * file
3815 filter_expression_register_uat(gui_module);
3817 /* Capture
3818 * These are preferences that can be read/written using the
3819 * preference module API. These preferences still use their own
3820 * configuration screens for access, but this cuts down on the
3821 * preference "string compare list" in set_pref()
3823 capture_module = prefs_register_module(NULL, "capture", "Capture",
3824 "Capture preferences", NULL, NULL, false);
3825 /* Capture preferences don't affect dissection */
3826 prefs_set_module_effect_flags(capture_module, PREF_EFFECT_CAPTURE);
3828 register_string_like_preference(capture_module, "device", "Default capture device",
3829 "Default capture device",
3830 &prefs.capture_device, PREF_STRING, NULL, false);
3832 register_string_like_preference(capture_module, "devices_linktypes", "Interface link-layer header type",
3833 "Interface link-layer header types (Ex: en0(1),en1(143),...)",
3834 &prefs.capture_devices_linktypes, PREF_STRING, NULL, false);
3836 register_string_like_preference(capture_module, "devices_descr", "Interface descriptions",
3837 "Interface descriptions (Ex: eth0(eth0 descr),eth1(eth1 descr),...)",
3838 &prefs.capture_devices_descr, PREF_STRING, NULL, false);
3840 register_string_like_preference(capture_module, "devices_hide", "Hide interface",
3841 "Hide interface? (Ex: eth0,eth3,...)",
3842 &prefs.capture_devices_hide, PREF_STRING, NULL, false);
3844 register_string_like_preference(capture_module, "devices_monitor_mode", "Capture in monitor mode",
3845 "By default, capture in monitor mode on interface? (Ex: eth0,eth3,...)",
3846 &prefs.capture_devices_monitor_mode, PREF_STRING, NULL, false);
3848 register_string_like_preference(capture_module, "devices_buffersize", "Interface buffer size",
3849 "Interface buffer size (Ex: en0(1),en1(143),...)",
3850 &prefs.capture_devices_buffersize, PREF_STRING, NULL, false);
3852 register_string_like_preference(capture_module, "devices_snaplen", "Interface snap length",
3853 "Interface snap length (Ex: en0(65535),en1(1430),...)",
3854 &prefs.capture_devices_snaplen, PREF_STRING, NULL, false);
3856 register_string_like_preference(capture_module, "devices_pmode", "Interface promiscuous mode",
3857 "Interface promiscuous mode (Ex: en0(0),en1(1),...)",
3858 &prefs.capture_devices_pmode, PREF_STRING, NULL, false);
3860 prefs_register_bool_preference(capture_module, "prom_mode", "Capture in promiscuous mode",
3861 "Capture in promiscuous mode?", &prefs.capture_prom_mode);
3863 prefs_register_bool_preference(capture_module, "monitor_mode", "Capture in monitor mode on 802.11 devices",
3864 "Capture in monitor mode on all 802.11 devices that support it?", &prefs.capture_monitor_mode);
3866 register_string_like_preference(capture_module, "devices_filter", "Interface capture filter",
3867 "Interface capture filter (Ex: en0(tcp),en1(udp),...)",
3868 &prefs.capture_devices_filter, PREF_STRING, NULL, false);
3870 prefs_register_bool_preference(capture_module, "pcap_ng", "Capture in pcapng format",
3871 "Capture in pcapng format?", &prefs.capture_pcap_ng);
3873 prefs_register_bool_preference(capture_module, "real_time_update", "Update packet list in real time during capture",
3874 "Update packet list in real time during capture?", &prefs.capture_real_time);
3876 prefs_register_uint_preference(capture_module, "update_interval",
3877 "Capture update interval",
3878 "Capture update interval in ms",
3880 &prefs.capture_update_interval);
3882 prefs_register_bool_preference(capture_module, "no_interface_load", "Don't load interfaces on startup",
3883 "Don't automatically load capture interfaces on startup", &prefs.capture_no_interface_load);
3885 prefs_register_bool_preference(capture_module, "no_extcap", "Disable external capture interfaces",
3886 "Disable external capture modules (extcap)", &prefs.capture_no_extcap);
3888 prefs_register_obsolete_preference(capture_module, "auto_scroll");
3890 prefs_register_bool_preference(capture_module, "show_info", "Show capture information dialog while capturing",
3891 "Show capture information dialog while capturing?", &prefs.capture_show_info);
3893 prefs_register_obsolete_preference(capture_module, "syntax_check_filter");
3895 custom_cbs.free_cb = capture_column_free_cb;
3896 custom_cbs.reset_cb = capture_column_reset_cb;
3897 custom_cbs.set_cb = capture_column_set_cb;
3898 custom_cbs.type_name_cb = capture_column_type_name_cb;
3899 custom_cbs.type_description_cb = capture_column_type_description_cb;
3900 custom_cbs.is_default_cb = capture_column_is_default_cb;
3901 custom_cbs.to_str_cb = capture_column_to_str_cb;
3902 prefs_register_list_custom_preference(capture_module, "columns", "Capture options dialog column list",
3903 "List of columns to be displayed", &custom_cbs, capture_column_init_cb, &prefs.capture_columns);
3905 /* Name Resolution */
3906 nameres_module = prefs_register_module(NULL, "nameres", "Name Resolution",
3907 "Name Resolution", "ChCustPreferencesSection.html#ChCustPrefsNameSection", addr_resolve_pref_apply, true);
3908 addr_resolve_pref_init(nameres_module);
3909 oid_pref_init(nameres_module);
3910 maxmind_db_pref_init(nameres_module);
3912 /* Printing
3913 * None of these have any effect; we keep them as obsolete preferences
3914 * in order to avoid errors when reading older preference files.
3916 printing = prefs_register_module(NULL, "print", "Printing",
3917 "Printing", NULL, NULL, false);
3918 prefs_register_obsolete_preference(printing, "format");
3919 prefs_register_obsolete_preference(printing, "command");
3920 prefs_register_obsolete_preference(printing, "file");
3922 /* Codecs */
3923 codecs_module = prefs_register_module(NULL, "codecs", "Codecs",
3924 "Codecs", NULL, NULL, true);
3926 /* Statistics */
3927 stats_module = prefs_register_module(NULL, "statistics", "Statistics",
3928 "Statistics", "ChCustPreferencesSection.html#_statistics", &stats_callback, true);
3930 prefs_register_uint_preference(stats_module, "update_interval",
3931 "Tap update interval in ms",
3932 "Determines time between tap updates",
3934 &prefs.tap_update_interval);
3936 prefs_register_uint_preference(stats_module, "flow_graph_max_export_items",
3937 "Maximum Flow Graph items to export as image",
3938 "The maximum number of Flow Graph items (frames) "
3939 "to include when exporting the graph as an image. "
3940 "Note that some formats (e.g., JPEG) have inherent "
3941 "pixel limits and image viewers might be unable to "
3942 "handle very large images.",
3944 &prefs.flow_graph_max_export_items);
3946 prefs_register_bool_preference(stats_module, "st_enable_burstinfo",
3947 "Enable the calculation of burst information",
3948 "If enabled burst rates will be calculated for statistics that use the stats_tree system. "
3949 "Burst rates are calculated over a much shorter time interval than the rate column.",
3950 &prefs.st_enable_burstinfo);
3952 prefs_register_bool_preference(stats_module, "st_burst_showcount",
3953 "Show burst count for item rather than rate",
3954 "If selected the stats_tree statistics nodes will show the count of events "
3955 "within the burst window instead of a burst rate. Burst rate is calculated "
3956 "as number of events within burst window divided by the burst windown length.",
3957 &prefs.st_burst_showcount);
3959 prefs_register_uint_preference(stats_module, "st_burst_resolution",
3960 "Burst rate resolution (ms)",
3961 "Sets the duration of the time interval into which events are grouped when calculating "
3962 "the burst rate. Higher resolution (smaller number) increases processing overhead.",
3963 10,&prefs.st_burst_resolution);
3965 prefs_register_uint_preference(stats_module, "st_burst_windowlen",
3966 "Burst rate window size (ms)",
3967 "Sets the duration of the sliding window during which the burst rate is "
3968 "measured. Longer window relative to burst rate resolution increases "
3969 "processing overhead. Will be truncated to a multiple of burst resolution.",
3970 10,&prefs.st_burst_windowlen);
3972 prefs_register_enum_preference(stats_module, "st_sort_defcolflag",
3973 "Default sort column for stats_tree stats",
3974 "Sets the default column by which stats based on the stats_tree "
3975 "system is sorted.",
3976 &prefs.st_sort_defcolflag, st_sort_col_vals, false);
3978 prefs_register_bool_preference(stats_module, "st_sort_defdescending",
3979 "Default stats_tree sort order is descending",
3980 "When selected, statistics based on the stats_tree system will by default "
3981 "be sorted in descending order.",
3982 &prefs.st_sort_defdescending);
3984 prefs_register_bool_preference(stats_module, "st_sort_casesensitve",
3985 "Case sensitive sort of stats_tree item names",
3986 "When selected, the item/node names of statistics based on the stats_tree "
3987 "system will be sorted taking case into account. Else the case of the name "
3988 "will be ignored.",
3989 &prefs.st_sort_casesensitve);
3991 prefs_register_bool_preference(stats_module, "st_sort_rng_nameonly",
3992 "Always sort 'range' nodes by name",
3993 "When selected, the stats_tree nodes representing a range of values "
3994 "(0-49, 50-100, etc.) will always be sorted by name (the range of the "
3995 "node). Else range nodes are sorted by the same column as the rest of "
3996 " the tree.",
3997 &prefs.st_sort_rng_nameonly);
3999 prefs_register_bool_preference(stats_module, "st_sort_rng_fixorder",
4000 "Always sort 'range' nodes in ascending order",
4001 "When selected, the stats_tree nodes representing a range of values "
4002 "(0-49, 50-100, etc.) will always be sorted ascending; else it follows "
4003 "the sort direction of the tree. Only effective if \"Always sort "
4004 "'range' nodes by name\" is also selected.",
4005 &prefs.st_sort_rng_fixorder);
4007 prefs_register_bool_preference(stats_module, "st_sort_showfullname",
4008 "Display the full stats_tree plug-in name",
4009 "When selected, the full name (including menu path) of the stats_tree "
4010 "plug-in is show in windows. If cleared the plug-in name is shown "
4011 "without menu path (only the part of the name after last '/' character.)",
4012 &prefs.st_sort_showfullname);
4014 /* Protocols */
4015 protocols_module = prefs_register_module(NULL, "protocols", "Protocols",
4016 "Protocols", "ChCustPreferencesSection.html#ChCustPrefsProtocolsSection", NULL, true);
4018 prefs_register_bool_preference(protocols_module, "display_hidden_proto_items",
4019 "Display hidden protocol items",
4020 "Display all hidden protocol items in the packet list.",
4021 &prefs.display_hidden_proto_items);
4023 prefs_register_bool_preference(protocols_module, "display_byte_fields_with_spaces",
4024 "Display byte fields with a space character between bytes",
4025 "Display all byte fields with a space character between each byte in the packet list.",
4026 &prefs.display_byte_fields_with_spaces);
4029 * Note the -t / option only affects the display of the packet timestamp
4030 * in the default time column; this is for all other absolute times.
4032 prefs_register_enum_preference(protocols_module, "display_abs_time_ascii",
4033 "Format absolute times like asctime",
4034 "When to format absolute times similar to asctime instead of ISO 8601, for backwards compatibility with older Wireshark.",
4035 (int*)&prefs.display_abs_time_ascii, abs_time_format_options, false);
4037 prefs_register_bool_preference(protocols_module, "enable_incomplete_dissectors_check",
4038 "Look for incomplete dissectors",
4039 "Look for dissectors that left some bytes undecoded.",
4040 &prefs.enable_incomplete_dissectors_check);
4042 prefs_register_bool_preference(protocols_module, "strict_conversation_tracking_heuristics",
4043 "Enable stricter conversation tracking heuristics",
4044 "Protocols may use things like VLAN ID or interface ID to narrow the potential for duplicate conversations. "
4045 "Currently ICMP and ICMPv6 use this preference to add VLAN ID to conversation tracking, and IPv4 uses this preference to take VLAN ID into account during reassembly",
4046 &prefs.strict_conversation_tracking_heuristics);
4048 prefs_register_bool_preference(protocols_module, "ignore_dup_frames",
4049 "Ignore duplicate frames",
4050 "Ignore frames that are exact duplicates of any previous frame.",
4051 &prefs.ignore_dup_frames);
4053 prefs_register_enum_preference(protocols_module, "conversation_deinterlacing_key",
4054 "Deinterlacing conversations key",
4055 "Separate into different conversations frames that look like duplicates but have different Interface, MAC, or VLAN field values.",
4056 (int *)&prefs.conversation_deinterlacing_key, conv_deint_options, false);
4058 prefs_register_uint_preference(protocols_module, "ignore_dup_frames_cache_entries",
4059 "The max number of hashes to keep in memory for determining duplicates frames",
4060 "If \"Ignore duplicate frames\" is set, this setting sets the maximum number "
4061 "of cache entries to maintain. A 0 means no limit.",
4062 10, &prefs.ignore_dup_frames_cache_entries);
4065 /* Obsolete preferences
4066 * These "modules" were reorganized/renamed to correspond to their GUI
4067 * configuration screen within the preferences dialog
4070 /* taps is now part of the stats module */
4071 prefs_register_module(NULL, "taps", "TAPS", "TAPS", NULL, NULL, false);
4072 /* packet_list is now part of the protocol (parent) module */
4073 prefs_register_module(NULL, "packet_list", "PACKET_LIST", "PACKET_LIST", NULL, NULL, false);
4074 /* stream is now part of the gui module */
4075 prefs_register_module(NULL, "stream", "STREAM", "STREAM", NULL, NULL, false);
4079 /* Parse through a list of comma-separated, possibly quoted strings.
4080 Return a list of the string data. */
4081 GList *
4082 prefs_get_string_list(const char *str)
4084 enum { PRE_STRING, IN_QUOT, NOT_IN_QUOT };
4086 int state = PRE_STRING, i = 0;
4087 bool backslash = false;
4088 unsigned char cur_c;
4089 const size_t default_size = 64;
4090 GString *slstr = NULL;
4091 GList *sl = NULL;
4093 /* Allocate a buffer for the first string. */
4094 slstr = g_string_sized_new(default_size);
4096 for (;;) {
4097 cur_c = str[i];
4098 if (cur_c == '\0') {
4099 /* It's the end of the input, so it's the end of the string we
4100 were working on, and there's no more input. */
4101 if (state == IN_QUOT || backslash) {
4102 /* We were in the middle of a quoted string or backslash escape,
4103 and ran out of characters; that's an error. */
4104 g_string_free(slstr, TRUE);
4105 prefs_clear_string_list(sl);
4106 return NULL;
4108 if (slstr->len > 0)
4109 sl = g_list_append(sl, g_string_free(slstr, FALSE));
4110 else
4111 g_string_free(slstr, TRUE);
4112 break;
4114 if (cur_c == '"' && !backslash) {
4115 switch (state) {
4116 case PRE_STRING:
4117 /* We hadn't yet started processing a string; this starts the
4118 string, and we're now quoting. */
4119 state = IN_QUOT;
4120 break;
4121 case IN_QUOT:
4122 /* We're in the middle of a quoted string, and we saw a quotation
4123 mark; we're no longer quoting. */
4124 state = NOT_IN_QUOT;
4125 break;
4126 case NOT_IN_QUOT:
4127 /* We're working on a string, but haven't seen a quote; we're
4128 now quoting. */
4129 state = IN_QUOT;
4130 break;
4131 default:
4132 break;
4134 } else if (cur_c == '\\' && !backslash) {
4135 /* We saw a backslash, and the previous character wasn't a
4136 backslash; escape the next character.
4138 This also means we've started a new string. */
4139 backslash = true;
4140 if (state == PRE_STRING)
4141 state = NOT_IN_QUOT;
4142 } else if (cur_c == ',' && state != IN_QUOT && !backslash) {
4143 /* We saw a comma, and we're not in the middle of a quoted string
4144 and it wasn't preceded by a backslash; it's the end of
4145 the string we were working on... */
4146 if (slstr->len > 0) {
4147 sl = g_list_append(sl, g_string_free(slstr, FALSE));
4148 slstr = g_string_sized_new(default_size);
4151 /* ...and the beginning of a new string. */
4152 state = PRE_STRING;
4153 } else if (!g_ascii_isspace(cur_c) || state != PRE_STRING) {
4154 /* Either this isn't a white-space character, or we've started a
4155 string (i.e., already seen a non-white-space character for that
4156 string and put it into the string).
4158 The character is to be put into the string; do so. */
4159 g_string_append_c(slstr, cur_c);
4161 /* If it was backslash-escaped, we're done with the backslash escape. */
4162 backslash = false;
4164 i++;
4166 return(sl);
4169 char *join_string_list(GList *sl)
4171 GString *joined_str = g_string_new("");
4172 GList *cur, *first;
4173 char *str;
4174 unsigned item_count = 0;
4176 cur = first = g_list_first(sl);
4177 while (cur) {
4178 item_count++;
4179 str = (char *)cur->data;
4181 if (cur != first)
4182 g_string_append_c(joined_str, ',');
4184 if (item_count % 2) {
4185 /* Wrap the line. */
4186 g_string_append(joined_str, "\n\t");
4187 } else
4188 g_string_append_c(joined_str, ' ');
4190 g_string_append_c(joined_str, '"');
4191 while (*str) {
4192 gunichar uc = g_utf8_get_char (str);
4194 if (uc == '"' || uc == '\\')
4195 g_string_append_c(joined_str, '\\');
4197 if (g_unichar_isprint(uc))
4198 g_string_append_unichar (joined_str, uc);
4200 str = g_utf8_next_char (str);
4203 g_string_append_c(joined_str, '"');
4205 cur = cur->next;
4207 return g_string_free(joined_str, FALSE);
4210 void
4211 prefs_clear_string_list(GList *sl)
4213 g_list_free_full(sl, g_free);
4217 * Takes a string, a pointer to an array of "enum_val_t"s, and a default int
4218 * value.
4219 * The array must be terminated by an entry with a null "name" string.
4221 * If the string matches a "name" string in an entry, the value from that
4222 * entry is returned.
4224 * Otherwise, if a string matches a "description" string in an entry, the
4225 * value from that entry is returned; we do that for backwards compatibility,
4226 * as we used to have only a "name" string that was used both for command-line
4227 * and configuration-file values and in the GUI (which meant either that
4228 * the GUI had what might be somewhat cryptic values to select from or that
4229 * the "-o" flag took long strings, often with spaces in them).
4231 * Otherwise, the default value that was passed as the third argument is
4232 * returned.
4234 static int
4235 find_val_for_string(const char *needle, const enum_val_t *haystack,
4236 int default_value)
4238 int i;
4240 for (i = 0; haystack[i].name != NULL; i++) {
4241 if (g_ascii_strcasecmp(needle, haystack[i].name) == 0) {
4242 return haystack[i].value;
4245 for (i = 0; haystack[i].name != NULL; i++) {
4246 if (g_ascii_strcasecmp(needle, haystack[i].description) == 0) {
4247 return haystack[i].value;
4250 return default_value;
4253 /* Preferences file format:
4254 * - Configuration directives start at the beginning of the line, and
4255 * are terminated with a colon.
4256 * - Directives can be continued on the next line by preceding them with
4257 * whitespace.
4259 * Example:
4261 # This is a comment line
4262 print.command: lpr
4263 print.file: /a/very/long/path/
4264 to/wireshark-out.ps
4268 /* Initialize non-dissector preferences to wired-in default values Called
4269 * at program startup and any time the profile changes. (The dissector
4270 * preferences are assumed to be set to those values by the dissectors.)
4271 * They may be overridden by the global preferences file or the user's
4272 * preferences file.
4274 static void
4275 init_prefs(void)
4277 if (prefs_initialized)
4278 return;
4280 uat_load_all();
4283 * Ensure the "global" preferences have been initialized so the
4284 * preference API has the proper default values to work from
4286 pre_init_prefs();
4288 prefs_register_modules();
4290 prefs_initialized = true;
4294 * Initialize non-dissector preferences used by the "register preference" API
4295 * to default values so the default values can be used when registered.
4297 * String, filename, and directory preferences will be g_freed so they must
4298 * be g_mallocated.
4300 static void
4301 pre_init_prefs(void)
4303 int i;
4304 char *col_name;
4305 fmt_data *cfmt;
4306 static const char *col_fmt_packets[] = {
4307 "No.", "%m", "Time", "%t",
4308 "Source", "%s", "Destination", "%d",
4309 "Protocol", "%p", "Length", "%L",
4310 "Info", "%i" };
4311 static const char **col_fmt = col_fmt_packets;
4312 int num_cols = 7;
4314 if (application_flavor_is_stratoshark()) {
4315 static const char *col_fmt_logs[] = {
4316 "No.", "%m",
4317 "Time", "%t",
4318 "Event name", "%Cus:sysdig.event_name:0:R",
4319 "Dir", "%Cus:evt.dir:0:R",
4320 "Proc Name", "%Cus:proc.name:0:R",
4321 "PID", "%Cus:proc.pid:0:R",
4322 "TID", "%Cus:thread.tid:0:R",
4323 "FD", "%Cus:fd.num:0:R",
4324 "FD Name", "%Cus:fd.name:0:R",
4325 "Container Name", "%Cus:container.name:0:R",
4326 "Arguments", "%Cus:evt.args:0:R",
4327 "Info", "%i"
4329 col_fmt = col_fmt_logs;
4330 num_cols = 12;
4333 prefs.restore_filter_after_following_stream = false;
4334 prefs.gui_toolbar_main_style = TB_STYLE_ICONS;
4335 /* We try to find the best font in the Qt code */
4336 g_free(prefs.gui_font_name);
4337 prefs.gui_font_name = g_strdup("");
4338 prefs.gui_active_fg.red = 0;
4339 prefs.gui_active_fg.green = 0;
4340 prefs.gui_active_fg.blue = 0;
4341 prefs.gui_active_bg.red = 52223;
4342 prefs.gui_active_bg.green = 59647;
4343 prefs.gui_active_bg.blue = 65535;
4344 prefs.gui_active_style = COLOR_STYLE_DEFAULT;
4345 prefs.gui_inactive_fg.red = 0;
4346 prefs.gui_inactive_fg.green = 0;
4347 prefs.gui_inactive_fg.blue = 0;
4348 prefs.gui_inactive_bg.red = 61439;
4349 prefs.gui_inactive_bg.green = 61439;
4350 prefs.gui_inactive_bg.blue = 61439;
4351 prefs.gui_inactive_style = COLOR_STYLE_DEFAULT;
4352 prefs.gui_marked_fg.red = 65535;
4353 prefs.gui_marked_fg.green = 65535;
4354 prefs.gui_marked_fg.blue = 65535;
4355 prefs.gui_marked_bg.red = 0;
4356 prefs.gui_marked_bg.green = 8224;
4357 prefs.gui_marked_bg.blue = 10794;
4358 prefs.gui_ignored_fg.red = 32767;
4359 prefs.gui_ignored_fg.green = 32767;
4360 prefs.gui_ignored_fg.blue = 32767;
4361 prefs.gui_ignored_bg.red = 65535;
4362 prefs.gui_ignored_bg.green = 65535;
4363 prefs.gui_ignored_bg.blue = 65535;
4364 g_free(prefs.gui_colorized_fg);
4365 prefs.gui_colorized_fg = g_strdup("000000,000000,000000,000000,000000,000000,000000,000000,000000,000000");
4366 g_free(prefs.gui_colorized_bg);
4367 prefs.gui_colorized_bg = g_strdup("ffc0c0,ffc0ff,e0c0e0,c0c0ff,c0e0e0,c0ffff,c0ffc0,ffffc0,e0e0c0,e0e0e0");
4368 prefs.st_client_fg.red = 32767;
4369 prefs.st_client_fg.green = 0;
4370 prefs.st_client_fg.blue = 0;
4371 prefs.st_client_bg.red = 64507;
4372 prefs.st_client_bg.green = 60909;
4373 prefs.st_client_bg.blue = 60909;
4374 prefs.st_server_fg.red = 0;
4375 prefs.st_server_fg.green = 0;
4376 prefs.st_server_fg.blue = 32767;
4377 prefs.st_server_bg.red = 60909;
4378 prefs.st_server_bg.green = 60909;
4379 prefs.st_server_bg.blue = 64507;
4381 if (gui_theme_is_dark) {
4382 // Green, red and yellow with HSV V = 84
4383 prefs.gui_text_valid.red = 0x0000; /* dark green */
4384 prefs.gui_text_valid.green = 0x66ff;
4385 prefs.gui_text_valid.blue = 0x0000;
4386 prefs.gui_text_invalid.red = 0x66FF; /* dark red */
4387 prefs.gui_text_invalid.green = 0x0000;
4388 prefs.gui_text_invalid.blue = 0x0000;
4389 prefs.gui_text_deprecated.red = 0x66FF; /* dark yellow / olive */
4390 prefs.gui_text_deprecated.green = 0x66FF;
4391 prefs.gui_text_deprecated.blue = 0x0000;
4392 } else {
4393 // Green, red and yellow with HSV V = 20
4394 prefs.gui_text_valid.red = 0xAFFF; /* light green */
4395 prefs.gui_text_valid.green = 0xFFFF;
4396 prefs.gui_text_valid.blue = 0xAFFF;
4397 prefs.gui_text_invalid.red = 0xFFFF; /* light red */
4398 prefs.gui_text_invalid.green = 0xAFFF;
4399 prefs.gui_text_invalid.blue = 0xAFFF;
4400 prefs.gui_text_deprecated.red = 0xFFFF; /* light yellow */
4401 prefs.gui_text_deprecated.green = 0xFFFF;
4402 prefs.gui_text_deprecated.blue = 0xAFFF;
4405 prefs.gui_geometry_save_position = true;
4406 prefs.gui_geometry_save_size = true;
4407 prefs.gui_geometry_save_maximized= true;
4408 prefs.gui_fileopen_style = FO_STYLE_LAST_OPENED;
4409 prefs.gui_recent_df_entries_max = 10;
4410 prefs.gui_recent_files_count_max = 10;
4411 g_free(prefs.gui_fileopen_dir);
4412 prefs.gui_fileopen_dir = g_strdup(get_persdatafile_dir());
4413 prefs.gui_fileopen_preview = 3;
4414 g_free(prefs.gui_tlskeylog_command);
4415 prefs.gui_tlskeylog_command = g_strdup("");
4416 prefs.gui_ask_unsaved = true;
4417 prefs.gui_autocomplete_filter = true;
4418 prefs.gui_find_wrap = true;
4419 prefs.gui_update_enabled = true;
4420 prefs.gui_update_channel = UPDATE_CHANNEL_STABLE;
4421 prefs.gui_update_interval = 60*60*24; /* Seconds */
4422 prefs.gui_debounce_timer = 400; /* milliseconds */
4423 g_free(prefs.gui_window_title);
4424 prefs.gui_window_title = g_strdup("");
4425 g_free(prefs.gui_prepend_window_title);
4426 prefs.gui_prepend_window_title = g_strdup("");
4427 g_free(prefs.gui_start_title);
4428 prefs.gui_start_title = g_strdup("The World's Most Popular Network Protocol Analyzer");
4429 prefs.gui_version_placement = version_both;
4430 prefs.gui_welcome_page_show_recent = true;
4431 prefs.gui_layout_type = layout_type_2;
4432 prefs.gui_layout_content_1 = layout_pane_content_plist;
4433 prefs.gui_layout_content_2 = layout_pane_content_pdetails;
4434 prefs.gui_layout_content_3 = layout_pane_content_pbytes;
4435 prefs.gui_packet_list_elide_mode = ELIDE_RIGHT;
4436 prefs.gui_packet_list_copy_format_options_for_keyboard_shortcut = COPY_FORMAT_TEXT;
4437 prefs.gui_packet_list_copy_text_with_aligned_columns = false;
4438 prefs.gui_packet_list_show_related = true;
4439 prefs.gui_packet_list_show_minimap = true;
4440 prefs.gui_packet_list_sortable = true;
4441 prefs.gui_packet_list_cached_rows_max = 10000;
4442 g_free (prefs.gui_interfaces_hide_types);
4443 prefs.gui_interfaces_hide_types = g_strdup("");
4444 prefs.gui_interfaces_show_hidden = false;
4445 prefs.gui_interfaces_remote_display = true;
4446 prefs.gui_packet_list_separator = false;
4447 prefs.gui_packet_header_column_definition = true;
4448 prefs.gui_packet_list_hover_style = true;
4449 prefs.gui_show_selected_packet = false;
4450 prefs.gui_show_file_load_time = false;
4451 prefs.gui_max_export_objects = 1000;
4452 prefs.gui_max_tree_items = 1 * 1000 * 1000;
4453 prefs.gui_max_tree_depth = 5 * 100;
4454 prefs.gui_decimal_places1 = DEF_GUI_DECIMAL_PLACES1;
4455 prefs.gui_decimal_places2 = DEF_GUI_DECIMAL_PLACES2;
4456 prefs.gui_decimal_places3 = DEF_GUI_DECIMAL_PLACES3;
4458 if (prefs.col_list) {
4459 free_col_info(prefs.col_list);
4460 prefs.col_list = NULL;
4462 for (i = 0; i < num_cols; i++) {
4463 cfmt = g_new0(fmt_data,1);
4464 cfmt->title = g_strdup(col_fmt[i * 2]);
4465 cfmt->visible = true;
4466 cfmt->display = COLUMN_DISPLAY_STRINGS;
4467 parse_column_format(cfmt, col_fmt[(i * 2) + 1]);
4468 prefs.col_list = g_list_append(prefs.col_list, cfmt);
4470 prefs.num_cols = num_cols;
4472 /* set the default values for the capture dialog box */
4473 prefs.capture_prom_mode = true;
4474 prefs.capture_monitor_mode = false;
4475 prefs.capture_pcap_ng = true;
4476 prefs.capture_real_time = true;
4477 prefs.capture_update_interval = DEFAULT_UPDATE_INTERVAL;
4478 prefs.capture_no_extcap = false;
4479 prefs.capture_show_info = false;
4481 if (!prefs.capture_columns) {
4482 /* First time through */
4483 for (i = 0; i < num_capture_cols; i++) {
4484 col_name = g_strdup(capture_cols[i]);
4485 prefs.capture_columns = g_list_append(prefs.capture_columns, col_name);
4489 /* set the default values for the tap/statistics dialog box */
4490 prefs.tap_update_interval = TAP_UPDATE_DEFAULT_INTERVAL;
4491 prefs.flow_graph_max_export_items = 1000;
4492 prefs.st_enable_burstinfo = true;
4493 prefs.st_burst_showcount = false;
4494 prefs.st_burst_resolution = ST_DEF_BURSTRES;
4495 prefs.st_burst_windowlen = ST_DEF_BURSTLEN;
4496 prefs.st_sort_casesensitve = true;
4497 prefs.st_sort_rng_fixorder = true;
4498 prefs.st_sort_rng_nameonly = true;
4499 prefs.st_sort_defcolflag = ST_SORT_COL_COUNT;
4500 prefs.st_sort_defdescending = true;
4501 prefs.st_sort_showfullname = false;
4503 /* protocols */
4504 prefs.display_hidden_proto_items = false;
4505 prefs.display_byte_fields_with_spaces = false;
4506 prefs.display_abs_time_ascii = ABS_TIME_ASCII_TREE;
4507 prefs.ignore_dup_frames = false;
4508 prefs.ignore_dup_frames_cache_entries = 10000;
4510 /* set the default values for the io graph dialog */
4511 prefs.gui_io_graph_automatic_update = true;
4512 prefs.gui_io_graph_enable_legend = true;
4514 /* set the default values for the packet dialog */
4515 prefs.gui_packet_dialog_layout = layout_vertical;
4516 prefs.gui_packet_details_show_byteview = true;
4520 * Reset a single dissector preference.
4522 void
4523 reset_pref(pref_t *pref)
4525 int type;
4526 if (!pref) return;
4528 type = pref->type;
4531 * This preference is no longer supported; it's not a
4532 * real preference, so we don't reset it (i.e., we
4533 * treat it as if it weren't found in the list of
4534 * preferences, and we weren't called in the first place).
4536 if (IS_PREF_OBSOLETE(type))
4537 return;
4538 else
4539 RESET_PREF_OBSOLETE(type);
4541 switch (type) {
4543 case PREF_UINT:
4544 *pref->varp.uint = pref->default_val.uint;
4545 break;
4547 case PREF_BOOL:
4548 *pref->varp.boolp = pref->default_val.boolval;
4549 break;
4551 case PREF_ENUM:
4552 case PREF_PROTO_TCP_SNDAMB_ENUM:
4553 *pref->varp.enump = pref->default_val.enumval;
4554 break;
4556 case PREF_STRING:
4557 case PREF_SAVE_FILENAME:
4558 case PREF_OPEN_FILENAME:
4559 case PREF_DIRNAME:
4560 case PREF_PASSWORD:
4561 case PREF_DISSECTOR:
4562 reset_string_like_preference(pref);
4563 break;
4565 case PREF_RANGE:
4566 case PREF_DECODE_AS_RANGE:
4567 wmem_free(wmem_epan_scope(), *pref->varp.range);
4568 *pref->varp.range = range_copy(wmem_epan_scope(), pref->default_val.range);
4569 break;
4571 case PREF_STATIC_TEXT:
4572 case PREF_UAT:
4573 /* Nothing to do */
4574 break;
4576 case PREF_COLOR:
4577 *pref->varp.colorp = pref->default_val.color;
4578 break;
4580 case PREF_CUSTOM:
4581 pref->custom_cbs.reset_cb(pref);
4582 break;
4586 static void
4587 reset_pref_cb(void *data, void *user_data)
4589 pref_t *pref = (pref_t *) data;
4590 module_t *module = (module_t *)user_data;
4592 if (pref && (pref->type == PREF_RANGE || pref->type == PREF_DECODE_AS_RANGE)) {
4594 * Some dissectors expect the range (returned via prefs_get_range_value)
4595 * to remain valid if it has not changed. If it did change, then we
4596 * should set "prefs_changed_flags" to ensure that the preference apply
4597 * callback is invoked. That callback will notify dissectors that it
4598 * should no longer assume the range to be valid.
4600 if (ranges_are_equal(*pref->varp.range, pref->default_val.range)) {
4601 /* Optimization: do not invoke apply callback if nothing changed. */
4602 return;
4604 module->prefs_changed_flags |= prefs_get_effect_flags(pref);
4606 reset_pref(pref);
4610 * Reset all preferences for a module.
4612 static bool
4613 reset_module_prefs(const void *key _U_, void *value, void *data _U_)
4615 module_t *module = (module_t *)value;
4616 g_list_foreach(module->prefs, reset_pref_cb, module);
4617 return false;
4620 /* Reset preferences */
4621 void
4622 prefs_reset(void)
4624 prefs_initialized = false;
4625 g_free(prefs.saved_at_version);
4626 prefs.saved_at_version = NULL;
4629 * Unload all UAT preferences.
4631 uat_unload_all();
4634 * Unload any loaded MIBs.
4636 oids_cleanup();
4639 * Reset the non-dissector preferences.
4641 init_prefs();
4644 * Reset the non-UAT dissector preferences.
4646 wmem_tree_foreach(prefs_modules, reset_module_prefs, NULL);
4649 #ifdef _WIN32
4650 static void
4651 read_registry(void)
4653 HKEY hTestKey;
4654 DWORD data;
4655 DWORD data_size = sizeof(DWORD);
4656 DWORD ret;
4658 ret = RegOpenKeyExA(HKEY_CURRENT_USER, REG_HKCU_WIRESHARK_KEY, 0, KEY_READ, &hTestKey);
4659 if (ret != ERROR_SUCCESS && ret != ERROR_FILE_NOT_FOUND) {
4660 ws_noisy("Cannot open HKCU "REG_HKCU_WIRESHARK_KEY": 0x%lx", ret);
4661 return;
4664 ret = RegQueryValueExA(hTestKey, LOG_HKCU_CONSOLE_OPEN, NULL, NULL, (LPBYTE)&data, &data_size);
4665 if (ret == ERROR_SUCCESS) {
4666 ws_log_console_open = (ws_log_console_open_pref)data;
4667 ws_noisy("Got "LOG_HKCU_CONSOLE_OPEN" from Windows registry: %d", ws_log_console_open);
4669 else if (ret != ERROR_FILE_NOT_FOUND) {
4670 ws_noisy("Error reading registry key "LOG_HKCU_CONSOLE_OPEN": 0x%lx", ret);
4673 RegCloseKey(hTestKey);
4675 #endif
4677 void
4678 prefs_read_module(const char *module)
4680 int err;
4681 char *pf_path;
4682 FILE *pf;
4684 module_t *target_module = prefs_find_module(module);
4685 if (!target_module) {
4686 return;
4689 /* Construct the pathname of the user's preferences file for the module. */
4690 char *pf_name = wmem_strdup_printf(NULL, "%s.cfg", module);
4691 pf_path = get_persconffile_path(pf_name, true);
4692 wmem_free(NULL, pf_name);
4694 /* Read the user's module preferences file, if it exists and is not a dir. */
4695 if (!test_for_regular_file(pf_path) || ((pf = ws_fopen(pf_path, "r")) == NULL)) {
4696 g_free(pf_path);
4697 /* Fall back to the user's generic preferences file. */
4698 pf_path = get_persconffile_path(PF_NAME, true);
4699 pf = ws_fopen(pf_path, "r");
4702 if (pf != NULL) {
4703 /* We succeeded in opening it; read it. */
4704 err = read_prefs_file(pf_path, pf, set_pref, target_module);
4705 if (err != 0) {
4706 /* We had an error reading the file; report it. */
4707 report_warning("Error reading your preferences file \"%s\": %s.",
4708 pf_path, g_strerror(err));
4709 } else
4710 g_free(pf_path);
4711 fclose(pf);
4712 } else {
4713 /* We failed to open it. If we failed for some reason other than
4714 "it doesn't exist", return the errno and the pathname, so our
4715 caller can report the error. */
4716 if (errno != ENOENT) {
4717 report_warning("Can't open your preferences file \"%s\": %s.",
4718 pf_path, g_strerror(errno));
4719 } else
4720 g_free(pf_path);
4723 return;
4726 /* Read the preferences file, fill in "prefs", and return a pointer to it.
4728 If we got an error (other than "it doesn't exist") we report it through
4729 the UI. */
4730 e_prefs *
4731 read_prefs(void)
4733 int err;
4734 char *pf_path;
4735 FILE *pf;
4737 /* clean up libsmi structures before reading prefs */
4738 oids_cleanup();
4740 init_prefs();
4742 #ifdef _WIN32
4743 read_registry();
4744 #endif
4747 * If we don't already have the pathname of the global preferences
4748 * file, construct it. Then, in either case, try to open the file.
4750 if (gpf_path == NULL) {
4752 * We don't have the path; try the new path first, and, if that
4753 * file doesn't exist, try the old path.
4755 gpf_path = get_datafile_path(PF_NAME);
4756 if ((pf = ws_fopen(gpf_path, "r")) == NULL && errno == ENOENT) {
4758 * It doesn't exist by the new name; try the old name.
4760 g_free(gpf_path);
4761 gpf_path = get_datafile_path(OLD_GPF_NAME);
4762 pf = ws_fopen(gpf_path, "r");
4764 } else {
4766 * We have the path; try it.
4768 pf = ws_fopen(gpf_path, "r");
4772 * If we were able to open the file, read it.
4773 * XXX - if it failed for a reason other than "it doesn't exist",
4774 * report the error.
4776 if (pf != NULL) {
4778 * Start out the counters of "mgcp.{tcp,udp}.port" entries we've
4779 * seen.
4781 mgcp_tcp_port_count = 0;
4782 mgcp_udp_port_count = 0;
4784 /* We succeeded in opening it; read it. */
4785 err = read_prefs_file(gpf_path, pf, set_pref, NULL);
4786 if (err != 0) {
4787 /* We had an error reading the file; report it. */
4788 report_warning("Error reading global preferences file \"%s\": %s.",
4789 gpf_path, g_strerror(err));
4791 fclose(pf);
4792 } else {
4793 /* We failed to open it. If we failed for some reason other than
4794 "it doesn't exist", report the error. */
4795 if (errno != ENOENT) {
4796 if (errno != 0) {
4797 report_warning("Can't open global preferences file \"%s\": %s.",
4798 gpf_path, g_strerror(errno));
4803 /* Construct the pathname of the user's preferences file. */
4804 pf_path = get_persconffile_path(PF_NAME, true);
4806 /* Read the user's preferences file, if it exists. */
4807 if ((pf = ws_fopen(pf_path, "r")) != NULL) {
4809 * Start out the counters of "mgcp.{tcp,udp}.port" entries we've
4810 * seen.
4812 mgcp_tcp_port_count = 0;
4813 mgcp_udp_port_count = 0;
4815 /* We succeeded in opening it; read it. */
4816 err = read_prefs_file(pf_path, pf, set_pref, NULL);
4817 if (err != 0) {
4818 /* We had an error reading the file; report it. */
4819 report_warning("Error reading your preferences file \"%s\": %s.",
4820 pf_path, g_strerror(err));
4821 } else
4822 g_free(pf_path);
4823 fclose(pf);
4824 } else {
4825 /* We failed to open it. If we failed for some reason other than
4826 "it doesn't exist", return the errno and the pathname, so our
4827 caller can report the error. */
4828 if (errno != ENOENT) {
4829 report_warning("Can't open your preferences file \"%s\": %s.",
4830 pf_path, g_strerror(errno));
4831 } else
4832 g_free(pf_path);
4835 /* load SMI modules if needed */
4836 oids_init();
4838 return &prefs;
4841 /* read the preferences file (or similar) and call the callback
4842 * function to set each key/value pair found */
4844 read_prefs_file(const char *pf_path, FILE *pf,
4845 pref_set_pair_cb pref_set_pair_fct, void *private_data)
4847 enum {
4848 START, /* beginning of a line */
4849 IN_VAR, /* processing key name */
4850 PRE_VAL, /* finished processing key name, skipping white space before value */
4851 IN_VAL, /* processing value */
4852 IN_SKIP /* skipping to the end of the line */
4853 } state = START;
4854 int got_c;
4855 GString *cur_val;
4856 GString *cur_var;
4857 bool got_val = false;
4858 int fline = 1, pline = 1;
4859 char hint[] = "(save preferences to remove this warning)";
4860 char ver[128];
4862 cur_val = g_string_new("");
4863 cur_var = g_string_new("");
4865 /* Try to read in the profile name in the first line of the preferences file. */
4866 if (fscanf(pf, "# Configuration file for %127[^\r\n]", ver) == 1) {
4867 /* Assume trailing period and remove it */
4868 g_free(prefs.saved_at_version);
4869 prefs.saved_at_version = g_strndup(ver, strlen(ver) - 1);
4871 rewind(pf);
4873 while ((got_c = ws_getc_unlocked(pf)) != EOF) {
4874 if (got_c == '\r') {
4875 /* Treat CR-LF at the end of a line like LF, so that if we're reading
4876 * a Windows-format file on UN*X, we handle it the same way we'd handle
4877 * a UN*X-format file. */
4878 got_c = ws_getc_unlocked(pf);
4879 if (got_c == EOF)
4880 break;
4881 if (got_c != '\n') {
4882 /* Put back the character after the CR, and process the CR normally. */
4883 ungetc(got_c, pf);
4884 got_c = '\r';
4887 if (got_c == '\n') {
4888 state = START;
4889 fline++;
4890 continue;
4893 switch (state) {
4894 case START:
4895 if (g_ascii_isalnum(got_c)) {
4896 if (cur_var->len > 0) {
4897 if (got_val) {
4898 if (cur_val->len > 0) {
4899 if (cur_val->str[cur_val->len-1] == ',') {
4901 * If the pref has a trailing comma, eliminate it.
4903 cur_val->str[cur_val->len-1] = '\0';
4904 ws_warning("%s line %d: trailing comma in \"%s\" %s", pf_path, pline, cur_var->str, hint);
4907 /* Call the routine to set the preference; it will parse
4908 the value as appropriate.
4910 Since we're reading a file, rather than processing
4911 explicit user input, for range preferences, silently
4912 lower values in excess of the range's maximum, rather
4913 than reporting errors and failing. */
4914 switch (pref_set_pair_fct(cur_var->str, cur_val->str, private_data, false)) {
4916 case PREFS_SET_OK:
4917 break;
4919 case PREFS_SET_SYNTAX_ERR:
4920 report_warning("Syntax error in preference \"%s\" at line %d of\n%s %s",
4921 cur_var->str, pline, pf_path, hint);
4922 break;
4924 case PREFS_SET_NO_SUCH_PREF:
4925 ws_warning("No such preference \"%s\" at line %d of\n%s %s",
4926 cur_var->str, pline, pf_path, hint);
4927 prefs.unknown_prefs = true;
4928 break;
4930 case PREFS_SET_OBSOLETE:
4932 * If an attempt is made to save the
4933 * preferences, a popup warning will be
4934 * displayed stating that obsolete prefs
4935 * have been detected and the user will
4936 * be given the opportunity to save these
4937 * prefs under a different profile name.
4938 * The prefs in question need to be listed
4939 * in the console window so that the
4940 * user can make an informed choice.
4942 ws_warning("Obsolete preference \"%s\" at line %d of\n%s %s",
4943 cur_var->str, pline, pf_path, hint);
4944 prefs.unknown_prefs = true;
4945 break;
4947 } else {
4948 ws_warning("Incomplete preference at line %d: of\n%s %s", pline, pf_path, hint);
4951 state = IN_VAR;
4952 got_val = false;
4953 g_string_truncate(cur_var, 0);
4954 g_string_append_c(cur_var, (char) got_c);
4955 pline = fline;
4956 } else if (g_ascii_isspace(got_c) && cur_var->len > 0 && got_val) {
4957 state = PRE_VAL;
4958 } else if (got_c == '#') {
4959 state = IN_SKIP;
4960 } else {
4961 ws_warning("Malformed preference at line %d of\n%s %s", fline, pf_path, hint);
4963 break;
4964 case IN_VAR:
4965 if (got_c != ':') {
4966 g_string_append_c(cur_var, (char) got_c);
4967 } else {
4968 /* This is a colon (':') */
4969 state = PRE_VAL;
4970 g_string_truncate(cur_val, 0);
4972 * Set got_val to true to accommodate prefs such as
4973 * "gui.fileopen.dir" that do not require a value.
4975 got_val = true;
4977 break;
4978 case PRE_VAL:
4979 if (!g_ascii_isspace(got_c)) {
4980 state = IN_VAL;
4981 g_string_append_c(cur_val, (char) got_c);
4983 break;
4984 case IN_VAL:
4985 g_string_append_c(cur_val, (char) got_c);
4986 break;
4987 case IN_SKIP:
4988 break;
4991 if (cur_var->len > 0) {
4992 if (got_val) {
4993 /* Call the routine to set the preference; it will parse
4994 the value as appropriate.
4996 Since we're reading a file, rather than processing
4997 explicit user input, for range preferences, silently
4998 lower values in excess of the range's maximum, rather
4999 than reporting errors and failing. */
5000 switch (pref_set_pair_fct(cur_var->str, cur_val->str, private_data, false)) {
5002 case PREFS_SET_OK:
5003 break;
5005 case PREFS_SET_SYNTAX_ERR:
5006 ws_warning("Syntax error in preference %s at line %d of\n%s %s",
5007 cur_var->str, pline, pf_path, hint);
5008 break;
5010 case PREFS_SET_NO_SUCH_PREF:
5011 ws_warning("No such preference \"%s\" at line %d of\n%s %s",
5012 cur_var->str, pline, pf_path, hint);
5013 prefs.unknown_prefs = true;
5014 break;
5016 case PREFS_SET_OBSOLETE:
5017 prefs.unknown_prefs = true;
5018 break;
5020 } else {
5021 ws_warning("Incomplete preference at line %d of\n%s %s",
5022 pline, pf_path, hint);
5026 g_string_free(cur_val, TRUE);
5027 g_string_free(cur_var, TRUE);
5029 if (ferror(pf))
5030 return errno;
5031 else
5032 return 0;
5036 * If we were handed a preference starting with "uat:", try to turn it into
5037 * a valid uat entry.
5039 static bool
5040 prefs_set_uat_pref(char *uat_entry, char **errmsg) {
5041 char *p, *colonp;
5042 uat_t *uat;
5043 bool ret;
5045 colonp = strchr(uat_entry, ':');
5046 if (colonp == NULL)
5047 return false;
5049 p = colonp;
5050 *p++ = '\0';
5053 * Skip over any white space (there probably won't be any, but
5054 * as we allow it in the preferences file, we might as well
5055 * allow it here).
5057 while (g_ascii_isspace(*p))
5058 p++;
5059 if (*p == '\0') {
5061 * Put the colon back, so if our caller uses, in an
5062 * error message, the string they passed us, the message
5063 * looks correct.
5065 *colonp = ':';
5066 return false;
5069 uat = uat_find(uat_entry);
5070 *colonp = ':';
5071 if (uat == NULL) {
5072 *errmsg = g_strdup("Unknown preference");
5073 return false;
5076 ret = uat_load_str(uat, p, errmsg);
5077 return ret;
5081 * Given a string of the form "<pref name>:<pref value>", as might appear
5082 * as an argument to a "-o" option, parse it and set the preference in
5083 * question. Return an indication of whether it succeeded or failed
5084 * in some fashion.
5086 prefs_set_pref_e
5087 prefs_set_pref(char *prefarg, char **errmsg)
5089 char *p, *colonp;
5090 prefs_set_pref_e ret;
5093 * Set the counters of "mgcp.{tcp,udp}.port" entries we've
5094 * seen to values that keep us from trying to interpret them
5095 * as "mgcp.{tcp,udp}.gateway_port" or "mgcp.{tcp,udp}.callagent_port",
5096 * as, from the command line, we have no way of guessing which
5097 * the user had in mind.
5099 mgcp_tcp_port_count = -1;
5100 mgcp_udp_port_count = -1;
5102 *errmsg = NULL;
5104 colonp = strchr(prefarg, ':');
5105 if (colonp == NULL)
5106 return PREFS_SET_SYNTAX_ERR;
5108 p = colonp;
5109 *p++ = '\0';
5112 * Skip over any white space (there probably won't be any, but
5113 * as we allow it in the preferences file, we might as well
5114 * allow it here).
5116 while (g_ascii_isspace(*p))
5117 p++;
5118 /* The empty string is a legal value for range preferences (PREF_RANGE,
5119 * PREF_DECODE_AS_RANGE), and string-like preferences (PREF_STRING,
5120 * PREF_SAVE_FILENAME, PREF_OPEN_FILENAME, PREF_DIRNAME), indeed often
5121 * not just useful but the default. A user might have a value saved
5122 * to their preference file but want to override it to default behavior.
5123 * Individual preference handlers of those types should be prepared to
5124 * deal with an empty string. For other types, it is up to set_pref() to
5125 * test for the empty string and set PREFS_SET_SYNTAX_ERROR there.
5127 if (strcmp(prefarg, "uat")) {
5128 ret = set_pref(prefarg, p, NULL, true);
5129 } else {
5130 ret = prefs_set_uat_pref(p, errmsg) ? PREFS_SET_OK : PREFS_SET_SYNTAX_ERR;
5132 *colonp = ':'; /* put the colon back */
5133 return ret;
5136 unsigned prefs_get_uint_value(pref_t *pref, pref_source_t source)
5138 switch (source)
5140 case pref_default:
5141 return pref->default_val.uint;
5142 case pref_stashed:
5143 return pref->stashed_val.uint;
5144 case pref_current:
5145 return *pref->varp.uint;
5146 default:
5147 ws_assert_not_reached();
5148 break;
5151 return 0;
5154 char* prefs_get_password_value(pref_t *pref, pref_source_t source)
5156 return prefs_get_string_value(pref, source);
5160 unsigned int prefs_set_uint_value(pref_t *pref, unsigned value, pref_source_t source)
5162 unsigned int changed = 0;
5163 switch (source)
5165 case pref_default:
5166 if (pref->default_val.uint != value) {
5167 pref->default_val.uint = value;
5168 changed = prefs_get_effect_flags(pref);
5170 break;
5171 case pref_stashed:
5172 if (pref->stashed_val.uint != value) {
5173 pref->stashed_val.uint = value;
5174 changed = prefs_get_effect_flags(pref);
5176 break;
5177 case pref_current:
5178 if (*pref->varp.uint != value) {
5179 *pref->varp.uint = value;
5180 changed = prefs_get_effect_flags(pref);
5182 break;
5183 default:
5184 ws_assert_not_reached();
5185 break;
5188 return changed;
5192 * For use by UI code that sets preferences.
5194 unsigned int
5195 prefs_set_password_value(pref_t *pref, const char* value, pref_source_t source)
5197 return prefs_set_string_value(pref, value, source);
5201 unsigned prefs_get_uint_base(pref_t *pref)
5203 return pref->info.base;
5207 * Returns true if the given device is hidden
5209 bool
5210 prefs_is_capture_device_hidden(const char *name)
5212 char *tok, *devices;
5213 size_t len;
5215 if (prefs.capture_devices_hide && name) {
5216 devices = g_strdup (prefs.capture_devices_hide);
5217 len = strlen (name);
5218 for (tok = strtok (devices, ","); tok; tok = strtok(NULL, ",")) {
5219 if (strlen (tok) == len && strcmp (name, tok) == 0) {
5220 g_free (devices);
5221 return true;
5224 g_free (devices);
5227 return false;
5231 * Returns true if the given column is visible (not hidden)
5233 static bool
5234 prefs_is_column_visible(const char *cols_hidden, int col)
5236 char *tok, *cols, *p;
5237 int cidx;
5240 * Do we have a list of hidden columns?
5242 if (cols_hidden) {
5244 * Yes - check the column against each of the ones in the
5245 * list.
5247 cols = g_strdup(cols_hidden);
5248 for (tok = strtok(cols, ","); tok; tok = strtok(NULL, ",")) {
5249 tok = g_strstrip(tok);
5251 cidx = (int)strtol(tok, &p, 10);
5252 if (p == tok || *p != '\0') {
5253 continue;
5255 if (cidx != col) {
5256 continue;
5259 * OK, they match, so it's one of the hidden fields,
5260 * hence not visible.
5262 g_free(cols);
5263 return false;
5265 g_free(cols);
5269 * No - either there are no hidden columns or this isn't one
5270 * of them - so it is visible.
5272 return true;
5276 * Returns true if the given column is visible (not hidden)
5278 static bool
5279 prefs_is_column_fmt_visible(const char *cols_hidden, fmt_data *cfmt)
5281 char *tok, *cols;
5282 fmt_data cfmt_hidden;
5285 * Do we have a list of hidden columns?
5287 if (cols_hidden) {
5289 * Yes - check the column against each of the ones in the
5290 * list.
5292 cols = g_strdup(cols_hidden);
5293 for (tok = strtok(cols, ","); tok; tok = strtok(NULL, ",")) {
5294 tok = g_strstrip(tok);
5297 * Parse this column format.
5299 if (!parse_column_format(&cfmt_hidden, tok)) {
5301 * It's not valid; ignore it.
5303 continue;
5307 * Does it match the column?
5309 if (cfmt->fmt != cfmt_hidden.fmt) {
5310 /* No. */
5311 g_free(cfmt_hidden.custom_fields);
5312 cfmt_hidden.custom_fields = NULL;
5313 continue;
5315 if (cfmt->fmt == COL_CUSTOM) {
5317 * A custom column has to have the same custom field
5318 * and occurrence.
5320 if (cfmt_hidden.custom_fields && cfmt->custom_fields) {
5321 if (strcmp(cfmt->custom_fields,
5322 cfmt_hidden.custom_fields) != 0) {
5323 /* Different fields. */
5324 g_free(cfmt_hidden.custom_fields);
5325 cfmt_hidden.custom_fields = NULL;
5326 continue;
5328 if (cfmt->custom_occurrence != cfmt_hidden.custom_occurrence) {
5329 /* Different occurrences settings. */
5330 g_free(cfmt_hidden.custom_fields);
5331 cfmt_hidden.custom_fields = NULL;
5332 continue;
5338 * OK, they match, so it's one of the hidden fields,
5339 * hence not visible.
5341 g_free(cfmt_hidden.custom_fields);
5342 g_free(cols);
5343 return false;
5345 g_free(cols);
5349 * No - either there are no hidden columns or this isn't one
5350 * of them - so it is visible.
5352 return true;
5356 * Returns true if the given device should capture in monitor mode by default
5358 bool
5359 prefs_capture_device_monitor_mode(const char *name)
5361 char *tok, *devices;
5362 size_t len;
5364 if (prefs.capture_devices_monitor_mode && name) {
5365 devices = g_strdup (prefs.capture_devices_monitor_mode);
5366 len = strlen (name);
5367 for (tok = strtok (devices, ","); tok; tok = strtok(NULL, ",")) {
5368 if (strlen (tok) == len && strcmp (name, tok) == 0) {
5369 g_free (devices);
5370 return true;
5373 g_free (devices);
5376 return false;
5380 * Returns true if the user has marked this column as visible
5382 bool
5383 prefs_capture_options_dialog_column_is_visible(const char *column)
5385 GList *curr;
5386 char *col;
5388 for (curr = g_list_first(prefs.capture_columns); curr; curr = g_list_next(curr)) {
5389 col = (char *)curr->data;
5390 if (col && (g_ascii_strcasecmp(col, column) == 0)) {
5391 return true;
5394 return false;
5397 bool
5398 prefs_has_layout_pane_content (layout_pane_content_e layout_pane_content)
5400 return ((prefs.gui_layout_content_1 == layout_pane_content) ||
5401 (prefs.gui_layout_content_2 == layout_pane_content) ||
5402 (prefs.gui_layout_content_3 == layout_pane_content));
5405 #define PRS_GUI_FILTER_LABEL "gui.filter_expressions.label"
5406 #define PRS_GUI_FILTER_EXPR "gui.filter_expressions.expr"
5407 #define PRS_GUI_FILTER_ENABLED "gui.filter_expressions.enabled"
5410 * Extract the red, green, and blue components of a 24-bit RGB value
5411 * and convert them from [0,255] to [0,65535].
5413 #define RED_COMPONENT(x) (uint16_t) (((((x) >> 16) & 0xff) * 65535 / 255))
5414 #define GREEN_COMPONENT(x) (uint16_t) (((((x) >> 8) & 0xff) * 65535 / 255))
5415 #define BLUE_COMPONENT(x) (uint16_t) ( (((x) & 0xff) * 65535 / 255))
5417 char
5418 string_to_name_resolve(const char *string, e_addr_resolve *name_resolve)
5420 char c;
5422 memset(name_resolve, 0, sizeof(e_addr_resolve));
5423 while ((c = *string++) != '\0') {
5424 switch (c) {
5425 case 'g':
5426 name_resolve->maxmind_geoip = true;
5427 break;
5428 case 'm':
5429 name_resolve->mac_name = true;
5430 break;
5431 case 'n':
5432 name_resolve->network_name = true;
5433 break;
5434 case 'N':
5435 name_resolve->use_external_net_name_resolver = true;
5436 break;
5437 case 't':
5438 name_resolve->transport_name = true;
5439 break;
5440 case 'd':
5441 name_resolve->dns_pkt_addr_resolution = true;
5442 break;
5443 case 's':
5444 name_resolve->handshake_sni_addr_resolution = true;
5445 break;
5446 case 'v':
5447 name_resolve->vlan_name = true;
5448 break;
5449 default:
5451 * Unrecognized letter.
5453 return c;
5456 return '\0';
5459 static bool
5460 deprecated_heur_dissector_pref(char *pref_name, const char *value)
5462 struct heur_pref_name
5464 const char* pref_name;
5465 const char* short_name;
5466 bool more_dissectors; /* For multiple dissectors controlled by the same preference */
5469 struct heur_pref_name heur_prefs[] = {
5470 {"acn.heuristic_acn", "acn_udp", 0},
5471 {"bfcp.enable", "bfcp_tcp", 1},
5472 {"bfcp.enable", "bfcp_udp", 0},
5473 {"bt-dht.enable", "bittorrent_dht_udp", 0},
5474 {"bt-utp.enable", "bt_utp_udp", 0},
5475 {"cattp.enable", "cattp_udp", 0},
5476 {"cfp.enable", "fp_eth", 0},
5477 {"dicom.heuristic", "dicom_tcp", 0},
5478 {"dnp3.heuristics", "dnp3_tcp", 1},
5479 {"dnp3.heuristics", "dnp3_udp", 0},
5480 {"dvb-s2_modeadapt.enable", "dvb_s2_udp", 0},
5481 {"esl.enable", "esl_eth", 0},
5482 {"fp.udp_heur", "fp_udp", 0},
5483 {"gvsp.enable_heuristic", "gvsp_udp", 0},
5484 {"hdcp2.enable", "hdcp2_tcp", 0},
5485 {"hislip.enable_heuristic", "hislip_tcp", 0},
5486 {"infiniband.dissect_eoib", "mellanox_eoib", 1},
5487 {"infiniband.identify_payload", "eth_over_ib", 0},
5488 {"jxta.udp.heuristic", "jxta_udp", 0},
5489 {"jxta.tcp.heuristic", "jxta_tcp", 0},
5490 {"jxta.sctp.heuristic", "jxta_sctp", 0},
5491 {"mac-lte.heuristic_mac_lte_over_udp", "mac_lte_udp", 0},
5492 {"mbim.bulk_heuristic", "mbim_usb_bulk", 0},
5493 {"norm.heuristic_norm", "rmt_norm_udp", 0},
5494 {"openflow.heuristic", "openflow_tcp", 0},
5495 {"pdcp-lte.heuristic_pdcp_lte_over_udp", "pdcp_lte_udp", 0},
5496 {"rlc.heuristic_rlc_over_udp", "rlc_udp", 0},
5497 {"rlc-lte.heuristic_rlc_lte_over_udp", "rlc_lte_udp", 0},
5498 {"rtcp.heuristic_rtcp", "rtcp_udp", 1},
5499 {"rtcp.heuristic_rtcp", "rtcp_stun", 0},
5500 {"rtp.heuristic_rtp", "rtp_udp", 1},
5501 {"rtp.heuristic_rtp", "rtp_stun", 0},
5502 {"teredo.heuristic_teredo", "teredo_udp", 0},
5503 {"vssmonitoring.use_heuristics", "vssmonitoring_eth", 0},
5504 {"xml.heuristic", "xml_http", 1},
5505 {"xml.heuristic", "xml_sip", 1},
5506 {"xml.heuristic", "xml_media", 0},
5507 {"xml.heuristic_tcp", "xml_tcp", 0},
5508 {"xml.heuristic_udp", "xml_udp", 0},
5511 unsigned int i;
5512 heur_dtbl_entry_t* heuristic;
5515 for (i = 0; i < array_length(heur_prefs); i++)
5517 if (strcmp(pref_name, heur_prefs[i].pref_name) == 0)
5519 heuristic = find_heur_dissector_by_unique_short_name(heur_prefs[i].short_name);
5520 if (heuristic != NULL) {
5521 heuristic->enabled = ((g_ascii_strcasecmp(value, "true") == 0) ? true : false);
5524 if (!heur_prefs[i].more_dissectors)
5525 return true;
5530 return false;
5533 static bool
5534 deprecated_enable_dissector_pref(char *pref_name, const char *value)
5536 struct dissector_pref_name
5538 const char* pref_name;
5539 const char* short_name;
5542 struct dissector_pref_name dissector_prefs[] = {
5543 {"transum.tsumenabled", "TRANSUM"},
5544 {"snort.enable_snort_dissector", "Snort"},
5545 {"prp.enable", "PRP"},
5548 unsigned int i;
5549 int proto_id;
5551 for (i = 0; i < array_length(dissector_prefs); i++)
5553 if (strcmp(pref_name, dissector_prefs[i].pref_name) == 0)
5555 proto_id = proto_get_id_by_short_name(dissector_prefs[i].short_name);
5556 if (proto_id >= 0)
5557 proto_set_decoding(proto_id, ((g_ascii_strcasecmp(value, "true") == 0) ? true : false));
5558 return true;
5562 return false;
5565 static bool
5566 deprecated_port_pref(char *pref_name, const char *value)
5568 struct port_pref_name
5570 const char* pref_name;
5571 const char* module_name; /* the protocol filter name */
5572 const char* table_name;
5573 unsigned base;
5576 struct obsolete_pref_name
5578 const char* pref_name;
5581 /* For now this is only supporting TCP/UDP port and RTP payload
5582 * types dissector preferences, which are assumed to be decimal */
5583 /* module_name is the filter name of the destination port preference,
5584 * which is usually the same as the original module but not
5585 * necessarily (e.g., if the preference is for what is now a PINO.)
5586 * XXX: Most of these were changed pre-2.0. Can we end support
5587 * for migrating legacy preferences at some point?
5589 struct port_pref_name port_prefs[] = {
5590 /* TCP */
5591 {"cmp.tcp_alternate_port", "cmp", "tcp.port", 10},
5592 {"h248.tcp_port", "h248", "tcp.port", 10},
5593 {"cops.tcp.cops_port", "cops", "tcp.port", 10},
5594 {"dhcpfo.tcp_port", "dhcpfo", "tcp.port", 10},
5595 {"enttec.tcp_port", "enttec", "tcp.port", 10},
5596 {"forces.tcp_alternate_port", "forces", "tcp.port", 10},
5597 {"ged125.tcp_port", "ged125", "tcp.port", 10},
5598 {"hpfeeds.dissector_port", "hpfeeds", "tcp.port", 10},
5599 {"lsc.port", "lsc", "tcp.port", 10},
5600 {"megaco.tcp.txt_port", "megaco", "tcp.port", 10},
5601 {"netsync.tcp_port", "netsync", "tcp.port", 10},
5602 {"osi.tpkt_port", "osi", "tcp.port", 10},
5603 {"rsync.tcp_port", "rsync", "tcp.port", 10},
5604 {"sametime.tcp_port", "sametime", "tcp.port", 10},
5605 {"sigcomp.tcp.port2", "sigcomp", "tcp.port", 10},
5606 {"synphasor.tcp_port", "synphasor", "tcp.port", 10},
5607 {"tipc.alternate_port", "tipc", "tcp.port", 10},
5608 {"vnc.alternate_port", "vnc", "tcp.port", 10},
5609 {"scop.port", "scop", "tcp.port", 10},
5610 {"scop.port_secure", "scop", "tcp.port", 10},
5611 {"tpncp.tcp.trunkpack_port", "tpncp", "tcp.port", 10},
5612 /* UDP */
5613 {"h248.udp_port", "h248", "udp.port", 10},
5614 {"actrace.udp_port", "actrace", "udp.port", 10},
5615 {"brp.port", "brp", "udp.port", 10},
5616 {"bvlc.additional_udp_port", "bvlc", "udp.port", 10},
5617 {"capwap.udp.port.control", "capwap", "udp.port", 10},
5618 {"capwap.udp.port.data", "capwap", "udp.port", 10},
5619 {"coap.udp_port", "coap", "udp.port", 10},
5620 {"enttec.udp_port", "enttec", "udp.port", 10},
5621 {"forces.udp_alternate_port", "forces", "udp.port", 10},
5622 {"ldss.udp_port", "ldss", "udp.port", 10},
5623 {"lmp.udp_port", "lmp", "udp.port", 10},
5624 {"ltp.port", "ltp", "udp.port", 10},
5625 {"lwres.udp.lwres_port", "lwres", "udp.port", 10},
5626 {"megaco.udp.txt_port", "megaco", "udp.port", 10},
5627 {"pfcp.port_pfcp", "pfcp", "udp.port", 10},
5628 {"pgm.udp.encap_ucast_port", "pgm", "udp.port", 10},
5629 {"pgm.udp.encap_mcast_port", "pgm", "udp.port", 10},
5630 {"quic.udp.quic.port", "quic", "udp.port", 10},
5631 {"quic.udp.quics.port", "quic", "udp.port", 10},
5632 {"radius.alternate_port", "radius", "udp.port", 10},
5633 {"rdt.default_udp_port", "rdt", "udp.port", 10},
5634 {"alc.default.udp_port", "alc", "udp.port", 10},
5635 {"sigcomp.udp.port2", "sigcomp", "udp.port", 10},
5636 {"synphasor.udp_port", "synphasor", "udp.port", 10},
5637 {"tdmop.udpport", "tdmop", "udp.port", 10},
5638 {"uaudp.port1", "uaudp", "udp.port", 10},
5639 {"uaudp.port2", "uaudp", "udp.port", 10},
5640 {"uaudp.port3", "uaudp", "udp.port", 10},
5641 {"uaudp.port4", "uaudp", "udp.port", 10},
5642 {"uhd.dissector_port", "uhd", "udp.port", 10},
5643 {"vrt.dissector_port", "vrt", "udp.port", 10},
5644 {"tpncp.udp.trunkpack_port", "tpncp", "udp.port", 10},
5645 /* SCTP */
5646 {"hnbap.port", "hnbap", "sctp.port", 10},
5647 {"m2pa.port", "m2pa", "sctp.port", 10},
5648 {"megaco.sctp.txt_port", "megaco", "sctp.port", 10},
5649 {"rua.port", "rua", "sctp.port", 10},
5650 /* SCTP PPI */
5651 {"lapd.sctp_payload_protocol_identifier", "lapd", "sctp.ppi", 10},
5652 /* SCCP SSN */
5653 {"ranap.sccp_ssn", "ranap", "sccp.ssn", 10},
5656 struct port_pref_name port_range_prefs[] = {
5657 /* TCP */
5658 {"couchbase.tcp.ports", "couchbase", "tcp.port", 10},
5659 {"gsm_ipa.tcp_ports", "gsm_ipa", "tcp.port", 10},
5660 {"kafka.tcp.ports", "kafka", "tcp.port", 10},
5661 {"kt.tcp.ports", "kt", "tcp.port", 10},
5662 {"memcache.tcp.ports", "memcache", "tcp.port", 10},
5663 {"mrcpv2.tcp.port_range", "mrcpv2", "tcp.port", 10},
5664 {"pdu_transport.ports.tcp", "pdu_transport", "tcp.port", 10},
5665 {"rtsp.tcp.port_range", "rtsp", "tcp.port", 10},
5666 {"sip.tcp.ports", "sip", "tcp.port", 10},
5667 {"someip.ports.tcp", "someip", "tcp.port", 10},
5668 {"tds.tcp_ports", "tds", "tcp.port", 10},
5669 {"tpkt.tcp.ports", "tpkt", "tcp.port", 10},
5670 {"uma.tcp.ports", "uma", "tcp.port", 10},
5671 /* UDP */
5672 {"aruba_erm.udp.ports", "arubs_erm", "udp.port", 10},
5673 {"diameter.udp.ports", "diameter", "udp.port", 10},
5674 {"dmp.udp_ports", "dmp", "udp.port", 10},
5675 {"dns.udp.ports", "dns", "udp.port", 10},
5676 {"gsm_ipa.udp_ports", "gsm_ipa", "udp.port", 10},
5677 {"hcrt.dissector_udp_port", "hcrt", "udp.port", 10},
5678 {"memcache.udp.ports", "memcache", "udp.port", 10},
5679 {"nb_rtpmux.udp_ports", "nb_rtpmux", "udp.port", 10},
5680 {"gprs-ns.udp.ports", "gprs-ns", "udp.port", 10},
5681 {"p_mul.udp_ports", "p_mul", "udp.port", 10},
5682 {"pdu_transport.ports.udp", "pdu_transport", "udp.port", 10},
5683 {"radius.ports", "radius", "udp.port", 10},
5684 {"sflow.ports", "sflow", "udp.port", 10},
5685 {"someip.ports.udp", "someip", "udp.port", 10},
5686 {"sscop.udp.ports", "sscop", "udp.port", 10},
5687 {"tftp.udp_ports", "tftp", "udp.port", 10},
5688 {"tipc.udp.ports", "tipc", "udp.port", 10},
5689 /* RTP */
5690 {"amr.dynamic.payload.type", "amr", "rtp.pt", 10},
5691 {"amr.wb.dynamic.payload.type", "amr_wb", "rtp.pt", 10},
5692 {"dvb-s2_modeadapt.dynamic.payload.type", "dvb-s2_modeadapt", "rtp.pt", 10},
5693 {"evs.dynamic.payload.type", "evs", "rtp.pt", 10},
5694 {"h263p.dynamic.payload.type", "h263p", "rtp.pt", 10},
5695 {"h264.dynamic.payload.type", "h264", "rtp.pt", 10},
5696 {"h265.dynamic.payload.type", "h265", "rtp.pt", 10},
5697 {"ismacryp.dynamic.payload.type", "ismacryp", "rtp.pt", 10},
5698 {"iuup.dynamic.payload.type", "iuup", "rtp.pt", 10},
5699 {"lapd.rtp_payload_type", "lapd", "rtp.pt", 10},
5700 {"mp4ves.dynamic.payload.type", "mp4ves", "rtp.pt", 10},
5701 {"mtp2.rtp_payload_type", "mtp2", "rtp.pt", 10},
5702 {"opus.dynamic.payload.type", "opus", "rtp.pt", 10},
5703 {"rtp.rfc2198_payload_type", "rtp_rfc2198", "rtp.pt", 10},
5704 {"rtpevent.event_payload_type_value", "rtpevent", "rtp.pt", 10},
5705 {"rtpevent.cisco_nse_payload_type_value", "rtpevent", "rtp.pt", 10},
5706 {"rtpmidi.midi_payload_type_value", "rtpmidi", "rtp.pt", 10},
5707 {"vp8.dynamic.payload.type", "vp8", "rtp.pt", 10},
5708 /* SCTP */
5709 {"diameter.sctp.ports", "diameter", "sctp.port", 10},
5710 {"sgsap.sctp_ports", "sgsap", "sctp.port", 10},
5711 /* SCCP SSN */
5712 {"pcap.ssn", "pcap", "sccp.ssn", 10},
5715 /* These are subdissectors of TPKT/OSITP that used to have a
5716 TCP port preference even though they were never
5717 directly on TCP. Convert them to use Decode As
5718 with the TPKT dissector handle */
5719 struct port_pref_name tpkt_subdissector_port_prefs[] = {
5720 {"dap.tcp.port", "dap", "tcp.port", 10},
5721 {"disp.tcp.port", "disp", "tcp.port", 10},
5722 {"dop.tcp.port", "dop", "tcp.port", 10},
5723 {"dsp.tcp.port", "dsp", "tcp.port", 10},
5724 {"p1.tcp.port", "p1", "tcp.port", 10},
5725 {"p7.tcp.port", "p7", "tcp.port", 10},
5726 {"rdp.tcp.port", "rdp", "tcp.port", 10},
5729 /* These are obsolete preferences from the dissectors' view,
5730 (typically because of a switch from a single value to a
5731 range value) but the name of the preference conflicts
5732 with the generated preference name from the dissector table.
5733 Don't allow the obsolete preference through to be handled */
5734 struct obsolete_pref_name obsolete_prefs[] = {
5735 {"diameter.tcp.port"},
5736 {"kafka.tcp.port"},
5737 {"mrcpv2.tcp.port"},
5738 {"rtsp.tcp.port"},
5739 {"sip.tcp.port"},
5740 {"t38.tcp.port"},
5743 unsigned int i;
5744 unsigned uval;
5745 dissector_table_t sub_dissectors;
5746 dissector_handle_t handle, tpkt_handle;
5747 module_t *module;
5748 pref_t *pref;
5750 static bool sanity_checked;
5751 if (!sanity_checked) {
5752 sanity_checked = true;
5753 for (i = 0; i < G_N_ELEMENTS(port_prefs); i++) {
5754 module = prefs_find_module(port_prefs[i].module_name);
5755 if (!module) {
5756 ws_warning("Deprecated ports pref check - module '%s' not found", port_prefs[i].module_name);
5757 continue;
5759 pref = prefs_find_preference(module, port_prefs[i].table_name);
5760 if (!pref) {
5761 ws_warning("Deprecated ports pref '%s.%s' not found", module->name, port_prefs[i].table_name);
5762 continue;
5764 if (pref->type != PREF_DECODE_AS_RANGE) {
5765 ws_warning("Deprecated ports pref '%s.%s' has wrong type: %#x (%s)", module->name, port_prefs[i].table_name, pref->type, prefs_pref_type_name(pref));
5770 for (i = 0; i < G_N_ELEMENTS(port_prefs); i++) {
5771 if (strcmp(pref_name, port_prefs[i].pref_name) == 0) {
5772 if (!ws_basestrtou32(value, NULL, &uval, port_prefs[i].base))
5773 return false; /* number was bad */
5775 module = prefs_find_module(port_prefs[i].module_name);
5776 pref = prefs_find_preference(module, port_prefs[i].table_name);
5777 if (pref != NULL) {
5778 module->prefs_changed_flags |= prefs_get_effect_flags(pref);
5779 if (pref->type == PREF_DECODE_AS_RANGE) {
5780 // The legacy preference was a port number, but the new
5781 // preference is a port range. Add to existing range.
5782 if (uval) {
5783 prefs_range_add_value(pref, uval);
5788 /* If the value is zero, it wouldn't add to the Decode As tables */
5789 if (uval != 0)
5791 sub_dissectors = find_dissector_table(port_prefs[i].table_name);
5792 if (sub_dissectors != NULL) {
5793 handle = dissector_table_get_dissector_handle(sub_dissectors, module->title);
5794 if (handle != NULL) {
5795 dissector_change_uint(port_prefs[i].table_name, uval, handle);
5796 decode_build_reset_list(port_prefs[i].table_name, dissector_table_get_type(sub_dissectors), GUINT_TO_POINTER(uval), NULL, NULL);
5801 return true;
5805 for (i = 0; i < array_length(port_range_prefs); i++)
5807 if (strcmp(pref_name, port_range_prefs[i].pref_name) == 0)
5809 uint32_t range_i, range_j;
5811 sub_dissectors = find_dissector_table(port_range_prefs[i].table_name);
5812 if (sub_dissectors != NULL) {
5813 switch (dissector_table_get_type(sub_dissectors)) {
5814 case FT_UINT8:
5815 case FT_UINT16:
5816 case FT_UINT24:
5817 case FT_UINT32:
5818 break;
5820 default:
5821 ws_error("The dissector table %s (%s) is not an integer type - are you using a buggy plugin?", port_range_prefs[i].table_name, get_dissector_table_ui_name(port_range_prefs[i].table_name));
5822 ws_assert_not_reached();
5825 module = prefs_find_module(port_range_prefs[i].module_name);
5826 pref = prefs_find_preference(module, port_range_prefs[i].table_name);
5827 if (pref != NULL)
5829 if (!prefs_set_range_value_work(pref, value, true, &module->prefs_changed_flags))
5831 return false; /* number was bad */
5834 handle = dissector_table_get_dissector_handle(sub_dissectors, module->title);
5835 if (handle != NULL) {
5837 for (range_i = 0; range_i < (*pref->varp.range)->nranges; range_i++) {
5838 for (range_j = (*pref->varp.range)->ranges[range_i].low; range_j < (*pref->varp.range)->ranges[range_i].high; range_j++) {
5839 dissector_change_uint(port_range_prefs[i].table_name, range_j, handle);
5840 decode_build_reset_list(port_range_prefs[i].table_name, dissector_table_get_type(sub_dissectors), GUINT_TO_POINTER(range_j), NULL, NULL);
5843 dissector_change_uint(port_range_prefs[i].table_name, (*pref->varp.range)->ranges[range_i].high, handle);
5844 decode_build_reset_list(port_range_prefs[i].table_name, dissector_table_get_type(sub_dissectors), GUINT_TO_POINTER((*pref->varp.range)->ranges[range_i].high), NULL, NULL);
5850 return true;
5854 for (i = 0; i < array_length(tpkt_subdissector_port_prefs); i++)
5856 if (strcmp(pref_name, tpkt_subdissector_port_prefs[i].pref_name) == 0)
5858 /* XXX - give an error if it doesn't fit in a unsigned? */
5859 if (!ws_basestrtou32(value, NULL, &uval, tpkt_subdissector_port_prefs[i].base))
5860 return false; /* number was bad */
5862 /* If the value is 0 or 102 (default TPKT port), don't add to the Decode As tables */
5863 if ((uval != 0) && (uval != 102))
5865 tpkt_handle = find_dissector("tpkt");
5866 if (tpkt_handle != NULL) {
5867 dissector_change_uint(tpkt_subdissector_port_prefs[i].table_name, uval, tpkt_handle);
5871 return true;
5875 for (i = 0; i < array_length(obsolete_prefs); i++)
5877 if (strcmp(pref_name, obsolete_prefs[i].pref_name) == 0)
5879 /* Just ignore the preference */
5880 return true;
5883 return false;
5886 static prefs_set_pref_e
5887 set_pref(char *pref_name, const char *value, void *private_data,
5888 bool return_range_errors)
5890 unsigned cval;
5891 unsigned uval;
5892 bool bval;
5893 int enum_val;
5894 char *dotp, *last_dotp;
5895 static char *filter_label = NULL;
5896 static bool filter_enabled = false;
5897 module_t *module, *containing_module, *target_module;
5898 pref_t *pref;
5899 int type;
5900 bool converted_pref = false;
5902 target_module = (module_t*)private_data;
5904 //The PRS_GUI field names are here for backwards compatibility
5905 //display filters have been converted to a UAT.
5906 if (strcmp(pref_name, PRS_GUI_FILTER_LABEL) == 0) {
5907 /* Assume that PRS_GUI_FILTER_EXPR follows this preference. In case of
5908 * malicious preference files, free the previous value to limit the size
5909 * of leaked memory. */
5910 g_free(filter_label);
5911 filter_label = g_strdup(value);
5912 } else if (strcmp(pref_name, PRS_GUI_FILTER_ENABLED) == 0) {
5913 filter_enabled = (strcmp(value, "TRUE") == 0) ? true : false;
5914 } else if (strcmp(pref_name, PRS_GUI_FILTER_EXPR) == 0) {
5915 /* Comments not supported for "old" preference style */
5916 filter_expression_new(filter_label, value, "", filter_enabled);
5917 g_free(filter_label);
5918 filter_label = NULL;
5919 /* Remember to save the new UAT to file. */
5920 prefs.filter_expressions_old = true;
5921 } else if (strcmp(pref_name, "gui.version_in_start_page") == 0) {
5922 /* Convert deprecated value to closest current equivalent */
5923 if (g_ascii_strcasecmp(value, "true") == 0) {
5924 prefs.gui_version_placement = version_both;
5925 } else {
5926 prefs.gui_version_placement = version_neither;
5928 } else if (strcmp(pref_name, "name_resolve") == 0 ||
5929 strcmp(pref_name, "capture.name_resolve") == 0) {
5931 * Handle the deprecated name resolution options.
5933 * "TRUE" and "FALSE", for backwards compatibility, are synonyms for
5934 * RESOLV_ALL and RESOLV_NONE.
5936 * Otherwise, we treat it as a list of name types we want to resolve.
5938 if (g_ascii_strcasecmp(value, "true") == 0) {
5939 gbl_resolv_flags.mac_name = true;
5940 gbl_resolv_flags.network_name = true;
5941 gbl_resolv_flags.transport_name = true;
5943 else if (g_ascii_strcasecmp(value, "false") == 0) {
5944 disable_name_resolution();
5946 else {
5947 /* start out with none set */
5948 disable_name_resolution();
5949 if (string_to_name_resolve(value, &gbl_resolv_flags) != '\0')
5950 return PREFS_SET_SYNTAX_ERR;
5952 } else if (deprecated_heur_dissector_pref(pref_name, value)) {
5953 /* Handled within deprecated_heur_dissector_pref() if found */
5954 } else if (deprecated_enable_dissector_pref(pref_name, value)) {
5955 /* Handled within deprecated_enable_dissector_pref() if found */
5956 } else if (deprecated_port_pref(pref_name, value)) {
5957 /* Handled within deprecated_port_pref() if found */
5958 } else if (strcmp(pref_name, "console.log.level") == 0) {
5959 /* Handled on the command line within ws_log_parse_args() */
5960 return PREFS_SET_OK;
5961 } else {
5962 /* Handle deprecated "global" options that don't have a module
5963 * associated with them
5965 if ((strcmp(pref_name, "name_resolve_concurrency") == 0) ||
5966 (strcmp(pref_name, "name_resolve_load_smi_modules") == 0) ||
5967 (strcmp(pref_name, "name_resolve_suppress_smi_errors") == 0)) {
5968 module = nameres_module;
5969 dotp = pref_name;
5970 } else {
5971 /* To which module does this preference belong? */
5972 module = NULL;
5973 last_dotp = pref_name;
5974 while (!module) {
5975 dotp = strchr(last_dotp, '.');
5976 if (dotp == NULL) {
5977 /* Either there's no such module, or no module was specified.
5978 In either case, that means there's no such preference. */
5979 return PREFS_SET_NO_SUCH_PREF;
5981 *dotp = '\0'; /* separate module and preference name */
5982 module = prefs_find_module(pref_name);
5985 * XXX - "Diameter" rather than "diameter" was used in earlier
5986 * versions of Wireshark; if we didn't find the module, and its name
5987 * was "Diameter", look for "diameter" instead.
5989 * In addition, the BEEP protocol used to be the BXXP protocol,
5990 * so if we didn't find the module, and its name was "bxxp",
5991 * look for "beep" instead.
5993 * Also, the preferences for GTP v0 and v1 were combined under
5994 * a single "gtp" heading, and the preferences for SMPP were
5995 * moved to "smpp-gsm-sms" and then moved to "gsm-sms-ud".
5996 * However, SMPP now has its own preferences, so we just map
5997 * "smpp-gsm-sms" to "gsm-sms-ud", and then handle SMPP below.
5999 * We also renamed "dcp" to "dccp", "x.25" to "x25", "x411" to "p1"
6000 * and "nsip" to "gprs_ns".
6002 * The SynOptics Network Management Protocol (SONMP) is now known by
6003 * its modern name, the Nortel Discovery Protocol (NDP).
6005 if (module == NULL) {
6007 * See if there's a backwards-compatibility name
6008 * that maps to this module.
6010 module = prefs_find_module_alias(pref_name);
6011 if (module == NULL) {
6013 * There's no alias for the module; see if the
6014 * module name matches any protocol aliases.
6016 header_field_info *hfinfo = proto_registrar_get_byalias(pref_name);
6017 if (hfinfo) {
6018 module = (module_t *) wmem_tree_lookup_string(prefs_modules, hfinfo->abbrev, WMEM_TREE_STRING_NOCASE);
6021 if (module == NULL) {
6023 * There aren't any aliases. Was the module
6024 * removed rather than renamed?
6026 if (strcmp(pref_name, "etheric") == 0 ||
6027 strcmp(pref_name, "isup_thin") == 0) {
6029 * The dissectors for these protocols were
6030 * removed as obsolete on 2009-07-70 in change
6031 * 739bfc6ff035583abb9434e0e988048de38a8d9a.
6033 return PREFS_SET_OBSOLETE;
6036 if (module) {
6037 converted_pref = true;
6038 prefs.unknown_prefs = true;
6041 *dotp = '.'; /* put the preference string back */
6042 dotp++; /* skip past separator to preference name */
6043 last_dotp = dotp;
6047 /* The pref is located in the module or a submodule.
6048 * Assume module, then search for a submodule holding the pref. */
6049 containing_module = module;
6050 pref = prefs_find_preference_with_submodule(module, dotp, &containing_module);
6052 if (pref == NULL) {
6053 prefs.unknown_prefs = true;
6055 /* "gui" prefix was added to column preferences for better organization
6056 * within the preferences file
6058 if (module == gui_column_module) {
6059 /* While this has a subtree, there is no apply callback, so no
6060 * need to use prefs_find_preference_with_submodule to update
6061 * containing_module. It would not be useful. */
6062 pref = prefs_find_preference(module, pref_name);
6064 else if (strcmp(module->name, "mgcp") == 0) {
6066 * XXX - "mgcp.display raw text toggle" and "mgcp.display dissect tree"
6067 * rather than "mgcp.display_raw_text" and "mgcp.display_dissect_tree"
6068 * were used in earlier versions of Wireshark; if we didn't find the
6069 * preference, it was an MGCP preference, and its name was
6070 * "display raw text toggle" or "display dissect tree", look for
6071 * "display_raw_text" or "display_dissect_tree" instead.
6073 * "mgcp.tcp.port" and "mgcp.udp.port" are harder to handle, as both
6074 * the gateway and callagent ports were given those names; we interpret
6075 * the first as "mgcp.{tcp,udp}.gateway_port" and the second as
6076 * "mgcp.{tcp,udp}.callagent_port", as that's the order in which
6077 * they were registered by the MCCP dissector and thus that's the
6078 * order in which they were written to the preferences file. (If
6079 * we're not reading the preferences file, but are handling stuff
6080 * from a "-o" command-line option, we have no clue which the user
6081 * had in mind - they should have used "mgcp.{tcp,udp}.gateway_port"
6082 * or "mgcp.{tcp,udp}.callagent_port" instead.)
6084 if (strcmp(dotp, "display raw text toggle") == 0)
6085 pref = prefs_find_preference(module, "display_raw_text");
6086 else if (strcmp(dotp, "display dissect tree") == 0)
6087 pref = prefs_find_preference(module, "display_dissect_tree");
6088 else if (strcmp(dotp, "tcp.port") == 0) {
6089 mgcp_tcp_port_count++;
6090 if (mgcp_tcp_port_count == 1) {
6091 /* It's the first one */
6092 pref = prefs_find_preference(module, "tcp.gateway_port");
6093 } else if (mgcp_tcp_port_count == 2) {
6094 /* It's the second one */
6095 pref = prefs_find_preference(module, "tcp.callagent_port");
6097 /* Otherwise it's from the command line, and we don't bother
6098 mapping it. */
6099 } else if (strcmp(dotp, "udp.port") == 0) {
6100 mgcp_udp_port_count++;
6101 if (mgcp_udp_port_count == 1) {
6102 /* It's the first one */
6103 pref = prefs_find_preference(module, "udp.gateway_port");
6104 } else if (mgcp_udp_port_count == 2) {
6105 /* It's the second one */
6106 pref = prefs_find_preference(module, "udp.callagent_port");
6108 /* Otherwise it's from the command line, and we don't bother
6109 mapping it. */
6111 } else if (strcmp(module->name, "smb") == 0) {
6112 /* Handle old names for SMB preferences. */
6113 if (strcmp(dotp, "smb.trans.reassembly") == 0)
6114 pref = prefs_find_preference(module, "trans_reassembly");
6115 else if (strcmp(dotp, "smb.dcerpc.reassembly") == 0)
6116 pref = prefs_find_preference(module, "dcerpc_reassembly");
6117 } else if (strcmp(module->name, "ndmp") == 0) {
6118 /* Handle old names for NDMP preferences. */
6119 if (strcmp(dotp, "ndmp.desegment") == 0)
6120 pref = prefs_find_preference(module, "desegment");
6121 } else if (strcmp(module->name, "diameter") == 0) {
6122 /* Handle old names for Diameter preferences. */
6123 if (strcmp(dotp, "diameter.desegment") == 0)
6124 pref = prefs_find_preference(module, "desegment");
6125 } else if (strcmp(module->name, "pcli") == 0) {
6126 /* Handle old names for PCLI preferences. */
6127 if (strcmp(dotp, "pcli.udp_port") == 0)
6128 pref = prefs_find_preference(module, "udp_port");
6129 } else if (strcmp(module->name, "artnet") == 0) {
6130 /* Handle old names for ARTNET preferences. */
6131 if (strcmp(dotp, "artnet.udp_port") == 0)
6132 pref = prefs_find_preference(module, "udp_port");
6133 } else if (strcmp(module->name, "mapi") == 0) {
6134 /* Handle old names for MAPI preferences. */
6135 if (strcmp(dotp, "mapi_decrypt") == 0)
6136 pref = prefs_find_preference(module, "decrypt");
6137 } else if (strcmp(module->name, "fc") == 0) {
6138 /* Handle old names for Fibre Channel preferences. */
6139 if (strcmp(dotp, "reassemble_fc") == 0)
6140 pref = prefs_find_preference(module, "reassemble");
6141 else if (strcmp(dotp, "fc_max_frame_size") == 0)
6142 pref = prefs_find_preference(module, "max_frame_size");
6143 } else if (strcmp(module->name, "fcip") == 0) {
6144 /* Handle old names for Fibre Channel-over-IP preferences. */
6145 if (strcmp(dotp, "desegment_fcip_messages") == 0)
6146 pref = prefs_find_preference(module, "desegment");
6147 else if (strcmp(dotp, "fcip_port") == 0)
6148 pref = prefs_find_preference(module, "target_port");
6149 } else if (strcmp(module->name, "gtp") == 0) {
6150 /* Handle old names for GTP preferences. */
6151 if (strcmp(dotp, "gtpv0_port") == 0)
6152 pref = prefs_find_preference(module, "v0_port");
6153 else if (strcmp(dotp, "gtpv1c_port") == 0)
6154 pref = prefs_find_preference(module, "v1c_port");
6155 else if (strcmp(dotp, "gtpv1u_port") == 0)
6156 pref = prefs_find_preference(module, "v1u_port");
6157 else if (strcmp(dotp, "gtp_dissect_tpdu") == 0)
6158 pref = prefs_find_preference(module, "dissect_tpdu");
6159 else if (strcmp(dotp, "gtpv0_dissect_cdr_as") == 0)
6160 pref = prefs_find_preference(module, "v0_dissect_cdr_as");
6161 else if (strcmp(dotp, "gtpv0_check_etsi") == 0)
6162 pref = prefs_find_preference(module, "v0_check_etsi");
6163 else if (strcmp(dotp, "gtpv1_check_etsi") == 0)
6164 pref = prefs_find_preference(module, "v1_check_etsi");
6165 } else if (strcmp(module->name, "ip") == 0) {
6166 /* Handle old names for IP preferences. */
6167 if (strcmp(dotp, "ip_summary_in_tree") == 0)
6168 pref = prefs_find_preference(module, "summary_in_tree");
6169 } else if (strcmp(module->name, "iscsi") == 0) {
6170 /* Handle old names for iSCSI preferences. */
6171 if (strcmp(dotp, "iscsi_port") == 0)
6172 pref = prefs_find_preference(module, "target_port");
6173 } else if (strcmp(module->name, "lmp") == 0) {
6174 /* Handle old names for LMP preferences. */
6175 if (strcmp(dotp, "lmp_version") == 0)
6176 pref = prefs_find_preference(module, "version");
6177 } else if (strcmp(module->name, "mtp3") == 0) {
6178 /* Handle old names for MTP3 preferences. */
6179 if (strcmp(dotp, "mtp3_standard") == 0)
6180 pref = prefs_find_preference(module, "standard");
6181 else if (strcmp(dotp, "net_addr_format") == 0)
6182 pref = prefs_find_preference(module, "addr_format");
6183 } else if (strcmp(module->name, "nlm") == 0) {
6184 /* Handle old names for NLM preferences. */
6185 if (strcmp(dotp, "nlm_msg_res_matching") == 0)
6186 pref = prefs_find_preference(module, "msg_res_matching");
6187 } else if (strcmp(module->name, "ppp") == 0) {
6188 /* Handle old names for PPP preferences. */
6189 if (strcmp(dotp, "ppp_fcs") == 0)
6190 pref = prefs_find_preference(module, "fcs_type");
6191 else if (strcmp(dotp, "ppp_vj") == 0)
6192 pref = prefs_find_preference(module, "decompress_vj");
6193 } else if (strcmp(module->name, "rsvp") == 0) {
6194 /* Handle old names for RSVP preferences. */
6195 if (strcmp(dotp, "rsvp_process_bundle") == 0)
6196 pref = prefs_find_preference(module, "process_bundle");
6197 } else if (strcmp(module->name, "tcp") == 0) {
6198 /* Handle old names for TCP preferences. */
6199 if (strcmp(dotp, "tcp_summary_in_tree") == 0)
6200 pref = prefs_find_preference(module, "summary_in_tree");
6201 else if (strcmp(dotp, "tcp_analyze_sequence_numbers") == 0)
6202 pref = prefs_find_preference(module, "analyze_sequence_numbers");
6203 else if (strcmp(dotp, "tcp_relative_sequence_numbers") == 0)
6204 pref = prefs_find_preference(module, "relative_sequence_numbers");
6205 else if (strcmp(dotp, "dissect_experimental_options_with_magic") == 0)
6206 pref = prefs_find_preference(module, "dissect_experimental_options_rfc6994");
6207 } else if (strcmp(module->name, "udp") == 0) {
6208 /* Handle old names for UDP preferences. */
6209 if (strcmp(dotp, "udp_summary_in_tree") == 0)
6210 pref = prefs_find_preference(module, "summary_in_tree");
6211 } else if (strcmp(module->name, "ndps") == 0) {
6212 /* Handle old names for NDPS preferences. */
6213 if (strcmp(dotp, "desegment_ndps") == 0)
6214 pref = prefs_find_preference(module, "desegment_tcp");
6215 } else if (strcmp(module->name, "http") == 0) {
6216 /* Handle old names for HTTP preferences. */
6217 if (strcmp(dotp, "desegment_http_headers") == 0)
6218 pref = prefs_find_preference(module, "desegment_headers");
6219 else if (strcmp(dotp, "desegment_http_body") == 0)
6220 pref = prefs_find_preference(module, "desegment_body");
6221 } else if (strcmp(module->name, "smpp") == 0) {
6222 /* Handle preferences that moved from SMPP. */
6223 module_t *new_module = prefs_find_module("gsm-sms-ud");
6224 if (new_module) {
6225 if (strcmp(dotp, "port_number_udh_means_wsp") == 0) {
6226 pref = prefs_find_preference(new_module, "port_number_udh_means_wsp");
6227 containing_module = new_module;
6228 } else if (strcmp(dotp, "try_dissect_1st_fragment") == 0) {
6229 pref = prefs_find_preference(new_module, "try_dissect_1st_fragment");
6230 containing_module = new_module;
6233 } else if (strcmp(module->name, "asn1") == 0) {
6234 /* Handle old generic ASN.1 preferences (it's not really a
6235 rename, as the new preferences support multiple ports,
6236 but we might as well copy them over). */
6237 if (strcmp(dotp, "tcp_port") == 0)
6238 pref = prefs_find_preference(module, "tcp_ports");
6239 else if (strcmp(dotp, "udp_port") == 0)
6240 pref = prefs_find_preference(module, "udp_ports");
6241 else if (strcmp(dotp, "sctp_port") == 0)
6242 pref = prefs_find_preference(module, "sctp_ports");
6243 } else if (strcmp(module->name, "llcgprs") == 0) {
6244 if (strcmp(dotp, "ignore_cipher_bit") == 0)
6245 pref = prefs_find_preference(module, "autodetect_cipher_bit");
6246 } else if (strcmp(module->name, "erf") == 0) {
6247 if (strcmp(dotp, "erfeth") == 0) {
6248 /* Handle the old "erfeth" preference; map it to the new
6249 "ethfcs" preference, and map the values to those for
6250 the new preference. */
6251 pref = prefs_find_preference(module, "ethfcs");
6252 if (strcmp(value, "ethfcs") == 0 || strcmp(value, "Ethernet with FCS") == 0)
6253 value = "TRUE";
6254 else if (strcmp(value, "eth") == 0 || strcmp(value, "Ethernet") == 0)
6255 value = "FALSE";
6256 else if (strcmp(value, "raw") == 0 || strcmp(value, "Raw data") == 0)
6257 value = "TRUE";
6258 } else if (strcmp(dotp, "erfatm") == 0) {
6259 /* Handle the old "erfatm" preference; map it to the new
6260 "aal5_type" preference, and map the values to those for
6261 the new preference. */
6262 pref = prefs_find_preference(module, "aal5_type");
6263 if (strcmp(value, "atm") == 0 || strcmp(value, "ATM") == 0)
6264 value = "guess";
6265 else if (strcmp(value, "llc") == 0 || strcmp(value, "LLC") == 0)
6266 value = "llc";
6267 else if (strcmp(value, "raw") == 0 || strcmp(value, "Raw data") == 0)
6268 value = "guess";
6269 } else if (strcmp(dotp, "erfhdlc") == 0) {
6270 /* Handle the old "erfhdlc" preference; map it to the new
6271 "hdlc_type" preference, and map the values to those for
6272 the new preference. */
6273 pref = prefs_find_preference(module, "hdlc_type");
6274 if (strcmp(value, "chdlc") == 0 || strcmp(value, "Cisco HDLC") == 0)
6275 value = "chdlc";
6276 else if (strcmp(value, "ppp") == 0 || strcmp(value, "PPP serial") == 0)
6277 value = "ppp";
6278 else if (strcmp(value, "fr") == 0 || strcmp(value, "Frame Relay") == 0)
6279 value = "frelay";
6280 else if (strcmp(value, "mtp2") == 0 || strcmp(value, "SS7 MTP2") == 0)
6281 value = "mtp2";
6282 else if (strcmp(value, "raw") == 0 || strcmp(value, "Raw data") == 0)
6283 value = "guess";
6285 } else if (strcmp(module->name, "eth") == 0) {
6286 /* "eth.qinq_ethertype" has been changed(restored) to "vlan.qinq.ethertype" */
6287 if (strcmp(dotp, "qinq_ethertype") == 0) {
6288 module_t *new_module = prefs_find_module("vlan");
6289 if (new_module) {
6290 pref = prefs_find_preference(new_module, "qinq_ethertype");
6291 containing_module = new_module;
6294 } else if (strcmp(module->name, "taps") == 0) {
6295 /* taps preferences moved to "statistics" module */
6296 if (strcmp(dotp, "update_interval") == 0)
6297 pref = prefs_find_preference(stats_module, dotp);
6298 } else if (strcmp(module->name, "packet_list") == 0) {
6299 /* packet_list preferences moved to protocol module */
6300 if (strcmp(dotp, "display_hidden_proto_items") == 0)
6301 pref = prefs_find_preference(protocols_module, dotp);
6302 } else if (strcmp(module->name, "stream") == 0) {
6303 /* stream preferences moved to gui color module */
6304 if ((strcmp(dotp, "client.fg") == 0) ||
6305 (strcmp(dotp, "client.bg") == 0) ||
6306 (strcmp(dotp, "server.fg") == 0) ||
6307 (strcmp(dotp, "server.bg") == 0))
6308 pref = prefs_find_preference(gui_color_module, pref_name);
6309 } else if (strcmp(module->name, "nameres") == 0) {
6310 if (strcmp(pref_name, "name_resolve_concurrency") == 0) {
6311 pref = prefs_find_preference(nameres_module, pref_name);
6312 } else if (strcmp(pref_name, "name_resolve_load_smi_modules") == 0) {
6313 pref = prefs_find_preference(nameres_module, "load_smi_modules");
6314 } else if (strcmp(pref_name, "name_resolve_suppress_smi_errors") == 0) {
6315 pref = prefs_find_preference(nameres_module, "suppress_smi_errors");
6317 } else if (strcmp(module->name, "extcap") == 0) {
6318 /* Handle the old "sshdump.remotesudo" preference; map it to the new
6319 "sshdump.remotepriv" preference, and map the boolean values to the
6320 appropriate strings of the new preference. */
6321 if (strcmp(dotp, "sshdump.remotesudo") == 0) {
6322 pref = prefs_find_preference(module, "sshdump.remotepriv");
6323 if (g_ascii_strcasecmp(value, "true") == 0)
6324 value = "sudo";
6325 else
6326 value = "none";
6329 if (pref) {
6330 converted_pref = true;
6333 if (pref == NULL ) {
6334 if (strcmp(module->name, "extcap") == 0 && g_list_length(module->prefs) <= 1) {
6336 * Assume that we've skipped extcap preference registration
6337 * and that only extcap.gui_save_on_start is loaded.
6339 return PREFS_SET_OK;
6341 return PREFS_SET_NO_SUCH_PREF; /* no such preference */
6344 if (target_module && target_module != containing_module) {
6345 /* Ignore */
6346 return PREFS_SET_OK;
6349 type = pref->type;
6350 if (IS_PREF_OBSOLETE(type)) {
6351 return PREFS_SET_OBSOLETE; /* no such preference any more */
6352 } else {
6353 RESET_PREF_OBSOLETE(type);
6356 if (converted_pref) {
6357 ws_warning("Preference \"%s\" has been converted to \"%s.%s\"\n"
6358 "Save your preferences to make this change permanent.",
6359 pref_name, module->name ? module->name : module->parent->name, prefs_get_name(pref));
6362 switch (type) {
6364 case PREF_UINT:
6365 if (!ws_basestrtou32(value, NULL, &uval, pref->info.base))
6366 return PREFS_SET_SYNTAX_ERR; /* number was bad */
6367 if (*pref->varp.uint != uval) {
6368 containing_module->prefs_changed_flags |= prefs_get_effect_flags(pref);
6369 *pref->varp.uint = uval;
6371 break;
6372 case PREF_BOOL:
6373 /* XXX - give an error if it's neither "true" nor "false"? */
6374 if (g_ascii_strcasecmp(value, "true") == 0)
6375 bval = true;
6376 else
6377 bval = false;
6378 if (*pref->varp.boolp != bval) {
6379 containing_module->prefs_changed_flags |= prefs_get_effect_flags(pref);
6380 *pref->varp.boolp = bval;
6382 break;
6384 case PREF_ENUM:
6385 /* XXX - give an error if it doesn't match? */
6386 enum_val = find_val_for_string(value, pref->info.enum_info.enumvals,
6387 *pref->varp.enump);
6388 if (*pref->varp.enump != enum_val) {
6389 containing_module->prefs_changed_flags |= prefs_get_effect_flags(pref);
6390 *pref->varp.enump = enum_val;
6392 break;
6394 case PREF_STRING:
6395 case PREF_SAVE_FILENAME:
6396 case PREF_OPEN_FILENAME:
6397 case PREF_DIRNAME:
6398 case PREF_DISSECTOR:
6399 containing_module->prefs_changed_flags |= prefs_set_string_value(pref, value, pref_current);
6400 break;
6402 case PREF_PASSWORD:
6403 /* Read value is every time empty */
6404 containing_module->prefs_changed_flags |= prefs_set_string_value(pref, "", pref_current);
6405 break;
6407 case PREF_RANGE:
6409 if (!prefs_set_range_value_work(pref, value, return_range_errors,
6410 &containing_module->prefs_changed_flags))
6411 return PREFS_SET_SYNTAX_ERR; /* number was bad */
6412 break;
6414 case PREF_DECODE_AS_RANGE:
6416 /* This is for backwards compatibility in case any of the preferences
6417 that shared the "Decode As" preference name and used to be PREF_RANGE
6418 are now applied directly to the Decode As funtionality */
6419 range_t *newrange;
6420 dissector_table_t sub_dissectors;
6421 dissector_handle_t handle;
6422 uint32_t i, j;
6424 if (range_convert_str_work(wmem_epan_scope(), &newrange, value, pref->info.max_value,
6425 return_range_errors) != CVT_NO_ERROR) {
6426 return PREFS_SET_SYNTAX_ERR; /* number was bad */
6429 if (!ranges_are_equal(*pref->varp.range, newrange)) {
6430 wmem_free(wmem_epan_scope(), *pref->varp.range);
6431 *pref->varp.range = newrange;
6432 containing_module->prefs_changed_flags |= prefs_get_effect_flags(pref);
6434 const char* table_name = prefs_get_dissector_table(pref);
6435 sub_dissectors = find_dissector_table(table_name);
6436 if (sub_dissectors != NULL) {
6437 handle = dissector_table_get_dissector_handle(sub_dissectors, module->title);
6438 if (handle != NULL) {
6439 /* Delete all of the old values from the dissector table */
6440 for (i = 0; i < (*pref->varp.range)->nranges; i++) {
6441 for (j = (*pref->varp.range)->ranges[i].low; j < (*pref->varp.range)->ranges[i].high; j++) {
6442 dissector_delete_uint(table_name, j, handle);
6443 decode_build_reset_list(table_name, dissector_table_get_type(sub_dissectors), GUINT_TO_POINTER(j), NULL, NULL);
6446 dissector_delete_uint(table_name, (*pref->varp.range)->ranges[i].high, handle);
6447 decode_build_reset_list(table_name, dissector_table_get_type(sub_dissectors), GUINT_TO_POINTER((*pref->varp.range)->ranges[i].high), NULL, NULL);
6450 /* Add new values to the dissector table */
6451 for (i = 0; i < newrange->nranges; i++) {
6452 for (j = newrange->ranges[i].low; j < newrange->ranges[i].high; j++) {
6453 dissector_change_uint(table_name, j, handle);
6454 decode_build_reset_list(table_name, dissector_table_get_type(sub_dissectors), GUINT_TO_POINTER(j), NULL, NULL);
6457 dissector_change_uint(table_name, newrange->ranges[i].high, handle);
6458 decode_build_reset_list(table_name, dissector_table_get_type(sub_dissectors), GUINT_TO_POINTER(newrange->ranges[i].high), NULL, NULL);
6461 /* XXX - Do we save the decode_as_entries file here? */
6464 } else {
6465 wmem_free(wmem_epan_scope(), newrange);
6467 break;
6470 case PREF_COLOR:
6472 if (!ws_hexstrtou32(value, NULL, &cval))
6473 return PREFS_SET_SYNTAX_ERR; /* number was bad */
6474 if ((pref->varp.colorp->red != RED_COMPONENT(cval)) ||
6475 (pref->varp.colorp->green != GREEN_COMPONENT(cval)) ||
6476 (pref->varp.colorp->blue != BLUE_COMPONENT(cval))) {
6477 containing_module->prefs_changed_flags |= prefs_get_effect_flags(pref);
6478 pref->varp.colorp->red = RED_COMPONENT(cval);
6479 pref->varp.colorp->green = GREEN_COMPONENT(cval);
6480 pref->varp.colorp->blue = BLUE_COMPONENT(cval);
6482 break;
6485 case PREF_CUSTOM:
6486 return pref->custom_cbs.set_cb(pref, value, &containing_module->prefs_changed_flags);
6488 case PREF_STATIC_TEXT:
6489 case PREF_UAT:
6490 case PREF_PROTO_TCP_SNDAMB_ENUM:
6492 /* There's no point in setting the TCP sequence override
6493 * value from the command line, because the pref is different
6494 * for each frame and reset to the default (0) for each new
6495 * file.
6497 break;
6502 return PREFS_SET_OK;
6505 typedef struct {
6506 FILE *pf;
6507 bool is_gui_module;
6508 } write_gui_pref_arg_t;
6510 const char *
6511 prefs_pref_type_name(pref_t *pref)
6513 const char *type_name = "[Unknown]";
6514 int type;
6516 if (!pref) {
6517 return type_name; /* ...or maybe assert? */
6520 type = pref->type;
6522 if (IS_PREF_OBSOLETE(type)) {
6523 type_name = "Obsolete";
6524 } else {
6525 RESET_PREF_OBSOLETE(type);
6528 switch (type) {
6530 case PREF_UINT:
6531 switch (pref->info.base) {
6533 case 10:
6534 type_name = "Decimal";
6535 break;
6537 case 8:
6538 type_name = "Octal";
6539 break;
6541 case 16:
6542 type_name = "Hexadecimal";
6543 break;
6545 break;
6547 case PREF_BOOL:
6548 type_name = "Boolean";
6549 break;
6551 case PREF_ENUM:
6552 case PREF_PROTO_TCP_SNDAMB_ENUM:
6553 type_name = "Choice";
6554 break;
6556 case PREF_STRING:
6557 type_name = "String";
6558 break;
6560 case PREF_SAVE_FILENAME:
6561 case PREF_OPEN_FILENAME:
6562 type_name = "Filename";
6563 break;
6565 case PREF_DIRNAME:
6566 type_name = "Directory";
6567 break;
6569 case PREF_RANGE:
6570 type_name = "Range";
6571 break;
6573 case PREF_COLOR:
6574 type_name = "Color";
6575 break;
6577 case PREF_CUSTOM:
6578 if (pref->custom_cbs.type_name_cb)
6579 return pref->custom_cbs.type_name_cb();
6580 type_name = "Custom";
6581 break;
6583 case PREF_DECODE_AS_RANGE:
6584 type_name = "Range (for Decode As)";
6585 break;
6587 case PREF_STATIC_TEXT:
6588 type_name = "Static text";
6589 break;
6591 case PREF_UAT:
6592 type_name = "UAT";
6593 break;
6595 case PREF_PASSWORD:
6596 type_name = "Password";
6597 break;
6599 case PREF_DISSECTOR:
6600 type_name = "Dissector";
6601 break;
6603 return type_name;
6606 unsigned int
6607 prefs_get_effect_flags(pref_t *pref)
6609 if (pref == NULL)
6610 return 0;
6612 return pref->effect_flags;
6615 void
6616 prefs_set_effect_flags(pref_t *pref, unsigned int flags)
6618 if (pref != NULL) {
6619 if (flags == 0) {
6620 ws_error("Setting \"%s\" preference effect flags to 0", pref->name);
6622 pref->effect_flags = flags;
6626 void
6627 prefs_set_effect_flags_by_name(module_t * module, const char *pref, unsigned int flags)
6629 prefs_set_effect_flags(prefs_find_preference(module, pref), flags);
6632 unsigned int
6633 prefs_get_module_effect_flags(module_t * module)
6635 if (module == NULL)
6636 return 0;
6638 return module->effect_flags;
6641 void
6642 prefs_set_module_effect_flags(module_t * module, unsigned int flags)
6644 if (module != NULL) {
6645 if (flags == 0) {
6646 ws_error("Setting module \"%s\" preference effect flags to 0", module->name);
6648 module->effect_flags = flags;
6652 char *
6653 prefs_pref_type_description(pref_t *pref)
6655 const char *type_desc = "An unknown preference type";
6656 int type;
6658 if (!pref) {
6659 return ws_strdup_printf("%s.", type_desc); /* ...or maybe assert? */
6662 type = pref->type;
6664 if (IS_PREF_OBSOLETE(type)) {
6665 type_desc = "An obsolete preference";
6666 } else {
6667 RESET_PREF_OBSOLETE(type);
6670 switch (type) {
6672 case PREF_UINT:
6673 switch (pref->info.base) {
6675 case 10:
6676 type_desc = "A decimal number";
6677 break;
6679 case 8:
6680 type_desc = "An octal number";
6681 break;
6683 case 16:
6684 type_desc = "A hexadecimal number";
6685 break;
6687 break;
6689 case PREF_BOOL:
6690 type_desc = "true or false (case-insensitive)";
6691 break;
6693 case PREF_ENUM:
6694 case PREF_PROTO_TCP_SNDAMB_ENUM:
6696 const enum_val_t *enum_valp = pref->info.enum_info.enumvals;
6697 GString *enum_str = g_string_new("One of: ");
6698 GString *desc_str = g_string_new("\nEquivalently, one of: ");
6699 bool distinct = false;
6700 while (enum_valp->name != NULL) {
6701 g_string_append(enum_str, enum_valp->name);
6702 g_string_append(desc_str, enum_valp->description);
6703 if (g_strcmp0(enum_valp->name, enum_valp->description) != 0) {
6704 distinct = true;
6706 enum_valp++;
6707 if (enum_valp->name != NULL) {
6708 g_string_append(enum_str, ", ");
6709 g_string_append(desc_str, ", ");
6712 if (distinct) {
6713 g_string_append(enum_str, desc_str->str);
6715 g_string_free(desc_str, TRUE);
6716 g_string_append(enum_str, "\n(case-insensitive).");
6717 return g_string_free(enum_str, FALSE);
6720 case PREF_STRING:
6721 type_desc = "A string";
6722 break;
6724 case PREF_SAVE_FILENAME:
6725 case PREF_OPEN_FILENAME:
6726 type_desc = "A path to a file";
6727 break;
6729 case PREF_DIRNAME:
6730 type_desc = "A path to a directory";
6731 break;
6733 case PREF_RANGE:
6735 type_desc = "A string denoting an positive integer range (e.g., \"1-20,30-40\")";
6736 break;
6739 case PREF_COLOR:
6741 type_desc = "A six-digit hexadecimal RGB color triplet (e.g. fce94f)";
6742 break;
6745 case PREF_CUSTOM:
6746 if (pref->custom_cbs.type_description_cb)
6747 return pref->custom_cbs.type_description_cb();
6748 type_desc = "A custom value";
6749 break;
6751 case PREF_DECODE_AS_RANGE:
6752 type_desc = "A string denoting an positive integer range for Decode As";
6753 break;
6755 case PREF_STATIC_TEXT:
6756 type_desc = "[Static text]";
6757 break;
6759 case PREF_UAT:
6760 type_desc = "Configuration data stored in its own file";
6761 break;
6763 case PREF_PASSWORD:
6764 type_desc = "Password (never stored on disk)";
6765 break;
6767 case PREF_DISSECTOR:
6768 type_desc = "A dissector name";
6769 break;
6771 default:
6772 break;
6774 return g_strdup(type_desc);
6777 bool
6778 prefs_pref_is_default(pref_t *pref)
6780 int type;
6781 if (!pref) return false;
6783 type = pref->type;
6784 if (IS_PREF_OBSOLETE(type)) {
6785 return false;
6786 } else {
6787 RESET_PREF_OBSOLETE(type);
6790 switch (type) {
6792 case PREF_UINT:
6793 if (pref->default_val.uint == *pref->varp.uint)
6794 return true;
6795 break;
6797 case PREF_BOOL:
6798 if (pref->default_val.boolval == *pref->varp.boolp)
6799 return true;
6800 break;
6802 case PREF_ENUM:
6803 case PREF_PROTO_TCP_SNDAMB_ENUM:
6804 if (pref->default_val.enumval == *pref->varp.enump)
6805 return true;
6806 break;
6808 case PREF_STRING:
6809 case PREF_SAVE_FILENAME:
6810 case PREF_OPEN_FILENAME:
6811 case PREF_DIRNAME:
6812 case PREF_PASSWORD:
6813 case PREF_DISSECTOR:
6814 if (!(g_strcmp0(pref->default_val.string, *pref->varp.string)))
6815 return true;
6816 break;
6818 case PREF_DECODE_AS_RANGE:
6819 case PREF_RANGE:
6821 if ((ranges_are_equal(pref->default_val.range, *pref->varp.range)))
6822 return true;
6823 break;
6826 case PREF_COLOR:
6828 if ((pref->default_val.color.red == pref->varp.colorp->red) &&
6829 (pref->default_val.color.green == pref->varp.colorp->green) &&
6830 (pref->default_val.color.blue == pref->varp.colorp->blue))
6831 return true;
6832 break;
6835 case PREF_CUSTOM:
6836 return pref->custom_cbs.is_default_cb(pref);
6838 case PREF_STATIC_TEXT:
6839 case PREF_UAT:
6840 return false;
6841 /* ws_assert_not_reached(); */
6842 break;
6844 return false;
6847 char *
6848 prefs_pref_to_str(pref_t *pref, pref_source_t source) {
6849 const char *pref_text = "[Unknown]";
6850 void *valp; /* pointer to preference value */
6851 color_t *pref_color;
6852 char *tmp_value, *ret_value;
6853 int type;
6855 if (!pref) {
6856 return g_strdup(pref_text);
6859 switch (source) {
6860 case pref_default:
6861 valp = &pref->default_val;
6862 /* valp = &boolval, &enumval, etc. are implied by union property */
6863 pref_color = &pref->default_val.color;
6864 break;
6865 case pref_stashed:
6866 valp = &pref->stashed_val;
6867 /* valp = &boolval, &enumval, etc. are implied by union property */
6868 pref_color = &pref->stashed_val.color;
6869 break;
6870 case pref_current:
6871 valp = pref->varp.uint;
6872 /* valp = boolval, enumval, etc. are implied by union property */
6873 pref_color = pref->varp.colorp;
6874 break;
6875 default:
6876 return g_strdup(pref_text);
6879 type = pref->type;
6880 if (IS_PREF_OBSOLETE(type)) {
6881 pref_text = "[Obsolete]";
6882 } else {
6883 RESET_PREF_OBSOLETE(type);
6886 switch (type) {
6888 case PREF_UINT:
6890 unsigned pref_uint = *(unsigned *) valp;
6891 switch (pref->info.base) {
6893 case 10:
6894 return ws_strdup_printf("%u", pref_uint);
6896 case 8:
6897 return ws_strdup_printf("%#o", pref_uint);
6899 case 16:
6900 return ws_strdup_printf("%#x", pref_uint);
6902 break;
6905 case PREF_BOOL:
6906 return g_strdup((*(bool *) valp) ? "TRUE" : "FALSE");
6908 case PREF_ENUM:
6909 case PREF_PROTO_TCP_SNDAMB_ENUM:
6911 int pref_enumval = *(int *) valp;
6912 const enum_val_t *enum_valp = pref->info.enum_info.enumvals;
6914 * TODO - We write the "description" value, because the "name" values
6915 * weren't validated to be command line friendly until 5.0, and a few
6916 * of them had to be changed. This allows older versions of Wireshark
6917 * to read preferences that they supported, as we supported either
6918 * the short name or the description when reading the preference files
6919 * or an "-o" option. Once 5.0 is the oldest supported version, switch
6920 * to writing the name below.
6922 while (enum_valp->name != NULL) {
6923 if (enum_valp->value == pref_enumval)
6924 return g_strdup(enum_valp->description);
6925 enum_valp++;
6927 break;
6930 case PREF_STRING:
6931 case PREF_SAVE_FILENAME:
6932 case PREF_OPEN_FILENAME:
6933 case PREF_DIRNAME:
6934 case PREF_PASSWORD:
6935 case PREF_DISSECTOR:
6936 return g_strdup(*(const char **) valp);
6938 case PREF_DECODE_AS_RANGE:
6939 case PREF_RANGE:
6940 /* Convert wmem to g_alloc memory */
6941 tmp_value = range_convert_range(NULL, *(range_t **) valp);
6942 ret_value = g_strdup(tmp_value);
6943 wmem_free(NULL, tmp_value);
6944 return ret_value;
6946 case PREF_COLOR:
6947 return ws_strdup_printf("%02x%02x%02x",
6948 (pref_color->red * 255 / 65535),
6949 (pref_color->green * 255 / 65535),
6950 (pref_color->blue * 255 / 65535));
6952 case PREF_CUSTOM:
6953 if (pref->custom_cbs.to_str_cb)
6954 return pref->custom_cbs.to_str_cb(pref, source == pref_default ? true : false);
6955 pref_text = "[Custom]";
6956 break;
6958 case PREF_STATIC_TEXT:
6959 pref_text = "[Static text]";
6960 break;
6962 case PREF_UAT:
6964 uat_t *uat = pref->varp.uat;
6965 if (uat && uat->filename)
6966 return ws_strdup_printf("[Managed in the file \"%s\"]", uat->filename);
6967 else
6968 pref_text = "[Managed in an unknown file]";
6969 break;
6972 default:
6973 break;
6975 return g_strdup(pref_text);
6979 * Write out a single dissector preference.
6981 static void
6982 write_pref(void *data, void *user_data)
6984 pref_t *pref = (pref_t *)data;
6985 write_pref_arg_t *arg = (write_pref_arg_t *)user_data;
6986 char **desc_lines;
6987 int i;
6988 int type;
6990 type = pref->type;
6992 if (IS_PREF_OBSOLETE(type)) {
6994 * This preference is no longer supported; it's not a
6995 * real preference, so we don't write it out (i.e., we
6996 * treat it as if it weren't found in the list of
6997 * preferences, and we weren't called in the first place).
6999 return;
7000 } else {
7001 RESET_PREF_OBSOLETE(type);
7004 switch (type) {
7006 case PREF_STATIC_TEXT:
7007 case PREF_UAT:
7008 /* Nothing to do; don't bother printing the description */
7009 return;
7010 case PREF_DECODE_AS_RANGE:
7011 /* Data is saved through Decode As mechanism and not part of preferences file */
7012 return;
7013 case PREF_PROTO_TCP_SNDAMB_ENUM:
7014 /* Not written to the preference file because the override is only
7015 * for the lifetime of the capture file and there is no single
7016 * value to write.
7018 return;
7019 default:
7020 break;
7023 if (pref->type != PREF_CUSTOM || pref->custom_cbs.type_name_cb() != NULL) {
7025 * The prefix will either be the module name or the parent
7026 * name if it's a subtree
7028 const char *name_prefix = (arg->module->name != NULL) ? arg->module->name : arg->module->parent->name;
7029 char *type_desc, *pref_text;
7030 const char * def_prefix = prefs_pref_is_default(pref) ? "#" : "";
7032 if (pref->type == PREF_CUSTOM)
7033 fprintf(arg->pf, "\n# %s", pref->custom_cbs.type_name_cb());
7034 fprintf(arg->pf, "\n");
7035 if (pref->description &&
7036 (g_ascii_strncasecmp(pref->description,"", 2) != 0)) {
7037 if (pref->type != PREF_CUSTOM) {
7038 /* We get duplicate lines otherwise. */
7040 desc_lines = g_strsplit(pref->description, "\n", 0);
7041 for (i = 0; desc_lines[i] != NULL; ++i) {
7042 fprintf(arg->pf, "# %s\n", desc_lines[i]);
7044 g_strfreev(desc_lines);
7046 } else {
7047 fprintf(arg->pf, "# No description\n");
7050 type_desc = prefs_pref_type_description(pref);
7051 desc_lines = g_strsplit(type_desc, "\n", 0);
7052 for (i = 0; desc_lines[i] != NULL; ++i) {
7053 fprintf(arg->pf, "# %s\n", desc_lines[i]);
7055 g_strfreev(desc_lines);
7056 g_free(type_desc);
7058 pref_text = prefs_pref_to_str(pref, pref_current);
7059 fprintf(arg->pf, "%s%s.%s: ", def_prefix, name_prefix, pref->name);
7060 if (pref->type != PREF_PASSWORD)
7062 desc_lines = g_strsplit(pref_text, "\n", 0);
7063 for (i = 0; desc_lines[i] != NULL; ++i) {
7064 fprintf(arg->pf, "%s%s\n", i == 0 ? "" : def_prefix, desc_lines[i]);
7066 if (i == 0)
7067 fprintf(arg->pf, "\n");
7068 g_strfreev(desc_lines);
7069 } else {
7070 /* We never store password value */
7071 fprintf(arg->pf, "\n");
7073 g_free(pref_text);
7078 static void
7079 count_non_uat_pref(void *data, void *user_data)
7081 pref_t *pref = (pref_t *)data;
7082 int *arg = (int *)user_data;
7084 switch (pref->type)
7086 case PREF_UAT:
7087 case PREF_OBSOLETE:
7088 case PREF_DECODE_AS_RANGE:
7089 case PREF_PROTO_TCP_SNDAMB_ENUM:
7090 //These types are not written in preference file
7091 break;
7092 default:
7093 (*arg)++;
7094 break;
7098 static int num_non_uat_prefs(module_t *module)
7100 int num = 0;
7102 g_list_foreach(module->prefs, count_non_uat_pref, &num);
7104 return num;
7108 * Write out all preferences for a module.
7110 static unsigned
7111 write_module_prefs(module_t *module, void *user_data)
7113 write_gui_pref_arg_t *gui_pref_arg = (write_gui_pref_arg_t*)user_data;
7114 write_pref_arg_t arg;
7116 /* The GUI module needs to be explicitly called out so it
7117 can be written out of order */
7118 if ((module == gui_module) && (gui_pref_arg->is_gui_module != true))
7119 return 0;
7121 /* Write a header for the main modules and GUI sub-modules */
7122 if (((module->parent == NULL) || (module->parent == gui_module)) &&
7123 ((prefs_module_has_submodules(module)) ||
7124 (num_non_uat_prefs(module) > 0) ||
7125 (module->name == NULL))) {
7126 if ((module->name == NULL) && (module->parent != NULL)) {
7127 fprintf(gui_pref_arg->pf, "\n####### %s: %s ########\n", module->parent->title, module->title);
7128 } else {
7129 fprintf(gui_pref_arg->pf, "\n####### %s ########\n", module->title);
7133 arg.module = module;
7134 arg.pf = gui_pref_arg->pf;
7135 g_list_foreach(arg.module->prefs, write_pref, &arg);
7137 if (prefs_module_has_submodules(module))
7138 return prefs_modules_foreach_submodules(module, write_module_prefs, user_data);
7140 return 0;
7143 #ifdef _WIN32
7144 static void
7145 write_registry(void)
7147 HKEY hTestKey;
7148 DWORD data;
7149 DWORD data_size;
7150 DWORD ret;
7152 ret = RegCreateKeyExA(HKEY_CURRENT_USER, REG_HKCU_WIRESHARK_KEY, 0, NULL,
7153 REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL,
7154 &hTestKey, NULL);
7155 if (ret != ERROR_SUCCESS) {
7156 ws_noisy("Cannot open HKCU "REG_HKCU_WIRESHARK_KEY": 0x%lx", ret);
7157 return;
7160 data = ws_log_console_open;
7161 data_size = sizeof(DWORD);
7162 ret = RegSetValueExA(hTestKey, LOG_HKCU_CONSOLE_OPEN, 0, REG_DWORD, (const BYTE *)&data, data_size);
7163 if (ret == ERROR_SUCCESS) {
7164 ws_noisy("Wrote "LOG_HKCU_CONSOLE_OPEN" to Windows registry: 0x%lu", data);
7166 else {
7167 ws_noisy("Error writing registry key "LOG_HKCU_CONSOLE_OPEN": 0x%lx", ret);
7170 RegCloseKey(hTestKey);
7172 #endif
7174 /* Write out "prefs" to the user's preferences file, and return 0.
7176 If the preferences file path is NULL, write to stdout.
7178 If we got an error, stuff a pointer to the path of the preferences file
7179 into "*pf_path_return", and return the errno. */
7181 write_prefs(char **pf_path_return)
7183 char *pf_path;
7184 FILE *pf;
7185 write_gui_pref_arg_t write_gui_pref_info;
7187 /* Needed for "-G defaultprefs" */
7188 init_prefs();
7190 #ifdef _WIN32
7191 write_registry();
7192 #endif
7194 /* To do:
7195 * - Split output lines longer than MAX_VAL_LEN
7196 * - Create a function for the preference directory check/creation
7197 * so that duplication can be avoided with filter.c
7200 if (pf_path_return != NULL) {
7201 pf_path = get_persconffile_path(PF_NAME, true);
7202 if ((pf = ws_fopen(pf_path, "w")) == NULL) {
7203 *pf_path_return = pf_path;
7204 return errno;
7206 g_free(pf_path);
7207 } else {
7208 pf = stdout;
7212 * If the preferences file is being written, be sure to write UAT files
7213 * first that were migrated from the preferences file.
7215 if (pf_path_return != NULL) {
7216 if (prefs.filter_expressions_old) {
7217 char *err = NULL;
7218 prefs.filter_expressions_old = false;
7219 if (!uat_save(uat_get_table_by_name("Display expressions"), &err)) {
7220 ws_warning("Unable to save Display expressions: %s", err);
7221 g_free(err);
7225 module_t *extcap_module = prefs_find_module("extcap");
7226 if (extcap_module && !prefs.capture_no_extcap) {
7227 char *ext_path = get_persconffile_path("extcap.cfg", true);
7228 FILE *extf;
7229 if ((extf = ws_fopen(ext_path, "w")) == NULL) {
7230 if (errno != EISDIR) {
7231 ws_warning("Unable to save extcap preferences \"%s\": %s",
7232 ext_path, g_strerror(errno));
7234 g_free(ext_path);
7235 } else {
7236 g_free(ext_path);
7238 fputs("# Extcap configuration file for Wireshark " VERSION ".\n"
7239 "#\n"
7240 "# This file is regenerated each time preferences are saved within\n"
7241 "# Wireshark. Making manual changes should be safe, however.\n"
7242 "# Preferences that have been commented out have not been\n"
7243 "# changed from their default value.\n", extf);
7245 write_gui_pref_info.pf = extf;
7246 write_gui_pref_info.is_gui_module = false;
7248 write_module_prefs(extcap_module, &write_gui_pref_info);
7250 fclose(extf);
7255 fputs("# Configuration file for Wireshark " VERSION ".\n"
7256 "#\n"
7257 "# This file is regenerated each time preferences are saved within\n"
7258 "# Wireshark. Making manual changes should be safe, however.\n"
7259 "# Preferences that have been commented out have not been\n"
7260 "# changed from their default value.\n", pf);
7263 * For "backwards compatibility" the GUI module is written first as it's
7264 * at the top of the file. This is followed by all modules that can't
7265 * fit into the preferences read/write API. Finally the remaining modules
7266 * are written in alphabetical order (including of course the protocol preferences)
7268 write_gui_pref_info.pf = pf;
7269 write_gui_pref_info.is_gui_module = true;
7271 write_module_prefs(gui_module, &write_gui_pref_info);
7273 write_gui_pref_info.is_gui_module = false;
7274 prefs_modules_foreach_submodules(NULL, write_module_prefs, &write_gui_pref_info);
7276 fclose(pf);
7278 /* XXX - catch I/O errors (e.g. "ran out of disk space") and return
7279 an error indication, or maybe write to a new preferences file and
7280 rename that file on top of the old one only if there are not I/O
7281 errors. */
7282 return 0;
7285 /** The col_list is only partly managed by the custom preference API
7286 * because its data is shared between multiple preferences, so
7287 * it's freed here
7289 static void
7290 free_col_info(GList *list)
7292 fmt_data *cfmt;
7293 GList *list_head = list;
7295 while (list != NULL) {
7296 cfmt = (fmt_data *)list->data;
7298 g_free(cfmt->title);
7299 g_free(cfmt->custom_fields);
7300 g_free(cfmt);
7301 list = g_list_next(list);
7303 g_list_free(list_head);
7307 * Editor modelines
7309 * Local Variables:
7310 * c-basic-offset: 4
7311 * tab-width: 8
7312 * indent-tabs-mode: nil
7313 * End:
7315 * ex: set shiftwidth=4 tabstop=8 expandtab:
7316 * :indentSize=4:tabSize=8:noTabs=true: