2 * Utilities for capture user interfaces
4 * Wireshark - Network traffic analyzer
5 * By Gerald Combs <gerald@wireshark.org>
6 * Copyright 1998 Gerald Combs
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
33 #include "epan/prefs.h"
34 #include "epan/ex-opt.h"
35 #include "capture_ifinfo.h"
36 #include "capture_ui_utils.h"
37 #include "wiretap/wtap.h"
38 #include "epan/to_str.h"
41 * Find user-specified capture device description that matches interface
45 capture_dev_user_descr_find(const gchar *if_name)
53 if ((prefs.capture_devices_descr == NULL) ||
54 (*prefs.capture_devices_descr == '\0')) {
55 /* There are no descriptions. */
59 if ((p = strstr(prefs.capture_devices_descr, if_name)) == NULL) {
60 /* There are, but there isn't one for this interface. */
65 /* error: ran into next interface description */
68 /* found left parenthesis, start of description */
72 /* skip over left parenthesis */
74 /* save pointer to beginning of description */
79 /* end of description */
88 if ((lp == 1) && (ct > 0) && (p2 != NULL)) {
89 /* Allocate enough space to return the string,
90 which runs from p2 to p, plus a terminating
92 descr = (char *)g_malloc(p - p2 + 1);
93 memcpy(descr, p2, p - p2);
102 capture_dev_user_linktype_find(const gchar *if_name)
107 if ((prefs.capture_devices_linktypes == NULL) ||
108 (*prefs.capture_devices_linktypes == '\0')) {
109 /* There are no link-layer header types */
113 if ((p = strstr(prefs.capture_devices_linktypes, if_name)) == NULL) {
114 /* There are, but there isn't one for this interface. */
118 p += strlen(if_name) + 1;
119 linktype = strtol(p, &next, 10);
120 if (next == p || *next != ')' || linktype < 0) {
124 if (linktype > G_MAXINT) {
125 /* Value doesn't fit in a gint */
129 return (gint)linktype;
132 #if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
134 capture_dev_user_buffersize_find(const gchar *if_name)
139 if ((prefs.capture_devices_buffersize == NULL) ||
140 (*prefs.capture_devices_buffersize == '\0')) {
141 /* There are no buffersizes defined */
145 if ((p = strstr(prefs.capture_devices_buffersize, if_name)) == NULL) {
146 /* There are, but there isn't one for this interface. */
150 p += strlen(if_name) + 1;
151 buffersize = (gint)strtol(p, &next, 10);
152 if (next == p || *next != ')' || buffersize < 0) {
156 if (buffersize > G_MAXINT) {
157 /* Value doesn't fit in a gint */
161 return (gint)buffersize;
166 capture_dev_user_snaplen_find(const gchar *if_name)
171 if ((prefs.capture_devices_snaplen == NULL) ||
172 (*prefs.capture_devices_snaplen == '\0')) {
173 /* There is no snap length defined */
177 if ((p = strstr(prefs.capture_devices_snaplen, if_name)) == NULL) {
178 /* There are, but there isn't one for this interface. */
182 p += strlen(if_name) + 3;
183 snaplen = (gint)strtol(p, &next, 10);
184 if (next == p || *next != ')' || snaplen < 0) {
188 if (snaplen > WTAP_MAX_PACKET_SIZE) {
189 /* Value doesn't fit in a gint */
193 return (gint)snaplen;
197 capture_dev_user_hassnap_find(const gchar *if_name)
202 if ((prefs.capture_devices_snaplen == NULL) ||
203 (*prefs.capture_devices_snaplen == '\0')) {
204 /* There is no snap length defined */
208 if ((p = strstr(prefs.capture_devices_snaplen, if_name)) == NULL) {
209 /* There are, but there isn't one for this interface. */
213 p += strlen(if_name) + 1;
214 hassnap = (gboolean)strtol(p, &next, 10);
215 if (next == p || *next != '(') {
220 return (gboolean)hassnap;
224 capture_dev_user_pmode_find(const gchar *if_name)
229 if ((prefs.capture_devices_pmode == NULL) ||
230 (*prefs.capture_devices_pmode == '\0')) {
231 /* There is no promiscuous mode defined */
235 if ((p = strstr(prefs.capture_devices_pmode, if_name)) == NULL) {
236 /* There are, but there isn't one for this interface. */
240 p += strlen(if_name) + 1;
241 pmode = (gboolean)strtol(p, &next, 10);
242 if (next == p || *next != ')') {
246 return (gboolean)pmode;
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 capture_interface_list() supplies a description, use that,
253 * otherwise use the interface name.
255 * The result must be g_free()'d when you're done with it.
257 * Note: given that this calls capture_interface_list(), which attempts to
258 * open all adapters it finds in order to check whether they can be
259 * captured on, this is an expensive routine to call, so don't call it
263 get_interface_descriptive_name(const char *if_name)
271 /* Do we have a user-supplied description? */
272 descr = capture_dev_user_descr_find(if_name);
274 /* Yes - make a copy of that. */
275 descr = g_strdup(descr);
276 } else if (strcmp(if_name, "-") == 0) {
278 * Strictly speaking, -X (extension) options are for modules, e.g. Lua
279 * and using one here stretches that definition. However, this doesn't
280 * waste a single-letter option on something that might be rarely used
281 * and is backward-compatible to 1.0.
283 descr = g_strdup(ex_opt_get_nth("stdin_descr", 0));
285 descr = g_strdup("Standard input");
288 /* No, we don't have a user-supplied description; did we get
289 one from the OS or libpcap? */
291 if_list = capture_interface_list(&err, NULL, NULL);
292 if (if_list != NULL) {
295 if_info = (if_info_t *)if_entry->data;
296 if (strcmp(if_info->name, if_name) == 0) {
297 if (if_info->friendly_name != NULL) {
298 /* We have a "friendly name"; return a copy of that
299 as the description - when we free the interface
300 list, that'll also free up the strings to which
302 descr = g_strdup(if_info->friendly_name);
303 } else if (if_info->vendor_description != NULL) {
304 /* We have no "friendly name", but we have a vendor
305 description; return a copy of that - when we free
306 the interface list, that'll also free up the strings
307 to which it refers. */
308 descr = g_strdup(if_info->vendor_description);
312 } while ((if_entry = g_list_next(if_entry)) != NULL);
314 free_interface_list(if_list);
317 /* The interface name is all we have, so just return a copy of that. */
318 descr = g_strdup(if_name);
326 /* search interface info by interface name */
328 search_info(GList *if_list, gchar *if_name)
334 for (if_entry = if_list; if_entry != NULL; if_entry = g_list_next(if_entry)) {
335 if_info = (if_info_t *)if_entry->data;
337 if(strcmp(if_name, if_info->name) == 0) {
346 /* build the string to display in the combo box for the given interface */
348 build_capture_combo_name(GList *if_list, gchar *if_name)
354 /* Do we have a user-supplied description? */
355 descr = capture_dev_user_descr_find(if_name);
357 /* Yes, we have a user-supplied description; use it. */
358 if_string = g_strdup_printf("%s: %s", descr, if_name);
361 /* No, we don't have a user-supplied description; did we get
362 one from the OS or libpcap? */
363 if_info = search_info(if_list, if_name);
364 if (if_info != NULL && if_info->vendor_description != NULL) {
366 if_string = g_strdup_printf("%s: %s", if_info->vendor_description,
370 if_string = g_strdup(if_name);
379 build_capture_combo_list(GList *if_list, gboolean do_hide)
388 if (if_list != NULL) {
389 /* Scan through the list and build a list of strings to display. */
390 for (if_entry = if_list; if_entry != NULL;
391 if_entry = g_list_next(if_entry)) {
392 if_info = (if_info_t *)if_entry->data;
394 /* Is this interface hidden and, if so, should we include it
396 if (!prefs_is_capture_device_hidden(if_info->name) || !do_hide) {
397 /* It's not hidden, or it is but we should include it in the list. */
399 /* Do we have a user-supplied description? */
400 descr = capture_dev_user_descr_find(if_info->name);
402 /* Yes, we have a user-supplied description; use it. */
403 if_string = g_strdup_printf("%s: %s", descr, if_info->name);
406 /* No, we don't have a user-supplied description; did we get
407 one from the OS or libpcap? */
408 if (if_info->vendor_description != NULL) {
410 if_string = g_strdup_printf("%s: %s",
411 if_info->vendor_description,
415 if_string = g_strdup(if_info->name);
418 combo_list = g_list_append(combo_list, if_string);
426 free_if_string(gpointer data, gpointer user_data _U_)
432 free_capture_combo_list(GList *combo_list)
434 if (combo_list != NULL) {
435 g_list_foreach(combo_list, free_if_string, NULL);
436 g_list_free(combo_list);
441 * Given text that contains an interface name possibly prefixed by an
442 * interface description, extract the interface name.
445 get_if_name(const char *if_text)
451 * We cannot assume that the interface name doesn't contain a space;
452 * some names on Windows OT do.
454 * We also can't assume it begins with "\Device\", either, as, on
455 * Windows OT, WinPcap doesn't put "\Device\" in front of the name.
457 * As I remember, we can't assume that the interface description
458 * doesn't contain a colon, either; I think some do.
460 * We can probably assume that the interface *name* doesn't contain
461 * a colon, however; if any interface name does contain a colon on
462 * Windows, it'll be time to just get rid of the damn interface
463 * descriptions in the drop-down list, have just the names in the
464 * drop-down list, and have a "Browse..." button to browse for interfaces,
465 * with names, descriptions, IP addresses, blah blah blah available when
468 * So we search backwards for a colon. If we don't find it, just
469 * return the entire string; otherwise, skip the colon and any blanks
470 * after it, and return that string.
472 if_name = if_text + strlen(if_text);
474 if (if_name == if_text) {
475 /* We're at the beginning of the string; return it. */
479 if (*if_name == ':') {
481 * We've found a colon.
482 * Unfortunately, a colon is used in the string "rpcap://",
483 * which is used in case of a remote capture.
484 * So we'll check to make sure the colon isn't followed by "//";
485 * it'll be followed by a blank if it separates the description
486 * and the interface name. (We don't wire in "rpcap", in case we
487 * support other protocols in the same syntax.)
488 * Unfortunately, another colon can be used in "rpcap://host:port/"
489 * before port. Check if colon is followed by digit.
491 if ((strncmp(if_name, "://", 3) != 0) && !isdigit(if_name[1])) {
493 * OK, we've found a colon followed neither by "//" nor by digit.
494 * Skip blanks following it.
497 while (*if_name == ' ')
502 /* Keep looking for a colon not followed by "//". */
506 * There's a space between the interface description and name, and
507 * the interface name shouldn't have a space in it (it doesn't, on
508 * UNIX systems); look backwards in the string for a space.
510 * (An interface name might, however, contain a colon in it, which
511 * is why we don't use the colon search on UNIX.)
513 if_name = strrchr(if_text, ' ');
514 if (if_name == NULL) {
523 /* Return interface_opts->descr (after setting it if it is not set)
524 * This is necessary because capture_opts.c can't set descr (at least
525 * not without adding significant dependencies there).
528 get_iface_description_for_interface(capture_options *capture_opts, guint i)
530 interface_options interface_opts;
532 if (i < capture_opts->ifaces->len) {
533 interface_opts = g_array_index(capture_opts->ifaces, interface_options, i);
534 if (!interface_opts.descr && interface_opts.name) {
535 interface_opts.descr = get_interface_descriptive_name(interface_opts.name);
536 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, i);
537 g_array_insert_val(capture_opts->ifaces, i, interface_opts);
539 return (interface_opts.descr);
545 #endif /* HAVE_LIBPCAP */