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
);
142 capture_dev_user_buffersize_find(const char *if_name
)
144 return capture_dev_get_if_int_property(prefs
.capture_devices_buffersize
, if_name
);
148 capture_dev_user_snaplen_find(const char *if_name
, bool *hassnap
, int *snaplen
)
154 if (if_name
== NULL
|| strlen(if_name
) < 1) {
158 if ((prefs
.capture_devices_snaplen
== NULL
) ||
159 (*prefs
.capture_devices_snaplen
== '\0')) {
160 /* There are no snap lengths defined */
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
++) {
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
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. */
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. */
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. */
207 if (!ws_strtoi(colonp
+ 3, &next
, &value
) ||
208 next
== colonp
+ 3 || *next
!= ')' || value
< 0) {
209 /* Syntax error or range error. Give up. */
216 /* Bad {hassnap}. Give up. */
222 g_strfreev(if_tokens
);
228 capture_dev_user_pmode_find(const char *if_name
, bool *pmode
)
232 value
= capture_dev_get_if_int_property(prefs
.capture_devices_pmode
, if_name
);
234 /* Not found or bad. */
237 *pmode
= (value
!= 0);
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.
261 get_interface_descriptive_name(const capture_options
*capture_opts
, const char *if_name
)
269 /* Do we have a user-supplied description? */
270 descr
= capture_dev_user_descr_find(if_name
);
272 /* No; try to construct a descriptive name. */
273 if (strcmp(if_name
, "-") == 0) {
274 descr
= g_strdup("Standard input");
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
) {
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
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
);
300 } while ((if_entry
= g_list_next(if_entry
)) != NULL
);
302 free_interface_list(if_list
);
305 /* The interface name is all we have, so just return a copy of that. */
306 descr
= g_strdup(if_name
);
315 build_capture_combo_list(GList
*if_list
, bool do_hide
)
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
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
);
338 /* Yes, we have a user-supplied description; use it. */
339 if_string
= ws_strdup_printf("%s: %s", descr
, if_info
->name
);
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
) {
346 if_string
= ws_strdup_printf("%s: %s",
347 if_info
->vendor_description
,
351 if_string
= g_strdup(if_info
->name
);
354 combo_list
= g_list_prepend(combo_list
, if_string
);
358 combo_list
= g_list_reverse(combo_list
);
365 free_if_string(void *data
, void *user_data _U_
)
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.
384 get_if_name(const char *if_text
)
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
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
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
);
416 if (if_name
== if_text
) {
417 /* We're at the beginning of the string; return it. */
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.
439 while (*if_name
== ' ')
444 /* Keep looking for a colon not followed by "//". */
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
) {
466 * Set the active DLT for a device appropriately.
469 set_active_dlt(interface_t
*device
, int global_default_dlt
)
472 bool found_active_dlt
;
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
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;
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
;
516 get_iface_list_string(capture_options
*capture_opts
, uint32_t style
)
518 GString
*iface_list_string
= g_string_new("");
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.
530 if (capture_opts
->ifaces
->len
< 2) {
532 if (capture_opts
->ifaces
->len
< 4) {
534 for (i
= 0; i
< capture_opts
->ifaces
->len
; i
++) {
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
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
);
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
);
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
);
590 g_string_append_printf(iface_list_string
, "%u interfaces", capture_opts
->ifaces
->len
);
592 return iface_list_string
;
594 #endif /* HAVE_LIBPCAP */