update epan/dissectors/pidl/drsuapi/drsuapi.idl from samba
[wireshark-sm.git] / epan / prefs.c
blobd504cfc7bf97b1cfd31319bacee7579fc407d9ca
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/filesystem.h>
27 #include <epan/addr_resolv.h>
28 #include <epan/oids.h>
29 #include <epan/maxmind_db.h>
30 #include <epan/packet.h>
31 #include <epan/prefs.h>
32 #include <epan/proto.h>
33 #include <epan/strutil.h>
34 #include <epan/column.h>
35 #include <epan/decode_as.h>
36 #include <capture_opts.h>
37 #include <wsutil/file_util.h>
38 #include <wsutil/report_message.h>
39 #include <wsutil/wslog.h>
40 #include <wsutil/ws_assert.h>
41 #include <wsutil/array.h>
43 #include <epan/prefs-int.h>
44 #include <epan/uat-int.h>
46 #include "epan/filter_expressions.h"
48 #include "epan/wmem_scopes.h"
49 #include <epan/stats_tree.h>
51 #define REG_HKCU_WIRESHARK_KEY "Software\\Wireshark"
54 * Module alias.
56 typedef struct pref_module_alias {
57 const char *name; /**< name of module alias */
58 module_t *module; /**< module for which it's an alias */
59 } module_alias_t;
61 /* Internal functions */
62 static module_t *find_subtree(module_t *parent, const char *tilte);
63 static module_t *prefs_register_module_or_subtree(module_t *parent,
64 const char *name, const char *title, const char *description, const char *help,
65 bool is_subtree, void (*apply_cb)(void), bool use_gui);
66 static void prefs_register_modules(void);
67 static module_t *prefs_find_module_alias(const char *name);
68 static prefs_set_pref_e set_pref(char*, const char*, void *, bool);
69 static void free_col_info(GList *);
70 static void pre_init_prefs(void);
71 static bool prefs_is_column_visible(const char *cols_hidden, int col);
72 static bool prefs_is_column_fmt_visible(const char *cols_hidden, fmt_data *cfmt);
73 static unsigned prefs_module_list_foreach(wmem_tree_t *module_list, module_cb callback,
74 void *user_data, bool skip_obsolete);
75 static int find_val_for_string(const char *needle, const enum_val_t *haystack, int default_value);
77 #define IS_PREF_OBSOLETE(p) ((p) & PREF_OBSOLETE)
78 #define SET_PREF_OBSOLETE(p) ((p) |= PREF_OBSOLETE)
79 #define RESET_PREF_OBSOLETE(p) ((p) &= ~PREF_OBSOLETE)
81 #define PF_NAME "preferences"
82 #define OLD_GPF_NAME "wireshark.conf" /* old name for global preferences file */
84 static bool prefs_initialized;
85 static char *gpf_path;
86 static char *cols_hidden_list;
87 static char *cols_hidden_fmt_list;
88 static bool gui_theme_is_dark;
91 * XXX - variables to allow us to attempt to interpret the first
92 * "mgcp.{tcp,udp}.port" in a preferences file as
93 * "mgcp.{tcp,udp}.gateway_port" and the second as
94 * "mgcp.{tcp,udp}.callagent_port".
96 static int mgcp_tcp_port_count;
97 static int mgcp_udp_port_count;
99 e_prefs prefs;
101 static const enum_val_t gui_console_open_type[] = {
102 {"NEVER", "NEVER", LOG_CONSOLE_OPEN_NEVER},
103 {"AUTOMATIC", "AUTOMATIC", LOG_CONSOLE_OPEN_AUTO},
104 {"ALWAYS", "ALWAYS", LOG_CONSOLE_OPEN_ALWAYS},
105 {NULL, NULL, -1}
108 static const enum_val_t gui_version_placement_type[] = {
109 {"WELCOME", "WELCOME", version_welcome_only},
110 {"TITLE", "TITLE", version_title_only},
111 {"BOTH", "BOTH", version_both},
112 {"NEITHER", "NEITHER", version_neither},
113 {NULL, NULL, -1}
116 static const enum_val_t gui_fileopen_style[] = {
117 {"LAST_OPENED", "LAST_OPENED", FO_STYLE_LAST_OPENED},
118 {"SPECIFIED", "SPECIFIED", FO_STYLE_SPECIFIED},
119 {"CWD", "CWD", FO_STYLE_CWD},
120 {NULL, NULL, -1}
123 static const enum_val_t gui_toolbar_style[] = {
124 {"ICONS", "ICONS", 0},
125 {"TEXT", "TEXT", 1},
126 {"BOTH", "BOTH", 2},
127 {NULL, NULL, -1}
130 static const enum_val_t gui_layout_content[] = {
131 {"NONE", "NONE", 0},
132 {"PLIST", "PLIST", 1},
133 {"PDETAILS", "PDETAILS", 2},
134 {"PBYTES", "PBYTES", 3},
135 {"PDIAGRAM", "PDIAGRAM", 4},
136 {NULL, NULL, -1}
139 static const enum_val_t gui_packet_dialog_layout[] = {
140 {"vertical", "Vertical (Stacked)", layout_vertical},
141 {"horizontal", "Horizontal (Side-by-side)", layout_horizontal},
142 {NULL, NULL, -1}
145 static const enum_val_t gui_update_channel[] = {
146 {"DEVELOPMENT", "DEVELOPMENT", UPDATE_CHANNEL_DEVELOPMENT},
147 {"STABLE", "STABLE", UPDATE_CHANNEL_STABLE},
148 {NULL, NULL, -1}
151 static const enum_val_t gui_selection_style[] = {
152 {"DEFAULT", "DEFAULT", COLOR_STYLE_DEFAULT},
153 {"FLAT", "FLAT", COLOR_STYLE_FLAT},
154 {"GRADIENT", "GRADIENT", COLOR_STYLE_GRADIENT},
155 {NULL, NULL, -1}
158 static const enum_val_t gui_color_scheme[] = {
159 {"system", "System Default", COLOR_SCHEME_DEFAULT},
160 {"light", "Light Mode", COLOR_SCHEME_LIGHT},
161 {"dark", "Dark Mode", COLOR_SCHEME_DARK},
162 {NULL, NULL, -1}
165 static const enum_val_t gui_packet_list_copy_format_options_for_keyboard_shortcut[] = {
166 {"TEXT", "Text", COPY_FORMAT_TEXT},
167 {"CSV", "CSV", COPY_FORMAT_CSV},
168 {"YAML", "YAML", COPY_FORMAT_YAML},
169 {"HTML", "HTML", COPY_FORMAT_HTML},
170 {NULL, NULL, -1}
173 /* None : Historical behavior, no deinterlacing */
174 #define CONV_DEINT_CHOICE_NONE 0
175 /* MI : MAC & Interface */
176 #define CONV_DEINT_CHOICE_MI CONV_DEINT_KEY_MAC + CONV_DEINT_KEY_INTERFACE
177 /* VM : VLAN & MAC */
178 #define CONV_DEINT_CHOICE_VM CONV_DEINT_KEY_VLAN + CONV_DEINT_KEY_MAC
179 /* VMI : VLAN & MAC & Interface */
180 #define CONV_DEINT_CHOICE_VMI CONV_DEINT_KEY_VLAN + CONV_DEINT_KEY_MAC + CONV_DEINT_KEY_INTERFACE
182 static const enum_val_t conv_deint_options[] = {
183 {"NONE", "NONE", CONV_DEINT_CHOICE_NONE},
184 {".MI", ".MI", CONV_DEINT_CHOICE_MI },
185 {"VM.", "VM.", CONV_DEINT_CHOICE_VM },
186 {"VMI", "VMI", CONV_DEINT_CHOICE_VMI },
187 {NULL, NULL, -1}
190 static const enum_val_t abs_time_format_options[] = {
191 {"NEVER", "Never", ABS_TIME_ASCII_NEVER},
192 {"TREE", "Protocol tree only", ABS_TIME_ASCII_TREE},
193 {"COLUMN", "Protocol tree and columns", ABS_TIME_ASCII_COLUMN},
194 {"ALWAYS", "Always", ABS_TIME_ASCII_ALWAYS},
195 {NULL, NULL, -1}
198 #if defined(HAVE_PCAP_CREATE)
199 /* Can set monitor mode and buffer size. */
200 static int num_capture_cols = 7;
201 static const char *capture_cols[7] = {
202 "INTERFACE",
203 "LINK",
204 "PMODE",
205 "SNAPLEN",
206 "MONITOR",
207 "BUFFER",
208 "FILTER"
210 #define CAPTURE_COL_TYPE_DESCRIPTION \
211 "Possible values: INTERFACE, LINK, PMODE, SNAPLEN, MONITOR, BUFFER, FILTER\n"
212 #elif defined(CAN_SET_CAPTURE_BUFFER_SIZE)
213 /* Can set buffer size but not monitor mode. */
214 static int num_capture_cols = 6;
215 static const char *capture_cols[6] = {
216 "INTERFACE",
217 "LINK",
218 "PMODE",
219 "SNAPLEN",
220 "BUFFER",
221 "FILTER"
223 #define CAPTURE_COL_TYPE_DESCRIPTION \
224 "Possible values: INTERFACE, LINK, PMODE, SNAPLEN, BUFFER, FILTER\n"
225 #else
226 /* Can neither set buffer size nor monitor mode. */
227 static int num_capture_cols = 5;
228 static const char *capture_cols[5] = {
229 "INTERFACE",
230 "LINK",
231 "PMODE",
232 "SNAPLEN",
233 "FILTER"
235 #define CAPTURE_COL_TYPE_DESCRIPTION \
236 "Possible values: INTERFACE, LINK, PMODE, SNAPLEN, FILTER\n"
237 #endif
239 static const enum_val_t gui_packet_list_elide_mode[] = {
240 {"LEFT", "LEFT", ELIDE_LEFT},
241 {"RIGHT", "RIGHT", ELIDE_RIGHT},
242 {"MIDDLE", "MIDDLE", ELIDE_MIDDLE},
243 {"NONE", "NONE", ELIDE_NONE},
244 {NULL, NULL, -1}
247 /** Struct to hold preference data */
248 struct preference {
249 const char *name; /**< name of preference */
250 const char *title; /**< title to use in GUI */
251 const char *description; /**< human-readable description of preference */
252 int ordinal; /**< ordinal number of this preference */
253 int type; /**< type of that preference */
254 unsigned int effect_flags; /**< Flags of types effected by preference (PREF_TYPE_DISSECTION, PREF_EFFECT_CAPTURE, etc).
255 Flags must be non-zero to ensure saving to disk */
256 union { /* The Qt preference code assumes that these will all be pointers (and unique) */
257 unsigned *uint;
258 bool *boolp;
259 int *enump;
260 char **string;
261 range_t **range;
262 struct epan_uat* uat;
263 color_t *colorp;
264 GList** list;
265 } varp; /**< pointer to variable storing the value */
266 union {
267 unsigned uint;
268 bool boolval;
269 int enumval;
270 char *string;
271 range_t *range;
272 color_t color;
273 GList* list;
274 } stashed_val; /**< original value, when editing from the GUI */
275 union {
276 unsigned uint;
277 bool boolval;
278 int enumval;
279 char *string;
280 range_t *range;
281 color_t color;
282 GList* list;
283 } default_val; /**< the default value of the preference */
284 union {
285 unsigned base; /**< input/output base, for PREF_UINT */
286 uint32_t max_value; /**< maximum value of a range */
287 struct {
288 const enum_val_t *enumvals; /**< list of name & values */
289 bool radio_buttons; /**< true if it should be shown as
290 radio buttons rather than as an
291 option menu or combo box in
292 the preferences tab */
293 } enum_info; /**< for PREF_ENUM */
294 } info; /**< display/text file information */
295 struct pref_custom_cbs custom_cbs; /**< for PREF_CUSTOM */
296 const char *dissector_table; /**< for PREF_DECODE_AS_RANGE */
297 const char *dissector_desc; /**< for PREF_DECODE_AS_RANGE */
300 const char* prefs_get_description(pref_t *pref)
302 return pref->description;
305 const char* prefs_get_title(pref_t *pref)
307 return pref->title;
310 int prefs_get_type(pref_t *pref)
312 return pref->type;
315 const char* prefs_get_name(pref_t *pref)
317 return pref->name;
320 uint32_t prefs_get_max_value(pref_t *pref)
322 return pref->info.max_value;
325 const char* prefs_get_dissector_table(pref_t *pref)
327 return pref->dissector_table;
330 static const char* prefs_get_dissector_description(pref_t *pref)
332 return pref->dissector_desc;
336 * List of all modules with preference settings.
338 static wmem_tree_t *prefs_modules;
341 * List of all modules that should show up at the top level of the
342 * tree in the preference dialog box.
344 static wmem_tree_t *prefs_top_level_modules;
347 * List of aliases for modules.
349 static wmem_tree_t *prefs_module_aliases;
351 /** Sets up memory used by proto routines. Called at program startup */
352 void
353 prefs_init(void)
355 memset(&prefs, 0, sizeof(prefs));
356 prefs_modules = wmem_tree_new(wmem_epan_scope());
357 prefs_top_level_modules = wmem_tree_new(wmem_epan_scope());
358 prefs_module_aliases = wmem_tree_new(wmem_epan_scope());
362 * Free the strings for a string-like preference.
364 static void
365 free_string_like_preference(pref_t *pref)
367 g_free(*pref->varp.string);
368 *pref->varp.string = NULL;
369 g_free(pref->default_val.string);
370 pref->default_val.string = NULL;
373 static void
374 free_pref(void *data, void *user_data _U_)
376 pref_t *pref = (pref_t *)data;
377 int type = pref->type;
379 /* we reset the PREF_OBSOLETE bit in order to allow the original preference to be freed */
380 RESET_PREF_OBSOLETE(type);
382 switch (type) {
383 case PREF_BOOL:
384 case PREF_ENUM:
385 case PREF_UINT:
386 case PREF_STATIC_TEXT:
387 case PREF_UAT:
388 case PREF_COLOR:
389 break;
390 case PREF_STRING:
391 case PREF_SAVE_FILENAME:
392 case PREF_OPEN_FILENAME:
393 case PREF_DIRNAME:
394 case PREF_PASSWORD:
395 case PREF_DISSECTOR:
396 free_string_like_preference(pref);
397 break;
398 case PREF_RANGE:
399 case PREF_DECODE_AS_RANGE:
400 wmem_free(wmem_epan_scope(), *pref->varp.range);
401 *pref->varp.range = NULL;
402 wmem_free(wmem_epan_scope(), pref->default_val.range);
403 pref->default_val.range = NULL;
404 break;
405 case PREF_CUSTOM:
406 if (strcmp(pref->name, "columns") == 0)
407 pref->stashed_val.boolval = true;
408 pref->custom_cbs.free_cb(pref);
409 break;
410 /* non-generic preferences */
411 case PREF_PROTO_TCP_SNDAMB_ENUM:
412 break;
415 g_free(pref);
418 static unsigned
419 free_module_prefs(module_t *module, void *data _U_)
421 if (module->prefs) {
422 g_list_foreach(module->prefs, free_pref, NULL);
423 g_list_free(module->prefs);
425 module->prefs = NULL;
426 module->numprefs = 0;
427 if (module->submodules) {
428 prefs_module_list_foreach(module->submodules, free_module_prefs, NULL, false);
430 /* We don't free the actual module: its submodules pointer points to
431 a wmem_tree and the module itself is stored in a wmem_tree
434 return 0;
437 /** Frees memory used by proto routines. Called at program shutdown */
438 void
439 prefs_cleanup(void)
441 /* This isn't strictly necessary since we're exiting anyway, but let's
442 * do what clean up we can.
444 prefs_module_list_foreach(prefs_modules, free_module_prefs, NULL, false);
446 /* Clean the uats */
447 uat_cleanup();
449 /* Shut down mmdbresolve */
450 maxmind_db_pref_cleanup();
452 g_free(prefs.saved_at_version);
453 g_free(gpf_path);
454 gpf_path = NULL;
457 void prefs_set_gui_theme_is_dark(bool is_dark)
459 gui_theme_is_dark = is_dark;
463 * Register a module that will have preferences.
464 * Specify the module under which to register it or NULL to register it
465 * at the top level, the name used for the module in the preferences file,
466 * the title used in the tab for it in a preferences dialog box, and a
467 * routine to call back when we apply the preferences.
469 static module_t *
470 prefs_register_module(module_t *parent, const char *name, const char *title,
471 const char *description, const char *help, void (*apply_cb)(void),
472 const bool use_gui)
474 return prefs_register_module_or_subtree(parent, name, title, description, help,
475 false, apply_cb, use_gui);
478 static void
479 prefs_deregister_module(module_t *parent, const char *name, const char *title)
481 /* Remove this module from the list of all modules */
482 module_t *module = (module_t *)wmem_tree_remove_string(prefs_modules, name, WMEM_TREE_STRING_NOCASE);
484 if (!module)
485 return;
487 if (parent == NULL) {
488 /* Remove from top */
489 wmem_tree_remove_string(prefs_top_level_modules, title, WMEM_TREE_STRING_NOCASE);
490 } else if (parent->submodules) {
491 /* Remove from parent */
492 wmem_tree_remove_string(parent->submodules, title, WMEM_TREE_STRING_NOCASE);
495 free_module_prefs(module, NULL);
496 wmem_free(wmem_epan_scope(), module);
500 * Register a subtree that will have modules under it.
501 * Specify the module under which to register it or NULL to register it
502 * at the top level and the title used in the tab for it in a preferences
503 * dialog box.
505 static module_t *
506 prefs_register_subtree(module_t *parent, const char *title, const char *description,
507 void (*apply_cb)(void))
509 return prefs_register_module_or_subtree(parent, NULL, title, description, NULL,
510 true, apply_cb,
511 parent ? parent->use_gui : false);
514 static module_t *
515 prefs_register_module_or_subtree(module_t *parent, const char *name,
516 const char *title, const char *description,
517 const char *help,
518 bool is_subtree, void (*apply_cb)(void),
519 bool use_gui)
521 module_t *module;
523 /* this module may have been created as a subtree item previously */
524 if ((module = find_subtree(parent, title))) {
525 /* the module is currently a subtree */
526 module->name = name;
527 module->apply_cb = apply_cb;
528 module->description = description;
529 module->help = help;
531 if (prefs_find_module(name) == NULL) {
532 wmem_tree_insert_string(prefs_modules, name, module,
533 WMEM_TREE_STRING_NOCASE);
536 return module;
539 module = wmem_new(wmem_epan_scope(), module_t);
540 module->name = name;
541 module->title = title;
542 module->description = description;
543 module->help = help;
544 module->apply_cb = apply_cb;
545 module->prefs = NULL; /* no preferences, to start */
546 module->parent = parent;
547 module->submodules = NULL; /* no submodules, to start */
548 module->numprefs = 0;
549 module->prefs_changed_flags = 0;
550 module->obsolete = false;
551 module->use_gui = use_gui;
552 /* A module's preferences affects dissection unless otherwise told */
553 module->effect_flags = PREF_EFFECT_DISSECTION;
556 * Do we have a module name?
558 if (name != NULL) {
560 /* Accept any letter case to conform with protocol names. ASN1 protocols
561 * don't use lower case names, so we can't require lower case. */
562 if (module_check_valid_name(name, false) != '\0') {
563 ws_error("Preference module \"%s\" contains invalid characters", name);
567 * Make sure there's not already a module with that
568 * name. Crash if there is, as that's an error in the
569 * code, and the code has to be fixed not to register
570 * more than one module with the same name.
572 * We search the list of all modules; the subtree stuff
573 * doesn't require preferences in subtrees to have names
574 * that reflect the subtree they're in (that would require
575 * protocol preferences to have a bogus "protocol.", or
576 * something such as that, to be added to all their names).
578 if (prefs_find_module(name) != NULL)
579 ws_error("Preference module \"%s\" is being registered twice", name);
582 * Insert this module in the list of all modules.
584 wmem_tree_insert_string(prefs_modules, name, module, WMEM_TREE_STRING_NOCASE);
585 } else {
587 * This has no name, just a title; check to make sure it's a
588 * subtree, and crash if it's not.
590 if (!is_subtree)
591 ws_error("Preferences module with no name is being registered at the top level");
595 * Insert this module into the appropriate place in the display
596 * tree.
598 if (parent == NULL) {
600 * It goes at the top.
602 wmem_tree_insert_string(prefs_top_level_modules, title, module, WMEM_TREE_STRING_NOCASE);
603 } else {
605 * It goes into the list for this module.
608 if (parent->submodules == NULL)
609 parent->submodules = wmem_tree_new(wmem_epan_scope());
611 wmem_tree_insert_string(parent->submodules, title, module, WMEM_TREE_STRING_NOCASE);
614 return module;
617 void
618 prefs_register_module_alias(const char *name, module_t *module)
620 module_alias_t *alias;
623 * Accept any name that can occur in protocol names. We allow upper-case
624 * letters, to handle the Diameter dissector having used "Diameter" rather
625 * than "diameter" as its preference module name in the past.
627 * Crash if the name is invalid, as that's an error in the code, but the name
628 * can be used on the command line, and shouldn't require quoting, etc.
630 if (module_check_valid_name(name, false) != '\0') {
631 ws_error("Preference module alias \"%s\" contains invalid characters", name);
635 * Make sure there's not already an alias with that
636 * name. Crash if there is, as that's an error in the
637 * code, and the code has to be fixed not to register
638 * more than one alias with the same name.
640 * We search the list of all aliases.
642 if (prefs_find_module_alias(name) != NULL)
643 ws_error("Preference module alias \"%s\" is being registered twice", name);
645 alias = wmem_new(wmem_epan_scope(), module_alias_t);
646 alias->name = name;
647 alias->module = module;
650 * Insert this module in the list of all modules.
652 wmem_tree_insert_string(prefs_module_aliases, name, alias, WMEM_TREE_STRING_NOCASE);
656 * Register that a protocol has preferences.
658 module_t *protocols_module;
660 module_t *
661 prefs_register_protocol(int id, void (*apply_cb)(void))
663 protocol_t *protocol;
666 * Have we yet created the "Protocols" subtree?
668 if (protocols_module == NULL) {
670 * No. Register Protocols subtree as well as any preferences
671 * for non-dissector modules.
673 pre_init_prefs();
674 prefs_register_modules();
676 protocol = find_protocol_by_id(id);
677 if (protocol == NULL)
678 ws_error("Protocol preferences being registered with an invalid protocol ID");
679 return prefs_register_module(protocols_module,
680 proto_get_protocol_filter_name(id),
681 proto_get_protocol_short_name(protocol),
682 proto_get_protocol_name(id), NULL, apply_cb, true);
685 void
686 prefs_deregister_protocol (int id)
688 protocol_t *protocol = find_protocol_by_id(id);
689 if (protocol == NULL)
690 ws_error("Protocol preferences being de-registered with an invalid protocol ID");
691 prefs_deregister_module (protocols_module,
692 proto_get_protocol_filter_name(id),
693 proto_get_protocol_short_name(protocol));
696 module_t *
697 prefs_register_protocol_subtree(const char *subtree, int id, void (*apply_cb)(void))
699 protocol_t *protocol;
700 module_t *subtree_module;
701 module_t *new_module;
702 char *sep = NULL, *ptr = NULL, *orig = NULL;
705 * Have we yet created the "Protocols" subtree?
706 * XXX - can we just do this by registering Protocols/{subtree}?
707 * If not, why not?
709 if (protocols_module == NULL) {
711 * No. Register Protocols subtree as well as any preferences
712 * for non-dissector modules.
714 pre_init_prefs();
715 prefs_register_modules();
718 subtree_module = protocols_module;
720 if (subtree) {
721 /* take a copy of the buffer, orig keeps a base pointer while ptr
722 * walks through the string */
723 orig = ptr = g_strdup(subtree);
725 while (ptr && *ptr) {
727 if ((sep = strchr(ptr, '/')))
728 *sep++ = '\0';
730 if (!(new_module = find_subtree(subtree_module, ptr))) {
732 * There's no such module; create it, with the description
733 * being the name (if it's later registered explicitly
734 * with a description, that will override it).
736 ptr = wmem_strdup(wmem_epan_scope(), ptr);
737 new_module = prefs_register_subtree(subtree_module, ptr, ptr, NULL);
740 subtree_module = new_module;
741 ptr = sep;
745 g_free(orig);
748 protocol = find_protocol_by_id(id);
749 if (protocol == NULL)
750 ws_error("Protocol subtree being registered with an invalid protocol ID");
751 return prefs_register_module(subtree_module,
752 proto_get_protocol_filter_name(id),
753 proto_get_protocol_short_name(protocol),
754 proto_get_protocol_name(id), NULL, apply_cb, true);
759 * Register that a protocol used to have preferences but no longer does,
760 * by creating an "obsolete" module for it.
762 module_t *
763 prefs_register_protocol_obsolete(int id)
765 module_t *module;
766 protocol_t *protocol;
769 * Have we yet created the "Protocols" subtree?
771 if (protocols_module == NULL) {
773 * No. Register Protocols subtree as well as any preferences
774 * for non-dissector modules.
776 pre_init_prefs();
777 prefs_register_modules();
779 protocol = find_protocol_by_id(id);
780 if (protocol == NULL)
781 ws_error("Protocol being registered with an invalid protocol ID");
782 module = prefs_register_module(protocols_module,
783 proto_get_protocol_filter_name(id),
784 proto_get_protocol_short_name(protocol),
785 proto_get_protocol_name(id), NULL, NULL, true);
786 module->obsolete = true;
787 return module;
791 * Register that a statistical tap has preferences.
793 * "name" is a name for the tap to use on the command line with "-o"
794 * and in preference files.
796 * "title" is a short human-readable name for the tap.
798 * "description" is a longer human-readable description of the tap.
800 module_t *stats_module;
802 module_t *
803 prefs_register_stat(const char *name, const char *title,
804 const char *description, void (*apply_cb)(void))
807 * Have we yet created the "Statistics" subtree?
809 if (stats_module == NULL) {
811 * No. Register Statistics subtree as well as any preferences
812 * for non-dissector modules.
814 pre_init_prefs();
815 prefs_register_modules();
818 return prefs_register_module(stats_module, name, title, description, NULL,
819 apply_cb, true);
823 * Register that a codec has preferences.
825 * "name" is a name for the codec to use on the command line with "-o"
826 * and in preference files.
828 * "title" is a short human-readable name for the codec.
830 * "description" is a longer human-readable description of the codec.
832 module_t *codecs_module;
834 module_t *
835 prefs_register_codec(const char *name, const char *title,
836 const char *description, void (*apply_cb)(void))
839 * Have we yet created the "Codecs" subtree?
841 if (codecs_module == NULL) {
843 * No. Register Codecs subtree as well as any preferences
844 * for non-dissector modules.
846 pre_init_prefs();
847 prefs_register_modules();
850 return prefs_register_module(codecs_module, name, title, description, NULL,
851 apply_cb, true);
854 module_t *
855 prefs_find_module(const char *name)
857 return (module_t *)wmem_tree_lookup_string(prefs_modules, name, WMEM_TREE_STRING_NOCASE);
860 static module_t *
861 find_subtree(module_t *parent, const char *name)
863 return (module_t *)wmem_tree_lookup_string(parent ? parent->submodules : prefs_top_level_modules, name, WMEM_TREE_STRING_NOCASE);
867 * Call a callback function, with a specified argument, for each module
868 * in a list of modules. If the list is NULL, searches the top-level
869 * list in the display tree of modules. If any callback returns a
870 * non-zero value, we stop and return that value, otherwise we
871 * return 0.
873 * Normally "obsolete" modules are ignored; their sole purpose is to allow old
874 * preferences for dissectors that no longer have preferences to be
875 * silently ignored in preference files. Does not ignore subtrees,
876 * as this can be used when walking the display tree of modules.
879 typedef struct {
880 module_cb callback;
881 void *user_data;
882 unsigned ret;
883 bool skip_obsolete;
884 } call_foreach_t;
886 static bool
887 call_foreach_cb(const void *key _U_, void *value, void *data)
889 module_t *module = (module_t*)value;
890 call_foreach_t *call_data = (call_foreach_t*)data;
892 if (!call_data->skip_obsolete || !module->obsolete)
893 call_data->ret = (*call_data->callback)(module, call_data->user_data);
895 return (call_data->ret != 0);
898 static unsigned
899 prefs_module_list_foreach(wmem_tree_t *module_list, module_cb callback,
900 void *user_data, bool skip_obsolete)
902 call_foreach_t call_data;
904 if (module_list == NULL)
905 module_list = prefs_top_level_modules;
907 call_data.callback = callback;
908 call_data.user_data = user_data;
909 call_data.ret = 0;
910 call_data.skip_obsolete = skip_obsolete;
911 wmem_tree_foreach(module_list, call_foreach_cb, &call_data);
912 return call_data.ret;
916 * Returns true if module has any submodules
918 bool
919 prefs_module_has_submodules(module_t *module)
921 if (module->submodules == NULL) {
922 return false;
925 if (wmem_tree_is_empty(module->submodules)) {
926 return false;
929 return true;
933 * Call a callback function, with a specified argument, for each module
934 * in the list of all modules. (This list does not include subtrees.)
936 * Ignores "obsolete" modules; their sole purpose is to allow old
937 * preferences for dissectors that no longer have preferences to be
938 * silently ignored in preference files.
940 unsigned
941 prefs_modules_foreach(module_cb callback, void *user_data)
943 return prefs_module_list_foreach(prefs_modules, callback, user_data, true);
947 * Call a callback function, with a specified argument, for each submodule
948 * of specified modules. If the module is NULL, goes through the top-level
949 * list in the display tree of modules.
951 * Ignores "obsolete" modules; their sole purpose is to allow old
952 * preferences for dissectors that no longer have preferences to be
953 * silently ignored in preference files. Does not ignore subtrees,
954 * as this can be used when walking the display tree of modules.
956 unsigned
957 prefs_modules_foreach_submodules(module_t *module, module_cb callback,
958 void *user_data)
960 return prefs_module_list_foreach((module)?module->submodules:prefs_top_level_modules, callback, user_data, true);
963 static bool
964 call_apply_cb(const void *key _U_, void *value, void *data _U_)
966 module_t *module = (module_t *)value;
968 if (module->obsolete)
969 return false;
970 if (module->prefs_changed_flags) {
971 if (module->apply_cb != NULL)
972 (*module->apply_cb)();
973 module->prefs_changed_flags = 0;
975 if (module->submodules)
976 wmem_tree_foreach(module->submodules, call_apply_cb, NULL);
977 return false;
981 * Call the "apply" callback function for each module if any of its
982 * preferences have changed, and then clear the flag saying its
983 * preferences have changed, as the module has been notified of that
984 * fact.
986 void
987 prefs_apply_all(void)
989 wmem_tree_foreach(prefs_modules, call_apply_cb, NULL);
993 * Call the "apply" callback function for a specific module if any of
994 * its preferences have changed, and then clear the flag saying its
995 * preferences have changed, as the module has been notified of that
996 * fact.
998 void
999 prefs_apply(module_t *module)
1001 if (module && module->prefs_changed_flags)
1002 call_apply_cb(NULL, module, NULL);
1005 static module_t *
1006 prefs_find_module_alias(const char *name)
1008 module_alias_t *alias;
1010 alias = (module_alias_t *)wmem_tree_lookup_string(prefs_module_aliases, name, WMEM_TREE_STRING_NOCASE);
1011 if (alias == NULL)
1012 return NULL;
1013 return alias->module;
1017 * Register a preference in a module's list of preferences.
1018 * If it has a title, give it an ordinal number; otherwise, it's a
1019 * preference that won't show up in the UI, so it shouldn't get an
1020 * ordinal number (the ordinal should be the ordinal in the set of
1021 * *visible* preferences).
1023 static pref_t *
1024 register_preference(module_t *module, const char *name, const char *title,
1025 const char *description, int type)
1027 pref_t *preference;
1028 const char *p;
1029 const char *name_prefix = (module->name != NULL) ? module->name : module->parent->name;
1031 preference = g_new(pref_t,1);
1032 preference->name = name;
1033 preference->title = title;
1034 preference->description = description;
1035 preference->type = type;
1036 /* Default to module's preference effects */
1037 preference->effect_flags = module->effect_flags;
1039 if (title != NULL)
1040 preference->ordinal = module->numprefs;
1041 else
1042 preference->ordinal = -1; /* no ordinal for you */
1045 * Make sure that only lower-case ASCII letters, numbers,
1046 * underscores, and dots appear in the preference name.
1048 * Crash if there is, as that's an error in the code;
1049 * you can make the title and description nice strings
1050 * with capitalization, white space, punctuation, etc.,
1051 * but the name can be used on the command line,
1052 * and shouldn't require quoting, shifting, etc.
1054 for (p = name; *p != '\0'; p++)
1055 if (!(g_ascii_islower(*p) || g_ascii_isdigit(*p) || *p == '_' || *p == '.'))
1056 ws_error("Preference \"%s.%s\" contains invalid characters", module->name, name);
1059 * Make sure there's not already a preference with that
1060 * name. Crash if there is, as that's an error in the
1061 * code, and the code has to be fixed not to register
1062 * more than one preference with the same name.
1064 if (prefs_find_preference(module, name) != NULL)
1065 ws_error("Preference %s has already been registered", name);
1067 if ((!IS_PREF_OBSOLETE(type)) &&
1068 /* Don't compare if it's a subtree */
1069 (module->name != NULL)) {
1071 * Make sure the preference name doesn't begin with the
1072 * module name, as that's redundant and Just Silly.
1074 if (!((strncmp(name, module->name, strlen(module->name)) != 0) ||
1075 (((name[strlen(module->name)]) != '.') && ((name[strlen(module->name)]) != '_'))))
1076 ws_error("Preference %s begins with the module name", name);
1079 /* The title shows up in the preferences dialog. Make sure it's UI-friendly. */
1080 if (preference->title) {
1081 const char *cur_char;
1082 if (preference->type != PREF_STATIC_TEXT && g_utf8_strlen(preference->title, -1) > 80) { // Arbitrary.
1083 ws_error("Title for preference %s.%s is too long: %s", name_prefix, preference->name, preference->title);
1086 if (!g_utf8_validate(preference->title, -1, NULL)) {
1087 ws_error("Title for preference %s.%s isn't valid UTF-8.", name_prefix, preference->name);
1090 for (cur_char = preference->title; *cur_char; cur_char = g_utf8_next_char(cur_char)) {
1091 if (!g_unichar_isprint(g_utf8_get_char(cur_char))) {
1092 ws_error("Title for preference %s.%s isn't printable UTF-8.", name_prefix, preference->name);
1097 if (preference->description) {
1098 if (!g_utf8_validate(preference->description, -1, NULL)) {
1099 ws_error("Description for preference %s.%s isn't valid UTF-8.", name_prefix, preference->name);
1104 * We passed all of our checks. Add the preference.
1106 module->prefs = g_list_append(module->prefs, preference);
1107 if (title != NULL)
1108 module->numprefs++;
1110 return preference;
1114 * Find a preference in a module's list of preferences, given the module
1115 * and the preference's name.
1117 typedef struct {
1118 GList *list_entry;
1119 const char *name;
1120 module_t *submodule;
1121 } find_pref_arg_t;
1123 static int
1124 preference_match(const void *a, const void *b)
1126 const pref_t *pref = (const pref_t *)a;
1127 const char *name = (const char *)b;
1129 return strcmp(name, pref->name);
1132 static bool
1133 module_find_pref_cb(const void *key _U_, void *value, void *data)
1135 find_pref_arg_t* arg = (find_pref_arg_t*)data;
1136 GList *list_entry;
1137 module_t *module = (module_t *)value;
1139 if (module == NULL)
1140 return false;
1142 list_entry = g_list_find_custom(module->prefs, arg->name,
1143 preference_match);
1145 if (list_entry == NULL)
1146 return false;
1148 arg->list_entry = list_entry;
1149 arg->submodule = module;
1150 return true;
1153 /* Tries to find a preference, setting containing_module to the (sub)module
1154 * holding this preference. */
1155 static struct preference *
1156 prefs_find_preference_with_submodule(module_t *module, const char *name,
1157 module_t **containing_module)
1159 find_pref_arg_t arg;
1160 GList *list_entry;
1162 if (module == NULL)
1163 return NULL; /* invalid parameters */
1165 list_entry = g_list_find_custom(module->prefs, name,
1166 preference_match);
1167 arg.submodule = NULL;
1169 if (list_entry == NULL)
1171 arg.list_entry = NULL;
1172 if (module->submodules != NULL)
1174 arg.name = name;
1175 wmem_tree_foreach(module->submodules, module_find_pref_cb, &arg);
1178 list_entry = arg.list_entry;
1181 if (list_entry == NULL)
1182 return NULL; /* no such preference */
1184 if (containing_module)
1185 *containing_module = arg.submodule ? arg.submodule : module;
1187 return (struct preference *) list_entry->data;
1190 struct preference *
1191 prefs_find_preference(module_t *module, const char *name)
1193 return prefs_find_preference_with_submodule(module, name, NULL);
1197 * Returns true if the given protocol has registered preferences
1199 bool
1200 prefs_is_registered_protocol(const char *name)
1202 module_t *m = prefs_find_module(name);
1204 return (m != NULL && !m->obsolete);
1208 * Returns the module title of a registered protocol
1210 const char *
1211 prefs_get_title_by_name(const char *name)
1213 module_t *m = prefs_find_module(name);
1215 return (m != NULL && !m->obsolete) ? m->title : NULL;
1219 * Register a preference with an unsigned integral value.
1221 void
1222 prefs_register_uint_preference(module_t *module, const char *name,
1223 const char *title, const char *description,
1224 unsigned base, unsigned *var)
1226 pref_t *preference;
1228 preference = register_preference(module, name, title, description,
1229 PREF_UINT);
1230 preference->varp.uint = var;
1231 preference->default_val.uint = *var;
1232 ws_assert(base > 0 && base != 1 && base < 37);
1233 preference->info.base = base;
1237 * XXX Add a prefs_register_{uint16|port}_preference which sets max_value?
1242 * Register a "custom" preference with a unsigned integral value.
1243 * XXX - This should be temporary until we can find a better way
1244 * to do "custom" preferences
1246 static void
1247 prefs_register_uint_custom_preference(module_t *module, const char *name,
1248 const char *title, const char *description,
1249 struct pref_custom_cbs* custom_cbs, unsigned *var)
1251 pref_t *preference;
1253 preference = register_preference(module, name, title, description,
1254 PREF_CUSTOM);
1256 preference->custom_cbs = *custom_cbs;
1257 preference->varp.uint = var;
1258 preference->default_val.uint = *var;
1262 * Register a preference with an Boolean value.
1264 void
1265 prefs_register_bool_preference(module_t *module, const char *name,
1266 const char *title, const char *description,
1267 bool *var)
1269 pref_t *preference;
1271 preference = register_preference(module, name, title, description,
1272 PREF_BOOL);
1273 preference->varp.boolp = var;
1274 preference->default_val.boolval = *var;
1277 unsigned int prefs_set_bool_value(pref_t *pref, bool value, pref_source_t source)
1279 unsigned int changed = 0;
1281 switch (source)
1283 case pref_default:
1284 if (pref->default_val.boolval != value) {
1285 pref->default_val.boolval = value;
1286 changed = prefs_get_effect_flags(pref);
1288 break;
1289 case pref_stashed:
1290 if (pref->stashed_val.boolval != value) {
1291 pref->stashed_val.boolval = value;
1292 changed = prefs_get_effect_flags(pref);
1294 break;
1295 case pref_current:
1296 if (*pref->varp.boolp != value) {
1297 *pref->varp.boolp = value;
1298 changed = prefs_get_effect_flags(pref);
1300 break;
1301 default:
1302 ws_assert_not_reached();
1303 break;
1306 return changed;
1309 void prefs_invert_bool_value(pref_t *pref, pref_source_t source)
1311 switch (source)
1313 case pref_default:
1314 pref->default_val.boolval = !pref->default_val.boolval;
1315 break;
1316 case pref_stashed:
1317 pref->stashed_val.boolval = !pref->stashed_val.boolval;
1318 break;
1319 case pref_current:
1320 *pref->varp.boolp = !(*pref->varp.boolp);
1321 break;
1322 default:
1323 ws_assert_not_reached();
1324 break;
1328 bool prefs_get_bool_value(pref_t *pref, pref_source_t source)
1330 switch (source)
1332 case pref_default:
1333 return pref->default_val.boolval;
1334 case pref_stashed:
1335 return pref->stashed_val.boolval;
1336 case pref_current:
1337 return *pref->varp.boolp;
1338 default:
1339 ws_assert_not_reached();
1340 break;
1343 return false;
1347 * Register a preference with an enumerated value.
1350 * XXX Should we get rid of the radio_buttons parameter and make that
1351 * behavior automatic depending on the number of items?
1353 void
1354 prefs_register_enum_preference(module_t *module, const char *name,
1355 const char *title, const char *description,
1356 int *var, const enum_val_t *enumvals,
1357 bool radio_buttons)
1359 pref_t *preference;
1361 /* Validate that the "name one would use on the command line for the value"
1362 * doesn't require quoting, etc. It's all treated case-insensitively so we
1363 * don't care about upper vs lower case.
1365 for (size_t i = 0; enumvals[i].name != NULL; i++) {
1366 for (const char *p = enumvals[i].name; *p != '\0'; p++)
1367 if (!(g_ascii_isalnum(*p) || *p == '_' || *p == '.' || *p == '-'))
1368 ws_error("Preference \"%s.%s\" enum value name \"%s\" contains invalid characters",
1369 module->name, name, enumvals[i].name);
1373 preference = register_preference(module, name, title, description,
1374 PREF_ENUM);
1375 preference->varp.enump = var;
1376 preference->default_val.enumval = *var;
1377 preference->info.enum_info.enumvals = enumvals;
1378 preference->info.enum_info.radio_buttons = radio_buttons;
1381 unsigned int prefs_set_enum_value(pref_t *pref, int value, pref_source_t source)
1383 unsigned int changed = 0;
1385 switch (source)
1387 case pref_default:
1388 if (pref->default_val.enumval != value) {
1389 pref->default_val.enumval = value;
1390 changed = prefs_get_effect_flags(pref);
1392 break;
1393 case pref_stashed:
1394 if (pref->stashed_val.enumval != value) {
1395 pref->stashed_val.enumval = value;
1396 changed = prefs_get_effect_flags(pref);
1398 break;
1399 case pref_current:
1400 if (*pref->varp.enump != value) {
1401 *pref->varp.enump = value;
1402 changed = prefs_get_effect_flags(pref);
1404 break;
1405 default:
1406 ws_assert_not_reached();
1407 break;
1410 return changed;
1413 unsigned int prefs_set_enum_string_value(pref_t *pref, const char *value, pref_source_t source)
1415 int enum_val = find_val_for_string(value, pref->info.enum_info.enumvals, *pref->varp.enump);
1417 return prefs_set_enum_value(pref, enum_val, source);
1420 int prefs_get_enum_value(pref_t *pref, pref_source_t source)
1422 switch (source)
1424 case pref_default:
1425 return pref->default_val.enumval;
1426 case pref_stashed:
1427 return pref->stashed_val.enumval;
1428 case pref_current:
1429 return *pref->varp.enump;
1430 default:
1431 ws_assert_not_reached();
1432 break;
1435 return 0;
1438 const enum_val_t* prefs_get_enumvals(pref_t *pref)
1440 return pref->info.enum_info.enumvals;
1443 bool prefs_get_enum_radiobuttons(pref_t *pref)
1445 return pref->info.enum_info.radio_buttons;
1449 * For use by UI code that sets preferences.
1451 unsigned int
1452 prefs_set_custom_value(pref_t *pref, const char *value, pref_source_t source _U_)
1454 /* XXX - support pref source for custom preferences */
1455 unsigned int changed = 0;
1456 pref->custom_cbs.set_cb(pref, value, &changed);
1457 return changed;
1460 static void
1461 register_string_like_preference(module_t *module, const char *name,
1462 const char *title, const char *description,
1463 char **var, int type,
1464 struct pref_custom_cbs* custom_cbs,
1465 bool free_tmp)
1467 pref_t *pref;
1468 char *tmp;
1470 pref = register_preference(module, name, title, description, type);
1473 * String preference values should be non-null (as you can't
1474 * keep them null after using the preferences GUI, you can at best
1475 * have them be null strings) and freeable (as we free them
1476 * if we change them).
1478 * If the value is a null pointer, make it a copy of a null
1479 * string, otherwise make it a copy of the value.
1481 tmp = *var;
1482 if (*var == NULL) {
1483 *var = g_strdup("");
1484 } else {
1485 *var = g_strdup(*var);
1487 if (free_tmp) {
1488 g_free(tmp);
1490 pref->varp.string = var;
1491 pref->default_val.string = g_strdup(*var);
1492 pref->stashed_val.string = NULL;
1493 if (type == PREF_CUSTOM) {
1494 ws_assert(custom_cbs);
1495 pref->custom_cbs = *custom_cbs;
1500 * Assign to a string preference.
1502 static void
1503 pref_set_string_like_pref_value(pref_t *pref, const char *value)
1505 DIAG_OFF(cast-qual)
1506 g_free((void *)*pref->varp.string);
1507 DIAG_ON(cast-qual)
1508 *pref->varp.string = g_strdup(value);
1512 * For use by UI code that sets preferences.
1514 unsigned int
1515 prefs_set_string_value(pref_t *pref, const char* value, pref_source_t source)
1517 unsigned int changed = 0;
1519 switch (source)
1521 case pref_default:
1522 if (*pref->default_val.string) {
1523 if (strcmp(pref->default_val.string, value) != 0) {
1524 changed = prefs_get_effect_flags(pref);
1525 g_free(pref->default_val.string);
1526 pref->default_val.string = g_strdup(value);
1528 } else if (value) {
1529 pref->default_val.string = g_strdup(value);
1531 break;
1532 case pref_stashed:
1533 if (pref->stashed_val.string) {
1534 if (strcmp(pref->stashed_val.string, value) != 0) {
1535 changed = prefs_get_effect_flags(pref);
1536 g_free(pref->stashed_val.string);
1537 pref->stashed_val.string = g_strdup(value);
1539 } else if (value) {
1540 pref->stashed_val.string = g_strdup(value);
1542 break;
1543 case pref_current:
1544 if (*pref->varp.string) {
1545 if (strcmp(*pref->varp.string, value) != 0) {
1546 changed = prefs_get_effect_flags(pref);
1547 pref_set_string_like_pref_value(pref, value);
1549 } else if (value) {
1550 pref_set_string_like_pref_value(pref, value);
1552 break;
1553 default:
1554 ws_assert_not_reached();
1555 break;
1558 return changed;
1561 char* prefs_get_string_value(pref_t *pref, pref_source_t source)
1563 switch (source)
1565 case pref_default:
1566 return pref->default_val.string;
1567 case pref_stashed:
1568 return pref->stashed_val.string;
1569 case pref_current:
1570 return *pref->varp.string;
1571 default:
1572 ws_assert_not_reached();
1573 break;
1576 return NULL;
1580 * Reset the value of a string-like preference.
1582 static void
1583 reset_string_like_preference(pref_t *pref)
1585 g_free(*pref->varp.string);
1586 *pref->varp.string = g_strdup(pref->default_val.string);
1590 * Register a preference with a character-string value.
1592 void
1593 prefs_register_string_preference(module_t *module, const char *name,
1594 const char *title, const char *description,
1595 const char **var)
1597 DIAG_OFF(cast-qual)
1598 register_string_like_preference(module, name, title, description,
1599 (char **)var, PREF_STRING, NULL, false);
1600 DIAG_ON(cast-qual)
1604 * Register a preference with a file name (string) value.
1606 void
1607 prefs_register_filename_preference(module_t *module, const char *name,
1608 const char *title, const char *description,
1609 const char **var, bool for_writing)
1611 DIAG_OFF(cast-qual)
1612 register_string_like_preference(module, name, title, description, (char **)var,
1613 for_writing ? PREF_SAVE_FILENAME : PREF_OPEN_FILENAME, NULL, false);
1614 DIAG_ON(cast-qual)
1618 * Register a preference with a directory name (string) value.
1620 void
1621 prefs_register_directory_preference(module_t *module, const char *name,
1622 const char *title, const char *description,
1623 const char **var)
1625 DIAG_OFF(cast-qual)
1626 register_string_like_preference(module, name, title, description,
1627 (char **)var, PREF_DIRNAME, NULL, false);
1628 DIAG_ON(cast-qual)
1631 /* Refactoring to handle both PREF_RANGE and PREF_DECODE_AS_RANGE */
1632 static pref_t*
1633 prefs_register_range_preference_common(module_t *module, const char *name,
1634 const char *title, const char *description,
1635 range_t **var, uint32_t max_value, int type)
1637 pref_t *preference;
1639 preference = register_preference(module, name, title, description, type);
1640 preference->info.max_value = max_value;
1643 * Range preference values should be non-null (as you can't
1644 * keep them null after using the preferences GUI, you can at best
1645 * have them be empty ranges) and freeable (as we free them
1646 * if we change them).
1648 * If the value is a null pointer, make it an empty range.
1650 if (*var == NULL)
1651 *var = range_empty(wmem_epan_scope());
1652 preference->varp.range = var;
1653 preference->default_val.range = range_copy(wmem_epan_scope(), *var);
1654 preference->stashed_val.range = NULL;
1656 return preference;
1660 * Register a preference with a ranged value.
1662 void
1663 prefs_register_range_preference(module_t *module, const char *name,
1664 const char *title, const char *description,
1665 range_t **var, uint32_t max_value)
1667 prefs_register_range_preference_common(module, name, title,
1668 description, var, max_value, PREF_RANGE);
1671 bool
1672 prefs_set_range_value_work(pref_t *pref, const char *value,
1673 bool return_range_errors, unsigned int *changed_flags)
1675 range_t *newrange;
1677 if (range_convert_str_work(wmem_epan_scope(), &newrange, value, pref->info.max_value,
1678 return_range_errors) != CVT_NO_ERROR) {
1679 return false; /* number was bad */
1682 if (!ranges_are_equal(*pref->varp.range, newrange)) {
1683 *changed_flags |= prefs_get_effect_flags(pref);
1684 wmem_free(wmem_epan_scope(), *pref->varp.range);
1685 *pref->varp.range = newrange;
1686 } else {
1687 wmem_free(wmem_epan_scope(), newrange);
1689 return true;
1693 * For use by UI code that sets preferences.
1695 unsigned int
1696 prefs_set_stashed_range_value(pref_t *pref, const char *value)
1698 range_t *newrange;
1700 if (range_convert_str_work(wmem_epan_scope(), &newrange, value, pref->info.max_value,
1701 true) != CVT_NO_ERROR) {
1702 return 0; /* number was bad */
1705 if (!ranges_are_equal(pref->stashed_val.range, newrange)) {
1706 wmem_free(wmem_epan_scope(), pref->stashed_val.range);
1707 pref->stashed_val.range = newrange;
1708 } else {
1709 wmem_free(wmem_epan_scope(), newrange);
1711 return prefs_get_effect_flags(pref);
1715 bool prefs_add_list_value(pref_t *pref, void* value, pref_source_t source)
1717 switch (source)
1719 case pref_default:
1720 pref->default_val.list = g_list_prepend(pref->default_val.list, value);
1721 break;
1722 case pref_stashed:
1723 pref->stashed_val.list = g_list_prepend(pref->stashed_val.list, value);
1724 break;
1725 case pref_current:
1726 *pref->varp.list = g_list_prepend(*pref->varp.list, value);
1727 break;
1728 default:
1729 ws_assert_not_reached();
1730 break;
1733 return true;
1736 GList* prefs_get_list_value(pref_t *pref, pref_source_t source)
1738 switch (source)
1740 case pref_default:
1741 return pref->default_val.list;
1742 case pref_stashed:
1743 return pref->stashed_val.list;
1744 case pref_current:
1745 return *pref->varp.list;
1746 default:
1747 ws_assert_not_reached();
1748 break;
1751 return NULL;
1754 bool prefs_set_range_value(pref_t *pref, range_t *value, pref_source_t source)
1756 bool changed = false;
1758 switch (source)
1760 case pref_default:
1761 if (!ranges_are_equal(pref->default_val.range, value)) {
1762 wmem_free(wmem_epan_scope(), pref->default_val.range);
1763 pref->default_val.range = range_copy(wmem_epan_scope(), value);
1764 changed = true;
1766 break;
1767 case pref_stashed:
1768 if (!ranges_are_equal(pref->stashed_val.range, value)) {
1769 wmem_free(wmem_epan_scope(), pref->stashed_val.range);
1770 pref->stashed_val.range = range_copy(wmem_epan_scope(), value);
1771 changed = true;
1773 break;
1774 case pref_current:
1775 if (!ranges_are_equal(*pref->varp.range, value)) {
1776 wmem_free(wmem_epan_scope(), *pref->varp.range);
1777 *pref->varp.range = range_copy(wmem_epan_scope(), value);
1778 changed = true;
1780 break;
1781 default:
1782 ws_assert_not_reached();
1783 break;
1786 return changed;
1789 range_t* prefs_get_range_value_real(pref_t *pref, pref_source_t source)
1791 switch (source)
1793 case pref_default:
1794 return pref->default_val.range;
1795 case pref_stashed:
1796 return pref->stashed_val.range;
1797 case pref_current:
1798 return *pref->varp.range;
1799 default:
1800 ws_assert_not_reached();
1801 break;
1804 return NULL;
1807 range_t* prefs_get_range_value(const char *module_name, const char* pref_name)
1809 pref_t *pref = prefs_find_preference(prefs_find_module(module_name), pref_name);
1810 if (pref == NULL) {
1811 return NULL;
1813 return prefs_get_range_value_real(pref, pref_current);
1816 void
1817 prefs_range_add_value(pref_t *pref, uint32_t val)
1819 range_add_value(wmem_epan_scope(), pref->varp.range, val);
1822 void
1823 prefs_range_remove_value(pref_t *pref, uint32_t val)
1825 range_remove_value(wmem_epan_scope(), pref->varp.range, val);
1829 * Register a static text 'preference'. It can be used to add explanatory
1830 * text inline with other preferences in the GUI.
1831 * Note: Static preferences are not saved to the preferences file.
1833 void
1834 prefs_register_static_text_preference(module_t *module, const char *name,
1835 const char *title,
1836 const char *description)
1838 register_preference(module, name, title, description, PREF_STATIC_TEXT);
1842 * Register a uat 'preference'. It adds a button that opens the uat's window in the
1843 * preferences tab of the module.
1845 extern void
1846 prefs_register_uat_preference(module_t *module, const char *name,
1847 const char *title, const char *description,
1848 uat_t* uat)
1851 pref_t* preference = register_preference(module, name, title, description, PREF_UAT);
1853 preference->varp.uat = uat;
1857 * Register a uat 'preference' for QT only. It adds a button that opens the uat's window in the
1858 * preferences tab of the module.
1860 extern void
1861 prefs_register_uat_preference_qt(module_t *module, const char *name,
1862 const char *title, const char *description,
1863 uat_t* uat)
1866 pref_t* preference = register_preference(module, name, title, description, PREF_UAT);
1868 preference->varp.uat = uat;
1871 struct epan_uat* prefs_get_uat_value(pref_t *pref)
1873 return pref->varp.uat;
1877 * Register a color preference.
1879 void
1880 prefs_register_color_preference(module_t *module, const char *name,
1881 const char *title, const char *description,
1882 color_t *color)
1884 pref_t* preference = register_preference(module, name, title, description, PREF_COLOR);
1886 preference->varp.colorp = color;
1887 preference->default_val.color = *color;
1890 bool prefs_set_color_value(pref_t *pref, color_t value, pref_source_t source)
1892 bool changed = false;
1894 switch (source)
1896 case pref_default:
1897 if ((pref->default_val.color.red != value.red) ||
1898 (pref->default_val.color.green != value.green) ||
1899 (pref->default_val.color.blue != value.blue)) {
1900 changed = true;
1901 pref->default_val.color = value;
1903 break;
1904 case pref_stashed:
1905 if ((pref->stashed_val.color.red != value.red) ||
1906 (pref->stashed_val.color.green != value.green) ||
1907 (pref->stashed_val.color.blue != value.blue)) {
1908 changed = true;
1909 pref->stashed_val.color = value;
1911 break;
1912 case pref_current:
1913 if ((pref->varp.colorp->red != value.red) ||
1914 (pref->varp.colorp->green != value.green) ||
1915 (pref->varp.colorp->blue != value.blue)) {
1916 changed = true;
1917 *pref->varp.colorp = value;
1919 break;
1920 default:
1921 ws_assert_not_reached();
1922 break;
1925 return changed;
1928 color_t* prefs_get_color_value(pref_t *pref, pref_source_t source)
1930 switch (source)
1932 case pref_default:
1933 return &pref->default_val.color;
1934 case pref_stashed:
1935 return &pref->stashed_val.color;
1936 case pref_current:
1937 return pref->varp.colorp;
1938 default:
1939 ws_assert_not_reached();
1940 break;
1943 return NULL;
1947 * Register a "custom" preference with a list.
1948 * XXX - This should be temporary until we can find a better way
1949 * to do "custom" preferences
1951 typedef void (*pref_custom_list_init_cb) (pref_t* pref, GList** value);
1953 static void
1954 prefs_register_list_custom_preference(module_t *module, const char *name,
1955 const char *title, const char *description,
1956 struct pref_custom_cbs* custom_cbs,
1957 pref_custom_list_init_cb init_cb,
1958 GList** list)
1960 pref_t* preference = register_preference(module, name, title, description, PREF_CUSTOM);
1962 preference->custom_cbs = *custom_cbs;
1963 init_cb(preference, list);
1967 * Register a custom preference.
1969 void
1970 prefs_register_custom_preference(module_t *module, const char *name,
1971 const char *title, const char *description,
1972 struct pref_custom_cbs* custom_cbs,
1973 void **custom_data _U_)
1975 pref_t* preference = register_preference(module, name, title, description, PREF_CUSTOM);
1977 preference->custom_cbs = *custom_cbs;
1978 /* XXX - wait until we can handle void** pointers
1979 preference->custom_cbs.init_cb(preference, custom_data);
1984 * Register a dedicated TCP preference for SEQ analysis overriding.
1985 * This is similar to the data structure from enum preference, except
1986 * that when a preference dialog is used, the stashed value is the list
1987 * of frame data pointers whose sequence analysis override will be set
1988 * to the current value if the dialog is accepted.
1990 * We don't need to read or write the value from the preferences file
1991 * (or command line), because the override is reset to the default (0)
1992 * for each frame when a new capture file is loaded.
1994 void
1995 prefs_register_custom_preference_TCP_Analysis(module_t *module, const char *name,
1996 const char *title, const char *description,
1997 int *var, const enum_val_t *enumvals,
1998 bool radio_buttons)
2000 pref_t *preference;
2002 preference = register_preference(module, name, title, description,
2003 PREF_PROTO_TCP_SNDAMB_ENUM);
2004 preference->varp.enump = var;
2005 preference->default_val.enumval = *var;
2006 preference->stashed_val.list = NULL;
2007 preference->info.enum_info.enumvals = enumvals;
2008 preference->info.enum_info.radio_buttons = radio_buttons;
2012 * Register a (internal) "Decode As" preference with a ranged value.
2014 void prefs_register_decode_as_range_preference(module_t *module, const char *name,
2015 const char *title, const char *description, range_t **var,
2016 uint32_t max_value, const char *dissector_table, const char *dissector_description)
2018 pref_t *preference;
2020 preference = prefs_register_range_preference_common(module, name, title,
2021 description, var, max_value, PREF_DECODE_AS_RANGE);
2022 preference->dissector_desc = dissector_description;
2023 preference->dissector_table = dissector_table;
2027 * Register a preference with password value.
2029 void
2030 prefs_register_password_preference(module_t *module, const char *name,
2031 const char *title, const char *description,
2032 const char **var)
2034 DIAG_OFF(cast-qual)
2035 register_string_like_preference(module, name, title, description,
2036 (char **)var, PREF_PASSWORD, NULL, false);
2037 DIAG_ON(cast-qual)
2041 * Register a preference with a dissector name.
2043 void
2044 prefs_register_dissector_preference(module_t *module, const char *name,
2045 const char *title, const char *description,
2046 const char **var)
2048 DIAG_OFF(cast-qual)
2049 register_string_like_preference(module, name, title, description,
2050 (char **)var, PREF_DISSECTOR, NULL, false);
2051 DIAG_ON(cast-qual)
2054 bool prefs_add_decode_as_value(pref_t *pref, unsigned value, bool replace)
2056 switch(pref->type)
2058 case PREF_DECODE_AS_RANGE:
2059 if (replace)
2061 /* If range has single value, replace it */
2062 if (((*pref->varp.range)->nranges == 1) &&
2063 ((*pref->varp.range)->ranges[0].low == (*pref->varp.range)->ranges[0].high)) {
2064 wmem_free(wmem_epan_scope(), *pref->varp.range);
2065 *pref->varp.range = range_empty(wmem_epan_scope());
2069 prefs_range_add_value(pref, value);
2070 break;
2071 default:
2072 /* XXX - Worth asserting over? */
2073 break;
2076 return true;
2079 bool prefs_remove_decode_as_value(pref_t *pref, unsigned value, bool set_default _U_)
2081 switch(pref->type)
2083 case PREF_DECODE_AS_RANGE:
2084 /* XXX - We could set to the default if the value is the only one
2085 * in the range.
2087 prefs_range_remove_value(pref, value);
2088 break;
2089 default:
2090 break;
2093 return true;
2097 * Register a preference that used to be supported but no longer is.
2099 void
2100 prefs_register_obsolete_preference(module_t *module, const char *name)
2102 register_preference(module, name, NULL, NULL, PREF_OBSOLETE);
2105 void
2106 prefs_set_preference_effect_fields(module_t *module, const char *name)
2108 pref_t * pref = prefs_find_preference(module, name);
2109 if (pref) {
2110 prefs_set_effect_flags(pref, prefs_get_effect_flags(pref) | PREF_EFFECT_FIELDS);
2115 * Check to see if a preference is obsolete.
2117 extern bool
2118 prefs_get_preference_obsolete(pref_t *pref)
2120 if (pref)
2121 return (IS_PREF_OBSOLETE(pref->type) ? true : false);
2123 return true;
2127 * Make a preference obsolete.
2129 extern prefs_set_pref_e
2130 prefs_set_preference_obsolete(pref_t *pref)
2132 if (pref) {
2133 SET_PREF_OBSOLETE(pref->type);
2134 return PREFS_SET_OK;
2136 return PREFS_SET_NO_SUCH_PREF;
2139 unsigned
2140 pref_stash(pref_t *pref, void *unused _U_)
2142 switch (pref->type) {
2144 case PREF_UINT:
2145 pref->stashed_val.uint = *pref->varp.uint;
2146 break;
2148 case PREF_BOOL:
2149 pref->stashed_val.boolval = *pref->varp.boolp;
2150 break;
2152 case PREF_ENUM:
2153 pref->stashed_val.enumval = *pref->varp.enump;
2154 break;
2156 case PREF_STRING:
2157 case PREF_SAVE_FILENAME:
2158 case PREF_OPEN_FILENAME:
2159 case PREF_DIRNAME:
2160 case PREF_PASSWORD:
2161 case PREF_DISSECTOR:
2162 g_free(pref->stashed_val.string);
2163 pref->stashed_val.string = g_strdup(*pref->varp.string);
2164 break;
2166 case PREF_DECODE_AS_RANGE:
2167 case PREF_RANGE:
2168 wmem_free(wmem_epan_scope(), pref->stashed_val.range);
2169 pref->stashed_val.range = range_copy(wmem_epan_scope(), *pref->varp.range);
2170 break;
2172 case PREF_COLOR:
2173 pref->stashed_val.color = *pref->varp.colorp;
2174 break;
2176 case PREF_STATIC_TEXT:
2177 case PREF_UAT:
2178 case PREF_CUSTOM:
2179 case PREF_PROTO_TCP_SNDAMB_ENUM:
2180 break;
2182 case PREF_OBSOLETE:
2183 ws_assert_not_reached();
2184 break;
2186 return 0;
2189 unsigned
2190 pref_unstash(pref_t *pref, void *unstash_data_p)
2192 pref_unstash_data_t *unstash_data = (pref_unstash_data_t *)unstash_data_p;
2193 dissector_table_t sub_dissectors = NULL;
2194 dissector_handle_t handle = NULL;
2196 /* Revert the preference to its saved value. */
2197 switch (pref->type) {
2199 case PREF_UINT:
2200 if (*pref->varp.uint != pref->stashed_val.uint) {
2201 unstash_data->module->prefs_changed_flags |= prefs_get_effect_flags(pref);
2202 *pref->varp.uint = pref->stashed_val.uint;
2204 break;
2206 case PREF_BOOL:
2207 if (*pref->varp.boolp != pref->stashed_val.boolval) {
2208 unstash_data->module->prefs_changed_flags |= prefs_get_effect_flags(pref);
2209 *pref->varp.boolp = pref->stashed_val.boolval;
2211 break;
2213 case PREF_ENUM:
2214 if (*pref->varp.enump != pref->stashed_val.enumval) {
2215 unstash_data->module->prefs_changed_flags |= prefs_get_effect_flags(pref);
2216 *pref->varp.enump = pref->stashed_val.enumval;
2218 break;
2220 case PREF_PROTO_TCP_SNDAMB_ENUM:
2222 /* The preference dialogs are modal so the frame_data pointers should
2223 * still be valid; otherwise we could store the frame numbers to
2224 * change.
2226 frame_data *fdata;
2227 for (GList* elem = pref->stashed_val.list; elem != NULL; elem = elem->next) {
2228 fdata = (frame_data*)elem->data;
2229 if (fdata->tcp_snd_manual_analysis != *pref->varp.enump) {
2230 unstash_data->module->prefs_changed_flags |= prefs_get_effect_flags(pref);
2231 fdata->tcp_snd_manual_analysis = *pref->varp.enump;
2234 break;
2236 case PREF_STRING:
2237 case PREF_SAVE_FILENAME:
2238 case PREF_OPEN_FILENAME:
2239 case PREF_DIRNAME:
2240 case PREF_PASSWORD:
2241 case PREF_DISSECTOR:
2242 if (strcmp(*pref->varp.string, pref->stashed_val.string) != 0) {
2243 unstash_data->module->prefs_changed_flags |= prefs_get_effect_flags(pref);
2244 g_free(*pref->varp.string);
2245 *pref->varp.string = g_strdup(pref->stashed_val.string);
2247 break;
2249 case PREF_DECODE_AS_RANGE:
2251 const char* table_name = prefs_get_dissector_table(pref);
2252 if (!ranges_are_equal(*pref->varp.range, pref->stashed_val.range)) {
2253 uint32_t i, j;
2254 unstash_data->module->prefs_changed_flags |= prefs_get_effect_flags(pref);
2256 if (unstash_data->handle_decode_as) {
2257 sub_dissectors = find_dissector_table(table_name);
2258 if (sub_dissectors != NULL) {
2259 const char *handle_desc = prefs_get_dissector_description(pref);
2260 // It should perhaps be possible to get this via dissector name.
2261 handle = dissector_table_get_dissector_handle(sub_dissectors, handle_desc);
2262 if (handle != NULL) {
2263 /* Set the current handle to NULL for all the old values
2264 * in the dissector table. If there isn't an initial
2265 * handle, this actually deletes the entry. (If there
2266 * is an initial entry, keep it around so that the
2267 * user can see the original value.)
2269 * XXX - If there's an initial handle which is not this,
2270 * reset it instead? At least this leaves the initial
2271 * handle visible in the Decode As table.
2273 for (i = 0; i < (*pref->varp.range)->nranges; i++) {
2274 for (j = (*pref->varp.range)->ranges[i].low; j < (*pref->varp.range)->ranges[i].high; j++) {
2275 dissector_change_uint(table_name, j, NULL);
2276 decode_build_reset_list(table_name, dissector_table_get_type(sub_dissectors), GUINT_TO_POINTER(j), NULL, NULL);
2279 dissector_change_uint(table_name, (*pref->varp.range)->ranges[i].high, NULL);
2280 decode_build_reset_list(table_name, dissector_table_get_type(sub_dissectors), GUINT_TO_POINTER((*pref->varp.range)->ranges[i].high), NULL, NULL);
2286 wmem_free(wmem_epan_scope(), *pref->varp.range);
2287 *pref->varp.range = range_copy(wmem_epan_scope(), pref->stashed_val.range);
2289 if (unstash_data->handle_decode_as) {
2290 if ((sub_dissectors != NULL) && (handle != NULL)) {
2292 /* Add new values to the dissector table */
2293 for (i = 0; i < (*pref->varp.range)->nranges; i++) {
2295 for (j = (*pref->varp.range)->ranges[i].low; j < (*pref->varp.range)->ranges[i].high; j++) {
2296 dissector_change_uint(table_name, j, handle);
2297 decode_build_reset_list(table_name, dissector_table_get_type(sub_dissectors), GUINT_TO_POINTER(j), NULL, NULL);
2300 dissector_change_uint(table_name, (*pref->varp.range)->ranges[i].high, handle);
2301 decode_build_reset_list(table_name, dissector_table_get_type(sub_dissectors), GUINT_TO_POINTER((*pref->varp.range)->ranges[i].high), NULL, NULL);
2306 break;
2308 case PREF_RANGE:
2309 if (!ranges_are_equal(*pref->varp.range, pref->stashed_val.range)) {
2310 unstash_data->module->prefs_changed_flags |= prefs_get_effect_flags(pref);
2311 wmem_free(wmem_epan_scope(), *pref->varp.range);
2312 *pref->varp.range = range_copy(wmem_epan_scope(), pref->stashed_val.range);
2314 break;
2316 case PREF_COLOR:
2317 if ((pref->varp.colorp->blue != pref->stashed_val.color.blue) ||
2318 (pref->varp.colorp->red != pref->stashed_val.color.red) ||
2319 (pref->varp.colorp->green != pref->stashed_val.color.green)) {
2320 unstash_data->module->prefs_changed_flags |= prefs_get_effect_flags(pref);
2321 *pref->varp.colorp = pref->stashed_val.color;
2323 break;
2325 case PREF_STATIC_TEXT:
2326 case PREF_UAT:
2327 case PREF_CUSTOM:
2328 break;
2330 case PREF_OBSOLETE:
2331 ws_assert_not_reached();
2332 break;
2334 return 0;
2337 void
2338 reset_stashed_pref(pref_t *pref) {
2339 switch (pref->type) {
2341 case PREF_UINT:
2342 pref->stashed_val.uint = pref->default_val.uint;
2343 break;
2345 case PREF_BOOL:
2346 pref->stashed_val.boolval = pref->default_val.boolval;
2347 break;
2349 case PREF_ENUM:
2350 pref->stashed_val.enumval = pref->default_val.enumval;
2351 break;
2353 case PREF_STRING:
2354 case PREF_SAVE_FILENAME:
2355 case PREF_OPEN_FILENAME:
2356 case PREF_DIRNAME:
2357 case PREF_PASSWORD:
2358 case PREF_DISSECTOR:
2359 g_free(pref->stashed_val.string);
2360 pref->stashed_val.string = g_strdup(pref->default_val.string);
2361 break;
2363 case PREF_DECODE_AS_RANGE:
2364 case PREF_RANGE:
2365 wmem_free(wmem_epan_scope(), pref->stashed_val.range);
2366 pref->stashed_val.range = range_copy(wmem_epan_scope(), pref->default_val.range);
2367 break;
2369 case PREF_PROTO_TCP_SNDAMB_ENUM:
2370 if (pref->stashed_val.list != NULL) {
2371 g_list_free(pref->stashed_val.list);
2372 pref->stashed_val.list = NULL;
2374 break;
2376 case PREF_COLOR:
2377 memcpy(&pref->stashed_val.color, &pref->default_val.color, sizeof(color_t));
2378 break;
2380 case PREF_STATIC_TEXT:
2381 case PREF_UAT:
2382 case PREF_CUSTOM:
2383 break;
2385 case PREF_OBSOLETE:
2386 ws_assert_not_reached();
2387 break;
2391 unsigned
2392 pref_clean_stash(pref_t *pref, void *unused _U_)
2394 switch (pref->type) {
2396 case PREF_UINT:
2397 break;
2399 case PREF_BOOL:
2400 break;
2402 case PREF_ENUM:
2403 break;
2405 case PREF_STRING:
2406 case PREF_SAVE_FILENAME:
2407 case PREF_OPEN_FILENAME:
2408 case PREF_DIRNAME:
2409 case PREF_PASSWORD:
2410 case PREF_DISSECTOR:
2411 if (pref->stashed_val.string != NULL) {
2412 g_free(pref->stashed_val.string);
2413 pref->stashed_val.string = NULL;
2415 break;
2417 case PREF_DECODE_AS_RANGE:
2418 case PREF_RANGE:
2419 if (pref->stashed_val.range != NULL) {
2420 wmem_free(wmem_epan_scope(), pref->stashed_val.range);
2421 pref->stashed_val.range = NULL;
2423 break;
2425 case PREF_STATIC_TEXT:
2426 case PREF_UAT:
2427 case PREF_COLOR:
2428 case PREF_CUSTOM:
2429 break;
2431 case PREF_PROTO_TCP_SNDAMB_ENUM:
2432 if (pref->stashed_val.list != NULL) {
2433 g_list_free(pref->stashed_val.list);
2434 pref->stashed_val.list = NULL;
2436 break;
2438 case PREF_OBSOLETE:
2439 ws_assert_not_reached();
2440 break;
2442 return 0;
2446 * Call a callback function, with a specified argument, for each preference
2447 * in a given module.
2449 * If any of the callbacks return a non-zero value, stop and return that
2450 * value, otherwise return 0.
2452 unsigned
2453 prefs_pref_foreach(module_t *module, pref_cb callback, void *user_data)
2455 GList *elem;
2456 pref_t *pref;
2457 unsigned ret;
2459 for (elem = g_list_first(module->prefs); elem != NULL; elem = g_list_next(elem)) {
2460 pref = (pref_t *)elem->data;
2461 if (IS_PREF_OBSOLETE(pref->type)) {
2463 * This preference is no longer supported; it's
2464 * not a real preference, so we don't call the
2465 * callback for it (i.e., we treat it as if it
2466 * weren't found in the list of preferences,
2467 * and we weren't called in the first place).
2469 continue;
2472 ret = (*callback)(pref, user_data);
2473 if (ret != 0)
2474 return ret;
2476 return 0;
2479 static const enum_val_t st_sort_col_vals[] = {
2480 { "name", "Node name (topic/item)", ST_SORT_COL_NAME },
2481 { "count", "Item count", ST_SORT_COL_COUNT },
2482 { "average", "Average value of the node", ST_SORT_COL_AVG },
2483 { "min", "Minimum value of the node", ST_SORT_COL_MIN },
2484 { "max", "Maximum value of the node", ST_SORT_COL_MAX },
2485 { "burst", "Burst rate of the node", ST_SORT_COL_BURSTRATE },
2486 { NULL, NULL, 0 }
2489 static void
2490 stats_callback(void)
2492 /* Test for a sane tap update interval */
2493 if (prefs.tap_update_interval < 100 || prefs.tap_update_interval > 10000)
2494 prefs.tap_update_interval = TAP_UPDATE_DEFAULT_INTERVAL;
2496 /* burst resolution can't be less than 1 (ms) */
2497 if (prefs.st_burst_resolution < 1) {
2498 prefs.st_burst_resolution = 1;
2500 else if (prefs.st_burst_resolution > ST_MAX_BURSTRES) {
2501 prefs.st_burst_resolution = ST_MAX_BURSTRES;
2503 /* make sure burst window value makes sense */
2504 if (prefs.st_burst_windowlen < prefs.st_burst_resolution) {
2505 prefs.st_burst_windowlen = prefs.st_burst_resolution;
2507 /* round burst window down to multiple of resolution */
2508 prefs.st_burst_windowlen -= prefs.st_burst_windowlen%prefs.st_burst_resolution;
2509 if ((prefs.st_burst_windowlen/prefs.st_burst_resolution) > ST_MAX_BURSTBUCKETS) {
2510 prefs.st_burst_windowlen = prefs.st_burst_resolution*ST_MAX_BURSTBUCKETS;
2514 static void
2515 gui_callback(void)
2517 /* Ensure there is at least one file count */
2518 if (prefs.gui_recent_files_count_max == 0)
2519 prefs.gui_recent_files_count_max = 10;
2521 /* Ensure there is at least one display filter entry */
2522 if (prefs.gui_recent_df_entries_max == 0)
2523 prefs.gui_recent_df_entries_max = 10;
2525 /* number of decimal places should be between 2 and 10 */
2526 if (prefs.gui_decimal_places1 < 2) {
2527 prefs.gui_decimal_places1 = 2;
2528 } else if (prefs.gui_decimal_places1 > 10) {
2529 prefs.gui_decimal_places1 = 10;
2531 /* number of decimal places should be between 2 and 10 */
2532 if (prefs.gui_decimal_places2 < 2) {
2533 prefs.gui_decimal_places2 = 2;
2534 } else if (prefs.gui_decimal_places2 > 10) {
2535 prefs.gui_decimal_places2 = 10;
2537 /* number of decimal places should be between 2 and 10 */
2538 if (prefs.gui_decimal_places3 < 2) {
2539 prefs.gui_decimal_places3 = 2;
2540 } else if (prefs.gui_decimal_places3 > 10) {
2541 prefs.gui_decimal_places3 = 10;
2545 static void
2546 gui_layout_callback(void)
2548 if (prefs.gui_layout_type == layout_unused ||
2549 prefs.gui_layout_type >= layout_type_max) {
2550 /* XXX - report an error? It's not a syntax error - we'd need to
2551 add a way of reporting a *semantic* error. */
2552 prefs.gui_layout_type = layout_type_2;
2556 /******************************************************
2557 * All custom preference function callbacks
2558 ******************************************************/
2559 static void custom_pref_no_cb(pref_t* pref _U_) {}
2562 * Column preference functions
2564 #define PRS_COL_HIDDEN_FMT "column.hidden"
2565 #define PRS_COL_HIDDEN "column.hide"
2566 #define PRS_COL_FMT "column.format"
2567 #define PRS_COL_NUM "column.number"
2568 static module_t *gui_column_module;
2570 static prefs_set_pref_e
2571 column_hidden_set_cb(pref_t* pref, const char* value, unsigned int* changed_flags)
2573 GList *clp;
2574 fmt_data *cfmt;
2575 pref_t *format_pref;
2578 * Prefer the new preference to the old format-based preference if we've
2579 * read it. (We probably could just compare the string to NULL and "".)
2581 prefs.cols_hide_new = true;
2583 (*changed_flags) |= prefs_set_string_value(pref, value, pref_current);
2586 * Set the "visible" flag for the existing columns; we need to
2587 * do this if we set PRS_COL_HIDDEN but don't set PRS_COL_FMT
2588 * after setting it (which might be the case if, for example, we
2589 * set PRS_COL_HIDDEN on the command line).
2591 format_pref = prefs_find_preference(gui_column_module, PRS_COL_FMT);
2592 clp = (format_pref) ? *format_pref->varp.list : NULL;
2593 int cidx = 1;
2594 while (clp) {
2595 cfmt = (fmt_data *)clp->data;
2596 cfmt->visible = prefs_is_column_visible(*pref->varp.string, cidx);
2597 cidx++;
2598 clp = clp->next;
2601 return PREFS_SET_OK;
2604 static const char *
2605 column_hidden_type_name_cb(void)
2607 return "Packet list hidden columns";
2610 static char *
2611 column_hidden_type_description_cb(void)
2613 return g_strdup("List all column indices (1-indexed) to hide in the packet list.");
2616 static char *
2617 column_hidden_to_str_cb(pref_t* pref, bool default_val)
2619 GString *cols_hidden;
2620 GList *clp;
2621 fmt_data *cfmt;
2622 pref_t *format_pref;
2623 int cidx = 1;
2625 if (default_val)
2626 return g_strdup(pref->default_val.string);
2628 cols_hidden = g_string_new("");
2629 format_pref = prefs_find_preference(gui_column_module, PRS_COL_FMT);
2630 clp = (format_pref) ? *format_pref->varp.list : NULL;
2631 while (clp) {
2632 cfmt = (fmt_data *) clp->data;
2633 if (!cfmt->visible) {
2634 if (cols_hidden->len)
2635 g_string_append (cols_hidden, ",");
2636 g_string_append_printf (cols_hidden, "%i", cidx);
2638 clp = clp->next;
2639 cidx++;
2642 return g_string_free (cols_hidden, FALSE);
2645 static bool
2646 column_hidden_is_default_cb(pref_t* pref)
2648 char *cur_hidden_str = column_hidden_to_str_cb(pref, false);
2649 bool is_default = g_strcmp0(cur_hidden_str, pref->default_val.string) == 0;
2651 g_free(cur_hidden_str);
2652 return is_default;
2655 static prefs_set_pref_e
2656 column_hidden_fmt_set_cb(pref_t* pref, const char* value, unsigned int* changed_flags)
2658 GList *clp;
2659 fmt_data *cfmt;
2660 pref_t *format_pref;
2662 (*changed_flags) |= prefs_set_string_value(pref, value, pref_current);
2665 * Set the "visible" flag for the existing columns; we need to
2666 * do this if we set PRS_COL_HIDDEN_FMT but don't set PRS_COL_FMT
2667 * after setting it (which might be the case if, for example, we
2668 * set PRS_COL_HIDDEN_FMT on the command line; it shouldn't happen
2669 * when reading the configuration file because we write (both of)
2670 * the hidden column prefs before the column format prefs.)
2672 format_pref = prefs_find_preference(gui_column_module, PRS_COL_FMT);
2673 clp = (format_pref) ? *format_pref->varp.list : NULL;
2674 while (clp) {
2675 cfmt = (fmt_data *)clp->data;
2676 cfmt->visible = prefs_is_column_fmt_visible(*pref->varp.string, cfmt);
2677 clp = clp->next;
2680 return PREFS_SET_OK;
2683 static const char *
2684 column_hidden_fmt_type_name_cb(void)
2686 return "Packet list hidden column formats (deprecated)";
2689 static char *
2690 column_hidden_fmt_type_description_cb(void)
2692 return g_strdup("List all column formats to hide in the packet list. Deprecated in favor of the index-based preference.");
2695 static char *
2696 column_hidden_fmt_to_str_cb(pref_t* pref, bool default_val)
2698 GString *cols_hidden;
2699 GList *clp;
2700 fmt_data *cfmt;
2701 pref_t *format_pref;
2703 if (default_val)
2704 return g_strdup(pref->default_val.string);
2706 cols_hidden = g_string_new("");
2707 format_pref = prefs_find_preference(gui_column_module, PRS_COL_FMT);
2708 clp = (format_pref) ? *format_pref->varp.list : NULL;
2709 while (clp) {
2710 char *prefs_fmt;
2711 cfmt = (fmt_data *) clp->data;
2712 if (!cfmt->visible) {
2713 if (cols_hidden->len)
2714 g_string_append (cols_hidden, ",");
2715 prefs_fmt = column_fmt_data_to_str(cfmt);
2716 g_string_append(cols_hidden, prefs_fmt);
2717 g_free(prefs_fmt);
2719 clp = clp->next;
2722 return g_string_free (cols_hidden, FALSE);
2725 static bool
2726 column_hidden_fmt_is_default_cb(pref_t* pref)
2728 char *cur_hidden_str = column_hidden_fmt_to_str_cb(pref, false);
2729 bool is_default = g_strcmp0(cur_hidden_str, pref->default_val.string) == 0;
2731 g_free(cur_hidden_str);
2732 return is_default;
2735 /* Number of columns "preference". This is only used internally and is not written to the
2736 * preference file
2738 static void
2739 column_num_reset_cb(pref_t* pref)
2741 *pref->varp.uint = pref->default_val.uint;
2744 static prefs_set_pref_e
2745 column_num_set_cb(pref_t* pref _U_, const char* value _U_, unsigned int* changed_flags _U_)
2747 /* Don't write this to the preferences file */
2748 return PREFS_SET_OK;
2751 static const char *
2752 column_num_type_name_cb(void)
2754 return NULL;
2757 static char *
2758 column_num_type_description_cb(void)
2760 return g_strdup("");
2763 static bool
2764 column_num_is_default_cb(pref_t* pref _U_)
2766 return true;
2769 static char *
2770 column_num_to_str_cb(pref_t* pref _U_, bool default_val _U_)
2772 return g_strdup("");
2776 * Column format custom preference functions
2778 static void
2779 column_format_init_cb(pref_t* pref, GList** value)
2781 fmt_data *src_cfmt, *dest_cfmt;
2782 GList *entry;
2784 pref->varp.list = value;
2786 pref->default_val.list = NULL;
2787 for (entry = *pref->varp.list; entry != NULL; entry = g_list_next(entry)) {
2788 src_cfmt = (fmt_data *)entry->data;
2789 dest_cfmt = g_new(fmt_data,1);
2790 dest_cfmt->title = g_strdup(src_cfmt->title);
2791 dest_cfmt->fmt = src_cfmt->fmt;
2792 if (src_cfmt->custom_fields) {
2793 dest_cfmt->custom_fields = g_strdup(src_cfmt->custom_fields);
2794 dest_cfmt->custom_occurrence = src_cfmt->custom_occurrence;
2795 } else {
2796 dest_cfmt->custom_fields = NULL;
2797 dest_cfmt->custom_occurrence = 0;
2799 dest_cfmt->visible = src_cfmt->visible;
2800 dest_cfmt->display = src_cfmt->display;
2801 pref->default_val.list = g_list_append(pref->default_val.list, dest_cfmt);
2804 column_register_fields();
2807 static void
2808 column_format_free_cb(pref_t* pref)
2810 free_col_info(*pref->varp.list);
2811 free_col_info(pref->default_val.list);
2814 static void
2815 column_format_reset_cb(pref_t* pref)
2817 fmt_data *src_cfmt, *dest_cfmt;
2818 GList *entry;
2819 pref_t *col_num_pref;
2821 free_col_info(*pref->varp.list);
2822 *pref->varp.list = NULL;
2824 for (entry = pref->default_val.list; entry != NULL; entry = g_list_next(entry)) {
2825 src_cfmt = (fmt_data *)entry->data;
2826 dest_cfmt = g_new(fmt_data,1);
2827 dest_cfmt->title = g_strdup(src_cfmt->title);
2828 dest_cfmt->fmt = src_cfmt->fmt;
2829 if (src_cfmt->custom_fields) {
2830 dest_cfmt->custom_fields = g_strdup(src_cfmt->custom_fields);
2831 dest_cfmt->custom_occurrence = src_cfmt->custom_occurrence;
2832 } else {
2833 dest_cfmt->custom_fields = NULL;
2834 dest_cfmt->custom_occurrence = 0;
2836 dest_cfmt->visible = src_cfmt->visible;
2837 dest_cfmt->display = src_cfmt->display;
2838 *pref->varp.list = g_list_append(*pref->varp.list, dest_cfmt);
2841 col_num_pref = prefs_find_preference(gui_column_module, PRS_COL_NUM);
2842 ws_assert(col_num_pref != NULL); /* Should never happen */
2843 column_num_reset_cb(col_num_pref);
2846 static prefs_set_pref_e
2847 column_format_set_cb(pref_t* pref, const char* value, unsigned int* changed_flags _U_)
2849 GList *col_l, *col_l_elt;
2850 fmt_data *cfmt;
2851 int llen;
2852 pref_t *hidden_pref, *col_num_pref;
2854 col_l = prefs_get_string_list(value);
2855 if (col_l == NULL)
2856 return PREFS_SET_SYNTAX_ERR;
2857 if ((g_list_length(col_l) % 2) != 0) {
2858 /* A title didn't have a matching format. */
2859 prefs_clear_string_list(col_l);
2860 return PREFS_SET_SYNTAX_ERR;
2862 /* Check to make sure all column formats are valid. */
2863 col_l_elt = g_list_first(col_l);
2864 while (col_l_elt) {
2865 fmt_data cfmt_check;
2867 /* Go past the title. */
2868 col_l_elt = col_l_elt->next;
2870 /* Some predefined columns have been migrated to use custom columns.
2871 * We'll convert these silently here */
2872 try_convert_to_custom_column((char **)&col_l_elt->data);
2874 /* Parse the format to see if it's valid. */
2875 if (!parse_column_format(&cfmt_check, (char *)col_l_elt->data)) {
2876 /* It's not a valid column format. */
2877 prefs_clear_string_list(col_l);
2878 return PREFS_SET_SYNTAX_ERR;
2880 if (cfmt_check.fmt == COL_CUSTOM) {
2881 /* We don't need the custom column field on this pass. */
2882 g_free(cfmt_check.custom_fields);
2885 /* Go past the format. */
2886 col_l_elt = col_l_elt->next;
2889 /* They're all valid; process them. */
2890 free_col_info(*pref->varp.list);
2891 *pref->varp.list = NULL;
2892 if (prefs.cols_hide_new) {
2893 hidden_pref = prefs_find_preference(gui_column_module, PRS_COL_HIDDEN);
2894 } else {
2895 hidden_pref = prefs_find_preference(gui_column_module, PRS_COL_HIDDEN_FMT);
2897 ws_assert(hidden_pref != NULL); /* Should never happen */
2898 col_num_pref = prefs_find_preference(gui_column_module, PRS_COL_NUM);
2899 ws_assert(col_num_pref != NULL); /* Should never happen */
2900 llen = g_list_length(col_l);
2901 *col_num_pref->varp.uint = llen / 2;
2902 col_l_elt = g_list_first(col_l);
2903 int cidx = 1;
2904 while (col_l_elt) {
2905 cfmt = g_new(fmt_data,1);
2906 cfmt->title = g_strdup((char *)col_l_elt->data);
2907 col_l_elt = col_l_elt->next;
2908 parse_column_format(cfmt, (char *)col_l_elt->data);
2909 if (prefs.cols_hide_new) {
2910 cfmt->visible = prefs_is_column_visible(*hidden_pref->varp.string, cidx);
2911 } else {
2912 cfmt->visible = prefs_is_column_fmt_visible(*hidden_pref->varp.string, cfmt);
2914 col_l_elt = col_l_elt->next;
2915 *pref->varp.list = g_list_append(*pref->varp.list, cfmt);
2916 cidx++;
2919 prefs_clear_string_list(col_l);
2920 free_string_like_preference(hidden_pref);
2921 column_register_fields();
2922 return PREFS_SET_OK;
2926 static const char *
2927 column_format_type_name_cb(void)
2929 return "Packet list column format";
2932 static char *
2933 column_format_type_description_cb(void)
2935 return g_strdup("Each pair of strings consists of a column title and its format");
2938 static bool
2939 column_format_is_default_cb(pref_t* pref)
2941 GList *clp = *pref->varp.list,
2942 *pref_col = g_list_first(clp),
2943 *def_col = g_list_first(pref->default_val.list);
2944 fmt_data *cfmt, *def_cfmt;
2945 bool is_default = true;
2946 pref_t *col_num_pref;
2948 /* See if the column data has changed from the default */
2949 col_num_pref = prefs_find_preference(gui_column_module, PRS_COL_NUM);
2950 if (col_num_pref && *col_num_pref->varp.uint != col_num_pref->default_val.uint) {
2951 is_default = false;
2952 } else {
2953 while (pref_col && def_col) {
2954 cfmt = (fmt_data *) pref_col->data;
2955 def_cfmt = (fmt_data *) def_col->data;
2956 if ((g_strcmp0(cfmt->title, def_cfmt->title) != 0) ||
2957 (cfmt->fmt != def_cfmt->fmt) ||
2958 (((cfmt->fmt == COL_CUSTOM) && (cfmt->custom_fields)) &&
2959 ((g_strcmp0(cfmt->custom_fields, def_cfmt->custom_fields) != 0) ||
2960 (cfmt->display != def_cfmt->display)))) {
2961 is_default = false;
2962 break;
2965 pref_col = pref_col->next;
2966 def_col = def_col->next;
2970 return is_default;
2973 static char *
2974 column_format_to_str_cb(pref_t* pref, bool default_val)
2976 GList *pref_l = default_val ? pref->default_val.list : *pref->varp.list;
2977 GList *clp = g_list_first(pref_l);
2978 GList *col_l;
2979 fmt_data *cfmt;
2980 char *column_format_str;
2982 col_l = NULL;
2983 while (clp) {
2984 cfmt = (fmt_data *) clp->data;
2985 col_l = g_list_append(col_l, g_strdup(cfmt->title));
2986 col_l = g_list_append(col_l, column_fmt_data_to_str(cfmt));
2987 clp = clp->next;
2990 column_format_str = join_string_list(col_l);
2991 prefs_clear_string_list(col_l);
2992 return column_format_str;
2996 /****** Capture column custom preference functions ******/
2998 /* This routine is only called when Wireshark is started, NOT when another profile is selected.
2999 Copy the pref->capture_columns list (just loaded with the capture_cols[] struct values)
3000 to prefs->default_val.list.
3002 static void
3003 capture_column_init_cb(pref_t* pref, GList** capture_cols_values)
3005 GList *ccv_list = *capture_cols_values,
3006 *dlist = NULL;
3008 /* */
3009 while (ccv_list) {
3010 dlist = g_list_append(dlist, g_strdup((char *)ccv_list->data));
3011 ccv_list = ccv_list->next;
3014 pref->default_val.list = dlist;
3015 pref->varp.list = &prefs.capture_columns;
3016 pref->stashed_val.boolval = false;
3019 /* Free the prefs->capture_columns list strings and remove the list entries.
3020 Note that since pref->varp.list points to &prefs.capture_columns, it is
3021 also freed.
3023 static void
3024 capture_column_free_cb(pref_t* pref)
3026 prefs_clear_string_list(prefs.capture_columns);
3027 prefs.capture_columns = NULL;
3029 if (pref->stashed_val.boolval == true) {
3030 prefs_clear_string_list(pref->default_val.list);
3031 pref->default_val.list = NULL;
3035 /* Copy pref->default_val.list to *pref->varp.list.
3037 static void
3038 capture_column_reset_cb(pref_t* pref)
3040 GList *vlist = NULL, *dlist;
3042 /* Free the column name strings and remove the links from *pref->varp.list */
3043 prefs_clear_string_list(*pref->varp.list);
3045 for (dlist = pref->default_val.list; dlist != NULL; dlist = g_list_next(dlist)) {
3046 vlist = g_list_append(vlist, g_strdup((char *)dlist->data));
3048 *pref->varp.list = vlist;
3051 static prefs_set_pref_e
3052 capture_column_set_cb(pref_t* pref, const char* value, unsigned int* changed_flags _U_)
3054 GList *col_l = prefs_get_string_list(value);
3055 GList *col_l_elt;
3056 char *col_name;
3057 int i;
3059 if (col_l == NULL)
3060 return PREFS_SET_SYNTAX_ERR;
3062 capture_column_free_cb(pref);
3064 /* If value (the list of capture.columns read from preferences) is empty, set capture.columns
3065 to the full list of valid capture column names. */
3066 col_l_elt = g_list_first(col_l);
3067 if (!(*(char *)col_l_elt->data)) {
3068 for (i = 0; i < num_capture_cols; i++) {
3069 col_name = g_strdup(capture_cols[i]);
3070 prefs.capture_columns = g_list_append(prefs.capture_columns, col_name);
3074 /* Verify that all the column names are valid. If not, use the entire list of valid columns.
3076 while (col_l_elt) {
3077 bool found_match = false;
3078 col_name = (char *)col_l_elt->data;
3080 for (i = 0; i < num_capture_cols; i++) {
3081 if (strcmp(col_name, capture_cols[i])==0) {
3082 found_match = true;
3083 break;
3086 if (!found_match) {
3087 /* One or more cols are invalid so use the entire list of valid cols. */
3088 for (i = 0; i < num_capture_cols; i++) {
3089 col_name = g_strdup(capture_cols[i]);
3090 prefs.capture_columns = g_list_append(prefs.capture_columns, col_name);
3092 pref->varp.list = &prefs.capture_columns;
3093 prefs_clear_string_list(col_l);
3094 return PREFS_SET_SYNTAX_ERR;
3096 col_l_elt = col_l_elt->next;
3099 col_l_elt = g_list_first(col_l);
3100 while (col_l_elt) {
3101 col_name = (char *)col_l_elt->data;
3102 prefs.capture_columns = g_list_append(prefs.capture_columns, col_name);
3103 col_l_elt = col_l_elt->next;
3105 pref->varp.list = &prefs.capture_columns;
3106 g_list_free(col_l);
3107 return PREFS_SET_OK;
3111 static const char *
3112 capture_column_type_name_cb(void)
3114 return "Column list";
3117 static char *
3118 capture_column_type_description_cb(void)
3120 return g_strdup(
3121 "List of columns to be displayed in the capture options dialog.\n"
3122 CAPTURE_COL_TYPE_DESCRIPTION);
3125 static bool
3126 capture_column_is_default_cb(pref_t* pref)
3128 GList *pref_col = g_list_first(prefs.capture_columns),
3129 *def_col = g_list_first(pref->default_val.list);
3130 bool is_default = true;
3132 /* See if the column data has changed from the default */
3133 while (pref_col && def_col) {
3134 if (strcmp((char *)pref_col->data, (char *)def_col->data) != 0) {
3135 is_default = false;
3136 break;
3138 pref_col = pref_col->next;
3139 def_col = def_col->next;
3142 /* Ensure the same column count */
3143 if (((pref_col == NULL) && (def_col != NULL)) ||
3144 ((pref_col != NULL) && (def_col == NULL)))
3145 is_default = false;
3147 return is_default;
3150 static char *
3151 capture_column_to_str_cb(pref_t* pref, bool default_val)
3154 GList *pref_l = default_val ? pref->default_val.list : prefs.capture_columns;
3155 GList *clp = g_list_first(pref_l);
3156 GList *col_l = NULL;
3157 char *col;
3158 char *capture_column_str;
3160 while (clp) {
3161 col = (char *) clp->data;
3162 col_l = g_list_append(col_l, g_strdup(col));
3163 clp = clp->next;
3166 capture_column_str = join_string_list(col_l);
3167 prefs_clear_string_list(col_l);
3168 return capture_column_str;
3171 static prefs_set_pref_e
3172 colorized_frame_set_cb(pref_t* pref, const char* value, unsigned int* changed_flags)
3174 (*changed_flags) |= prefs_set_string_value(pref, value, pref_current);
3175 return PREFS_SET_OK;
3178 static const char *
3179 colorized_frame_type_name_cb(void)
3181 /* Don't write the colors of the 10 easy-access-colorfilters to the preferences
3182 * file until the colors can be changed in the GUI. Currently this is not really
3183 * possible since the STOCK-icons for these colors are hardcoded.
3185 * XXX Find a way to change the colors of the STOCK-icons on the fly and then
3186 * add these 10 colors to the list of colors that can be changed through
3187 * the preferences.
3190 return NULL;
3193 static char *
3194 colorized_frame_type_description_cb(void)
3196 return g_strdup("");
3199 static bool
3200 colorized_frame_is_default_cb(pref_t* pref _U_)
3202 return true;
3205 static char *
3206 colorized_frame_to_str_cb(pref_t* pref _U_, bool default_val _U_)
3208 return g_strdup("");
3212 * Register all non-dissector modules' preferences.
3214 static module_t *gui_module;
3215 static module_t *gui_color_module;
3216 static module_t *nameres_module;
3218 static void
3219 prefs_register_modules(void)
3221 module_t *printing, *capture_module, *console_module,
3222 *gui_layout_module, *gui_font_module;
3223 module_t *extcap_module;
3224 unsigned int layout_gui_flags;
3225 struct pref_custom_cbs custom_cbs;
3227 if (protocols_module != NULL) {
3228 /* Already setup preferences */
3229 return;
3232 /* GUI
3233 * These are "simple" GUI preferences that can be read/written using the
3234 * preference module API. These preferences still use their own
3235 * configuration screens for access, but this cuts down on the
3236 * preference "string compare list" in set_pref()
3238 extcap_module = prefs_register_module(NULL, "extcap", "Extcap Utilities",
3239 "Extcap Utilities", NULL, NULL, false);
3241 /* Setting default value to true */
3242 prefs.extcap_save_on_start = true;
3243 prefs_register_bool_preference(extcap_module, "gui_save_on_start",
3244 "Save arguments on start of capture",
3245 "Save arguments on start of capture",
3246 &prefs.extcap_save_on_start);
3248 /* GUI
3249 * These are "simple" GUI preferences that can be read/written using the
3250 * preference module API. These preferences still use their own
3251 * configuration screens for access, but this cuts down on the
3252 * preference "string compare list" in set_pref()
3254 gui_module = prefs_register_module(NULL, "gui", "User Interface",
3255 "User Interface", NULL, &gui_callback, false);
3257 * The GUI preferences don't affect dissection in general.
3258 * Any changes are signaled in other ways, so PREF_EFFECT_GUI doesn't
3259 * explicitly do anything, but wslua_set_preference expects *some*
3260 * effect flag to be set if the preference was changed.
3261 * We have to do this again for all the submodules (except for the
3262 * layout submodule, which has its own effect flag).
3264 unsigned gui_effect_flags = prefs_get_module_effect_flags(gui_module);
3265 gui_effect_flags |= PREF_EFFECT_GUI;
3266 gui_effect_flags &= (~PREF_EFFECT_DISSECTION);
3267 prefs_set_module_effect_flags(gui_module, gui_effect_flags);
3270 * gui.console_open is stored in the registry in addition to the
3271 * preferences file. It is also read independently by ws_log_init()
3272 * for early log initialization of the console.
3274 prefs_register_enum_preference(gui_module, "console_open",
3275 "Open a console window",
3276 "Open a console window (Windows only)",
3277 (int *)&ws_log_console_open, gui_console_open_type, false);
3279 prefs_register_obsolete_preference(gui_module, "scrollbar_on_right");
3280 prefs_register_obsolete_preference(gui_module, "packet_list_sel_browse");
3281 prefs_register_obsolete_preference(gui_module, "protocol_tree_sel_browse");
3282 prefs_register_obsolete_preference(gui_module, "tree_view_altern_colors");
3283 prefs_register_obsolete_preference(gui_module, "expert_composite_eyecandy");
3284 prefs_register_obsolete_preference(gui_module, "filter_toolbar_show_in_statusbar");
3286 prefs_register_bool_preference(gui_module, "restore_filter_after_following_stream",
3287 "Restore current display filter after following a stream",
3288 "Restore current display filter after following a stream?",
3289 &prefs.restore_filter_after_following_stream);
3291 prefs_register_obsolete_preference(gui_module, "protocol_tree_line_style");
3293 prefs_register_obsolete_preference(gui_module, "protocol_tree_expander_style");
3295 prefs_register_obsolete_preference(gui_module, "hex_dump_highlight_style");
3297 prefs_register_obsolete_preference(gui_module, "packet_editor.enabled");
3299 gui_column_module = prefs_register_subtree(gui_module, "Columns", "Columns", NULL);
3300 prefs_set_module_effect_flags(gui_column_module, gui_effect_flags);
3301 /* For reading older preference files with "column." preferences */
3302 prefs_register_module_alias("column", gui_column_module);
3305 custom_cbs.free_cb = free_string_like_preference;
3306 custom_cbs.reset_cb = reset_string_like_preference;
3307 custom_cbs.set_cb = column_hidden_set_cb;
3308 custom_cbs.type_name_cb = column_hidden_type_name_cb;
3309 custom_cbs.type_description_cb = column_hidden_type_description_cb;
3310 custom_cbs.is_default_cb = column_hidden_is_default_cb;
3311 custom_cbs.to_str_cb = column_hidden_to_str_cb;
3312 register_string_like_preference(gui_column_module, PRS_COL_HIDDEN, "Packet list hidden columns",
3313 "List all column indices (1-indexed) to hide in the packet list",
3314 &cols_hidden_list, PREF_CUSTOM, &custom_cbs, false);
3316 custom_cbs.set_cb = column_hidden_fmt_set_cb;
3317 custom_cbs.type_name_cb = column_hidden_fmt_type_name_cb;
3318 custom_cbs.type_description_cb = column_hidden_fmt_type_description_cb;
3319 custom_cbs.is_default_cb = column_hidden_fmt_is_default_cb;
3320 custom_cbs.to_str_cb = column_hidden_fmt_to_str_cb;
3322 register_string_like_preference(gui_column_module, PRS_COL_HIDDEN_FMT, "Packet list hidden column formats (deprecated)",
3323 "List all column formats to hide in the packet list; deprecated in favor of the index-based preference",
3324 &cols_hidden_fmt_list, PREF_CUSTOM, &custom_cbs, false);
3326 custom_cbs.free_cb = column_format_free_cb;
3327 custom_cbs.reset_cb = column_format_reset_cb;
3328 custom_cbs.set_cb = column_format_set_cb;
3329 custom_cbs.type_name_cb = column_format_type_name_cb;
3330 custom_cbs.type_description_cb = column_format_type_description_cb;
3331 custom_cbs.is_default_cb = column_format_is_default_cb;
3332 custom_cbs.to_str_cb = column_format_to_str_cb;
3334 prefs_register_list_custom_preference(gui_column_module, PRS_COL_FMT, "Packet list column format",
3335 "Each pair of strings consists of a column title and its format", &custom_cbs,
3336 column_format_init_cb, &prefs.col_list);
3338 /* Number of columns. This is only used internally and is not written to the
3339 * preference file
3341 custom_cbs.free_cb = custom_pref_no_cb;
3342 custom_cbs.reset_cb = column_num_reset_cb;
3343 custom_cbs.set_cb = column_num_set_cb;
3344 custom_cbs.type_name_cb = column_num_type_name_cb;
3345 custom_cbs.type_description_cb = column_num_type_description_cb;
3346 custom_cbs.is_default_cb = column_num_is_default_cb;
3347 custom_cbs.to_str_cb = column_num_to_str_cb;
3348 prefs_register_uint_custom_preference(gui_column_module, PRS_COL_NUM, "Number of columns",
3349 "Number of columns in col_list", &custom_cbs, &prefs.num_cols);
3351 /* User Interface : Font */
3352 gui_font_module = prefs_register_subtree(gui_module, "Font", "Font", NULL);
3353 prefs_set_module_effect_flags(gui_font_module, gui_effect_flags);
3355 prefs_register_obsolete_preference(gui_font_module, "font_name");
3357 prefs_register_obsolete_preference(gui_font_module, "gtk2.font_name");
3359 register_string_like_preference(gui_font_module, "qt.font_name", "Font name",
3360 "Font name for packet list, protocol tree, and hex dump panes. (Qt)",
3361 &prefs.gui_font_name, PREF_STRING, NULL, true);
3363 /* User Interface : Colors */
3364 gui_color_module = prefs_register_subtree(gui_module, "Colors", "Colors", NULL);
3365 unsigned gui_color_effect_flags = gui_effect_flags | PREF_EFFECT_GUI_COLOR;
3366 prefs_set_module_effect_flags(gui_color_module, gui_color_effect_flags);
3368 prefs_register_enum_preference(gui_color_module, "color_scheme", "Color scheme", "Color scheme",
3369 &prefs.gui_color_scheme, gui_color_scheme, false);
3371 prefs_register_color_preference(gui_color_module, "active_frame.fg", "Foreground color for an active selected item",
3372 "Foreground color for an active selected item", &prefs.gui_active_fg);
3374 prefs_register_color_preference(gui_color_module, "active_frame.bg", "Background color for an active selected item",
3375 "Background color for an active selected item", &prefs.gui_active_bg);
3377 prefs_register_enum_preference(gui_color_module, "active_frame.style", "Color style for an active selected item",
3378 "Color style for an active selected item", &prefs.gui_active_style, gui_selection_style, false);
3380 prefs_register_color_preference(gui_color_module, "inactive_frame.fg", "Foreground color for an inactive selected item",
3381 "Foreground color for an inactive selected item", &prefs.gui_inactive_fg);
3383 prefs_register_color_preference(gui_color_module, "inactive_frame.bg", "Background color for an inactive selected item",
3384 "Background color for an inactive selected item", &prefs.gui_inactive_bg);
3386 prefs_register_enum_preference(gui_color_module, "inactive_frame.style", "Color style for an inactive selected item",
3387 "Color style for an inactive selected item", &prefs.gui_inactive_style, gui_selection_style, false);
3389 prefs_register_color_preference(gui_color_module, "marked_frame.fg", "Color preferences for a marked frame",
3390 "Color preferences for a marked frame", &prefs.gui_marked_fg);
3392 prefs_register_color_preference(gui_color_module, "marked_frame.bg", "Color preferences for a marked frame",
3393 "Color preferences for a marked frame", &prefs.gui_marked_bg);
3395 prefs_register_color_preference(gui_color_module, "ignored_frame.fg", "Color preferences for a ignored frame",
3396 "Color preferences for a ignored frame", &prefs.gui_ignored_fg);
3398 prefs_register_color_preference(gui_color_module, "ignored_frame.bg", "Color preferences for a ignored frame",
3399 "Color preferences for a ignored frame", &prefs.gui_ignored_bg);
3401 prefs_register_color_preference(gui_color_module, "stream.client.fg", "TCP stream window color preference",
3402 "TCP stream window color preference", &prefs.st_client_fg);
3404 prefs_register_color_preference(gui_color_module, "stream.client.bg", "TCP stream window color preference",
3405 "TCP stream window color preference", &prefs.st_client_bg);
3407 prefs_register_color_preference(gui_color_module, "stream.server.fg", "TCP stream window color preference",
3408 "TCP stream window color preference", &prefs.st_server_fg);
3410 prefs_register_color_preference(gui_color_module, "stream.server.bg", "TCP stream window color preference",
3411 "TCP stream window color preference", &prefs.st_server_bg);
3413 custom_cbs.free_cb = free_string_like_preference;
3414 custom_cbs.reset_cb = reset_string_like_preference;
3415 custom_cbs.set_cb = colorized_frame_set_cb;
3416 custom_cbs.type_name_cb = colorized_frame_type_name_cb;
3417 custom_cbs.type_description_cb = colorized_frame_type_description_cb;
3418 custom_cbs.is_default_cb = colorized_frame_is_default_cb;
3419 custom_cbs.to_str_cb = colorized_frame_to_str_cb;
3420 register_string_like_preference(gui_column_module, "colorized_frame.fg", "Colorized Foreground",
3421 "Filter Colorized Foreground",
3422 &prefs.gui_colorized_fg, PREF_CUSTOM, &custom_cbs, true);
3424 custom_cbs.free_cb = free_string_like_preference;
3425 custom_cbs.reset_cb = reset_string_like_preference;
3426 custom_cbs.set_cb = colorized_frame_set_cb;
3427 custom_cbs.type_name_cb = colorized_frame_type_name_cb;
3428 custom_cbs.type_description_cb = colorized_frame_type_description_cb;
3429 custom_cbs.is_default_cb = colorized_frame_is_default_cb;
3430 custom_cbs.to_str_cb = colorized_frame_to_str_cb;
3431 register_string_like_preference(gui_column_module, "colorized_frame.bg", "Colorized Background",
3432 "Filter Colorized Background",
3433 &prefs.gui_colorized_bg, PREF_CUSTOM, &custom_cbs, true);
3435 prefs_register_color_preference(gui_color_module, "color_filter_bg.valid", "Valid color filter background",
3436 "Valid color filter background", &prefs.gui_text_valid);
3438 prefs_register_color_preference(gui_color_module, "color_filter_bg.invalid", "Invalid color filter background",
3439 "Invalid color filter background", &prefs.gui_text_invalid);
3441 prefs_register_color_preference(gui_color_module, "color_filter_bg.deprecated", "Deprecated color filter background",
3442 "Deprecated color filter background", &prefs.gui_text_deprecated);
3444 prefs_register_enum_preference(gui_module, "fileopen.style",
3445 "Where to start the File Open dialog box",
3446 "Where to start the File Open dialog box",
3447 &prefs.gui_fileopen_style, gui_fileopen_style, false);
3449 prefs_register_uint_preference(gui_module, "recent_files_count.max",
3450 "The max. number of items in the open recent files list",
3451 "The max. number of items in the open recent files list",
3453 &prefs.gui_recent_files_count_max);
3455 prefs_register_uint_preference(gui_module, "recent_display_filter_entries.max",
3456 "The max. number of entries in the display filter list",
3457 "The max. number of entries in the display filter list",
3459 &prefs.gui_recent_df_entries_max);
3461 register_string_like_preference(gui_module, "fileopen.dir", "Start Directory",
3462 "Directory to start in when opening File Open dialog.",
3463 &prefs.gui_fileopen_dir, PREF_DIRNAME, NULL, true);
3465 prefs_register_obsolete_preference(gui_module, "fileopen.remembered_dir");
3467 prefs_register_uint_preference(gui_module, "fileopen.preview",
3468 "The preview timeout in the File Open dialog",
3469 "The preview timeout in the File Open dialog",
3471 &prefs.gui_fileopen_preview);
3473 register_string_like_preference(gui_module, "tlskeylog_command", "Program to launch with TLS Keylog",
3474 "Program path or command line to launch with SSLKEYLOGFILE",
3475 &prefs.gui_tlskeylog_command, PREF_STRING, NULL, true);
3477 prefs_register_bool_preference(gui_module, "ask_unsaved",
3478 "Ask to save unsaved capture files",
3479 "Ask to save unsaved capture files?",
3480 &prefs.gui_ask_unsaved);
3482 prefs_register_bool_preference(gui_module, "autocomplete_filter",
3483 "Display autocompletion for filter text",
3484 "Display an autocomplete suggestion for display and capture filter controls",
3485 &prefs.gui_autocomplete_filter);
3487 prefs_register_bool_preference(gui_module, "find_wrap",
3488 "Wrap to beginning/end of file during search",
3489 "Wrap to beginning/end of file during search?",
3490 &prefs.gui_find_wrap);
3492 prefs_register_obsolete_preference(gui_module, "use_pref_save");
3494 prefs_register_bool_preference(gui_module, "geometry.save.position",
3495 "Save window position at exit",
3496 "Save window position at exit?",
3497 &prefs.gui_geometry_save_position);
3499 prefs_register_bool_preference(gui_module, "geometry.save.size",
3500 "Save window size at exit",
3501 "Save window size at exit?",
3502 &prefs.gui_geometry_save_size);
3504 prefs_register_bool_preference(gui_module, "geometry.save.maximized",
3505 "Save window maximized state at exit",
3506 "Save window maximized state at exit?",
3507 &prefs.gui_geometry_save_maximized);
3509 prefs_register_obsolete_preference(gui_module, "macosx_style");
3511 prefs_register_obsolete_preference(gui_module, "geometry.main.x");
3512 prefs_register_obsolete_preference(gui_module, "geometry.main.y");
3513 prefs_register_obsolete_preference(gui_module, "geometry.main.width");
3514 prefs_register_obsolete_preference(gui_module, "geometry.main.height");
3515 prefs_register_obsolete_preference(gui_module, "toolbar_main_show");
3517 prefs_register_enum_preference(gui_module, "toolbar_main_style",
3518 "Main Toolbar style",
3519 "Main Toolbar style",
3520 &prefs.gui_toolbar_main_style, gui_toolbar_style, false);
3522 prefs_register_obsolete_preference(gui_module, "toolbar_filter_style");
3523 prefs_register_obsolete_preference(gui_module, "webbrowser");
3525 prefs_register_bool_preference(gui_module, "update.enabled",
3526 "Check for updates",
3527 "Check for updates (Windows and macOS only)",
3528 &prefs.gui_update_enabled);
3530 prefs_register_enum_preference(gui_module, "update.channel",
3531 "Update channel",
3532 "The type of update to fetch. You should probably leave this set to STABLE.",
3533 (int*)(void*)(&prefs.gui_update_channel), gui_update_channel, false);
3535 prefs_register_uint_preference(gui_module, "update.interval",
3536 "How often to check for software updates",
3537 "How often to check for software updates in seconds",
3539 &prefs.gui_update_interval);
3541 prefs_register_uint_preference(gui_module, "debounce.timer",
3542 "How long to wait before processing computationally intensive user input",
3543 "How long to wait (in milliseconds) before processing "
3544 "computationally intensive user input. "
3545 "If you type quickly, consider lowering the value for a 'snappier' "
3546 "experience. "
3547 "If you type slowly, consider increasing the value to avoid performance issues. "
3548 "This is currently used to delay searches in View -> Internals -> Supported Protocols "
3549 "and Preferences -> Advanced menu.",
3551 &prefs.gui_debounce_timer);
3553 register_string_like_preference(gui_module, "window_title", "Custom window title",
3554 "Custom window title to be appended to the existing title\n"
3555 "%C = capture comment from command line\n"
3556 "%F = file path of the capture file\n"
3557 "%P = profile name\n"
3558 "%S = a conditional separator (\" - \") that only shows when surrounded by variables with values or static text\n"
3559 "%V = version info",
3560 &prefs.gui_window_title, PREF_STRING, NULL, true);
3562 register_string_like_preference(gui_module, "prepend_window_title", "Custom window title prefix",
3563 "Custom window title to be prepended to the existing title\n"
3564 "%C = capture comment from command line\n"
3565 "%F = file path of the capture file\n"
3566 "%P = profile name\n"
3567 "%S = a conditional separator (\" - \") that only shows when surrounded by variables with values or static text\n"
3568 "%V = version info",
3569 &prefs.gui_prepend_window_title, PREF_STRING, NULL, true);
3571 register_string_like_preference(gui_module, "start_title", "Custom start page title",
3572 "Custom start page title",
3573 &prefs.gui_start_title, PREF_STRING, NULL, true);
3575 prefs_register_enum_preference(gui_module, "version_placement",
3576 "Show version in the start page and/or main screen's title bar",
3577 "Show version in the start page and/or main screen's title bar",
3578 (int*)(void*)(&prefs.gui_version_placement), gui_version_placement_type, false);
3580 prefs_register_obsolete_preference(gui_module, "auto_scroll_on_expand");
3581 prefs_register_obsolete_preference(gui_module, "auto_scroll_percentage");
3583 prefs_register_uint_preference(gui_module, "max_export_objects",
3584 "Maximum number of exported objects",
3585 "The maximum number of objects that can be exported",
3587 &prefs.gui_max_export_objects);
3588 prefs_register_uint_preference(gui_module, "max_tree_items",
3589 "Maximum number of tree items",
3590 "The maximum number of items that can be added to the dissection tree (Increase with caution)",
3592 &prefs.gui_max_tree_items);
3594 * Used independently by proto_tree_add_node, call_dissector*, dissector_try_heuristic,
3595 * and increment_dissection_depth.
3597 prefs_register_uint_preference(gui_module, "max_tree_depth",
3598 "Maximum dissection depth",
3599 "The maximum depth for dissection tree and protocol layer checks. (Increase with caution)",
3601 &prefs.gui_max_tree_depth);
3603 prefs_register_bool_preference(gui_module, "welcome_page.show_recent",
3604 "Show recent files on the welcome page",
3605 "This will enable or disable the 'Open' list on the welcome page.",
3606 &prefs.gui_welcome_page_show_recent);
3608 /* User Interface : Layout */
3609 gui_layout_module = prefs_register_subtree(gui_module, "Layout", "Layout", gui_layout_callback);
3610 /* Adjust the preference effects of layout GUI for better handling of preferences at Wireshark (GUI) level */
3611 layout_gui_flags = prefs_get_module_effect_flags(gui_layout_module);
3612 layout_gui_flags |= PREF_EFFECT_GUI_LAYOUT;
3613 layout_gui_flags &= (~PREF_EFFECT_DISSECTION);
3615 prefs_register_uint_preference(gui_layout_module, "layout_type",
3616 "Layout type",
3617 "Layout type (1-6)",
3619 (unsigned*)(void*)(&prefs.gui_layout_type));
3620 prefs_set_effect_flags_by_name(gui_layout_module, "layout_type", layout_gui_flags);
3622 prefs_register_enum_preference(gui_layout_module, "layout_content_1",
3623 "Layout content of the pane 1",
3624 "Layout content of the pane 1",
3625 (int*)(void*)(&prefs.gui_layout_content_1), gui_layout_content, false);
3626 prefs_set_effect_flags_by_name(gui_layout_module, "layout_content_1", layout_gui_flags);
3628 prefs_register_enum_preference(gui_layout_module, "layout_content_2",
3629 "Layout content of the pane 2",
3630 "Layout content of the pane 2",
3631 (int*)(void*)(&prefs.gui_layout_content_2), gui_layout_content, false);
3632 prefs_set_effect_flags_by_name(gui_layout_module, "layout_content_2", layout_gui_flags);
3634 prefs_register_enum_preference(gui_layout_module, "layout_content_3",
3635 "Layout content of the pane 3",
3636 "Layout content of the pane 3",
3637 (int*)(void*)(&prefs.gui_layout_content_3), gui_layout_content, false);
3638 prefs_set_effect_flags_by_name(gui_layout_module, "layout_content_3", layout_gui_flags);
3640 prefs_register_bool_preference(gui_layout_module, "packet_list_separator.enabled",
3641 "Enable Packet List Separator",
3642 "Enable Packet List Separator",
3643 &prefs.gui_packet_list_separator);
3645 prefs_register_bool_preference(gui_layout_module, "packet_header_column_definition.enabled",
3646 "Show column definition in packet list header",
3647 "Show column definition in packet list header",
3648 &prefs.gui_packet_header_column_definition);
3650 /* packet_list_hover_style affects the colors, not the layout.
3651 * It's in the layout module to group it with the other packet list
3652 * preferences for the user's benefit with the dialog.
3654 prefs_register_bool_preference(gui_layout_module, "packet_list_hover_style.enabled",
3655 "Enable Packet List mouse-over colorization",
3656 "Enable Packet List mouse-over colorization",
3657 &prefs.gui_packet_list_hover_style);
3658 prefs_set_effect_flags_by_name(gui_layout_module, "packet_list_hover_style.enabled", gui_color_effect_flags);
3660 prefs_register_bool_preference(gui_layout_module, "show_selected_packet.enabled",
3661 "Show selected packet in the Status Bar",
3662 "Show selected packet in the Status Bar",
3663 &prefs.gui_show_selected_packet);
3665 prefs_register_bool_preference(gui_layout_module, "show_file_load_time.enabled",
3666 "Show file load time in the Status Bar",
3667 "Show file load time in the Status Bar",
3668 &prefs.gui_show_file_load_time);
3670 prefs_register_enum_preference(gui_layout_module, "packet_dialog_layout",
3671 "Packet Dialog layout",
3672 "Packet Dialog layout",
3673 (unsigned*)(void*)(&prefs.gui_packet_dialog_layout), gui_packet_dialog_layout, false);
3675 prefs_register_enum_preference(gui_module, "packet_list_elide_mode",
3676 "Elide mode",
3677 "The position of \"...\" (ellipsis) in packet list text.",
3678 (int*)(void*)(&prefs.gui_packet_list_elide_mode), gui_packet_list_elide_mode, false);
3679 prefs_register_uint_preference(gui_module, "decimal_places1",
3680 "Count of decimal places for values of type 1",
3681 "Sets the count of decimal places for values of type 1."
3682 "Type 1 values are defined by authors."
3683 "Value can be in range 2 to 10.",
3684 10,&prefs.gui_decimal_places1);
3686 prefs_register_uint_preference(gui_module, "decimal_places2",
3687 "Count of decimal places for values of type 2",
3688 "Sets the count of decimal places for values of type 2."
3689 "Type 2 values are defined by authors."
3690 "Value can be in range 2 to 10.",
3691 10,&prefs.gui_decimal_places2);
3693 prefs_register_uint_preference(gui_module, "decimal_places3",
3694 "Count of decimal places for values of type 3",
3695 "Sets the count of decimal places for values of type 3."
3696 "Type 3 values are defined by authors."
3697 "Value can be in range 2 to 10.",
3698 10,&prefs.gui_decimal_places3);
3700 prefs_register_bool_preference(gui_module, "rtp_player_use_disk1",
3701 "RTP Player saves temporary data to disk",
3702 "If set to true, RTP Player saves temporary data to "
3703 "temp files on disk. If not set, it uses memory."
3704 "Every stream uses one file therefore you might touch "
3705 "OS limit for count of opened files."
3706 "When ui.rtp_player_use_disk2 is set to true too, it uses "
3707 " two files per RTP stream together."
3708 ,&prefs.gui_rtp_player_use_disk1);
3710 prefs_register_bool_preference(gui_module, "rtp_player_use_disk2",
3711 "RTP Player saves temporary dictionary for data to disk",
3712 "If set to true, RTP Player saves temporary dictionary to "
3713 "temp files on disk. If not set, it uses memory."
3714 "Every stream uses one file therefore you might touch "
3715 "OS limit for count of opened files."
3716 "When ui.rtp_player_use_disk1 is set to true too, it uses "
3717 " two files per RTP stream."
3718 ,&prefs.gui_rtp_player_use_disk2);
3720 prefs_register_enum_preference(gui_layout_module, "gui_packet_list_copy_format_options_for_keyboard_shortcut",
3721 "Allows text to be copied with selected format",
3722 "Allows text to be copied with selected format when copied via keyboard",
3723 (int*)(void*)(&prefs.gui_packet_list_copy_format_options_for_keyboard_shortcut),
3724 gui_packet_list_copy_format_options_for_keyboard_shortcut, false);
3726 prefs_register_bool_preference(gui_layout_module, "gui_packet_list_copy_text_with_aligned_columns",
3727 "Allows text to be copied with aligned columns",
3728 "Allows text to be copied with aligned columns when copied via menu or keyboard",
3729 &prefs.gui_packet_list_copy_text_with_aligned_columns);
3731 prefs_register_bool_preference(gui_layout_module, "packet_list_show_related",
3732 "Show Related Packets",
3733 "Show related packet indicators in the first column",
3734 &prefs.gui_packet_list_show_related);
3736 prefs_register_bool_preference(gui_layout_module, "packet_list_show_minimap",
3737 "Enable Intelligent Scroll Bar",
3738 "Show the intelligent scroll bar (a minimap of packet list colors in the scrollbar)",
3739 &prefs.gui_packet_list_show_minimap);
3741 prefs_register_bool_preference(gui_module, "packet_list_is_sortable",
3742 "Allow packet list to be sortable",
3743 "To prevent sorting by mistake (which can take some time to calculate), it can be disabled",
3744 &prefs.gui_packet_list_sortable);
3746 prefs_register_uint_preference(gui_module, "packet_list_cached_rows_max",
3747 "Maximum cached rows",
3748 "Maximum number of rows that can be sorted by columns that require dissection. Increasing this increases memory consumption by caching column text",
3750 &prefs.gui_packet_list_cached_rows_max);
3752 prefs_register_bool_preference(gui_module, "interfaces_show_hidden",
3753 "Show hidden interfaces",
3754 "Show all interfaces, including interfaces marked as hidden",
3755 &prefs.gui_interfaces_show_hidden);
3757 prefs_register_bool_preference(gui_module, "interfaces_remote_display",
3758 "Show Remote interfaces",
3759 "Show remote interfaces in the interface selection",
3760 &prefs.gui_interfaces_remote_display);
3762 register_string_like_preference(gui_module, "interfaces_hidden_types", "Hide interface types in list",
3763 "Hide the given interface types in the startup list.\n"
3764 "A comma-separated string of interface type values (e.g. 5,9).\n"
3765 "0 = Wired,\n"
3766 "1 = AirPCAP,\n"
3767 "2 = Pipe,\n"
3768 "3 = STDIN,\n"
3769 "4 = Bluetooth,\n"
3770 "5 = Wireless,\n"
3771 "6 = Dial-Up,\n"
3772 "7 = USB,\n"
3773 "8 = External Capture,\n"
3774 "9 = Virtual",
3775 &prefs.gui_interfaces_hide_types, PREF_STRING, NULL, true);
3777 prefs_register_bool_preference(gui_module, "io_graph_automatic_update",
3778 "Enables automatic updates for IO Graph",
3779 "Enables automatic updates for IO Graph",
3780 &prefs.gui_io_graph_automatic_update);
3782 prefs_register_bool_preference(gui_module, "io_graph_enable_legend",
3783 "Enables the legend of IO Graph",
3784 "Enables the legend of IO Graph",
3785 &prefs.gui_io_graph_enable_legend);
3787 prefs_register_bool_preference(gui_module, "show_byteview_in_dialog",
3788 "Show the byte view in the packet details dialog",
3789 "Show the byte view in the packet details dialog",
3790 &prefs.gui_packet_details_show_byteview);
3792 /* Console
3793 * These are preferences that can be read/written using the
3794 * preference module API. These preferences still use their own
3795 * configuration screens for access, but this cuts down on the
3796 * preference "string compare list" in set_pref()
3798 console_module = prefs_register_module(NULL, "console", "Console",
3799 "Console logging and debugging output", NULL, NULL, false);
3801 prefs_register_obsolete_preference(console_module, "log.level");
3803 prefs_register_bool_preference(console_module, "incomplete_dissectors_check_debug",
3804 "Print debug line for incomplete dissectors",
3805 "Look for dissectors that left some bytes undecoded (debug)",
3806 &prefs.incomplete_dissectors_check_debug);
3808 /* Display filter Expressions
3809 * This used to be an array of individual fields that has now been
3810 * converted to a UAT. Just make it part of the GUI category even
3811 * though the name of the preference will never be seen in preference
3812 * file
3814 filter_expression_register_uat(gui_module);
3816 /* Capture
3817 * These are preferences that can be read/written using the
3818 * preference module API. These preferences still use their own
3819 * configuration screens for access, but this cuts down on the
3820 * preference "string compare list" in set_pref()
3822 capture_module = prefs_register_module(NULL, "capture", "Capture",
3823 "Capture preferences", NULL, NULL, false);
3824 /* Capture preferences don't affect dissection */
3825 prefs_set_module_effect_flags(capture_module, PREF_EFFECT_CAPTURE);
3827 register_string_like_preference(capture_module, "device", "Default capture device",
3828 "Default capture device",
3829 &prefs.capture_device, PREF_STRING, NULL, false);
3831 register_string_like_preference(capture_module, "devices_linktypes", "Interface link-layer header type",
3832 "Interface link-layer header types (Ex: en0(1),en1(143),...)",
3833 &prefs.capture_devices_linktypes, PREF_STRING, NULL, false);
3835 register_string_like_preference(capture_module, "devices_descr", "Interface descriptions",
3836 "Interface descriptions (Ex: eth0(eth0 descr),eth1(eth1 descr),...)",
3837 &prefs.capture_devices_descr, PREF_STRING, NULL, false);
3839 register_string_like_preference(capture_module, "devices_hide", "Hide interface",
3840 "Hide interface? (Ex: eth0,eth3,...)",
3841 &prefs.capture_devices_hide, PREF_STRING, NULL, false);
3843 register_string_like_preference(capture_module, "devices_monitor_mode", "Capture in monitor mode",
3844 "By default, capture in monitor mode on interface? (Ex: eth0,eth3,...)",
3845 &prefs.capture_devices_monitor_mode, PREF_STRING, NULL, false);
3847 register_string_like_preference(capture_module, "devices_buffersize", "Interface buffer size",
3848 "Interface buffer size (Ex: en0(1),en1(143),...)",
3849 &prefs.capture_devices_buffersize, PREF_STRING, NULL, false);
3851 register_string_like_preference(capture_module, "devices_snaplen", "Interface snap length",
3852 "Interface snap length (Ex: en0(65535),en1(1430),...)",
3853 &prefs.capture_devices_snaplen, PREF_STRING, NULL, false);
3855 register_string_like_preference(capture_module, "devices_pmode", "Interface promiscuous mode",
3856 "Interface promiscuous mode (Ex: en0(0),en1(1),...)",
3857 &prefs.capture_devices_pmode, PREF_STRING, NULL, false);
3859 prefs_register_bool_preference(capture_module, "prom_mode", "Capture in promiscuous mode",
3860 "Capture in promiscuous mode?", &prefs.capture_prom_mode);
3862 prefs_register_bool_preference(capture_module, "monitor_mode", "Capture in monitor mode on 802.11 devices",
3863 "Capture in monitor mode on all 802.11 devices that support it?", &prefs.capture_monitor_mode);
3865 register_string_like_preference(capture_module, "devices_filter", "Interface capture filter",
3866 "Interface capture filter (Ex: en0(tcp),en1(udp),...)",
3867 &prefs.capture_devices_filter, PREF_STRING, NULL, false);
3869 prefs_register_bool_preference(capture_module, "pcap_ng", "Capture in pcapng format",
3870 "Capture in pcapng format?", &prefs.capture_pcap_ng);
3872 prefs_register_bool_preference(capture_module, "real_time_update", "Update packet list in real time during capture",
3873 "Update packet list in real time during capture?", &prefs.capture_real_time);
3875 prefs_register_uint_preference(capture_module, "update_interval",
3876 "Capture update interval",
3877 "Capture update interval in ms",
3879 &prefs.capture_update_interval);
3881 prefs_register_bool_preference(capture_module, "no_interface_load", "Don't load interfaces on startup",
3882 "Don't automatically load capture interfaces on startup", &prefs.capture_no_interface_load);
3884 prefs_register_bool_preference(capture_module, "no_extcap", "Disable external capture interfaces",
3885 "Disable external capture modules (extcap)", &prefs.capture_no_extcap);
3887 prefs_register_obsolete_preference(capture_module, "auto_scroll");
3889 prefs_register_bool_preference(capture_module, "show_info", "Show capture information dialog while capturing",
3890 "Show capture information dialog while capturing?", &prefs.capture_show_info);
3892 prefs_register_obsolete_preference(capture_module, "syntax_check_filter");
3894 custom_cbs.free_cb = capture_column_free_cb;
3895 custom_cbs.reset_cb = capture_column_reset_cb;
3896 custom_cbs.set_cb = capture_column_set_cb;
3897 custom_cbs.type_name_cb = capture_column_type_name_cb;
3898 custom_cbs.type_description_cb = capture_column_type_description_cb;
3899 custom_cbs.is_default_cb = capture_column_is_default_cb;
3900 custom_cbs.to_str_cb = capture_column_to_str_cb;
3901 prefs_register_list_custom_preference(capture_module, "columns", "Capture options dialog column list",
3902 "List of columns to be displayed", &custom_cbs, capture_column_init_cb, &prefs.capture_columns);
3904 /* Name Resolution */
3905 nameres_module = prefs_register_module(NULL, "nameres", "Name Resolution",
3906 "Name Resolution", "ChCustPreferencesSection.html#ChCustPrefsNameSection", addr_resolve_pref_apply, true);
3907 addr_resolve_pref_init(nameres_module);
3908 oid_pref_init(nameres_module);
3909 maxmind_db_pref_init(nameres_module);
3911 /* Printing
3912 * None of these have any effect; we keep them as obsolete preferences
3913 * in order to avoid errors when reading older preference files.
3915 printing = prefs_register_module(NULL, "print", "Printing",
3916 "Printing", NULL, NULL, false);
3917 prefs_register_obsolete_preference(printing, "format");
3918 prefs_register_obsolete_preference(printing, "command");
3919 prefs_register_obsolete_preference(printing, "file");
3921 /* Codecs */
3922 codecs_module = prefs_register_module(NULL, "codecs", "Codecs",
3923 "Codecs", NULL, NULL, true);
3925 /* Statistics */
3926 stats_module = prefs_register_module(NULL, "statistics", "Statistics",
3927 "Statistics", "ChCustPreferencesSection.html#_statistics", &stats_callback, true);
3929 prefs_register_uint_preference(stats_module, "update_interval",
3930 "Tap update interval in ms",
3931 "Determines time between tap updates",
3933 &prefs.tap_update_interval);
3935 prefs_register_uint_preference(stats_module, "flow_graph_max_export_items",
3936 "Maximum Flow Graph items to export as image",
3937 "The maximum number of Flow Graph items (frames) "
3938 "to include when exporting the graph as an image. "
3939 "Note that some formats (e.g., JPEG) have inherent "
3940 "pixel limits and image viewers might be unable to "
3941 "handle very large images.",
3943 &prefs.flow_graph_max_export_items);
3945 prefs_register_bool_preference(stats_module, "st_enable_burstinfo",
3946 "Enable the calculation of burst information",
3947 "If enabled burst rates will be calculated for statistics that use the stats_tree system. "
3948 "Burst rates are calculated over a much shorter time interval than the rate column.",
3949 &prefs.st_enable_burstinfo);
3951 prefs_register_bool_preference(stats_module, "st_burst_showcount",
3952 "Show burst count for item rather than rate",
3953 "If selected the stats_tree statistics nodes will show the count of events "
3954 "within the burst window instead of a burst rate. Burst rate is calculated "
3955 "as number of events within burst window divided by the burst windown length.",
3956 &prefs.st_burst_showcount);
3958 prefs_register_uint_preference(stats_module, "st_burst_resolution",
3959 "Burst rate resolution (ms)",
3960 "Sets the duration of the time interval into which events are grouped when calculating "
3961 "the burst rate. Higher resolution (smaller number) increases processing overhead.",
3962 10,&prefs.st_burst_resolution);
3964 prefs_register_uint_preference(stats_module, "st_burst_windowlen",
3965 "Burst rate window size (ms)",
3966 "Sets the duration of the sliding window during which the burst rate is "
3967 "measured. Longer window relative to burst rate resolution increases "
3968 "processing overhead. Will be truncated to a multiple of burst resolution.",
3969 10,&prefs.st_burst_windowlen);
3971 prefs_register_enum_preference(stats_module, "st_sort_defcolflag",
3972 "Default sort column for stats_tree stats",
3973 "Sets the default column by which stats based on the stats_tree "
3974 "system is sorted.",
3975 &prefs.st_sort_defcolflag, st_sort_col_vals, false);
3977 prefs_register_bool_preference(stats_module, "st_sort_defdescending",
3978 "Default stats_tree sort order is descending",
3979 "When selected, statistics based on the stats_tree system will by default "
3980 "be sorted in descending order.",
3981 &prefs.st_sort_defdescending);
3983 prefs_register_bool_preference(stats_module, "st_sort_casesensitve",
3984 "Case sensitive sort of stats_tree item names",
3985 "When selected, the item/node names of statistics based on the stats_tree "
3986 "system will be sorted taking case into account. Else the case of the name "
3987 "will be ignored.",
3988 &prefs.st_sort_casesensitve);
3990 prefs_register_bool_preference(stats_module, "st_sort_rng_nameonly",
3991 "Always sort 'range' nodes by name",
3992 "When selected, the stats_tree nodes representing a range of values "
3993 "(0-49, 50-100, etc.) will always be sorted by name (the range of the "
3994 "node). Else range nodes are sorted by the same column as the rest of "
3995 " the tree.",
3996 &prefs.st_sort_rng_nameonly);
3998 prefs_register_bool_preference(stats_module, "st_sort_rng_fixorder",
3999 "Always sort 'range' nodes in ascending order",
4000 "When selected, the stats_tree nodes representing a range of values "
4001 "(0-49, 50-100, etc.) will always be sorted ascending; else it follows "
4002 "the sort direction of the tree. Only effective if \"Always sort "
4003 "'range' nodes by name\" is also selected.",
4004 &prefs.st_sort_rng_fixorder);
4006 prefs_register_bool_preference(stats_module, "st_sort_showfullname",
4007 "Display the full stats_tree plug-in name",
4008 "When selected, the full name (including menu path) of the stats_tree "
4009 "plug-in is show in windows. If cleared the plug-in name is shown "
4010 "without menu path (only the part of the name after last '/' character.)",
4011 &prefs.st_sort_showfullname);
4013 /* Protocols */
4014 protocols_module = prefs_register_module(NULL, "protocols", "Protocols",
4015 "Protocols", "ChCustPreferencesSection.html#ChCustPrefsProtocolsSection", NULL, true);
4017 prefs_register_bool_preference(protocols_module, "display_hidden_proto_items",
4018 "Display hidden protocol items",
4019 "Display all hidden protocol items in the packet list.",
4020 &prefs.display_hidden_proto_items);
4022 prefs_register_bool_preference(protocols_module, "display_byte_fields_with_spaces",
4023 "Display byte fields with a space character between bytes",
4024 "Display all byte fields with a space character between each byte in the packet list.",
4025 &prefs.display_byte_fields_with_spaces);
4028 * Note the -t / option only affects the display of the packet timestamp
4029 * in the default time column; this is for all other absolute times.
4031 prefs_register_enum_preference(protocols_module, "display_abs_time_ascii",
4032 "Format absolute times like asctime",
4033 "When to format absolute times similar to asctime instead of ISO 8601, for backwards compatibility with older Wireshark.",
4034 (int*)&prefs.display_abs_time_ascii, abs_time_format_options, false);
4036 prefs_register_bool_preference(protocols_module, "enable_incomplete_dissectors_check",
4037 "Look for incomplete dissectors",
4038 "Look for dissectors that left some bytes undecoded.",
4039 &prefs.enable_incomplete_dissectors_check);
4041 prefs_register_bool_preference(protocols_module, "strict_conversation_tracking_heuristics",
4042 "Enable stricter conversation tracking heuristics",
4043 "Protocols may use things like VLAN ID or interface ID to narrow the potential for duplicate conversations. "
4044 "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",
4045 &prefs.strict_conversation_tracking_heuristics);
4047 prefs_register_bool_preference(protocols_module, "ignore_dup_frames",
4048 "Ignore duplicate frames",
4049 "Ignore frames that are exact duplicates of any previous frame.",
4050 &prefs.ignore_dup_frames);
4052 prefs_register_enum_preference(protocols_module, "conversation_deinterlacing_key",
4053 "Deinterlacing conversations key",
4054 "Separate into different conversations frames that look like duplicates but have different Interface, MAC, or VLAN field values.",
4055 (int *)&prefs.conversation_deinterlacing_key, conv_deint_options, false);
4057 prefs_register_uint_preference(protocols_module, "ignore_dup_frames_cache_entries",
4058 "The max number of hashes to keep in memory for determining duplicates frames",
4059 "If \"Ignore duplicate frames\" is set, this setting sets the maximum number "
4060 "of cache entries to maintain. A 0 means no limit.",
4061 10, &prefs.ignore_dup_frames_cache_entries);
4064 /* Obsolete preferences
4065 * These "modules" were reorganized/renamed to correspond to their GUI
4066 * configuration screen within the preferences dialog
4069 /* taps is now part of the stats module */
4070 prefs_register_module(NULL, "taps", "TAPS", "TAPS", NULL, NULL, false);
4071 /* packet_list is now part of the protocol (parent) module */
4072 prefs_register_module(NULL, "packet_list", "PACKET_LIST", "PACKET_LIST", NULL, NULL, false);
4073 /* stream is now part of the gui module */
4074 prefs_register_module(NULL, "stream", "STREAM", "STREAM", NULL, NULL, false);
4078 /* Parse through a list of comma-separated, possibly quoted strings.
4079 Return a list of the string data. */
4080 GList *
4081 prefs_get_string_list(const char *str)
4083 enum { PRE_STRING, IN_QUOT, NOT_IN_QUOT };
4085 int state = PRE_STRING, i = 0;
4086 bool backslash = false;
4087 unsigned char cur_c;
4088 const size_t default_size = 64;
4089 GString *slstr = NULL;
4090 GList *sl = NULL;
4092 /* Allocate a buffer for the first string. */
4093 slstr = g_string_sized_new(default_size);
4095 for (;;) {
4096 cur_c = str[i];
4097 if (cur_c == '\0') {
4098 /* It's the end of the input, so it's the end of the string we
4099 were working on, and there's no more input. */
4100 if (state == IN_QUOT || backslash) {
4101 /* We were in the middle of a quoted string or backslash escape,
4102 and ran out of characters; that's an error. */
4103 g_string_free(slstr, TRUE);
4104 prefs_clear_string_list(sl);
4105 return NULL;
4107 if (slstr->len > 0)
4108 sl = g_list_append(sl, g_string_free(slstr, FALSE));
4109 else
4110 g_string_free(slstr, TRUE);
4111 break;
4113 if (cur_c == '"' && !backslash) {
4114 switch (state) {
4115 case PRE_STRING:
4116 /* We hadn't yet started processing a string; this starts the
4117 string, and we're now quoting. */
4118 state = IN_QUOT;
4119 break;
4120 case IN_QUOT:
4121 /* We're in the middle of a quoted string, and we saw a quotation
4122 mark; we're no longer quoting. */
4123 state = NOT_IN_QUOT;
4124 break;
4125 case NOT_IN_QUOT:
4126 /* We're working on a string, but haven't seen a quote; we're
4127 now quoting. */
4128 state = IN_QUOT;
4129 break;
4130 default:
4131 break;
4133 } else if (cur_c == '\\' && !backslash) {
4134 /* We saw a backslash, and the previous character wasn't a
4135 backslash; escape the next character.
4137 This also means we've started a new string. */
4138 backslash = true;
4139 if (state == PRE_STRING)
4140 state = NOT_IN_QUOT;
4141 } else if (cur_c == ',' && state != IN_QUOT && !backslash) {
4142 /* We saw a comma, and we're not in the middle of a quoted string
4143 and it wasn't preceded by a backslash; it's the end of
4144 the string we were working on... */
4145 if (slstr->len > 0) {
4146 sl = g_list_append(sl, g_string_free(slstr, FALSE));
4147 slstr = g_string_sized_new(default_size);
4150 /* ...and the beginning of a new string. */
4151 state = PRE_STRING;
4152 } else if (!g_ascii_isspace(cur_c) || state != PRE_STRING) {
4153 /* Either this isn't a white-space character, or we've started a
4154 string (i.e., already seen a non-white-space character for that
4155 string and put it into the string).
4157 The character is to be put into the string; do so. */
4158 g_string_append_c(slstr, cur_c);
4160 /* If it was backslash-escaped, we're done with the backslash escape. */
4161 backslash = false;
4163 i++;
4165 return(sl);
4168 char *join_string_list(GList *sl)
4170 GString *joined_str = g_string_new("");
4171 GList *cur, *first;
4172 char *str;
4173 unsigned item_count = 0;
4175 cur = first = g_list_first(sl);
4176 while (cur) {
4177 item_count++;
4178 str = (char *)cur->data;
4180 if (cur != first)
4181 g_string_append_c(joined_str, ',');
4183 if (item_count % 2) {
4184 /* Wrap the line. */
4185 g_string_append(joined_str, "\n\t");
4186 } else
4187 g_string_append_c(joined_str, ' ');
4189 g_string_append_c(joined_str, '"');
4190 while (*str) {
4191 gunichar uc = g_utf8_get_char (str);
4193 if (uc == '"' || uc == '\\')
4194 g_string_append_c(joined_str, '\\');
4196 if (g_unichar_isprint(uc))
4197 g_string_append_unichar (joined_str, uc);
4199 str = g_utf8_next_char (str);
4202 g_string_append_c(joined_str, '"');
4204 cur = cur->next;
4206 return g_string_free(joined_str, FALSE);
4209 void
4210 prefs_clear_string_list(GList *sl)
4212 g_list_free_full(sl, g_free);
4216 * Takes a string, a pointer to an array of "enum_val_t"s, and a default int
4217 * value.
4218 * The array must be terminated by an entry with a null "name" string.
4220 * If the string matches a "name" string in an entry, the value from that
4221 * entry is returned.
4223 * Otherwise, if a string matches a "description" string in an entry, the
4224 * value from that entry is returned; we do that for backwards compatibility,
4225 * as we used to have only a "name" string that was used both for command-line
4226 * and configuration-file values and in the GUI (which meant either that
4227 * the GUI had what might be somewhat cryptic values to select from or that
4228 * the "-o" flag took long strings, often with spaces in them).
4230 * Otherwise, the default value that was passed as the third argument is
4231 * returned.
4233 static int
4234 find_val_for_string(const char *needle, const enum_val_t *haystack,
4235 int default_value)
4237 int i;
4239 for (i = 0; haystack[i].name != NULL; i++) {
4240 if (g_ascii_strcasecmp(needle, haystack[i].name) == 0) {
4241 return haystack[i].value;
4244 for (i = 0; haystack[i].name != NULL; i++) {
4245 if (g_ascii_strcasecmp(needle, haystack[i].description) == 0) {
4246 return haystack[i].value;
4249 return default_value;
4252 /* Preferences file format:
4253 * - Configuration directives start at the beginning of the line, and
4254 * are terminated with a colon.
4255 * - Directives can be continued on the next line by preceding them with
4256 * whitespace.
4258 * Example:
4260 # This is a comment line
4261 print.command: lpr
4262 print.file: /a/very/long/path/
4263 to/wireshark-out.ps
4267 /* Initialize non-dissector preferences to wired-in default values Called
4268 * at program startup and any time the profile changes. (The dissector
4269 * preferences are assumed to be set to those values by the dissectors.)
4270 * They may be overridden by the global preferences file or the user's
4271 * preferences file.
4273 static void
4274 init_prefs(void)
4276 if (prefs_initialized)
4277 return;
4279 uat_load_all();
4282 * Ensure the "global" preferences have been initialized so the
4283 * preference API has the proper default values to work from
4285 pre_init_prefs();
4287 prefs_register_modules();
4289 prefs_initialized = true;
4293 * Initialize non-dissector preferences used by the "register preference" API
4294 * to default values so the default values can be used when registered.
4296 * String, filename, and directory preferences will be g_freed so they must
4297 * be g_mallocated.
4299 static void
4300 pre_init_prefs(void)
4302 int i;
4303 char *col_name;
4304 fmt_data *cfmt;
4305 static const char *col_fmt_packets[] = {
4306 "No.", "%m", "Time", "%t",
4307 "Source", "%s", "Destination", "%d",
4308 "Protocol", "%p", "Length", "%L",
4309 "Info", "%i" };
4310 static const char **col_fmt = col_fmt_packets;
4311 int num_cols = 7;
4313 if (!is_packet_configuration_namespace()) {
4314 static const char *col_fmt_logs[] = {
4315 "No.", "%m",
4316 "Time", "%t",
4317 "Event name", "%Cus:sysdig.event_name:0:R",
4318 "Dir", "%Cus:evt.dir:0:R",
4319 "Proc Name", "%Cus:proc.name:0:R",
4320 "PID", "%Cus:proc.pid:0:R",
4321 "TID", "%Cus:thread.tid:0:R",
4322 "FD", "%Cus:fd.num:0:R",
4323 "FD Name", "%Cus:fd.name:0:R",
4324 "Container Name", "%Cus:container.name:0:R",
4325 "Arguments", "%Cus:evt.args:0:R",
4326 "Info", "%i"
4328 col_fmt = col_fmt_logs;
4329 num_cols = 12;
4332 prefs.restore_filter_after_following_stream = false;
4333 prefs.gui_toolbar_main_style = TB_STYLE_ICONS;
4334 /* We try to find the best font in the Qt code */
4335 g_free(prefs.gui_font_name);
4336 prefs.gui_font_name = g_strdup("");
4337 prefs.gui_active_fg.red = 0;
4338 prefs.gui_active_fg.green = 0;
4339 prefs.gui_active_fg.blue = 0;
4340 prefs.gui_active_bg.red = 52223;
4341 prefs.gui_active_bg.green = 59647;
4342 prefs.gui_active_bg.blue = 65535;
4343 prefs.gui_active_style = COLOR_STYLE_DEFAULT;
4344 prefs.gui_inactive_fg.red = 0;
4345 prefs.gui_inactive_fg.green = 0;
4346 prefs.gui_inactive_fg.blue = 0;
4347 prefs.gui_inactive_bg.red = 61439;
4348 prefs.gui_inactive_bg.green = 61439;
4349 prefs.gui_inactive_bg.blue = 61439;
4350 prefs.gui_inactive_style = COLOR_STYLE_DEFAULT;
4351 prefs.gui_marked_fg.red = 65535;
4352 prefs.gui_marked_fg.green = 65535;
4353 prefs.gui_marked_fg.blue = 65535;
4354 prefs.gui_marked_bg.red = 0;
4355 prefs.gui_marked_bg.green = 8224;
4356 prefs.gui_marked_bg.blue = 10794;
4357 prefs.gui_ignored_fg.red = 32767;
4358 prefs.gui_ignored_fg.green = 32767;
4359 prefs.gui_ignored_fg.blue = 32767;
4360 prefs.gui_ignored_bg.red = 65535;
4361 prefs.gui_ignored_bg.green = 65535;
4362 prefs.gui_ignored_bg.blue = 65535;
4363 g_free(prefs.gui_colorized_fg);
4364 prefs.gui_colorized_fg = g_strdup("000000,000000,000000,000000,000000,000000,000000,000000,000000,000000");
4365 g_free(prefs.gui_colorized_bg);
4366 prefs.gui_colorized_bg = g_strdup("ffc0c0,ffc0ff,e0c0e0,c0c0ff,c0e0e0,c0ffff,c0ffc0,ffffc0,e0e0c0,e0e0e0");
4367 prefs.st_client_fg.red = 32767;
4368 prefs.st_client_fg.green = 0;
4369 prefs.st_client_fg.blue = 0;
4370 prefs.st_client_bg.red = 64507;
4371 prefs.st_client_bg.green = 60909;
4372 prefs.st_client_bg.blue = 60909;
4373 prefs.st_server_fg.red = 0;
4374 prefs.st_server_fg.green = 0;
4375 prefs.st_server_fg.blue = 32767;
4376 prefs.st_server_bg.red = 60909;
4377 prefs.st_server_bg.green = 60909;
4378 prefs.st_server_bg.blue = 64507;
4380 if (gui_theme_is_dark) {
4381 // Green, red and yellow with HSV V = 84
4382 prefs.gui_text_valid.red = 0x0000; /* dark green */
4383 prefs.gui_text_valid.green = 0x66ff;
4384 prefs.gui_text_valid.blue = 0x0000;
4385 prefs.gui_text_invalid.red = 0x66FF; /* dark red */
4386 prefs.gui_text_invalid.green = 0x0000;
4387 prefs.gui_text_invalid.blue = 0x0000;
4388 prefs.gui_text_deprecated.red = 0x66FF; /* dark yellow / olive */
4389 prefs.gui_text_deprecated.green = 0x66FF;
4390 prefs.gui_text_deprecated.blue = 0x0000;
4391 } else {
4392 // Green, red and yellow with HSV V = 20
4393 prefs.gui_text_valid.red = 0xAFFF; /* light green */
4394 prefs.gui_text_valid.green = 0xFFFF;
4395 prefs.gui_text_valid.blue = 0xAFFF;
4396 prefs.gui_text_invalid.red = 0xFFFF; /* light red */
4397 prefs.gui_text_invalid.green = 0xAFFF;
4398 prefs.gui_text_invalid.blue = 0xAFFF;
4399 prefs.gui_text_deprecated.red = 0xFFFF; /* light yellow */
4400 prefs.gui_text_deprecated.green = 0xFFFF;
4401 prefs.gui_text_deprecated.blue = 0xAFFF;
4404 prefs.gui_geometry_save_position = true;
4405 prefs.gui_geometry_save_size = true;
4406 prefs.gui_geometry_save_maximized= true;
4407 prefs.gui_fileopen_style = FO_STYLE_LAST_OPENED;
4408 prefs.gui_recent_df_entries_max = 10;
4409 prefs.gui_recent_files_count_max = 10;
4410 g_free(prefs.gui_fileopen_dir);
4411 prefs.gui_fileopen_dir = g_strdup(get_persdatafile_dir());
4412 prefs.gui_fileopen_preview = 3;
4413 g_free(prefs.gui_tlskeylog_command);
4414 prefs.gui_tlskeylog_command = g_strdup("");
4415 prefs.gui_ask_unsaved = true;
4416 prefs.gui_autocomplete_filter = true;
4417 prefs.gui_find_wrap = true;
4418 prefs.gui_update_enabled = true;
4419 prefs.gui_update_channel = UPDATE_CHANNEL_STABLE;
4420 prefs.gui_update_interval = 60*60*24; /* Seconds */
4421 prefs.gui_debounce_timer = 400; /* milliseconds */
4422 g_free(prefs.gui_window_title);
4423 prefs.gui_window_title = g_strdup("");
4424 g_free(prefs.gui_prepend_window_title);
4425 prefs.gui_prepend_window_title = g_strdup("");
4426 g_free(prefs.gui_start_title);
4427 prefs.gui_start_title = g_strdup("The World's Most Popular Network Protocol Analyzer");
4428 prefs.gui_version_placement = version_both;
4429 prefs.gui_welcome_page_show_recent = true;
4430 prefs.gui_layout_type = layout_type_2;
4431 prefs.gui_layout_content_1 = layout_pane_content_plist;
4432 prefs.gui_layout_content_2 = layout_pane_content_pdetails;
4433 prefs.gui_layout_content_3 = layout_pane_content_pbytes;
4434 prefs.gui_packet_list_elide_mode = ELIDE_RIGHT;
4435 prefs.gui_packet_list_copy_format_options_for_keyboard_shortcut = COPY_FORMAT_TEXT;
4436 prefs.gui_packet_list_copy_text_with_aligned_columns = false;
4437 prefs.gui_packet_list_show_related = true;
4438 prefs.gui_packet_list_show_minimap = true;
4439 prefs.gui_packet_list_sortable = true;
4440 prefs.gui_packet_list_cached_rows_max = 10000;
4441 g_free (prefs.gui_interfaces_hide_types);
4442 prefs.gui_interfaces_hide_types = g_strdup("");
4443 prefs.gui_interfaces_show_hidden = false;
4444 prefs.gui_interfaces_remote_display = true;
4445 prefs.gui_packet_list_separator = false;
4446 prefs.gui_packet_header_column_definition = true;
4447 prefs.gui_packet_list_hover_style = true;
4448 prefs.gui_show_selected_packet = false;
4449 prefs.gui_show_file_load_time = false;
4450 prefs.gui_max_export_objects = 1000;
4451 prefs.gui_max_tree_items = 1 * 1000 * 1000;
4452 prefs.gui_max_tree_depth = 5 * 100;
4453 prefs.gui_decimal_places1 = DEF_GUI_DECIMAL_PLACES1;
4454 prefs.gui_decimal_places2 = DEF_GUI_DECIMAL_PLACES2;
4455 prefs.gui_decimal_places3 = DEF_GUI_DECIMAL_PLACES3;
4457 if (prefs.col_list) {
4458 free_col_info(prefs.col_list);
4459 prefs.col_list = NULL;
4461 for (i = 0; i < num_cols; i++) {
4462 cfmt = g_new0(fmt_data,1);
4463 cfmt->title = g_strdup(col_fmt[i * 2]);
4464 cfmt->visible = true;
4465 cfmt->display = COLUMN_DISPLAY_STRINGS;
4466 parse_column_format(cfmt, col_fmt[(i * 2) + 1]);
4467 prefs.col_list = g_list_append(prefs.col_list, cfmt);
4469 prefs.num_cols = num_cols;
4471 /* set the default values for the capture dialog box */
4472 prefs.capture_prom_mode = true;
4473 prefs.capture_monitor_mode = false;
4474 prefs.capture_pcap_ng = true;
4475 prefs.capture_real_time = true;
4476 prefs.capture_update_interval = DEFAULT_UPDATE_INTERVAL;
4477 prefs.capture_no_extcap = false;
4478 prefs.capture_show_info = false;
4480 if (!prefs.capture_columns) {
4481 /* First time through */
4482 for (i = 0; i < num_capture_cols; i++) {
4483 col_name = g_strdup(capture_cols[i]);
4484 prefs.capture_columns = g_list_append(prefs.capture_columns, col_name);
4488 /* set the default values for the tap/statistics dialog box */
4489 prefs.tap_update_interval = TAP_UPDATE_DEFAULT_INTERVAL;
4490 prefs.flow_graph_max_export_items = 1000;
4491 prefs.st_enable_burstinfo = true;
4492 prefs.st_burst_showcount = false;
4493 prefs.st_burst_resolution = ST_DEF_BURSTRES;
4494 prefs.st_burst_windowlen = ST_DEF_BURSTLEN;
4495 prefs.st_sort_casesensitve = true;
4496 prefs.st_sort_rng_fixorder = true;
4497 prefs.st_sort_rng_nameonly = true;
4498 prefs.st_sort_defcolflag = ST_SORT_COL_COUNT;
4499 prefs.st_sort_defdescending = true;
4500 prefs.st_sort_showfullname = false;
4502 /* protocols */
4503 prefs.display_hidden_proto_items = false;
4504 prefs.display_byte_fields_with_spaces = false;
4505 prefs.display_abs_time_ascii = ABS_TIME_ASCII_TREE;
4506 prefs.ignore_dup_frames = false;
4507 prefs.ignore_dup_frames_cache_entries = 10000;
4509 /* set the default values for the io graph dialog */
4510 prefs.gui_io_graph_automatic_update = true;
4511 prefs.gui_io_graph_enable_legend = true;
4513 /* set the default values for the packet dialog */
4514 prefs.gui_packet_dialog_layout = layout_vertical;
4515 prefs.gui_packet_details_show_byteview = true;
4519 * Reset a single dissector preference.
4521 void
4522 reset_pref(pref_t *pref)
4524 int type;
4525 if (!pref) return;
4527 type = pref->type;
4530 * This preference is no longer supported; it's not a
4531 * real preference, so we don't reset it (i.e., we
4532 * treat it as if it weren't found in the list of
4533 * preferences, and we weren't called in the first place).
4535 if (IS_PREF_OBSOLETE(type))
4536 return;
4537 else
4538 RESET_PREF_OBSOLETE(type);
4540 switch (type) {
4542 case PREF_UINT:
4543 *pref->varp.uint = pref->default_val.uint;
4544 break;
4546 case PREF_BOOL:
4547 *pref->varp.boolp = pref->default_val.boolval;
4548 break;
4550 case PREF_ENUM:
4551 case PREF_PROTO_TCP_SNDAMB_ENUM:
4552 *pref->varp.enump = pref->default_val.enumval;
4553 break;
4555 case PREF_STRING:
4556 case PREF_SAVE_FILENAME:
4557 case PREF_OPEN_FILENAME:
4558 case PREF_DIRNAME:
4559 case PREF_PASSWORD:
4560 case PREF_DISSECTOR:
4561 reset_string_like_preference(pref);
4562 break;
4564 case PREF_RANGE:
4565 case PREF_DECODE_AS_RANGE:
4566 wmem_free(wmem_epan_scope(), *pref->varp.range);
4567 *pref->varp.range = range_copy(wmem_epan_scope(), pref->default_val.range);
4568 break;
4570 case PREF_STATIC_TEXT:
4571 case PREF_UAT:
4572 /* Nothing to do */
4573 break;
4575 case PREF_COLOR:
4576 *pref->varp.colorp = pref->default_val.color;
4577 break;
4579 case PREF_CUSTOM:
4580 pref->custom_cbs.reset_cb(pref);
4581 break;
4585 static void
4586 reset_pref_cb(void *data, void *user_data)
4588 pref_t *pref = (pref_t *) data;
4589 module_t *module = (module_t *)user_data;
4591 if (pref && (pref->type == PREF_RANGE || pref->type == PREF_DECODE_AS_RANGE)) {
4593 * Some dissectors expect the range (returned via prefs_get_range_value)
4594 * to remain valid if it has not changed. If it did change, then we
4595 * should set "prefs_changed_flags" to ensure that the preference apply
4596 * callback is invoked. That callback will notify dissectors that it
4597 * should no longer assume the range to be valid.
4599 if (ranges_are_equal(*pref->varp.range, pref->default_val.range)) {
4600 /* Optimization: do not invoke apply callback if nothing changed. */
4601 return;
4603 module->prefs_changed_flags |= prefs_get_effect_flags(pref);
4605 reset_pref(pref);
4609 * Reset all preferences for a module.
4611 static bool
4612 reset_module_prefs(const void *key _U_, void *value, void *data _U_)
4614 module_t *module = (module_t *)value;
4615 g_list_foreach(module->prefs, reset_pref_cb, module);
4616 return false;
4619 /* Reset preferences */
4620 void
4621 prefs_reset(void)
4623 prefs_initialized = false;
4624 g_free(prefs.saved_at_version);
4625 prefs.saved_at_version = NULL;
4628 * Unload all UAT preferences.
4630 uat_unload_all();
4633 * Unload any loaded MIBs.
4635 oids_cleanup();
4638 * Reset the non-dissector preferences.
4640 init_prefs();
4643 * Reset the non-UAT dissector preferences.
4645 wmem_tree_foreach(prefs_modules, reset_module_prefs, NULL);
4648 #ifdef _WIN32
4649 static void
4650 read_registry(void)
4652 HKEY hTestKey;
4653 DWORD data;
4654 DWORD data_size = sizeof(DWORD);
4655 DWORD ret;
4657 ret = RegOpenKeyExA(HKEY_CURRENT_USER, REG_HKCU_WIRESHARK_KEY, 0, KEY_READ, &hTestKey);
4658 if (ret != ERROR_SUCCESS && ret != ERROR_FILE_NOT_FOUND) {
4659 ws_noisy("Cannot open HKCU "REG_HKCU_WIRESHARK_KEY": 0x%lx", ret);
4660 return;
4663 ret = RegQueryValueExA(hTestKey, LOG_HKCU_CONSOLE_OPEN, NULL, NULL, (LPBYTE)&data, &data_size);
4664 if (ret == ERROR_SUCCESS) {
4665 ws_log_console_open = (ws_log_console_open_pref)data;
4666 ws_noisy("Got "LOG_HKCU_CONSOLE_OPEN" from Windows registry: %d", ws_log_console_open);
4668 else if (ret != ERROR_FILE_NOT_FOUND) {
4669 ws_noisy("Error reading registry key "LOG_HKCU_CONSOLE_OPEN": 0x%lx", ret);
4672 RegCloseKey(hTestKey);
4674 #endif
4676 void
4677 prefs_read_module(const char *module)
4679 int err;
4680 char *pf_path;
4681 FILE *pf;
4683 module_t *target_module = prefs_find_module(module);
4684 if (!target_module) {
4685 return;
4688 /* Construct the pathname of the user's preferences file for the module. */
4689 char *pf_name = wmem_strdup_printf(NULL, "%s.cfg", module);
4690 pf_path = get_persconffile_path(pf_name, true);
4691 wmem_free(NULL, pf_name);
4693 /* Read the user's module preferences file, if it exists and is not a dir. */
4694 if (!test_for_regular_file(pf_path) || ((pf = ws_fopen(pf_path, "r")) == NULL)) {
4695 g_free(pf_path);
4696 /* Fall back to the user's generic preferences file. */
4697 pf_path = get_persconffile_path(PF_NAME, true);
4698 pf = ws_fopen(pf_path, "r");
4701 if (pf != NULL) {
4702 /* We succeeded in opening it; read it. */
4703 err = read_prefs_file(pf_path, pf, set_pref, target_module);
4704 if (err != 0) {
4705 /* We had an error reading the file; report it. */
4706 report_warning("Error reading your preferences file \"%s\": %s.",
4707 pf_path, g_strerror(err));
4708 } else
4709 g_free(pf_path);
4710 fclose(pf);
4711 } else {
4712 /* We failed to open it. If we failed for some reason other than
4713 "it doesn't exist", return the errno and the pathname, so our
4714 caller can report the error. */
4715 if (errno != ENOENT) {
4716 report_warning("Can't open your preferences file \"%s\": %s.",
4717 pf_path, g_strerror(errno));
4718 } else
4719 g_free(pf_path);
4722 return;
4725 /* Read the preferences file, fill in "prefs", and return a pointer to it.
4727 If we got an error (other than "it doesn't exist") we report it through
4728 the UI. */
4729 e_prefs *
4730 read_prefs(void)
4732 int err;
4733 char *pf_path;
4734 FILE *pf;
4736 /* clean up libsmi structures before reading prefs */
4737 oids_cleanup();
4739 init_prefs();
4741 #ifdef _WIN32
4742 read_registry();
4743 #endif
4746 * If we don't already have the pathname of the global preferences
4747 * file, construct it. Then, in either case, try to open the file.
4749 if (gpf_path == NULL) {
4751 * We don't have the path; try the new path first, and, if that
4752 * file doesn't exist, try the old path.
4754 gpf_path = get_datafile_path(PF_NAME);
4755 if ((pf = ws_fopen(gpf_path, "r")) == NULL && errno == ENOENT) {
4757 * It doesn't exist by the new name; try the old name.
4759 g_free(gpf_path);
4760 gpf_path = get_datafile_path(OLD_GPF_NAME);
4761 pf = ws_fopen(gpf_path, "r");
4763 } else {
4765 * We have the path; try it.
4767 pf = ws_fopen(gpf_path, "r");
4771 * If we were able to open the file, read it.
4772 * XXX - if it failed for a reason other than "it doesn't exist",
4773 * report the error.
4775 if (pf != NULL) {
4777 * Start out the counters of "mgcp.{tcp,udp}.port" entries we've
4778 * seen.
4780 mgcp_tcp_port_count = 0;
4781 mgcp_udp_port_count = 0;
4783 /* We succeeded in opening it; read it. */
4784 err = read_prefs_file(gpf_path, pf, set_pref, NULL);
4785 if (err != 0) {
4786 /* We had an error reading the file; report it. */
4787 report_warning("Error reading global preferences file \"%s\": %s.",
4788 gpf_path, g_strerror(err));
4790 fclose(pf);
4791 } else {
4792 /* We failed to open it. If we failed for some reason other than
4793 "it doesn't exist", report the error. */
4794 if (errno != ENOENT) {
4795 if (errno != 0) {
4796 report_warning("Can't open global preferences file \"%s\": %s.",
4797 gpf_path, g_strerror(errno));
4802 /* Construct the pathname of the user's preferences file. */
4803 pf_path = get_persconffile_path(PF_NAME, true);
4805 /* Read the user's preferences file, if it exists. */
4806 if ((pf = ws_fopen(pf_path, "r")) != NULL) {
4808 * Start out the counters of "mgcp.{tcp,udp}.port" entries we've
4809 * seen.
4811 mgcp_tcp_port_count = 0;
4812 mgcp_udp_port_count = 0;
4814 /* We succeeded in opening it; read it. */
4815 err = read_prefs_file(pf_path, pf, set_pref, NULL);
4816 if (err != 0) {
4817 /* We had an error reading the file; report it. */
4818 report_warning("Error reading your preferences file \"%s\": %s.",
4819 pf_path, g_strerror(err));
4820 } else
4821 g_free(pf_path);
4822 fclose(pf);
4823 } else {
4824 /* We failed to open it. If we failed for some reason other than
4825 "it doesn't exist", return the errno and the pathname, so our
4826 caller can report the error. */
4827 if (errno != ENOENT) {
4828 report_warning("Can't open your preferences file \"%s\": %s.",
4829 pf_path, g_strerror(errno));
4830 } else
4831 g_free(pf_path);
4834 /* load SMI modules if needed */
4835 oids_init();
4837 return &prefs;
4840 /* read the preferences file (or similar) and call the callback
4841 * function to set each key/value pair found */
4843 read_prefs_file(const char *pf_path, FILE *pf,
4844 pref_set_pair_cb pref_set_pair_fct, void *private_data)
4846 enum {
4847 START, /* beginning of a line */
4848 IN_VAR, /* processing key name */
4849 PRE_VAL, /* finished processing key name, skipping white space before value */
4850 IN_VAL, /* processing value */
4851 IN_SKIP /* skipping to the end of the line */
4852 } state = START;
4853 int got_c;
4854 GString *cur_val;
4855 GString *cur_var;
4856 bool got_val = false;
4857 int fline = 1, pline = 1;
4858 char hint[] = "(save preferences to remove this warning)";
4859 char ver[128];
4861 cur_val = g_string_new("");
4862 cur_var = g_string_new("");
4864 /* Try to read in the profile name in the first line of the preferences file. */
4865 if (fscanf(pf, "# Configuration file for %127[^\r\n]", ver) == 1) {
4866 /* Assume trailing period and remove it */
4867 g_free(prefs.saved_at_version);
4868 prefs.saved_at_version = g_strndup(ver, strlen(ver) - 1);
4870 rewind(pf);
4872 while ((got_c = ws_getc_unlocked(pf)) != EOF) {
4873 if (got_c == '\r') {
4874 /* Treat CR-LF at the end of a line like LF, so that if we're reading
4875 * a Windows-format file on UN*X, we handle it the same way we'd handle
4876 * a UN*X-format file. */
4877 got_c = ws_getc_unlocked(pf);
4878 if (got_c == EOF)
4879 break;
4880 if (got_c != '\n') {
4881 /* Put back the character after the CR, and process the CR normally. */
4882 ungetc(got_c, pf);
4883 got_c = '\r';
4886 if (got_c == '\n') {
4887 state = START;
4888 fline++;
4889 continue;
4892 switch (state) {
4893 case START:
4894 if (g_ascii_isalnum(got_c)) {
4895 if (cur_var->len > 0) {
4896 if (got_val) {
4897 if (cur_val->len > 0) {
4898 if (cur_val->str[cur_val->len-1] == ',') {
4900 * If the pref has a trailing comma, eliminate it.
4902 cur_val->str[cur_val->len-1] = '\0';
4903 ws_warning("%s line %d: trailing comma in \"%s\" %s", pf_path, pline, cur_var->str, hint);
4906 /* Call the routine to set the preference; it will parse
4907 the value as appropriate.
4909 Since we're reading a file, rather than processing
4910 explicit user input, for range preferences, silently
4911 lower values in excess of the range's maximum, rather
4912 than reporting errors and failing. */
4913 switch (pref_set_pair_fct(cur_var->str, cur_val->str, private_data, false)) {
4915 case PREFS_SET_OK:
4916 break;
4918 case PREFS_SET_SYNTAX_ERR:
4919 report_warning("Syntax error in preference \"%s\" at line %d of\n%s %s",
4920 cur_var->str, pline, pf_path, hint);
4921 break;
4923 case PREFS_SET_NO_SUCH_PREF:
4924 ws_warning("No such preference \"%s\" at line %d of\n%s %s",
4925 cur_var->str, pline, pf_path, hint);
4926 prefs.unknown_prefs = true;
4927 break;
4929 case PREFS_SET_OBSOLETE:
4931 * If an attempt is made to save the
4932 * preferences, a popup warning will be
4933 * displayed stating that obsolete prefs
4934 * have been detected and the user will
4935 * be given the opportunity to save these
4936 * prefs under a different profile name.
4937 * The prefs in question need to be listed
4938 * in the console window so that the
4939 * user can make an informed choice.
4941 ws_warning("Obsolete preference \"%s\" at line %d of\n%s %s",
4942 cur_var->str, pline, pf_path, hint);
4943 prefs.unknown_prefs = true;
4944 break;
4946 } else {
4947 ws_warning("Incomplete preference at line %d: of\n%s %s", pline, pf_path, hint);
4950 state = IN_VAR;
4951 got_val = false;
4952 g_string_truncate(cur_var, 0);
4953 g_string_append_c(cur_var, (char) got_c);
4954 pline = fline;
4955 } else if (g_ascii_isspace(got_c) && cur_var->len > 0 && got_val) {
4956 state = PRE_VAL;
4957 } else if (got_c == '#') {
4958 state = IN_SKIP;
4959 } else {
4960 ws_warning("Malformed preference at line %d of\n%s %s", fline, pf_path, hint);
4962 break;
4963 case IN_VAR:
4964 if (got_c != ':') {
4965 g_string_append_c(cur_var, (char) got_c);
4966 } else {
4967 /* This is a colon (':') */
4968 state = PRE_VAL;
4969 g_string_truncate(cur_val, 0);
4971 * Set got_val to true to accommodate prefs such as
4972 * "gui.fileopen.dir" that do not require a value.
4974 got_val = true;
4976 break;
4977 case PRE_VAL:
4978 if (!g_ascii_isspace(got_c)) {
4979 state = IN_VAL;
4980 g_string_append_c(cur_val, (char) got_c);
4982 break;
4983 case IN_VAL:
4984 g_string_append_c(cur_val, (char) got_c);
4985 break;
4986 case IN_SKIP:
4987 break;
4990 if (cur_var->len > 0) {
4991 if (got_val) {
4992 /* Call the routine to set the preference; it will parse
4993 the value as appropriate.
4995 Since we're reading a file, rather than processing
4996 explicit user input, for range preferences, silently
4997 lower values in excess of the range's maximum, rather
4998 than reporting errors and failing. */
4999 switch (pref_set_pair_fct(cur_var->str, cur_val->str, private_data, false)) {
5001 case PREFS_SET_OK:
5002 break;
5004 case PREFS_SET_SYNTAX_ERR:
5005 ws_warning("Syntax error in preference %s at line %d of\n%s %s",
5006 cur_var->str, pline, pf_path, hint);
5007 break;
5009 case PREFS_SET_NO_SUCH_PREF:
5010 ws_warning("No such preference \"%s\" at line %d of\n%s %s",
5011 cur_var->str, pline, pf_path, hint);
5012 prefs.unknown_prefs = true;
5013 break;
5015 case PREFS_SET_OBSOLETE:
5016 prefs.unknown_prefs = true;
5017 break;
5019 } else {
5020 ws_warning("Incomplete preference at line %d of\n%s %s",
5021 pline, pf_path, hint);
5025 g_string_free(cur_val, TRUE);
5026 g_string_free(cur_var, TRUE);
5028 if (ferror(pf))
5029 return errno;
5030 else
5031 return 0;
5035 * If we were handed a preference starting with "uat:", try to turn it into
5036 * a valid uat entry.
5038 static bool
5039 prefs_set_uat_pref(char *uat_entry, char **errmsg) {
5040 char *p, *colonp;
5041 uat_t *uat;
5042 bool ret;
5044 colonp = strchr(uat_entry, ':');
5045 if (colonp == NULL)
5046 return false;
5048 p = colonp;
5049 *p++ = '\0';
5052 * Skip over any white space (there probably won't be any, but
5053 * as we allow it in the preferences file, we might as well
5054 * allow it here).
5056 while (g_ascii_isspace(*p))
5057 p++;
5058 if (*p == '\0') {
5060 * Put the colon back, so if our caller uses, in an
5061 * error message, the string they passed us, the message
5062 * looks correct.
5064 *colonp = ':';
5065 return false;
5068 uat = uat_find(uat_entry);
5069 *colonp = ':';
5070 if (uat == NULL) {
5071 *errmsg = g_strdup("Unknown preference");
5072 return false;
5075 ret = uat_load_str(uat, p, errmsg);
5076 return ret;
5080 * Given a string of the form "<pref name>:<pref value>", as might appear
5081 * as an argument to a "-o" option, parse it and set the preference in
5082 * question. Return an indication of whether it succeeded or failed
5083 * in some fashion.
5085 prefs_set_pref_e
5086 prefs_set_pref(char *prefarg, char **errmsg)
5088 char *p, *colonp;
5089 prefs_set_pref_e ret;
5092 * Set the counters of "mgcp.{tcp,udp}.port" entries we've
5093 * seen to values that keep us from trying to interpret them
5094 * as "mgcp.{tcp,udp}.gateway_port" or "mgcp.{tcp,udp}.callagent_port",
5095 * as, from the command line, we have no way of guessing which
5096 * the user had in mind.
5098 mgcp_tcp_port_count = -1;
5099 mgcp_udp_port_count = -1;
5101 *errmsg = NULL;
5103 colonp = strchr(prefarg, ':');
5104 if (colonp == NULL)
5105 return PREFS_SET_SYNTAX_ERR;
5107 p = colonp;
5108 *p++ = '\0';
5111 * Skip over any white space (there probably won't be any, but
5112 * as we allow it in the preferences file, we might as well
5113 * allow it here).
5115 while (g_ascii_isspace(*p))
5116 p++;
5117 /* The empty string is a legal value for range preferences (PREF_RANGE,
5118 * PREF_DECODE_AS_RANGE), and string-like preferences (PREF_STRING,
5119 * PREF_SAVE_FILENAME, PREF_OPEN_FILENAME, PREF_DIRNAME), indeed often
5120 * not just useful but the default. A user might have a value saved
5121 * to their preference file but want to override it to default behavior.
5122 * Individual preference handlers of those types should be prepared to
5123 * deal with an empty string. For other types, it is up to set_pref() to
5124 * test for the empty string and set PREFS_SET_SYNTAX_ERROR there.
5126 if (strcmp(prefarg, "uat")) {
5127 ret = set_pref(prefarg, p, NULL, true);
5128 } else {
5129 ret = prefs_set_uat_pref(p, errmsg) ? PREFS_SET_OK : PREFS_SET_SYNTAX_ERR;
5131 *colonp = ':'; /* put the colon back */
5132 return ret;
5135 unsigned prefs_get_uint_value_real(pref_t *pref, pref_source_t source)
5137 switch (source)
5139 case pref_default:
5140 return pref->default_val.uint;
5141 case pref_stashed:
5142 return pref->stashed_val.uint;
5143 case pref_current:
5144 return *pref->varp.uint;
5145 default:
5146 ws_assert_not_reached();
5147 break;
5150 return 0;
5153 unsigned prefs_get_uint_value(const char *module_name, const char* pref_name)
5155 pref_t *pref = prefs_find_preference(prefs_find_module(module_name), pref_name);
5156 if (pref == NULL) {
5157 return 0;
5159 return prefs_get_uint_value_real(pref, pref_current);
5162 char* prefs_get_password_value(pref_t *pref, pref_source_t source)
5164 return prefs_get_string_value(pref, source);
5168 unsigned int prefs_set_uint_value(pref_t *pref, unsigned value, pref_source_t source)
5170 unsigned int changed = 0;
5171 switch (source)
5173 case pref_default:
5174 if (pref->default_val.uint != value) {
5175 pref->default_val.uint = value;
5176 changed = prefs_get_effect_flags(pref);
5178 break;
5179 case pref_stashed:
5180 if (pref->stashed_val.uint != value) {
5181 pref->stashed_val.uint = value;
5182 changed = prefs_get_effect_flags(pref);
5184 break;
5185 case pref_current:
5186 if (*pref->varp.uint != value) {
5187 *pref->varp.uint = value;
5188 changed = prefs_get_effect_flags(pref);
5190 break;
5191 default:
5192 ws_assert_not_reached();
5193 break;
5196 return changed;
5200 * For use by UI code that sets preferences.
5202 unsigned int
5203 prefs_set_password_value(pref_t *pref, const char* value, pref_source_t source)
5205 return prefs_set_string_value(pref, value, source);
5209 unsigned prefs_get_uint_base(pref_t *pref)
5211 return pref->info.base;
5215 * Returns true if the given device is hidden
5217 bool
5218 prefs_is_capture_device_hidden(const char *name)
5220 char *tok, *devices;
5221 size_t len;
5223 if (prefs.capture_devices_hide && name) {
5224 devices = g_strdup (prefs.capture_devices_hide);
5225 len = strlen (name);
5226 for (tok = strtok (devices, ","); tok; tok = strtok(NULL, ",")) {
5227 if (strlen (tok) == len && strcmp (name, tok) == 0) {
5228 g_free (devices);
5229 return true;
5232 g_free (devices);
5235 return false;
5239 * Returns true if the given column is visible (not hidden)
5241 static bool
5242 prefs_is_column_visible(const char *cols_hidden, int col)
5244 char *tok, *cols, *p;
5245 int cidx;
5248 * Do we have a list of hidden columns?
5250 if (cols_hidden) {
5252 * Yes - check the column against each of the ones in the
5253 * list.
5255 cols = g_strdup(cols_hidden);
5256 for (tok = strtok(cols, ","); tok; tok = strtok(NULL, ",")) {
5257 tok = g_strstrip(tok);
5259 cidx = (int)strtol(tok, &p, 10);
5260 if (p == tok || *p != '\0') {
5261 continue;
5263 if (cidx != col) {
5264 continue;
5267 * OK, they match, so it's one of the hidden fields,
5268 * hence not visible.
5270 g_free(cols);
5271 return false;
5273 g_free(cols);
5277 * No - either there are no hidden columns or this isn't one
5278 * of them - so it is visible.
5280 return true;
5284 * Returns true if the given column is visible (not hidden)
5286 static bool
5287 prefs_is_column_fmt_visible(const char *cols_hidden, fmt_data *cfmt)
5289 char *tok, *cols;
5290 fmt_data cfmt_hidden;
5293 * Do we have a list of hidden columns?
5295 if (cols_hidden) {
5297 * Yes - check the column against each of the ones in the
5298 * list.
5300 cols = g_strdup(cols_hidden);
5301 for (tok = strtok(cols, ","); tok; tok = strtok(NULL, ",")) {
5302 tok = g_strstrip(tok);
5305 * Parse this column format.
5307 if (!parse_column_format(&cfmt_hidden, tok)) {
5309 * It's not valid; ignore it.
5311 continue;
5315 * Does it match the column?
5317 if (cfmt->fmt != cfmt_hidden.fmt) {
5318 /* No. */
5319 g_free(cfmt_hidden.custom_fields);
5320 cfmt_hidden.custom_fields = NULL;
5321 continue;
5323 if (cfmt->fmt == COL_CUSTOM) {
5325 * A custom column has to have the same custom field
5326 * and occurrence.
5328 if (cfmt_hidden.custom_fields && cfmt->custom_fields) {
5329 if (strcmp(cfmt->custom_fields,
5330 cfmt_hidden.custom_fields) != 0) {
5331 /* Different fields. */
5332 g_free(cfmt_hidden.custom_fields);
5333 cfmt_hidden.custom_fields = NULL;
5334 continue;
5336 if (cfmt->custom_occurrence != cfmt_hidden.custom_occurrence) {
5337 /* Different occurrences settings. */
5338 g_free(cfmt_hidden.custom_fields);
5339 cfmt_hidden.custom_fields = NULL;
5340 continue;
5346 * OK, they match, so it's one of the hidden fields,
5347 * hence not visible.
5349 g_free(cfmt_hidden.custom_fields);
5350 g_free(cols);
5351 return false;
5353 g_free(cols);
5357 * No - either there are no hidden columns or this isn't one
5358 * of them - so it is visible.
5360 return true;
5364 * Returns true if the given device should capture in monitor mode by default
5366 bool
5367 prefs_capture_device_monitor_mode(const char *name)
5369 char *tok, *devices;
5370 size_t len;
5372 if (prefs.capture_devices_monitor_mode && name) {
5373 devices = g_strdup (prefs.capture_devices_monitor_mode);
5374 len = strlen (name);
5375 for (tok = strtok (devices, ","); tok; tok = strtok(NULL, ",")) {
5376 if (strlen (tok) == len && strcmp (name, tok) == 0) {
5377 g_free (devices);
5378 return true;
5381 g_free (devices);
5384 return false;
5388 * Returns true if the user has marked this column as visible
5390 bool
5391 prefs_capture_options_dialog_column_is_visible(const char *column)
5393 GList *curr;
5394 char *col;
5396 for (curr = g_list_first(prefs.capture_columns); curr; curr = g_list_next(curr)) {
5397 col = (char *)curr->data;
5398 if (col && (g_ascii_strcasecmp(col, column) == 0)) {
5399 return true;
5402 return false;
5405 bool
5406 prefs_has_layout_pane_content (layout_pane_content_e layout_pane_content)
5408 return ((prefs.gui_layout_content_1 == layout_pane_content) ||
5409 (prefs.gui_layout_content_2 == layout_pane_content) ||
5410 (prefs.gui_layout_content_3 == layout_pane_content));
5413 #define PRS_GUI_FILTER_LABEL "gui.filter_expressions.label"
5414 #define PRS_GUI_FILTER_EXPR "gui.filter_expressions.expr"
5415 #define PRS_GUI_FILTER_ENABLED "gui.filter_expressions.enabled"
5418 * Extract the red, green, and blue components of a 24-bit RGB value
5419 * and convert them from [0,255] to [0,65535].
5421 #define RED_COMPONENT(x) (uint16_t) (((((x) >> 16) & 0xff) * 65535 / 255))
5422 #define GREEN_COMPONENT(x) (uint16_t) (((((x) >> 8) & 0xff) * 65535 / 255))
5423 #define BLUE_COMPONENT(x) (uint16_t) ( (((x) & 0xff) * 65535 / 255))
5425 char
5426 string_to_name_resolve(const char *string, e_addr_resolve *name_resolve)
5428 char c;
5430 memset(name_resolve, 0, sizeof(e_addr_resolve));
5431 while ((c = *string++) != '\0') {
5432 switch (c) {
5433 case 'g':
5434 name_resolve->maxmind_geoip = true;
5435 break;
5436 case 'm':
5437 name_resolve->mac_name = true;
5438 break;
5439 case 'n':
5440 name_resolve->network_name = true;
5441 break;
5442 case 'N':
5443 name_resolve->use_external_net_name_resolver = true;
5444 break;
5445 case 't':
5446 name_resolve->transport_name = true;
5447 break;
5448 case 'd':
5449 name_resolve->dns_pkt_addr_resolution = true;
5450 break;
5451 case 's':
5452 name_resolve->handshake_sni_addr_resolution = true;
5453 break;
5454 case 'v':
5455 name_resolve->vlan_name = true;
5456 break;
5457 default:
5459 * Unrecognized letter.
5461 return c;
5464 return '\0';
5467 static bool
5468 deprecated_heur_dissector_pref(char *pref_name, const char *value)
5470 struct heur_pref_name
5472 const char* pref_name;
5473 const char* short_name;
5474 bool more_dissectors; /* For multiple dissectors controlled by the same preference */
5477 struct heur_pref_name heur_prefs[] = {
5478 {"acn.heuristic_acn", "acn_udp", 0},
5479 {"bfcp.enable", "bfcp_tcp", 1},
5480 {"bfcp.enable", "bfcp_udp", 0},
5481 {"bt-dht.enable", "bittorrent_dht_udp", 0},
5482 {"bt-utp.enable", "bt_utp_udp", 0},
5483 {"cattp.enable", "cattp_udp", 0},
5484 {"cfp.enable", "fp_eth", 0},
5485 {"dicom.heuristic", "dicom_tcp", 0},
5486 {"dnp3.heuristics", "dnp3_tcp", 1},
5487 {"dnp3.heuristics", "dnp3_udp", 0},
5488 {"dvb-s2_modeadapt.enable", "dvb_s2_udp", 0},
5489 {"esl.enable", "esl_eth", 0},
5490 {"fp.udp_heur", "fp_udp", 0},
5491 {"gvsp.enable_heuristic", "gvsp_udp", 0},
5492 {"hdcp2.enable", "hdcp2_tcp", 0},
5493 {"hislip.enable_heuristic", "hislip_tcp", 0},
5494 {"infiniband.dissect_eoib", "mellanox_eoib", 1},
5495 {"infiniband.identify_payload", "eth_over_ib", 0},
5496 {"jxta.udp.heuristic", "jxta_udp", 0},
5497 {"jxta.tcp.heuristic", "jxta_tcp", 0},
5498 {"jxta.sctp.heuristic", "jxta_sctp", 0},
5499 {"mac-lte.heuristic_mac_lte_over_udp", "mac_lte_udp", 0},
5500 {"mbim.bulk_heuristic", "mbim_usb_bulk", 0},
5501 {"norm.heuristic_norm", "rmt_norm_udp", 0},
5502 {"openflow.heuristic", "openflow_tcp", 0},
5503 {"pdcp-lte.heuristic_pdcp_lte_over_udp", "pdcp_lte_udp", 0},
5504 {"rlc.heuristic_rlc_over_udp", "rlc_udp", 0},
5505 {"rlc-lte.heuristic_rlc_lte_over_udp", "rlc_lte_udp", 0},
5506 {"rtcp.heuristic_rtcp", "rtcp_udp", 1},
5507 {"rtcp.heuristic_rtcp", "rtcp_stun", 0},
5508 {"rtp.heuristic_rtp", "rtp_udp", 1},
5509 {"rtp.heuristic_rtp", "rtp_stun", 0},
5510 {"teredo.heuristic_teredo", "teredo_udp", 0},
5511 {"vssmonitoring.use_heuristics", "vssmonitoring_eth", 0},
5512 {"xml.heuristic", "xml_http", 1},
5513 {"xml.heuristic", "xml_sip", 1},
5514 {"xml.heuristic", "xml_media", 0},
5515 {"xml.heuristic_tcp", "xml_tcp", 0},
5516 {"xml.heuristic_udp", "xml_udp", 0},
5519 unsigned int i;
5520 heur_dtbl_entry_t* heuristic;
5523 for (i = 0; i < array_length(heur_prefs); i++)
5525 if (strcmp(pref_name, heur_prefs[i].pref_name) == 0)
5527 heuristic = find_heur_dissector_by_unique_short_name(heur_prefs[i].short_name);
5528 if (heuristic != NULL) {
5529 heuristic->enabled = ((g_ascii_strcasecmp(value, "true") == 0) ? true : false);
5532 if (!heur_prefs[i].more_dissectors)
5533 return true;
5538 return false;
5541 static bool
5542 deprecated_enable_dissector_pref(char *pref_name, const char *value)
5544 struct dissector_pref_name
5546 const char* pref_name;
5547 const char* short_name;
5550 struct dissector_pref_name dissector_prefs[] = {
5551 {"transum.tsumenabled", "TRANSUM"},
5552 {"snort.enable_snort_dissector", "Snort"},
5553 {"prp.enable", "PRP"},
5556 unsigned int i;
5557 int proto_id;
5559 for (i = 0; i < array_length(dissector_prefs); i++)
5561 if (strcmp(pref_name, dissector_prefs[i].pref_name) == 0)
5563 proto_id = proto_get_id_by_short_name(dissector_prefs[i].short_name);
5564 if (proto_id >= 0)
5565 proto_set_decoding(proto_id, ((g_ascii_strcasecmp(value, "true") == 0) ? true : false));
5566 return true;
5570 return false;
5573 static bool
5574 deprecated_port_pref(char *pref_name, const char *value)
5576 struct port_pref_name
5578 const char* pref_name;
5579 const char* module_name; /* the protocol filter name */
5580 const char* table_name;
5581 unsigned base;
5584 struct obsolete_pref_name
5586 const char* pref_name;
5589 /* For now this is only supporting TCP/UDP port and RTP payload
5590 * types dissector preferences, which are assumed to be decimal */
5591 /* module_name is the filter name of the destination port preference,
5592 * which is usually the same as the original module but not
5593 * necessarily (e.g., if the preference is for what is now a PINO.)
5594 * XXX: Most of these were changed pre-2.0. Can we end support
5595 * for migrating legacy preferences at some point?
5597 struct port_pref_name port_prefs[] = {
5598 /* TCP */
5599 {"cmp.tcp_alternate_port", "cmp", "tcp.port", 10},
5600 {"h248.tcp_port", "h248", "tcp.port", 10},
5601 {"cops.tcp.cops_port", "cops", "tcp.port", 10},
5602 {"dhcpfo.tcp_port", "dhcpfo", "tcp.port", 10},
5603 {"enttec.tcp_port", "enttec", "tcp.port", 10},
5604 {"forces.tcp_alternate_port", "forces", "tcp.port", 10},
5605 {"ged125.tcp_port", "ged125", "tcp.port", 10},
5606 {"hpfeeds.dissector_port", "hpfeeds", "tcp.port", 10},
5607 {"lsc.port", "lsc", "tcp.port", 10},
5608 {"megaco.tcp.txt_port", "megaco", "tcp.port", 10},
5609 {"netsync.tcp_port", "netsync", "tcp.port", 10},
5610 {"osi.tpkt_port", "osi", "tcp.port", 10},
5611 {"rsync.tcp_port", "rsync", "tcp.port", 10},
5612 {"sametime.tcp_port", "sametime", "tcp.port", 10},
5613 {"sigcomp.tcp.port2", "sigcomp", "tcp.port", 10},
5614 {"synphasor.tcp_port", "synphasor", "tcp.port", 10},
5615 {"tipc.alternate_port", "tipc", "tcp.port", 10},
5616 {"vnc.alternate_port", "vnc", "tcp.port", 10},
5617 {"scop.port", "scop", "tcp.port", 10},
5618 {"scop.port_secure", "scop", "tcp.port", 10},
5619 {"tpncp.tcp.trunkpack_port", "tpncp", "tcp.port", 10},
5620 /* UDP */
5621 {"h248.udp_port", "h248", "udp.port", 10},
5622 {"actrace.udp_port", "actrace", "udp.port", 10},
5623 {"brp.port", "brp", "udp.port", 10},
5624 {"bvlc.additional_udp_port", "bvlc", "udp.port", 10},
5625 {"capwap.udp.port.control", "capwap", "udp.port", 10},
5626 {"capwap.udp.port.data", "capwap", "udp.port", 10},
5627 {"coap.udp_port", "coap", "udp.port", 10},
5628 {"enttec.udp_port", "enttec", "udp.port", 10},
5629 {"forces.udp_alternate_port", "forces", "udp.port", 10},
5630 {"ldss.udp_port", "ldss", "udp.port", 10},
5631 {"lmp.udp_port", "lmp", "udp.port", 10},
5632 {"ltp.port", "ltp", "udp.port", 10},
5633 {"lwres.udp.lwres_port", "lwres", "udp.port", 10},
5634 {"megaco.udp.txt_port", "megaco", "udp.port", 10},
5635 {"pfcp.port_pfcp", "pfcp", "udp.port", 10},
5636 {"pgm.udp.encap_ucast_port", "pgm", "udp.port", 10},
5637 {"pgm.udp.encap_mcast_port", "pgm", "udp.port", 10},
5638 {"quic.udp.quic.port", "quic", "udp.port", 10},
5639 {"quic.udp.quics.port", "quic", "udp.port", 10},
5640 {"radius.alternate_port", "radius", "udp.port", 10},
5641 {"rdt.default_udp_port", "rdt", "udp.port", 10},
5642 {"alc.default.udp_port", "alc", "udp.port", 10},
5643 {"sigcomp.udp.port2", "sigcomp", "udp.port", 10},
5644 {"synphasor.udp_port", "synphasor", "udp.port", 10},
5645 {"tdmop.udpport", "tdmop", "udp.port", 10},
5646 {"uaudp.port1", "uaudp", "udp.port", 10},
5647 {"uaudp.port2", "uaudp", "udp.port", 10},
5648 {"uaudp.port3", "uaudp", "udp.port", 10},
5649 {"uaudp.port4", "uaudp", "udp.port", 10},
5650 {"uhd.dissector_port", "uhd", "udp.port", 10},
5651 {"vrt.dissector_port", "vrt", "udp.port", 10},
5652 {"tpncp.udp.trunkpack_port", "tpncp", "udp.port", 10},
5653 /* SCTP */
5654 {"hnbap.port", "hnbap", "sctp.port", 10},
5655 {"m2pa.port", "m2pa", "sctp.port", 10},
5656 {"megaco.sctp.txt_port", "megaco", "sctp.port", 10},
5657 {"rua.port", "rua", "sctp.port", 10},
5658 /* SCTP PPI */
5659 {"lapd.sctp_payload_protocol_identifier", "lapd", "sctp.ppi", 10},
5660 /* SCCP SSN */
5661 {"ranap.sccp_ssn", "ranap", "sccp.ssn", 10},
5664 struct port_pref_name port_range_prefs[] = {
5665 /* TCP */
5666 {"couchbase.tcp.ports", "couchbase", "tcp.port", 10},
5667 {"gsm_ipa.tcp_ports", "gsm_ipa", "tcp.port", 10},
5668 {"kafka.tcp.ports", "kafka", "tcp.port", 10},
5669 {"kt.tcp.ports", "kt", "tcp.port", 10},
5670 {"memcache.tcp.ports", "memcache", "tcp.port", 10},
5671 {"mrcpv2.tcp.port_range", "mrcpv2", "tcp.port", 10},
5672 {"pdu_transport.ports.tcp", "pdu_transport", "tcp.port", 10},
5673 {"rtsp.tcp.port_range", "rtsp", "tcp.port", 10},
5674 {"sip.tcp.ports", "sip", "tcp.port", 10},
5675 {"someip.ports.tcp", "someip", "tcp.port", 10},
5676 {"tds.tcp_ports", "tds", "tcp.port", 10},
5677 {"tpkt.tcp.ports", "tpkt", "tcp.port", 10},
5678 {"uma.tcp.ports", "uma", "tcp.port", 10},
5679 /* UDP */
5680 {"aruba_erm.udp.ports", "arubs_erm", "udp.port", 10},
5681 {"diameter.udp.ports", "diameter", "udp.port", 10},
5682 {"dmp.udp_ports", "dmp", "udp.port", 10},
5683 {"dns.udp.ports", "dns", "udp.port", 10},
5684 {"gsm_ipa.udp_ports", "gsm_ipa", "udp.port", 10},
5685 {"hcrt.dissector_udp_port", "hcrt", "udp.port", 10},
5686 {"memcache.udp.ports", "memcache", "udp.port", 10},
5687 {"nb_rtpmux.udp_ports", "nb_rtpmux", "udp.port", 10},
5688 {"gprs-ns.udp.ports", "gprs-ns", "udp.port", 10},
5689 {"p_mul.udp_ports", "p_mul", "udp.port", 10},
5690 {"pdu_transport.ports.udp", "pdu_transport", "udp.port", 10},
5691 {"radius.ports", "radius", "udp.port", 10},
5692 {"sflow.ports", "sflow", "udp.port", 10},
5693 {"someip.ports.udp", "someip", "udp.port", 10},
5694 {"sscop.udp.ports", "sscop", "udp.port", 10},
5695 {"tftp.udp_ports", "tftp", "udp.port", 10},
5696 {"tipc.udp.ports", "tipc", "udp.port", 10},
5697 /* RTP */
5698 {"amr.dynamic.payload.type", "amr", "rtp.pt", 10},
5699 {"amr.wb.dynamic.payload.type", "amr_wb", "rtp.pt", 10},
5700 {"dvb-s2_modeadapt.dynamic.payload.type", "dvb-s2_modeadapt", "rtp.pt", 10},
5701 {"evs.dynamic.payload.type", "evs", "rtp.pt", 10},
5702 {"h263p.dynamic.payload.type", "h263p", "rtp.pt", 10},
5703 {"h264.dynamic.payload.type", "h264", "rtp.pt", 10},
5704 {"h265.dynamic.payload.type", "h265", "rtp.pt", 10},
5705 {"ismacryp.dynamic.payload.type", "ismacryp", "rtp.pt", 10},
5706 {"iuup.dynamic.payload.type", "iuup", "rtp.pt", 10},
5707 {"lapd.rtp_payload_type", "lapd", "rtp.pt", 10},
5708 {"mp4ves.dynamic.payload.type", "mp4ves", "rtp.pt", 10},
5709 {"mtp2.rtp_payload_type", "mtp2", "rtp.pt", 10},
5710 {"opus.dynamic.payload.type", "opus", "rtp.pt", 10},
5711 {"rtp.rfc2198_payload_type", "rtp_rfc2198", "rtp.pt", 10},
5712 {"rtpevent.event_payload_type_value", "rtpevent", "rtp.pt", 10},
5713 {"rtpevent.cisco_nse_payload_type_value", "rtpevent", "rtp.pt", 10},
5714 {"rtpmidi.midi_payload_type_value", "rtpmidi", "rtp.pt", 10},
5715 {"vp8.dynamic.payload.type", "vp8", "rtp.pt", 10},
5716 /* SCTP */
5717 {"diameter.sctp.ports", "diameter", "sctp.port", 10},
5718 {"sgsap.sctp_ports", "sgsap", "sctp.port", 10},
5719 /* SCCP SSN */
5720 {"pcap.ssn", "pcap", "sccp.ssn", 10},
5723 /* These are subdissectors of TPKT/OSITP that used to have a
5724 TCP port preference even though they were never
5725 directly on TCP. Convert them to use Decode As
5726 with the TPKT dissector handle */
5727 struct port_pref_name tpkt_subdissector_port_prefs[] = {
5728 {"dap.tcp.port", "dap", "tcp.port", 10},
5729 {"disp.tcp.port", "disp", "tcp.port", 10},
5730 {"dop.tcp.port", "dop", "tcp.port", 10},
5731 {"dsp.tcp.port", "dsp", "tcp.port", 10},
5732 {"p1.tcp.port", "p1", "tcp.port", 10},
5733 {"p7.tcp.port", "p7", "tcp.port", 10},
5734 {"rdp.tcp.port", "rdp", "tcp.port", 10},
5737 /* These are obsolete preferences from the dissectors' view,
5738 (typically because of a switch from a single value to a
5739 range value) but the name of the preference conflicts
5740 with the generated preference name from the dissector table.
5741 Don't allow the obsolete preference through to be handled */
5742 struct obsolete_pref_name obsolete_prefs[] = {
5743 {"diameter.tcp.port"},
5744 {"kafka.tcp.port"},
5745 {"mrcpv2.tcp.port"},
5746 {"rtsp.tcp.port"},
5747 {"sip.tcp.port"},
5748 {"t38.tcp.port"},
5751 unsigned int i;
5752 unsigned uval;
5753 dissector_table_t sub_dissectors;
5754 dissector_handle_t handle, tpkt_handle;
5755 module_t *module;
5756 pref_t *pref;
5758 static bool sanity_checked;
5759 if (!sanity_checked) {
5760 sanity_checked = true;
5761 for (i = 0; i < G_N_ELEMENTS(port_prefs); i++) {
5762 module = prefs_find_module(port_prefs[i].module_name);
5763 if (!module) {
5764 ws_warning("Deprecated ports pref check - module '%s' not found", port_prefs[i].module_name);
5765 continue;
5767 pref = prefs_find_preference(module, port_prefs[i].table_name);
5768 if (!pref) {
5769 ws_warning("Deprecated ports pref '%s.%s' not found", module->name, port_prefs[i].table_name);
5770 continue;
5772 if (pref->type != PREF_DECODE_AS_RANGE) {
5773 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));
5778 for (i = 0; i < G_N_ELEMENTS(port_prefs); i++) {
5779 if (strcmp(pref_name, port_prefs[i].pref_name) == 0) {
5780 if (!ws_basestrtou32(value, NULL, &uval, port_prefs[i].base))
5781 return false; /* number was bad */
5783 module = prefs_find_module(port_prefs[i].module_name);
5784 pref = prefs_find_preference(module, port_prefs[i].table_name);
5785 if (pref != NULL) {
5786 module->prefs_changed_flags |= prefs_get_effect_flags(pref);
5787 if (pref->type == PREF_DECODE_AS_RANGE) {
5788 // The legacy preference was a port number, but the new
5789 // preference is a port range. Add to existing range.
5790 if (uval) {
5791 prefs_range_add_value(pref, uval);
5796 /* If the value is zero, it wouldn't add to the Decode As tables */
5797 if (uval != 0)
5799 sub_dissectors = find_dissector_table(port_prefs[i].table_name);
5800 if (sub_dissectors != NULL) {
5801 handle = dissector_table_get_dissector_handle(sub_dissectors, module->title);
5802 if (handle != NULL) {
5803 dissector_change_uint(port_prefs[i].table_name, uval, handle);
5804 decode_build_reset_list(port_prefs[i].table_name, dissector_table_get_type(sub_dissectors), GUINT_TO_POINTER(uval), NULL, NULL);
5809 return true;
5813 for (i = 0; i < array_length(port_range_prefs); i++)
5815 if (strcmp(pref_name, port_range_prefs[i].pref_name) == 0)
5817 uint32_t range_i, range_j;
5819 sub_dissectors = find_dissector_table(port_range_prefs[i].table_name);
5820 if (sub_dissectors != NULL) {
5821 switch (dissector_table_get_type(sub_dissectors)) {
5822 case FT_UINT8:
5823 case FT_UINT16:
5824 case FT_UINT24:
5825 case FT_UINT32:
5826 break;
5828 default:
5829 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));
5830 ws_assert_not_reached();
5833 module = prefs_find_module(port_range_prefs[i].module_name);
5834 pref = prefs_find_preference(module, port_range_prefs[i].table_name);
5835 if (pref != NULL)
5837 if (!prefs_set_range_value_work(pref, value, true, &module->prefs_changed_flags))
5839 return false; /* number was bad */
5842 handle = dissector_table_get_dissector_handle(sub_dissectors, module->title);
5843 if (handle != NULL) {
5845 for (range_i = 0; range_i < (*pref->varp.range)->nranges; range_i++) {
5846 for (range_j = (*pref->varp.range)->ranges[range_i].low; range_j < (*pref->varp.range)->ranges[range_i].high; range_j++) {
5847 dissector_change_uint(port_range_prefs[i].table_name, range_j, handle);
5848 decode_build_reset_list(port_range_prefs[i].table_name, dissector_table_get_type(sub_dissectors), GUINT_TO_POINTER(range_j), NULL, NULL);
5851 dissector_change_uint(port_range_prefs[i].table_name, (*pref->varp.range)->ranges[range_i].high, handle);
5852 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);
5858 return true;
5862 for (i = 0; i < array_length(tpkt_subdissector_port_prefs); i++)
5864 if (strcmp(pref_name, tpkt_subdissector_port_prefs[i].pref_name) == 0)
5866 /* XXX - give an error if it doesn't fit in a unsigned? */
5867 if (!ws_basestrtou32(value, NULL, &uval, tpkt_subdissector_port_prefs[i].base))
5868 return false; /* number was bad */
5870 /* If the value is 0 or 102 (default TPKT port), don't add to the Decode As tables */
5871 if ((uval != 0) && (uval != 102))
5873 tpkt_handle = find_dissector("tpkt");
5874 if (tpkt_handle != NULL) {
5875 dissector_change_uint(tpkt_subdissector_port_prefs[i].table_name, uval, tpkt_handle);
5879 return true;
5883 for (i = 0; i < array_length(obsolete_prefs); i++)
5885 if (strcmp(pref_name, obsolete_prefs[i].pref_name) == 0)
5887 /* Just ignore the preference */
5888 return true;
5891 return false;
5894 static prefs_set_pref_e
5895 set_pref(char *pref_name, const char *value, void *private_data,
5896 bool return_range_errors)
5898 unsigned cval;
5899 unsigned uval;
5900 bool bval;
5901 int enum_val;
5902 char *dotp, *last_dotp;
5903 static char *filter_label = NULL;
5904 static bool filter_enabled = false;
5905 module_t *module, *containing_module, *target_module;
5906 pref_t *pref;
5907 int type;
5908 bool converted_pref = false;
5910 target_module = (module_t*)private_data;
5912 //The PRS_GUI field names are here for backwards compatibility
5913 //display filters have been converted to a UAT.
5914 if (strcmp(pref_name, PRS_GUI_FILTER_LABEL) == 0) {
5915 /* Assume that PRS_GUI_FILTER_EXPR follows this preference. In case of
5916 * malicious preference files, free the previous value to limit the size
5917 * of leaked memory. */
5918 g_free(filter_label);
5919 filter_label = g_strdup(value);
5920 } else if (strcmp(pref_name, PRS_GUI_FILTER_ENABLED) == 0) {
5921 filter_enabled = (strcmp(value, "TRUE") == 0) ? true : false;
5922 } else if (strcmp(pref_name, PRS_GUI_FILTER_EXPR) == 0) {
5923 /* Comments not supported for "old" preference style */
5924 filter_expression_new(filter_label, value, "", filter_enabled);
5925 g_free(filter_label);
5926 filter_label = NULL;
5927 /* Remember to save the new UAT to file. */
5928 prefs.filter_expressions_old = true;
5929 } else if (strcmp(pref_name, "gui.version_in_start_page") == 0) {
5930 /* Convert deprecated value to closest current equivalent */
5931 if (g_ascii_strcasecmp(value, "true") == 0) {
5932 prefs.gui_version_placement = version_both;
5933 } else {
5934 prefs.gui_version_placement = version_neither;
5936 } else if (strcmp(pref_name, "name_resolve") == 0 ||
5937 strcmp(pref_name, "capture.name_resolve") == 0) {
5939 * Handle the deprecated name resolution options.
5941 * "TRUE" and "FALSE", for backwards compatibility, are synonyms for
5942 * RESOLV_ALL and RESOLV_NONE.
5944 * Otherwise, we treat it as a list of name types we want to resolve.
5946 if (g_ascii_strcasecmp(value, "true") == 0) {
5947 gbl_resolv_flags.mac_name = true;
5948 gbl_resolv_flags.network_name = true;
5949 gbl_resolv_flags.transport_name = true;
5951 else if (g_ascii_strcasecmp(value, "false") == 0) {
5952 disable_name_resolution();
5954 else {
5955 /* start out with none set */
5956 disable_name_resolution();
5957 if (string_to_name_resolve(value, &gbl_resolv_flags) != '\0')
5958 return PREFS_SET_SYNTAX_ERR;
5960 } else if (deprecated_heur_dissector_pref(pref_name, value)) {
5961 /* Handled within deprecated_heur_dissector_pref() if found */
5962 } else if (deprecated_enable_dissector_pref(pref_name, value)) {
5963 /* Handled within deprecated_enable_dissector_pref() if found */
5964 } else if (deprecated_port_pref(pref_name, value)) {
5965 /* Handled within deprecated_port_pref() if found */
5966 } else if (strcmp(pref_name, "console.log.level") == 0) {
5967 /* Handled on the command line within ws_log_parse_args() */
5968 return PREFS_SET_OK;
5969 } else {
5970 /* Handle deprecated "global" options that don't have a module
5971 * associated with them
5973 if ((strcmp(pref_name, "name_resolve_concurrency") == 0) ||
5974 (strcmp(pref_name, "name_resolve_load_smi_modules") == 0) ||
5975 (strcmp(pref_name, "name_resolve_suppress_smi_errors") == 0)) {
5976 module = nameres_module;
5977 dotp = pref_name;
5978 } else {
5979 /* To which module does this preference belong? */
5980 module = NULL;
5981 last_dotp = pref_name;
5982 while (!module) {
5983 dotp = strchr(last_dotp, '.');
5984 if (dotp == NULL) {
5985 /* Either there's no such module, or no module was specified.
5986 In either case, that means there's no such preference. */
5987 return PREFS_SET_NO_SUCH_PREF;
5989 *dotp = '\0'; /* separate module and preference name */
5990 module = prefs_find_module(pref_name);
5993 * XXX - "Diameter" rather than "diameter" was used in earlier
5994 * versions of Wireshark; if we didn't find the module, and its name
5995 * was "Diameter", look for "diameter" instead.
5997 * In addition, the BEEP protocol used to be the BXXP protocol,
5998 * so if we didn't find the module, and its name was "bxxp",
5999 * look for "beep" instead.
6001 * Also, the preferences for GTP v0 and v1 were combined under
6002 * a single "gtp" heading, and the preferences for SMPP were
6003 * moved to "smpp-gsm-sms" and then moved to "gsm-sms-ud".
6004 * However, SMPP now has its own preferences, so we just map
6005 * "smpp-gsm-sms" to "gsm-sms-ud", and then handle SMPP below.
6007 * We also renamed "dcp" to "dccp", "x.25" to "x25", "x411" to "p1"
6008 * and "nsip" to "gprs_ns".
6010 * The SynOptics Network Management Protocol (SONMP) is now known by
6011 * its modern name, the Nortel Discovery Protocol (NDP).
6013 if (module == NULL) {
6015 * See if there's a backwards-compatibility name
6016 * that maps to this module.
6018 module = prefs_find_module_alias(pref_name);
6019 if (module == NULL) {
6021 * There's no alias for the module; see if the
6022 * module name matches any protocol aliases.
6024 header_field_info *hfinfo = proto_registrar_get_byalias(pref_name);
6025 if (hfinfo) {
6026 module = (module_t *) wmem_tree_lookup_string(prefs_modules, hfinfo->abbrev, WMEM_TREE_STRING_NOCASE);
6029 if (module == NULL) {
6031 * There aren't any aliases. Was the module
6032 * removed rather than renamed?
6034 if (strcmp(pref_name, "etheric") == 0 ||
6035 strcmp(pref_name, "isup_thin") == 0) {
6037 * The dissectors for these protocols were
6038 * removed as obsolete on 2009-07-70 in change
6039 * 739bfc6ff035583abb9434e0e988048de38a8d9a.
6041 return PREFS_SET_OBSOLETE;
6044 if (module) {
6045 converted_pref = true;
6046 prefs.unknown_prefs = true;
6049 *dotp = '.'; /* put the preference string back */
6050 dotp++; /* skip past separator to preference name */
6051 last_dotp = dotp;
6055 /* The pref is located in the module or a submodule.
6056 * Assume module, then search for a submodule holding the pref. */
6057 containing_module = module;
6058 pref = prefs_find_preference_with_submodule(module, dotp, &containing_module);
6060 if (pref == NULL) {
6061 prefs.unknown_prefs = true;
6063 /* "gui" prefix was added to column preferences for better organization
6064 * within the preferences file
6066 if (module == gui_column_module) {
6067 /* While this has a subtree, there is no apply callback, so no
6068 * need to use prefs_find_preference_with_submodule to update
6069 * containing_module. It would not be useful. */
6070 pref = prefs_find_preference(module, pref_name);
6072 else if (strcmp(module->name, "mgcp") == 0) {
6074 * XXX - "mgcp.display raw text toggle" and "mgcp.display dissect tree"
6075 * rather than "mgcp.display_raw_text" and "mgcp.display_dissect_tree"
6076 * were used in earlier versions of Wireshark; if we didn't find the
6077 * preference, it was an MGCP preference, and its name was
6078 * "display raw text toggle" or "display dissect tree", look for
6079 * "display_raw_text" or "display_dissect_tree" instead.
6081 * "mgcp.tcp.port" and "mgcp.udp.port" are harder to handle, as both
6082 * the gateway and callagent ports were given those names; we interpret
6083 * the first as "mgcp.{tcp,udp}.gateway_port" and the second as
6084 * "mgcp.{tcp,udp}.callagent_port", as that's the order in which
6085 * they were registered by the MCCP dissector and thus that's the
6086 * order in which they were written to the preferences file. (If
6087 * we're not reading the preferences file, but are handling stuff
6088 * from a "-o" command-line option, we have no clue which the user
6089 * had in mind - they should have used "mgcp.{tcp,udp}.gateway_port"
6090 * or "mgcp.{tcp,udp}.callagent_port" instead.)
6092 if (strcmp(dotp, "display raw text toggle") == 0)
6093 pref = prefs_find_preference(module, "display_raw_text");
6094 else if (strcmp(dotp, "display dissect tree") == 0)
6095 pref = prefs_find_preference(module, "display_dissect_tree");
6096 else if (strcmp(dotp, "tcp.port") == 0) {
6097 mgcp_tcp_port_count++;
6098 if (mgcp_tcp_port_count == 1) {
6099 /* It's the first one */
6100 pref = prefs_find_preference(module, "tcp.gateway_port");
6101 } else if (mgcp_tcp_port_count == 2) {
6102 /* It's the second one */
6103 pref = prefs_find_preference(module, "tcp.callagent_port");
6105 /* Otherwise it's from the command line, and we don't bother
6106 mapping it. */
6107 } else if (strcmp(dotp, "udp.port") == 0) {
6108 mgcp_udp_port_count++;
6109 if (mgcp_udp_port_count == 1) {
6110 /* It's the first one */
6111 pref = prefs_find_preference(module, "udp.gateway_port");
6112 } else if (mgcp_udp_port_count == 2) {
6113 /* It's the second one */
6114 pref = prefs_find_preference(module, "udp.callagent_port");
6116 /* Otherwise it's from the command line, and we don't bother
6117 mapping it. */
6119 } else if (strcmp(module->name, "smb") == 0) {
6120 /* Handle old names for SMB preferences. */
6121 if (strcmp(dotp, "smb.trans.reassembly") == 0)
6122 pref = prefs_find_preference(module, "trans_reassembly");
6123 else if (strcmp(dotp, "smb.dcerpc.reassembly") == 0)
6124 pref = prefs_find_preference(module, "dcerpc_reassembly");
6125 } else if (strcmp(module->name, "ndmp") == 0) {
6126 /* Handle old names for NDMP preferences. */
6127 if (strcmp(dotp, "ndmp.desegment") == 0)
6128 pref = prefs_find_preference(module, "desegment");
6129 } else if (strcmp(module->name, "diameter") == 0) {
6130 /* Handle old names for Diameter preferences. */
6131 if (strcmp(dotp, "diameter.desegment") == 0)
6132 pref = prefs_find_preference(module, "desegment");
6133 } else if (strcmp(module->name, "pcli") == 0) {
6134 /* Handle old names for PCLI preferences. */
6135 if (strcmp(dotp, "pcli.udp_port") == 0)
6136 pref = prefs_find_preference(module, "udp_port");
6137 } else if (strcmp(module->name, "artnet") == 0) {
6138 /* Handle old names for ARTNET preferences. */
6139 if (strcmp(dotp, "artnet.udp_port") == 0)
6140 pref = prefs_find_preference(module, "udp_port");
6141 } else if (strcmp(module->name, "mapi") == 0) {
6142 /* Handle old names for MAPI preferences. */
6143 if (strcmp(dotp, "mapi_decrypt") == 0)
6144 pref = prefs_find_preference(module, "decrypt");
6145 } else if (strcmp(module->name, "fc") == 0) {
6146 /* Handle old names for Fibre Channel preferences. */
6147 if (strcmp(dotp, "reassemble_fc") == 0)
6148 pref = prefs_find_preference(module, "reassemble");
6149 else if (strcmp(dotp, "fc_max_frame_size") == 0)
6150 pref = prefs_find_preference(module, "max_frame_size");
6151 } else if (strcmp(module->name, "fcip") == 0) {
6152 /* Handle old names for Fibre Channel-over-IP preferences. */
6153 if (strcmp(dotp, "desegment_fcip_messages") == 0)
6154 pref = prefs_find_preference(module, "desegment");
6155 else if (strcmp(dotp, "fcip_port") == 0)
6156 pref = prefs_find_preference(module, "target_port");
6157 } else if (strcmp(module->name, "gtp") == 0) {
6158 /* Handle old names for GTP preferences. */
6159 if (strcmp(dotp, "gtpv0_port") == 0)
6160 pref = prefs_find_preference(module, "v0_port");
6161 else if (strcmp(dotp, "gtpv1c_port") == 0)
6162 pref = prefs_find_preference(module, "v1c_port");
6163 else if (strcmp(dotp, "gtpv1u_port") == 0)
6164 pref = prefs_find_preference(module, "v1u_port");
6165 else if (strcmp(dotp, "gtp_dissect_tpdu") == 0)
6166 pref = prefs_find_preference(module, "dissect_tpdu");
6167 else if (strcmp(dotp, "gtpv0_dissect_cdr_as") == 0)
6168 pref = prefs_find_preference(module, "v0_dissect_cdr_as");
6169 else if (strcmp(dotp, "gtpv0_check_etsi") == 0)
6170 pref = prefs_find_preference(module, "v0_check_etsi");
6171 else if (strcmp(dotp, "gtpv1_check_etsi") == 0)
6172 pref = prefs_find_preference(module, "v1_check_etsi");
6173 } else if (strcmp(module->name, "ip") == 0) {
6174 /* Handle old names for IP preferences. */
6175 if (strcmp(dotp, "ip_summary_in_tree") == 0)
6176 pref = prefs_find_preference(module, "summary_in_tree");
6177 } else if (strcmp(module->name, "iscsi") == 0) {
6178 /* Handle old names for iSCSI preferences. */
6179 if (strcmp(dotp, "iscsi_port") == 0)
6180 pref = prefs_find_preference(module, "target_port");
6181 } else if (strcmp(module->name, "lmp") == 0) {
6182 /* Handle old names for LMP preferences. */
6183 if (strcmp(dotp, "lmp_version") == 0)
6184 pref = prefs_find_preference(module, "version");
6185 } else if (strcmp(module->name, "mtp3") == 0) {
6186 /* Handle old names for MTP3 preferences. */
6187 if (strcmp(dotp, "mtp3_standard") == 0)
6188 pref = prefs_find_preference(module, "standard");
6189 else if (strcmp(dotp, "net_addr_format") == 0)
6190 pref = prefs_find_preference(module, "addr_format");
6191 } else if (strcmp(module->name, "nlm") == 0) {
6192 /* Handle old names for NLM preferences. */
6193 if (strcmp(dotp, "nlm_msg_res_matching") == 0)
6194 pref = prefs_find_preference(module, "msg_res_matching");
6195 } else if (strcmp(module->name, "ppp") == 0) {
6196 /* Handle old names for PPP preferences. */
6197 if (strcmp(dotp, "ppp_fcs") == 0)
6198 pref = prefs_find_preference(module, "fcs_type");
6199 else if (strcmp(dotp, "ppp_vj") == 0)
6200 pref = prefs_find_preference(module, "decompress_vj");
6201 } else if (strcmp(module->name, "rsvp") == 0) {
6202 /* Handle old names for RSVP preferences. */
6203 if (strcmp(dotp, "rsvp_process_bundle") == 0)
6204 pref = prefs_find_preference(module, "process_bundle");
6205 } else if (strcmp(module->name, "tcp") == 0) {
6206 /* Handle old names for TCP preferences. */
6207 if (strcmp(dotp, "tcp_summary_in_tree") == 0)
6208 pref = prefs_find_preference(module, "summary_in_tree");
6209 else if (strcmp(dotp, "tcp_analyze_sequence_numbers") == 0)
6210 pref = prefs_find_preference(module, "analyze_sequence_numbers");
6211 else if (strcmp(dotp, "tcp_relative_sequence_numbers") == 0)
6212 pref = prefs_find_preference(module, "relative_sequence_numbers");
6213 else if (strcmp(dotp, "dissect_experimental_options_with_magic") == 0)
6214 pref = prefs_find_preference(module, "dissect_experimental_options_rfc6994");
6215 } else if (strcmp(module->name, "udp") == 0) {
6216 /* Handle old names for UDP preferences. */
6217 if (strcmp(dotp, "udp_summary_in_tree") == 0)
6218 pref = prefs_find_preference(module, "summary_in_tree");
6219 } else if (strcmp(module->name, "ndps") == 0) {
6220 /* Handle old names for NDPS preferences. */
6221 if (strcmp(dotp, "desegment_ndps") == 0)
6222 pref = prefs_find_preference(module, "desegment_tcp");
6223 } else if (strcmp(module->name, "http") == 0) {
6224 /* Handle old names for HTTP preferences. */
6225 if (strcmp(dotp, "desegment_http_headers") == 0)
6226 pref = prefs_find_preference(module, "desegment_headers");
6227 else if (strcmp(dotp, "desegment_http_body") == 0)
6228 pref = prefs_find_preference(module, "desegment_body");
6229 } else if (strcmp(module->name, "smpp") == 0) {
6230 /* Handle preferences that moved from SMPP. */
6231 module_t *new_module = prefs_find_module("gsm-sms-ud");
6232 if (new_module) {
6233 if (strcmp(dotp, "port_number_udh_means_wsp") == 0) {
6234 pref = prefs_find_preference(new_module, "port_number_udh_means_wsp");
6235 containing_module = new_module;
6236 } else if (strcmp(dotp, "try_dissect_1st_fragment") == 0) {
6237 pref = prefs_find_preference(new_module, "try_dissect_1st_fragment");
6238 containing_module = new_module;
6241 } else if (strcmp(module->name, "asn1") == 0) {
6242 /* Handle old generic ASN.1 preferences (it's not really a
6243 rename, as the new preferences support multiple ports,
6244 but we might as well copy them over). */
6245 if (strcmp(dotp, "tcp_port") == 0)
6246 pref = prefs_find_preference(module, "tcp_ports");
6247 else if (strcmp(dotp, "udp_port") == 0)
6248 pref = prefs_find_preference(module, "udp_ports");
6249 else if (strcmp(dotp, "sctp_port") == 0)
6250 pref = prefs_find_preference(module, "sctp_ports");
6251 } else if (strcmp(module->name, "llcgprs") == 0) {
6252 if (strcmp(dotp, "ignore_cipher_bit") == 0)
6253 pref = prefs_find_preference(module, "autodetect_cipher_bit");
6254 } else if (strcmp(module->name, "erf") == 0) {
6255 if (strcmp(dotp, "erfeth") == 0) {
6256 /* Handle the old "erfeth" preference; map it to the new
6257 "ethfcs" preference, and map the values to those for
6258 the new preference. */
6259 pref = prefs_find_preference(module, "ethfcs");
6260 if (strcmp(value, "ethfcs") == 0 || strcmp(value, "Ethernet with FCS") == 0)
6261 value = "TRUE";
6262 else if (strcmp(value, "eth") == 0 || strcmp(value, "Ethernet") == 0)
6263 value = "FALSE";
6264 else if (strcmp(value, "raw") == 0 || strcmp(value, "Raw data") == 0)
6265 value = "TRUE";
6266 } else if (strcmp(dotp, "erfatm") == 0) {
6267 /* Handle the old "erfatm" preference; map it to the new
6268 "aal5_type" preference, and map the values to those for
6269 the new preference. */
6270 pref = prefs_find_preference(module, "aal5_type");
6271 if (strcmp(value, "atm") == 0 || strcmp(value, "ATM") == 0)
6272 value = "guess";
6273 else if (strcmp(value, "llc") == 0 || strcmp(value, "LLC") == 0)
6274 value = "llc";
6275 else if (strcmp(value, "raw") == 0 || strcmp(value, "Raw data") == 0)
6276 value = "guess";
6277 } else if (strcmp(dotp, "erfhdlc") == 0) {
6278 /* Handle the old "erfhdlc" preference; map it to the new
6279 "hdlc_type" preference, and map the values to those for
6280 the new preference. */
6281 pref = prefs_find_preference(module, "hdlc_type");
6282 if (strcmp(value, "chdlc") == 0 || strcmp(value, "Cisco HDLC") == 0)
6283 value = "chdlc";
6284 else if (strcmp(value, "ppp") == 0 || strcmp(value, "PPP serial") == 0)
6285 value = "ppp";
6286 else if (strcmp(value, "fr") == 0 || strcmp(value, "Frame Relay") == 0)
6287 value = "frelay";
6288 else if (strcmp(value, "mtp2") == 0 || strcmp(value, "SS7 MTP2") == 0)
6289 value = "mtp2";
6290 else if (strcmp(value, "raw") == 0 || strcmp(value, "Raw data") == 0)
6291 value = "guess";
6293 } else if (strcmp(module->name, "eth") == 0) {
6294 /* "eth.qinq_ethertype" has been changed(restored) to "vlan.qinq.ethertype" */
6295 if (strcmp(dotp, "qinq_ethertype") == 0) {
6296 module_t *new_module = prefs_find_module("vlan");
6297 if (new_module) {
6298 pref = prefs_find_preference(new_module, "qinq_ethertype");
6299 containing_module = new_module;
6302 } else if (strcmp(module->name, "taps") == 0) {
6303 /* taps preferences moved to "statistics" module */
6304 if (strcmp(dotp, "update_interval") == 0)
6305 pref = prefs_find_preference(stats_module, dotp);
6306 } else if (strcmp(module->name, "packet_list") == 0) {
6307 /* packet_list preferences moved to protocol module */
6308 if (strcmp(dotp, "display_hidden_proto_items") == 0)
6309 pref = prefs_find_preference(protocols_module, dotp);
6310 } else if (strcmp(module->name, "stream") == 0) {
6311 /* stream preferences moved to gui color module */
6312 if ((strcmp(dotp, "client.fg") == 0) ||
6313 (strcmp(dotp, "client.bg") == 0) ||
6314 (strcmp(dotp, "server.fg") == 0) ||
6315 (strcmp(dotp, "server.bg") == 0))
6316 pref = prefs_find_preference(gui_color_module, pref_name);
6317 } else if (strcmp(module->name, "nameres") == 0) {
6318 if (strcmp(pref_name, "name_resolve_concurrency") == 0) {
6319 pref = prefs_find_preference(nameres_module, pref_name);
6320 } else if (strcmp(pref_name, "name_resolve_load_smi_modules") == 0) {
6321 pref = prefs_find_preference(nameres_module, "load_smi_modules");
6322 } else if (strcmp(pref_name, "name_resolve_suppress_smi_errors") == 0) {
6323 pref = prefs_find_preference(nameres_module, "suppress_smi_errors");
6325 } else if (strcmp(module->name, "extcap") == 0) {
6326 /* Handle the old "sshdump.remotesudo" preference; map it to the new
6327 "sshdump.remotepriv" preference, and map the boolean values to the
6328 appropriate strings of the new preference. */
6329 if (strcmp(dotp, "sshdump.remotesudo") == 0) {
6330 pref = prefs_find_preference(module, "sshdump.remotepriv");
6331 if (g_ascii_strcasecmp(value, "true") == 0)
6332 value = "sudo";
6333 else
6334 value = "none";
6337 if (pref) {
6338 converted_pref = true;
6341 if (pref == NULL ) {
6342 if (strcmp(module->name, "extcap") == 0 && g_list_length(module->prefs) <= 1) {
6344 * Assume that we've skipped extcap preference registration
6345 * and that only extcap.gui_save_on_start is loaded.
6347 return PREFS_SET_OK;
6349 return PREFS_SET_NO_SUCH_PREF; /* no such preference */
6352 if (target_module && target_module != containing_module) {
6353 /* Ignore */
6354 return PREFS_SET_OK;
6357 type = pref->type;
6358 if (IS_PREF_OBSOLETE(type)) {
6359 return PREFS_SET_OBSOLETE; /* no such preference any more */
6360 } else {
6361 RESET_PREF_OBSOLETE(type);
6364 if (converted_pref) {
6365 ws_warning("Preference \"%s\" has been converted to \"%s.%s\"\n"
6366 "Save your preferences to make this change permanent.",
6367 pref_name, module->name ? module->name : module->parent->name, prefs_get_name(pref));
6370 switch (type) {
6372 case PREF_UINT:
6373 if (!ws_basestrtou32(value, NULL, &uval, pref->info.base))
6374 return PREFS_SET_SYNTAX_ERR; /* number was bad */
6375 if (*pref->varp.uint != uval) {
6376 containing_module->prefs_changed_flags |= prefs_get_effect_flags(pref);
6377 *pref->varp.uint = uval;
6379 break;
6380 case PREF_BOOL:
6381 /* XXX - give an error if it's neither "true" nor "false"? */
6382 if (g_ascii_strcasecmp(value, "true") == 0)
6383 bval = true;
6384 else
6385 bval = false;
6386 if (*pref->varp.boolp != bval) {
6387 containing_module->prefs_changed_flags |= prefs_get_effect_flags(pref);
6388 *pref->varp.boolp = bval;
6390 break;
6392 case PREF_ENUM:
6393 /* XXX - give an error if it doesn't match? */
6394 enum_val = find_val_for_string(value, pref->info.enum_info.enumvals,
6395 *pref->varp.enump);
6396 if (*pref->varp.enump != enum_val) {
6397 containing_module->prefs_changed_flags |= prefs_get_effect_flags(pref);
6398 *pref->varp.enump = enum_val;
6400 break;
6402 case PREF_STRING:
6403 case PREF_SAVE_FILENAME:
6404 case PREF_OPEN_FILENAME:
6405 case PREF_DIRNAME:
6406 case PREF_DISSECTOR:
6407 containing_module->prefs_changed_flags |= prefs_set_string_value(pref, value, pref_current);
6408 break;
6410 case PREF_PASSWORD:
6411 /* Read value is every time empty */
6412 containing_module->prefs_changed_flags |= prefs_set_string_value(pref, "", pref_current);
6413 break;
6415 case PREF_RANGE:
6417 if (!prefs_set_range_value_work(pref, value, return_range_errors,
6418 &containing_module->prefs_changed_flags))
6419 return PREFS_SET_SYNTAX_ERR; /* number was bad */
6420 break;
6422 case PREF_DECODE_AS_RANGE:
6424 /* This is for backwards compatibility in case any of the preferences
6425 that shared the "Decode As" preference name and used to be PREF_RANGE
6426 are now applied directly to the Decode As funtionality */
6427 range_t *newrange;
6428 dissector_table_t sub_dissectors;
6429 dissector_handle_t handle;
6430 uint32_t i, j;
6432 if (range_convert_str_work(wmem_epan_scope(), &newrange, value, pref->info.max_value,
6433 return_range_errors) != CVT_NO_ERROR) {
6434 return PREFS_SET_SYNTAX_ERR; /* number was bad */
6437 if (!ranges_are_equal(*pref->varp.range, newrange)) {
6438 wmem_free(wmem_epan_scope(), *pref->varp.range);
6439 *pref->varp.range = newrange;
6440 containing_module->prefs_changed_flags |= prefs_get_effect_flags(pref);
6442 const char* table_name = prefs_get_dissector_table(pref);
6443 sub_dissectors = find_dissector_table(table_name);
6444 if (sub_dissectors != NULL) {
6445 handle = dissector_table_get_dissector_handle(sub_dissectors, module->title);
6446 if (handle != NULL) {
6447 /* Delete all of the old values from the dissector table */
6448 for (i = 0; i < (*pref->varp.range)->nranges; i++) {
6449 for (j = (*pref->varp.range)->ranges[i].low; j < (*pref->varp.range)->ranges[i].high; j++) {
6450 dissector_delete_uint(table_name, j, handle);
6451 decode_build_reset_list(table_name, dissector_table_get_type(sub_dissectors), GUINT_TO_POINTER(j), NULL, NULL);
6454 dissector_delete_uint(table_name, (*pref->varp.range)->ranges[i].high, handle);
6455 decode_build_reset_list(table_name, dissector_table_get_type(sub_dissectors), GUINT_TO_POINTER((*pref->varp.range)->ranges[i].high), NULL, NULL);
6458 /* Add new values to the dissector table */
6459 for (i = 0; i < newrange->nranges; i++) {
6460 for (j = newrange->ranges[i].low; j < newrange->ranges[i].high; j++) {
6461 dissector_change_uint(table_name, j, handle);
6462 decode_build_reset_list(table_name, dissector_table_get_type(sub_dissectors), GUINT_TO_POINTER(j), NULL, NULL);
6465 dissector_change_uint(table_name, newrange->ranges[i].high, handle);
6466 decode_build_reset_list(table_name, dissector_table_get_type(sub_dissectors), GUINT_TO_POINTER(newrange->ranges[i].high), NULL, NULL);
6469 /* XXX - Do we save the decode_as_entries file here? */
6472 } else {
6473 wmem_free(wmem_epan_scope(), newrange);
6475 break;
6478 case PREF_COLOR:
6480 if (!ws_hexstrtou32(value, NULL, &cval))
6481 return PREFS_SET_SYNTAX_ERR; /* number was bad */
6482 if ((pref->varp.colorp->red != RED_COMPONENT(cval)) ||
6483 (pref->varp.colorp->green != GREEN_COMPONENT(cval)) ||
6484 (pref->varp.colorp->blue != BLUE_COMPONENT(cval))) {
6485 containing_module->prefs_changed_flags |= prefs_get_effect_flags(pref);
6486 pref->varp.colorp->red = RED_COMPONENT(cval);
6487 pref->varp.colorp->green = GREEN_COMPONENT(cval);
6488 pref->varp.colorp->blue = BLUE_COMPONENT(cval);
6490 break;
6493 case PREF_CUSTOM:
6494 return pref->custom_cbs.set_cb(pref, value, &containing_module->prefs_changed_flags);
6496 case PREF_STATIC_TEXT:
6497 case PREF_UAT:
6498 case PREF_PROTO_TCP_SNDAMB_ENUM:
6500 /* There's no point in setting the TCP sequence override
6501 * value from the command line, because the pref is different
6502 * for each frame and reset to the default (0) for each new
6503 * file.
6505 break;
6510 return PREFS_SET_OK;
6513 typedef struct {
6514 FILE *pf;
6515 bool is_gui_module;
6516 } write_gui_pref_arg_t;
6518 const char *
6519 prefs_pref_type_name(pref_t *pref)
6521 const char *type_name = "[Unknown]";
6522 int type;
6524 if (!pref) {
6525 return type_name; /* ...or maybe assert? */
6528 type = pref->type;
6530 if (IS_PREF_OBSOLETE(type)) {
6531 type_name = "Obsolete";
6532 } else {
6533 RESET_PREF_OBSOLETE(type);
6536 switch (type) {
6538 case PREF_UINT:
6539 switch (pref->info.base) {
6541 case 10:
6542 type_name = "Decimal";
6543 break;
6545 case 8:
6546 type_name = "Octal";
6547 break;
6549 case 16:
6550 type_name = "Hexadecimal";
6551 break;
6553 break;
6555 case PREF_BOOL:
6556 type_name = "Boolean";
6557 break;
6559 case PREF_ENUM:
6560 case PREF_PROTO_TCP_SNDAMB_ENUM:
6561 type_name = "Choice";
6562 break;
6564 case PREF_STRING:
6565 type_name = "String";
6566 break;
6568 case PREF_SAVE_FILENAME:
6569 case PREF_OPEN_FILENAME:
6570 type_name = "Filename";
6571 break;
6573 case PREF_DIRNAME:
6574 type_name = "Directory";
6575 break;
6577 case PREF_RANGE:
6578 type_name = "Range";
6579 break;
6581 case PREF_COLOR:
6582 type_name = "Color";
6583 break;
6585 case PREF_CUSTOM:
6586 if (pref->custom_cbs.type_name_cb)
6587 return pref->custom_cbs.type_name_cb();
6588 type_name = "Custom";
6589 break;
6591 case PREF_DECODE_AS_RANGE:
6592 type_name = "Range (for Decode As)";
6593 break;
6595 case PREF_STATIC_TEXT:
6596 type_name = "Static text";
6597 break;
6599 case PREF_UAT:
6600 type_name = "UAT";
6601 break;
6603 case PREF_PASSWORD:
6604 type_name = "Password";
6605 break;
6607 case PREF_DISSECTOR:
6608 type_name = "Dissector";
6609 break;
6611 return type_name;
6614 unsigned int
6615 prefs_get_effect_flags(pref_t *pref)
6617 if (pref == NULL)
6618 return 0;
6620 return pref->effect_flags;
6623 void
6624 prefs_set_effect_flags(pref_t *pref, unsigned int flags)
6626 if (pref != NULL) {
6627 if (flags == 0) {
6628 ws_error("Setting \"%s\" preference effect flags to 0", pref->name);
6630 pref->effect_flags = flags;
6634 void
6635 prefs_set_effect_flags_by_name(module_t * module, const char *pref, unsigned int flags)
6637 prefs_set_effect_flags(prefs_find_preference(module, pref), flags);
6640 unsigned int
6641 prefs_get_module_effect_flags(module_t * module)
6643 if (module == NULL)
6644 return 0;
6646 return module->effect_flags;
6649 void
6650 prefs_set_module_effect_flags(module_t * module, unsigned int flags)
6652 if (module != NULL) {
6653 if (flags == 0) {
6654 ws_error("Setting module \"%s\" preference effect flags to 0", module->name);
6656 module->effect_flags = flags;
6660 char *
6661 prefs_pref_type_description(pref_t *pref)
6663 const char *type_desc = "An unknown preference type";
6664 int type;
6666 if (!pref) {
6667 return ws_strdup_printf("%s.", type_desc); /* ...or maybe assert? */
6670 type = pref->type;
6672 if (IS_PREF_OBSOLETE(type)) {
6673 type_desc = "An obsolete preference";
6674 } else {
6675 RESET_PREF_OBSOLETE(type);
6678 switch (type) {
6680 case PREF_UINT:
6681 switch (pref->info.base) {
6683 case 10:
6684 type_desc = "A decimal number";
6685 break;
6687 case 8:
6688 type_desc = "An octal number";
6689 break;
6691 case 16:
6692 type_desc = "A hexadecimal number";
6693 break;
6695 break;
6697 case PREF_BOOL:
6698 type_desc = "true or false (case-insensitive)";
6699 break;
6701 case PREF_ENUM:
6702 case PREF_PROTO_TCP_SNDAMB_ENUM:
6704 const enum_val_t *enum_valp = pref->info.enum_info.enumvals;
6705 GString *enum_str = g_string_new("One of: ");
6706 GString *desc_str = g_string_new("\nEquivalently, one of: ");
6707 bool distinct = false;
6708 while (enum_valp->name != NULL) {
6709 g_string_append(enum_str, enum_valp->name);
6710 g_string_append(desc_str, enum_valp->description);
6711 if (g_strcmp0(enum_valp->name, enum_valp->description) != 0) {
6712 distinct = true;
6714 enum_valp++;
6715 if (enum_valp->name != NULL) {
6716 g_string_append(enum_str, ", ");
6717 g_string_append(desc_str, ", ");
6720 if (distinct) {
6721 g_string_append(enum_str, desc_str->str);
6723 g_string_free(desc_str, TRUE);
6724 g_string_append(enum_str, "\n(case-insensitive).");
6725 return g_string_free(enum_str, FALSE);
6728 case PREF_STRING:
6729 type_desc = "A string";
6730 break;
6732 case PREF_SAVE_FILENAME:
6733 case PREF_OPEN_FILENAME:
6734 type_desc = "A path to a file";
6735 break;
6737 case PREF_DIRNAME:
6738 type_desc = "A path to a directory";
6739 break;
6741 case PREF_RANGE:
6743 type_desc = "A string denoting an positive integer range (e.g., \"1-20,30-40\")";
6744 break;
6747 case PREF_COLOR:
6749 type_desc = "A six-digit hexadecimal RGB color triplet (e.g. fce94f)";
6750 break;
6753 case PREF_CUSTOM:
6754 if (pref->custom_cbs.type_description_cb)
6755 return pref->custom_cbs.type_description_cb();
6756 type_desc = "A custom value";
6757 break;
6759 case PREF_DECODE_AS_RANGE:
6760 type_desc = "A string denoting an positive integer range for Decode As";
6761 break;
6763 case PREF_STATIC_TEXT:
6764 type_desc = "[Static text]";
6765 break;
6767 case PREF_UAT:
6768 type_desc = "Configuration data stored in its own file";
6769 break;
6771 case PREF_PASSWORD:
6772 type_desc = "Password (never stored on disk)";
6773 break;
6775 case PREF_DISSECTOR:
6776 type_desc = "A dissector name";
6777 break;
6779 default:
6780 break;
6782 return g_strdup(type_desc);
6785 bool
6786 prefs_pref_is_default(pref_t *pref)
6788 int type;
6789 if (!pref) return false;
6791 type = pref->type;
6792 if (IS_PREF_OBSOLETE(type)) {
6793 return false;
6794 } else {
6795 RESET_PREF_OBSOLETE(type);
6798 switch (type) {
6800 case PREF_UINT:
6801 if (pref->default_val.uint == *pref->varp.uint)
6802 return true;
6803 break;
6805 case PREF_BOOL:
6806 if (pref->default_val.boolval == *pref->varp.boolp)
6807 return true;
6808 break;
6810 case PREF_ENUM:
6811 case PREF_PROTO_TCP_SNDAMB_ENUM:
6812 if (pref->default_val.enumval == *pref->varp.enump)
6813 return true;
6814 break;
6816 case PREF_STRING:
6817 case PREF_SAVE_FILENAME:
6818 case PREF_OPEN_FILENAME:
6819 case PREF_DIRNAME:
6820 case PREF_PASSWORD:
6821 case PREF_DISSECTOR:
6822 if (!(g_strcmp0(pref->default_val.string, *pref->varp.string)))
6823 return true;
6824 break;
6826 case PREF_DECODE_AS_RANGE:
6827 case PREF_RANGE:
6829 if ((ranges_are_equal(pref->default_val.range, *pref->varp.range)))
6830 return true;
6831 break;
6834 case PREF_COLOR:
6836 if ((pref->default_val.color.red == pref->varp.colorp->red) &&
6837 (pref->default_val.color.green == pref->varp.colorp->green) &&
6838 (pref->default_val.color.blue == pref->varp.colorp->blue))
6839 return true;
6840 break;
6843 case PREF_CUSTOM:
6844 return pref->custom_cbs.is_default_cb(pref);
6846 case PREF_STATIC_TEXT:
6847 case PREF_UAT:
6848 return false;
6849 /* ws_assert_not_reached(); */
6850 break;
6852 return false;
6855 char *
6856 prefs_pref_to_str(pref_t *pref, pref_source_t source) {
6857 const char *pref_text = "[Unknown]";
6858 void *valp; /* pointer to preference value */
6859 color_t *pref_color;
6860 char *tmp_value, *ret_value;
6861 int type;
6863 if (!pref) {
6864 return g_strdup(pref_text);
6867 switch (source) {
6868 case pref_default:
6869 valp = &pref->default_val;
6870 /* valp = &boolval, &enumval, etc. are implied by union property */
6871 pref_color = &pref->default_val.color;
6872 break;
6873 case pref_stashed:
6874 valp = &pref->stashed_val;
6875 /* valp = &boolval, &enumval, etc. are implied by union property */
6876 pref_color = &pref->stashed_val.color;
6877 break;
6878 case pref_current:
6879 valp = pref->varp.uint;
6880 /* valp = boolval, enumval, etc. are implied by union property */
6881 pref_color = pref->varp.colorp;
6882 break;
6883 default:
6884 return g_strdup(pref_text);
6887 type = pref->type;
6888 if (IS_PREF_OBSOLETE(type)) {
6889 pref_text = "[Obsolete]";
6890 } else {
6891 RESET_PREF_OBSOLETE(type);
6894 switch (type) {
6896 case PREF_UINT:
6898 unsigned pref_uint = *(unsigned *) valp;
6899 switch (pref->info.base) {
6901 case 10:
6902 return ws_strdup_printf("%u", pref_uint);
6904 case 8:
6905 return ws_strdup_printf("%#o", pref_uint);
6907 case 16:
6908 return ws_strdup_printf("%#x", pref_uint);
6910 break;
6913 case PREF_BOOL:
6914 return g_strdup((*(bool *) valp) ? "TRUE" : "FALSE");
6916 case PREF_ENUM:
6917 case PREF_PROTO_TCP_SNDAMB_ENUM:
6919 int pref_enumval = *(int *) valp;
6920 const enum_val_t *enum_valp = pref->info.enum_info.enumvals;
6922 * TODO - We write the "description" value, because the "name" values
6923 * weren't validated to be command line friendly until 5.0, and a few
6924 * of them had to be changed. This allows older versions of Wireshark
6925 * to read preferences that they supported, as we supported either
6926 * the short name or the description when reading the preference files
6927 * or an "-o" option. Once 5.0 is the oldest supported version, switch
6928 * to writing the name below.
6930 while (enum_valp->name != NULL) {
6931 if (enum_valp->value == pref_enumval)
6932 return g_strdup(enum_valp->description);
6933 enum_valp++;
6935 break;
6938 case PREF_STRING:
6939 case PREF_SAVE_FILENAME:
6940 case PREF_OPEN_FILENAME:
6941 case PREF_DIRNAME:
6942 case PREF_PASSWORD:
6943 case PREF_DISSECTOR:
6944 return g_strdup(*(const char **) valp);
6946 case PREF_DECODE_AS_RANGE:
6947 case PREF_RANGE:
6948 /* Convert wmem to g_alloc memory */
6949 tmp_value = range_convert_range(NULL, *(range_t **) valp);
6950 ret_value = g_strdup(tmp_value);
6951 wmem_free(NULL, tmp_value);
6952 return ret_value;
6954 case PREF_COLOR:
6955 return ws_strdup_printf("%02x%02x%02x",
6956 (pref_color->red * 255 / 65535),
6957 (pref_color->green * 255 / 65535),
6958 (pref_color->blue * 255 / 65535));
6960 case PREF_CUSTOM:
6961 if (pref->custom_cbs.to_str_cb)
6962 return pref->custom_cbs.to_str_cb(pref, source == pref_default ? true : false);
6963 pref_text = "[Custom]";
6964 break;
6966 case PREF_STATIC_TEXT:
6967 pref_text = "[Static text]";
6968 break;
6970 case PREF_UAT:
6972 uat_t *uat = pref->varp.uat;
6973 if (uat && uat->filename)
6974 return ws_strdup_printf("[Managed in the file \"%s\"]", uat->filename);
6975 else
6976 pref_text = "[Managed in an unknown file]";
6977 break;
6980 default:
6981 break;
6983 return g_strdup(pref_text);
6987 * Write out a single dissector preference.
6989 static void
6990 write_pref(void *data, void *user_data)
6992 pref_t *pref = (pref_t *)data;
6993 write_pref_arg_t *arg = (write_pref_arg_t *)user_data;
6994 char **desc_lines;
6995 int i;
6996 int type;
6998 type = pref->type;
7000 if (IS_PREF_OBSOLETE(type)) {
7002 * This preference is no longer supported; it's not a
7003 * real preference, so we don't write it out (i.e., we
7004 * treat it as if it weren't found in the list of
7005 * preferences, and we weren't called in the first place).
7007 return;
7008 } else {
7009 RESET_PREF_OBSOLETE(type);
7012 switch (type) {
7014 case PREF_STATIC_TEXT:
7015 case PREF_UAT:
7016 /* Nothing to do; don't bother printing the description */
7017 return;
7018 case PREF_DECODE_AS_RANGE:
7019 /* Data is saved through Decode As mechanism and not part of preferences file */
7020 return;
7021 case PREF_PROTO_TCP_SNDAMB_ENUM:
7022 /* Not written to the preference file because the override is only
7023 * for the lifetime of the capture file and there is no single
7024 * value to write.
7026 return;
7027 default:
7028 break;
7031 if (pref->type != PREF_CUSTOM || pref->custom_cbs.type_name_cb() != NULL) {
7033 * The prefix will either be the module name or the parent
7034 * name if it's a subtree
7036 const char *name_prefix = (arg->module->name != NULL) ? arg->module->name : arg->module->parent->name;
7037 char *type_desc, *pref_text;
7038 const char * def_prefix = prefs_pref_is_default(pref) ? "#" : "";
7040 if (pref->type == PREF_CUSTOM)
7041 fprintf(arg->pf, "\n# %s", pref->custom_cbs.type_name_cb());
7042 fprintf(arg->pf, "\n");
7043 if (pref->description &&
7044 (g_ascii_strncasecmp(pref->description,"", 2) != 0)) {
7045 if (pref->type != PREF_CUSTOM) {
7046 /* We get duplicate lines otherwise. */
7048 desc_lines = g_strsplit(pref->description, "\n", 0);
7049 for (i = 0; desc_lines[i] != NULL; ++i) {
7050 fprintf(arg->pf, "# %s\n", desc_lines[i]);
7052 g_strfreev(desc_lines);
7054 } else {
7055 fprintf(arg->pf, "# No description\n");
7058 type_desc = prefs_pref_type_description(pref);
7059 desc_lines = g_strsplit(type_desc, "\n", 0);
7060 for (i = 0; desc_lines[i] != NULL; ++i) {
7061 fprintf(arg->pf, "# %s\n", desc_lines[i]);
7063 g_strfreev(desc_lines);
7064 g_free(type_desc);
7066 pref_text = prefs_pref_to_str(pref, pref_current);
7067 fprintf(arg->pf, "%s%s.%s: ", def_prefix, name_prefix, pref->name);
7068 if (pref->type != PREF_PASSWORD)
7070 desc_lines = g_strsplit(pref_text, "\n", 0);
7071 for (i = 0; desc_lines[i] != NULL; ++i) {
7072 fprintf(arg->pf, "%s%s\n", i == 0 ? "" : def_prefix, desc_lines[i]);
7074 if (i == 0)
7075 fprintf(arg->pf, "\n");
7076 g_strfreev(desc_lines);
7077 } else {
7078 /* We never store password value */
7079 fprintf(arg->pf, "\n");
7081 g_free(pref_text);
7086 static void
7087 count_non_uat_pref(void *data, void *user_data)
7089 pref_t *pref = (pref_t *)data;
7090 int *arg = (int *)user_data;
7092 switch (pref->type)
7094 case PREF_UAT:
7095 case PREF_OBSOLETE:
7096 case PREF_DECODE_AS_RANGE:
7097 case PREF_PROTO_TCP_SNDAMB_ENUM:
7098 //These types are not written in preference file
7099 break;
7100 default:
7101 (*arg)++;
7102 break;
7106 static int num_non_uat_prefs(module_t *module)
7108 int num = 0;
7110 g_list_foreach(module->prefs, count_non_uat_pref, &num);
7112 return num;
7116 * Write out all preferences for a module.
7118 static unsigned
7119 write_module_prefs(module_t *module, void *user_data)
7121 write_gui_pref_arg_t *gui_pref_arg = (write_gui_pref_arg_t*)user_data;
7122 write_pref_arg_t arg;
7124 /* The GUI module needs to be explicitly called out so it
7125 can be written out of order */
7126 if ((module == gui_module) && (gui_pref_arg->is_gui_module != true))
7127 return 0;
7129 /* Write a header for the main modules and GUI sub-modules */
7130 if (((module->parent == NULL) || (module->parent == gui_module)) &&
7131 ((prefs_module_has_submodules(module)) ||
7132 (num_non_uat_prefs(module) > 0) ||
7133 (module->name == NULL))) {
7134 if ((module->name == NULL) && (module->parent != NULL)) {
7135 fprintf(gui_pref_arg->pf, "\n####### %s: %s ########\n", module->parent->title, module->title);
7136 } else {
7137 fprintf(gui_pref_arg->pf, "\n####### %s ########\n", module->title);
7141 arg.module = module;
7142 arg.pf = gui_pref_arg->pf;
7143 g_list_foreach(arg.module->prefs, write_pref, &arg);
7145 if (prefs_module_has_submodules(module))
7146 return prefs_modules_foreach_submodules(module, write_module_prefs, user_data);
7148 return 0;
7151 #ifdef _WIN32
7152 static void
7153 write_registry(void)
7155 HKEY hTestKey;
7156 DWORD data;
7157 DWORD data_size;
7158 DWORD ret;
7160 ret = RegCreateKeyExA(HKEY_CURRENT_USER, REG_HKCU_WIRESHARK_KEY, 0, NULL,
7161 REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL,
7162 &hTestKey, NULL);
7163 if (ret != ERROR_SUCCESS) {
7164 ws_noisy("Cannot open HKCU "REG_HKCU_WIRESHARK_KEY": 0x%lx", ret);
7165 return;
7168 data = ws_log_console_open;
7169 data_size = sizeof(DWORD);
7170 ret = RegSetValueExA(hTestKey, LOG_HKCU_CONSOLE_OPEN, 0, REG_DWORD, (const BYTE *)&data, data_size);
7171 if (ret == ERROR_SUCCESS) {
7172 ws_noisy("Wrote "LOG_HKCU_CONSOLE_OPEN" to Windows registry: 0x%lu", data);
7174 else {
7175 ws_noisy("Error writing registry key "LOG_HKCU_CONSOLE_OPEN": 0x%lx", ret);
7178 RegCloseKey(hTestKey);
7180 #endif
7182 /* Write out "prefs" to the user's preferences file, and return 0.
7184 If the preferences file path is NULL, write to stdout.
7186 If we got an error, stuff a pointer to the path of the preferences file
7187 into "*pf_path_return", and return the errno. */
7189 write_prefs(char **pf_path_return)
7191 char *pf_path;
7192 FILE *pf;
7193 write_gui_pref_arg_t write_gui_pref_info;
7195 /* Needed for "-G defaultprefs" */
7196 init_prefs();
7198 #ifdef _WIN32
7199 write_registry();
7200 #endif
7202 /* To do:
7203 * - Split output lines longer than MAX_VAL_LEN
7204 * - Create a function for the preference directory check/creation
7205 * so that duplication can be avoided with filter.c
7208 if (pf_path_return != NULL) {
7209 pf_path = get_persconffile_path(PF_NAME, true);
7210 if ((pf = ws_fopen(pf_path, "w")) == NULL) {
7211 *pf_path_return = pf_path;
7212 return errno;
7214 g_free(pf_path);
7215 } else {
7216 pf = stdout;
7220 * If the preferences file is being written, be sure to write UAT files
7221 * first that were migrated from the preferences file.
7223 if (pf_path_return != NULL) {
7224 if (prefs.filter_expressions_old) {
7225 char *err = NULL;
7226 prefs.filter_expressions_old = false;
7227 if (!uat_save(uat_get_table_by_name("Display expressions"), &err)) {
7228 ws_warning("Unable to save Display expressions: %s", err);
7229 g_free(err);
7233 module_t *extcap_module = prefs_find_module("extcap");
7234 if (extcap_module && !prefs.capture_no_extcap) {
7235 char *ext_path = get_persconffile_path("extcap.cfg", true);
7236 FILE *extf;
7237 if ((extf = ws_fopen(ext_path, "w")) == NULL) {
7238 if (errno != EISDIR) {
7239 ws_warning("Unable to save extcap preferences \"%s\": %s",
7240 ext_path, g_strerror(errno));
7242 g_free(ext_path);
7243 } else {
7244 g_free(ext_path);
7246 fputs("# Extcap configuration file for Wireshark " VERSION ".\n"
7247 "#\n"
7248 "# This file is regenerated each time preferences are saved within\n"
7249 "# Wireshark. Making manual changes should be safe, however.\n"
7250 "# Preferences that have been commented out have not been\n"
7251 "# changed from their default value.\n", extf);
7253 write_gui_pref_info.pf = extf;
7254 write_gui_pref_info.is_gui_module = false;
7256 write_module_prefs(extcap_module, &write_gui_pref_info);
7258 fclose(extf);
7263 fputs("# Configuration file for Wireshark " VERSION ".\n"
7264 "#\n"
7265 "# This file is regenerated each time preferences are saved within\n"
7266 "# Wireshark. Making manual changes should be safe, however.\n"
7267 "# Preferences that have been commented out have not been\n"
7268 "# changed from their default value.\n", pf);
7271 * For "backwards compatibility" the GUI module is written first as it's
7272 * at the top of the file. This is followed by all modules that can't
7273 * fit into the preferences read/write API. Finally the remaining modules
7274 * are written in alphabetical order (including of course the protocol preferences)
7276 write_gui_pref_info.pf = pf;
7277 write_gui_pref_info.is_gui_module = true;
7279 write_module_prefs(gui_module, &write_gui_pref_info);
7281 write_gui_pref_info.is_gui_module = false;
7282 prefs_modules_foreach_submodules(NULL, write_module_prefs, &write_gui_pref_info);
7284 fclose(pf);
7286 /* XXX - catch I/O errors (e.g. "ran out of disk space") and return
7287 an error indication, or maybe write to a new preferences file and
7288 rename that file on top of the old one only if there are not I/O
7289 errors. */
7290 return 0;
7293 /** The col_list is only partly managed by the custom preference API
7294 * because its data is shared between multiple preferences, so
7295 * it's freed here
7297 static void
7298 free_col_info(GList *list)
7300 fmt_data *cfmt;
7301 GList *list_head = list;
7303 while (list != NULL) {
7304 cfmt = (fmt_data *)list->data;
7306 g_free(cfmt->title);
7307 g_free(cfmt->custom_fields);
7308 g_free(cfmt);
7309 list = g_list_next(list);
7311 g_list_free(list_head);
7315 * Editor modelines
7317 * Local Variables:
7318 * c-basic-offset: 4
7319 * tab-width: 8
7320 * indent-tabs-mode: nil
7321 * End:
7323 * ex: set shiftwidth=4 tabstop=8 expandtab:
7324 * :indentSize=4:tabSize=8:noTabs=true: