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
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
36 capture_dev_get_if_property(const char *pref
, const char *if_name
)
39 char *property
= NULL
;
42 if (if_name
== NULL
|| strlen(if_name
) < 1) {
46 if (pref
== NULL
|| strlen(pref
) < 1) {
47 /* There is no interface information list. */
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
66 opening_parenp
= strchr(if_tokens
[i
], '(');
67 if (opening_parenp
== NULL
) {
68 /* No opening parenthesis. Give up. */
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. */
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);
85 g_strfreev(if_tokens
);
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.
95 capture_dev_get_if_int_property(const char *pref
, const char *if_name
)
97 char *property_string
;
100 property_string
= capture_dev_get_if_property(pref
, if_name
);
101 if (property_string
== NULL
) {
102 /* No property found for this interface. */
105 if (!ws_strtoi(property_string
, NULL
, &property
)) {
106 /* Syntax error or range error */
107 g_free(property_string
);
111 g_free(property_string
);
116 * Find user-specified capture device description that matches interface
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));
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
);
150 capture_dev_user_snaplen_find(const char *if_name
, bool *hassnap
, int *snaplen
)
156 if (if_name
== NULL
|| strlen(if_name
) < 1) {
160 if ((prefs
.capture_devices_snaplen
== NULL
) ||
161 (*prefs
.capture_devices_snaplen
== '\0')) {
162 /* There are no snap lengths defined */
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
++) {
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
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. */
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. */
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. */
209 if (!ws_strtoi(colonp
+ 3, &next
, &value
) ||
210 next
== colonp
+ 3 || *next
!= ')' || value
< 0) {
211 /* Syntax error or range error. Give up. */
218 /* Bad {hassnap}. Give up. */
224 g_strfreev(if_tokens
);
230 capture_dev_user_pmode_find(const char *if_name
, bool *pmode
)
234 value
= capture_dev_get_if_int_property(prefs
.capture_devices_pmode
, if_name
);
236 /* Not found or bad. */
239 *pmode
= (value
!= 0);
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.
263 get_interface_descriptive_name(const capture_options
*capture_opts
, const char *if_name
)
271 /* Do we have a user-supplied description? */
272 descr
= capture_dev_user_descr_find(if_name
);
274 /* No; try to construct a descriptive name. */
275 if (strcmp(if_name
, "-") == 0) {
276 descr
= g_strdup("Standard input");
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
) {
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
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
);
302 } while ((if_entry
= g_list_next(if_entry
)) != NULL
);
304 free_interface_list(if_list
);
307 /* The interface name is all we have, so just return a copy of that. */
308 descr
= g_strdup(if_name
);
317 build_capture_combo_list(GList
*if_list
, bool do_hide
)
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
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
);
340 /* Yes, we have a user-supplied description; use it. */
341 if_string
= ws_strdup_printf("%s: %s", descr
, if_info
->name
);
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
) {
348 if_string
= ws_strdup_printf("%s: %s",
349 if_info
->vendor_description
,
353 if_string
= g_strdup(if_info
->name
);
356 combo_list
= g_list_prepend(combo_list
, if_string
);
360 combo_list
= g_list_reverse(combo_list
);
367 free_if_string(void *data
, void *user_data _U_
)
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.
386 get_if_name(const char *if_text
)
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
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
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
);
418 if (if_name
== if_text
) {
419 /* We're at the beginning of the string; return it. */
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.
441 while (*if_name
== ' ')
446 /* Keep looking for a colon not followed by "//". */
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
) {
468 * Set the active DLT for a device appropriately.
471 set_active_dlt(interface_t
*device
, int global_default_dlt
)
474 bool found_active_dlt
;
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
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;
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
;
518 get_iface_list_string(capture_options
*capture_opts
, uint32_t style
)
520 GString
*iface_list_string
= g_string_new("");
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.
532 if (capture_opts
->ifaces
->len
< 2) {
534 if (capture_opts
->ifaces
->len
< 4) {
536 for (i
= 0; i
< capture_opts
->ifaces
->len
; i
++) {
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
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
);
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
);
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
);
592 g_string_append_printf(iface_list_string
, "%u interfaces", capture_opts
->ifaces
->len
);
594 return iface_list_string
;
596 #endif /* HAVE_LIBPCAP */