dcerpc SPLIT auth_session_key
[wireshark-sm.git] / epan / prefs.c
blob3808a63d4a0b754d0332d92b4974fb01156cd827
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 static int num_capture_cols = 7;
200 static const char *capture_cols[7] = {
201 "INTERFACE",
202 "LINK",
203 "PMODE",
204 "SNAPLEN",
205 "MONITOR",
206 "BUFFER",
207 "FILTER"
209 #define CAPTURE_COL_TYPE_DESCRIPTION \
210 "Possible values: INTERFACE, LINK, PMODE, SNAPLEN, MONITOR, BUFFER, FILTER\n"
212 static const enum_val_t gui_packet_list_elide_mode[] = {
213 {"LEFT", "LEFT", ELIDE_LEFT},
214 {"RIGHT", "RIGHT", ELIDE_RIGHT},
215 {"MIDDLE", "MIDDLE", ELIDE_MIDDLE},
216 {"NONE", "NONE", ELIDE_NONE},
217 {NULL, NULL, -1}
220 /** Struct to hold preference data */
221 struct preference {
222 const char *name; /**< name of preference */
223 const char *title; /**< title to use in GUI */
224 const char *description; /**< human-readable description of preference */
225 int ordinal; /**< ordinal number of this preference */
226 int type; /**< type of that preference */
227 unsigned int effect_flags; /**< Flags of types effected by preference (PREF_TYPE_DISSECTION, PREF_EFFECT_CAPTURE, etc).
228 Flags must be non-zero to ensure saving to disk */
229 union { /* The Qt preference code assumes that these will all be pointers (and unique) */
230 unsigned *uint;
231 bool *boolp;
232 int *enump;
233 char **string;
234 range_t **range;
235 struct epan_uat* uat;
236 color_t *colorp;
237 GList** list;
238 } varp; /**< pointer to variable storing the value */
239 union {
240 unsigned uint;
241 bool boolval;
242 int enumval;
243 char *string;
244 range_t *range;
245 color_t color;
246 GList* list;
247 } stashed_val; /**< original value, when editing from the GUI */
248 union {
249 unsigned uint;
250 bool boolval;
251 int enumval;
252 char *string;
253 range_t *range;
254 color_t color;
255 GList* list;
256 } default_val; /**< the default value of the preference */
257 union {
258 unsigned base; /**< input/output base, for PREF_UINT */
259 uint32_t max_value; /**< maximum value of a range */
260 struct {
261 const enum_val_t *enumvals; /**< list of name & values */
262 bool radio_buttons; /**< true if it should be shown as
263 radio buttons rather than as an
264 option menu or combo box in
265 the preferences tab */
266 } enum_info; /**< for PREF_ENUM */
267 } info; /**< display/text file information */
268 struct pref_custom_cbs custom_cbs; /**< for PREF_CUSTOM */
269 const char *dissector_table; /**< for PREF_DECODE_AS_RANGE */
270 const char *dissector_desc; /**< for PREF_DECODE_AS_RANGE */
273 const char* prefs_get_description(pref_t *pref)
275 return pref->description;
278 const char* prefs_get_title(pref_t *pref)
280 return pref->title;
283 int prefs_get_type(pref_t *pref)
285 return pref->type;
288 const char* prefs_get_name(pref_t *pref)
290 return pref->name;
293 uint32_t prefs_get_max_value(pref_t *pref)
295 return pref->info.max_value;
298 const char* prefs_get_dissector_table(pref_t *pref)
300 return pref->dissector_table;
303 static const char* prefs_get_dissector_description(pref_t *pref)
305 return pref->dissector_desc;
309 * List of all modules with preference settings.
311 static wmem_tree_t *prefs_modules;
314 * List of all modules that should show up at the top level of the
315 * tree in the preference dialog box.
317 static wmem_tree_t *prefs_top_level_modules;
320 * List of aliases for modules.
322 static wmem_tree_t *prefs_module_aliases;
324 /** Sets up memory used by proto routines. Called at program startup */
325 void
326 prefs_init(void)
328 memset(&prefs, 0, sizeof(prefs));
329 prefs_modules = wmem_tree_new(wmem_epan_scope());
330 prefs_top_level_modules = wmem_tree_new(wmem_epan_scope());
331 prefs_module_aliases = wmem_tree_new(wmem_epan_scope());
335 * Free the strings for a string-like preference.
337 static void
338 free_string_like_preference(pref_t *pref)
340 g_free(*pref->varp.string);
341 *pref->varp.string = NULL;
342 g_free(pref->default_val.string);
343 pref->default_val.string = NULL;
346 static void
347 free_pref(void *data, void *user_data _U_)
349 pref_t *pref = (pref_t *)data;
350 int type = pref->type;
352 /* we reset the PREF_OBSOLETE bit in order to allow the original preference to be freed */
353 RESET_PREF_OBSOLETE(type);
355 switch (type) {
356 case PREF_BOOL:
357 case PREF_ENUM:
358 case PREF_UINT:
359 case PREF_STATIC_TEXT:
360 case PREF_UAT:
361 case PREF_COLOR:
362 break;
363 case PREF_STRING:
364 case PREF_SAVE_FILENAME:
365 case PREF_OPEN_FILENAME:
366 case PREF_DIRNAME:
367 case PREF_PASSWORD:
368 case PREF_DISSECTOR:
369 free_string_like_preference(pref);
370 break;
371 case PREF_RANGE:
372 case PREF_DECODE_AS_RANGE:
373 wmem_free(wmem_epan_scope(), *pref->varp.range);
374 *pref->varp.range = NULL;
375 wmem_free(wmem_epan_scope(), pref->default_val.range);
376 pref->default_val.range = NULL;
377 break;
378 case PREF_CUSTOM:
379 if (strcmp(pref->name, "columns") == 0)
380 pref->stashed_val.boolval = true;
381 pref->custom_cbs.free_cb(pref);
382 break;
383 /* non-generic preferences */
384 case PREF_PROTO_TCP_SNDAMB_ENUM:
385 break;
388 g_free(pref);
391 static unsigned
392 free_module_prefs(module_t *module, void *data _U_)
394 if (module->prefs) {
395 g_list_foreach(module->prefs, free_pref, NULL);
396 g_list_free(module->prefs);
398 module->prefs = NULL;
399 module->numprefs = 0;
400 if (module->submodules) {
401 prefs_module_list_foreach(module->submodules, free_module_prefs, NULL, false);
403 /* We don't free the actual module: its submodules pointer points to
404 a wmem_tree and the module itself is stored in a wmem_tree
407 return 0;
410 /** Frees memory used by proto routines. Called at program shutdown */
411 void
412 prefs_cleanup(void)
414 /* This isn't strictly necessary since we're exiting anyway, but let's
415 * do what clean up we can.
417 prefs_module_list_foreach(prefs_modules, free_module_prefs, NULL, false);
419 /* Clean the uats */
420 uat_cleanup();
422 /* Shut down mmdbresolve */
423 maxmind_db_pref_cleanup();
425 g_free(prefs.saved_at_version);
426 g_free(gpf_path);
427 gpf_path = NULL;
430 void prefs_set_gui_theme_is_dark(bool is_dark)
432 gui_theme_is_dark = is_dark;
436 * Register a module that will have preferences.
437 * Specify the module under which to register it or NULL to register it
438 * at the top level, the name used for the module in the preferences file,
439 * the title used in the tab for it in a preferences dialog box, and a
440 * routine to call back when we apply the preferences.
442 static module_t *
443 prefs_register_module(module_t *parent, const char *name, const char *title,
444 const char *description, const char *help, void (*apply_cb)(void),
445 const bool use_gui)
447 return prefs_register_module_or_subtree(parent, name, title, description, help,
448 false, apply_cb, use_gui);
451 static void
452 prefs_deregister_module(module_t *parent, const char *name, const char *title)
454 /* Remove this module from the list of all modules */
455 module_t *module = (module_t *)wmem_tree_remove_string(prefs_modules, name, WMEM_TREE_STRING_NOCASE);
457 if (!module)
458 return;
460 if (parent == NULL) {
461 /* Remove from top */
462 wmem_tree_remove_string(prefs_top_level_modules, title, WMEM_TREE_STRING_NOCASE);
463 } else if (parent->submodules) {
464 /* Remove from parent */
465 wmem_tree_remove_string(parent->submodules, title, WMEM_TREE_STRING_NOCASE);
468 free_module_prefs(module, NULL);
469 wmem_free(wmem_epan_scope(), module);
473 * Register a subtree that will have modules under it.
474 * Specify the module under which to register it or NULL to register it
475 * at the top level and the title used in the tab for it in a preferences
476 * dialog box.
478 static module_t *
479 prefs_register_subtree(module_t *parent, const char *title, const char *description,
480 void (*apply_cb)(void))
482 return prefs_register_module_or_subtree(parent, NULL, title, description, NULL,
483 true, apply_cb,
484 parent ? parent->use_gui : false);
487 static module_t *
488 prefs_register_module_or_subtree(module_t *parent, const char *name,
489 const char *title, const char *description,
490 const char *help,
491 bool is_subtree, void (*apply_cb)(void),
492 bool use_gui)
494 module_t *module;
496 /* this module may have been created as a subtree item previously */
497 if ((module = find_subtree(parent, title))) {
498 /* the module is currently a subtree */
499 module->name = name;
500 module->apply_cb = apply_cb;
501 module->description = description;
502 module->help = help;
504 if (prefs_find_module(name) == NULL) {
505 wmem_tree_insert_string(prefs_modules, name, module,
506 WMEM_TREE_STRING_NOCASE);
509 return module;
512 module = wmem_new(wmem_epan_scope(), module_t);
513 module->name = name;
514 module->title = title;
515 module->description = description;
516 module->help = help;
517 module->apply_cb = apply_cb;
518 module->prefs = NULL; /* no preferences, to start */
519 module->parent = parent;
520 module->submodules = NULL; /* no submodules, to start */
521 module->numprefs = 0;
522 module->prefs_changed_flags = 0;
523 module->obsolete = false;
524 module->use_gui = use_gui;
525 /* A module's preferences affects dissection unless otherwise told */
526 module->effect_flags = PREF_EFFECT_DISSECTION;
529 * Do we have a module name?
531 if (name != NULL) {
533 /* Accept any letter case to conform with protocol names. ASN1 protocols
534 * don't use lower case names, so we can't require lower case. */
535 if (module_check_valid_name(name, false) != '\0') {
536 ws_error("Preference module \"%s\" contains invalid characters", name);
540 * Make sure there's not already a module with that
541 * name. Crash if there is, as that's an error in the
542 * code, and the code has to be fixed not to register
543 * more than one module with the same name.
545 * We search the list of all modules; the subtree stuff
546 * doesn't require preferences in subtrees to have names
547 * that reflect the subtree they're in (that would require
548 * protocol preferences to have a bogus "protocol.", or
549 * something such as that, to be added to all their names).
551 if (prefs_find_module(name) != NULL)
552 ws_error("Preference module \"%s\" is being registered twice", name);
555 * Insert this module in the list of all modules.
557 wmem_tree_insert_string(prefs_modules, name, module, WMEM_TREE_STRING_NOCASE);
558 } else {
560 * This has no name, just a title; check to make sure it's a
561 * subtree, and crash if it's not.
563 if (!is_subtree)
564 ws_error("Preferences module with no name is being registered at the top level");
568 * Insert this module into the appropriate place in the display
569 * tree.
571 if (parent == NULL) {
573 * It goes at the top.
575 wmem_tree_insert_string(prefs_top_level_modules, title, module, WMEM_TREE_STRING_NOCASE);
576 } else {
578 * It goes into the list for this module.
581 if (parent->submodules == NULL)
582 parent->submodules = wmem_tree_new(wmem_epan_scope());
584 wmem_tree_insert_string(parent->submodules, title, module, WMEM_TREE_STRING_NOCASE);
587 return module;
590 void
591 prefs_register_module_alias(const char *name, module_t *module)
593 module_alias_t *alias;
596 * Accept any name that can occur in protocol names. We allow upper-case
597 * letters, to handle the Diameter dissector having used "Diameter" rather
598 * than "diameter" as its preference module name in the past.
600 * Crash if the name is invalid, as that's an error in the code, but the name
601 * can be used on the command line, and shouldn't require quoting, etc.
603 if (module_check_valid_name(name, false) != '\0') {
604 ws_error("Preference module alias \"%s\" contains invalid characters", name);
608 * Make sure there's not already an alias with that
609 * name. Crash if there is, as that's an error in the
610 * code, and the code has to be fixed not to register
611 * more than one alias with the same name.
613 * We search the list of all aliases.
615 if (prefs_find_module_alias(name) != NULL)
616 ws_error("Preference module alias \"%s\" is being registered twice", name);
618 alias = wmem_new(wmem_epan_scope(), module_alias_t);
619 alias->name = name;
620 alias->module = module;
623 * Insert this module in the list of all modules.
625 wmem_tree_insert_string(prefs_module_aliases, name, alias, WMEM_TREE_STRING_NOCASE);
629 * Register that a protocol has preferences.
631 module_t *protocols_module;
633 module_t *
634 prefs_register_protocol(int id, void (*apply_cb)(void))
636 protocol_t *protocol;
639 * Have we yet created the "Protocols" subtree?
641 if (protocols_module == NULL) {
643 * No. Register Protocols subtree as well as any preferences
644 * for non-dissector modules.
646 pre_init_prefs();
647 prefs_register_modules();
649 protocol = find_protocol_by_id(id);
650 if (protocol == NULL)
651 ws_error("Protocol preferences being registered with an invalid protocol ID");
652 return prefs_register_module(protocols_module,
653 proto_get_protocol_filter_name(id),
654 proto_get_protocol_short_name(protocol),
655 proto_get_protocol_name(id), NULL, apply_cb, true);
658 void
659 prefs_deregister_protocol (int id)
661 protocol_t *protocol = find_protocol_by_id(id);
662 if (protocol == NULL)
663 ws_error("Protocol preferences being de-registered with an invalid protocol ID");
664 prefs_deregister_module (protocols_module,
665 proto_get_protocol_filter_name(id),
666 proto_get_protocol_short_name(protocol));
669 module_t *
670 prefs_register_protocol_subtree(const char *subtree, int id, void (*apply_cb)(void))
672 protocol_t *protocol;
673 module_t *subtree_module;
674 module_t *new_module;
675 char *sep = NULL, *ptr = NULL, *orig = NULL;
678 * Have we yet created the "Protocols" subtree?
679 * XXX - can we just do this by registering Protocols/{subtree}?
680 * If not, why not?
682 if (protocols_module == NULL) {
684 * No. Register Protocols subtree as well as any preferences
685 * for non-dissector modules.
687 pre_init_prefs();
688 prefs_register_modules();
691 subtree_module = protocols_module;
693 if (subtree) {
694 /* take a copy of the buffer, orig keeps a base pointer while ptr
695 * walks through the string */
696 orig = ptr = g_strdup(subtree);
698 while (ptr && *ptr) {
700 if ((sep = strchr(ptr, '/')))
701 *sep++ = '\0';
703 if (!(new_module = find_subtree(subtree_module, ptr))) {
705 * There's no such module; create it, with the description
706 * being the name (if it's later registered explicitly
707 * with a description, that will override it).
709 ptr = wmem_strdup(wmem_epan_scope(), ptr);
710 new_module = prefs_register_subtree(subtree_module, ptr, ptr, NULL);
713 subtree_module = new_module;
714 ptr = sep;
718 g_free(orig);
721 protocol = find_protocol_by_id(id);
722 if (protocol == NULL)
723 ws_error("Protocol subtree being registered with an invalid protocol ID");
724 return prefs_register_module(subtree_module,
725 proto_get_protocol_filter_name(id),
726 proto_get_protocol_short_name(protocol),
727 proto_get_protocol_name(id), NULL, apply_cb, true);
732 * Register that a protocol used to have preferences but no longer does,
733 * by creating an "obsolete" module for it.
735 module_t *
736 prefs_register_protocol_obsolete(int id)
738 module_t *module;
739 protocol_t *protocol;
742 * Have we yet created the "Protocols" subtree?
744 if (protocols_module == NULL) {
746 * No. Register Protocols subtree as well as any preferences
747 * for non-dissector modules.
749 pre_init_prefs();
750 prefs_register_modules();
752 protocol = find_protocol_by_id(id);
753 if (protocol == NULL)
754 ws_error("Protocol being registered with an invalid protocol ID");
755 module = prefs_register_module(protocols_module,
756 proto_get_protocol_filter_name(id),
757 proto_get_protocol_short_name(protocol),
758 proto_get_protocol_name(id), NULL, NULL, true);
759 module->obsolete = true;
760 return module;
764 * Register that a statistical tap has preferences.
766 * "name" is a name for the tap to use on the command line with "-o"
767 * and in preference files.
769 * "title" is a short human-readable name for the tap.
771 * "description" is a longer human-readable description of the tap.
773 module_t *stats_module;
775 module_t *
776 prefs_register_stat(const char *name, const char *title,
777 const char *description, void (*apply_cb)(void))
780 * Have we yet created the "Statistics" subtree?
782 if (stats_module == NULL) {
784 * No. Register Statistics subtree as well as any preferences
785 * for non-dissector modules.
787 pre_init_prefs();
788 prefs_register_modules();
791 return prefs_register_module(stats_module, name, title, description, NULL,
792 apply_cb, true);
796 * Register that a codec has preferences.
798 * "name" is a name for the codec to use on the command line with "-o"
799 * and in preference files.
801 * "title" is a short human-readable name for the codec.
803 * "description" is a longer human-readable description of the codec.
805 module_t *codecs_module;
807 module_t *
808 prefs_register_codec(const char *name, const char *title,
809 const char *description, void (*apply_cb)(void))
812 * Have we yet created the "Codecs" subtree?
814 if (codecs_module == NULL) {
816 * No. Register Codecs subtree as well as any preferences
817 * for non-dissector modules.
819 pre_init_prefs();
820 prefs_register_modules();
823 return prefs_register_module(codecs_module, name, title, description, NULL,
824 apply_cb, true);
827 module_t *
828 prefs_find_module(const char *name)
830 return (module_t *)wmem_tree_lookup_string(prefs_modules, name, WMEM_TREE_STRING_NOCASE);
833 static module_t *
834 find_subtree(module_t *parent, const char *name)
836 return (module_t *)wmem_tree_lookup_string(parent ? parent->submodules : prefs_top_level_modules, name, WMEM_TREE_STRING_NOCASE);
840 * Call a callback function, with a specified argument, for each module
841 * in a list of modules. If the list is NULL, searches the top-level
842 * list in the display tree of modules. If any callback returns a
843 * non-zero value, we stop and return that value, otherwise we
844 * return 0.
846 * Normally "obsolete" modules are ignored; their sole purpose is to allow old
847 * preferences for dissectors that no longer have preferences to be
848 * silently ignored in preference files. Does not ignore subtrees,
849 * as this can be used when walking the display tree of modules.
852 typedef struct {
853 module_cb callback;
854 void *user_data;
855 unsigned ret;
856 bool skip_obsolete;
857 } call_foreach_t;
859 static bool
860 call_foreach_cb(const void *key _U_, void *value, void *data)
862 module_t *module = (module_t*)value;
863 call_foreach_t *call_data = (call_foreach_t*)data;
865 if (!call_data->skip_obsolete || !module->obsolete)
866 call_data->ret = (*call_data->callback)(module, call_data->user_data);
868 return (call_data->ret != 0);
871 static unsigned
872 prefs_module_list_foreach(wmem_tree_t *module_list, module_cb callback,
873 void *user_data, bool skip_obsolete)
875 call_foreach_t call_data;
877 if (module_list == NULL)
878 module_list = prefs_top_level_modules;
880 call_data.callback = callback;
881 call_data.user_data = user_data;
882 call_data.ret = 0;
883 call_data.skip_obsolete = skip_obsolete;
884 wmem_tree_foreach(module_list, call_foreach_cb, &call_data);
885 return call_data.ret;
889 * Returns true if module has any submodules
891 bool
892 prefs_module_has_submodules(module_t *module)
894 if (module->submodules == NULL) {
895 return false;
898 if (wmem_tree_is_empty(module->submodules)) {
899 return false;
902 return true;
906 * Call a callback function, with a specified argument, for each module
907 * in the list of all modules. (This list does not include subtrees.)
909 * Ignores "obsolete" modules; their sole purpose is to allow old
910 * preferences for dissectors that no longer have preferences to be
911 * silently ignored in preference files.
913 unsigned
914 prefs_modules_foreach(module_cb callback, void *user_data)
916 return prefs_module_list_foreach(prefs_modules, callback, user_data, true);
920 * Call a callback function, with a specified argument, for each submodule
921 * of specified modules. If the module is NULL, goes through the top-level
922 * list in the display tree of modules.
924 * Ignores "obsolete" modules; their sole purpose is to allow old
925 * preferences for dissectors that no longer have preferences to be
926 * silently ignored in preference files. Does not ignore subtrees,
927 * as this can be used when walking the display tree of modules.
929 unsigned
930 prefs_modules_foreach_submodules(module_t *module, module_cb callback,
931 void *user_data)
933 return prefs_module_list_foreach((module)?module->submodules:prefs_top_level_modules, callback, user_data, true);
936 static bool
937 call_apply_cb(const void *key _U_, void *value, void *data _U_)
939 module_t *module = (module_t *)value;
941 if (module->obsolete)
942 return false;
943 if (module->prefs_changed_flags) {
944 if (module->apply_cb != NULL)
945 (*module->apply_cb)();
946 module->prefs_changed_flags = 0;
948 if (module->submodules)
949 wmem_tree_foreach(module->submodules, call_apply_cb, NULL);
950 return false;
954 * Call the "apply" callback function for each module if any of its
955 * preferences have changed, and then clear the flag saying its
956 * preferences have changed, as the module has been notified of that
957 * fact.
959 void
960 prefs_apply_all(void)
962 wmem_tree_foreach(prefs_modules, call_apply_cb, NULL);
966 * Call the "apply" callback function for a specific module if any of
967 * its preferences have changed, and then clear the flag saying its
968 * preferences have changed, as the module has been notified of that
969 * fact.
971 void
972 prefs_apply(module_t *module)
974 if (module && module->prefs_changed_flags)
975 call_apply_cb(NULL, module, NULL);
978 static module_t *
979 prefs_find_module_alias(const char *name)
981 module_alias_t *alias;
983 alias = (module_alias_t *)wmem_tree_lookup_string(prefs_module_aliases, name, WMEM_TREE_STRING_NOCASE);
984 if (alias == NULL)
985 return NULL;
986 return alias->module;
990 * Register a preference in a module's list of preferences.
991 * If it has a title, give it an ordinal number; otherwise, it's a
992 * preference that won't show up in the UI, so it shouldn't get an
993 * ordinal number (the ordinal should be the ordinal in the set of
994 * *visible* preferences).
996 static pref_t *
997 register_preference(module_t *module, const char *name, const char *title,
998 const char *description, int type)
1000 pref_t *preference;
1001 const char *p;
1002 const char *name_prefix = (module->name != NULL) ? module->name : module->parent->name;
1004 preference = g_new(pref_t,1);
1005 preference->name = name;
1006 preference->title = title;
1007 preference->description = description;
1008 preference->type = type;
1009 /* Default to module's preference effects */
1010 preference->effect_flags = module->effect_flags;
1012 if (title != NULL)
1013 preference->ordinal = module->numprefs;
1014 else
1015 preference->ordinal = -1; /* no ordinal for you */
1018 * Make sure that only lower-case ASCII letters, numbers,
1019 * underscores, and dots appear in the preference name.
1021 * Crash if there is, as that's an error in the code;
1022 * you can make the title and description nice strings
1023 * with capitalization, white space, punctuation, etc.,
1024 * but the name can be used on the command line,
1025 * and shouldn't require quoting, shifting, etc.
1027 for (p = name; *p != '\0'; p++)
1028 if (!(g_ascii_islower(*p) || g_ascii_isdigit(*p) || *p == '_' || *p == '.'))
1029 ws_error("Preference \"%s.%s\" contains invalid characters", module->name, name);
1032 * Make sure there's not already a preference with that
1033 * name. Crash if there is, as that's an error in the
1034 * code, and the code has to be fixed not to register
1035 * more than one preference with the same name.
1037 if (prefs_find_preference(module, name) != NULL)
1038 ws_error("Preference %s has already been registered", name);
1040 if ((!IS_PREF_OBSOLETE(type)) &&
1041 /* Don't compare if it's a subtree */
1042 (module->name != NULL)) {
1044 * Make sure the preference name doesn't begin with the
1045 * module name, as that's redundant and Just Silly.
1047 if (!((strncmp(name, module->name, strlen(module->name)) != 0) ||
1048 (((name[strlen(module->name)]) != '.') && ((name[strlen(module->name)]) != '_'))))
1049 ws_error("Preference %s begins with the module name", name);
1052 /* The title shows up in the preferences dialog. Make sure it's UI-friendly. */
1053 if (preference->title) {
1054 const char *cur_char;
1055 if (preference->type != PREF_STATIC_TEXT && g_utf8_strlen(preference->title, -1) > 80) { // Arbitrary.
1056 ws_error("Title for preference %s.%s is too long: %s", name_prefix, preference->name, preference->title);
1059 if (!g_utf8_validate(preference->title, -1, NULL)) {
1060 ws_error("Title for preference %s.%s isn't valid UTF-8.", name_prefix, preference->name);
1063 for (cur_char = preference->title; *cur_char; cur_char = g_utf8_next_char(cur_char)) {
1064 if (!g_unichar_isprint(g_utf8_get_char(cur_char))) {
1065 ws_error("Title for preference %s.%s isn't printable UTF-8.", name_prefix, preference->name);
1070 if (preference->description) {
1071 if (!g_utf8_validate(preference->description, -1, NULL)) {
1072 ws_error("Description for preference %s.%s isn't valid UTF-8.", name_prefix, preference->name);
1077 * We passed all of our checks. Add the preference.
1079 module->prefs = g_list_append(module->prefs, preference);
1080 if (title != NULL)
1081 module->numprefs++;
1083 return preference;
1087 * Find a preference in a module's list of preferences, given the module
1088 * and the preference's name.
1090 typedef struct {
1091 GList *list_entry;
1092 const char *name;
1093 module_t *submodule;
1094 } find_pref_arg_t;
1096 static int
1097 preference_match(const void *a, const void *b)
1099 const pref_t *pref = (const pref_t *)a;
1100 const char *name = (const char *)b;
1102 return strcmp(name, pref->name);
1105 static bool
1106 module_find_pref_cb(const void *key _U_, void *value, void *data)
1108 find_pref_arg_t* arg = (find_pref_arg_t*)data;
1109 GList *list_entry;
1110 module_t *module = (module_t *)value;
1112 if (module == NULL)
1113 return false;
1115 list_entry = g_list_find_custom(module->prefs, arg->name,
1116 preference_match);
1118 if (list_entry == NULL)
1119 return false;
1121 arg->list_entry = list_entry;
1122 arg->submodule = module;
1123 return true;
1126 /* Tries to find a preference, setting containing_module to the (sub)module
1127 * holding this preference. */
1128 static pref_t *
1129 prefs_find_preference_with_submodule(module_t *module, const char *name,
1130 module_t **containing_module)
1132 find_pref_arg_t arg;
1133 GList *list_entry;
1135 if (module == NULL)
1136 return NULL; /* invalid parameters */
1138 list_entry = g_list_find_custom(module->prefs, name,
1139 preference_match);
1140 arg.submodule = NULL;
1142 if (list_entry == NULL)
1144 arg.list_entry = NULL;
1145 if (module->submodules != NULL)
1147 arg.name = name;
1148 wmem_tree_foreach(module->submodules, module_find_pref_cb, &arg);
1151 list_entry = arg.list_entry;
1154 if (list_entry == NULL)
1155 return NULL; /* no such preference */
1157 if (containing_module)
1158 *containing_module = arg.submodule ? arg.submodule : module;
1160 return (pref_t *) list_entry->data;
1163 pref_t *
1164 prefs_find_preference(module_t *module, const char *name)
1166 return prefs_find_preference_with_submodule(module, name, NULL);
1170 * Returns true if the given protocol has registered preferences
1172 bool
1173 prefs_is_registered_protocol(const char *name)
1175 module_t *m = prefs_find_module(name);
1177 return (m != NULL && !m->obsolete);
1181 * Returns the module title of a registered protocol
1183 const char *
1184 prefs_get_title_by_name(const char *name)
1186 module_t *m = prefs_find_module(name);
1188 return (m != NULL && !m->obsolete) ? m->title : NULL;
1192 * Register a preference with an unsigned integral value.
1194 void
1195 prefs_register_uint_preference(module_t *module, const char *name,
1196 const char *title, const char *description,
1197 unsigned base, unsigned *var)
1199 pref_t *preference;
1201 preference = register_preference(module, name, title, description,
1202 PREF_UINT);
1203 preference->varp.uint = var;
1204 preference->default_val.uint = *var;
1205 ws_assert(base > 0 && base != 1 && base < 37);
1206 preference->info.base = base;
1210 * XXX Add a prefs_register_{uint16|port}_preference which sets max_value?
1215 * Register a "custom" preference with a unsigned integral value.
1216 * XXX - This should be temporary until we can find a better way
1217 * to do "custom" preferences
1219 static void
1220 prefs_register_uint_custom_preference(module_t *module, const char *name,
1221 const char *title, const char *description,
1222 struct pref_custom_cbs* custom_cbs, unsigned *var)
1224 pref_t *preference;
1226 preference = register_preference(module, name, title, description,
1227 PREF_CUSTOM);
1229 preference->custom_cbs = *custom_cbs;
1230 preference->varp.uint = var;
1231 preference->default_val.uint = *var;
1235 * Register a preference with an Boolean value.
1237 void
1238 prefs_register_bool_preference(module_t *module, const char *name,
1239 const char *title, const char *description,
1240 bool *var)
1242 pref_t *preference;
1244 preference = register_preference(module, name, title, description,
1245 PREF_BOOL);
1246 preference->varp.boolp = var;
1247 preference->default_val.boolval = *var;
1250 unsigned int prefs_set_bool_value(pref_t *pref, bool value, pref_source_t source)
1252 unsigned int changed = 0;
1254 switch (source)
1256 case pref_default:
1257 if (pref->default_val.boolval != value) {
1258 pref->default_val.boolval = value;
1259 changed = prefs_get_effect_flags(pref);
1261 break;
1262 case pref_stashed:
1263 if (pref->stashed_val.boolval != value) {
1264 pref->stashed_val.boolval = value;
1265 changed = prefs_get_effect_flags(pref);
1267 break;
1268 case pref_current:
1269 if (*pref->varp.boolp != value) {
1270 *pref->varp.boolp = value;
1271 changed = prefs_get_effect_flags(pref);
1273 break;
1274 default:
1275 ws_assert_not_reached();
1276 break;
1279 return changed;
1282 void prefs_invert_bool_value(pref_t *pref, pref_source_t source)
1284 switch (source)
1286 case pref_default:
1287 pref->default_val.boolval = !pref->default_val.boolval;
1288 break;
1289 case pref_stashed:
1290 pref->stashed_val.boolval = !pref->stashed_val.boolval;
1291 break;
1292 case pref_current:
1293 *pref->varp.boolp = !(*pref->varp.boolp);
1294 break;
1295 default:
1296 ws_assert_not_reached();
1297 break;
1301 bool prefs_get_bool_value(pref_t *pref, pref_source_t source)
1303 switch (source)
1305 case pref_default:
1306 return pref->default_val.boolval;
1307 case pref_stashed:
1308 return pref->stashed_val.boolval;
1309 case pref_current:
1310 return *pref->varp.boolp;
1311 default:
1312 ws_assert_not_reached();
1313 break;
1316 return false;
1320 * Register a preference with an enumerated value.
1323 * XXX Should we get rid of the radio_buttons parameter and make that
1324 * behavior automatic depending on the number of items?
1326 void
1327 prefs_register_enum_preference(module_t *module, const char *name,
1328 const char *title, const char *description,
1329 int *var, const enum_val_t *enumvals,
1330 bool radio_buttons)
1332 pref_t *preference;
1334 /* Validate that the "name one would use on the command line for the value"
1335 * doesn't require quoting, etc. It's all treated case-insensitively so we
1336 * don't care about upper vs lower case.
1338 for (size_t i = 0; enumvals[i].name != NULL; i++) {
1339 for (const char *p = enumvals[i].name; *p != '\0'; p++)
1340 if (!(g_ascii_isalnum(*p) || *p == '_' || *p == '.' || *p == '-'))
1341 ws_error("Preference \"%s.%s\" enum value name \"%s\" contains invalid characters",
1342 module->name, name, enumvals[i].name);
1346 preference = register_preference(module, name, title, description,
1347 PREF_ENUM);
1348 preference->varp.enump = var;
1349 preference->default_val.enumval = *var;
1350 preference->info.enum_info.enumvals = enumvals;
1351 preference->info.enum_info.radio_buttons = radio_buttons;
1354 unsigned int prefs_set_enum_value(pref_t *pref, int value, pref_source_t source)
1356 unsigned int changed = 0;
1358 switch (source)
1360 case pref_default:
1361 if (pref->default_val.enumval != value) {
1362 pref->default_val.enumval = value;
1363 changed = prefs_get_effect_flags(pref);
1365 break;
1366 case pref_stashed:
1367 if (pref->stashed_val.enumval != value) {
1368 pref->stashed_val.enumval = value;
1369 changed = prefs_get_effect_flags(pref);
1371 break;
1372 case pref_current:
1373 if (*pref->varp.enump != value) {
1374 *pref->varp.enump = value;
1375 changed = prefs_get_effect_flags(pref);
1377 break;
1378 default:
1379 ws_assert_not_reached();
1380 break;
1383 return changed;
1386 unsigned int prefs_set_enum_string_value(pref_t *pref, const char *value, pref_source_t source)
1388 int enum_val = find_val_for_string(value, pref->info.enum_info.enumvals, *pref->varp.enump);
1390 return prefs_set_enum_value(pref, enum_val, source);
1393 int prefs_get_enum_value(pref_t *pref, pref_source_t source)
1395 switch (source)
1397 case pref_default:
1398 return pref->default_val.enumval;
1399 case pref_stashed:
1400 return pref->stashed_val.enumval;
1401 case pref_current:
1402 return *pref->varp.enump;
1403 default:
1404 ws_assert_not_reached();
1405 break;
1408 return 0;
1411 const enum_val_t* prefs_get_enumvals(pref_t *pref)
1413 return pref->info.enum_info.enumvals;
1416 bool prefs_get_enum_radiobuttons(pref_t *pref)
1418 return pref->info.enum_info.radio_buttons;
1422 * For use by UI code that sets preferences.
1424 unsigned int
1425 prefs_set_custom_value(pref_t *pref, const char *value, pref_source_t source _U_)
1427 /* XXX - support pref source for custom preferences */
1428 unsigned int changed = 0;
1429 pref->custom_cbs.set_cb(pref, value, &changed);
1430 return changed;
1433 static void
1434 register_string_like_preference(module_t *module, const char *name,
1435 const char *title, const char *description,
1436 char **var, int type,
1437 struct pref_custom_cbs* custom_cbs,
1438 bool free_tmp)
1440 pref_t *pref;
1441 char *tmp;
1443 pref = register_preference(module, name, title, description, type);
1446 * String preference values should be non-null (as you can't
1447 * keep them null after using the preferences GUI, you can at best
1448 * have them be null strings) and freeable (as we free them
1449 * if we change them).
1451 * If the value is a null pointer, make it a copy of a null
1452 * string, otherwise make it a copy of the value.
1454 tmp = *var;
1455 if (*var == NULL) {
1456 *var = g_strdup("");
1457 } else {
1458 *var = g_strdup(*var);
1460 if (free_tmp) {
1461 g_free(tmp);
1463 pref->varp.string = var;
1464 pref->default_val.string = g_strdup(*var);
1465 pref->stashed_val.string = NULL;
1466 if (type == PREF_CUSTOM) {
1467 ws_assert(custom_cbs);
1468 pref->custom_cbs = *custom_cbs;
1473 * Assign to a string preference.
1475 static void
1476 pref_set_string_like_pref_value(pref_t *pref, const char *value)
1478 DIAG_OFF(cast-qual)
1479 g_free((void *)*pref->varp.string);
1480 DIAG_ON(cast-qual)
1481 *pref->varp.string = g_strdup(value);
1485 * For use by UI code that sets preferences.
1487 unsigned int
1488 prefs_set_string_value(pref_t *pref, const char* value, pref_source_t source)
1490 unsigned int changed = 0;
1492 switch (source)
1494 case pref_default:
1495 if (*pref->default_val.string) {
1496 if (strcmp(pref->default_val.string, value) != 0) {
1497 changed = prefs_get_effect_flags(pref);
1498 g_free(pref->default_val.string);
1499 pref->default_val.string = g_strdup(value);
1501 } else if (value) {
1502 pref->default_val.string = g_strdup(value);
1504 break;
1505 case pref_stashed:
1506 if (pref->stashed_val.string) {
1507 if (strcmp(pref->stashed_val.string, value) != 0) {
1508 changed = prefs_get_effect_flags(pref);
1509 g_free(pref->stashed_val.string);
1510 pref->stashed_val.string = g_strdup(value);
1512 } else if (value) {
1513 pref->stashed_val.string = g_strdup(value);
1515 break;
1516 case pref_current:
1517 if (*pref->varp.string) {
1518 if (strcmp(*pref->varp.string, value) != 0) {
1519 changed = prefs_get_effect_flags(pref);
1520 pref_set_string_like_pref_value(pref, value);
1522 } else if (value) {
1523 pref_set_string_like_pref_value(pref, value);
1525 break;
1526 default:
1527 ws_assert_not_reached();
1528 break;
1531 return changed;
1534 char* prefs_get_string_value(pref_t *pref, pref_source_t source)
1536 switch (source)
1538 case pref_default:
1539 return pref->default_val.string;
1540 case pref_stashed:
1541 return pref->stashed_val.string;
1542 case pref_current:
1543 return *pref->varp.string;
1544 default:
1545 ws_assert_not_reached();
1546 break;
1549 return NULL;
1553 * Reset the value of a string-like preference.
1555 static void
1556 reset_string_like_preference(pref_t *pref)
1558 g_free(*pref->varp.string);
1559 *pref->varp.string = g_strdup(pref->default_val.string);
1563 * Register a preference with a character-string value.
1565 void
1566 prefs_register_string_preference(module_t *module, const char *name,
1567 const char *title, const char *description,
1568 const char **var)
1570 DIAG_OFF(cast-qual)
1571 register_string_like_preference(module, name, title, description,
1572 (char **)var, PREF_STRING, NULL, false);
1573 DIAG_ON(cast-qual)
1577 * Register a preference with a file name (string) value.
1579 void
1580 prefs_register_filename_preference(module_t *module, const char *name,
1581 const char *title, const char *description,
1582 const char **var, bool for_writing)
1584 DIAG_OFF(cast-qual)
1585 register_string_like_preference(module, name, title, description, (char **)var,
1586 for_writing ? PREF_SAVE_FILENAME : PREF_OPEN_FILENAME, NULL, false);
1587 DIAG_ON(cast-qual)
1591 * Register a preference with a directory name (string) value.
1593 void
1594 prefs_register_directory_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_DIRNAME, NULL, false);
1601 DIAG_ON(cast-qual)
1604 /* Refactoring to handle both PREF_RANGE and PREF_DECODE_AS_RANGE */
1605 static pref_t*
1606 prefs_register_range_preference_common(module_t *module, const char *name,
1607 const char *title, const char *description,
1608 range_t **var, uint32_t max_value, int type)
1610 pref_t *preference;
1612 preference = register_preference(module, name, title, description, type);
1613 preference->info.max_value = max_value;
1616 * Range preference values should be non-null (as you can't
1617 * keep them null after using the preferences GUI, you can at best
1618 * have them be empty ranges) and freeable (as we free them
1619 * if we change them).
1621 * If the value is a null pointer, make it an empty range.
1623 if (*var == NULL)
1624 *var = range_empty(wmem_epan_scope());
1625 preference->varp.range = var;
1626 preference->default_val.range = range_copy(wmem_epan_scope(), *var);
1627 preference->stashed_val.range = NULL;
1629 return preference;
1633 * Register a preference with a ranged value.
1635 void
1636 prefs_register_range_preference(module_t *module, const char *name,
1637 const char *title, const char *description,
1638 range_t **var, uint32_t max_value)
1640 prefs_register_range_preference_common(module, name, title,
1641 description, var, max_value, PREF_RANGE);
1644 bool
1645 prefs_set_range_value_work(pref_t *pref, const char *value,
1646 bool return_range_errors, unsigned int *changed_flags)
1648 range_t *newrange;
1650 if (range_convert_str_work(wmem_epan_scope(), &newrange, value, pref->info.max_value,
1651 return_range_errors) != CVT_NO_ERROR) {
1652 return false; /* number was bad */
1655 if (!ranges_are_equal(*pref->varp.range, newrange)) {
1656 *changed_flags |= prefs_get_effect_flags(pref);
1657 wmem_free(wmem_epan_scope(), *pref->varp.range);
1658 *pref->varp.range = newrange;
1659 } else {
1660 wmem_free(wmem_epan_scope(), newrange);
1662 return true;
1666 * For use by UI code that sets preferences.
1668 unsigned int
1669 prefs_set_stashed_range_value(pref_t *pref, const char *value)
1671 range_t *newrange;
1673 if (range_convert_str_work(wmem_epan_scope(), &newrange, value, pref->info.max_value,
1674 true) != CVT_NO_ERROR) {
1675 return 0; /* number was bad */
1678 if (!ranges_are_equal(pref->stashed_val.range, newrange)) {
1679 wmem_free(wmem_epan_scope(), pref->stashed_val.range);
1680 pref->stashed_val.range = newrange;
1681 } else {
1682 wmem_free(wmem_epan_scope(), newrange);
1684 return prefs_get_effect_flags(pref);
1688 bool prefs_add_list_value(pref_t *pref, void* value, pref_source_t source)
1690 switch (source)
1692 case pref_default:
1693 pref->default_val.list = g_list_prepend(pref->default_val.list, value);
1694 break;
1695 case pref_stashed:
1696 pref->stashed_val.list = g_list_prepend(pref->stashed_val.list, value);
1697 break;
1698 case pref_current:
1699 *pref->varp.list = g_list_prepend(*pref->varp.list, value);
1700 break;
1701 default:
1702 ws_assert_not_reached();
1703 break;
1706 return true;
1709 GList* prefs_get_list_value(pref_t *pref, pref_source_t source)
1711 switch (source)
1713 case pref_default:
1714 return pref->default_val.list;
1715 case pref_stashed:
1716 return pref->stashed_val.list;
1717 case pref_current:
1718 return *pref->varp.list;
1719 default:
1720 ws_assert_not_reached();
1721 break;
1724 return NULL;
1727 bool prefs_set_range_value(pref_t *pref, range_t *value, pref_source_t source)
1729 bool changed = false;
1731 switch (source)
1733 case pref_default:
1734 if (!ranges_are_equal(pref->default_val.range, value)) {
1735 wmem_free(wmem_epan_scope(), pref->default_val.range);
1736 pref->default_val.range = range_copy(wmem_epan_scope(), value);
1737 changed = true;
1739 break;
1740 case pref_stashed:
1741 if (!ranges_are_equal(pref->stashed_val.range, value)) {
1742 wmem_free(wmem_epan_scope(), pref->stashed_val.range);
1743 pref->stashed_val.range = range_copy(wmem_epan_scope(), value);
1744 changed = true;
1746 break;
1747 case pref_current:
1748 if (!ranges_are_equal(*pref->varp.range, value)) {
1749 wmem_free(wmem_epan_scope(), *pref->varp.range);
1750 *pref->varp.range = range_copy(wmem_epan_scope(), value);
1751 changed = true;
1753 break;
1754 default:
1755 ws_assert_not_reached();
1756 break;
1759 return changed;
1762 range_t* prefs_get_range_value_real(pref_t *pref, pref_source_t source)
1764 switch (source)
1766 case pref_default:
1767 return pref->default_val.range;
1768 case pref_stashed:
1769 return pref->stashed_val.range;
1770 case pref_current:
1771 return *pref->varp.range;
1772 default:
1773 ws_assert_not_reached();
1774 break;
1777 return NULL;
1780 range_t* prefs_get_range_value(const char *module_name, const char* pref_name)
1782 pref_t *pref = prefs_find_preference(prefs_find_module(module_name), pref_name);
1783 if (pref == NULL) {
1784 return NULL;
1786 return prefs_get_range_value_real(pref, pref_current);
1789 void
1790 prefs_range_add_value(pref_t *pref, uint32_t val)
1792 range_add_value(wmem_epan_scope(), pref->varp.range, val);
1795 void
1796 prefs_range_remove_value(pref_t *pref, uint32_t val)
1798 range_remove_value(wmem_epan_scope(), pref->varp.range, val);
1802 * Register a static text 'preference'. It can be used to add explanatory
1803 * text inline with other preferences in the GUI.
1804 * Note: Static preferences are not saved to the preferences file.
1806 void
1807 prefs_register_static_text_preference(module_t *module, const char *name,
1808 const char *title,
1809 const char *description)
1811 register_preference(module, name, title, description, PREF_STATIC_TEXT);
1815 * Register a uat 'preference'. It adds a button that opens the uat's window in the
1816 * preferences tab of the module.
1818 extern void
1819 prefs_register_uat_preference(module_t *module, const char *name,
1820 const char *title, const char *description,
1821 uat_t* uat)
1824 pref_t* preference = register_preference(module, name, title, description, PREF_UAT);
1826 preference->varp.uat = uat;
1830 * Register a uat 'preference' for QT only. It adds a button that opens the uat's window in the
1831 * preferences tab of the module.
1833 extern void
1834 prefs_register_uat_preference_qt(module_t *module, const char *name,
1835 const char *title, const char *description,
1836 uat_t* uat)
1839 pref_t* preference = register_preference(module, name, title, description, PREF_UAT);
1841 preference->varp.uat = uat;
1844 struct epan_uat* prefs_get_uat_value(pref_t *pref)
1846 return pref->varp.uat;
1850 * Register a color preference.
1852 void
1853 prefs_register_color_preference(module_t *module, const char *name,
1854 const char *title, const char *description,
1855 color_t *color)
1857 pref_t* preference = register_preference(module, name, title, description, PREF_COLOR);
1859 preference->varp.colorp = color;
1860 preference->default_val.color = *color;
1863 bool prefs_set_color_value(pref_t *pref, color_t value, pref_source_t source)
1865 bool changed = false;
1867 switch (source)
1869 case pref_default:
1870 if ((pref->default_val.color.red != value.red) ||
1871 (pref->default_val.color.green != value.green) ||
1872 (pref->default_val.color.blue != value.blue)) {
1873 changed = true;
1874 pref->default_val.color = value;
1876 break;
1877 case pref_stashed:
1878 if ((pref->stashed_val.color.red != value.red) ||
1879 (pref->stashed_val.color.green != value.green) ||
1880 (pref->stashed_val.color.blue != value.blue)) {
1881 changed = true;
1882 pref->stashed_val.color = value;
1884 break;
1885 case pref_current:
1886 if ((pref->varp.colorp->red != value.red) ||
1887 (pref->varp.colorp->green != value.green) ||
1888 (pref->varp.colorp->blue != value.blue)) {
1889 changed = true;
1890 *pref->varp.colorp = value;
1892 break;
1893 default:
1894 ws_assert_not_reached();
1895 break;
1898 return changed;
1901 color_t* prefs_get_color_value(pref_t *pref, pref_source_t source)
1903 switch (source)
1905 case pref_default:
1906 return &pref->default_val.color;
1907 case pref_stashed:
1908 return &pref->stashed_val.color;
1909 case pref_current:
1910 return pref->varp.colorp;
1911 default:
1912 ws_assert_not_reached();
1913 break;
1916 return NULL;
1920 * Register a "custom" preference with a list.
1921 * XXX - This should be temporary until we can find a better way
1922 * to do "custom" preferences
1924 typedef void (*pref_custom_list_init_cb) (pref_t* pref, GList** value);
1926 static void
1927 prefs_register_list_custom_preference(module_t *module, const char *name,
1928 const char *title, const char *description,
1929 struct pref_custom_cbs* custom_cbs,
1930 pref_custom_list_init_cb init_cb,
1931 GList** list)
1933 pref_t* preference = register_preference(module, name, title, description, PREF_CUSTOM);
1935 preference->custom_cbs = *custom_cbs;
1936 init_cb(preference, list);
1940 * Register a custom preference.
1942 void
1943 prefs_register_custom_preference(module_t *module, const char *name,
1944 const char *title, const char *description,
1945 struct pref_custom_cbs* custom_cbs,
1946 void **custom_data _U_)
1948 pref_t* preference = register_preference(module, name, title, description, PREF_CUSTOM);
1950 preference->custom_cbs = *custom_cbs;
1951 /* XXX - wait until we can handle void** pointers
1952 preference->custom_cbs.init_cb(preference, custom_data);
1957 * Register a dedicated TCP preference for SEQ analysis overriding.
1958 * This is similar to the data structure from enum preference, except
1959 * that when a preference dialog is used, the stashed value is the list
1960 * of frame data pointers whose sequence analysis override will be set
1961 * to the current value if the dialog is accepted.
1963 * We don't need to read or write the value from the preferences file
1964 * (or command line), because the override is reset to the default (0)
1965 * for each frame when a new capture file is loaded.
1967 void
1968 prefs_register_custom_preference_TCP_Analysis(module_t *module, const char *name,
1969 const char *title, const char *description,
1970 int *var, const enum_val_t *enumvals,
1971 bool radio_buttons)
1973 pref_t *preference;
1975 preference = register_preference(module, name, title, description,
1976 PREF_PROTO_TCP_SNDAMB_ENUM);
1977 preference->varp.enump = var;
1978 preference->default_val.enumval = *var;
1979 preference->stashed_val.list = NULL;
1980 preference->info.enum_info.enumvals = enumvals;
1981 preference->info.enum_info.radio_buttons = radio_buttons;
1985 * Register a (internal) "Decode As" preference with a ranged value.
1987 void prefs_register_decode_as_range_preference(module_t *module, const char *name,
1988 const char *title, const char *description, range_t **var,
1989 uint32_t max_value, const char *dissector_table, const char *dissector_description)
1991 pref_t *preference;
1993 preference = prefs_register_range_preference_common(module, name, title,
1994 description, var, max_value, PREF_DECODE_AS_RANGE);
1995 preference->dissector_desc = dissector_description;
1996 preference->dissector_table = dissector_table;
2000 * Register a preference with password value.
2002 void
2003 prefs_register_password_preference(module_t *module, const char *name,
2004 const char *title, const char *description,
2005 const char **var)
2007 DIAG_OFF(cast-qual)
2008 register_string_like_preference(module, name, title, description,
2009 (char **)var, PREF_PASSWORD, NULL, false);
2010 DIAG_ON(cast-qual)
2014 * Register a preference with a dissector name.
2016 void
2017 prefs_register_dissector_preference(module_t *module, const char *name,
2018 const char *title, const char *description,
2019 const char **var)
2021 DIAG_OFF(cast-qual)
2022 register_string_like_preference(module, name, title, description,
2023 (char **)var, PREF_DISSECTOR, NULL, false);
2024 DIAG_ON(cast-qual)
2027 bool prefs_add_decode_as_value(pref_t *pref, unsigned value, bool replace)
2029 switch(pref->type)
2031 case PREF_DECODE_AS_RANGE:
2032 if (replace)
2034 /* If range has single value, replace it */
2035 if (((*pref->varp.range)->nranges == 1) &&
2036 ((*pref->varp.range)->ranges[0].low == (*pref->varp.range)->ranges[0].high)) {
2037 wmem_free(wmem_epan_scope(), *pref->varp.range);
2038 *pref->varp.range = range_empty(wmem_epan_scope());
2042 prefs_range_add_value(pref, value);
2043 break;
2044 default:
2045 /* XXX - Worth asserting over? */
2046 break;
2049 return true;
2052 bool prefs_remove_decode_as_value(pref_t *pref, unsigned value, bool set_default _U_)
2054 switch(pref->type)
2056 case PREF_DECODE_AS_RANGE:
2057 /* XXX - We could set to the default if the value is the only one
2058 * in the range.
2060 prefs_range_remove_value(pref, value);
2061 break;
2062 default:
2063 break;
2066 return true;
2070 * Register a preference that used to be supported but no longer is.
2072 void
2073 prefs_register_obsolete_preference(module_t *module, const char *name)
2075 register_preference(module, name, NULL, NULL, PREF_OBSOLETE);
2078 void
2079 prefs_set_preference_effect_fields(module_t *module, const char *name)
2081 pref_t * pref = prefs_find_preference(module, name);
2082 if (pref) {
2083 prefs_set_effect_flags(pref, prefs_get_effect_flags(pref) | PREF_EFFECT_FIELDS);
2088 * Check to see if a preference is obsolete.
2090 extern bool
2091 prefs_get_preference_obsolete(pref_t *pref)
2093 if (pref)
2094 return (IS_PREF_OBSOLETE(pref->type) ? true : false);
2096 return true;
2100 * Make a preference obsolete.
2102 extern prefs_set_pref_e
2103 prefs_set_preference_obsolete(pref_t *pref)
2105 if (pref) {
2106 SET_PREF_OBSOLETE(pref->type);
2107 return PREFS_SET_OK;
2109 return PREFS_SET_NO_SUCH_PREF;
2112 unsigned
2113 pref_stash(pref_t *pref, void *unused _U_)
2115 switch (pref->type) {
2117 case PREF_UINT:
2118 pref->stashed_val.uint = *pref->varp.uint;
2119 break;
2121 case PREF_BOOL:
2122 pref->stashed_val.boolval = *pref->varp.boolp;
2123 break;
2125 case PREF_ENUM:
2126 pref->stashed_val.enumval = *pref->varp.enump;
2127 break;
2129 case PREF_STRING:
2130 case PREF_SAVE_FILENAME:
2131 case PREF_OPEN_FILENAME:
2132 case PREF_DIRNAME:
2133 case PREF_PASSWORD:
2134 case PREF_DISSECTOR:
2135 g_free(pref->stashed_val.string);
2136 pref->stashed_val.string = g_strdup(*pref->varp.string);
2137 break;
2139 case PREF_DECODE_AS_RANGE:
2140 case PREF_RANGE:
2141 wmem_free(wmem_epan_scope(), pref->stashed_val.range);
2142 pref->stashed_val.range = range_copy(wmem_epan_scope(), *pref->varp.range);
2143 break;
2145 case PREF_COLOR:
2146 pref->stashed_val.color = *pref->varp.colorp;
2147 break;
2149 case PREF_STATIC_TEXT:
2150 case PREF_UAT:
2151 case PREF_CUSTOM:
2152 case PREF_PROTO_TCP_SNDAMB_ENUM:
2153 break;
2155 case PREF_OBSOLETE:
2156 ws_assert_not_reached();
2157 break;
2159 return 0;
2162 unsigned
2163 pref_unstash(pref_t *pref, void *unstash_data_p)
2165 pref_unstash_data_t *unstash_data = (pref_unstash_data_t *)unstash_data_p;
2166 dissector_table_t sub_dissectors = NULL;
2167 dissector_handle_t handle = NULL;
2169 /* Revert the preference to its saved value. */
2170 switch (pref->type) {
2172 case PREF_UINT:
2173 if (*pref->varp.uint != pref->stashed_val.uint) {
2174 unstash_data->module->prefs_changed_flags |= prefs_get_effect_flags(pref);
2175 *pref->varp.uint = pref->stashed_val.uint;
2177 break;
2179 case PREF_BOOL:
2180 if (*pref->varp.boolp != pref->stashed_val.boolval) {
2181 unstash_data->module->prefs_changed_flags |= prefs_get_effect_flags(pref);
2182 *pref->varp.boolp = pref->stashed_val.boolval;
2184 break;
2186 case PREF_ENUM:
2187 if (*pref->varp.enump != pref->stashed_val.enumval) {
2188 unstash_data->module->prefs_changed_flags |= prefs_get_effect_flags(pref);
2189 *pref->varp.enump = pref->stashed_val.enumval;
2191 break;
2193 case PREF_PROTO_TCP_SNDAMB_ENUM:
2195 /* The preference dialogs are modal so the frame_data pointers should
2196 * still be valid; otherwise we could store the frame numbers to
2197 * change.
2199 frame_data *fdata;
2200 for (GList* elem = pref->stashed_val.list; elem != NULL; elem = elem->next) {
2201 fdata = (frame_data*)elem->data;
2202 if (fdata->tcp_snd_manual_analysis != *pref->varp.enump) {
2203 unstash_data->module->prefs_changed_flags |= prefs_get_effect_flags(pref);
2204 fdata->tcp_snd_manual_analysis = *pref->varp.enump;
2207 break;
2209 case PREF_STRING:
2210 case PREF_SAVE_FILENAME:
2211 case PREF_OPEN_FILENAME:
2212 case PREF_DIRNAME:
2213 case PREF_PASSWORD:
2214 case PREF_DISSECTOR:
2215 if (strcmp(*pref->varp.string, pref->stashed_val.string) != 0) {
2216 unstash_data->module->prefs_changed_flags |= prefs_get_effect_flags(pref);
2217 g_free(*pref->varp.string);
2218 *pref->varp.string = g_strdup(pref->stashed_val.string);
2220 break;
2222 case PREF_DECODE_AS_RANGE:
2224 const char* table_name = prefs_get_dissector_table(pref);
2225 if (!ranges_are_equal(*pref->varp.range, pref->stashed_val.range)) {
2226 uint32_t i, j;
2227 unstash_data->module->prefs_changed_flags |= prefs_get_effect_flags(pref);
2229 if (unstash_data->handle_decode_as) {
2230 sub_dissectors = find_dissector_table(table_name);
2231 if (sub_dissectors != NULL) {
2232 const char *handle_desc = prefs_get_dissector_description(pref);
2233 // It should perhaps be possible to get this via dissector name.
2234 handle = dissector_table_get_dissector_handle(sub_dissectors, handle_desc);
2235 if (handle != NULL) {
2236 /* Set the current handle to NULL for all the old values
2237 * in the dissector table. If there isn't an initial
2238 * handle, this actually deletes the entry. (If there
2239 * is an initial entry, keep it around so that the
2240 * user can see the original value.)
2242 * XXX - If there's an initial handle which is not this,
2243 * reset it instead? At least this leaves the initial
2244 * handle visible in the Decode As table.
2246 for (i = 0; i < (*pref->varp.range)->nranges; i++) {
2247 for (j = (*pref->varp.range)->ranges[i].low; j < (*pref->varp.range)->ranges[i].high; j++) {
2248 dissector_change_uint(table_name, j, NULL);
2249 decode_build_reset_list(table_name, dissector_table_get_type(sub_dissectors), GUINT_TO_POINTER(j), NULL, NULL);
2252 dissector_change_uint(table_name, (*pref->varp.range)->ranges[i].high, NULL);
2253 decode_build_reset_list(table_name, dissector_table_get_type(sub_dissectors), GUINT_TO_POINTER((*pref->varp.range)->ranges[i].high), NULL, NULL);
2259 wmem_free(wmem_epan_scope(), *pref->varp.range);
2260 *pref->varp.range = range_copy(wmem_epan_scope(), pref->stashed_val.range);
2262 if (unstash_data->handle_decode_as) {
2263 if ((sub_dissectors != NULL) && (handle != NULL)) {
2265 /* Add new values to the dissector table */
2266 for (i = 0; i < (*pref->varp.range)->nranges; i++) {
2268 for (j = (*pref->varp.range)->ranges[i].low; j < (*pref->varp.range)->ranges[i].high; j++) {
2269 dissector_change_uint(table_name, j, handle);
2270 decode_build_reset_list(table_name, dissector_table_get_type(sub_dissectors), GUINT_TO_POINTER(j), NULL, NULL);
2273 dissector_change_uint(table_name, (*pref->varp.range)->ranges[i].high, handle);
2274 decode_build_reset_list(table_name, dissector_table_get_type(sub_dissectors), GUINT_TO_POINTER((*pref->varp.range)->ranges[i].high), NULL, NULL);
2279 break;
2281 case PREF_RANGE:
2282 if (!ranges_are_equal(*pref->varp.range, pref->stashed_val.range)) {
2283 unstash_data->module->prefs_changed_flags |= prefs_get_effect_flags(pref);
2284 wmem_free(wmem_epan_scope(), *pref->varp.range);
2285 *pref->varp.range = range_copy(wmem_epan_scope(), pref->stashed_val.range);
2287 break;
2289 case PREF_COLOR:
2290 if ((pref->varp.colorp->blue != pref->stashed_val.color.blue) ||
2291 (pref->varp.colorp->red != pref->stashed_val.color.red) ||
2292 (pref->varp.colorp->green != pref->stashed_val.color.green)) {
2293 unstash_data->module->prefs_changed_flags |= prefs_get_effect_flags(pref);
2294 *pref->varp.colorp = pref->stashed_val.color;
2296 break;
2298 case PREF_STATIC_TEXT:
2299 case PREF_UAT:
2300 case PREF_CUSTOM:
2301 break;
2303 case PREF_OBSOLETE:
2304 ws_assert_not_reached();
2305 break;
2307 return 0;
2310 void
2311 reset_stashed_pref(pref_t *pref) {
2312 switch (pref->type) {
2314 case PREF_UINT:
2315 pref->stashed_val.uint = pref->default_val.uint;
2316 break;
2318 case PREF_BOOL:
2319 pref->stashed_val.boolval = pref->default_val.boolval;
2320 break;
2322 case PREF_ENUM:
2323 pref->stashed_val.enumval = pref->default_val.enumval;
2324 break;
2326 case PREF_STRING:
2327 case PREF_SAVE_FILENAME:
2328 case PREF_OPEN_FILENAME:
2329 case PREF_DIRNAME:
2330 case PREF_PASSWORD:
2331 case PREF_DISSECTOR:
2332 g_free(pref->stashed_val.string);
2333 pref->stashed_val.string = g_strdup(pref->default_val.string);
2334 break;
2336 case PREF_DECODE_AS_RANGE:
2337 case PREF_RANGE:
2338 wmem_free(wmem_epan_scope(), pref->stashed_val.range);
2339 pref->stashed_val.range = range_copy(wmem_epan_scope(), pref->default_val.range);
2340 break;
2342 case PREF_PROTO_TCP_SNDAMB_ENUM:
2343 if (pref->stashed_val.list != NULL) {
2344 g_list_free(pref->stashed_val.list);
2345 pref->stashed_val.list = NULL;
2347 break;
2349 case PREF_COLOR:
2350 memcpy(&pref->stashed_val.color, &pref->default_val.color, sizeof(color_t));
2351 break;
2353 case PREF_STATIC_TEXT:
2354 case PREF_UAT:
2355 case PREF_CUSTOM:
2356 break;
2358 case PREF_OBSOLETE:
2359 ws_assert_not_reached();
2360 break;
2364 unsigned
2365 pref_clean_stash(pref_t *pref, void *unused _U_)
2367 switch (pref->type) {
2369 case PREF_UINT:
2370 break;
2372 case PREF_BOOL:
2373 break;
2375 case PREF_ENUM:
2376 break;
2378 case PREF_STRING:
2379 case PREF_SAVE_FILENAME:
2380 case PREF_OPEN_FILENAME:
2381 case PREF_DIRNAME:
2382 case PREF_PASSWORD:
2383 case PREF_DISSECTOR:
2384 if (pref->stashed_val.string != NULL) {
2385 g_free(pref->stashed_val.string);
2386 pref->stashed_val.string = NULL;
2388 break;
2390 case PREF_DECODE_AS_RANGE:
2391 case PREF_RANGE:
2392 if (pref->stashed_val.range != NULL) {
2393 wmem_free(wmem_epan_scope(), pref->stashed_val.range);
2394 pref->stashed_val.range = NULL;
2396 break;
2398 case PREF_STATIC_TEXT:
2399 case PREF_UAT:
2400 case PREF_COLOR:
2401 case PREF_CUSTOM:
2402 break;
2404 case PREF_PROTO_TCP_SNDAMB_ENUM:
2405 if (pref->stashed_val.list != NULL) {
2406 g_list_free(pref->stashed_val.list);
2407 pref->stashed_val.list = NULL;
2409 break;
2411 case PREF_OBSOLETE:
2412 ws_assert_not_reached();
2413 break;
2415 return 0;
2419 * Call a callback function, with a specified argument, for each preference
2420 * in a given module.
2422 * If any of the callbacks return a non-zero value, stop and return that
2423 * value, otherwise return 0.
2425 unsigned
2426 prefs_pref_foreach(module_t *module, pref_cb callback, void *user_data)
2428 GList *elem;
2429 pref_t *pref;
2430 unsigned ret;
2432 for (elem = g_list_first(module->prefs); elem != NULL; elem = g_list_next(elem)) {
2433 pref = (pref_t *)elem->data;
2434 if (IS_PREF_OBSOLETE(pref->type)) {
2436 * This preference is no longer supported; it's
2437 * not a real preference, so we don't call the
2438 * callback for it (i.e., we treat it as if it
2439 * weren't found in the list of preferences,
2440 * and we weren't called in the first place).
2442 continue;
2445 ret = (*callback)(pref, user_data);
2446 if (ret != 0)
2447 return ret;
2449 return 0;
2452 static const enum_val_t st_sort_col_vals[] = {
2453 { "name", "Node name (topic/item)", ST_SORT_COL_NAME },
2454 { "count", "Item count", ST_SORT_COL_COUNT },
2455 { "average", "Average value of the node", ST_SORT_COL_AVG },
2456 { "min", "Minimum value of the node", ST_SORT_COL_MIN },
2457 { "max", "Maximum value of the node", ST_SORT_COL_MAX },
2458 { "burst", "Burst rate of the node", ST_SORT_COL_BURSTRATE },
2459 { NULL, NULL, 0 }
2462 static void
2463 stats_callback(void)
2465 /* Test for a sane tap update interval */
2466 if (prefs.tap_update_interval < 100 || prefs.tap_update_interval > 10000)
2467 prefs.tap_update_interval = TAP_UPDATE_DEFAULT_INTERVAL;
2469 /* burst resolution can't be less than 1 (ms) */
2470 if (prefs.st_burst_resolution < 1) {
2471 prefs.st_burst_resolution = 1;
2473 else if (prefs.st_burst_resolution > ST_MAX_BURSTRES) {
2474 prefs.st_burst_resolution = ST_MAX_BURSTRES;
2476 /* make sure burst window value makes sense */
2477 if (prefs.st_burst_windowlen < prefs.st_burst_resolution) {
2478 prefs.st_burst_windowlen = prefs.st_burst_resolution;
2480 /* round burst window down to multiple of resolution */
2481 prefs.st_burst_windowlen -= prefs.st_burst_windowlen%prefs.st_burst_resolution;
2482 if ((prefs.st_burst_windowlen/prefs.st_burst_resolution) > ST_MAX_BURSTBUCKETS) {
2483 prefs.st_burst_windowlen = prefs.st_burst_resolution*ST_MAX_BURSTBUCKETS;
2487 static void
2488 gui_callback(void)
2490 /* Ensure there is at least one file count */
2491 if (prefs.gui_recent_files_count_max == 0)
2492 prefs.gui_recent_files_count_max = 10;
2494 /* Ensure there is at least one display filter entry */
2495 if (prefs.gui_recent_df_entries_max == 0)
2496 prefs.gui_recent_df_entries_max = 10;
2498 /* number of decimal places should be between 2 and 10 */
2499 if (prefs.gui_decimal_places1 < 2) {
2500 prefs.gui_decimal_places1 = 2;
2501 } else if (prefs.gui_decimal_places1 > 10) {
2502 prefs.gui_decimal_places1 = 10;
2504 /* number of decimal places should be between 2 and 10 */
2505 if (prefs.gui_decimal_places2 < 2) {
2506 prefs.gui_decimal_places2 = 2;
2507 } else if (prefs.gui_decimal_places2 > 10) {
2508 prefs.gui_decimal_places2 = 10;
2510 /* number of decimal places should be between 2 and 10 */
2511 if (prefs.gui_decimal_places3 < 2) {
2512 prefs.gui_decimal_places3 = 2;
2513 } else if (prefs.gui_decimal_places3 > 10) {
2514 prefs.gui_decimal_places3 = 10;
2518 static void
2519 gui_layout_callback(void)
2521 if (prefs.gui_layout_type == layout_unused ||
2522 prefs.gui_layout_type >= layout_type_max) {
2523 /* XXX - report an error? It's not a syntax error - we'd need to
2524 add a way of reporting a *semantic* error. */
2525 prefs.gui_layout_type = layout_type_2;
2529 /******************************************************
2530 * All custom preference function callbacks
2531 ******************************************************/
2532 static void custom_pref_no_cb(pref_t* pref _U_) {}
2535 * Column preference functions
2537 #define PRS_COL_HIDDEN_FMT "column.hidden"
2538 #define PRS_COL_HIDDEN "column.hide"
2539 #define PRS_COL_FMT "column.format"
2540 #define PRS_COL_NUM "column.number"
2541 static module_t *gui_column_module;
2543 static prefs_set_pref_e
2544 column_hidden_set_cb(pref_t* pref, const char* value, unsigned int* changed_flags)
2546 GList *clp;
2547 fmt_data *cfmt;
2548 pref_t *format_pref;
2551 * Prefer the new preference to the old format-based preference if we've
2552 * read it. (We probably could just compare the string to NULL and "".)
2554 prefs.cols_hide_new = true;
2556 (*changed_flags) |= prefs_set_string_value(pref, value, pref_current);
2559 * Set the "visible" flag for the existing columns; we need to
2560 * do this if we set PRS_COL_HIDDEN but don't set PRS_COL_FMT
2561 * after setting it (which might be the case if, for example, we
2562 * set PRS_COL_HIDDEN on the command line).
2564 format_pref = prefs_find_preference(gui_column_module, PRS_COL_FMT);
2565 clp = (format_pref) ? *format_pref->varp.list : NULL;
2566 int cidx = 1;
2567 while (clp) {
2568 cfmt = (fmt_data *)clp->data;
2569 cfmt->visible = prefs_is_column_visible(*pref->varp.string, cidx);
2570 cidx++;
2571 clp = clp->next;
2574 return PREFS_SET_OK;
2577 static const char *
2578 column_hidden_type_name_cb(void)
2580 return "Packet list hidden columns";
2583 static char *
2584 column_hidden_type_description_cb(void)
2586 return g_strdup("List all column indices (1-indexed) to hide in the packet list.");
2589 static char *
2590 column_hidden_to_str_cb(pref_t* pref, bool default_val)
2592 GString *cols_hidden;
2593 GList *clp;
2594 fmt_data *cfmt;
2595 pref_t *format_pref;
2596 int cidx = 1;
2598 if (default_val)
2599 return g_strdup(pref->default_val.string);
2601 cols_hidden = g_string_new("");
2602 format_pref = prefs_find_preference(gui_column_module, PRS_COL_FMT);
2603 clp = (format_pref) ? *format_pref->varp.list : NULL;
2604 while (clp) {
2605 cfmt = (fmt_data *) clp->data;
2606 if (!cfmt->visible) {
2607 if (cols_hidden->len)
2608 g_string_append (cols_hidden, ",");
2609 g_string_append_printf (cols_hidden, "%i", cidx);
2611 clp = clp->next;
2612 cidx++;
2615 return g_string_free (cols_hidden, FALSE);
2618 static bool
2619 column_hidden_is_default_cb(pref_t* pref)
2621 char *cur_hidden_str = column_hidden_to_str_cb(pref, false);
2622 bool is_default = g_strcmp0(cur_hidden_str, pref->default_val.string) == 0;
2624 g_free(cur_hidden_str);
2625 return is_default;
2628 static prefs_set_pref_e
2629 column_hidden_fmt_set_cb(pref_t* pref, const char* value, unsigned int* changed_flags)
2631 GList *clp;
2632 fmt_data *cfmt;
2633 pref_t *format_pref;
2635 (*changed_flags) |= prefs_set_string_value(pref, value, pref_current);
2638 * Set the "visible" flag for the existing columns; we need to
2639 * do this if we set PRS_COL_HIDDEN_FMT but don't set PRS_COL_FMT
2640 * after setting it (which might be the case if, for example, we
2641 * set PRS_COL_HIDDEN_FMT on the command line; it shouldn't happen
2642 * when reading the configuration file because we write (both of)
2643 * the hidden column prefs before the column format prefs.)
2645 format_pref = prefs_find_preference(gui_column_module, PRS_COL_FMT);
2646 clp = (format_pref) ? *format_pref->varp.list : NULL;
2647 while (clp) {
2648 cfmt = (fmt_data *)clp->data;
2649 cfmt->visible = prefs_is_column_fmt_visible(*pref->varp.string, cfmt);
2650 clp = clp->next;
2653 return PREFS_SET_OK;
2656 static const char *
2657 column_hidden_fmt_type_name_cb(void)
2659 return "Packet list hidden column formats (deprecated)";
2662 static char *
2663 column_hidden_fmt_type_description_cb(void)
2665 return g_strdup("List all column formats to hide in the packet list. Deprecated in favor of the index-based preference.");
2668 static char *
2669 column_hidden_fmt_to_str_cb(pref_t* pref, bool default_val)
2671 GString *cols_hidden;
2672 GList *clp;
2673 fmt_data *cfmt;
2674 pref_t *format_pref;
2676 if (default_val)
2677 return g_strdup(pref->default_val.string);
2679 cols_hidden = g_string_new("");
2680 format_pref = prefs_find_preference(gui_column_module, PRS_COL_FMT);
2681 clp = (format_pref) ? *format_pref->varp.list : NULL;
2682 while (clp) {
2683 char *prefs_fmt;
2684 cfmt = (fmt_data *) clp->data;
2685 if (!cfmt->visible) {
2686 if (cols_hidden->len)
2687 g_string_append (cols_hidden, ",");
2688 prefs_fmt = column_fmt_data_to_str(cfmt);
2689 g_string_append(cols_hidden, prefs_fmt);
2690 g_free(prefs_fmt);
2692 clp = clp->next;
2695 return g_string_free (cols_hidden, FALSE);
2698 static bool
2699 column_hidden_fmt_is_default_cb(pref_t* pref)
2701 char *cur_hidden_str = column_hidden_fmt_to_str_cb(pref, false);
2702 bool is_default = g_strcmp0(cur_hidden_str, pref->default_val.string) == 0;
2704 g_free(cur_hidden_str);
2705 return is_default;
2708 /* Number of columns "preference". This is only used internally and is not written to the
2709 * preference file
2711 static void
2712 column_num_reset_cb(pref_t* pref)
2714 *pref->varp.uint = pref->default_val.uint;
2717 static prefs_set_pref_e
2718 column_num_set_cb(pref_t* pref _U_, const char* value _U_, unsigned int* changed_flags _U_)
2720 /* Don't write this to the preferences file */
2721 return PREFS_SET_OK;
2724 static const char *
2725 column_num_type_name_cb(void)
2727 return NULL;
2730 static char *
2731 column_num_type_description_cb(void)
2733 return g_strdup("");
2736 static bool
2737 column_num_is_default_cb(pref_t* pref _U_)
2739 return true;
2742 static char *
2743 column_num_to_str_cb(pref_t* pref _U_, bool default_val _U_)
2745 return g_strdup("");
2749 * Column format custom preference functions
2751 static void
2752 column_format_init_cb(pref_t* pref, GList** value)
2754 fmt_data *src_cfmt, *dest_cfmt;
2755 GList *entry;
2757 pref->varp.list = value;
2759 pref->default_val.list = NULL;
2760 for (entry = *pref->varp.list; entry != NULL; entry = g_list_next(entry)) {
2761 src_cfmt = (fmt_data *)entry->data;
2762 dest_cfmt = g_new(fmt_data,1);
2763 dest_cfmt->title = g_strdup(src_cfmt->title);
2764 dest_cfmt->fmt = src_cfmt->fmt;
2765 if (src_cfmt->custom_fields) {
2766 dest_cfmt->custom_fields = g_strdup(src_cfmt->custom_fields);
2767 dest_cfmt->custom_occurrence = src_cfmt->custom_occurrence;
2768 } else {
2769 dest_cfmt->custom_fields = NULL;
2770 dest_cfmt->custom_occurrence = 0;
2772 dest_cfmt->visible = src_cfmt->visible;
2773 dest_cfmt->display = src_cfmt->display;
2774 pref->default_val.list = g_list_append(pref->default_val.list, dest_cfmt);
2777 column_register_fields();
2780 static void
2781 column_format_free_cb(pref_t* pref)
2783 free_col_info(*pref->varp.list);
2784 free_col_info(pref->default_val.list);
2787 static void
2788 column_format_reset_cb(pref_t* pref)
2790 fmt_data *src_cfmt, *dest_cfmt;
2791 GList *entry;
2792 pref_t *col_num_pref;
2794 free_col_info(*pref->varp.list);
2795 *pref->varp.list = NULL;
2797 for (entry = pref->default_val.list; entry != NULL; entry = g_list_next(entry)) {
2798 src_cfmt = (fmt_data *)entry->data;
2799 dest_cfmt = g_new(fmt_data,1);
2800 dest_cfmt->title = g_strdup(src_cfmt->title);
2801 dest_cfmt->fmt = src_cfmt->fmt;
2802 if (src_cfmt->custom_fields) {
2803 dest_cfmt->custom_fields = g_strdup(src_cfmt->custom_fields);
2804 dest_cfmt->custom_occurrence = src_cfmt->custom_occurrence;
2805 } else {
2806 dest_cfmt->custom_fields = NULL;
2807 dest_cfmt->custom_occurrence = 0;
2809 dest_cfmt->visible = src_cfmt->visible;
2810 dest_cfmt->display = src_cfmt->display;
2811 *pref->varp.list = g_list_append(*pref->varp.list, dest_cfmt);
2814 col_num_pref = prefs_find_preference(gui_column_module, PRS_COL_NUM);
2815 ws_assert(col_num_pref != NULL); /* Should never happen */
2816 column_num_reset_cb(col_num_pref);
2819 static prefs_set_pref_e
2820 column_format_set_cb(pref_t* pref, const char* value, unsigned int* changed_flags _U_)
2822 GList *col_l, *col_l_elt;
2823 fmt_data *cfmt;
2824 int llen;
2825 pref_t *hidden_pref, *col_num_pref;
2827 col_l = prefs_get_string_list(value);
2828 if (col_l == NULL)
2829 return PREFS_SET_SYNTAX_ERR;
2830 if ((g_list_length(col_l) % 2) != 0) {
2831 /* A title didn't have a matching format. */
2832 prefs_clear_string_list(col_l);
2833 return PREFS_SET_SYNTAX_ERR;
2835 /* Check to make sure all column formats are valid. */
2836 col_l_elt = g_list_first(col_l);
2837 while (col_l_elt) {
2838 fmt_data cfmt_check;
2840 /* Go past the title. */
2841 col_l_elt = col_l_elt->next;
2843 /* Some predefined columns have been migrated to use custom columns.
2844 * We'll convert these silently here */
2845 try_convert_to_custom_column((char **)&col_l_elt->data);
2847 /* Parse the format to see if it's valid. */
2848 if (!parse_column_format(&cfmt_check, (char *)col_l_elt->data)) {
2849 /* It's not a valid column format. */
2850 prefs_clear_string_list(col_l);
2851 return PREFS_SET_SYNTAX_ERR;
2853 if (cfmt_check.fmt == COL_CUSTOM) {
2854 /* We don't need the custom column field on this pass. */
2855 g_free(cfmt_check.custom_fields);
2858 /* Go past the format. */
2859 col_l_elt = col_l_elt->next;
2862 /* They're all valid; process them. */
2863 free_col_info(*pref->varp.list);
2864 *pref->varp.list = NULL;
2865 if (prefs.cols_hide_new) {
2866 hidden_pref = prefs_find_preference(gui_column_module, PRS_COL_HIDDEN);
2867 } else {
2868 hidden_pref = prefs_find_preference(gui_column_module, PRS_COL_HIDDEN_FMT);
2870 ws_assert(hidden_pref != NULL); /* Should never happen */
2871 col_num_pref = prefs_find_preference(gui_column_module, PRS_COL_NUM);
2872 ws_assert(col_num_pref != NULL); /* Should never happen */
2873 llen = g_list_length(col_l);
2874 *col_num_pref->varp.uint = llen / 2;
2875 col_l_elt = g_list_first(col_l);
2876 int cidx = 1;
2877 while (col_l_elt) {
2878 cfmt = g_new(fmt_data,1);
2879 cfmt->title = g_strdup((char *)col_l_elt->data);
2880 col_l_elt = col_l_elt->next;
2881 parse_column_format(cfmt, (char *)col_l_elt->data);
2882 if (prefs.cols_hide_new) {
2883 cfmt->visible = prefs_is_column_visible(*hidden_pref->varp.string, cidx);
2884 } else {
2885 cfmt->visible = prefs_is_column_fmt_visible(*hidden_pref->varp.string, cfmt);
2887 col_l_elt = col_l_elt->next;
2888 *pref->varp.list = g_list_append(*pref->varp.list, cfmt);
2889 cidx++;
2892 prefs_clear_string_list(col_l);
2893 free_string_like_preference(hidden_pref);
2894 column_register_fields();
2895 return PREFS_SET_OK;
2899 static const char *
2900 column_format_type_name_cb(void)
2902 return "Packet list column format";
2905 static char *
2906 column_format_type_description_cb(void)
2908 return g_strdup("Each pair of strings consists of a column title and its format");
2911 static bool
2912 column_format_is_default_cb(pref_t* pref)
2914 GList *clp = *pref->varp.list,
2915 *pref_col = g_list_first(clp),
2916 *def_col = g_list_first(pref->default_val.list);
2917 fmt_data *cfmt, *def_cfmt;
2918 bool is_default = true;
2919 pref_t *col_num_pref;
2921 /* See if the column data has changed from the default */
2922 col_num_pref = prefs_find_preference(gui_column_module, PRS_COL_NUM);
2923 if (col_num_pref && *col_num_pref->varp.uint != col_num_pref->default_val.uint) {
2924 is_default = false;
2925 } else {
2926 while (pref_col && def_col) {
2927 cfmt = (fmt_data *) pref_col->data;
2928 def_cfmt = (fmt_data *) def_col->data;
2929 if ((g_strcmp0(cfmt->title, def_cfmt->title) != 0) ||
2930 (cfmt->fmt != def_cfmt->fmt) ||
2931 (((cfmt->fmt == COL_CUSTOM) && (cfmt->custom_fields)) &&
2932 ((g_strcmp0(cfmt->custom_fields, def_cfmt->custom_fields) != 0) ||
2933 (cfmt->display != def_cfmt->display)))) {
2934 is_default = false;
2935 break;
2938 pref_col = pref_col->next;
2939 def_col = def_col->next;
2943 return is_default;
2946 static char *
2947 column_format_to_str_cb(pref_t* pref, bool default_val)
2949 GList *pref_l = default_val ? pref->default_val.list : *pref->varp.list;
2950 GList *clp = g_list_first(pref_l);
2951 GList *col_l;
2952 fmt_data *cfmt;
2953 char *column_format_str;
2955 col_l = NULL;
2956 while (clp) {
2957 cfmt = (fmt_data *) clp->data;
2958 col_l = g_list_append(col_l, g_strdup(cfmt->title));
2959 col_l = g_list_append(col_l, column_fmt_data_to_str(cfmt));
2960 clp = clp->next;
2963 column_format_str = join_string_list(col_l);
2964 prefs_clear_string_list(col_l);
2965 return column_format_str;
2969 /****** Capture column custom preference functions ******/
2971 /* This routine is only called when Wireshark is started, NOT when another profile is selected.
2972 Copy the pref->capture_columns list (just loaded with the capture_cols[] struct values)
2973 to prefs->default_val.list.
2975 static void
2976 capture_column_init_cb(pref_t* pref, GList** capture_cols_values)
2978 GList *ccv_list = *capture_cols_values,
2979 *dlist = NULL;
2981 /* */
2982 while (ccv_list) {
2983 dlist = g_list_append(dlist, g_strdup((char *)ccv_list->data));
2984 ccv_list = ccv_list->next;
2987 pref->default_val.list = dlist;
2988 pref->varp.list = &prefs.capture_columns;
2989 pref->stashed_val.boolval = false;
2992 /* Free the prefs->capture_columns list strings and remove the list entries.
2993 Note that since pref->varp.list points to &prefs.capture_columns, it is
2994 also freed.
2996 static void
2997 capture_column_free_cb(pref_t* pref)
2999 prefs_clear_string_list(prefs.capture_columns);
3000 prefs.capture_columns = NULL;
3002 if (pref->stashed_val.boolval == true) {
3003 prefs_clear_string_list(pref->default_val.list);
3004 pref->default_val.list = NULL;
3008 /* Copy pref->default_val.list to *pref->varp.list.
3010 static void
3011 capture_column_reset_cb(pref_t* pref)
3013 GList *vlist = NULL, *dlist;
3015 /* Free the column name strings and remove the links from *pref->varp.list */
3016 prefs_clear_string_list(*pref->varp.list);
3018 for (dlist = pref->default_val.list; dlist != NULL; dlist = g_list_next(dlist)) {
3019 vlist = g_list_append(vlist, g_strdup((char *)dlist->data));
3021 *pref->varp.list = vlist;
3024 static prefs_set_pref_e
3025 capture_column_set_cb(pref_t* pref, const char* value, unsigned int* changed_flags _U_)
3027 GList *col_l = prefs_get_string_list(value);
3028 GList *col_l_elt;
3029 char *col_name;
3030 int i;
3032 if (col_l == NULL)
3033 return PREFS_SET_SYNTAX_ERR;
3035 capture_column_free_cb(pref);
3037 /* If value (the list of capture.columns read from preferences) is empty, set capture.columns
3038 to the full list of valid capture column names. */
3039 col_l_elt = g_list_first(col_l);
3040 if (!(*(char *)col_l_elt->data)) {
3041 for (i = 0; i < num_capture_cols; i++) {
3042 col_name = g_strdup(capture_cols[i]);
3043 prefs.capture_columns = g_list_append(prefs.capture_columns, col_name);
3047 /* Verify that all the column names are valid. If not, use the entire list of valid columns.
3049 while (col_l_elt) {
3050 bool found_match = false;
3051 col_name = (char *)col_l_elt->data;
3053 for (i = 0; i < num_capture_cols; i++) {
3054 if (strcmp(col_name, capture_cols[i])==0) {
3055 found_match = true;
3056 break;
3059 if (!found_match) {
3060 /* One or more cols are invalid so use the entire list of valid cols. */
3061 for (i = 0; i < num_capture_cols; i++) {
3062 col_name = g_strdup(capture_cols[i]);
3063 prefs.capture_columns = g_list_append(prefs.capture_columns, col_name);
3065 pref->varp.list = &prefs.capture_columns;
3066 prefs_clear_string_list(col_l);
3067 return PREFS_SET_SYNTAX_ERR;
3069 col_l_elt = col_l_elt->next;
3072 col_l_elt = g_list_first(col_l);
3073 while (col_l_elt) {
3074 col_name = (char *)col_l_elt->data;
3075 prefs.capture_columns = g_list_append(prefs.capture_columns, col_name);
3076 col_l_elt = col_l_elt->next;
3078 pref->varp.list = &prefs.capture_columns;
3079 g_list_free(col_l);
3080 return PREFS_SET_OK;
3084 static const char *
3085 capture_column_type_name_cb(void)
3087 return "Column list";
3090 static char *
3091 capture_column_type_description_cb(void)
3093 return g_strdup(
3094 "List of columns to be displayed in the capture options dialog.\n"
3095 CAPTURE_COL_TYPE_DESCRIPTION);
3098 static bool
3099 capture_column_is_default_cb(pref_t* pref)
3101 GList *pref_col = g_list_first(prefs.capture_columns),
3102 *def_col = g_list_first(pref->default_val.list);
3103 bool is_default = true;
3105 /* See if the column data has changed from the default */
3106 while (pref_col && def_col) {
3107 if (strcmp((char *)pref_col->data, (char *)def_col->data) != 0) {
3108 is_default = false;
3109 break;
3111 pref_col = pref_col->next;
3112 def_col = def_col->next;
3115 /* Ensure the same column count */
3116 if (((pref_col == NULL) && (def_col != NULL)) ||
3117 ((pref_col != NULL) && (def_col == NULL)))
3118 is_default = false;
3120 return is_default;
3123 static char *
3124 capture_column_to_str_cb(pref_t* pref, bool default_val)
3127 GList *pref_l = default_val ? pref->default_val.list : prefs.capture_columns;
3128 GList *clp = g_list_first(pref_l);
3129 GList *col_l = NULL;
3130 char *col;
3131 char *capture_column_str;
3133 while (clp) {
3134 col = (char *) clp->data;
3135 col_l = g_list_append(col_l, g_strdup(col));
3136 clp = clp->next;
3139 capture_column_str = join_string_list(col_l);
3140 prefs_clear_string_list(col_l);
3141 return capture_column_str;
3144 static prefs_set_pref_e
3145 colorized_frame_set_cb(pref_t* pref, const char* value, unsigned int* changed_flags)
3147 (*changed_flags) |= prefs_set_string_value(pref, value, pref_current);
3148 return PREFS_SET_OK;
3151 static const char *
3152 colorized_frame_type_name_cb(void)
3154 /* Don't write the colors of the 10 easy-access-colorfilters to the preferences
3155 * file until the colors can be changed in the GUI. Currently this is not really
3156 * possible since the STOCK-icons for these colors are hardcoded.
3158 * XXX Find a way to change the colors of the STOCK-icons on the fly and then
3159 * add these 10 colors to the list of colors that can be changed through
3160 * the preferences.
3163 return NULL;
3166 static char *
3167 colorized_frame_type_description_cb(void)
3169 return g_strdup("");
3172 static bool
3173 colorized_frame_is_default_cb(pref_t* pref _U_)
3175 return true;
3178 static char *
3179 colorized_frame_to_str_cb(pref_t* pref _U_, bool default_val _U_)
3181 return g_strdup("");
3185 * Register all non-dissector modules' preferences.
3187 static module_t *gui_module;
3188 static module_t *gui_color_module;
3189 static module_t *nameres_module;
3191 static void
3192 prefs_register_modules(void)
3194 module_t *printing, *capture_module, *console_module,
3195 *gui_layout_module, *gui_font_module;
3196 module_t *extcap_module;
3197 unsigned int layout_gui_flags;
3198 struct pref_custom_cbs custom_cbs;
3200 if (protocols_module != NULL) {
3201 /* Already setup preferences */
3202 return;
3205 /* GUI
3206 * These are "simple" GUI preferences that can be read/written using the
3207 * preference module API. These preferences still use their own
3208 * configuration screens for access, but this cuts down on the
3209 * preference "string compare list" in set_pref()
3211 extcap_module = prefs_register_module(NULL, "extcap", "Extcap Utilities",
3212 "Extcap Utilities", NULL, NULL, false);
3214 /* Setting default value to true */
3215 prefs.extcap_save_on_start = true;
3216 prefs_register_bool_preference(extcap_module, "gui_save_on_start",
3217 "Save arguments on start of capture",
3218 "Save arguments on start of capture",
3219 &prefs.extcap_save_on_start);
3221 /* GUI
3222 * These are "simple" GUI preferences that can be read/written using the
3223 * preference module API. These preferences still use their own
3224 * configuration screens for access, but this cuts down on the
3225 * preference "string compare list" in set_pref()
3227 gui_module = prefs_register_module(NULL, "gui", "User Interface",
3228 "User Interface", NULL, &gui_callback, false);
3230 * The GUI preferences don't affect dissection in general.
3231 * Any changes are signaled in other ways, so PREF_EFFECT_GUI doesn't
3232 * explicitly do anything, but wslua_set_preference expects *some*
3233 * effect flag to be set if the preference was changed.
3234 * We have to do this again for all the submodules (except for the
3235 * layout submodule, which has its own effect flag).
3237 unsigned gui_effect_flags = prefs_get_module_effect_flags(gui_module);
3238 gui_effect_flags |= PREF_EFFECT_GUI;
3239 gui_effect_flags &= (~PREF_EFFECT_DISSECTION);
3240 prefs_set_module_effect_flags(gui_module, gui_effect_flags);
3243 * gui.console_open is stored in the registry in addition to the
3244 * preferences file. It is also read independently by ws_log_init()
3245 * for early log initialization of the console.
3247 prefs_register_enum_preference(gui_module, "console_open",
3248 "Open a console window",
3249 "Open a console window (Windows only)",
3250 (int *)&ws_log_console_open, gui_console_open_type, false);
3252 prefs_register_obsolete_preference(gui_module, "scrollbar_on_right");
3253 prefs_register_obsolete_preference(gui_module, "packet_list_sel_browse");
3254 prefs_register_obsolete_preference(gui_module, "protocol_tree_sel_browse");
3255 prefs_register_obsolete_preference(gui_module, "tree_view_altern_colors");
3256 prefs_register_obsolete_preference(gui_module, "expert_composite_eyecandy");
3257 prefs_register_obsolete_preference(gui_module, "filter_toolbar_show_in_statusbar");
3259 prefs_register_bool_preference(gui_module, "restore_filter_after_following_stream",
3260 "Restore current display filter after following a stream",
3261 "Restore current display filter after following a stream?",
3262 &prefs.restore_filter_after_following_stream);
3264 prefs_register_obsolete_preference(gui_module, "protocol_tree_line_style");
3266 prefs_register_obsolete_preference(gui_module, "protocol_tree_expander_style");
3268 prefs_register_obsolete_preference(gui_module, "hex_dump_highlight_style");
3270 prefs_register_obsolete_preference(gui_module, "packet_editor.enabled");
3272 gui_column_module = prefs_register_subtree(gui_module, "Columns", "Columns", NULL);
3273 prefs_set_module_effect_flags(gui_column_module, gui_effect_flags);
3274 /* For reading older preference files with "column." preferences */
3275 prefs_register_module_alias("column", gui_column_module);
3278 custom_cbs.free_cb = free_string_like_preference;
3279 custom_cbs.reset_cb = reset_string_like_preference;
3280 custom_cbs.set_cb = column_hidden_set_cb;
3281 custom_cbs.type_name_cb = column_hidden_type_name_cb;
3282 custom_cbs.type_description_cb = column_hidden_type_description_cb;
3283 custom_cbs.is_default_cb = column_hidden_is_default_cb;
3284 custom_cbs.to_str_cb = column_hidden_to_str_cb;
3285 register_string_like_preference(gui_column_module, PRS_COL_HIDDEN, "Packet list hidden columns",
3286 "List all column indices (1-indexed) to hide in the packet list",
3287 &cols_hidden_list, PREF_CUSTOM, &custom_cbs, false);
3289 custom_cbs.set_cb = column_hidden_fmt_set_cb;
3290 custom_cbs.type_name_cb = column_hidden_fmt_type_name_cb;
3291 custom_cbs.type_description_cb = column_hidden_fmt_type_description_cb;
3292 custom_cbs.is_default_cb = column_hidden_fmt_is_default_cb;
3293 custom_cbs.to_str_cb = column_hidden_fmt_to_str_cb;
3295 register_string_like_preference(gui_column_module, PRS_COL_HIDDEN_FMT, "Packet list hidden column formats (deprecated)",
3296 "List all column formats to hide in the packet list; deprecated in favor of the index-based preference",
3297 &cols_hidden_fmt_list, PREF_CUSTOM, &custom_cbs, false);
3299 custom_cbs.free_cb = column_format_free_cb;
3300 custom_cbs.reset_cb = column_format_reset_cb;
3301 custom_cbs.set_cb = column_format_set_cb;
3302 custom_cbs.type_name_cb = column_format_type_name_cb;
3303 custom_cbs.type_description_cb = column_format_type_description_cb;
3304 custom_cbs.is_default_cb = column_format_is_default_cb;
3305 custom_cbs.to_str_cb = column_format_to_str_cb;
3307 prefs_register_list_custom_preference(gui_column_module, PRS_COL_FMT, "Packet list column format",
3308 "Each pair of strings consists of a column title and its format", &custom_cbs,
3309 column_format_init_cb, &prefs.col_list);
3311 /* Number of columns. This is only used internally and is not written to the
3312 * preference file
3314 custom_cbs.free_cb = custom_pref_no_cb;
3315 custom_cbs.reset_cb = column_num_reset_cb;
3316 custom_cbs.set_cb = column_num_set_cb;
3317 custom_cbs.type_name_cb = column_num_type_name_cb;
3318 custom_cbs.type_description_cb = column_num_type_description_cb;
3319 custom_cbs.is_default_cb = column_num_is_default_cb;
3320 custom_cbs.to_str_cb = column_num_to_str_cb;
3321 prefs_register_uint_custom_preference(gui_column_module, PRS_COL_NUM, "Number of columns",
3322 "Number of columns in col_list", &custom_cbs, &prefs.num_cols);
3324 /* User Interface : Font */
3325 gui_font_module = prefs_register_subtree(gui_module, "Font", "Font", NULL);
3326 prefs_set_module_effect_flags(gui_font_module, gui_effect_flags);
3328 prefs_register_obsolete_preference(gui_font_module, "font_name");
3330 prefs_register_obsolete_preference(gui_font_module, "gtk2.font_name");
3332 register_string_like_preference(gui_font_module, "qt.font_name", "Font name",
3333 "Font name for packet list, protocol tree, and hex dump panes. (Qt)",
3334 &prefs.gui_font_name, PREF_STRING, NULL, true);
3336 /* User Interface : Colors */
3337 gui_color_module = prefs_register_subtree(gui_module, "Colors", "Colors", NULL);
3338 unsigned gui_color_effect_flags = gui_effect_flags | PREF_EFFECT_GUI_COLOR;
3339 prefs_set_module_effect_flags(gui_color_module, gui_color_effect_flags);
3341 prefs_register_enum_preference(gui_color_module, "color_scheme", "Color scheme", "Color scheme",
3342 &prefs.gui_color_scheme, gui_color_scheme, false);
3344 prefs_register_color_preference(gui_color_module, "active_frame.fg", "Foreground color for an active selected item",
3345 "Foreground color for an active selected item", &prefs.gui_active_fg);
3347 prefs_register_color_preference(gui_color_module, "active_frame.bg", "Background color for an active selected item",
3348 "Background color for an active selected item", &prefs.gui_active_bg);
3350 prefs_register_enum_preference(gui_color_module, "active_frame.style", "Color style for an active selected item",
3351 "Color style for an active selected item", &prefs.gui_active_style, gui_selection_style, false);
3353 prefs_register_color_preference(gui_color_module, "inactive_frame.fg", "Foreground color for an inactive selected item",
3354 "Foreground color for an inactive selected item", &prefs.gui_inactive_fg);
3356 prefs_register_color_preference(gui_color_module, "inactive_frame.bg", "Background color for an inactive selected item",
3357 "Background color for an inactive selected item", &prefs.gui_inactive_bg);
3359 prefs_register_enum_preference(gui_color_module, "inactive_frame.style", "Color style for an inactive selected item",
3360 "Color style for an inactive selected item", &prefs.gui_inactive_style, gui_selection_style, false);
3362 prefs_register_color_preference(gui_color_module, "marked_frame.fg", "Color preferences for a marked frame",
3363 "Color preferences for a marked frame", &prefs.gui_marked_fg);
3365 prefs_register_color_preference(gui_color_module, "marked_frame.bg", "Color preferences for a marked frame",
3366 "Color preferences for a marked frame", &prefs.gui_marked_bg);
3368 prefs_register_color_preference(gui_color_module, "ignored_frame.fg", "Color preferences for a ignored frame",
3369 "Color preferences for a ignored frame", &prefs.gui_ignored_fg);
3371 prefs_register_color_preference(gui_color_module, "ignored_frame.bg", "Color preferences for a ignored frame",
3372 "Color preferences for a ignored frame", &prefs.gui_ignored_bg);
3374 prefs_register_color_preference(gui_color_module, "stream.client.fg", "TCP stream window color preference",
3375 "TCP stream window color preference", &prefs.st_client_fg);
3377 prefs_register_color_preference(gui_color_module, "stream.client.bg", "TCP stream window color preference",
3378 "TCP stream window color preference", &prefs.st_client_bg);
3380 prefs_register_color_preference(gui_color_module, "stream.server.fg", "TCP stream window color preference",
3381 "TCP stream window color preference", &prefs.st_server_fg);
3383 prefs_register_color_preference(gui_color_module, "stream.server.bg", "TCP stream window color preference",
3384 "TCP stream window color preference", &prefs.st_server_bg);
3386 custom_cbs.free_cb = free_string_like_preference;
3387 custom_cbs.reset_cb = reset_string_like_preference;
3388 custom_cbs.set_cb = colorized_frame_set_cb;
3389 custom_cbs.type_name_cb = colorized_frame_type_name_cb;
3390 custom_cbs.type_description_cb = colorized_frame_type_description_cb;
3391 custom_cbs.is_default_cb = colorized_frame_is_default_cb;
3392 custom_cbs.to_str_cb = colorized_frame_to_str_cb;
3393 register_string_like_preference(gui_column_module, "colorized_frame.fg", "Colorized Foreground",
3394 "Filter Colorized Foreground",
3395 &prefs.gui_colorized_fg, PREF_CUSTOM, &custom_cbs, true);
3397 custom_cbs.free_cb = free_string_like_preference;
3398 custom_cbs.reset_cb = reset_string_like_preference;
3399 custom_cbs.set_cb = colorized_frame_set_cb;
3400 custom_cbs.type_name_cb = colorized_frame_type_name_cb;
3401 custom_cbs.type_description_cb = colorized_frame_type_description_cb;
3402 custom_cbs.is_default_cb = colorized_frame_is_default_cb;
3403 custom_cbs.to_str_cb = colorized_frame_to_str_cb;
3404 register_string_like_preference(gui_column_module, "colorized_frame.bg", "Colorized Background",
3405 "Filter Colorized Background",
3406 &prefs.gui_colorized_bg, PREF_CUSTOM, &custom_cbs, true);
3408 prefs_register_color_preference(gui_color_module, "color_filter_bg.valid", "Valid color filter background",
3409 "Valid color filter background", &prefs.gui_text_valid);
3411 prefs_register_color_preference(gui_color_module, "color_filter_bg.invalid", "Invalid color filter background",
3412 "Invalid color filter background", &prefs.gui_text_invalid);
3414 prefs_register_color_preference(gui_color_module, "color_filter_bg.deprecated", "Deprecated color filter background",
3415 "Deprecated color filter background", &prefs.gui_text_deprecated);
3417 prefs_register_enum_preference(gui_module, "fileopen.style",
3418 "Where to start the File Open dialog box",
3419 "Where to start the File Open dialog box",
3420 &prefs.gui_fileopen_style, gui_fileopen_style, false);
3422 prefs_register_uint_preference(gui_module, "recent_files_count.max",
3423 "The max. number of items in the open recent files list",
3424 "The max. number of items in the open recent files list",
3426 &prefs.gui_recent_files_count_max);
3428 prefs_register_uint_preference(gui_module, "recent_display_filter_entries.max",
3429 "The max. number of entries in the display filter list",
3430 "The max. number of entries in the display filter list",
3432 &prefs.gui_recent_df_entries_max);
3434 register_string_like_preference(gui_module, "fileopen.dir", "Start Directory",
3435 "Directory to start in when opening File Open dialog.",
3436 &prefs.gui_fileopen_dir, PREF_DIRNAME, NULL, true);
3438 prefs_register_obsolete_preference(gui_module, "fileopen.remembered_dir");
3440 prefs_register_uint_preference(gui_module, "fileopen.preview",
3441 "The preview timeout in the File Open dialog",
3442 "The preview timeout in the File Open dialog",
3444 &prefs.gui_fileopen_preview);
3446 register_string_like_preference(gui_module, "tlskeylog_command", "Program to launch with TLS Keylog",
3447 "Program path or command line to launch with SSLKEYLOGFILE",
3448 &prefs.gui_tlskeylog_command, PREF_STRING, NULL, true);
3450 prefs_register_bool_preference(gui_module, "ask_unsaved",
3451 "Ask to save unsaved capture files",
3452 "Ask to save unsaved capture files?",
3453 &prefs.gui_ask_unsaved);
3455 prefs_register_bool_preference(gui_module, "autocomplete_filter",
3456 "Display autocompletion for filter text",
3457 "Display an autocomplete suggestion for display and capture filter controls",
3458 &prefs.gui_autocomplete_filter);
3460 prefs_register_bool_preference(gui_module, "find_wrap",
3461 "Wrap to beginning/end of file during search",
3462 "Wrap to beginning/end of file during search?",
3463 &prefs.gui_find_wrap);
3465 prefs_register_obsolete_preference(gui_module, "use_pref_save");
3467 prefs_register_bool_preference(gui_module, "geometry.save.position",
3468 "Save window position at exit",
3469 "Save window position at exit?",
3470 &prefs.gui_geometry_save_position);
3472 prefs_register_bool_preference(gui_module, "geometry.save.size",
3473 "Save window size at exit",
3474 "Save window size at exit?",
3475 &prefs.gui_geometry_save_size);
3477 prefs_register_bool_preference(gui_module, "geometry.save.maximized",
3478 "Save window maximized state at exit",
3479 "Save window maximized state at exit?",
3480 &prefs.gui_geometry_save_maximized);
3482 prefs_register_obsolete_preference(gui_module, "macosx_style");
3484 prefs_register_obsolete_preference(gui_module, "geometry.main.x");
3485 prefs_register_obsolete_preference(gui_module, "geometry.main.y");
3486 prefs_register_obsolete_preference(gui_module, "geometry.main.width");
3487 prefs_register_obsolete_preference(gui_module, "geometry.main.height");
3488 prefs_register_obsolete_preference(gui_module, "toolbar_main_show");
3490 prefs_register_enum_preference(gui_module, "toolbar_main_style",
3491 "Main Toolbar style",
3492 "Main Toolbar style",
3493 &prefs.gui_toolbar_main_style, gui_toolbar_style, false);
3495 prefs_register_obsolete_preference(gui_module, "toolbar_filter_style");
3496 prefs_register_obsolete_preference(gui_module, "webbrowser");
3498 prefs_register_bool_preference(gui_module, "update.enabled",
3499 "Check for updates",
3500 "Check for updates (Windows and macOS only)",
3501 &prefs.gui_update_enabled);
3503 prefs_register_enum_preference(gui_module, "update.channel",
3504 "Update channel",
3505 "The type of update to fetch. You should probably leave this set to STABLE.",
3506 (int*)(void*)(&prefs.gui_update_channel), gui_update_channel, false);
3508 prefs_register_uint_preference(gui_module, "update.interval",
3509 "How often to check for software updates",
3510 "How often to check for software updates in seconds",
3512 &prefs.gui_update_interval);
3514 prefs_register_uint_preference(gui_module, "debounce.timer",
3515 "How long to wait before processing computationally intensive user input",
3516 "How long to wait (in milliseconds) before processing "
3517 "computationally intensive user input. "
3518 "If you type quickly, consider lowering the value for a 'snappier' "
3519 "experience. "
3520 "If you type slowly, consider increasing the value to avoid performance issues. "
3521 "This is currently used to delay searches in View -> Internals -> Supported Protocols "
3522 "and Preferences -> Advanced menu.",
3524 &prefs.gui_debounce_timer);
3526 register_string_like_preference(gui_module, "window_title", "Custom window title",
3527 "Custom window title to be appended to the existing title\n"
3528 "%C = capture comment from command line\n"
3529 "%F = file path of the capture file\n"
3530 "%P = profile name\n"
3531 "%S = a conditional separator (\" - \") that only shows when surrounded by variables with values or static text\n"
3532 "%V = version info",
3533 &prefs.gui_window_title, PREF_STRING, NULL, true);
3535 register_string_like_preference(gui_module, "prepend_window_title", "Custom window title prefix",
3536 "Custom window title to be prepended to the existing title\n"
3537 "%C = capture comment from command line\n"
3538 "%F = file path of the capture file\n"
3539 "%P = profile name\n"
3540 "%S = a conditional separator (\" - \") that only shows when surrounded by variables with values or static text\n"
3541 "%V = version info",
3542 &prefs.gui_prepend_window_title, PREF_STRING, NULL, true);
3544 register_string_like_preference(gui_module, "start_title", "Custom start page title",
3545 "Custom start page title",
3546 &prefs.gui_start_title, PREF_STRING, NULL, true);
3548 prefs_register_enum_preference(gui_module, "version_placement",
3549 "Show version in the start page and/or main screen's title bar",
3550 "Show version in the start page and/or main screen's title bar",
3551 (int*)(void*)(&prefs.gui_version_placement), gui_version_placement_type, false);
3553 prefs_register_obsolete_preference(gui_module, "auto_scroll_on_expand");
3554 prefs_register_obsolete_preference(gui_module, "auto_scroll_percentage");
3556 prefs_register_uint_preference(gui_module, "max_export_objects",
3557 "Maximum number of exported objects",
3558 "The maximum number of objects that can be exported",
3560 &prefs.gui_max_export_objects);
3561 prefs_register_uint_preference(gui_module, "max_tree_items",
3562 "Maximum number of tree items",
3563 "The maximum number of items that can be added to the dissection tree (Increase with caution)",
3565 &prefs.gui_max_tree_items);
3567 * Used independently by proto_tree_add_node, call_dissector*, dissector_try_heuristic,
3568 * and increment_dissection_depth.
3570 prefs_register_uint_preference(gui_module, "max_tree_depth",
3571 "Maximum dissection depth",
3572 "The maximum depth for dissection tree and protocol layer checks. (Increase with caution)",
3574 &prefs.gui_max_tree_depth);
3576 prefs_register_bool_preference(gui_module, "welcome_page.show_recent",
3577 "Show recent files on the welcome page",
3578 "This will enable or disable the 'Open' list on the welcome page.",
3579 &prefs.gui_welcome_page_show_recent);
3581 /* User Interface : Layout */
3582 gui_layout_module = prefs_register_subtree(gui_module, "Layout", "Layout", gui_layout_callback);
3583 /* Adjust the preference effects of layout GUI for better handling of preferences at Wireshark (GUI) level */
3584 layout_gui_flags = prefs_get_module_effect_flags(gui_layout_module);
3585 layout_gui_flags |= PREF_EFFECT_GUI_LAYOUT;
3586 layout_gui_flags &= (~PREF_EFFECT_DISSECTION);
3588 prefs_register_uint_preference(gui_layout_module, "layout_type",
3589 "Layout type",
3590 "Layout type (1-6)",
3592 (unsigned*)(void*)(&prefs.gui_layout_type));
3593 prefs_set_effect_flags_by_name(gui_layout_module, "layout_type", layout_gui_flags);
3595 prefs_register_enum_preference(gui_layout_module, "layout_content_1",
3596 "Layout content of the pane 1",
3597 "Layout content of the pane 1",
3598 (int*)(void*)(&prefs.gui_layout_content_1), gui_layout_content, false);
3599 prefs_set_effect_flags_by_name(gui_layout_module, "layout_content_1", layout_gui_flags);
3601 prefs_register_enum_preference(gui_layout_module, "layout_content_2",
3602 "Layout content of the pane 2",
3603 "Layout content of the pane 2",
3604 (int*)(void*)(&prefs.gui_layout_content_2), gui_layout_content, false);
3605 prefs_set_effect_flags_by_name(gui_layout_module, "layout_content_2", layout_gui_flags);
3607 prefs_register_enum_preference(gui_layout_module, "layout_content_3",
3608 "Layout content of the pane 3",
3609 "Layout content of the pane 3",
3610 (int*)(void*)(&prefs.gui_layout_content_3), gui_layout_content, false);
3611 prefs_set_effect_flags_by_name(gui_layout_module, "layout_content_3", layout_gui_flags);
3613 prefs_register_bool_preference(gui_layout_module, "packet_list_separator.enabled",
3614 "Enable Packet List Separator",
3615 "Enable Packet List Separator",
3616 &prefs.gui_packet_list_separator);
3618 prefs_register_bool_preference(gui_layout_module, "packet_header_column_definition.enabled",
3619 "Show column definition in packet list header",
3620 "Show column definition in packet list header",
3621 &prefs.gui_packet_header_column_definition);
3623 /* packet_list_hover_style affects the colors, not the layout.
3624 * It's in the layout module to group it with the other packet list
3625 * preferences for the user's benefit with the dialog.
3627 prefs_register_bool_preference(gui_layout_module, "packet_list_hover_style.enabled",
3628 "Enable Packet List mouse-over colorization",
3629 "Enable Packet List mouse-over colorization",
3630 &prefs.gui_packet_list_hover_style);
3631 prefs_set_effect_flags_by_name(gui_layout_module, "packet_list_hover_style.enabled", gui_color_effect_flags);
3633 prefs_register_bool_preference(gui_layout_module, "show_selected_packet.enabled",
3634 "Show selected packet in the Status Bar",
3635 "Show selected packet in the Status Bar",
3636 &prefs.gui_show_selected_packet);
3638 prefs_register_bool_preference(gui_layout_module, "show_file_load_time.enabled",
3639 "Show file load time in the Status Bar",
3640 "Show file load time in the Status Bar",
3641 &prefs.gui_show_file_load_time);
3643 prefs_register_enum_preference(gui_layout_module, "packet_dialog_layout",
3644 "Packet Dialog layout",
3645 "Packet Dialog layout",
3646 (unsigned*)(void*)(&prefs.gui_packet_dialog_layout), gui_packet_dialog_layout, false);
3648 prefs_register_enum_preference(gui_module, "packet_list_elide_mode",
3649 "Elide mode",
3650 "The position of \"...\" (ellipsis) in packet list text.",
3651 (int*)(void*)(&prefs.gui_packet_list_elide_mode), gui_packet_list_elide_mode, false);
3652 prefs_register_uint_preference(gui_module, "decimal_places1",
3653 "Count of decimal places for values of type 1",
3654 "Sets the count of decimal places for values of type 1."
3655 "Type 1 values are defined by authors."
3656 "Value can be in range 2 to 10.",
3657 10,&prefs.gui_decimal_places1);
3659 prefs_register_uint_preference(gui_module, "decimal_places2",
3660 "Count of decimal places for values of type 2",
3661 "Sets the count of decimal places for values of type 2."
3662 "Type 2 values are defined by authors."
3663 "Value can be in range 2 to 10.",
3664 10,&prefs.gui_decimal_places2);
3666 prefs_register_uint_preference(gui_module, "decimal_places3",
3667 "Count of decimal places for values of type 3",
3668 "Sets the count of decimal places for values of type 3."
3669 "Type 3 values are defined by authors."
3670 "Value can be in range 2 to 10.",
3671 10,&prefs.gui_decimal_places3);
3673 prefs_register_bool_preference(gui_module, "rtp_player_use_disk1",
3674 "RTP Player saves temporary data to disk",
3675 "If set to true, RTP Player saves temporary data to "
3676 "temp files on disk. If not set, it uses memory."
3677 "Every stream uses one file therefore you might touch "
3678 "OS limit for count of opened files."
3679 "When ui.rtp_player_use_disk2 is set to true too, it uses "
3680 " two files per RTP stream together."
3681 ,&prefs.gui_rtp_player_use_disk1);
3683 prefs_register_bool_preference(gui_module, "rtp_player_use_disk2",
3684 "RTP Player saves temporary dictionary for data to disk",
3685 "If set to true, RTP Player saves temporary dictionary to "
3686 "temp files on disk. If not set, it uses memory."
3687 "Every stream uses one file therefore you might touch "
3688 "OS limit for count of opened files."
3689 "When ui.rtp_player_use_disk1 is set to true too, it uses "
3690 " two files per RTP stream."
3691 ,&prefs.gui_rtp_player_use_disk2);
3693 prefs_register_enum_preference(gui_layout_module, "gui_packet_list_copy_format_options_for_keyboard_shortcut",
3694 "Allows text to be copied with selected format",
3695 "Allows text to be copied with selected format when copied via keyboard",
3696 (int*)(void*)(&prefs.gui_packet_list_copy_format_options_for_keyboard_shortcut),
3697 gui_packet_list_copy_format_options_for_keyboard_shortcut, false);
3699 prefs_register_bool_preference(gui_layout_module, "gui_packet_list_copy_text_with_aligned_columns",
3700 "Allows text to be copied with aligned columns",
3701 "Allows text to be copied with aligned columns when copied via menu or keyboard",
3702 &prefs.gui_packet_list_copy_text_with_aligned_columns);
3704 prefs_register_bool_preference(gui_layout_module, "packet_list_show_related",
3705 "Show Related Packets",
3706 "Show related packet indicators in the first column",
3707 &prefs.gui_packet_list_show_related);
3709 prefs_register_bool_preference(gui_layout_module, "packet_list_show_minimap",
3710 "Enable Intelligent Scroll Bar",
3711 "Show the intelligent scroll bar (a minimap of packet list colors in the scrollbar)",
3712 &prefs.gui_packet_list_show_minimap);
3714 prefs_register_bool_preference(gui_module, "packet_list_is_sortable",
3715 "Allow packet list to be sortable",
3716 "To prevent sorting by mistake (which can take some time to calculate), it can be disabled",
3717 &prefs.gui_packet_list_sortable);
3719 prefs_register_uint_preference(gui_module, "packet_list_cached_rows_max",
3720 "Maximum cached rows",
3721 "Maximum number of rows that can be sorted by columns that require dissection. Increasing this increases memory consumption by caching column text",
3723 &prefs.gui_packet_list_cached_rows_max);
3725 prefs_register_bool_preference(gui_module, "interfaces_show_hidden",
3726 "Show hidden interfaces",
3727 "Show all interfaces, including interfaces marked as hidden",
3728 &prefs.gui_interfaces_show_hidden);
3730 prefs_register_bool_preference(gui_module, "interfaces_remote_display",
3731 "Show Remote interfaces",
3732 "Show remote interfaces in the interface selection",
3733 &prefs.gui_interfaces_remote_display);
3735 register_string_like_preference(gui_module, "interfaces_hidden_types", "Hide interface types in list",
3736 "Hide the given interface types in the startup list.\n"
3737 "A comma-separated string of interface type values (e.g. 5,9).\n"
3738 "0 = Wired,\n"
3739 "1 = AirPCAP,\n"
3740 "2 = Pipe,\n"
3741 "3 = STDIN,\n"
3742 "4 = Bluetooth,\n"
3743 "5 = Wireless,\n"
3744 "6 = Dial-Up,\n"
3745 "7 = USB,\n"
3746 "8 = External Capture,\n"
3747 "9 = Virtual",
3748 &prefs.gui_interfaces_hide_types, PREF_STRING, NULL, true);
3750 prefs_register_bool_preference(gui_module, "io_graph_automatic_update",
3751 "Enables automatic updates for IO Graph",
3752 "Enables automatic updates for IO Graph",
3753 &prefs.gui_io_graph_automatic_update);
3755 prefs_register_bool_preference(gui_module, "io_graph_enable_legend",
3756 "Enables the legend of IO Graph",
3757 "Enables the legend of IO Graph",
3758 &prefs.gui_io_graph_enable_legend);
3760 prefs_register_bool_preference(gui_module, "show_byteview_in_dialog",
3761 "Show the byte view in the packet details dialog",
3762 "Show the byte view in the packet details dialog",
3763 &prefs.gui_packet_details_show_byteview);
3765 /* Console
3766 * These are preferences that can be read/written using the
3767 * preference module API. These preferences still use their own
3768 * configuration screens for access, but this cuts down on the
3769 * preference "string compare list" in set_pref()
3771 console_module = prefs_register_module(NULL, "console", "Console",
3772 "Console logging and debugging output", NULL, NULL, false);
3774 prefs_register_obsolete_preference(console_module, "log.level");
3776 prefs_register_bool_preference(console_module, "incomplete_dissectors_check_debug",
3777 "Print debug line for incomplete dissectors",
3778 "Look for dissectors that left some bytes undecoded (debug)",
3779 &prefs.incomplete_dissectors_check_debug);
3781 /* Display filter Expressions
3782 * This used to be an array of individual fields that has now been
3783 * converted to a UAT. Just make it part of the GUI category even
3784 * though the name of the preference will never be seen in preference
3785 * file
3787 filter_expression_register_uat(gui_module);
3789 /* Capture
3790 * These are preferences that can be read/written using the
3791 * preference module API. These preferences still use their own
3792 * configuration screens for access, but this cuts down on the
3793 * preference "string compare list" in set_pref()
3795 capture_module = prefs_register_module(NULL, "capture", "Capture",
3796 "Capture preferences", NULL, NULL, false);
3797 /* Capture preferences don't affect dissection */
3798 prefs_set_module_effect_flags(capture_module, PREF_EFFECT_CAPTURE);
3800 register_string_like_preference(capture_module, "device", "Default capture device",
3801 "Default capture device",
3802 &prefs.capture_device, PREF_STRING, NULL, false);
3804 register_string_like_preference(capture_module, "devices_linktypes", "Interface link-layer header type",
3805 "Interface link-layer header types (Ex: en0(1),en1(143),...)",
3806 &prefs.capture_devices_linktypes, PREF_STRING, NULL, false);
3808 register_string_like_preference(capture_module, "devices_descr", "Interface descriptions",
3809 "Interface descriptions (Ex: eth0(eth0 descr),eth1(eth1 descr),...)",
3810 &prefs.capture_devices_descr, PREF_STRING, NULL, false);
3812 register_string_like_preference(capture_module, "devices_hide", "Hide interface",
3813 "Hide interface? (Ex: eth0,eth3,...)",
3814 &prefs.capture_devices_hide, PREF_STRING, NULL, false);
3816 register_string_like_preference(capture_module, "devices_monitor_mode", "Capture in monitor mode",
3817 "By default, capture in monitor mode on interface? (Ex: eth0,eth3,...)",
3818 &prefs.capture_devices_monitor_mode, PREF_STRING, NULL, false);
3820 register_string_like_preference(capture_module, "devices_buffersize", "Interface buffer size",
3821 "Interface buffer size (Ex: en0(1),en1(143),...)",
3822 &prefs.capture_devices_buffersize, PREF_STRING, NULL, false);
3824 register_string_like_preference(capture_module, "devices_snaplen", "Interface snap length",
3825 "Interface snap length (Ex: en0(65535),en1(1430),...)",
3826 &prefs.capture_devices_snaplen, PREF_STRING, NULL, false);
3828 register_string_like_preference(capture_module, "devices_pmode", "Interface promiscuous mode",
3829 "Interface promiscuous mode (Ex: en0(0),en1(1),...)",
3830 &prefs.capture_devices_pmode, PREF_STRING, NULL, false);
3832 prefs_register_bool_preference(capture_module, "prom_mode", "Capture in promiscuous mode",
3833 "Capture in promiscuous mode?", &prefs.capture_prom_mode);
3835 prefs_register_bool_preference(capture_module, "monitor_mode", "Capture in monitor mode on 802.11 devices",
3836 "Capture in monitor mode on all 802.11 devices that support it?", &prefs.capture_monitor_mode);
3838 register_string_like_preference(capture_module, "devices_filter", "Interface capture filter",
3839 "Interface capture filter (Ex: en0(tcp),en1(udp),...)",
3840 &prefs.capture_devices_filter, PREF_STRING, NULL, false);
3842 prefs_register_bool_preference(capture_module, "pcap_ng", "Capture in pcapng format",
3843 "Capture in pcapng format?", &prefs.capture_pcap_ng);
3845 prefs_register_bool_preference(capture_module, "real_time_update", "Update packet list in real time during capture",
3846 "Update packet list in real time during capture?", &prefs.capture_real_time);
3848 prefs_register_uint_preference(capture_module, "update_interval",
3849 "Capture update interval",
3850 "Capture update interval in ms",
3852 &prefs.capture_update_interval);
3854 prefs_register_bool_preference(capture_module, "no_interface_load", "Don't load interfaces on startup",
3855 "Don't automatically load capture interfaces on startup", &prefs.capture_no_interface_load);
3857 prefs_register_bool_preference(capture_module, "no_extcap", "Disable external capture interfaces",
3858 "Disable external capture modules (extcap)", &prefs.capture_no_extcap);
3860 prefs_register_obsolete_preference(capture_module, "auto_scroll");
3862 prefs_register_bool_preference(capture_module, "show_info", "Show capture information dialog while capturing",
3863 "Show capture information dialog while capturing?", &prefs.capture_show_info);
3865 prefs_register_obsolete_preference(capture_module, "syntax_check_filter");
3867 custom_cbs.free_cb = capture_column_free_cb;
3868 custom_cbs.reset_cb = capture_column_reset_cb;
3869 custom_cbs.set_cb = capture_column_set_cb;
3870 custom_cbs.type_name_cb = capture_column_type_name_cb;
3871 custom_cbs.type_description_cb = capture_column_type_description_cb;
3872 custom_cbs.is_default_cb = capture_column_is_default_cb;
3873 custom_cbs.to_str_cb = capture_column_to_str_cb;
3874 prefs_register_list_custom_preference(capture_module, "columns", "Capture options dialog column list",
3875 "List of columns to be displayed", &custom_cbs, capture_column_init_cb, &prefs.capture_columns);
3877 /* Name Resolution */
3878 nameres_module = prefs_register_module(NULL, "nameres", "Name Resolution",
3879 "Name Resolution", "ChCustPreferencesSection.html#ChCustPrefsNameSection", addr_resolve_pref_apply, true);
3880 addr_resolve_pref_init(nameres_module);
3881 oid_pref_init(nameres_module);
3882 maxmind_db_pref_init(nameres_module);
3884 /* Printing
3885 * None of these have any effect; we keep them as obsolete preferences
3886 * in order to avoid errors when reading older preference files.
3888 printing = prefs_register_module(NULL, "print", "Printing",
3889 "Printing", NULL, NULL, false);
3890 prefs_register_obsolete_preference(printing, "format");
3891 prefs_register_obsolete_preference(printing, "command");
3892 prefs_register_obsolete_preference(printing, "file");
3894 /* Codecs */
3895 codecs_module = prefs_register_module(NULL, "codecs", "Codecs",
3896 "Codecs", NULL, NULL, true);
3898 /* Statistics */
3899 stats_module = prefs_register_module(NULL, "statistics", "Statistics",
3900 "Statistics", "ChCustPreferencesSection.html#_statistics", &stats_callback, true);
3902 prefs_register_uint_preference(stats_module, "update_interval",
3903 "Tap update interval in ms",
3904 "Determines time between tap updates",
3906 &prefs.tap_update_interval);
3908 prefs_register_uint_preference(stats_module, "flow_graph_max_export_items",
3909 "Maximum Flow Graph items to export as image",
3910 "The maximum number of Flow Graph items (frames) "
3911 "to include when exporting the graph as an image. "
3912 "Note that some formats (e.g., JPEG) have inherent "
3913 "pixel limits and image viewers might be unable to "
3914 "handle very large images.",
3916 &prefs.flow_graph_max_export_items);
3918 prefs_register_bool_preference(stats_module, "st_enable_burstinfo",
3919 "Enable the calculation of burst information",
3920 "If enabled burst rates will be calculated for statistics that use the stats_tree system. "
3921 "Burst rates are calculated over a much shorter time interval than the rate column.",
3922 &prefs.st_enable_burstinfo);
3924 prefs_register_bool_preference(stats_module, "st_burst_showcount",
3925 "Show burst count for item rather than rate",
3926 "If selected the stats_tree statistics nodes will show the count of events "
3927 "within the burst window instead of a burst rate. Burst rate is calculated "
3928 "as number of events within burst window divided by the burst windown length.",
3929 &prefs.st_burst_showcount);
3931 prefs_register_uint_preference(stats_module, "st_burst_resolution",
3932 "Burst rate resolution (ms)",
3933 "Sets the duration of the time interval into which events are grouped when calculating "
3934 "the burst rate. Higher resolution (smaller number) increases processing overhead.",
3935 10,&prefs.st_burst_resolution);
3937 prefs_register_uint_preference(stats_module, "st_burst_windowlen",
3938 "Burst rate window size (ms)",
3939 "Sets the duration of the sliding window during which the burst rate is "
3940 "measured. Longer window relative to burst rate resolution increases "
3941 "processing overhead. Will be truncated to a multiple of burst resolution.",
3942 10,&prefs.st_burst_windowlen);
3944 prefs_register_enum_preference(stats_module, "st_sort_defcolflag",
3945 "Default sort column for stats_tree stats",
3946 "Sets the default column by which stats based on the stats_tree "
3947 "system is sorted.",
3948 &prefs.st_sort_defcolflag, st_sort_col_vals, false);
3950 prefs_register_bool_preference(stats_module, "st_sort_defdescending",
3951 "Default stats_tree sort order is descending",
3952 "When selected, statistics based on the stats_tree system will by default "
3953 "be sorted in descending order.",
3954 &prefs.st_sort_defdescending);
3956 prefs_register_bool_preference(stats_module, "st_sort_casesensitve",
3957 "Case sensitive sort of stats_tree item names",
3958 "When selected, the item/node names of statistics based on the stats_tree "
3959 "system will be sorted taking case into account. Else the case of the name "
3960 "will be ignored.",
3961 &prefs.st_sort_casesensitve);
3963 prefs_register_bool_preference(stats_module, "st_sort_rng_nameonly",
3964 "Always sort 'range' nodes by name",
3965 "When selected, the stats_tree nodes representing a range of values "
3966 "(0-49, 50-100, etc.) will always be sorted by name (the range of the "
3967 "node). Else range nodes are sorted by the same column as the rest of "
3968 " the tree.",
3969 &prefs.st_sort_rng_nameonly);
3971 prefs_register_bool_preference(stats_module, "st_sort_rng_fixorder",
3972 "Always sort 'range' nodes in ascending order",
3973 "When selected, the stats_tree nodes representing a range of values "
3974 "(0-49, 50-100, etc.) will always be sorted ascending; else it follows "
3975 "the sort direction of the tree. Only effective if \"Always sort "
3976 "'range' nodes by name\" is also selected.",
3977 &prefs.st_sort_rng_fixorder);
3979 prefs_register_bool_preference(stats_module, "st_sort_showfullname",
3980 "Display the full stats_tree plug-in name",
3981 "When selected, the full name (including menu path) of the stats_tree "
3982 "plug-in is show in windows. If cleared the plug-in name is shown "
3983 "without menu path (only the part of the name after last '/' character.)",
3984 &prefs.st_sort_showfullname);
3986 /* Protocols */
3987 protocols_module = prefs_register_module(NULL, "protocols", "Protocols",
3988 "Protocols", "ChCustPreferencesSection.html#ChCustPrefsProtocolsSection", NULL, true);
3990 prefs_register_bool_preference(protocols_module, "display_hidden_proto_items",
3991 "Display hidden protocol items",
3992 "Display all hidden protocol items in the packet list.",
3993 &prefs.display_hidden_proto_items);
3995 prefs_register_bool_preference(protocols_module, "display_byte_fields_with_spaces",
3996 "Display byte fields with a space character between bytes",
3997 "Display all byte fields with a space character between each byte in the packet list.",
3998 &prefs.display_byte_fields_with_spaces);
4001 * Note the -t / option only affects the display of the packet timestamp
4002 * in the default time column; this is for all other absolute times.
4004 prefs_register_enum_preference(protocols_module, "display_abs_time_ascii",
4005 "Format absolute times like asctime",
4006 "When to format absolute times similar to asctime instead of ISO 8601, for backwards compatibility with older Wireshark.",
4007 (int*)&prefs.display_abs_time_ascii, abs_time_format_options, false);
4009 prefs_register_bool_preference(protocols_module, "enable_incomplete_dissectors_check",
4010 "Look for incomplete dissectors",
4011 "Look for dissectors that left some bytes undecoded.",
4012 &prefs.enable_incomplete_dissectors_check);
4014 prefs_register_bool_preference(protocols_module, "strict_conversation_tracking_heuristics",
4015 "Enable stricter conversation tracking heuristics",
4016 "Protocols may use things like VLAN ID or interface ID to narrow the potential for duplicate conversations. "
4017 "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",
4018 &prefs.strict_conversation_tracking_heuristics);
4020 prefs_register_bool_preference(protocols_module, "ignore_dup_frames",
4021 "Ignore duplicate frames",
4022 "Ignore frames that are exact duplicates of any previous frame.",
4023 &prefs.ignore_dup_frames);
4025 prefs_register_enum_preference(protocols_module, "conversation_deinterlacing_key",
4026 "Deinterlacing conversations key",
4027 "Separate into different conversations frames that look like duplicates but have different Interface, MAC, or VLAN field values.",
4028 (int *)&prefs.conversation_deinterlacing_key, conv_deint_options, false);
4030 prefs_register_uint_preference(protocols_module, "ignore_dup_frames_cache_entries",
4031 "The max number of hashes to keep in memory for determining duplicates frames",
4032 "If \"Ignore duplicate frames\" is set, this setting sets the maximum number "
4033 "of cache entries to maintain. A 0 means no limit.",
4034 10, &prefs.ignore_dup_frames_cache_entries);
4037 /* Obsolete preferences
4038 * These "modules" were reorganized/renamed to correspond to their GUI
4039 * configuration screen within the preferences dialog
4042 /* taps is now part of the stats module */
4043 prefs_register_module(NULL, "taps", "TAPS", "TAPS", NULL, NULL, false);
4044 /* packet_list is now part of the protocol (parent) module */
4045 prefs_register_module(NULL, "packet_list", "PACKET_LIST", "PACKET_LIST", NULL, NULL, false);
4046 /* stream is now part of the gui module */
4047 prefs_register_module(NULL, "stream", "STREAM", "STREAM", NULL, NULL, false);
4051 /* Parse through a list of comma-separated, possibly quoted strings.
4052 Return a list of the string data. */
4053 GList *
4054 prefs_get_string_list(const char *str)
4056 enum { PRE_STRING, IN_QUOT, NOT_IN_QUOT };
4058 int state = PRE_STRING, i = 0;
4059 bool backslash = false;
4060 unsigned char cur_c;
4061 const size_t default_size = 64;
4062 GString *slstr = NULL;
4063 GList *sl = NULL;
4065 /* Allocate a buffer for the first string. */
4066 slstr = g_string_sized_new(default_size);
4068 for (;;) {
4069 cur_c = str[i];
4070 if (cur_c == '\0') {
4071 /* It's the end of the input, so it's the end of the string we
4072 were working on, and there's no more input. */
4073 if (state == IN_QUOT || backslash) {
4074 /* We were in the middle of a quoted string or backslash escape,
4075 and ran out of characters; that's an error. */
4076 g_string_free(slstr, TRUE);
4077 prefs_clear_string_list(sl);
4078 return NULL;
4080 if (slstr->len > 0)
4081 sl = g_list_append(sl, g_string_free(slstr, FALSE));
4082 else
4083 g_string_free(slstr, TRUE);
4084 break;
4086 if (cur_c == '"' && !backslash) {
4087 switch (state) {
4088 case PRE_STRING:
4089 /* We hadn't yet started processing a string; this starts the
4090 string, and we're now quoting. */
4091 state = IN_QUOT;
4092 break;
4093 case IN_QUOT:
4094 /* We're in the middle of a quoted string, and we saw a quotation
4095 mark; we're no longer quoting. */
4096 state = NOT_IN_QUOT;
4097 break;
4098 case NOT_IN_QUOT:
4099 /* We're working on a string, but haven't seen a quote; we're
4100 now quoting. */
4101 state = IN_QUOT;
4102 break;
4103 default:
4104 break;
4106 } else if (cur_c == '\\' && !backslash) {
4107 /* We saw a backslash, and the previous character wasn't a
4108 backslash; escape the next character.
4110 This also means we've started a new string. */
4111 backslash = true;
4112 if (state == PRE_STRING)
4113 state = NOT_IN_QUOT;
4114 } else if (cur_c == ',' && state != IN_QUOT && !backslash) {
4115 /* We saw a comma, and we're not in the middle of a quoted string
4116 and it wasn't preceded by a backslash; it's the end of
4117 the string we were working on... */
4118 if (slstr->len > 0) {
4119 sl = g_list_append(sl, g_string_free(slstr, FALSE));
4120 slstr = g_string_sized_new(default_size);
4123 /* ...and the beginning of a new string. */
4124 state = PRE_STRING;
4125 } else if (!g_ascii_isspace(cur_c) || state != PRE_STRING) {
4126 /* Either this isn't a white-space character, or we've started a
4127 string (i.e., already seen a non-white-space character for that
4128 string and put it into the string).
4130 The character is to be put into the string; do so. */
4131 g_string_append_c(slstr, cur_c);
4133 /* If it was backslash-escaped, we're done with the backslash escape. */
4134 backslash = false;
4136 i++;
4138 return(sl);
4141 char *join_string_list(GList *sl)
4143 GString *joined_str = g_string_new("");
4144 GList *cur, *first;
4145 char *str;
4146 unsigned item_count = 0;
4148 cur = first = g_list_first(sl);
4149 while (cur) {
4150 item_count++;
4151 str = (char *)cur->data;
4153 if (cur != first)
4154 g_string_append_c(joined_str, ',');
4156 if (item_count % 2) {
4157 /* Wrap the line. */
4158 g_string_append(joined_str, "\n\t");
4159 } else
4160 g_string_append_c(joined_str, ' ');
4162 g_string_append_c(joined_str, '"');
4163 while (*str) {
4164 gunichar uc = g_utf8_get_char (str);
4166 if (uc == '"' || uc == '\\')
4167 g_string_append_c(joined_str, '\\');
4169 if (g_unichar_isprint(uc))
4170 g_string_append_unichar (joined_str, uc);
4172 str = g_utf8_next_char (str);
4175 g_string_append_c(joined_str, '"');
4177 cur = cur->next;
4179 return g_string_free(joined_str, FALSE);
4182 void
4183 prefs_clear_string_list(GList *sl)
4185 g_list_free_full(sl, g_free);
4189 * Takes a string, a pointer to an array of "enum_val_t"s, and a default int
4190 * value.
4191 * The array must be terminated by an entry with a null "name" string.
4193 * If the string matches a "name" string in an entry, the value from that
4194 * entry is returned.
4196 * Otherwise, if a string matches a "description" string in an entry, the
4197 * value from that entry is returned; we do that for backwards compatibility,
4198 * as we used to have only a "name" string that was used both for command-line
4199 * and configuration-file values and in the GUI (which meant either that
4200 * the GUI had what might be somewhat cryptic values to select from or that
4201 * the "-o" flag took long strings, often with spaces in them).
4203 * Otherwise, the default value that was passed as the third argument is
4204 * returned.
4206 static int
4207 find_val_for_string(const char *needle, const enum_val_t *haystack,
4208 int default_value)
4210 int i;
4212 for (i = 0; haystack[i].name != NULL; i++) {
4213 if (g_ascii_strcasecmp(needle, haystack[i].name) == 0) {
4214 return haystack[i].value;
4217 for (i = 0; haystack[i].name != NULL; i++) {
4218 if (g_ascii_strcasecmp(needle, haystack[i].description) == 0) {
4219 return haystack[i].value;
4222 return default_value;
4225 /* Preferences file format:
4226 * - Configuration directives start at the beginning of the line, and
4227 * are terminated with a colon.
4228 * - Directives can be continued on the next line by preceding them with
4229 * whitespace.
4231 * Example:
4233 # This is a comment line
4234 print.command: lpr
4235 print.file: /a/very/long/path/
4236 to/wireshark-out.ps
4240 /* Initialize non-dissector preferences to wired-in default values Called
4241 * at program startup and any time the profile changes. (The dissector
4242 * preferences are assumed to be set to those values by the dissectors.)
4243 * They may be overridden by the global preferences file or the user's
4244 * preferences file.
4246 static void
4247 init_prefs(void)
4249 if (prefs_initialized)
4250 return;
4252 uat_load_all();
4255 * Ensure the "global" preferences have been initialized so the
4256 * preference API has the proper default values to work from
4258 pre_init_prefs();
4260 prefs_register_modules();
4262 prefs_initialized = true;
4266 * Initialize non-dissector preferences used by the "register preference" API
4267 * to default values so the default values can be used when registered.
4269 * String, filename, and directory preferences will be g_freed so they must
4270 * be g_mallocated.
4272 static void
4273 pre_init_prefs(void)
4275 int i;
4276 char *col_name;
4277 fmt_data *cfmt;
4278 static const char *col_fmt_packets[] = {
4279 "No.", "%m", "Time", "%t",
4280 "Source", "%s", "Destination", "%d",
4281 "Protocol", "%p", "Length", "%L",
4282 "Info", "%i" };
4283 static const char **col_fmt = col_fmt_packets;
4284 int num_cols = 7;
4286 if (application_flavor_is_stratoshark()) {
4287 static const char *col_fmt_logs[] = {
4288 "No.", "%m",
4289 "Time", "%t",
4290 "Event name", "%Cus:sysdig.event_name:0:R",
4291 "Dir", "%Cus:evt.dir:0:R",
4292 "Proc Name", "%Cus:proc.name:0:R",
4293 "PID", "%Cus:proc.pid:0:R",
4294 "TID", "%Cus:thread.tid:0:R",
4295 "FD", "%Cus:fd.num:0:R",
4296 "FD Name", "%Cus:fd.name:0:R",
4297 "Container Name", "%Cus:container.name:0:R",
4298 "Arguments", "%Cus:evt.args:0:R",
4299 "Info", "%i"
4301 col_fmt = col_fmt_logs;
4302 num_cols = 12;
4305 prefs.restore_filter_after_following_stream = false;
4306 prefs.gui_toolbar_main_style = TB_STYLE_ICONS;
4307 /* We try to find the best font in the Qt code */
4308 g_free(prefs.gui_font_name);
4309 prefs.gui_font_name = g_strdup("");
4310 prefs.gui_active_fg.red = 0;
4311 prefs.gui_active_fg.green = 0;
4312 prefs.gui_active_fg.blue = 0;
4313 prefs.gui_active_bg.red = 52223;
4314 prefs.gui_active_bg.green = 59647;
4315 prefs.gui_active_bg.blue = 65535;
4316 prefs.gui_active_style = COLOR_STYLE_DEFAULT;
4317 prefs.gui_inactive_fg.red = 0;
4318 prefs.gui_inactive_fg.green = 0;
4319 prefs.gui_inactive_fg.blue = 0;
4320 prefs.gui_inactive_bg.red = 61439;
4321 prefs.gui_inactive_bg.green = 61439;
4322 prefs.gui_inactive_bg.blue = 61439;
4323 prefs.gui_inactive_style = COLOR_STYLE_DEFAULT;
4324 prefs.gui_marked_fg.red = 65535;
4325 prefs.gui_marked_fg.green = 65535;
4326 prefs.gui_marked_fg.blue = 65535;
4327 prefs.gui_marked_bg.red = 0;
4328 prefs.gui_marked_bg.green = 8224;
4329 prefs.gui_marked_bg.blue = 10794;
4330 prefs.gui_ignored_fg.red = 32767;
4331 prefs.gui_ignored_fg.green = 32767;
4332 prefs.gui_ignored_fg.blue = 32767;
4333 prefs.gui_ignored_bg.red = 65535;
4334 prefs.gui_ignored_bg.green = 65535;
4335 prefs.gui_ignored_bg.blue = 65535;
4336 g_free(prefs.gui_colorized_fg);
4337 prefs.gui_colorized_fg = g_strdup("000000,000000,000000,000000,000000,000000,000000,000000,000000,000000");
4338 g_free(prefs.gui_colorized_bg);
4339 prefs.gui_colorized_bg = g_strdup("ffc0c0,ffc0ff,e0c0e0,c0c0ff,c0e0e0,c0ffff,c0ffc0,ffffc0,e0e0c0,e0e0e0");
4340 prefs.st_client_fg.red = 32767;
4341 prefs.st_client_fg.green = 0;
4342 prefs.st_client_fg.blue = 0;
4343 prefs.st_client_bg.red = 64507;
4344 prefs.st_client_bg.green = 60909;
4345 prefs.st_client_bg.blue = 60909;
4346 prefs.st_server_fg.red = 0;
4347 prefs.st_server_fg.green = 0;
4348 prefs.st_server_fg.blue = 32767;
4349 prefs.st_server_bg.red = 60909;
4350 prefs.st_server_bg.green = 60909;
4351 prefs.st_server_bg.blue = 64507;
4353 if (gui_theme_is_dark) {
4354 // Green, red and yellow with HSV V = 84
4355 prefs.gui_text_valid.red = 0x0000; /* dark green */
4356 prefs.gui_text_valid.green = 0x66ff;
4357 prefs.gui_text_valid.blue = 0x0000;
4358 prefs.gui_text_invalid.red = 0x66FF; /* dark red */
4359 prefs.gui_text_invalid.green = 0x0000;
4360 prefs.gui_text_invalid.blue = 0x0000;
4361 prefs.gui_text_deprecated.red = 0x66FF; /* dark yellow / olive */
4362 prefs.gui_text_deprecated.green = 0x66FF;
4363 prefs.gui_text_deprecated.blue = 0x0000;
4364 } else {
4365 // Green, red and yellow with HSV V = 20
4366 prefs.gui_text_valid.red = 0xAFFF; /* light green */
4367 prefs.gui_text_valid.green = 0xFFFF;
4368 prefs.gui_text_valid.blue = 0xAFFF;
4369 prefs.gui_text_invalid.red = 0xFFFF; /* light red */
4370 prefs.gui_text_invalid.green = 0xAFFF;
4371 prefs.gui_text_invalid.blue = 0xAFFF;
4372 prefs.gui_text_deprecated.red = 0xFFFF; /* light yellow */
4373 prefs.gui_text_deprecated.green = 0xFFFF;
4374 prefs.gui_text_deprecated.blue = 0xAFFF;
4377 prefs.gui_geometry_save_position = true;
4378 prefs.gui_geometry_save_size = true;
4379 prefs.gui_geometry_save_maximized= true;
4380 prefs.gui_fileopen_style = FO_STYLE_LAST_OPENED;
4381 prefs.gui_recent_df_entries_max = 10;
4382 prefs.gui_recent_files_count_max = 10;
4383 g_free(prefs.gui_fileopen_dir);
4384 prefs.gui_fileopen_dir = g_strdup(get_persdatafile_dir());
4385 prefs.gui_fileopen_preview = 3;
4386 g_free(prefs.gui_tlskeylog_command);
4387 prefs.gui_tlskeylog_command = g_strdup("");
4388 prefs.gui_ask_unsaved = true;
4389 prefs.gui_autocomplete_filter = true;
4390 prefs.gui_find_wrap = true;
4391 prefs.gui_update_enabled = true;
4392 prefs.gui_update_channel = UPDATE_CHANNEL_STABLE;
4393 prefs.gui_update_interval = 60*60*24; /* Seconds */
4394 prefs.gui_debounce_timer = 400; /* milliseconds */
4395 g_free(prefs.gui_window_title);
4396 prefs.gui_window_title = g_strdup("");
4397 g_free(prefs.gui_prepend_window_title);
4398 prefs.gui_prepend_window_title = g_strdup("");
4399 g_free(prefs.gui_start_title);
4400 prefs.gui_start_title = g_strdup("The World's Most Popular Network Protocol Analyzer");
4401 prefs.gui_version_placement = version_both;
4402 prefs.gui_welcome_page_show_recent = true;
4403 prefs.gui_layout_type = layout_type_2;
4404 prefs.gui_layout_content_1 = layout_pane_content_plist;
4405 prefs.gui_layout_content_2 = layout_pane_content_pdetails;
4406 prefs.gui_layout_content_3 = layout_pane_content_pbytes;
4407 prefs.gui_packet_list_elide_mode = ELIDE_RIGHT;
4408 prefs.gui_packet_list_copy_format_options_for_keyboard_shortcut = COPY_FORMAT_TEXT;
4409 prefs.gui_packet_list_copy_text_with_aligned_columns = false;
4410 prefs.gui_packet_list_show_related = true;
4411 prefs.gui_packet_list_show_minimap = true;
4412 prefs.gui_packet_list_sortable = true;
4413 prefs.gui_packet_list_cached_rows_max = 10000;
4414 g_free (prefs.gui_interfaces_hide_types);
4415 prefs.gui_interfaces_hide_types = g_strdup("");
4416 prefs.gui_interfaces_show_hidden = false;
4417 prefs.gui_interfaces_remote_display = true;
4418 prefs.gui_packet_list_separator = false;
4419 prefs.gui_packet_header_column_definition = true;
4420 prefs.gui_packet_list_hover_style = true;
4421 prefs.gui_show_selected_packet = false;
4422 prefs.gui_show_file_load_time = false;
4423 prefs.gui_max_export_objects = 1000;
4424 prefs.gui_max_tree_items = 1 * 1000 * 1000;
4425 prefs.gui_max_tree_depth = 5 * 100;
4426 prefs.gui_decimal_places1 = DEF_GUI_DECIMAL_PLACES1;
4427 prefs.gui_decimal_places2 = DEF_GUI_DECIMAL_PLACES2;
4428 prefs.gui_decimal_places3 = DEF_GUI_DECIMAL_PLACES3;
4430 if (prefs.col_list) {
4431 free_col_info(prefs.col_list);
4432 prefs.col_list = NULL;
4434 for (i = 0; i < num_cols; i++) {
4435 cfmt = g_new0(fmt_data,1);
4436 cfmt->title = g_strdup(col_fmt[i * 2]);
4437 cfmt->visible = true;
4438 cfmt->display = COLUMN_DISPLAY_STRINGS;
4439 parse_column_format(cfmt, col_fmt[(i * 2) + 1]);
4440 prefs.col_list = g_list_append(prefs.col_list, cfmt);
4442 prefs.num_cols = num_cols;
4444 /* set the default values for the capture dialog box */
4445 prefs.capture_prom_mode = true;
4446 prefs.capture_monitor_mode = false;
4447 prefs.capture_pcap_ng = true;
4448 prefs.capture_real_time = true;
4449 prefs.capture_update_interval = DEFAULT_UPDATE_INTERVAL;
4450 prefs.capture_no_extcap = false;
4451 prefs.capture_show_info = false;
4453 if (!prefs.capture_columns) {
4454 /* First time through */
4455 for (i = 0; i < num_capture_cols; i++) {
4456 col_name = g_strdup(capture_cols[i]);
4457 prefs.capture_columns = g_list_append(prefs.capture_columns, col_name);
4461 /* set the default values for the tap/statistics dialog box */
4462 prefs.tap_update_interval = TAP_UPDATE_DEFAULT_INTERVAL;
4463 prefs.flow_graph_max_export_items = 1000;
4464 prefs.st_enable_burstinfo = true;
4465 prefs.st_burst_showcount = false;
4466 prefs.st_burst_resolution = ST_DEF_BURSTRES;
4467 prefs.st_burst_windowlen = ST_DEF_BURSTLEN;
4468 prefs.st_sort_casesensitve = true;
4469 prefs.st_sort_rng_fixorder = true;
4470 prefs.st_sort_rng_nameonly = true;
4471 prefs.st_sort_defcolflag = ST_SORT_COL_COUNT;
4472 prefs.st_sort_defdescending = true;
4473 prefs.st_sort_showfullname = false;
4475 /* protocols */
4476 prefs.display_hidden_proto_items = false;
4477 prefs.display_byte_fields_with_spaces = false;
4478 prefs.display_abs_time_ascii = ABS_TIME_ASCII_TREE;
4479 prefs.ignore_dup_frames = false;
4480 prefs.ignore_dup_frames_cache_entries = 10000;
4482 /* set the default values for the io graph dialog */
4483 prefs.gui_io_graph_automatic_update = true;
4484 prefs.gui_io_graph_enable_legend = true;
4486 /* set the default values for the packet dialog */
4487 prefs.gui_packet_dialog_layout = layout_vertical;
4488 prefs.gui_packet_details_show_byteview = true;
4492 * Reset a single dissector preference.
4494 void
4495 reset_pref(pref_t *pref)
4497 int type;
4498 if (!pref) return;
4500 type = pref->type;
4503 * This preference is no longer supported; it's not a
4504 * real preference, so we don't reset it (i.e., we
4505 * treat it as if it weren't found in the list of
4506 * preferences, and we weren't called in the first place).
4508 if (IS_PREF_OBSOLETE(type))
4509 return;
4510 else
4511 RESET_PREF_OBSOLETE(type);
4513 switch (type) {
4515 case PREF_UINT:
4516 *pref->varp.uint = pref->default_val.uint;
4517 break;
4519 case PREF_BOOL:
4520 *pref->varp.boolp = pref->default_val.boolval;
4521 break;
4523 case PREF_ENUM:
4524 case PREF_PROTO_TCP_SNDAMB_ENUM:
4525 *pref->varp.enump = pref->default_val.enumval;
4526 break;
4528 case PREF_STRING:
4529 case PREF_SAVE_FILENAME:
4530 case PREF_OPEN_FILENAME:
4531 case PREF_DIRNAME:
4532 case PREF_PASSWORD:
4533 case PREF_DISSECTOR:
4534 reset_string_like_preference(pref);
4535 break;
4537 case PREF_RANGE:
4538 case PREF_DECODE_AS_RANGE:
4539 wmem_free(wmem_epan_scope(), *pref->varp.range);
4540 *pref->varp.range = range_copy(wmem_epan_scope(), pref->default_val.range);
4541 break;
4543 case PREF_STATIC_TEXT:
4544 case PREF_UAT:
4545 /* Nothing to do */
4546 break;
4548 case PREF_COLOR:
4549 *pref->varp.colorp = pref->default_val.color;
4550 break;
4552 case PREF_CUSTOM:
4553 pref->custom_cbs.reset_cb(pref);
4554 break;
4558 static void
4559 reset_pref_cb(void *data, void *user_data)
4561 pref_t *pref = (pref_t *) data;
4562 module_t *module = (module_t *)user_data;
4564 if (pref && (pref->type == PREF_RANGE || pref->type == PREF_DECODE_AS_RANGE)) {
4566 * Some dissectors expect the range (returned via prefs_get_range_value)
4567 * to remain valid if it has not changed. If it did change, then we
4568 * should set "prefs_changed_flags" to ensure that the preference apply
4569 * callback is invoked. That callback will notify dissectors that it
4570 * should no longer assume the range to be valid.
4572 if (ranges_are_equal(*pref->varp.range, pref->default_val.range)) {
4573 /* Optimization: do not invoke apply callback if nothing changed. */
4574 return;
4576 module->prefs_changed_flags |= prefs_get_effect_flags(pref);
4578 reset_pref(pref);
4582 * Reset all preferences for a module.
4584 static bool
4585 reset_module_prefs(const void *key _U_, void *value, void *data _U_)
4587 module_t *module = (module_t *)value;
4588 g_list_foreach(module->prefs, reset_pref_cb, module);
4589 return false;
4592 /* Reset preferences */
4593 void
4594 prefs_reset(void)
4596 prefs_initialized = false;
4597 g_free(prefs.saved_at_version);
4598 prefs.saved_at_version = NULL;
4601 * Unload all UAT preferences.
4603 uat_unload_all();
4606 * Unload any loaded MIBs.
4608 oids_cleanup();
4611 * Reset the non-dissector preferences.
4613 init_prefs();
4616 * Reset the non-UAT dissector preferences.
4618 wmem_tree_foreach(prefs_modules, reset_module_prefs, NULL);
4621 #ifdef _WIN32
4622 static void
4623 read_registry(void)
4625 HKEY hTestKey;
4626 DWORD data;
4627 DWORD data_size = sizeof(DWORD);
4628 DWORD ret;
4630 ret = RegOpenKeyExA(HKEY_CURRENT_USER, REG_HKCU_WIRESHARK_KEY, 0, KEY_READ, &hTestKey);
4631 if (ret != ERROR_SUCCESS && ret != ERROR_FILE_NOT_FOUND) {
4632 ws_noisy("Cannot open HKCU "REG_HKCU_WIRESHARK_KEY": 0x%lx", ret);
4633 return;
4636 ret = RegQueryValueExA(hTestKey, LOG_HKCU_CONSOLE_OPEN, NULL, NULL, (LPBYTE)&data, &data_size);
4637 if (ret == ERROR_SUCCESS) {
4638 ws_log_console_open = (ws_log_console_open_pref)data;
4639 ws_noisy("Got "LOG_HKCU_CONSOLE_OPEN" from Windows registry: %d", ws_log_console_open);
4641 else if (ret != ERROR_FILE_NOT_FOUND) {
4642 ws_noisy("Error reading registry key "LOG_HKCU_CONSOLE_OPEN": 0x%lx", ret);
4645 RegCloseKey(hTestKey);
4647 #endif
4649 void
4650 prefs_read_module(const char *module)
4652 int err;
4653 char *pf_path;
4654 FILE *pf;
4656 module_t *target_module = prefs_find_module(module);
4657 if (!target_module) {
4658 return;
4661 /* Construct the pathname of the user's preferences file for the module. */
4662 char *pf_name = wmem_strdup_printf(NULL, "%s.cfg", module);
4663 pf_path = get_persconffile_path(pf_name, true);
4664 wmem_free(NULL, pf_name);
4666 /* Read the user's module preferences file, if it exists and is not a dir. */
4667 if (!test_for_regular_file(pf_path) || ((pf = ws_fopen(pf_path, "r")) == NULL)) {
4668 g_free(pf_path);
4669 /* Fall back to the user's generic preferences file. */
4670 pf_path = get_persconffile_path(PF_NAME, true);
4671 pf = ws_fopen(pf_path, "r");
4674 if (pf != NULL) {
4675 /* We succeeded in opening it; read it. */
4676 err = read_prefs_file(pf_path, pf, set_pref, target_module);
4677 if (err != 0) {
4678 /* We had an error reading the file; report it. */
4679 report_warning("Error reading your preferences file \"%s\": %s.",
4680 pf_path, g_strerror(err));
4681 } else
4682 g_free(pf_path);
4683 fclose(pf);
4684 } else {
4685 /* We failed to open it. If we failed for some reason other than
4686 "it doesn't exist", return the errno and the pathname, so our
4687 caller can report the error. */
4688 if (errno != ENOENT) {
4689 report_warning("Can't open your preferences file \"%s\": %s.",
4690 pf_path, g_strerror(errno));
4691 } else
4692 g_free(pf_path);
4695 return;
4698 /* Read the preferences file, fill in "prefs", and return a pointer to it.
4700 If we got an error (other than "it doesn't exist") we report it through
4701 the UI. */
4702 e_prefs *
4703 read_prefs(void)
4705 int err;
4706 char *pf_path;
4707 FILE *pf;
4709 /* clean up libsmi structures before reading prefs */
4710 oids_cleanup();
4712 init_prefs();
4714 #ifdef _WIN32
4715 read_registry();
4716 #endif
4719 * If we don't already have the pathname of the global preferences
4720 * file, construct it. Then, in either case, try to open the file.
4722 if (gpf_path == NULL) {
4724 * We don't have the path; try the new path first, and, if that
4725 * file doesn't exist, try the old path.
4727 gpf_path = get_datafile_path(PF_NAME);
4728 if ((pf = ws_fopen(gpf_path, "r")) == NULL && errno == ENOENT) {
4730 * It doesn't exist by the new name; try the old name.
4732 g_free(gpf_path);
4733 gpf_path = get_datafile_path(OLD_GPF_NAME);
4734 pf = ws_fopen(gpf_path, "r");
4736 } else {
4738 * We have the path; try it.
4740 pf = ws_fopen(gpf_path, "r");
4744 * If we were able to open the file, read it.
4745 * XXX - if it failed for a reason other than "it doesn't exist",
4746 * report the error.
4748 if (pf != NULL) {
4750 * Start out the counters of "mgcp.{tcp,udp}.port" entries we've
4751 * seen.
4753 mgcp_tcp_port_count = 0;
4754 mgcp_udp_port_count = 0;
4756 /* We succeeded in opening it; read it. */
4757 err = read_prefs_file(gpf_path, pf, set_pref, NULL);
4758 if (err != 0) {
4759 /* We had an error reading the file; report it. */
4760 report_warning("Error reading global preferences file \"%s\": %s.",
4761 gpf_path, g_strerror(err));
4763 fclose(pf);
4764 } else {
4765 /* We failed to open it. If we failed for some reason other than
4766 "it doesn't exist", report the error. */
4767 if (errno != ENOENT) {
4768 if (errno != 0) {
4769 report_warning("Can't open global preferences file \"%s\": %s.",
4770 gpf_path, g_strerror(errno));
4775 /* Construct the pathname of the user's preferences file. */
4776 pf_path = get_persconffile_path(PF_NAME, true);
4778 /* Read the user's preferences file, if it exists. */
4779 if ((pf = ws_fopen(pf_path, "r")) != NULL) {
4781 * Start out the counters of "mgcp.{tcp,udp}.port" entries we've
4782 * seen.
4784 mgcp_tcp_port_count = 0;
4785 mgcp_udp_port_count = 0;
4787 /* We succeeded in opening it; read it. */
4788 err = read_prefs_file(pf_path, pf, set_pref, NULL);
4789 if (err != 0) {
4790 /* We had an error reading the file; report it. */
4791 report_warning("Error reading your preferences file \"%s\": %s.",
4792 pf_path, g_strerror(err));
4793 } else
4794 g_free(pf_path);
4795 fclose(pf);
4796 } else {
4797 /* We failed to open it. If we failed for some reason other than
4798 "it doesn't exist", return the errno and the pathname, so our
4799 caller can report the error. */
4800 if (errno != ENOENT) {
4801 report_warning("Can't open your preferences file \"%s\": %s.",
4802 pf_path, g_strerror(errno));
4803 } else
4804 g_free(pf_path);
4807 /* load SMI modules if needed */
4808 oids_init();
4810 return &prefs;
4813 /* read the preferences file (or similar) and call the callback
4814 * function to set each key/value pair found */
4816 read_prefs_file(const char *pf_path, FILE *pf,
4817 pref_set_pair_cb pref_set_pair_fct, void *private_data)
4819 enum {
4820 START, /* beginning of a line */
4821 IN_VAR, /* processing key name */
4822 PRE_VAL, /* finished processing key name, skipping white space before value */
4823 IN_VAL, /* processing value */
4824 IN_SKIP /* skipping to the end of the line */
4825 } state = START;
4826 int got_c;
4827 GString *cur_val;
4828 GString *cur_var;
4829 bool got_val = false;
4830 int fline = 1, pline = 1;
4831 char hint[] = "(save preferences to remove this warning)";
4832 char ver[128];
4834 cur_val = g_string_new("");
4835 cur_var = g_string_new("");
4837 /* Try to read in the profile name in the first line of the preferences file. */
4838 if (fscanf(pf, "# Configuration file for %127[^\r\n]", ver) == 1) {
4839 /* Assume trailing period and remove it */
4840 g_free(prefs.saved_at_version);
4841 prefs.saved_at_version = g_strndup(ver, strlen(ver) - 1);
4843 rewind(pf);
4845 while ((got_c = ws_getc_unlocked(pf)) != EOF) {
4846 if (got_c == '\r') {
4847 /* Treat CR-LF at the end of a line like LF, so that if we're reading
4848 * a Windows-format file on UN*X, we handle it the same way we'd handle
4849 * a UN*X-format file. */
4850 got_c = ws_getc_unlocked(pf);
4851 if (got_c == EOF)
4852 break;
4853 if (got_c != '\n') {
4854 /* Put back the character after the CR, and process the CR normally. */
4855 ungetc(got_c, pf);
4856 got_c = '\r';
4859 if (got_c == '\n') {
4860 state = START;
4861 fline++;
4862 continue;
4865 switch (state) {
4866 case START:
4867 if (g_ascii_isalnum(got_c)) {
4868 if (cur_var->len > 0) {
4869 if (got_val) {
4870 if (cur_val->len > 0) {
4871 if (cur_val->str[cur_val->len-1] == ',') {
4873 * If the pref has a trailing comma, eliminate it.
4875 cur_val->str[cur_val->len-1] = '\0';
4876 ws_warning("%s line %d: trailing comma in \"%s\" %s", pf_path, pline, cur_var->str, hint);
4879 /* Call the routine to set the preference; it will parse
4880 the value as appropriate.
4882 Since we're reading a file, rather than processing
4883 explicit user input, for range preferences, silently
4884 lower values in excess of the range's maximum, rather
4885 than reporting errors and failing. */
4886 switch (pref_set_pair_fct(cur_var->str, cur_val->str, private_data, false)) {
4888 case PREFS_SET_OK:
4889 break;
4891 case PREFS_SET_SYNTAX_ERR:
4892 report_warning("Syntax error in preference \"%s\" at line %d of\n%s %s",
4893 cur_var->str, pline, pf_path, hint);
4894 break;
4896 case PREFS_SET_NO_SUCH_PREF:
4897 ws_warning("No such preference \"%s\" at line %d of\n%s %s",
4898 cur_var->str, pline, pf_path, hint);
4899 prefs.unknown_prefs = true;
4900 break;
4902 case PREFS_SET_OBSOLETE:
4904 * If an attempt is made to save the
4905 * preferences, a popup warning will be
4906 * displayed stating that obsolete prefs
4907 * have been detected and the user will
4908 * be given the opportunity to save these
4909 * prefs under a different profile name.
4910 * The prefs in question need to be listed
4911 * in the console window so that the
4912 * user can make an informed choice.
4914 ws_warning("Obsolete preference \"%s\" at line %d of\n%s %s",
4915 cur_var->str, pline, pf_path, hint);
4916 prefs.unknown_prefs = true;
4917 break;
4919 } else {
4920 ws_warning("Incomplete preference at line %d: of\n%s %s", pline, pf_path, hint);
4923 state = IN_VAR;
4924 got_val = false;
4925 g_string_truncate(cur_var, 0);
4926 g_string_append_c(cur_var, (char) got_c);
4927 pline = fline;
4928 } else if (g_ascii_isspace(got_c) && cur_var->len > 0 && got_val) {
4929 state = PRE_VAL;
4930 } else if (got_c == '#') {
4931 state = IN_SKIP;
4932 } else {
4933 ws_warning("Malformed preference at line %d of\n%s %s", fline, pf_path, hint);
4935 break;
4936 case IN_VAR:
4937 if (got_c != ':') {
4938 g_string_append_c(cur_var, (char) got_c);
4939 } else {
4940 /* This is a colon (':') */
4941 state = PRE_VAL;
4942 g_string_truncate(cur_val, 0);
4944 * Set got_val to true to accommodate prefs such as
4945 * "gui.fileopen.dir" that do not require a value.
4947 got_val = true;
4949 break;
4950 case PRE_VAL:
4951 if (!g_ascii_isspace(got_c)) {
4952 state = IN_VAL;
4953 g_string_append_c(cur_val, (char) got_c);
4955 break;
4956 case IN_VAL:
4957 g_string_append_c(cur_val, (char) got_c);
4958 break;
4959 case IN_SKIP:
4960 break;
4963 if (cur_var->len > 0) {
4964 if (got_val) {
4965 /* Call the routine to set the preference; it will parse
4966 the value as appropriate.
4968 Since we're reading a file, rather than processing
4969 explicit user input, for range preferences, silently
4970 lower values in excess of the range's maximum, rather
4971 than reporting errors and failing. */
4972 switch (pref_set_pair_fct(cur_var->str, cur_val->str, private_data, false)) {
4974 case PREFS_SET_OK:
4975 break;
4977 case PREFS_SET_SYNTAX_ERR:
4978 ws_warning("Syntax error in preference %s at line %d of\n%s %s",
4979 cur_var->str, pline, pf_path, hint);
4980 break;
4982 case PREFS_SET_NO_SUCH_PREF:
4983 ws_warning("No such preference \"%s\" at line %d of\n%s %s",
4984 cur_var->str, pline, pf_path, hint);
4985 prefs.unknown_prefs = true;
4986 break;
4988 case PREFS_SET_OBSOLETE:
4989 prefs.unknown_prefs = true;
4990 break;
4992 } else {
4993 ws_warning("Incomplete preference at line %d of\n%s %s",
4994 pline, pf_path, hint);
4998 g_string_free(cur_val, TRUE);
4999 g_string_free(cur_var, TRUE);
5001 if (ferror(pf))
5002 return errno;
5003 else
5004 return 0;
5008 * If we were handed a preference starting with "uat:", try to turn it into
5009 * a valid uat entry.
5011 static bool
5012 prefs_set_uat_pref(char *uat_entry, char **errmsg) {
5013 char *p, *colonp;
5014 uat_t *uat;
5015 bool ret;
5017 colonp = strchr(uat_entry, ':');
5018 if (colonp == NULL)
5019 return false;
5021 p = colonp;
5022 *p++ = '\0';
5025 * Skip over any white space (there probably won't be any, but
5026 * as we allow it in the preferences file, we might as well
5027 * allow it here).
5029 while (g_ascii_isspace(*p))
5030 p++;
5031 if (*p == '\0') {
5033 * Put the colon back, so if our caller uses, in an
5034 * error message, the string they passed us, the message
5035 * looks correct.
5037 *colonp = ':';
5038 return false;
5041 uat = uat_find(uat_entry);
5042 *colonp = ':';
5043 if (uat == NULL) {
5044 *errmsg = g_strdup("Unknown preference");
5045 return false;
5048 ret = uat_load_str(uat, p, errmsg);
5049 return ret;
5053 * Given a string of the form "<pref name>:<pref value>", as might appear
5054 * as an argument to a "-o" option, parse it and set the preference in
5055 * question. Return an indication of whether it succeeded or failed
5056 * in some fashion.
5058 prefs_set_pref_e
5059 prefs_set_pref(char *prefarg, char **errmsg)
5061 char *p, *colonp;
5062 prefs_set_pref_e ret;
5065 * Set the counters of "mgcp.{tcp,udp}.port" entries we've
5066 * seen to values that keep us from trying to interpret them
5067 * as "mgcp.{tcp,udp}.gateway_port" or "mgcp.{tcp,udp}.callagent_port",
5068 * as, from the command line, we have no way of guessing which
5069 * the user had in mind.
5071 mgcp_tcp_port_count = -1;
5072 mgcp_udp_port_count = -1;
5074 *errmsg = NULL;
5076 colonp = strchr(prefarg, ':');
5077 if (colonp == NULL)
5078 return PREFS_SET_SYNTAX_ERR;
5080 p = colonp;
5081 *p++ = '\0';
5084 * Skip over any white space (there probably won't be any, but
5085 * as we allow it in the preferences file, we might as well
5086 * allow it here).
5088 while (g_ascii_isspace(*p))
5089 p++;
5090 /* The empty string is a legal value for range preferences (PREF_RANGE,
5091 * PREF_DECODE_AS_RANGE), and string-like preferences (PREF_STRING,
5092 * PREF_SAVE_FILENAME, PREF_OPEN_FILENAME, PREF_DIRNAME), indeed often
5093 * not just useful but the default. A user might have a value saved
5094 * to their preference file but want to override it to default behavior.
5095 * Individual preference handlers of those types should be prepared to
5096 * deal with an empty string. For other types, it is up to set_pref() to
5097 * test for the empty string and set PREFS_SET_SYNTAX_ERROR there.
5099 if (strcmp(prefarg, "uat")) {
5100 ret = set_pref(prefarg, p, NULL, true);
5101 } else {
5102 ret = prefs_set_uat_pref(p, errmsg) ? PREFS_SET_OK : PREFS_SET_SYNTAX_ERR;
5104 *colonp = ':'; /* put the colon back */
5105 return ret;
5108 unsigned prefs_get_uint_value(pref_t *pref, pref_source_t source)
5110 switch (source)
5112 case pref_default:
5113 return pref->default_val.uint;
5114 case pref_stashed:
5115 return pref->stashed_val.uint;
5116 case pref_current:
5117 return *pref->varp.uint;
5118 default:
5119 ws_assert_not_reached();
5120 break;
5123 return 0;
5126 char* prefs_get_password_value(pref_t *pref, pref_source_t source)
5128 return prefs_get_string_value(pref, source);
5132 unsigned int prefs_set_uint_value(pref_t *pref, unsigned value, pref_source_t source)
5134 unsigned int changed = 0;
5135 switch (source)
5137 case pref_default:
5138 if (pref->default_val.uint != value) {
5139 pref->default_val.uint = value;
5140 changed = prefs_get_effect_flags(pref);
5142 break;
5143 case pref_stashed:
5144 if (pref->stashed_val.uint != value) {
5145 pref->stashed_val.uint = value;
5146 changed = prefs_get_effect_flags(pref);
5148 break;
5149 case pref_current:
5150 if (*pref->varp.uint != value) {
5151 *pref->varp.uint = value;
5152 changed = prefs_get_effect_flags(pref);
5154 break;
5155 default:
5156 ws_assert_not_reached();
5157 break;
5160 return changed;
5164 * For use by UI code that sets preferences.
5166 unsigned int
5167 prefs_set_password_value(pref_t *pref, const char* value, pref_source_t source)
5169 return prefs_set_string_value(pref, value, source);
5173 unsigned prefs_get_uint_base(pref_t *pref)
5175 return pref->info.base;
5179 * Returns true if the given device is hidden
5181 bool
5182 prefs_is_capture_device_hidden(const char *name)
5184 char *tok, *devices;
5185 size_t len;
5187 if (prefs.capture_devices_hide && name) {
5188 devices = g_strdup (prefs.capture_devices_hide);
5189 len = strlen (name);
5190 for (tok = strtok (devices, ","); tok; tok = strtok(NULL, ",")) {
5191 if (strlen (tok) == len && strcmp (name, tok) == 0) {
5192 g_free (devices);
5193 return true;
5196 g_free (devices);
5199 return false;
5203 * Returns true if the given column is visible (not hidden)
5205 static bool
5206 prefs_is_column_visible(const char *cols_hidden, int col)
5208 char *tok, *cols, *p;
5209 int cidx;
5212 * Do we have a list of hidden columns?
5214 if (cols_hidden) {
5216 * Yes - check the column against each of the ones in the
5217 * list.
5219 cols = g_strdup(cols_hidden);
5220 for (tok = strtok(cols, ","); tok; tok = strtok(NULL, ",")) {
5221 tok = g_strstrip(tok);
5223 cidx = (int)strtol(tok, &p, 10);
5224 if (p == tok || *p != '\0') {
5225 continue;
5227 if (cidx != col) {
5228 continue;
5231 * OK, they match, so it's one of the hidden fields,
5232 * hence not visible.
5234 g_free(cols);
5235 return false;
5237 g_free(cols);
5241 * No - either there are no hidden columns or this isn't one
5242 * of them - so it is visible.
5244 return true;
5248 * Returns true if the given column is visible (not hidden)
5250 static bool
5251 prefs_is_column_fmt_visible(const char *cols_hidden, fmt_data *cfmt)
5253 char *tok, *cols;
5254 fmt_data cfmt_hidden;
5257 * Do we have a list of hidden columns?
5259 if (cols_hidden) {
5261 * Yes - check the column against each of the ones in the
5262 * list.
5264 cols = g_strdup(cols_hidden);
5265 for (tok = strtok(cols, ","); tok; tok = strtok(NULL, ",")) {
5266 tok = g_strstrip(tok);
5269 * Parse this column format.
5271 if (!parse_column_format(&cfmt_hidden, tok)) {
5273 * It's not valid; ignore it.
5275 continue;
5279 * Does it match the column?
5281 if (cfmt->fmt != cfmt_hidden.fmt) {
5282 /* No. */
5283 g_free(cfmt_hidden.custom_fields);
5284 cfmt_hidden.custom_fields = NULL;
5285 continue;
5287 if (cfmt->fmt == COL_CUSTOM) {
5289 * A custom column has to have the same custom field
5290 * and occurrence.
5292 if (cfmt_hidden.custom_fields && cfmt->custom_fields) {
5293 if (strcmp(cfmt->custom_fields,
5294 cfmt_hidden.custom_fields) != 0) {
5295 /* Different fields. */
5296 g_free(cfmt_hidden.custom_fields);
5297 cfmt_hidden.custom_fields = NULL;
5298 continue;
5300 if (cfmt->custom_occurrence != cfmt_hidden.custom_occurrence) {
5301 /* Different occurrences settings. */
5302 g_free(cfmt_hidden.custom_fields);
5303 cfmt_hidden.custom_fields = NULL;
5304 continue;
5310 * OK, they match, so it's one of the hidden fields,
5311 * hence not visible.
5313 g_free(cfmt_hidden.custom_fields);
5314 g_free(cols);
5315 return false;
5317 g_free(cols);
5321 * No - either there are no hidden columns or this isn't one
5322 * of them - so it is visible.
5324 return true;
5328 * Returns true if the given device should capture in monitor mode by default
5330 bool
5331 prefs_capture_device_monitor_mode(const char *name)
5333 char *tok, *devices;
5334 size_t len;
5336 if (prefs.capture_devices_monitor_mode && name) {
5337 devices = g_strdup (prefs.capture_devices_monitor_mode);
5338 len = strlen (name);
5339 for (tok = strtok (devices, ","); tok; tok = strtok(NULL, ",")) {
5340 if (strlen (tok) == len && strcmp (name, tok) == 0) {
5341 g_free (devices);
5342 return true;
5345 g_free (devices);
5348 return false;
5352 * Returns true if the user has marked this column as visible
5354 bool
5355 prefs_capture_options_dialog_column_is_visible(const char *column)
5357 GList *curr;
5358 char *col;
5360 for (curr = g_list_first(prefs.capture_columns); curr; curr = g_list_next(curr)) {
5361 col = (char *)curr->data;
5362 if (col && (g_ascii_strcasecmp(col, column) == 0)) {
5363 return true;
5366 return false;
5369 bool
5370 prefs_has_layout_pane_content (layout_pane_content_e layout_pane_content)
5372 return ((prefs.gui_layout_content_1 == layout_pane_content) ||
5373 (prefs.gui_layout_content_2 == layout_pane_content) ||
5374 (prefs.gui_layout_content_3 == layout_pane_content));
5377 #define PRS_GUI_FILTER_LABEL "gui.filter_expressions.label"
5378 #define PRS_GUI_FILTER_EXPR "gui.filter_expressions.expr"
5379 #define PRS_GUI_FILTER_ENABLED "gui.filter_expressions.enabled"
5382 * Extract the red, green, and blue components of a 24-bit RGB value
5383 * and convert them from [0,255] to [0,65535].
5385 #define RED_COMPONENT(x) (uint16_t) (((((x) >> 16) & 0xff) * 65535 / 255))
5386 #define GREEN_COMPONENT(x) (uint16_t) (((((x) >> 8) & 0xff) * 65535 / 255))
5387 #define BLUE_COMPONENT(x) (uint16_t) ( (((x) & 0xff) * 65535 / 255))
5389 char
5390 string_to_name_resolve(const char *string, e_addr_resolve *name_resolve)
5392 char c;
5394 memset(name_resolve, 0, sizeof(e_addr_resolve));
5395 while ((c = *string++) != '\0') {
5396 switch (c) {
5397 case 'g':
5398 name_resolve->maxmind_geoip = true;
5399 break;
5400 case 'm':
5401 name_resolve->mac_name = true;
5402 break;
5403 case 'n':
5404 name_resolve->network_name = true;
5405 break;
5406 case 'N':
5407 name_resolve->use_external_net_name_resolver = true;
5408 break;
5409 case 't':
5410 name_resolve->transport_name = true;
5411 break;
5412 case 'd':
5413 name_resolve->dns_pkt_addr_resolution = true;
5414 break;
5415 case 's':
5416 name_resolve->handshake_sni_addr_resolution = true;
5417 break;
5418 case 'v':
5419 name_resolve->vlan_name = true;
5420 break;
5421 default:
5423 * Unrecognized letter.
5425 return c;
5428 return '\0';
5431 static bool
5432 deprecated_heur_dissector_pref(char *pref_name, const char *value)
5434 struct heur_pref_name
5436 const char* pref_name;
5437 const char* short_name;
5438 bool more_dissectors; /* For multiple dissectors controlled by the same preference */
5441 struct heur_pref_name heur_prefs[] = {
5442 {"acn.heuristic_acn", "acn_udp", 0},
5443 {"bfcp.enable", "bfcp_tcp", 1},
5444 {"bfcp.enable", "bfcp_udp", 0},
5445 {"bt-dht.enable", "bittorrent_dht_udp", 0},
5446 {"bt-utp.enable", "bt_utp_udp", 0},
5447 {"cattp.enable", "cattp_udp", 0},
5448 {"cfp.enable", "fp_eth", 0},
5449 {"dicom.heuristic", "dicom_tcp", 0},
5450 {"dnp3.heuristics", "dnp3_tcp", 1},
5451 {"dnp3.heuristics", "dnp3_udp", 0},
5452 {"dvb-s2_modeadapt.enable", "dvb_s2_udp", 0},
5453 {"esl.enable", "esl_eth", 0},
5454 {"fp.udp_heur", "fp_udp", 0},
5455 {"gvsp.enable_heuristic", "gvsp_udp", 0},
5456 {"hdcp2.enable", "hdcp2_tcp", 0},
5457 {"hislip.enable_heuristic", "hislip_tcp", 0},
5458 {"infiniband.dissect_eoib", "mellanox_eoib", 1},
5459 {"infiniband.identify_payload", "eth_over_ib", 0},
5460 {"jxta.udp.heuristic", "jxta_udp", 0},
5461 {"jxta.tcp.heuristic", "jxta_tcp", 0},
5462 {"jxta.sctp.heuristic", "jxta_sctp", 0},
5463 {"mac-lte.heuristic_mac_lte_over_udp", "mac_lte_udp", 0},
5464 {"mbim.bulk_heuristic", "mbim_usb_bulk", 0},
5465 {"norm.heuristic_norm", "rmt_norm_udp", 0},
5466 {"openflow.heuristic", "openflow_tcp", 0},
5467 {"pdcp-lte.heuristic_pdcp_lte_over_udp", "pdcp_lte_udp", 0},
5468 {"rlc.heuristic_rlc_over_udp", "rlc_udp", 0},
5469 {"rlc-lte.heuristic_rlc_lte_over_udp", "rlc_lte_udp", 0},
5470 {"rtcp.heuristic_rtcp", "rtcp_udp", 1},
5471 {"rtcp.heuristic_rtcp", "rtcp_stun", 0},
5472 {"rtp.heuristic_rtp", "rtp_udp", 1},
5473 {"rtp.heuristic_rtp", "rtp_stun", 0},
5474 {"teredo.heuristic_teredo", "teredo_udp", 0},
5475 {"vssmonitoring.use_heuristics", "vssmonitoring_eth", 0},
5476 {"xml.heuristic", "xml_http", 1},
5477 {"xml.heuristic", "xml_sip", 1},
5478 {"xml.heuristic", "xml_media", 0},
5479 {"xml.heuristic_tcp", "xml_tcp", 0},
5480 {"xml.heuristic_udp", "xml_udp", 0},
5483 unsigned int i;
5484 heur_dtbl_entry_t* heuristic;
5487 for (i = 0; i < array_length(heur_prefs); i++)
5489 if (strcmp(pref_name, heur_prefs[i].pref_name) == 0)
5491 heuristic = find_heur_dissector_by_unique_short_name(heur_prefs[i].short_name);
5492 if (heuristic != NULL) {
5493 heuristic->enabled = ((g_ascii_strcasecmp(value, "true") == 0) ? true : false);
5496 if (!heur_prefs[i].more_dissectors)
5497 return true;
5502 return false;
5505 static bool
5506 deprecated_enable_dissector_pref(char *pref_name, const char *value)
5508 struct dissector_pref_name
5510 const char* pref_name;
5511 const char* short_name;
5514 struct dissector_pref_name dissector_prefs[] = {
5515 {"transum.tsumenabled", "TRANSUM"},
5516 {"snort.enable_snort_dissector", "Snort"},
5517 {"prp.enable", "PRP"},
5520 unsigned int i;
5521 int proto_id;
5523 for (i = 0; i < array_length(dissector_prefs); i++)
5525 if (strcmp(pref_name, dissector_prefs[i].pref_name) == 0)
5527 proto_id = proto_get_id_by_short_name(dissector_prefs[i].short_name);
5528 if (proto_id >= 0)
5529 proto_set_decoding(proto_id, ((g_ascii_strcasecmp(value, "true") == 0) ? true : false));
5530 return true;
5534 return false;
5537 static bool
5538 deprecated_port_pref(char *pref_name, const char *value)
5540 struct port_pref_name
5542 const char* pref_name;
5543 const char* module_name; /* the protocol filter name */
5544 const char* table_name;
5545 unsigned base;
5548 struct obsolete_pref_name
5550 const char* pref_name;
5553 /* For now this is only supporting TCP/UDP port and RTP payload
5554 * types dissector preferences, which are assumed to be decimal */
5555 /* module_name is the filter name of the destination port preference,
5556 * which is usually the same as the original module but not
5557 * necessarily (e.g., if the preference is for what is now a PINO.)
5558 * XXX: Most of these were changed pre-2.0. Can we end support
5559 * for migrating legacy preferences at some point?
5561 struct port_pref_name port_prefs[] = {
5562 /* TCP */
5563 {"cmp.tcp_alternate_port", "cmp", "tcp.port", 10},
5564 {"h248.tcp_port", "h248", "tcp.port", 10},
5565 {"cops.tcp.cops_port", "cops", "tcp.port", 10},
5566 {"dhcpfo.tcp_port", "dhcpfo", "tcp.port", 10},
5567 {"enttec.tcp_port", "enttec", "tcp.port", 10},
5568 {"forces.tcp_alternate_port", "forces", "tcp.port", 10},
5569 {"ged125.tcp_port", "ged125", "tcp.port", 10},
5570 {"hpfeeds.dissector_port", "hpfeeds", "tcp.port", 10},
5571 {"lsc.port", "lsc", "tcp.port", 10},
5572 {"megaco.tcp.txt_port", "megaco", "tcp.port", 10},
5573 {"netsync.tcp_port", "netsync", "tcp.port", 10},
5574 {"osi.tpkt_port", "osi", "tcp.port", 10},
5575 {"rsync.tcp_port", "rsync", "tcp.port", 10},
5576 {"sametime.tcp_port", "sametime", "tcp.port", 10},
5577 {"sigcomp.tcp.port2", "sigcomp", "tcp.port", 10},
5578 {"synphasor.tcp_port", "synphasor", "tcp.port", 10},
5579 {"tipc.alternate_port", "tipc", "tcp.port", 10},
5580 {"vnc.alternate_port", "vnc", "tcp.port", 10},
5581 {"scop.port", "scop", "tcp.port", 10},
5582 {"scop.port_secure", "scop", "tcp.port", 10},
5583 {"tpncp.tcp.trunkpack_port", "tpncp", "tcp.port", 10},
5584 /* UDP */
5585 {"h248.udp_port", "h248", "udp.port", 10},
5586 {"actrace.udp_port", "actrace", "udp.port", 10},
5587 {"brp.port", "brp", "udp.port", 10},
5588 {"bvlc.additional_udp_port", "bvlc", "udp.port", 10},
5589 {"capwap.udp.port.control", "capwap", "udp.port", 10},
5590 {"capwap.udp.port.data", "capwap", "udp.port", 10},
5591 {"coap.udp_port", "coap", "udp.port", 10},
5592 {"enttec.udp_port", "enttec", "udp.port", 10},
5593 {"forces.udp_alternate_port", "forces", "udp.port", 10},
5594 {"ldss.udp_port", "ldss", "udp.port", 10},
5595 {"lmp.udp_port", "lmp", "udp.port", 10},
5596 {"ltp.port", "ltp", "udp.port", 10},
5597 {"lwres.udp.lwres_port", "lwres", "udp.port", 10},
5598 {"megaco.udp.txt_port", "megaco", "udp.port", 10},
5599 {"pfcp.port_pfcp", "pfcp", "udp.port", 10},
5600 {"pgm.udp.encap_ucast_port", "pgm", "udp.port", 10},
5601 {"pgm.udp.encap_mcast_port", "pgm", "udp.port", 10},
5602 {"quic.udp.quic.port", "quic", "udp.port", 10},
5603 {"quic.udp.quics.port", "quic", "udp.port", 10},
5604 {"radius.alternate_port", "radius", "udp.port", 10},
5605 {"rdt.default_udp_port", "rdt", "udp.port", 10},
5606 {"alc.default.udp_port", "alc", "udp.port", 10},
5607 {"sigcomp.udp.port2", "sigcomp", "udp.port", 10},
5608 {"synphasor.udp_port", "synphasor", "udp.port", 10},
5609 {"tdmop.udpport", "tdmop", "udp.port", 10},
5610 {"uaudp.port1", "uaudp", "udp.port", 10},
5611 {"uaudp.port2", "uaudp", "udp.port", 10},
5612 {"uaudp.port3", "uaudp", "udp.port", 10},
5613 {"uaudp.port4", "uaudp", "udp.port", 10},
5614 {"uhd.dissector_port", "uhd", "udp.port", 10},
5615 {"vrt.dissector_port", "vrt", "udp.port", 10},
5616 {"tpncp.udp.trunkpack_port", "tpncp", "udp.port", 10},
5617 /* SCTP */
5618 {"hnbap.port", "hnbap", "sctp.port", 10},
5619 {"m2pa.port", "m2pa", "sctp.port", 10},
5620 {"megaco.sctp.txt_port", "megaco", "sctp.port", 10},
5621 {"rua.port", "rua", "sctp.port", 10},
5622 /* SCTP PPI */
5623 {"lapd.sctp_payload_protocol_identifier", "lapd", "sctp.ppi", 10},
5624 /* SCCP SSN */
5625 {"ranap.sccp_ssn", "ranap", "sccp.ssn", 10},
5628 struct port_pref_name port_range_prefs[] = {
5629 /* TCP */
5630 {"couchbase.tcp.ports", "couchbase", "tcp.port", 10},
5631 {"gsm_ipa.tcp_ports", "gsm_ipa", "tcp.port", 10},
5632 {"kafka.tcp.ports", "kafka", "tcp.port", 10},
5633 {"kt.tcp.ports", "kt", "tcp.port", 10},
5634 {"memcache.tcp.ports", "memcache", "tcp.port", 10},
5635 {"mrcpv2.tcp.port_range", "mrcpv2", "tcp.port", 10},
5636 {"pdu_transport.ports.tcp", "pdu_transport", "tcp.port", 10},
5637 {"rtsp.tcp.port_range", "rtsp", "tcp.port", 10},
5638 {"sip.tcp.ports", "sip", "tcp.port", 10},
5639 {"someip.ports.tcp", "someip", "tcp.port", 10},
5640 {"tds.tcp_ports", "tds", "tcp.port", 10},
5641 {"tpkt.tcp.ports", "tpkt", "tcp.port", 10},
5642 {"uma.tcp.ports", "uma", "tcp.port", 10},
5643 /* UDP */
5644 {"aruba_erm.udp.ports", "arubs_erm", "udp.port", 10},
5645 {"diameter.udp.ports", "diameter", "udp.port", 10},
5646 {"dmp.udp_ports", "dmp", "udp.port", 10},
5647 {"dns.udp.ports", "dns", "udp.port", 10},
5648 {"gsm_ipa.udp_ports", "gsm_ipa", "udp.port", 10},
5649 {"hcrt.dissector_udp_port", "hcrt", "udp.port", 10},
5650 {"memcache.udp.ports", "memcache", "udp.port", 10},
5651 {"nb_rtpmux.udp_ports", "nb_rtpmux", "udp.port", 10},
5652 {"gprs-ns.udp.ports", "gprs-ns", "udp.port", 10},
5653 {"p_mul.udp_ports", "p_mul", "udp.port", 10},
5654 {"pdu_transport.ports.udp", "pdu_transport", "udp.port", 10},
5655 {"radius.ports", "radius", "udp.port", 10},
5656 {"sflow.ports", "sflow", "udp.port", 10},
5657 {"someip.ports.udp", "someip", "udp.port", 10},
5658 {"sscop.udp.ports", "sscop", "udp.port", 10},
5659 {"tftp.udp_ports", "tftp", "udp.port", 10},
5660 {"tipc.udp.ports", "tipc", "udp.port", 10},
5661 /* RTP */
5662 {"amr.dynamic.payload.type", "amr", "rtp.pt", 10},
5663 {"amr.wb.dynamic.payload.type", "amr_wb", "rtp.pt", 10},
5664 {"dvb-s2_modeadapt.dynamic.payload.type", "dvb-s2_modeadapt", "rtp.pt", 10},
5665 {"evs.dynamic.payload.type", "evs", "rtp.pt", 10},
5666 {"h263p.dynamic.payload.type", "h263p", "rtp.pt", 10},
5667 {"h264.dynamic.payload.type", "h264", "rtp.pt", 10},
5668 {"h265.dynamic.payload.type", "h265", "rtp.pt", 10},
5669 {"ismacryp.dynamic.payload.type", "ismacryp", "rtp.pt", 10},
5670 {"iuup.dynamic.payload.type", "iuup", "rtp.pt", 10},
5671 {"lapd.rtp_payload_type", "lapd", "rtp.pt", 10},
5672 {"mp4ves.dynamic.payload.type", "mp4ves", "rtp.pt", 10},
5673 {"mtp2.rtp_payload_type", "mtp2", "rtp.pt", 10},
5674 {"opus.dynamic.payload.type", "opus", "rtp.pt", 10},
5675 {"rtp.rfc2198_payload_type", "rtp_rfc2198", "rtp.pt", 10},
5676 {"rtpevent.event_payload_type_value", "rtpevent", "rtp.pt", 10},
5677 {"rtpevent.cisco_nse_payload_type_value", "rtpevent", "rtp.pt", 10},
5678 {"rtpmidi.midi_payload_type_value", "rtpmidi", "rtp.pt", 10},
5679 {"vp8.dynamic.payload.type", "vp8", "rtp.pt", 10},
5680 /* SCTP */
5681 {"diameter.sctp.ports", "diameter", "sctp.port", 10},
5682 {"sgsap.sctp_ports", "sgsap", "sctp.port", 10},
5683 /* SCCP SSN */
5684 {"pcap.ssn", "pcap", "sccp.ssn", 10},
5687 /* These are subdissectors of TPKT/OSITP that used to have a
5688 TCP port preference even though they were never
5689 directly on TCP. Convert them to use Decode As
5690 with the TPKT dissector handle */
5691 struct port_pref_name tpkt_subdissector_port_prefs[] = {
5692 {"dap.tcp.port", "dap", "tcp.port", 10},
5693 {"disp.tcp.port", "disp", "tcp.port", 10},
5694 {"dop.tcp.port", "dop", "tcp.port", 10},
5695 {"dsp.tcp.port", "dsp", "tcp.port", 10},
5696 {"p1.tcp.port", "p1", "tcp.port", 10},
5697 {"p7.tcp.port", "p7", "tcp.port", 10},
5698 {"rdp.tcp.port", "rdp", "tcp.port", 10},
5701 /* These are obsolete preferences from the dissectors' view,
5702 (typically because of a switch from a single value to a
5703 range value) but the name of the preference conflicts
5704 with the generated preference name from the dissector table.
5705 Don't allow the obsolete preference through to be handled */
5706 struct obsolete_pref_name obsolete_prefs[] = {
5707 {"diameter.tcp.port"},
5708 {"kafka.tcp.port"},
5709 {"mrcpv2.tcp.port"},
5710 {"rtsp.tcp.port"},
5711 {"sip.tcp.port"},
5712 {"t38.tcp.port"},
5715 unsigned int i;
5716 unsigned uval;
5717 dissector_table_t sub_dissectors;
5718 dissector_handle_t handle, tpkt_handle;
5719 module_t *module;
5720 pref_t *pref;
5722 static bool sanity_checked;
5723 if (!sanity_checked) {
5724 sanity_checked = true;
5725 for (i = 0; i < G_N_ELEMENTS(port_prefs); i++) {
5726 module = prefs_find_module(port_prefs[i].module_name);
5727 if (!module) {
5728 ws_warning("Deprecated ports pref check - module '%s' not found", port_prefs[i].module_name);
5729 continue;
5731 pref = prefs_find_preference(module, port_prefs[i].table_name);
5732 if (!pref) {
5733 ws_warning("Deprecated ports pref '%s.%s' not found", module->name, port_prefs[i].table_name);
5734 continue;
5736 if (pref->type != PREF_DECODE_AS_RANGE) {
5737 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));
5742 for (i = 0; i < G_N_ELEMENTS(port_prefs); i++) {
5743 if (strcmp(pref_name, port_prefs[i].pref_name) == 0) {
5744 if (!ws_basestrtou32(value, NULL, &uval, port_prefs[i].base))
5745 return false; /* number was bad */
5747 module = prefs_find_module(port_prefs[i].module_name);
5748 pref = prefs_find_preference(module, port_prefs[i].table_name);
5749 if (pref != NULL) {
5750 module->prefs_changed_flags |= prefs_get_effect_flags(pref);
5751 if (pref->type == PREF_DECODE_AS_RANGE) {
5752 // The legacy preference was a port number, but the new
5753 // preference is a port range. Add to existing range.
5754 if (uval) {
5755 prefs_range_add_value(pref, uval);
5760 /* If the value is zero, it wouldn't add to the Decode As tables */
5761 if (uval != 0)
5763 sub_dissectors = find_dissector_table(port_prefs[i].table_name);
5764 if (sub_dissectors != NULL) {
5765 handle = dissector_table_get_dissector_handle(sub_dissectors, module->title);
5766 if (handle != NULL) {
5767 dissector_change_uint(port_prefs[i].table_name, uval, handle);
5768 decode_build_reset_list(port_prefs[i].table_name, dissector_table_get_type(sub_dissectors), GUINT_TO_POINTER(uval), NULL, NULL);
5773 return true;
5777 for (i = 0; i < array_length(port_range_prefs); i++)
5779 if (strcmp(pref_name, port_range_prefs[i].pref_name) == 0)
5781 uint32_t range_i, range_j;
5783 sub_dissectors = find_dissector_table(port_range_prefs[i].table_name);
5784 if (sub_dissectors != NULL) {
5785 switch (dissector_table_get_type(sub_dissectors)) {
5786 case FT_UINT8:
5787 case FT_UINT16:
5788 case FT_UINT24:
5789 case FT_UINT32:
5790 break;
5792 default:
5793 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));
5794 ws_assert_not_reached();
5797 module = prefs_find_module(port_range_prefs[i].module_name);
5798 pref = prefs_find_preference(module, port_range_prefs[i].table_name);
5799 if (pref != NULL)
5801 if (!prefs_set_range_value_work(pref, value, true, &module->prefs_changed_flags))
5803 return false; /* number was bad */
5806 handle = dissector_table_get_dissector_handle(sub_dissectors, module->title);
5807 if (handle != NULL) {
5809 for (range_i = 0; range_i < (*pref->varp.range)->nranges; range_i++) {
5810 for (range_j = (*pref->varp.range)->ranges[range_i].low; range_j < (*pref->varp.range)->ranges[range_i].high; range_j++) {
5811 dissector_change_uint(port_range_prefs[i].table_name, range_j, handle);
5812 decode_build_reset_list(port_range_prefs[i].table_name, dissector_table_get_type(sub_dissectors), GUINT_TO_POINTER(range_j), NULL, NULL);
5815 dissector_change_uint(port_range_prefs[i].table_name, (*pref->varp.range)->ranges[range_i].high, handle);
5816 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);
5822 return true;
5826 for (i = 0; i < array_length(tpkt_subdissector_port_prefs); i++)
5828 if (strcmp(pref_name, tpkt_subdissector_port_prefs[i].pref_name) == 0)
5830 /* XXX - give an error if it doesn't fit in a unsigned? */
5831 if (!ws_basestrtou32(value, NULL, &uval, tpkt_subdissector_port_prefs[i].base))
5832 return false; /* number was bad */
5834 /* If the value is 0 or 102 (default TPKT port), don't add to the Decode As tables */
5835 if ((uval != 0) && (uval != 102))
5837 tpkt_handle = find_dissector("tpkt");
5838 if (tpkt_handle != NULL) {
5839 dissector_change_uint(tpkt_subdissector_port_prefs[i].table_name, uval, tpkt_handle);
5843 return true;
5847 for (i = 0; i < array_length(obsolete_prefs); i++)
5849 if (strcmp(pref_name, obsolete_prefs[i].pref_name) == 0)
5851 /* Just ignore the preference */
5852 return true;
5855 return false;
5858 static prefs_set_pref_e
5859 set_pref(char *pref_name, const char *value, void *private_data,
5860 bool return_range_errors)
5862 unsigned cval;
5863 unsigned uval;
5864 bool bval;
5865 int enum_val;
5866 char *dotp, *last_dotp;
5867 static char *filter_label = NULL;
5868 static bool filter_enabled = false;
5869 module_t *module, *containing_module, *target_module;
5870 pref_t *pref;
5871 int type;
5872 bool converted_pref = false;
5874 target_module = (module_t*)private_data;
5876 //The PRS_GUI field names are here for backwards compatibility
5877 //display filters have been converted to a UAT.
5878 if (strcmp(pref_name, PRS_GUI_FILTER_LABEL) == 0) {
5879 /* Assume that PRS_GUI_FILTER_EXPR follows this preference. In case of
5880 * malicious preference files, free the previous value to limit the size
5881 * of leaked memory. */
5882 g_free(filter_label);
5883 filter_label = g_strdup(value);
5884 } else if (strcmp(pref_name, PRS_GUI_FILTER_ENABLED) == 0) {
5885 filter_enabled = (strcmp(value, "TRUE") == 0) ? true : false;
5886 } else if (strcmp(pref_name, PRS_GUI_FILTER_EXPR) == 0) {
5887 /* Comments not supported for "old" preference style */
5888 filter_expression_new(filter_label, value, "", filter_enabled);
5889 g_free(filter_label);
5890 filter_label = NULL;
5891 /* Remember to save the new UAT to file. */
5892 prefs.filter_expressions_old = true;
5893 } else if (strcmp(pref_name, "gui.version_in_start_page") == 0) {
5894 /* Convert deprecated value to closest current equivalent */
5895 if (g_ascii_strcasecmp(value, "true") == 0) {
5896 prefs.gui_version_placement = version_both;
5897 } else {
5898 prefs.gui_version_placement = version_neither;
5900 } else if (strcmp(pref_name, "name_resolve") == 0 ||
5901 strcmp(pref_name, "capture.name_resolve") == 0) {
5903 * Handle the deprecated name resolution options.
5905 * "TRUE" and "FALSE", for backwards compatibility, are synonyms for
5906 * RESOLV_ALL and RESOLV_NONE.
5908 * Otherwise, we treat it as a list of name types we want to resolve.
5910 if (g_ascii_strcasecmp(value, "true") == 0) {
5911 gbl_resolv_flags.mac_name = true;
5912 gbl_resolv_flags.network_name = true;
5913 gbl_resolv_flags.transport_name = true;
5915 else if (g_ascii_strcasecmp(value, "false") == 0) {
5916 disable_name_resolution();
5918 else {
5919 /* start out with none set */
5920 disable_name_resolution();
5921 if (string_to_name_resolve(value, &gbl_resolv_flags) != '\0')
5922 return PREFS_SET_SYNTAX_ERR;
5924 } else if (deprecated_heur_dissector_pref(pref_name, value)) {
5925 /* Handled within deprecated_heur_dissector_pref() if found */
5926 } else if (deprecated_enable_dissector_pref(pref_name, value)) {
5927 /* Handled within deprecated_enable_dissector_pref() if found */
5928 } else if (deprecated_port_pref(pref_name, value)) {
5929 /* Handled within deprecated_port_pref() if found */
5930 } else if (strcmp(pref_name, "console.log.level") == 0) {
5931 /* Handled on the command line within ws_log_parse_args() */
5932 return PREFS_SET_OK;
5933 } else {
5934 /* Handle deprecated "global" options that don't have a module
5935 * associated with them
5937 if ((strcmp(pref_name, "name_resolve_concurrency") == 0) ||
5938 (strcmp(pref_name, "name_resolve_load_smi_modules") == 0) ||
5939 (strcmp(pref_name, "name_resolve_suppress_smi_errors") == 0)) {
5940 module = nameres_module;
5941 dotp = pref_name;
5942 } else {
5943 /* To which module does this preference belong? */
5944 module = NULL;
5945 last_dotp = pref_name;
5946 while (!module) {
5947 dotp = strchr(last_dotp, '.');
5948 if (dotp == NULL) {
5949 /* Either there's no such module, or no module was specified.
5950 In either case, that means there's no such preference. */
5951 return PREFS_SET_NO_SUCH_PREF;
5953 *dotp = '\0'; /* separate module and preference name */
5954 module = prefs_find_module(pref_name);
5957 * XXX - "Diameter" rather than "diameter" was used in earlier
5958 * versions of Wireshark; if we didn't find the module, and its name
5959 * was "Diameter", look for "diameter" instead.
5961 * In addition, the BEEP protocol used to be the BXXP protocol,
5962 * so if we didn't find the module, and its name was "bxxp",
5963 * look for "beep" instead.
5965 * Also, the preferences for GTP v0 and v1 were combined under
5966 * a single "gtp" heading, and the preferences for SMPP were
5967 * moved to "smpp-gsm-sms" and then moved to "gsm-sms-ud".
5968 * However, SMPP now has its own preferences, so we just map
5969 * "smpp-gsm-sms" to "gsm-sms-ud", and then handle SMPP below.
5971 * We also renamed "dcp" to "dccp", "x.25" to "x25", "x411" to "p1"
5972 * and "nsip" to "gprs_ns".
5974 * The SynOptics Network Management Protocol (SONMP) is now known by
5975 * its modern name, the Nortel Discovery Protocol (NDP).
5977 if (module == NULL) {
5979 * See if there's a backwards-compatibility name
5980 * that maps to this module.
5982 module = prefs_find_module_alias(pref_name);
5983 if (module == NULL) {
5985 * There's no alias for the module; see if the
5986 * module name matches any protocol aliases.
5988 header_field_info *hfinfo = proto_registrar_get_byalias(pref_name);
5989 if (hfinfo) {
5990 module = (module_t *) wmem_tree_lookup_string(prefs_modules, hfinfo->abbrev, WMEM_TREE_STRING_NOCASE);
5993 if (module == NULL) {
5995 * There aren't any aliases. Was the module
5996 * removed rather than renamed?
5998 if (strcmp(pref_name, "etheric") == 0 ||
5999 strcmp(pref_name, "isup_thin") == 0) {
6001 * The dissectors for these protocols were
6002 * removed as obsolete on 2009-07-70 in change
6003 * 739bfc6ff035583abb9434e0e988048de38a8d9a.
6005 return PREFS_SET_OBSOLETE;
6008 if (module) {
6009 converted_pref = true;
6010 prefs.unknown_prefs = true;
6013 *dotp = '.'; /* put the preference string back */
6014 dotp++; /* skip past separator to preference name */
6015 last_dotp = dotp;
6019 /* The pref is located in the module or a submodule.
6020 * Assume module, then search for a submodule holding the pref. */
6021 containing_module = module;
6022 pref = prefs_find_preference_with_submodule(module, dotp, &containing_module);
6024 if (pref == NULL) {
6025 prefs.unknown_prefs = true;
6027 /* "gui" prefix was added to column preferences for better organization
6028 * within the preferences file
6030 if (module == gui_column_module) {
6031 /* While this has a subtree, there is no apply callback, so no
6032 * need to use prefs_find_preference_with_submodule to update
6033 * containing_module. It would not be useful. */
6034 pref = prefs_find_preference(module, pref_name);
6036 else if (strcmp(module->name, "mgcp") == 0) {
6038 * XXX - "mgcp.display raw text toggle" and "mgcp.display dissect tree"
6039 * rather than "mgcp.display_raw_text" and "mgcp.display_dissect_tree"
6040 * were used in earlier versions of Wireshark; if we didn't find the
6041 * preference, it was an MGCP preference, and its name was
6042 * "display raw text toggle" or "display dissect tree", look for
6043 * "display_raw_text" or "display_dissect_tree" instead.
6045 * "mgcp.tcp.port" and "mgcp.udp.port" are harder to handle, as both
6046 * the gateway and callagent ports were given those names; we interpret
6047 * the first as "mgcp.{tcp,udp}.gateway_port" and the second as
6048 * "mgcp.{tcp,udp}.callagent_port", as that's the order in which
6049 * they were registered by the MCCP dissector and thus that's the
6050 * order in which they were written to the preferences file. (If
6051 * we're not reading the preferences file, but are handling stuff
6052 * from a "-o" command-line option, we have no clue which the user
6053 * had in mind - they should have used "mgcp.{tcp,udp}.gateway_port"
6054 * or "mgcp.{tcp,udp}.callagent_port" instead.)
6056 if (strcmp(dotp, "display raw text toggle") == 0)
6057 pref = prefs_find_preference(module, "display_raw_text");
6058 else if (strcmp(dotp, "display dissect tree") == 0)
6059 pref = prefs_find_preference(module, "display_dissect_tree");
6060 else if (strcmp(dotp, "tcp.port") == 0) {
6061 mgcp_tcp_port_count++;
6062 if (mgcp_tcp_port_count == 1) {
6063 /* It's the first one */
6064 pref = prefs_find_preference(module, "tcp.gateway_port");
6065 } else if (mgcp_tcp_port_count == 2) {
6066 /* It's the second one */
6067 pref = prefs_find_preference(module, "tcp.callagent_port");
6069 /* Otherwise it's from the command line, and we don't bother
6070 mapping it. */
6071 } else if (strcmp(dotp, "udp.port") == 0) {
6072 mgcp_udp_port_count++;
6073 if (mgcp_udp_port_count == 1) {
6074 /* It's the first one */
6075 pref = prefs_find_preference(module, "udp.gateway_port");
6076 } else if (mgcp_udp_port_count == 2) {
6077 /* It's the second one */
6078 pref = prefs_find_preference(module, "udp.callagent_port");
6080 /* Otherwise it's from the command line, and we don't bother
6081 mapping it. */
6083 } else if (strcmp(module->name, "smb") == 0) {
6084 /* Handle old names for SMB preferences. */
6085 if (strcmp(dotp, "smb.trans.reassembly") == 0)
6086 pref = prefs_find_preference(module, "trans_reassembly");
6087 else if (strcmp(dotp, "smb.dcerpc.reassembly") == 0)
6088 pref = prefs_find_preference(module, "dcerpc_reassembly");
6089 } else if (strcmp(module->name, "ndmp") == 0) {
6090 /* Handle old names for NDMP preferences. */
6091 if (strcmp(dotp, "ndmp.desegment") == 0)
6092 pref = prefs_find_preference(module, "desegment");
6093 } else if (strcmp(module->name, "diameter") == 0) {
6094 /* Handle old names for Diameter preferences. */
6095 if (strcmp(dotp, "diameter.desegment") == 0)
6096 pref = prefs_find_preference(module, "desegment");
6097 } else if (strcmp(module->name, "pcli") == 0) {
6098 /* Handle old names for PCLI preferences. */
6099 if (strcmp(dotp, "pcli.udp_port") == 0)
6100 pref = prefs_find_preference(module, "udp_port");
6101 } else if (strcmp(module->name, "artnet") == 0) {
6102 /* Handle old names for ARTNET preferences. */
6103 if (strcmp(dotp, "artnet.udp_port") == 0)
6104 pref = prefs_find_preference(module, "udp_port");
6105 } else if (strcmp(module->name, "mapi") == 0) {
6106 /* Handle old names for MAPI preferences. */
6107 if (strcmp(dotp, "mapi_decrypt") == 0)
6108 pref = prefs_find_preference(module, "decrypt");
6109 } else if (strcmp(module->name, "fc") == 0) {
6110 /* Handle old names for Fibre Channel preferences. */
6111 if (strcmp(dotp, "reassemble_fc") == 0)
6112 pref = prefs_find_preference(module, "reassemble");
6113 else if (strcmp(dotp, "fc_max_frame_size") == 0)
6114 pref = prefs_find_preference(module, "max_frame_size");
6115 } else if (strcmp(module->name, "fcip") == 0) {
6116 /* Handle old names for Fibre Channel-over-IP preferences. */
6117 if (strcmp(dotp, "desegment_fcip_messages") == 0)
6118 pref = prefs_find_preference(module, "desegment");
6119 else if (strcmp(dotp, "fcip_port") == 0)
6120 pref = prefs_find_preference(module, "target_port");
6121 } else if (strcmp(module->name, "gtp") == 0) {
6122 /* Handle old names for GTP preferences. */
6123 if (strcmp(dotp, "gtpv0_port") == 0)
6124 pref = prefs_find_preference(module, "v0_port");
6125 else if (strcmp(dotp, "gtpv1c_port") == 0)
6126 pref = prefs_find_preference(module, "v1c_port");
6127 else if (strcmp(dotp, "gtpv1u_port") == 0)
6128 pref = prefs_find_preference(module, "v1u_port");
6129 else if (strcmp(dotp, "gtp_dissect_tpdu") == 0)
6130 pref = prefs_find_preference(module, "dissect_tpdu");
6131 else if (strcmp(dotp, "gtpv0_dissect_cdr_as") == 0)
6132 pref = prefs_find_preference(module, "v0_dissect_cdr_as");
6133 else if (strcmp(dotp, "gtpv0_check_etsi") == 0)
6134 pref = prefs_find_preference(module, "v0_check_etsi");
6135 else if (strcmp(dotp, "gtpv1_check_etsi") == 0)
6136 pref = prefs_find_preference(module, "v1_check_etsi");
6137 } else if (strcmp(module->name, "ip") == 0) {
6138 /* Handle old names for IP preferences. */
6139 if (strcmp(dotp, "ip_summary_in_tree") == 0)
6140 pref = prefs_find_preference(module, "summary_in_tree");
6141 } else if (strcmp(module->name, "iscsi") == 0) {
6142 /* Handle old names for iSCSI preferences. */
6143 if (strcmp(dotp, "iscsi_port") == 0)
6144 pref = prefs_find_preference(module, "target_port");
6145 } else if (strcmp(module->name, "lmp") == 0) {
6146 /* Handle old names for LMP preferences. */
6147 if (strcmp(dotp, "lmp_version") == 0)
6148 pref = prefs_find_preference(module, "version");
6149 } else if (strcmp(module->name, "mtp3") == 0) {
6150 /* Handle old names for MTP3 preferences. */
6151 if (strcmp(dotp, "mtp3_standard") == 0)
6152 pref = prefs_find_preference(module, "standard");
6153 else if (strcmp(dotp, "net_addr_format") == 0)
6154 pref = prefs_find_preference(module, "addr_format");
6155 } else if (strcmp(module->name, "nlm") == 0) {
6156 /* Handle old names for NLM preferences. */
6157 if (strcmp(dotp, "nlm_msg_res_matching") == 0)
6158 pref = prefs_find_preference(module, "msg_res_matching");
6159 } else if (strcmp(module->name, "ppp") == 0) {
6160 /* Handle old names for PPP preferences. */
6161 if (strcmp(dotp, "ppp_fcs") == 0)
6162 pref = prefs_find_preference(module, "fcs_type");
6163 else if (strcmp(dotp, "ppp_vj") == 0)
6164 pref = prefs_find_preference(module, "decompress_vj");
6165 } else if (strcmp(module->name, "rsvp") == 0) {
6166 /* Handle old names for RSVP preferences. */
6167 if (strcmp(dotp, "rsvp_process_bundle") == 0)
6168 pref = prefs_find_preference(module, "process_bundle");
6169 } else if (strcmp(module->name, "tcp") == 0) {
6170 /* Handle old names for TCP preferences. */
6171 if (strcmp(dotp, "tcp_summary_in_tree") == 0)
6172 pref = prefs_find_preference(module, "summary_in_tree");
6173 else if (strcmp(dotp, "tcp_analyze_sequence_numbers") == 0)
6174 pref = prefs_find_preference(module, "analyze_sequence_numbers");
6175 else if (strcmp(dotp, "tcp_relative_sequence_numbers") == 0)
6176 pref = prefs_find_preference(module, "relative_sequence_numbers");
6177 else if (strcmp(dotp, "dissect_experimental_options_with_magic") == 0)
6178 pref = prefs_find_preference(module, "dissect_experimental_options_rfc6994");
6179 } else if (strcmp(module->name, "udp") == 0) {
6180 /* Handle old names for UDP preferences. */
6181 if (strcmp(dotp, "udp_summary_in_tree") == 0)
6182 pref = prefs_find_preference(module, "summary_in_tree");
6183 } else if (strcmp(module->name, "ndps") == 0) {
6184 /* Handle old names for NDPS preferences. */
6185 if (strcmp(dotp, "desegment_ndps") == 0)
6186 pref = prefs_find_preference(module, "desegment_tcp");
6187 } else if (strcmp(module->name, "http") == 0) {
6188 /* Handle old names for HTTP preferences. */
6189 if (strcmp(dotp, "desegment_http_headers") == 0)
6190 pref = prefs_find_preference(module, "desegment_headers");
6191 else if (strcmp(dotp, "desegment_http_body") == 0)
6192 pref = prefs_find_preference(module, "desegment_body");
6193 } else if (strcmp(module->name, "smpp") == 0) {
6194 /* Handle preferences that moved from SMPP. */
6195 module_t *new_module = prefs_find_module("gsm-sms-ud");
6196 if (new_module) {
6197 if (strcmp(dotp, "port_number_udh_means_wsp") == 0) {
6198 pref = prefs_find_preference(new_module, "port_number_udh_means_wsp");
6199 containing_module = new_module;
6200 } else if (strcmp(dotp, "try_dissect_1st_fragment") == 0) {
6201 pref = prefs_find_preference(new_module, "try_dissect_1st_fragment");
6202 containing_module = new_module;
6205 } else if (strcmp(module->name, "asn1") == 0) {
6206 /* Handle old generic ASN.1 preferences (it's not really a
6207 rename, as the new preferences support multiple ports,
6208 but we might as well copy them over). */
6209 if (strcmp(dotp, "tcp_port") == 0)
6210 pref = prefs_find_preference(module, "tcp_ports");
6211 else if (strcmp(dotp, "udp_port") == 0)
6212 pref = prefs_find_preference(module, "udp_ports");
6213 else if (strcmp(dotp, "sctp_port") == 0)
6214 pref = prefs_find_preference(module, "sctp_ports");
6215 } else if (strcmp(module->name, "llcgprs") == 0) {
6216 if (strcmp(dotp, "ignore_cipher_bit") == 0)
6217 pref = prefs_find_preference(module, "autodetect_cipher_bit");
6218 } else if (strcmp(module->name, "erf") == 0) {
6219 if (strcmp(dotp, "erfeth") == 0) {
6220 /* Handle the old "erfeth" preference; map it to the new
6221 "ethfcs" preference, and map the values to those for
6222 the new preference. */
6223 pref = prefs_find_preference(module, "ethfcs");
6224 if (strcmp(value, "ethfcs") == 0 || strcmp(value, "Ethernet with FCS") == 0)
6225 value = "TRUE";
6226 else if (strcmp(value, "eth") == 0 || strcmp(value, "Ethernet") == 0)
6227 value = "FALSE";
6228 else if (strcmp(value, "raw") == 0 || strcmp(value, "Raw data") == 0)
6229 value = "TRUE";
6230 } else if (strcmp(dotp, "erfatm") == 0) {
6231 /* Handle the old "erfatm" preference; map it to the new
6232 "aal5_type" preference, and map the values to those for
6233 the new preference. */
6234 pref = prefs_find_preference(module, "aal5_type");
6235 if (strcmp(value, "atm") == 0 || strcmp(value, "ATM") == 0)
6236 value = "guess";
6237 else if (strcmp(value, "llc") == 0 || strcmp(value, "LLC") == 0)
6238 value = "llc";
6239 else if (strcmp(value, "raw") == 0 || strcmp(value, "Raw data") == 0)
6240 value = "guess";
6241 } else if (strcmp(dotp, "erfhdlc") == 0) {
6242 /* Handle the old "erfhdlc" preference; map it to the new
6243 "hdlc_type" preference, and map the values to those for
6244 the new preference. */
6245 pref = prefs_find_preference(module, "hdlc_type");
6246 if (strcmp(value, "chdlc") == 0 || strcmp(value, "Cisco HDLC") == 0)
6247 value = "chdlc";
6248 else if (strcmp(value, "ppp") == 0 || strcmp(value, "PPP serial") == 0)
6249 value = "ppp";
6250 else if (strcmp(value, "fr") == 0 || strcmp(value, "Frame Relay") == 0)
6251 value = "frelay";
6252 else if (strcmp(value, "mtp2") == 0 || strcmp(value, "SS7 MTP2") == 0)
6253 value = "mtp2";
6254 else if (strcmp(value, "raw") == 0 || strcmp(value, "Raw data") == 0)
6255 value = "guess";
6257 } else if (strcmp(module->name, "eth") == 0) {
6258 /* "eth.qinq_ethertype" has been changed(restored) to "vlan.qinq.ethertype" */
6259 if (strcmp(dotp, "qinq_ethertype") == 0) {
6260 module_t *new_module = prefs_find_module("vlan");
6261 if (new_module) {
6262 pref = prefs_find_preference(new_module, "qinq_ethertype");
6263 containing_module = new_module;
6266 } else if (strcmp(module->name, "taps") == 0) {
6267 /* taps preferences moved to "statistics" module */
6268 if (strcmp(dotp, "update_interval") == 0)
6269 pref = prefs_find_preference(stats_module, dotp);
6270 } else if (strcmp(module->name, "packet_list") == 0) {
6271 /* packet_list preferences moved to protocol module */
6272 if (strcmp(dotp, "display_hidden_proto_items") == 0)
6273 pref = prefs_find_preference(protocols_module, dotp);
6274 } else if (strcmp(module->name, "stream") == 0) {
6275 /* stream preferences moved to gui color module */
6276 if ((strcmp(dotp, "client.fg") == 0) ||
6277 (strcmp(dotp, "client.bg") == 0) ||
6278 (strcmp(dotp, "server.fg") == 0) ||
6279 (strcmp(dotp, "server.bg") == 0))
6280 pref = prefs_find_preference(gui_color_module, pref_name);
6281 } else if (strcmp(module->name, "nameres") == 0) {
6282 if (strcmp(pref_name, "name_resolve_concurrency") == 0) {
6283 pref = prefs_find_preference(nameres_module, pref_name);
6284 } else if (strcmp(pref_name, "name_resolve_load_smi_modules") == 0) {
6285 pref = prefs_find_preference(nameres_module, "load_smi_modules");
6286 } else if (strcmp(pref_name, "name_resolve_suppress_smi_errors") == 0) {
6287 pref = prefs_find_preference(nameres_module, "suppress_smi_errors");
6289 } else if (strcmp(module->name, "extcap") == 0) {
6290 /* Handle the old "sshdump.remotesudo" preference; map it to the new
6291 "sshdump.remotepriv" preference, and map the boolean values to the
6292 appropriate strings of the new preference. */
6293 if (strcmp(dotp, "sshdump.remotesudo") == 0) {
6294 pref = prefs_find_preference(module, "sshdump.remotepriv");
6295 if (g_ascii_strcasecmp(value, "true") == 0)
6296 value = "sudo";
6297 else
6298 value = "none";
6301 if (pref) {
6302 converted_pref = true;
6305 if (pref == NULL ) {
6306 if (strcmp(module->name, "extcap") == 0 && g_list_length(module->prefs) <= 1) {
6308 * Assume that we've skipped extcap preference registration
6309 * and that only extcap.gui_save_on_start is loaded.
6311 return PREFS_SET_OK;
6313 return PREFS_SET_NO_SUCH_PREF; /* no such preference */
6316 if (target_module && target_module != containing_module) {
6317 /* Ignore */
6318 return PREFS_SET_OK;
6321 type = pref->type;
6322 if (IS_PREF_OBSOLETE(type)) {
6323 return PREFS_SET_OBSOLETE; /* no such preference any more */
6324 } else {
6325 RESET_PREF_OBSOLETE(type);
6328 if (converted_pref) {
6329 ws_warning("Preference \"%s\" has been converted to \"%s.%s\"\n"
6330 "Save your preferences to make this change permanent.",
6331 pref_name, module->name ? module->name : module->parent->name, prefs_get_name(pref));
6334 switch (type) {
6336 case PREF_UINT:
6337 if (!ws_basestrtou32(value, NULL, &uval, pref->info.base))
6338 return PREFS_SET_SYNTAX_ERR; /* number was bad */
6339 if (*pref->varp.uint != uval) {
6340 containing_module->prefs_changed_flags |= prefs_get_effect_flags(pref);
6341 *pref->varp.uint = uval;
6343 break;
6344 case PREF_BOOL:
6345 /* XXX - give an error if it's neither "true" nor "false"? */
6346 if (g_ascii_strcasecmp(value, "true") == 0)
6347 bval = true;
6348 else
6349 bval = false;
6350 if (*pref->varp.boolp != bval) {
6351 containing_module->prefs_changed_flags |= prefs_get_effect_flags(pref);
6352 *pref->varp.boolp = bval;
6354 break;
6356 case PREF_ENUM:
6357 /* XXX - give an error if it doesn't match? */
6358 enum_val = find_val_for_string(value, pref->info.enum_info.enumvals,
6359 *pref->varp.enump);
6360 if (*pref->varp.enump != enum_val) {
6361 containing_module->prefs_changed_flags |= prefs_get_effect_flags(pref);
6362 *pref->varp.enump = enum_val;
6364 break;
6366 case PREF_STRING:
6367 case PREF_SAVE_FILENAME:
6368 case PREF_OPEN_FILENAME:
6369 case PREF_DIRNAME:
6370 case PREF_DISSECTOR:
6371 containing_module->prefs_changed_flags |= prefs_set_string_value(pref, value, pref_current);
6372 break;
6374 case PREF_PASSWORD:
6375 /* Read value is every time empty */
6376 containing_module->prefs_changed_flags |= prefs_set_string_value(pref, "", pref_current);
6377 break;
6379 case PREF_RANGE:
6381 if (!prefs_set_range_value_work(pref, value, return_range_errors,
6382 &containing_module->prefs_changed_flags))
6383 return PREFS_SET_SYNTAX_ERR; /* number was bad */
6384 break;
6386 case PREF_DECODE_AS_RANGE:
6388 /* This is for backwards compatibility in case any of the preferences
6389 that shared the "Decode As" preference name and used to be PREF_RANGE
6390 are now applied directly to the Decode As funtionality */
6391 range_t *newrange;
6392 dissector_table_t sub_dissectors;
6393 dissector_handle_t handle;
6394 uint32_t i, j;
6396 if (range_convert_str_work(wmem_epan_scope(), &newrange, value, pref->info.max_value,
6397 return_range_errors) != CVT_NO_ERROR) {
6398 return PREFS_SET_SYNTAX_ERR; /* number was bad */
6401 if (!ranges_are_equal(*pref->varp.range, newrange)) {
6402 wmem_free(wmem_epan_scope(), *pref->varp.range);
6403 *pref->varp.range = newrange;
6404 containing_module->prefs_changed_flags |= prefs_get_effect_flags(pref);
6406 const char* table_name = prefs_get_dissector_table(pref);
6407 sub_dissectors = find_dissector_table(table_name);
6408 if (sub_dissectors != NULL) {
6409 handle = dissector_table_get_dissector_handle(sub_dissectors, module->title);
6410 if (handle != NULL) {
6411 /* Delete all of the old values from the dissector table */
6412 for (i = 0; i < (*pref->varp.range)->nranges; i++) {
6413 for (j = (*pref->varp.range)->ranges[i].low; j < (*pref->varp.range)->ranges[i].high; j++) {
6414 dissector_delete_uint(table_name, j, handle);
6415 decode_build_reset_list(table_name, dissector_table_get_type(sub_dissectors), GUINT_TO_POINTER(j), NULL, NULL);
6418 dissector_delete_uint(table_name, (*pref->varp.range)->ranges[i].high, handle);
6419 decode_build_reset_list(table_name, dissector_table_get_type(sub_dissectors), GUINT_TO_POINTER((*pref->varp.range)->ranges[i].high), NULL, NULL);
6422 /* Add new values to the dissector table */
6423 for (i = 0; i < newrange->nranges; i++) {
6424 for (j = newrange->ranges[i].low; j < newrange->ranges[i].high; j++) {
6425 dissector_change_uint(table_name, j, handle);
6426 decode_build_reset_list(table_name, dissector_table_get_type(sub_dissectors), GUINT_TO_POINTER(j), NULL, NULL);
6429 dissector_change_uint(table_name, newrange->ranges[i].high, handle);
6430 decode_build_reset_list(table_name, dissector_table_get_type(sub_dissectors), GUINT_TO_POINTER(newrange->ranges[i].high), NULL, NULL);
6433 /* XXX - Do we save the decode_as_entries file here? */
6436 } else {
6437 wmem_free(wmem_epan_scope(), newrange);
6439 break;
6442 case PREF_COLOR:
6444 if (!ws_hexstrtou32(value, NULL, &cval))
6445 return PREFS_SET_SYNTAX_ERR; /* number was bad */
6446 if ((pref->varp.colorp->red != RED_COMPONENT(cval)) ||
6447 (pref->varp.colorp->green != GREEN_COMPONENT(cval)) ||
6448 (pref->varp.colorp->blue != BLUE_COMPONENT(cval))) {
6449 containing_module->prefs_changed_flags |= prefs_get_effect_flags(pref);
6450 pref->varp.colorp->red = RED_COMPONENT(cval);
6451 pref->varp.colorp->green = GREEN_COMPONENT(cval);
6452 pref->varp.colorp->blue = BLUE_COMPONENT(cval);
6454 break;
6457 case PREF_CUSTOM:
6458 return pref->custom_cbs.set_cb(pref, value, &containing_module->prefs_changed_flags);
6460 case PREF_STATIC_TEXT:
6461 case PREF_UAT:
6462 case PREF_PROTO_TCP_SNDAMB_ENUM:
6464 /* There's no point in setting the TCP sequence override
6465 * value from the command line, because the pref is different
6466 * for each frame and reset to the default (0) for each new
6467 * file.
6469 break;
6474 return PREFS_SET_OK;
6477 typedef struct {
6478 FILE *pf;
6479 bool is_gui_module;
6480 } write_gui_pref_arg_t;
6482 const char *
6483 prefs_pref_type_name(pref_t *pref)
6485 const char *type_name = "[Unknown]";
6486 int type;
6488 if (!pref) {
6489 return type_name; /* ...or maybe assert? */
6492 type = pref->type;
6494 if (IS_PREF_OBSOLETE(type)) {
6495 type_name = "Obsolete";
6496 } else {
6497 RESET_PREF_OBSOLETE(type);
6500 switch (type) {
6502 case PREF_UINT:
6503 switch (pref->info.base) {
6505 case 10:
6506 type_name = "Decimal";
6507 break;
6509 case 8:
6510 type_name = "Octal";
6511 break;
6513 case 16:
6514 type_name = "Hexadecimal";
6515 break;
6517 break;
6519 case PREF_BOOL:
6520 type_name = "Boolean";
6521 break;
6523 case PREF_ENUM:
6524 case PREF_PROTO_TCP_SNDAMB_ENUM:
6525 type_name = "Choice";
6526 break;
6528 case PREF_STRING:
6529 type_name = "String";
6530 break;
6532 case PREF_SAVE_FILENAME:
6533 case PREF_OPEN_FILENAME:
6534 type_name = "Filename";
6535 break;
6537 case PREF_DIRNAME:
6538 type_name = "Directory";
6539 break;
6541 case PREF_RANGE:
6542 type_name = "Range";
6543 break;
6545 case PREF_COLOR:
6546 type_name = "Color";
6547 break;
6549 case PREF_CUSTOM:
6550 if (pref->custom_cbs.type_name_cb)
6551 return pref->custom_cbs.type_name_cb();
6552 type_name = "Custom";
6553 break;
6555 case PREF_DECODE_AS_RANGE:
6556 type_name = "Range (for Decode As)";
6557 break;
6559 case PREF_STATIC_TEXT:
6560 type_name = "Static text";
6561 break;
6563 case PREF_UAT:
6564 type_name = "UAT";
6565 break;
6567 case PREF_PASSWORD:
6568 type_name = "Password";
6569 break;
6571 case PREF_DISSECTOR:
6572 type_name = "Dissector";
6573 break;
6575 return type_name;
6578 unsigned int
6579 prefs_get_effect_flags(pref_t *pref)
6581 if (pref == NULL)
6582 return 0;
6584 return pref->effect_flags;
6587 void
6588 prefs_set_effect_flags(pref_t *pref, unsigned int flags)
6590 if (pref != NULL) {
6591 if (flags == 0) {
6592 ws_error("Setting \"%s\" preference effect flags to 0", pref->name);
6594 pref->effect_flags = flags;
6598 void
6599 prefs_set_effect_flags_by_name(module_t * module, const char *pref, unsigned int flags)
6601 prefs_set_effect_flags(prefs_find_preference(module, pref), flags);
6604 unsigned int
6605 prefs_get_module_effect_flags(module_t * module)
6607 if (module == NULL)
6608 return 0;
6610 return module->effect_flags;
6613 void
6614 prefs_set_module_effect_flags(module_t * module, unsigned int flags)
6616 if (module != NULL) {
6617 if (flags == 0) {
6618 ws_error("Setting module \"%s\" preference effect flags to 0", module->name);
6620 module->effect_flags = flags;
6624 char *
6625 prefs_pref_type_description(pref_t *pref)
6627 const char *type_desc = "An unknown preference type";
6628 int type;
6630 if (!pref) {
6631 return ws_strdup_printf("%s.", type_desc); /* ...or maybe assert? */
6634 type = pref->type;
6636 if (IS_PREF_OBSOLETE(type)) {
6637 type_desc = "An obsolete preference";
6638 } else {
6639 RESET_PREF_OBSOLETE(type);
6642 switch (type) {
6644 case PREF_UINT:
6645 switch (pref->info.base) {
6647 case 10:
6648 type_desc = "A decimal number";
6649 break;
6651 case 8:
6652 type_desc = "An octal number";
6653 break;
6655 case 16:
6656 type_desc = "A hexadecimal number";
6657 break;
6659 break;
6661 case PREF_BOOL:
6662 type_desc = "true or false (case-insensitive)";
6663 break;
6665 case PREF_ENUM:
6666 case PREF_PROTO_TCP_SNDAMB_ENUM:
6668 const enum_val_t *enum_valp = pref->info.enum_info.enumvals;
6669 GString *enum_str = g_string_new("One of: ");
6670 GString *desc_str = g_string_new("\nEquivalently, one of: ");
6671 bool distinct = false;
6672 while (enum_valp->name != NULL) {
6673 g_string_append(enum_str, enum_valp->name);
6674 g_string_append(desc_str, enum_valp->description);
6675 if (g_strcmp0(enum_valp->name, enum_valp->description) != 0) {
6676 distinct = true;
6678 enum_valp++;
6679 if (enum_valp->name != NULL) {
6680 g_string_append(enum_str, ", ");
6681 g_string_append(desc_str, ", ");
6684 if (distinct) {
6685 g_string_append(enum_str, desc_str->str);
6687 g_string_free(desc_str, TRUE);
6688 g_string_append(enum_str, "\n(case-insensitive).");
6689 return g_string_free(enum_str, FALSE);
6692 case PREF_STRING:
6693 type_desc = "A string";
6694 break;
6696 case PREF_SAVE_FILENAME:
6697 case PREF_OPEN_FILENAME:
6698 type_desc = "A path to a file";
6699 break;
6701 case PREF_DIRNAME:
6702 type_desc = "A path to a directory";
6703 break;
6705 case PREF_RANGE:
6707 type_desc = "A string denoting an positive integer range (e.g., \"1-20,30-40\")";
6708 break;
6711 case PREF_COLOR:
6713 type_desc = "A six-digit hexadecimal RGB color triplet (e.g. fce94f)";
6714 break;
6717 case PREF_CUSTOM:
6718 if (pref->custom_cbs.type_description_cb)
6719 return pref->custom_cbs.type_description_cb();
6720 type_desc = "A custom value";
6721 break;
6723 case PREF_DECODE_AS_RANGE:
6724 type_desc = "A string denoting an positive integer range for Decode As";
6725 break;
6727 case PREF_STATIC_TEXT:
6728 type_desc = "[Static text]";
6729 break;
6731 case PREF_UAT:
6732 type_desc = "Configuration data stored in its own file";
6733 break;
6735 case PREF_PASSWORD:
6736 type_desc = "Password (never stored on disk)";
6737 break;
6739 case PREF_DISSECTOR:
6740 type_desc = "A dissector name";
6741 break;
6743 default:
6744 break;
6746 return g_strdup(type_desc);
6749 bool
6750 prefs_pref_is_default(pref_t *pref)
6752 int type;
6753 if (!pref) return false;
6755 type = pref->type;
6756 if (IS_PREF_OBSOLETE(type)) {
6757 return false;
6758 } else {
6759 RESET_PREF_OBSOLETE(type);
6762 switch (type) {
6764 case PREF_UINT:
6765 if (pref->default_val.uint == *pref->varp.uint)
6766 return true;
6767 break;
6769 case PREF_BOOL:
6770 if (pref->default_val.boolval == *pref->varp.boolp)
6771 return true;
6772 break;
6774 case PREF_ENUM:
6775 case PREF_PROTO_TCP_SNDAMB_ENUM:
6776 if (pref->default_val.enumval == *pref->varp.enump)
6777 return true;
6778 break;
6780 case PREF_STRING:
6781 case PREF_SAVE_FILENAME:
6782 case PREF_OPEN_FILENAME:
6783 case PREF_DIRNAME:
6784 case PREF_PASSWORD:
6785 case PREF_DISSECTOR:
6786 if (!(g_strcmp0(pref->default_val.string, *pref->varp.string)))
6787 return true;
6788 break;
6790 case PREF_DECODE_AS_RANGE:
6791 case PREF_RANGE:
6793 if ((ranges_are_equal(pref->default_val.range, *pref->varp.range)))
6794 return true;
6795 break;
6798 case PREF_COLOR:
6800 if ((pref->default_val.color.red == pref->varp.colorp->red) &&
6801 (pref->default_val.color.green == pref->varp.colorp->green) &&
6802 (pref->default_val.color.blue == pref->varp.colorp->blue))
6803 return true;
6804 break;
6807 case PREF_CUSTOM:
6808 return pref->custom_cbs.is_default_cb(pref);
6810 case PREF_STATIC_TEXT:
6811 case PREF_UAT:
6812 return false;
6813 /* ws_assert_not_reached(); */
6814 break;
6816 return false;
6819 char *
6820 prefs_pref_to_str(pref_t *pref, pref_source_t source) {
6821 const char *pref_text = "[Unknown]";
6822 void *valp; /* pointer to preference value */
6823 color_t *pref_color;
6824 char *tmp_value, *ret_value;
6825 int type;
6827 if (!pref) {
6828 return g_strdup(pref_text);
6831 switch (source) {
6832 case pref_default:
6833 valp = &pref->default_val;
6834 /* valp = &boolval, &enumval, etc. are implied by union property */
6835 pref_color = &pref->default_val.color;
6836 break;
6837 case pref_stashed:
6838 valp = &pref->stashed_val;
6839 /* valp = &boolval, &enumval, etc. are implied by union property */
6840 pref_color = &pref->stashed_val.color;
6841 break;
6842 case pref_current:
6843 valp = pref->varp.uint;
6844 /* valp = boolval, enumval, etc. are implied by union property */
6845 pref_color = pref->varp.colorp;
6846 break;
6847 default:
6848 return g_strdup(pref_text);
6851 type = pref->type;
6852 if (IS_PREF_OBSOLETE(type)) {
6853 pref_text = "[Obsolete]";
6854 } else {
6855 RESET_PREF_OBSOLETE(type);
6858 switch (type) {
6860 case PREF_UINT:
6862 unsigned pref_uint = *(unsigned *) valp;
6863 switch (pref->info.base) {
6865 case 10:
6866 return ws_strdup_printf("%u", pref_uint);
6868 case 8:
6869 return ws_strdup_printf("%#o", pref_uint);
6871 case 16:
6872 return ws_strdup_printf("%#x", pref_uint);
6874 break;
6877 case PREF_BOOL:
6878 return g_strdup((*(bool *) valp) ? "TRUE" : "FALSE");
6880 case PREF_ENUM:
6881 case PREF_PROTO_TCP_SNDAMB_ENUM:
6883 int pref_enumval = *(int *) valp;
6884 const enum_val_t *enum_valp = pref->info.enum_info.enumvals;
6886 * TODO - We write the "description" value, because the "name" values
6887 * weren't validated to be command line friendly until 5.0, and a few
6888 * of them had to be changed. This allows older versions of Wireshark
6889 * to read preferences that they supported, as we supported either
6890 * the short name or the description when reading the preference files
6891 * or an "-o" option. Once 5.0 is the oldest supported version, switch
6892 * to writing the name below.
6894 while (enum_valp->name != NULL) {
6895 if (enum_valp->value == pref_enumval)
6896 return g_strdup(enum_valp->description);
6897 enum_valp++;
6899 break;
6902 case PREF_STRING:
6903 case PREF_SAVE_FILENAME:
6904 case PREF_OPEN_FILENAME:
6905 case PREF_DIRNAME:
6906 case PREF_PASSWORD:
6907 case PREF_DISSECTOR:
6908 return g_strdup(*(const char **) valp);
6910 case PREF_DECODE_AS_RANGE:
6911 case PREF_RANGE:
6912 /* Convert wmem to g_alloc memory */
6913 tmp_value = range_convert_range(NULL, *(range_t **) valp);
6914 ret_value = g_strdup(tmp_value);
6915 wmem_free(NULL, tmp_value);
6916 return ret_value;
6918 case PREF_COLOR:
6919 return ws_strdup_printf("%02x%02x%02x",
6920 (pref_color->red * 255 / 65535),
6921 (pref_color->green * 255 / 65535),
6922 (pref_color->blue * 255 / 65535));
6924 case PREF_CUSTOM:
6925 if (pref->custom_cbs.to_str_cb)
6926 return pref->custom_cbs.to_str_cb(pref, source == pref_default ? true : false);
6927 pref_text = "[Custom]";
6928 break;
6930 case PREF_STATIC_TEXT:
6931 pref_text = "[Static text]";
6932 break;
6934 case PREF_UAT:
6936 uat_t *uat = pref->varp.uat;
6937 if (uat && uat->filename)
6938 return ws_strdup_printf("[Managed in the file \"%s\"]", uat->filename);
6939 else
6940 pref_text = "[Managed in an unknown file]";
6941 break;
6944 default:
6945 break;
6947 return g_strdup(pref_text);
6951 * Write out a single dissector preference.
6953 static void
6954 write_pref(void *data, void *user_data)
6956 pref_t *pref = (pref_t *)data;
6957 write_pref_arg_t *arg = (write_pref_arg_t *)user_data;
6958 char **desc_lines;
6959 int i;
6960 int type;
6962 type = pref->type;
6964 if (IS_PREF_OBSOLETE(type)) {
6966 * This preference is no longer supported; it's not a
6967 * real preference, so we don't write it out (i.e., we
6968 * treat it as if it weren't found in the list of
6969 * preferences, and we weren't called in the first place).
6971 return;
6972 } else {
6973 RESET_PREF_OBSOLETE(type);
6976 switch (type) {
6978 case PREF_STATIC_TEXT:
6979 case PREF_UAT:
6980 /* Nothing to do; don't bother printing the description */
6981 return;
6982 case PREF_DECODE_AS_RANGE:
6983 /* Data is saved through Decode As mechanism and not part of preferences file */
6984 return;
6985 case PREF_PROTO_TCP_SNDAMB_ENUM:
6986 /* Not written to the preference file because the override is only
6987 * for the lifetime of the capture file and there is no single
6988 * value to write.
6990 return;
6991 default:
6992 break;
6995 if (pref->type != PREF_CUSTOM || pref->custom_cbs.type_name_cb() != NULL) {
6997 * The prefix will either be the module name or the parent
6998 * name if it's a subtree
7000 const char *name_prefix = (arg->module->name != NULL) ? arg->module->name : arg->module->parent->name;
7001 char *type_desc, *pref_text;
7002 const char * def_prefix = prefs_pref_is_default(pref) ? "#" : "";
7004 if (pref->type == PREF_CUSTOM)
7005 fprintf(arg->pf, "\n# %s", pref->custom_cbs.type_name_cb());
7006 fprintf(arg->pf, "\n");
7007 if (pref->description &&
7008 (g_ascii_strncasecmp(pref->description,"", 2) != 0)) {
7009 if (pref->type != PREF_CUSTOM) {
7010 /* We get duplicate lines otherwise. */
7012 desc_lines = g_strsplit(pref->description, "\n", 0);
7013 for (i = 0; desc_lines[i] != NULL; ++i) {
7014 fprintf(arg->pf, "# %s\n", desc_lines[i]);
7016 g_strfreev(desc_lines);
7018 } else {
7019 fprintf(arg->pf, "# No description\n");
7022 type_desc = prefs_pref_type_description(pref);
7023 desc_lines = g_strsplit(type_desc, "\n", 0);
7024 for (i = 0; desc_lines[i] != NULL; ++i) {
7025 fprintf(arg->pf, "# %s\n", desc_lines[i]);
7027 g_strfreev(desc_lines);
7028 g_free(type_desc);
7030 pref_text = prefs_pref_to_str(pref, pref_current);
7031 fprintf(arg->pf, "%s%s.%s: ", def_prefix, name_prefix, pref->name);
7032 if (pref->type != PREF_PASSWORD)
7034 desc_lines = g_strsplit(pref_text, "\n", 0);
7035 for (i = 0; desc_lines[i] != NULL; ++i) {
7036 fprintf(arg->pf, "%s%s\n", i == 0 ? "" : def_prefix, desc_lines[i]);
7038 if (i == 0)
7039 fprintf(arg->pf, "\n");
7040 g_strfreev(desc_lines);
7041 } else {
7042 /* We never store password value */
7043 fprintf(arg->pf, "\n");
7045 g_free(pref_text);
7050 static void
7051 count_non_uat_pref(void *data, void *user_data)
7053 pref_t *pref = (pref_t *)data;
7054 int *arg = (int *)user_data;
7056 switch (pref->type)
7058 case PREF_UAT:
7059 case PREF_OBSOLETE:
7060 case PREF_DECODE_AS_RANGE:
7061 case PREF_PROTO_TCP_SNDAMB_ENUM:
7062 //These types are not written in preference file
7063 break;
7064 default:
7065 (*arg)++;
7066 break;
7070 static int num_non_uat_prefs(module_t *module)
7072 int num = 0;
7074 g_list_foreach(module->prefs, count_non_uat_pref, &num);
7076 return num;
7080 * Write out all preferences for a module.
7082 static unsigned
7083 write_module_prefs(module_t *module, void *user_data)
7085 write_gui_pref_arg_t *gui_pref_arg = (write_gui_pref_arg_t*)user_data;
7086 write_pref_arg_t arg;
7088 /* The GUI module needs to be explicitly called out so it
7089 can be written out of order */
7090 if ((module == gui_module) && (gui_pref_arg->is_gui_module != true))
7091 return 0;
7093 /* Write a header for the main modules and GUI sub-modules */
7094 if (((module->parent == NULL) || (module->parent == gui_module)) &&
7095 ((prefs_module_has_submodules(module)) ||
7096 (num_non_uat_prefs(module) > 0) ||
7097 (module->name == NULL))) {
7098 if ((module->name == NULL) && (module->parent != NULL)) {
7099 fprintf(gui_pref_arg->pf, "\n####### %s: %s ########\n", module->parent->title, module->title);
7100 } else {
7101 fprintf(gui_pref_arg->pf, "\n####### %s ########\n", module->title);
7105 arg.module = module;
7106 arg.pf = gui_pref_arg->pf;
7107 g_list_foreach(arg.module->prefs, write_pref, &arg);
7109 if (prefs_module_has_submodules(module))
7110 return prefs_modules_foreach_submodules(module, write_module_prefs, user_data);
7112 return 0;
7115 #ifdef _WIN32
7116 static void
7117 write_registry(void)
7119 HKEY hTestKey;
7120 DWORD data;
7121 DWORD data_size;
7122 DWORD ret;
7124 ret = RegCreateKeyExA(HKEY_CURRENT_USER, REG_HKCU_WIRESHARK_KEY, 0, NULL,
7125 REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL,
7126 &hTestKey, NULL);
7127 if (ret != ERROR_SUCCESS) {
7128 ws_noisy("Cannot open HKCU "REG_HKCU_WIRESHARK_KEY": 0x%lx", ret);
7129 return;
7132 data = ws_log_console_open;
7133 data_size = sizeof(DWORD);
7134 ret = RegSetValueExA(hTestKey, LOG_HKCU_CONSOLE_OPEN, 0, REG_DWORD, (const BYTE *)&data, data_size);
7135 if (ret == ERROR_SUCCESS) {
7136 ws_noisy("Wrote "LOG_HKCU_CONSOLE_OPEN" to Windows registry: 0x%lu", data);
7138 else {
7139 ws_noisy("Error writing registry key "LOG_HKCU_CONSOLE_OPEN": 0x%lx", ret);
7142 RegCloseKey(hTestKey);
7144 #endif
7146 /* Write out "prefs" to the user's preferences file, and return 0.
7148 If the preferences file path is NULL, write to stdout.
7150 If we got an error, stuff a pointer to the path of the preferences file
7151 into "*pf_path_return", and return the errno. */
7153 write_prefs(char **pf_path_return)
7155 char *pf_path;
7156 FILE *pf;
7157 write_gui_pref_arg_t write_gui_pref_info;
7159 /* Needed for "-G defaultprefs" */
7160 init_prefs();
7162 #ifdef _WIN32
7163 write_registry();
7164 #endif
7166 /* To do:
7167 * - Split output lines longer than MAX_VAL_LEN
7168 * - Create a function for the preference directory check/creation
7169 * so that duplication can be avoided with filter.c
7172 if (pf_path_return != NULL) {
7173 pf_path = get_persconffile_path(PF_NAME, true);
7174 if ((pf = ws_fopen(pf_path, "w")) == NULL) {
7175 *pf_path_return = pf_path;
7176 return errno;
7178 g_free(pf_path);
7179 } else {
7180 pf = stdout;
7184 * If the preferences file is being written, be sure to write UAT files
7185 * first that were migrated from the preferences file.
7187 if (pf_path_return != NULL) {
7188 if (prefs.filter_expressions_old) {
7189 char *err = NULL;
7190 prefs.filter_expressions_old = false;
7191 if (!uat_save(uat_get_table_by_name("Display expressions"), &err)) {
7192 ws_warning("Unable to save Display expressions: %s", err);
7193 g_free(err);
7197 module_t *extcap_module = prefs_find_module("extcap");
7198 if (extcap_module && !prefs.capture_no_extcap) {
7199 char *ext_path = get_persconffile_path("extcap.cfg", true);
7200 FILE *extf;
7201 if ((extf = ws_fopen(ext_path, "w")) == NULL) {
7202 if (errno != EISDIR) {
7203 ws_warning("Unable to save extcap preferences \"%s\": %s",
7204 ext_path, g_strerror(errno));
7206 g_free(ext_path);
7207 } else {
7208 g_free(ext_path);
7210 fputs("# Extcap configuration file for Wireshark " VERSION ".\n"
7211 "#\n"
7212 "# This file is regenerated each time preferences are saved within\n"
7213 "# Wireshark. Making manual changes should be safe, however.\n"
7214 "# Preferences that have been commented out have not been\n"
7215 "# changed from their default value.\n", extf);
7217 write_gui_pref_info.pf = extf;
7218 write_gui_pref_info.is_gui_module = false;
7220 write_module_prefs(extcap_module, &write_gui_pref_info);
7222 fclose(extf);
7227 fputs("# Configuration file for Wireshark " VERSION ".\n"
7228 "#\n"
7229 "# This file is regenerated each time preferences are saved within\n"
7230 "# Wireshark. Making manual changes should be safe, however.\n"
7231 "# Preferences that have been commented out have not been\n"
7232 "# changed from their default value.\n", pf);
7235 * For "backwards compatibility" the GUI module is written first as it's
7236 * at the top of the file. This is followed by all modules that can't
7237 * fit into the preferences read/write API. Finally the remaining modules
7238 * are written in alphabetical order (including of course the protocol preferences)
7240 write_gui_pref_info.pf = pf;
7241 write_gui_pref_info.is_gui_module = true;
7243 write_module_prefs(gui_module, &write_gui_pref_info);
7245 write_gui_pref_info.is_gui_module = false;
7246 prefs_modules_foreach_submodules(NULL, write_module_prefs, &write_gui_pref_info);
7248 fclose(pf);
7250 /* XXX - catch I/O errors (e.g. "ran out of disk space") and return
7251 an error indication, or maybe write to a new preferences file and
7252 rename that file on top of the old one only if there are not I/O
7253 errors. */
7254 return 0;
7257 /** The col_list is only partly managed by the custom preference API
7258 * because its data is shared between multiple preferences, so
7259 * it's freed here
7261 static void
7262 free_col_info(GList *list)
7264 fmt_data *cfmt;
7265 GList *list_head = list;
7267 while (list != NULL) {
7268 cfmt = (fmt_data *)list->data;
7270 g_free(cfmt->title);
7271 g_free(cfmt->custom_fields);
7272 g_free(cfmt);
7273 list = g_list_next(list);
7275 g_list_free(list_head);
7279 * Editor modelines
7281 * Local Variables:
7282 * c-basic-offset: 4
7283 * tab-width: 8
7284 * indent-tabs-mode: nil
7285 * End:
7287 * ex: set shiftwidth=4 tabstop=8 expandtab:
7288 * :indentSize=4:tabSize=8:noTabs=true: