[Automatic update for 2018-04-08]
[metze/wireshark/wip.git] / ui / dissect_opts.c
1 /* dissect_opts.c
2  * Routines for dissection options setting
3  *
4  * Wireshark - Network traffic analyzer
5  * By Gerald Combs <gerald@wireshark.org>
6  * Copyright 1998 Gerald Combs
7  *
8  * SPDX-License-Identifier: GPL-2.0-or-later*/
9
10 #include <config.h>
11
12 #include <stdio.h>
13 #include <stdlib.h>
14
15 #include <string.h>
16
17 #include <errno.h>
18
19 #include <glib.h>
20
21 #include <epan/prefs.h>
22 #include <epan/timestamp.h>
23 #include <epan/addr_resolv.h>
24 #include <epan/disabled_protos.h>
25
26 #include "ui/decode_as_utils.h"
27
28 #if defined(HAVE_HEIMDAL_KERBEROS) || defined(HAVE_MIT_KERBEROS)
29 #include <epan/dissectors/read_keytab_file.h>
30 #endif
31
32 #include <wsutil/clopts_common.h>
33 #include <wsutil/cmdarg_err.h>
34 #include <wsutil/file_util.h>
35
36 #include "ui/dissect_opts.h"
37
38 dissect_options global_dissect_options;
39
40 void
41 dissect_opts_init(void)
42 {
43     global_dissect_options.time_format = TS_NOT_SET;
44     global_dissect_options.disable_protocol_slist = NULL;
45     global_dissect_options.enable_protocol_slist = NULL;
46     global_dissect_options.enable_heur_slist = NULL;
47     global_dissect_options.disable_heur_slist = NULL;
48 }
49
50 gboolean
51 dissect_opts_handle_opt(int opt, char *optarg_str_p)
52 {
53     char badopt;
54
55     switch(opt) {
56     case 'd':        /* Decode as rule */
57         if (!decode_as_command_option(optarg_str_p))
58              return FALSE;
59         break;
60     case 'K':        /* Kerberos keytab file */
61 #if defined(HAVE_HEIMDAL_KERBEROS) || defined(HAVE_MIT_KERBEROS)
62         read_keytab_file(optarg_str_p);
63 #else
64         cmdarg_err("-K specified, but Kerberos keytab file support isn't present");
65         return FALSE;
66 #endif
67         break;
68     case 'n':        /* No name resolution */
69         disable_name_resolution();
70         break;
71     case 'N':        /* Select what types of addresses/port #s to resolve */
72         badopt = string_to_name_resolve(optarg_str_p, &gbl_resolv_flags);
73         if (badopt != '\0') {
74             cmdarg_err("-N specifies unknown resolving option '%c'; valid options are:",
75                        badopt);
76             cmdarg_err_cont("\t'd' to enable address resolution from captured DNS packets\n"
77                             "\t'm' to enable MAC address resolution\n"
78                             "\t'n' to enable network address resolution\n"
79                             "\t'N' to enable using external resolvers (e.g., DNS)\n"
80                             "\t    for network address resolution\n"
81                             "\t't' to enable transport-layer port number resolution");
82             return FALSE;
83         }
84         break;
85     case 't':        /* Time stamp type */
86         if (strcmp(optarg_str_p, "r") == 0)
87             global_dissect_options.time_format = TS_RELATIVE;
88         else if (strcmp(optarg_str_p, "a") == 0)
89             global_dissect_options.time_format = TS_ABSOLUTE;
90         else if (strcmp(optarg_str_p, "ad") == 0)
91             global_dissect_options.time_format = TS_ABSOLUTE_WITH_YMD;
92         else if (strcmp(optarg_str_p, "adoy") == 0)
93             global_dissect_options.time_format = TS_ABSOLUTE_WITH_YDOY;
94         else if (strcmp(optarg_str_p, "d") == 0)
95             global_dissect_options.time_format = TS_DELTA;
96         else if (strcmp(optarg_str_p, "dd") == 0)
97             global_dissect_options.time_format = TS_DELTA_DIS;
98         else if (strcmp(optarg_str_p, "e") == 0)
99             global_dissect_options.time_format = TS_EPOCH;
100         else if (strcmp(optarg_str_p, "u") == 0)
101             global_dissect_options.time_format = TS_UTC;
102         else if (strcmp(optarg_str_p, "ud") == 0)
103             global_dissect_options.time_format = TS_UTC_WITH_YMD;
104         else if (strcmp(optarg_str_p, "udoy") == 0)
105             global_dissect_options.time_format = TS_UTC_WITH_YDOY;
106         else {
107             cmdarg_err("Invalid time stamp type \"%s\"; it must be one of:", optarg_str_p);
108             cmdarg_err_cont("\t\"a\"    for absolute\n"
109                             "\t\"ad\"   for absolute with YYYY-MM-DD date\n"
110                             "\t\"adoy\" for absolute with YYYY/DOY date\n"
111                             "\t\"d\"    for delta\n"
112                             "\t\"dd\"   for delta displayed\n"
113                             "\t\"e\"    for epoch\n"
114                             "\t\"r\"    for relative\n"
115                             "\t\"u\"    for absolute UTC\n"
116                             "\t\"ud\"   for absolute UTC with YYYY-MM-DD date\n"
117                             "\t\"udoy\" for absolute UTC with YYYY/DOY date");
118             return FALSE;
119         }
120         break;
121     case 'u':        /* Seconds type */
122         if (strcmp(optarg_str_p, "s") == 0)
123             timestamp_set_seconds_type(TS_SECONDS_DEFAULT);
124         else if (strcmp(optarg_str_p, "hms") == 0)
125             timestamp_set_seconds_type(TS_SECONDS_HOUR_MIN_SEC);
126         else {
127             cmdarg_err("Invalid seconds type \"%s\"; it must be one of:", optarg_str_p);
128             cmdarg_err_cont("\t\"s\"   for seconds\n"
129                             "\t\"hms\" for hours, minutes and seconds");
130             return FALSE;
131         }
132         break;
133     case LONGOPT_DISABLE_PROTOCOL: /* disable dissection of protocol */
134         global_dissect_options.disable_protocol_slist = g_slist_append(global_dissect_options.disable_protocol_slist, optarg_str_p);
135         break;
136     case LONGOPT_ENABLE_HEURISTIC: /* enable heuristic dissection of protocol */
137         global_dissect_options.enable_heur_slist = g_slist_append(global_dissect_options.enable_heur_slist, optarg_str_p);
138         break;
139     case LONGOPT_DISABLE_HEURISTIC: /* disable heuristic dissection of protocol */
140         global_dissect_options.disable_heur_slist = g_slist_append(global_dissect_options.disable_heur_slist, optarg_str_p);
141         break;
142     case LONGOPT_ENABLE_PROTOCOL: /* enable dissection of protocol (that is disableed by default) */
143         global_dissect_options.enable_protocol_slist = g_slist_append(global_dissect_options.enable_protocol_slist, optarg_str_p);
144         break;
145     default:
146         /* the caller is responsible to send us only the right opt's */
147         g_assert_not_reached();
148     }
149     return TRUE;
150 }
151
152 gboolean
153 setup_enabled_and_disabled_protocols(void)
154 {
155     gboolean success = TRUE;
156
157     if (global_dissect_options.disable_protocol_slist) {
158         GSList *proto_disable;
159
160         for (proto_disable = global_dissect_options.disable_protocol_slist; proto_disable != NULL; proto_disable = g_slist_next(proto_disable))
161             proto_disable_proto_by_name((char*)proto_disable->data);
162     }
163
164     if (global_dissect_options.enable_protocol_slist) {
165         GSList *proto_enable;
166
167         for (proto_enable = global_dissect_options.enable_protocol_slist; proto_enable != NULL; proto_enable = g_slist_next(proto_enable))
168             proto_enable_proto_by_name((char*)proto_enable->data);
169     }
170
171     if (global_dissect_options.enable_heur_slist) {
172         GSList *heur_enable;
173
174         for (heur_enable = global_dissect_options.enable_heur_slist; heur_enable != NULL; heur_enable = g_slist_next(heur_enable)) {
175             if (!proto_enable_heuristic_by_name((char*)heur_enable->data, TRUE)) {
176                 cmdarg_err("No such protocol %s, can't enable", (char*)heur_enable->data);
177                 success = FALSE;
178             }
179         }
180     }
181
182     if (global_dissect_options.disable_heur_slist) {
183         GSList *heur_disable;
184
185         for (heur_disable = global_dissect_options.disable_heur_slist; heur_disable != NULL; heur_disable = g_slist_next(heur_disable)) {
186             if (!proto_enable_heuristic_by_name((char*)heur_disable->data, FALSE)) {
187                 cmdarg_err("No such protocol %s, can't disable", (char*)heur_disable->data);
188                 success = FALSE;
189             }
190         }
191     }
192     return success;
193 }
194
195 /*
196  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
197  *
198  * Local variables:
199  * c-basic-offset: 4
200  * tab-width: 8
201  * indent-tabs-mode: nil
202  * End:
203  *
204  * vi: set shiftwidth=4 tabstop=8 expandtab:
205  * :indentSize=4:tabSize=8:noTabs=true:
206  */