LATER... ei_kerberos_kdc_session_key ...
[wireshark-sm.git] / ui / capture_ui_utils.c
blobd530ee1e2d0506edd753b22abc8c9924949d4a99
1 /* capture_ui_utils.c
2 * Utilities for capture user interfaces
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 #ifdef HAVE_LIBPCAP
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <glib.h>
20 #include "epan/prefs.h"
21 #include "epan/ex-opt.h"
22 #include "capture/capture_ifinfo.h"
23 #include "ui/capture_ui_utils.h"
24 #include "ui/capture_globals.h"
25 #include "wiretap/wtap.h"
26 #include "epan/to_str.h"
27 #include "wsutil/strtoi.h"
30 * In a list of interface information, in the form of a comma-separated
31 * list of {name}({property}) items, find the entry for a particular
32 * interface, and return a pointer a g_malloced string containing
33 * the property.
35 static char *
36 capture_dev_get_if_property(const char *pref, const char *if_name)
38 char **if_tokens;
39 char *property = NULL;
40 int i;
42 if (if_name == NULL || strlen(if_name) < 1) {
43 return NULL;
46 if (pref == NULL || strlen(pref) < 1) {
47 /* There is no interface information list. */
48 return NULL;
52 * Split the list into a sequence of items.
54 * XXX - this relies on the items not themselves containing commas.
56 if_tokens = g_strsplit(pref, ",", -1);
57 for (i = 0; if_tokens[i] != NULL; i++) {
58 char *opening_parenp, *closing_parenp;
61 * Separate this item into name and property.
62 * The first opening parenthesis and the last closing parenthesis
63 * surround the property. Any other parentheses are part of
64 * the property.
66 opening_parenp = strchr(if_tokens[i], '(');
67 if (opening_parenp == NULL) {
68 /* No opening parenthesis. Give up. */
69 break;
71 closing_parenp = strrchr(if_tokens[i], ')');
72 if (closing_parenp == NULL || closing_parenp <= opening_parenp) {
73 /* No closing parenthesis or invalid input. Give up. */
74 break;
76 *opening_parenp = '\0'; /* Split {name} from what follows */
77 *closing_parenp = '\0'; /* Terminate {property} */
78 if (strcmp(if_tokens[i], if_name) == 0) {
79 if (strlen(opening_parenp + 1) > 0) {
80 property = g_strdup(opening_parenp + 1);
82 break;
85 g_strfreev(if_tokens);
87 return property;
91 * Find a property that should be an integral value, and return the
92 * value or, if it's not found or not a valid integral value, -1.
94 static int
95 capture_dev_get_if_int_property(const char *pref, const char *if_name)
97 char *property_string;
98 int property;
100 property_string = capture_dev_get_if_property(pref, if_name);
101 if (property_string == NULL) {
102 /* No property found for this interface. */
103 return -1;
105 if (!ws_strtoi(property_string, NULL, &property)) {
106 /* Syntax error or range error */
107 g_free(property_string);
108 return -1;
111 g_free(property_string);
112 return property;
116 * Find user-specified capture device description that matches interface
117 * name, if any.
119 char *
120 capture_dev_user_descr_find(const char *if_name)
122 char *descr = capture_dev_get_if_property(prefs.capture_devices_descr, if_name);
123 if (descr == NULL && g_strcmp0(if_name, "-") == 0) {
125 * Strictly speaking, -X (extension) options are for modules, e.g. Lua
126 * and using one here stretches that definition. However, this doesn't
127 * waste a single-letter option on something that might be rarely used
128 * and is backward-compatible to 1.0.
130 descr = g_strdup(ex_opt_get_nth("stdin_descr", 0));
132 return descr;
136 capture_dev_user_linktype_find(const char *if_name)
138 return capture_dev_get_if_int_property(prefs.capture_devices_linktypes, if_name);
141 #ifdef CAN_SET_CAPTURE_BUFFER_SIZE
143 capture_dev_user_buffersize_find(const char *if_name)
145 return capture_dev_get_if_int_property(prefs.capture_devices_buffersize, if_name);
147 #endif
149 bool
150 capture_dev_user_snaplen_find(const char *if_name, bool *hassnap, int *snaplen)
152 bool found = false;
153 char **if_tokens;
154 int i;
156 if (if_name == NULL || strlen(if_name) < 1) {
157 return false;
160 if ((prefs.capture_devices_snaplen == NULL) ||
161 (*prefs.capture_devices_snaplen == '\0')) {
162 /* There are no snap lengths defined */
163 return false;
167 * Split the list into a sequence of items.
169 * XXX - this relies on the items not themselves containing commas.
171 if_tokens = g_strsplit(prefs.capture_devices_snaplen, ",", -1);
172 for (i = 0; if_tokens[i] != NULL; i++) {
173 char *colonp;
174 const char *next;
175 int value;
178 * This one's a bit ugly.
179 * The syntax of the item is {name}:{hassnap}({snaplen}),
180 * where {hassnap} is 0 if the interface shouldn't have a snapshot
181 * length and 1 if it should, and {snaplen} is the maximum snapshot
182 * length if {hassnap} is 0 and the specified snapshot length if
183 * {hassnap} is 1.
185 * Sadly, : was a bad choice of separator, given that, on some OSes,
186 * an interface can have a colon in its name.
188 * So we look for the *last* colon in the string.
190 colonp = strrchr(if_tokens[i], ':');
191 if (colonp == NULL) {
192 /* No separating colon. Give up. */
193 break;
195 *colonp = '\0'; /* Split {name} from what follows */
196 if (strcmp(if_tokens[i], if_name) == 0) {
197 /* OK, this matches. */
198 if (*(colonp + 1) == '0') {
199 /* {hassnap} is false, so just set the snaplen to WTAP_MAX_PACKET_SIZE_STANDARD. */
200 found = true;
201 *hassnap = false;
202 *snaplen = WTAP_MAX_PACKET_SIZE_STANDARD;
203 } else if (*(colonp + 1) == '1') {
204 /* {hassnap} is true, so extract {snaplen} */
205 if (*(colonp + 2) != '(') {
206 /* Not followed by a parenthesis. Give up. */
207 break;
209 if (!ws_strtoi(colonp + 3, &next, &value) ||
210 next == colonp + 3 || *next != ')' || value < 0) {
211 /* Syntax error or range error. Give up. */
212 break;
214 found = true;
215 *hassnap = true;
216 *snaplen = value;
217 } else {
218 /* Bad {hassnap}. Give up. */
219 break;
221 break;
224 g_strfreev(if_tokens);
226 return found;
229 bool
230 capture_dev_user_pmode_find(const char *if_name, bool *pmode)
232 int value;
234 value = capture_dev_get_if_int_property(prefs.capture_devices_pmode, if_name);
235 if (value == -1) {
236 /* Not found or bad. */
237 return false;
239 *pmode = (value != 0);
240 return true;
243 char*
244 capture_dev_user_cfilter_find(const char *if_name)
246 return capture_dev_get_if_property(prefs.capture_devices_filter, if_name);
250 * Return as descriptive a name for an interface as we can get.
251 * If the user has specified a comment, use that. Otherwise,
252 * if the get_iface_list() method of capture_opts supplies a
253 * description, use that, otherwise use the interface name.
255 * The result must be g_free()'d when you're done with it.
257 * Note: given that this likely calls capture_interface_list(), which
258 * attempts to open all adapters it finds in order to check whether
259 * they can be captured on, this is an expensive routine to call, so
260 * don't call it frequently.
262 char *
263 get_interface_descriptive_name(const capture_options *capture_opts, const char *if_name)
265 char *descr;
266 GList *if_list;
267 GList *if_entry;
268 if_info_t *if_info;
269 int err;
271 /* Do we have a user-supplied description? */
272 descr = capture_dev_user_descr_find(if_name);
273 if (descr == NULL) {
274 /* No; try to construct a descriptive name. */
275 if (strcmp(if_name, "-") == 0) {
276 descr = g_strdup("Standard input");
277 } else {
278 /* No, we don't have a user-supplied description; did we get
279 one from the OS or libpcap? */
280 /* XXX: Search in capture_opts->ifaces (or all_ifaces) first? */
281 if_list = capture_opts->get_iface_list(&err, NULL);
282 if (if_list != NULL) {
283 if_entry = if_list;
284 do {
285 if_info = (if_info_t *)if_entry->data;
286 if (strcmp(if_info->name, if_name) == 0) {
287 if (if_info->friendly_name != NULL) {
288 /* We have a "friendly name"; return a copy of that
289 as the description - when we free the interface
290 list, that'll also free up the strings to which
291 it refers. */
292 descr = g_strdup(if_info->friendly_name);
293 } else if (if_info->vendor_description != NULL) {
294 /* We have no "friendly name", but we have a vendor
295 description; return a copy of that - when we free
296 the interface list, that'll also free up the strings
297 to which it refers. */
298 descr = g_strdup(if_info->vendor_description);
300 break;
302 } while ((if_entry = g_list_next(if_entry)) != NULL);
304 free_interface_list(if_list);
306 if (descr == NULL) {
307 /* The interface name is all we have, so just return a copy of that. */
308 descr = g_strdup(if_name);
313 return descr;
316 GList *
317 build_capture_combo_list(GList *if_list, bool do_hide)
319 GList *combo_list;
320 GList *if_entry;
321 if_info_t *if_info;
322 char *if_string;
323 char *descr;
325 combo_list = NULL;
326 if (if_list != NULL) {
327 /* Scan through the list and build a list of strings to display. */
328 for (if_entry = if_list; if_entry != NULL;
329 if_entry = g_list_next(if_entry)) {
330 if_info = (if_info_t *)if_entry->data;
332 /* Is this interface hidden and, if so, should we include it
333 anyway? */
334 if (!prefs_is_capture_device_hidden(if_info->name) || !do_hide) {
335 /* It's not hidden, or it is but we should include it in the list. */
337 /* Do we have a user-supplied description? */
338 descr = capture_dev_user_descr_find(if_info->name);
339 if (descr != NULL) {
340 /* Yes, we have a user-supplied description; use it. */
341 if_string = ws_strdup_printf("%s: %s", descr, if_info->name);
342 g_free(descr);
343 } else {
344 /* No, we don't have a user-supplied description; did we get
345 one from the OS or libpcap? */
346 if (if_info->vendor_description != NULL) {
347 /* Yes - use it. */
348 if_string = ws_strdup_printf("%s: %s",
349 if_info->vendor_description,
350 if_info->name);
351 } else {
352 /* No. */
353 if_string = g_strdup(if_info->name);
356 combo_list = g_list_prepend(combo_list, if_string);
358 }/*for*/
359 if(combo_list){
360 combo_list = g_list_reverse(combo_list);
363 return combo_list;
366 static void
367 free_if_string(void *data, void *user_data _U_)
369 g_free(data);
372 void
373 free_capture_combo_list(GList *combo_list)
375 if (combo_list != NULL) {
376 g_list_foreach(combo_list, free_if_string, NULL);
377 g_list_free(combo_list);
382 * Given text that contains an interface name possibly prefixed by an
383 * interface description, extract the interface name.
385 const char *
386 get_if_name(const char *if_text)
388 const char *if_name;
390 #ifdef _WIN32
392 * We cannot assume that the interface name doesn't contain a space;
393 * some names on Windows OT do.
395 * We also can't assume it begins with "\Device\", either, as, on
396 * Windows OT, WinPcap doesn't put "\Device\" in front of the name.
398 * XXX - we don't support Windows OT any more; do we need to worry
399 * about this?
401 * As I remember, we can't assume that the interface description
402 * doesn't contain a colon, either; I think some do.
404 * We can probably assume that the interface *name* doesn't contain
405 * a colon, however; if any interface name does contain a colon on
406 * Windows, it'll be time to just get rid of the damn interface
407 * descriptions in the drop-down list, have just the names in the
408 * drop-down list, and have a "Browse..." button to browse for interfaces,
409 * with names, descriptions, IP addresses, blah blah blah available when
410 * possible.
412 * So we search backwards for a colon. If we don't find it, just
413 * return the entire string; otherwise, skip the colon and any blanks
414 * after it, and return that string.
416 if_name = if_text + strlen(if_text);
417 for (;;) {
418 if (if_name == if_text) {
419 /* We're at the beginning of the string; return it. */
420 break;
422 if_name--;
423 if (*if_name == ':') {
425 * We've found a colon.
426 * Unfortunately, a colon is used in the string "rpcap://",
427 * which is used in case of a remote capture.
428 * So we'll check to make sure the colon isn't followed by "//";
429 * it'll be followed by a blank if it separates the description
430 * and the interface name. (We don't wire in "rpcap", in case we
431 * support other protocols in the same syntax.)
432 * Unfortunately, another colon can be used in "rpcap://host:port/"
433 * before port. Check if colon is followed by digit.
435 if ((strncmp(if_name, "://", 3) != 0) && !g_ascii_isdigit(if_name[1])) {
437 * OK, we've found a colon followed neither by "//" nor by digit.
438 * Skip blanks following it.
440 if_name++;
441 while (*if_name == ' ')
442 if_name++;
443 break;
446 /* Keep looking for a colon not followed by "//". */
448 #else
450 * There's a space between the interface description and name, and
451 * the interface name shouldn't have a space in it (it doesn't, on
452 * UNIX systems); look backwards in the string for a space.
454 * (An interface name might, however, contain a colon in it, which
455 * is why we don't use the colon search on UNIX.)
457 if_name = strrchr(if_text, ' ');
458 if (if_name == NULL) {
459 if_name = if_text;
460 } else {
461 if_name++;
463 #endif
464 return if_name;
468 * Set the active DLT for a device appropriately.
470 void
471 set_active_dlt(interface_t *device, int global_default_dlt)
473 GList *list;
474 bool found_active_dlt;
475 link_row *link;
478 * If there's a preference for the link-layer header type for
479 * this interface, use it. If not, use the all-interface
480 * default; if that's not set on the command line, that will
481 * be -1, meaning "use per-interface defaults", otherwise
482 * we'll fail if it's not one of the types the interface
483 * supports.
485 if ((device->active_dlt = capture_dev_user_linktype_find(device->name)) == -1) {
486 device->active_dlt = global_default_dlt;
490 * Is that one of the supported link-layer header types?
491 * If not, set it to -1, so we'll fall back on the first supported
492 * link-layer header type.
494 found_active_dlt = false;
495 for (list = device->links; list != NULL; list = g_list_next(list)) {
496 link = (link_row *)(list->data);
497 if (link->dlt != -1 && link->dlt == device->active_dlt) {
498 found_active_dlt = true;
499 break;
502 if (!found_active_dlt) {
503 device->active_dlt = -1;
505 if (device->active_dlt == -1) {
506 /* Fall back on the first supported DLT, if we have one. */
507 for (list = device->links; list != NULL; list = g_list_next(list)) {
508 link = (link_row *)(list->data);
509 if (link->dlt != -1) {
510 device->active_dlt = link->dlt;
511 break;
517 GString *
518 get_iface_list_string(capture_options *capture_opts, uint32_t style)
520 GString *iface_list_string = g_string_new("");
521 unsigned i;
524 * If we have a descriptive name for the interface, show that,
525 * rather than its raw name. On NT 5.x (2K/XP/Server2K3), the
526 * interface name is something like "\Device\NPF_{242423..."
527 * which is pretty useless to the normal user. On other platforms,
528 * it might be less cryptic, but if a more descriptive name is
529 * available, we should still use that.
531 #ifdef _WIN32
532 if (capture_opts->ifaces->len < 2) {
533 #else
534 if (capture_opts->ifaces->len < 4) {
535 #endif
536 for (i = 0; i < capture_opts->ifaces->len; i++) {
537 if (i > 0) {
538 if (capture_opts->ifaces->len > 2) {
539 g_string_append_printf(iface_list_string, ",");
541 g_string_append_printf(iface_list_string, " ");
542 if (i == capture_opts->ifaces->len - 1) {
543 g_string_append_printf(iface_list_string, "and ");
547 interface_options *interface_opts = &g_array_index(capture_opts->ifaces, interface_options, i);
549 if (style & IFLIST_QUOTE_IF_DESCRIPTION)
550 g_string_append_printf(iface_list_string, "'");
551 /* If we have a special user-supplied description (via the prefs
552 * or the documented "-X stdin_descr" option for stdin), make sure
553 * we're using it.
555 char *user_descr = capture_dev_user_descr_find(interface_opts->name);
556 if (user_descr != NULL) {
557 if (g_strcmp0(interface_opts->descr, user_descr) != 0) {
558 g_free(interface_opts->descr);
559 interface_opts->descr = user_descr;
560 g_free(interface_opts->display_name);
561 interface_opts->display_name = g_strdup(interface_opts->descr);
562 } else {
563 g_free(user_descr);
566 if (interface_opts->display_name == NULL) {
568 * We don't have a display name; generate one.
569 * fill_in_interface_opts_from_finfo and
570 * capture_opts_add_iface_opt always fill in
571 * the display name, so this shouldn't be necessary.
573 if (interface_opts->descr == NULL) {
574 if (interface_opts->name != NULL)
575 interface_opts->descr = get_interface_descriptive_name(capture_opts, interface_opts->name);
576 else
577 interface_opts->descr = g_strdup("(Unknown)");
579 interface_opts->display_name = g_strdup(interface_opts->descr);
581 g_string_append_printf(iface_list_string, "%s", interface_opts->display_name);
582 if (style & IFLIST_QUOTE_IF_DESCRIPTION)
583 g_string_append_printf(iface_list_string, "'");
584 if (style & IFLIST_SHOW_FILTER) {
585 if (interface_opts->cfilter != NULL &&
586 strlen(interface_opts->cfilter) > 0) {
587 g_string_append_printf(iface_list_string, " (%s)", interface_opts->cfilter);
591 } else {
592 g_string_append_printf(iface_list_string, "%u interfaces", capture_opts->ifaces->len);
594 return iface_list_string;
596 #endif /* HAVE_LIBPCAP */