2 * Utilities for capture user interfaces
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
37 #include "epan/prefs.h"
38 #include "epan/ex-opt.h"
39 #include "capture_ifinfo.h"
40 #include "capture_ui_utils.h"
43 * Find user-specified capture device description that matches interface
47 capture_dev_user_descr_find(const gchar *if_name)
55 if (prefs.capture_devices_descr == NULL) {
56 /* There are no descriptions. */
60 if ((p = strstr(prefs.capture_devices_descr, if_name)) == NULL) {
61 /* There are, but there isn't one for this interface. */
66 /* error: ran into next interface description */
69 /* found left parenthesis, start of description */
73 /* skip over left parenthesis */
75 /* save pointer to beginning of description */
80 /* end of description */
89 if ((lp == 1) && (ct > 0) && (p2 != NULL)) {
90 /* Allocate enough space to return the string,
91 which runs from p2 to p, plus a terminating
93 descr = g_malloc(p - p2 + 1);
94 memcpy(descr, p2, p - p2);
103 capture_dev_user_linktype_find(const gchar *if_name)
108 if (prefs.capture_devices_linktypes == NULL) {
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;
133 * Return as descriptive a name for an interface as we can get.
134 * If the user has specified a comment, use that. Otherwise,
135 * if capture_interface_list() supplies a description, use that,
136 * otherwise use the interface name.
138 * The result must be g_free()'d when you're done with it.
140 * Note: given that this calls capture_interface_list(), which attempts to
141 * open all adapters it finds in order to check whether they can be
142 * captured on, this is an expensive routine to call, so don't call it
146 get_interface_descriptive_name(const char *if_name)
154 /* Do we have a user-supplied description? */
155 descr = capture_dev_user_descr_find(if_name);
157 /* Yes - make a copy of that. */
158 descr = g_strdup(descr);
159 } else if (strcmp(if_name, "-") == 0) {
161 * Strictly speaking, -X (extension) options are for modules, e.g. Lua
162 * and using one here stretches that definition. However, this doesn't
163 * waste a single-letter option on something that might be rarely used
164 * and is backward-compatible to 1.0.
166 descr = g_strdup(ex_opt_get_nth("stdin_descr", 0));
168 descr = g_strdup("Standard input");
171 /* No, we don't have a user-supplied description; did we get
172 one from the OS or libpcap? */
174 if_list = capture_interface_list(&err, NULL);
175 if (if_list != NULL && if_name != NULL) {
178 if_info = if_entry->data;
179 if (strcmp(if_info->name, if_name) == 0) {
180 if (if_info->description != NULL) {
181 /* Return a copy of that - when we free the interface
182 list, that'll also free up the strings to which
184 descr = g_strdup(if_info->description);
188 } while ((if_entry = g_list_next(if_entry)) != NULL);
190 free_interface_list(if_list);
193 /* The interface name is all we have, so just return a copy of that. */
194 descr = g_strdup(if_name);
202 /* search interface info by interface name */
204 search_info(GList *if_list, gchar *if_name)
210 for (if_entry = if_list; if_entry != NULL; if_entry = g_list_next(if_entry)) {
211 if_info = if_entry->data;
213 if(strcmp(if_name, if_info->name) == 0) {
222 /* build the string to display in the combo box for the given interface */
224 build_capture_combo_name(GList *if_list, gchar *if_name)
231 /* Do we have a user-supplied description? */
232 descr = capture_dev_user_descr_find(if_name);
234 /* Yes, we have a user-supplied description; use it. */
235 if_string = g_strdup_printf("%s: %s", descr, if_name);
238 /* No, we don't have a user-supplied description; did we get
239 one from the OS or libpcap? */
240 if_info = search_info(if_list, if_name);
241 if (if_info && if_info->description != NULL) {
243 if_string = g_strdup_printf("%s: %s", if_info->description,
247 if_string = g_strdup(if_name);
256 build_capture_combo_list(GList *if_list, gboolean do_hide)
265 if (if_list != NULL) {
266 /* Scan through the list and build a list of strings to display. */
267 for (if_entry = if_list; if_entry != NULL;
268 if_entry = g_list_next(if_entry)) {
269 if_info = if_entry->data;
271 /* Is this interface hidden and, if so, should we include it
273 if (!prefs_is_capture_device_hidden(if_info->name) || !do_hide) {
274 /* It's not hidden, or it is but we should include it in the list. */
276 /* Do we have a user-supplied description? */
277 descr = capture_dev_user_descr_find(if_info->name);
279 /* Yes, we have a user-supplied description; use it. */
280 if_string = g_strdup_printf("%s: %s", descr, if_info->name);
283 /* No, we don't have a user-supplied description; did we get
284 one from the OS or libpcap? */
285 if (if_info->description != NULL) {
287 if_string = g_strdup_printf("%s: %s", if_info->description,
291 if_string = g_strdup(if_info->name);
294 combo_list = g_list_append(combo_list, if_string);
302 free_if_string(gpointer data, gpointer user_data _U_)
308 free_capture_combo_list(GList *combo_list)
310 if (combo_list != NULL) {
311 g_list_foreach(combo_list, free_if_string, NULL);
312 g_list_free(combo_list);
317 * Given text that contains an interface name possibly prefixed by an
318 * interface description, extract the interface name.
321 get_if_name(const char *if_text)
327 * We cannot assume that the interface name doesn't contain a space;
328 * some names on Windows OT do.
330 * We also can't assume it begins with "\Device\", either, as, on
331 * Windows OT, WinPcap doesn't put "\Device\" in front of the name.
333 * As I remember, we can't assume that the interface description
334 * doesn't contain a colon, either; I think some do.
336 * We can probably assume that the interface *name* doesn't contain
337 * a colon, however; if any interface name does contain a colon on
338 * Windows, it'll be time to just get rid of the damn interface
339 * descriptions in the drop-down list, have just the names in the
340 * drop-down list, and have a "Browse..." button to browse for interfaces,
341 * with names, descriptions, IP addresses, blah blah blah available when
344 * So we search backwards for a colon. If we don't find it, just
345 * return the entire string; otherwise, skip the colon and any blanks
346 * after it, and return that string.
348 if_name = if_text + strlen(if_text);
350 if (if_name == if_text) {
351 /* We're at the beginning of the string; return it. */
355 if (*if_name == ':') {
357 * We've found a colon.
358 * Unfortunately, a colon is used in the string "rpcap://",
359 * which is used in case of a remote capture.
360 * So we'll check to make sure the colon isn't followed by "//";
361 * it'll be followed by a blank if it separates the description
362 * and the interface name. (We don't wire in "rpcap", in case we
363 * support other protocols in the same syntax.)
364 * Unfortunately, another colon can be used in "rpcap://host:port/"
365 * before port. Check if colon is followed by digit.
367 if ((strncmp(if_name, "://", 3) != 0) && !isdigit(if_name[1])) {
369 * OK, we've found a colon followed neither by "//" nor by digit.
370 * Skip blanks following it.
373 while (*if_name == ' ')
378 /* Keep looking for a colon not followed by "//". */
382 * There's a space between the interface description and name, and
383 * the interface name shouldn't have a space in it (it doesn't, on
384 * UNIX systems); look backwards in the string for a space.
386 * (An interface name might, however, contain a colon in it, which
387 * is why we don't use the colon search on UNIX.)
389 if_name = strrchr(if_text, ' ');
390 if (if_name == NULL) {
399 /* Return capture_opts->iface_descr (after setting it if it is not set)
400 * This is necessary because capture_opts.c can't set iface_descr (at least
401 * not without adding significant dependencies there).
404 get_iface_description(capture_options *capture_opts)
406 if (!capture_opts->iface_descr && capture_opts->iface)
407 capture_opts->iface_descr = get_interface_descriptive_name(capture_opts->iface);
409 return(capture_opts->iface_descr);
412 #endif /* HAVE_LIBPCAP */