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.
36 #include <epan/prefs.h>
37 #include "capture-pcap-util.h"
38 #include "capture_ui_utils.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 /* There are no descriptions. */
58 if ((p = strstr(prefs.capture_devices_descr, if_name)) == NULL) {
59 /* There are, but there isn't one for this interface. */
64 /* error: ran into next interface description */
67 /* found left parenthesis, start of description */
71 /* skip over left parenthesis */
73 /* save pointer to beginning of description */
78 /* end of description */
87 if ((lp == 1) && (ct > 0) && (p2 != NULL)) {
88 /* Allocate enough space to return the string,
89 which runs from p2 to p, plus a terminating
91 descr = g_malloc(p - p2 + 1);
92 memcpy(descr, p2, p - p2);
101 capture_dev_user_linktype_find(const gchar *if_name)
106 if (prefs.capture_devices_linktypes == NULL) {
107 /* There are no link-layer header types */
111 if ((p = strstr(prefs.capture_devices_linktypes, if_name)) == NULL) {
112 /* There are, but there isn't one for this interface. */
116 p += strlen(if_name) + 1;
117 linktype = strtol(p, &next, 10);
118 if (next == p || *next != ')' || linktype < 0) {
127 * Return as descriptive a name for an interface as we can get.
128 * If the user has specified a comment, use that. Otherwise,
129 * if get_interface_list() supplies a description, use that,
130 * otherwise use the interface name.
132 * The result must be g_free()'d when you're done with it.
134 * Note: given that this calls get_interface_list(), which attempts to
135 * open all adapters it finds in order to check whether they can be
136 * captured on, this is an expensive routine to call, so don't call it
140 get_interface_descriptive_name(const char *if_name)
148 /* Do we have a user-supplied description? */
149 descr = capture_dev_user_descr_find(if_name);
151 /* Yes - make a copy of that. */
152 descr = g_strdup(descr);
154 /* No, we don't have a user-supplied description; did we get
155 one from the OS or libpcap? */
157 if_list = get_interface_list(&err, NULL);
158 if (if_list != NULL && if_name != NULL) {
161 if_info = if_entry->data;
162 if (strcmp(if_info->name, if_name) == 0) {
163 if (if_info->description != NULL) {
164 /* Return a copy of that - when we free the interface
165 list, that'll also free up the strings to which
167 descr = g_strdup(if_info->description);
171 } while ((if_entry = g_list_next(if_entry)) != NULL);
173 free_interface_list(if_list);
176 /* The interface name is all we have, so just return a copy of that. */
177 descr = g_strdup(if_name);
185 /* search interface info by interface name */
187 search_info(GList *if_list, gchar *if_name)
193 for (if_entry = if_list; if_entry != NULL; if_entry = g_list_next(if_entry)) {
194 if_info = if_entry->data;
196 if(strcmp(if_name, if_info->name) == 0) {
205 /* build the string to display in the combo box for the given interface */
207 build_capture_combo_name(GList *if_list, gchar *if_name)
214 /* Do we have a user-supplied description? */
215 descr = capture_dev_user_descr_find(if_name);
217 /* Yes, we have a user-supplied description; use it. */
218 if_string = g_strdup_printf("%s: %s", descr, if_name);
221 /* No, we don't have a user-supplied description; did we get
222 one from the OS or libpcap? */
223 if_info = search_info(if_list, if_name);
224 if (if_info && if_info->description != NULL) {
226 if_string = g_strdup_printf("%s: %s", if_info->description,
230 if_string = g_strdup(if_name);
239 build_capture_combo_list(GList *if_list, gboolean do_hide)
248 if (if_list != NULL) {
249 /* Scan through the list and build a list of strings to display. */
250 for (if_entry = if_list; if_entry != NULL;
251 if_entry = g_list_next(if_entry)) {
252 if_info = if_entry->data;
254 /* Is this interface hidden and, if so, should we include it
256 if (!prefs_is_capture_device_hidden(if_info->name) || !do_hide) {
257 /* It's not hidden, or it is but we should include it in the list. */
259 /* Do we have a user-supplied description? */
260 descr = capture_dev_user_descr_find(if_info->name);
262 /* Yes, we have a user-supplied description; use it. */
263 if_string = g_strdup_printf("%s: %s", descr, if_info->name);
266 /* No, we don't have a user-supplied description; did we get
267 one from the OS or libpcap? */
268 if (if_info->description != NULL) {
270 if_string = g_strdup_printf("%s: %s", if_info->description,
274 if_string = g_strdup(if_info->name);
277 combo_list = g_list_append(combo_list, if_string);
285 free_if_string(gpointer data, gpointer user_data _U_)
291 free_capture_combo_list(GList *combo_list)
293 if (combo_list != NULL) {
294 g_list_foreach(combo_list, free_if_string, NULL);
295 g_list_free(combo_list);
300 * Given text that contains an interface name possibly prefixed by an
301 * interface description, extract the interface name.
304 get_if_name(const char *if_text)
310 * We cannot assume that the interface name doesn't contain a space;
311 * some names on Windows OT do.
313 * We also can't assume it begins with "\Device\", either, as, on
314 * Windows OT, WinPcap doesn't put "\Device\" in front of the name.
316 * As I remember, we can't assume that the interface description
317 * doesn't contain a colon, either; I think some do.
319 * We can probably assume that the interface *name* doesn't contain
320 * a colon, however; if any interface name does contain a colon on
321 * Windows, it'll be time to just get rid of the damn interface
322 * descriptions in the drop-down list, have just the names in the
323 * drop-down list, and have a "Browse..." button to browse for interfaces,
324 * with names, descriptions, IP addresses, blah blah blah available when
327 * So we search backwards for a colon. If we don't find it, just
328 * return the entire string; otherwise, skip the colon and any blanks
329 * after it, and return that string.
331 if_name = if_text + strlen(if_text);
333 if (if_name == if_text) {
334 /* We're at the beginning of the string; return it. */
338 if (*if_name == ':') {
340 * We've found a colon.
341 * Unfortunately, a colon is used in the string "rpcap://",
342 * which is used in case of a remote capture.
343 * So we'll check to make sure the colon isn't followed by "//";
344 * it'll be followed by a blank if it separates the description
345 * and the interface name. (We don't wire in "rpcap", in case we
346 * support other protocols in the same syntax.)
347 * Unfortunately, another colon can be used in "rpcap://host:port/"
348 * before port. Check if colon is followed by digit.
350 if ((strncmp(if_name, "://", 3) != 0) && !isdigit(if_name[1])) {
352 * OK, we've found a colon followed neither by "//" nor by digit.
353 * Skip blanks following it.
356 while (*if_name == ' ')
361 /* Keep looking for a colon not followed by "//". */
365 * There's a space between the interface description and name, and
366 * the interface name shouldn't have a space in it (it doesn't, on
367 * UNIX systems); look backwards in the string for a space.
369 * (An interface name might, however, contain a colon in it, which
370 * is why we don't use the colon search on UNIX.)
372 if_name = strrchr(if_text, ' ');
373 if (if_name == NULL) {
382 /* Return capture_opts->iface_descr (after setting it if it is not set)
383 * This is necessary because capture_opts.c can't set iface_descr (at least
384 * not without adding significant dependencies there).
387 get_iface_description(capture_options *capture_opts)
389 if (!capture_opts->iface_descr && capture_opts->iface)
390 capture_opts->iface_descr = get_interface_descriptive_name(capture_opts->iface);
392 return(capture_opts->iface_descr);
395 #endif /* HAVE_LIBPCAP */