update epan/dissectors/pidl/drsuapi/drsuapi.idl from samba
[wireshark-sm.git] / ui / dissect_opts.c
blob5f812faaa4c9109490599ab5d8f77dada4114948
1 /* dissect_opts.c
2 * Routines for dissection options setting
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>
13 #include <stdio.h>
14 #include <stdlib.h>
16 #include <string.h>
18 #include <glib.h>
20 #include <epan/prefs.h>
21 #include <epan/timestamp.h>
22 #include <epan/addr_resolv.h>
23 #include <epan/disabled_protos.h>
25 #include "ui/decode_as_utils.h"
27 #if defined(HAVE_HEIMDAL_KERBEROS) || defined(HAVE_MIT_KERBEROS)
28 #include <epan/dissectors/read_keytab_file.h>
29 #endif
31 #include <wsutil/clopts_common.h>
32 #include <wsutil/strtoi.h>
33 #include <wsutil/cmdarg_err.h>
34 #include <wsutil/file_util.h>
35 #include <wsutil/ws_assert.h>
37 #include "ui/dissect_opts.h"
39 dissect_options global_dissect_options = {
40 .time_format = TS_NOT_SET,
41 .time_precision = TS_PREC_NOT_SET
44 bool
45 dissect_opts_handle_opt(int opt, char *optarg_str_p)
47 char badopt;
48 char *dotp;
49 ts_precision tsp;
51 switch(opt) {
52 case 'd': /* Decode as rule */
53 if (!decode_as_command_option(optarg_str_p))
54 return false;
55 break;
56 case 'K': /* Kerberos keytab file */
57 #if defined(HAVE_HEIMDAL_KERBEROS) || defined(HAVE_MIT_KERBEROS)
58 read_keytab_file(optarg_str_p);
59 #else
60 cmdarg_err("-K specified, but Kerberos keytab file support isn't present");
61 return false;
62 #endif
63 break;
64 case 'n': /* No name resolution */
65 disable_name_resolution();
66 break;
67 case 'N': /* Select what types of addresses/port #s to resolve */
68 badopt = string_to_name_resolve(optarg_str_p, &gbl_resolv_flags);
69 if (badopt != '\0') {
70 cmdarg_err("-N specifies unknown resolving option '%c'; valid options are:",
71 badopt);
72 cmdarg_err_cont("\t'd' to enable address resolution from captured DNS packets\n"
73 "\t'g' to enable address geolocation information from MaxMind databases\n"
74 "\t'm' to enable MAC address resolution\n"
75 "\t'n' to enable network address resolution\n"
76 "\t'N' to enable using external resolvers (e.g., DNS)\n"
77 "\t for network address resolution\n"
78 "\t's' to enable address resolution using SNI information found in captured\n"
79 "\t handshake packets\n"
80 "\t't' to enable transport-layer port number resolution\n"
81 "\t'v' to enable VLAN IDs to names resolution");
82 return false;
84 break;
85 case 't': /* Time stamp type */
86 tsp = TS_PREC_NOT_SET;
87 dotp = strchr(optarg_str_p, '.');
88 if (dotp != NULL) {
89 if (strcmp(dotp + 1, "") == 0) {
90 /* Nothing specified; use appropriate precision for the file. */
91 tsp = TS_PREC_AUTO;
92 } else {
94 * Precision must be a number giving the number of
95 * digits of precision.
97 uint32_t val;
99 if (!ws_strtou32(dotp + 1, NULL, &val) || val > WS_TSPREC_MAX) {
100 cmdarg_err("Invalid .N time stamp precision \"%s\"; N must be a value between 0 and %u or absent",
101 dotp + 1, WS_TSPREC_MAX);
102 return false;
104 tsp = val;
106 /* Mask the '.' while checking format. */
107 *dotp = '\0';
109 if (strcmp(optarg_str_p, "r") == 0)
110 global_dissect_options.time_format = TS_RELATIVE;
111 else if (strcmp(optarg_str_p, "a") == 0)
112 global_dissect_options.time_format = TS_ABSOLUTE;
113 else if (strcmp(optarg_str_p, "ad") == 0)
114 global_dissect_options.time_format = TS_ABSOLUTE_WITH_YMD;
115 else if (strcmp(optarg_str_p, "adoy") == 0)
116 global_dissect_options.time_format = TS_ABSOLUTE_WITH_YDOY;
117 else if (strcmp(optarg_str_p, "d") == 0)
118 global_dissect_options.time_format = TS_DELTA;
119 else if (strcmp(optarg_str_p, "dd") == 0)
120 global_dissect_options.time_format = TS_DELTA_DIS;
121 else if (strcmp(optarg_str_p, "e") == 0)
122 global_dissect_options.time_format = TS_EPOCH;
123 else if (strcmp(optarg_str_p, "u") == 0)
124 global_dissect_options.time_format = TS_UTC;
125 else if (strcmp(optarg_str_p, "ud") == 0)
126 global_dissect_options.time_format = TS_UTC_WITH_YMD;
127 else if (strcmp(optarg_str_p, "udoy") == 0)
128 global_dissect_options.time_format = TS_UTC_WITH_YDOY;
129 else if (optarg_str_p != dotp) {
130 /* If (optarg_str_p == dotp), user only set precision. */
131 cmdarg_err("Invalid time stamp type \"%s\"; it must be one of:", optarg_str_p);
132 cmdarg_err_cont("\t\"a\" for absolute\n"
133 "\t\"ad\" for absolute with YYYY-MM-DD date\n"
134 "\t\"adoy\" for absolute with YYYY/DOY date\n"
135 "\t\"d\" for delta\n"
136 "\t\"dd\" for delta displayed\n"
137 "\t\"e\" for epoch\n"
138 "\t\"r\" for relative\n"
139 "\t\"u\" for absolute UTC\n"
140 "\t\"ud\" for absolute UTC with YYYY-MM-DD date\n"
141 "\t\"udoy\" for absolute UTC with YYYY/DOY date");
142 if (dotp)
143 *dotp = '.';
144 return false;
146 if (dotp) {
147 *dotp = '.';
148 global_dissect_options.time_precision = tsp;
150 break;
151 case 'u': /* Seconds type */
152 if (strcmp(optarg_str_p, "s") == 0)
153 timestamp_set_seconds_type(TS_SECONDS_DEFAULT);
154 else if (strcmp(optarg_str_p, "hms") == 0)
155 timestamp_set_seconds_type(TS_SECONDS_HOUR_MIN_SEC);
156 else {
157 cmdarg_err("Invalid seconds type \"%s\"; it must be one of:", optarg_str_p);
158 cmdarg_err_cont("\t\"s\" for seconds\n"
159 "\t\"hms\" for hours, minutes and seconds");
160 return false;
162 break;
163 case LONGOPT_DISABLE_PROTOCOL: /* disable dissection of protocol */
164 global_dissect_options.disable_protocol_slist = g_slist_append(global_dissect_options.disable_protocol_slist, optarg_str_p);
165 break;
166 case LONGOPT_ENABLE_HEURISTIC: /* enable heuristic dissection of protocol */
167 global_dissect_options.enable_heur_slist = g_slist_append(global_dissect_options.enable_heur_slist, optarg_str_p);
168 break;
169 case LONGOPT_DISABLE_HEURISTIC: /* disable heuristic dissection of protocol */
170 global_dissect_options.disable_heur_slist = g_slist_append(global_dissect_options.disable_heur_slist, optarg_str_p);
171 break;
172 case LONGOPT_ENABLE_PROTOCOL: /* enable dissection of protocol (that is disabled by default) */
173 global_dissect_options.enable_protocol_slist = g_slist_append(global_dissect_options.enable_protocol_slist, optarg_str_p);
174 break;
175 case LONGOPT_ONLY_PROTOCOLS: /* enable dissection of these comma separated protocols only */
176 proto_disable_all();
177 for (char *ps = strtok (optarg_str_p, ","); ps; ps = strtok(NULL, ",")){
178 global_dissect_options.enable_protocol_slist = g_slist_append(global_dissect_options.enable_protocol_slist, ps);
180 break;
181 case LONGOPT_DISABLE_ALL_PROTOCOLS: /* disable dissection of all protocols */
182 proto_disable_all();
183 break;
184 default:
185 /* the caller is responsible to send us only the right opt's */
186 ws_assert_not_reached();
188 return true;
191 typedef bool (proto_set_func)(const char *);
193 static bool
194 process_enable_disable_list(GSList *list, proto_set_func callback)
196 bool success = true;
197 bool rv;
198 GSList *iter;
199 char *c;
200 char *proto_name;
202 for (iter = list; iter != NULL; iter = g_slist_next(iter)) {
203 proto_name = (char *)iter->data;
204 c = strchr(proto_name, ',');
205 if (c == NULL) {
206 rv = callback(proto_name);
207 if (!rv) {
208 cmdarg_err("No such protocol %s", proto_name);
209 success = false;
212 else {
213 char *start;
214 char save;
216 start = proto_name;
217 while(1) {
218 if (c != NULL) {
219 save = *c;
220 *c = '\0';
222 rv = callback(start);
223 if (!rv) {
224 cmdarg_err("No such protocol %s", start);
225 success = false;
227 if (c != NULL) {
228 *c = save;
229 start = (save == ',' ? c+1 : c);
230 c = strchr(start, ',');
232 else {
233 break;
239 return success;
242 bool
243 setup_enabled_and_disabled_protocols(void)
245 bool success = true;
247 success = success && process_enable_disable_list(global_dissect_options.disable_protocol_slist,
248 proto_disable_proto_by_name);
249 success = success && process_enable_disable_list(global_dissect_options.enable_protocol_slist,
250 proto_enable_proto_by_name);
251 success = success && process_enable_disable_list(global_dissect_options.enable_heur_slist,
252 proto_enable_heuristic_by_name);
253 success = success && process_enable_disable_list(global_dissect_options.disable_heur_slist,
254 proto_disable_heuristic_by_name);
255 return success;