Kerberos: add kerberos_inject_longterm_key() helper function
[wireshark-sm.git] / ui / decode_as_utils.c
blobdec0868f1493f27f6a2e7afffa0fc4d74e0cacb6
1 /* decode_as_utils.c
3 * Routines to modify dissector tables on the fly.
5 * By David Hampton <dhampton@mac.com>
6 * Copyright 2001 David Hampton
8 * SPDX-License-Identifier: GPL-2.0-or-later
9 */
10 #include "config.h"
12 #include <stdlib.h>
14 #include "epan/decode_as.h"
15 #include "epan/packet.h"
16 #include "epan/prefs.h"
17 #include "epan/prefs-int.h"
19 #include "ui/decode_as_utils.h"
20 #include "ui/simple_dialog.h"
22 #include "wsutil/file_util.h"
23 #include "wsutil/filesystem.h"
24 #include <wsutil/ws_assert.h>
25 #include "wsutil/cmdarg_err.h"
27 /* XXX - We might want to switch this to a UAT */
29 static const char* prev_display_dissector_name;
32 * For a dissector table, print on the stream described by output,
33 * its short name (which is what's used in the "-d" option) and its
34 * descriptive name.
36 static void
37 display_dissector_table_names(const char *table_name, const char *ui_name,
38 void *output)
40 ftenum_t selector_type = get_dissector_table_selector_type(table_name);
42 switch (selector_type) {
44 case FT_UINT8:
45 case FT_UINT16:
46 case FT_UINT24:
47 case FT_UINT32:
48 case FT_STRING:
49 case FT_STRINGZ:
50 case FT_UINT_STRING:
51 case FT_STRINGZPAD:
52 case FT_STRINGZTRUNC:
53 case FT_NONE:
54 /* This option supports these types. */
55 if ((prev_display_dissector_name == NULL) ||
56 (strcmp(prev_display_dissector_name, table_name) != 0)) {
57 fprintf((FILE *)output, "\t%s (%s)\n", table_name, ui_name);
58 prev_display_dissector_name = table_name;
60 break;
62 default:
63 /* Other types (FT_GUID, FT_BYTES) are not supported. */
64 break;
69 * For a dissector handle, print on the stream described by output,
70 * the filter name (which is what's used in the "-d" option) and the full
71 * name for the protocol that corresponds to this handle.
73 static void
74 display_dissector_names(const char *table _U_, void *handle, void *output)
76 int proto_id;
77 const char *proto_filter_name;
78 const char *proto_ui_name;
80 proto_id = dissector_handle_get_protocol_index((dissector_handle_t)handle);
82 if (proto_id != -1) {
83 proto_filter_name = proto_get_protocol_filter_name(proto_id);
84 proto_ui_name = proto_get_protocol_name(proto_id);
85 ws_assert(proto_filter_name != NULL);
86 ws_assert(proto_ui_name != NULL);
88 if ((prev_display_dissector_name == NULL) ||
89 (strcmp(prev_display_dissector_name, proto_filter_name) != 0)) {
90 fprintf((FILE *)output, "\t%s (%s)\n",
91 proto_filter_name,
92 proto_ui_name);
93 prev_display_dissector_name = proto_filter_name;
99 * Allow dissector key names to be sorted alphabetically
102 static int
103 compare_dissector_key_name(const void *dissector_a, const void *dissector_b)
105 return strcmp((const char*)dissector_a, (const char*)dissector_b);
109 * Print all layer type names supported.
110 * We send the output to the stream described by the handle output.
112 static void
113 fprint_all_layer_types(FILE *output)
116 prev_display_dissector_name = NULL;
117 dissector_all_tables_foreach_table(display_dissector_table_names, (void *)output, (GCompareFunc)compare_dissector_key_name);
121 * Print all protocol names supported for a specific layer type.
122 * table_name contains the layer type name in which the search is performed.
123 * We send the output to the stream described by the handle output.
125 static void
126 fprint_all_protocols_for_layer_types(FILE *output, char *table_name)
129 prev_display_dissector_name = NULL;
130 dissector_table_foreach_handle(table_name,
131 display_dissector_names,
132 (void *)output);
136 * The protocol_name_search structure is used by find_protocol_name_func()
137 * to pass parameters and store results
139 struct protocol_name_search{
140 const char *searched_name; /* Protocol filter name we are looking for */
141 dissector_handle_t matched_handle; /* Handle for a dissector whose protocol has the specified filter name */
142 unsigned nb_match; /* How many dissectors matched searched_name */
144 typedef struct protocol_name_search *protocol_name_search_t;
147 * This function parses all dissectors associated with a table to find the
148 * one whose protocol has the specified filter name. It is called
149 * as a reference function in a call to dissector_table_foreach_handle.
150 * The name we are looking for, as well as the results, are stored in the
151 * protocol_name_search struct pointed to by user_data.
152 * If called using dissector_table_foreach_handle, we actually parse the
153 * whole list of dissectors.
155 static void
156 find_protocol_name_func(const char *table _U_, void *handle, void *user_data)
159 int proto_id;
160 const char *protocol_filter_name;
161 protocol_name_search_t search_info;
163 ws_assert(handle);
165 search_info = (protocol_name_search_t)user_data;
167 proto_id = dissector_handle_get_protocol_index((dissector_handle_t)handle);
168 if (proto_id != -1) {
169 protocol_filter_name = proto_get_protocol_filter_name(proto_id);
170 ws_assert(protocol_filter_name != NULL);
171 if (strcmp(protocol_filter_name, search_info->searched_name) == 0) {
172 /* Found a match */
173 if (search_info->nb_match == 0) {
174 /* Record this handle only if this is the first match */
175 search_info->matched_handle = (dissector_handle_t)handle; /* Record the handle for this matching dissector */
177 search_info->nb_match++;
183 * The function below parses the command-line parameters for the decode as
184 * feature (a string pointer by cl_param).
185 * It checks the format of the command-line, searches for a matching table
186 * and dissector. If a table/dissector match is not found, we display a
187 * summary of the available tables/dissectors (on stderr) and return false.
188 * If everything is fine, we get the "Decode as" preference activated,
189 * then we return true.
191 bool decode_as_command_option(const char *cl_param)
193 char *table_name;
194 uint32_t selector = 0, selector2 = 0;
195 char *decoded_param;
196 char *remaining_param;
197 char *selector_str = NULL;
198 char *dissector_str;
199 dissector_handle_t dissector_matching;
200 dissector_table_t table_matching;
201 ftenum_t dissector_table_selector_type;
202 struct protocol_name_search user_protocol_name;
203 uint64_t i;
204 char op = '\0';
206 /* The following code will allocate and copy the command-line options in a string pointed by decoded_param */
208 ws_assert(cl_param);
209 decoded_param = g_strdup(cl_param);
210 ws_assert(decoded_param);
213 /* The lines below will parse this string (modifying it) to extract all
214 necessary information. Note that decoded_param is still needed since
215 strings are not copied - we just save pointers. */
217 /* This section extracts a layer type (table_name) from decoded_param */
218 table_name = decoded_param; /* Layer type string starts from beginning */
220 remaining_param = strchr(table_name, '=');
221 if (remaining_param == NULL) {
222 /* Dissector tables of type FT_NONE aren't required to specify a value, so for now
223 just check for comma */
224 remaining_param = strchr(table_name, ',');
225 if (remaining_param == NULL) {
226 cmdarg_err("Parameter \"%s\" doesn't follow the template \"%s\"", cl_param, DECODE_AS_ARG_TEMPLATE);
227 } else {
228 *remaining_param = '\0'; /* Terminate the layer type string (table_name) where ',' was detected */
230 /* If the argument does not follow the template, carry on anyway to check
231 if the table name is at least correct. If remaining_param is NULL,
232 we'll exit anyway further down */
234 else {
235 *remaining_param = '\0'; /* Terminate the layer type string (table_name) where '=' was detected */
238 /* Remove leading and trailing spaces from the table name */
239 while (table_name[0] == ' ')
240 table_name++;
241 while (table_name[strlen(table_name) - 1] == ' ')
242 table_name[strlen(table_name) - 1] = '\0'; /* Note: if empty string, while loop will eventually exit */
244 /* The following part searches a table matching with the layer type specified */
245 table_matching = NULL;
247 /* Look for the requested table */
248 if (!(*(table_name))) { /* Is the table name empty, if so, don't even search for anything, display a message */
249 cmdarg_err("No layer type specified"); /* Note, we don't exit here, but table_matching will remain NULL, so we exit below */
251 else {
252 table_matching = find_dissector_table(table_name);
253 if (!table_matching) {
254 cmdarg_err("Unknown layer type -- %s", table_name); /* Note, we don't exit here, but table_matching will remain NULL, so we exit below */
258 if (!table_matching) {
259 /* Display a list of supported layer types to help the user, if the
260 specified layer type was not found */
261 cmdarg_err("Valid layer types are:");
262 fprint_all_layer_types(stderr);
264 if (remaining_param == NULL || !table_matching) {
265 /* Exit if the layer type was not found, or if no '=' separator was found
266 (see above) */
267 g_free(decoded_param);
268 return false;
271 dissector_table_selector_type = get_dissector_table_selector_type(table_name);
273 if (dissector_table_selector_type != FT_NONE) {
274 if (*(remaining_param + 1) != '=') { /* Check for "==" and not only '=' */
275 cmdarg_err("WARNING: -d requires \"==\" instead of \"=\". Option will be treated as \"%s==%s\"", table_name, remaining_param + 1);
277 else {
278 remaining_param++; /* Move to the second '=' */
279 *remaining_param = '\0'; /* Remove the second '=' */
281 remaining_param++; /* Position after the layer type string */
284 /* This section extracts a selector value (selector_str) from decoded_param */
286 selector_str = remaining_param; /* Next part starts with the selector number */
288 remaining_param = strchr(selector_str, ',');
289 if (remaining_param == NULL) {
290 cmdarg_err("Parameter \"%s\" doesn't follow the template \"%s\"", cl_param, DECODE_AS_ARG_TEMPLATE);
291 /* If the argument does not follow the template, carry on anyway to check
292 if the selector value is at least correct. If remaining_param is NULL,
293 we'll exit anyway further down */
295 else {
296 *remaining_param = '\0'; /* Terminate the selector number string (selector_str) where ',' was detected */
300 switch (dissector_table_selector_type) {
302 case FT_UINT8:
303 case FT_UINT16:
304 case FT_UINT24:
305 case FT_UINT32:
307 /* The selector for this table is an unsigned number. Parse it as such.
308 Skip leading spaces for backwards compatibility (previously sscanf was used). */
309 char *str = selector_str;
310 char *end;
311 uint64_t val;
313 while (g_ascii_isspace(*str)) {
314 str++;
317 val = g_ascii_strtoull(str, &end, 0);
318 if (str == end || val > UINT32_MAX) {
319 cmdarg_err("Invalid selector number \"%s\"", selector_str);
320 g_free(decoded_param);
321 return false;
323 selector = (uint32_t) val;
325 if (*end == '\0') {
326 /* not a range, but a single (valid) value */
327 op = '\0';
328 selector2 = 0;
329 } else if (*end == ':' || *end == '-') {
330 /* range value such as "8888:3" or "8888-8890" */
331 op = *end;
332 str = end + 1;
334 val = g_ascii_strtoull(str, &end, 0);
335 if (str == end || val > UINT32_MAX || *end != '\0') {
336 cmdarg_err("Invalid selector numeric range \"%s\"", selector_str);
337 g_free(decoded_param);
338 return false;
340 selector2 = (uint32_t) val;
342 if (op == ':') {
343 if ((selector2 == 0) || ((uint64_t)selector + selector2 - 1) > UINT32_MAX) {
344 cmdarg_err("Invalid selector numeric range \"%s\"", selector_str);
345 g_free(decoded_param);
346 return false;
349 else if (selector2 < selector) {
350 /* We could swap them for the user, but maybe it's better to call
351 * this out as an error in case it's not what was intended? */
352 cmdarg_err("Invalid selector numeric range \"%s\"", selector_str);
353 g_free(decoded_param);
354 return false;
356 } else {
357 /* neither a valid single value, nor a range. */
358 cmdarg_err("Invalid selector number \"%s\"", selector_str);
359 g_free(decoded_param);
360 return false;
362 break;
365 case FT_STRING:
366 case FT_STRINGZ:
367 case FT_UINT_STRING:
368 case FT_STRINGZPAD:
369 case FT_STRINGZTRUNC:
370 /* The selector for this table is a string. */
371 break;
373 case FT_NONE:
374 /* There is no selector for this table */
375 break;
377 case FT_BYTES:
378 /* Custom table. Parsing a selector is not really possible. */
379 cmdarg_err("\"%s\" is a custom table; specifying selectors on the command line is not supported.", table_name);
380 g_free(decoded_param);
381 return false;
383 case FT_GUID:
384 /* GUID table. It might be possible to parse a selector (guid_key)
385 * in the future, but not now. */
386 cmdarg_err("\"%s\" is a GUID table; specifying selectors on the command line is not supported.", table_name);
387 g_free(decoded_param);
388 return false;
390 default:
391 /* There are currently no dissector tables with any types other
392 than the ones listed above. */
393 ws_assert_not_reached();
396 if (remaining_param == NULL) {
397 /* Exit if no ',' separator was found (see above) */
398 cmdarg_err("Valid protocols for layer type \"%s\" are:", table_name);
399 fprint_all_protocols_for_layer_types(stderr, table_name);
400 g_free(decoded_param);
401 return false;
404 remaining_param++; /* Position after the selector number string */
406 /* This section extracts a protocol filter name (dissector_str) from decoded_param */
408 dissector_str = remaining_param; /* All the rest of the string is the dissector (decode as protocol) name */
410 /* Remove leading and trailing spaces from the dissector name */
411 while (dissector_str[0] == ' ')
412 dissector_str++;
413 while (dissector_str[strlen(dissector_str) - 1] == ' ')
414 dissector_str[strlen(dissector_str) - 1] = '\0'; /* Note: if empty string, while loop will eventually exit */
416 dissector_matching = NULL;
418 /* We now have a pointer to the handle for the requested table inside the variable table_matching */
419 if (!(*dissector_str)) { /* Is the dissector name empty, if so, don't even search for a matching dissector and display all dissectors found for the selected table */
420 cmdarg_err("No protocol name specified"); /* Note, we don't exit here, but dissector_matching will remain NULL, so we exit below */
422 else {
423 header_field_info *hfi = proto_registrar_get_byalias(dissector_str);
425 user_protocol_name.nb_match = 0;
426 if (hfi) {
427 user_protocol_name.searched_name = hfi->abbrev;
428 } else {
429 user_protocol_name.searched_name = dissector_str;
431 user_protocol_name.matched_handle = NULL;
433 dissector_table_foreach_handle(table_name, find_protocol_name_func, &user_protocol_name); /* Go and perform the search for this dissector in the this table's dissectors' names and shortnames */
435 if (user_protocol_name.nb_match != 0) {
436 dissector_matching = user_protocol_name.matched_handle;
437 if (user_protocol_name.nb_match > 1) {
438 cmdarg_err("WARNING: Protocol \"%s\" matched %u dissectors, first one will be used", dissector_str, user_protocol_name.nb_match);
441 else {
442 /* OK, check whether the problem is that there isn't any such
443 protocol, or that there is but it's not specified as a protocol
444 that's valid for that dissector table.
445 Note, we don't exit here, but dissector_matching will remain NULL,
446 so we exit below */
447 if (proto_get_id_by_filter_name(dissector_str) == -1) {
448 /* No such protocol */
449 cmdarg_err("Unknown protocol -- \"%s\"", dissector_str);
451 else {
452 cmdarg_err("Protocol \"%s\" isn't valid for layer type \"%s\"",
453 dissector_str, table_name);
458 if (!dissector_matching) {
459 cmdarg_err("Valid protocols for layer type \"%s\" are:", table_name);
460 fprint_all_protocols_for_layer_types(stderr, table_name);
461 g_free(decoded_param);
462 return false;
465 /* This is the end of the code that parses the command-line options.
466 All information is now stored in the variables:
467 table_name
468 selector
469 dissector_matching
470 The above variables that are strings are still pointing to areas within
471 decoded_parm. decoded_parm thus still needs to be kept allocated in
472 until we stop needing these variables
473 decoded_param will be deallocated at each exit point of this function */
476 /* We now have a pointer to the handle for the requested dissector
477 (requested protocol) inside the variable dissector_matching */
478 switch (dissector_table_selector_type) {
480 case FT_UINT8:
481 case FT_UINT16:
482 case FT_UINT24:
483 case FT_UINT32:
484 /* The selector for this table is an unsigned number. */
485 if (op == '\0') {
486 dissector_change_uint(table_name, selector, dissector_matching);
488 else if (op == ':') {
489 for (i = selector; i < (uint64_t)selector + selector2; i++) {
490 dissector_change_uint(table_name, (uint32_t)i, dissector_matching);
493 else { /* op == '-' */
494 for (i = selector; i <= selector2; i++) {
495 dissector_change_uint(table_name, (uint32_t)i, dissector_matching);
498 break;
500 case FT_STRING:
501 case FT_STRINGZ:
502 case FT_UINT_STRING:
503 case FT_STRINGZPAD:
504 case FT_STRINGZTRUNC:
505 /* The selector for this table is a string. */
506 dissector_change_string(table_name, selector_str, dissector_matching);
507 break;
509 case FT_NONE:
510 /* Just directly set the dissector found. */
511 dissector_change_payload(table_name, dissector_matching);
512 break;
514 default:
515 /* There are currently no dissector tables with any types other
516 than the ones listed above. (We already exited for FT_GUID and
517 FT_BYTES tables.) */
518 ws_assert_not_reached();
520 g_free(decoded_param); /* "Decode As" rule has been successfully added */
521 return true;