sync epan/dissectors/pidl/drsuapi/drsuapi.idl
[wireshark-sm.git] / ui / capture_ui_utils.c
blobcd84c1a8249f25848c6b8f449814728e5fbe9937
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);
142 capture_dev_user_buffersize_find(const char *if_name)
144 return capture_dev_get_if_int_property(prefs.capture_devices_buffersize, if_name);
147 bool
148 capture_dev_user_snaplen_find(const char *if_name, bool *hassnap, int *snaplen)
150 bool found = false;
151 char **if_tokens;
152 int i;
154 if (if_name == NULL || strlen(if_name) < 1) {
155 return false;
158 if ((prefs.capture_devices_snaplen == NULL) ||
159 (*prefs.capture_devices_snaplen == '\0')) {
160 /* There are no snap lengths defined */
161 return false;
165 * Split the list into a sequence of items.
167 * XXX - this relies on the items not themselves containing commas.
169 if_tokens = g_strsplit(prefs.capture_devices_snaplen, ",", -1);
170 for (i = 0; if_tokens[i] != NULL; i++) {
171 char *colonp;
172 const char *next;
173 int value;
176 * This one's a bit ugly.
177 * The syntax of the item is {name}:{hassnap}({snaplen}),
178 * where {hassnap} is 0 if the interface shouldn't have a snapshot
179 * length and 1 if it should, and {snaplen} is the maximum snapshot
180 * length if {hassnap} is 0 and the specified snapshot length if
181 * {hassnap} is 1.
183 * Sadly, : was a bad choice of separator, given that, on some OSes,
184 * an interface can have a colon in its name.
186 * So we look for the *last* colon in the string.
188 colonp = strrchr(if_tokens[i], ':');
189 if (colonp == NULL) {
190 /* No separating colon. Give up. */
191 break;
193 *colonp = '\0'; /* Split {name} from what follows */
194 if (strcmp(if_tokens[i], if_name) == 0) {
195 /* OK, this matches. */
196 if (*(colonp + 1) == '0') {
197 /* {hassnap} is false, so just set the snaplen to WTAP_MAX_PACKET_SIZE_STANDARD. */
198 found = true;
199 *hassnap = false;
200 *snaplen = WTAP_MAX_PACKET_SIZE_STANDARD;
201 } else if (*(colonp + 1) == '1') {
202 /* {hassnap} is true, so extract {snaplen} */
203 if (*(colonp + 2) != '(') {
204 /* Not followed by a parenthesis. Give up. */
205 break;
207 if (!ws_strtoi(colonp + 3, &next, &value) ||
208 next == colonp + 3 || *next != ')' || value < 0) {
209 /* Syntax error or range error. Give up. */
210 break;
212 found = true;
213 *hassnap = true;
214 *snaplen = value;
215 } else {
216 /* Bad {hassnap}. Give up. */
217 break;
219 break;
222 g_strfreev(if_tokens);
224 return found;
227 bool
228 capture_dev_user_pmode_find(const char *if_name, bool *pmode)
230 int value;
232 value = capture_dev_get_if_int_property(prefs.capture_devices_pmode, if_name);
233 if (value == -1) {
234 /* Not found or bad. */
235 return false;
237 *pmode = (value != 0);
238 return true;
241 char*
242 capture_dev_user_cfilter_find(const char *if_name)
244 return capture_dev_get_if_property(prefs.capture_devices_filter, if_name);
248 * Return as descriptive a name for an interface as we can get.
249 * If the user has specified a comment, use that. Otherwise,
250 * if the get_iface_list() method of capture_opts supplies a
251 * description, use that, otherwise use the interface name.
253 * The result must be g_free()'d when you're done with it.
255 * Note: given that this likely calls capture_interface_list(), which
256 * attempts to open all adapters it finds in order to check whether
257 * they can be captured on, this is an expensive routine to call, so
258 * don't call it frequently.
260 char *
261 get_interface_descriptive_name(const capture_options *capture_opts, const char *if_name)
263 char *descr;
264 GList *if_list;
265 GList *if_entry;
266 if_info_t *if_info;
267 int err;
269 /* Do we have a user-supplied description? */
270 descr = capture_dev_user_descr_find(if_name);
271 if (descr == NULL) {
272 /* No; try to construct a descriptive name. */
273 if (strcmp(if_name, "-") == 0) {
274 descr = g_strdup("Standard input");
275 } else {
276 /* No, we don't have a user-supplied description; did we get
277 one from the OS or libpcap? */
278 /* XXX: Search in capture_opts->ifaces (or all_ifaces) first? */
279 if_list = capture_opts->get_iface_list(&err, NULL);
280 if (if_list != NULL) {
281 if_entry = if_list;
282 do {
283 if_info = (if_info_t *)if_entry->data;
284 if (strcmp(if_info->name, if_name) == 0) {
285 if (if_info->friendly_name != NULL) {
286 /* We have a "friendly name"; return a copy of that
287 as the description - when we free the interface
288 list, that'll also free up the strings to which
289 it refers. */
290 descr = g_strdup(if_info->friendly_name);
291 } else if (if_info->vendor_description != NULL) {
292 /* We have no "friendly name", but we have a vendor
293 description; return a copy of that - when we free
294 the interface list, that'll also free up the strings
295 to which it refers. */
296 descr = g_strdup(if_info->vendor_description);
298 break;
300 } while ((if_entry = g_list_next(if_entry)) != NULL);
302 free_interface_list(if_list);
304 if (descr == NULL) {
305 /* The interface name is all we have, so just return a copy of that. */
306 descr = g_strdup(if_name);
311 return descr;
314 GList *
315 build_capture_combo_list(GList *if_list, bool do_hide)
317 GList *combo_list;
318 GList *if_entry;
319 if_info_t *if_info;
320 char *if_string;
321 char *descr;
323 combo_list = NULL;
324 if (if_list != NULL) {
325 /* Scan through the list and build a list of strings to display. */
326 for (if_entry = if_list; if_entry != NULL;
327 if_entry = g_list_next(if_entry)) {
328 if_info = (if_info_t *)if_entry->data;
330 /* Is this interface hidden and, if so, should we include it
331 anyway? */
332 if (!prefs_is_capture_device_hidden(if_info->name) || !do_hide) {
333 /* It's not hidden, or it is but we should include it in the list. */
335 /* Do we have a user-supplied description? */
336 descr = capture_dev_user_descr_find(if_info->name);
337 if (descr != NULL) {
338 /* Yes, we have a user-supplied description; use it. */
339 if_string = ws_strdup_printf("%s: %s", descr, if_info->name);
340 g_free(descr);
341 } else {
342 /* No, we don't have a user-supplied description; did we get
343 one from the OS or libpcap? */
344 if (if_info->vendor_description != NULL) {
345 /* Yes - use it. */
346 if_string = ws_strdup_printf("%s: %s",
347 if_info->vendor_description,
348 if_info->name);
349 } else {
350 /* No. */
351 if_string = g_strdup(if_info->name);
354 combo_list = g_list_prepend(combo_list, if_string);
356 }/*for*/
357 if(combo_list){
358 combo_list = g_list_reverse(combo_list);
361 return combo_list;
364 static void
365 free_if_string(void *data, void *user_data _U_)
367 g_free(data);
370 void
371 free_capture_combo_list(GList *combo_list)
373 if (combo_list != NULL) {
374 g_list_foreach(combo_list, free_if_string, NULL);
375 g_list_free(combo_list);
380 * Given text that contains an interface name possibly prefixed by an
381 * interface description, extract the interface name.
383 const char *
384 get_if_name(const char *if_text)
386 const char *if_name;
388 #ifdef _WIN32
390 * We cannot assume that the interface name doesn't contain a space;
391 * some names on Windows OT do.
393 * We also can't assume it begins with "\Device\", either, as, on
394 * Windows OT, WinPcap doesn't put "\Device\" in front of the name.
396 * XXX - we don't support Windows OT any more; do we need to worry
397 * about this?
399 * As I remember, we can't assume that the interface description
400 * doesn't contain a colon, either; I think some do.
402 * We can probably assume that the interface *name* doesn't contain
403 * a colon, however; if any interface name does contain a colon on
404 * Windows, it'll be time to just get rid of the damn interface
405 * descriptions in the drop-down list, have just the names in the
406 * drop-down list, and have a "Browse..." button to browse for interfaces,
407 * with names, descriptions, IP addresses, blah blah blah available when
408 * possible.
410 * So we search backwards for a colon. If we don't find it, just
411 * return the entire string; otherwise, skip the colon and any blanks
412 * after it, and return that string.
414 if_name = if_text + strlen(if_text);
415 for (;;) {
416 if (if_name == if_text) {
417 /* We're at the beginning of the string; return it. */
418 break;
420 if_name--;
421 if (*if_name == ':') {
423 * We've found a colon.
424 * Unfortunately, a colon is used in the string "rpcap://",
425 * which is used in case of a remote capture.
426 * So we'll check to make sure the colon isn't followed by "//";
427 * it'll be followed by a blank if it separates the description
428 * and the interface name. (We don't wire in "rpcap", in case we
429 * support other protocols in the same syntax.)
430 * Unfortunately, another colon can be used in "rpcap://host:port/"
431 * before port. Check if colon is followed by digit.
433 if ((strncmp(if_name, "://", 3) != 0) && !g_ascii_isdigit(if_name[1])) {
435 * OK, we've found a colon followed neither by "//" nor by digit.
436 * Skip blanks following it.
438 if_name++;
439 while (*if_name == ' ')
440 if_name++;
441 break;
444 /* Keep looking for a colon not followed by "//". */
446 #else
448 * There's a space between the interface description and name, and
449 * the interface name shouldn't have a space in it (it doesn't, on
450 * UNIX systems); look backwards in the string for a space.
452 * (An interface name might, however, contain a colon in it, which
453 * is why we don't use the colon search on UNIX.)
455 if_name = strrchr(if_text, ' ');
456 if (if_name == NULL) {
457 if_name = if_text;
458 } else {
459 if_name++;
461 #endif
462 return if_name;
466 * Set the active DLT for a device appropriately.
468 void
469 set_active_dlt(interface_t *device, int global_default_dlt)
471 GList *list;
472 bool found_active_dlt;
473 link_row *link;
476 * If there's a preference for the link-layer header type for
477 * this interface, use it. If not, use the all-interface
478 * default; if that's not set on the command line, that will
479 * be -1, meaning "use per-interface defaults", otherwise
480 * we'll fail if it's not one of the types the interface
481 * supports.
483 if ((device->active_dlt = capture_dev_user_linktype_find(device->name)) == -1) {
484 device->active_dlt = global_default_dlt;
488 * Is that one of the supported link-layer header types?
489 * If not, set it to -1, so we'll fall back on the first supported
490 * link-layer header type.
492 found_active_dlt = false;
493 for (list = device->links; list != NULL; list = g_list_next(list)) {
494 link = (link_row *)(list->data);
495 if (link->dlt != -1 && link->dlt == device->active_dlt) {
496 found_active_dlt = true;
497 break;
500 if (!found_active_dlt) {
501 device->active_dlt = -1;
503 if (device->active_dlt == -1) {
504 /* Fall back on the first supported DLT, if we have one. */
505 for (list = device->links; list != NULL; list = g_list_next(list)) {
506 link = (link_row *)(list->data);
507 if (link->dlt != -1) {
508 device->active_dlt = link->dlt;
509 break;
515 GString *
516 get_iface_list_string(capture_options *capture_opts, uint32_t style)
518 GString *iface_list_string = g_string_new("");
519 unsigned i;
522 * If we have a descriptive name for the interface, show that,
523 * rather than its raw name. On NT 5.x (2K/XP/Server2K3), the
524 * interface name is something like "\Device\NPF_{242423..."
525 * which is pretty useless to the normal user. On other platforms,
526 * it might be less cryptic, but if a more descriptive name is
527 * available, we should still use that.
529 #ifdef _WIN32
530 if (capture_opts->ifaces->len < 2) {
531 #else
532 if (capture_opts->ifaces->len < 4) {
533 #endif
534 for (i = 0; i < capture_opts->ifaces->len; i++) {
535 if (i > 0) {
536 if (capture_opts->ifaces->len > 2) {
537 g_string_append_printf(iface_list_string, ",");
539 g_string_append_printf(iface_list_string, " ");
540 if (i == capture_opts->ifaces->len - 1) {
541 g_string_append_printf(iface_list_string, "and ");
545 interface_options *interface_opts = &g_array_index(capture_opts->ifaces, interface_options, i);
547 if (style & IFLIST_QUOTE_IF_DESCRIPTION)
548 g_string_append_printf(iface_list_string, "'");
549 /* If we have a special user-supplied description (via the prefs
550 * or the documented "-X stdin_descr" option for stdin), make sure
551 * we're using it.
553 char *user_descr = capture_dev_user_descr_find(interface_opts->name);
554 if (user_descr != NULL) {
555 if (g_strcmp0(interface_opts->descr, user_descr) != 0) {
556 g_free(interface_opts->descr);
557 interface_opts->descr = user_descr;
558 g_free(interface_opts->display_name);
559 interface_opts->display_name = g_strdup(interface_opts->descr);
560 } else {
561 g_free(user_descr);
564 if (interface_opts->display_name == NULL) {
566 * We don't have a display name; generate one.
567 * fill_in_interface_opts_from_finfo and
568 * capture_opts_add_iface_opt always fill in
569 * the display name, so this shouldn't be necessary.
571 if (interface_opts->descr == NULL) {
572 if (interface_opts->name != NULL)
573 interface_opts->descr = get_interface_descriptive_name(capture_opts, interface_opts->name);
574 else
575 interface_opts->descr = g_strdup("(Unknown)");
577 interface_opts->display_name = g_strdup(interface_opts->descr);
579 g_string_append_printf(iface_list_string, "%s", interface_opts->display_name);
580 if (style & IFLIST_QUOTE_IF_DESCRIPTION)
581 g_string_append_printf(iface_list_string, "'");
582 if (style & IFLIST_SHOW_FILTER) {
583 if (interface_opts->cfilter != NULL &&
584 strlen(interface_opts->cfilter) > 0) {
585 g_string_append_printf(iface_list_string, " (%s)", interface_opts->cfilter);
589 } else {
590 g_string_append_printf(iface_list_string, "%u interfaces", capture_opts->ifaces->len);
592 return iface_list_string;
594 #endif /* HAVE_LIBPCAP */