2 * Utilities for capture user interfaces
6 * Ethereal - Network traffic analyzer
7 * By Gerald Combs <gerald@ethereal.com>
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.
35 #include <epan/prefs.h>
36 #include "pcap-util.h"
37 #include "capture_ui_utils.h"
40 * Find user-specified capture device description that matches interface
44 capture_dev_user_descr_find(const gchar *if_name)
52 if (prefs.capture_devices_descr == NULL) {
53 /* There are no descriptions. */
57 if ((p = strstr(prefs.capture_devices_descr, if_name)) == NULL) {
58 /* There are, but there isn't one for this interface. */
63 /* error: ran into next interface description */
66 /* found left parenthesis, start of description */
70 /* skip over left parenthesis */
72 /* save pointer to beginning of description */
77 /* end of description */
86 if ((lp == 1) && (ct > 0) && (p2 != NULL)) {
87 /* Allocate enough space to return the string,
88 which runs from p2 to p, plus a terminating
90 descr = g_malloc(p - p2 + 1);
91 memcpy(descr, p2, p - p2);
100 * Return as descriptive a name for an interface as we can get.
101 * If the user has specified a comment, use that. Otherwise,
102 * if get_interface_list() supplies a description, use that,
103 * otherwise use the interface name.
106 get_interface_descriptive_name(const char *if_name)
113 char err_buf[PCAP_ERRBUF_SIZE];
115 /* Do we have a user-supplied description? */
116 descr = capture_dev_user_descr_find(if_name);
118 /* Yes - make a copy of that. */
119 descr = g_strdup(descr);
121 /* No, we don't have a user-supplied description; did we get
122 one from the OS or libpcap? */
124 if_list = get_interface_list(&err, err_buf);
125 if (if_list != NULL) {
128 if_info = if_entry->data;
129 if (strcmp(if_info->name, if_name) == 0) {
130 if (if_info->description != NULL) {
131 /* Return a copy of that - when we free the interface
132 list, that'll also free up the strings to which
134 descr = g_strdup(if_info->description);
138 } while ((if_entry = g_list_next(if_entry)) != NULL);
140 free_interface_list(if_list);
143 /* The interface name is all we have, so just return a copy of that. */
144 descr = g_strdup(if_name);
152 build_capture_combo_list(GList *if_list, gboolean do_hide)
161 if (if_list != NULL) {
162 /* Scan through the list and build a list of strings to display. */
163 for (if_entry = if_list; if_entry != NULL;
164 if_entry = g_list_next(if_entry)) {
165 if_info = if_entry->data;
167 /* Is this interface hidden and, if so, should we include it
169 if (prefs.capture_devices_hide == NULL ||
170 strstr(prefs.capture_devices_hide, if_info->name) == NULL ||
172 /* It's not hidden, or it is but we should include it in the list. */
174 /* Do we have a user-supplied description? */
175 descr = capture_dev_user_descr_find(if_info->name);
177 /* Yes, we have a user-supplied description; use it. */
178 if_string = g_strdup_printf("%s: %s", descr, if_info->name);
181 /* No, we don't have a user-supplied description; did we get
182 one from the OS or libpcap? */
183 if (if_info->description != NULL) {
185 if_string = g_strdup_printf("%s: %s", if_info->description,
189 if_string = g_strdup(if_info->name);
192 combo_list = g_list_append(combo_list, if_string);
200 free_if_string(gpointer data, gpointer user_data _U_)
206 free_capture_combo_list(GList *combo_list)
208 if (combo_list != NULL) {
209 g_list_foreach(combo_list, free_if_string, NULL);
210 g_list_free(combo_list);
215 * Given text that contains an interface name possibly prefixed by an
216 * interface description, extract the interface name.
219 get_if_name(char *if_text)
225 * We cannot assume that the interface name doesn't contain a space;
226 * some names on Windows OT do.
228 * We also can't assume it begins with "\Device\", either, as, on
229 * Windows OT, WinPcap doesn't put "\Device\" in front of the name.
231 * As I remember, we can't assume that the interface description
232 * doesn't contain a colon, either; I think some do.
234 * We can probably assume that the interface *name* doesn't contain
235 * a colon, however; if any interface name does contain a colon on
236 * Windows, it'll be time to just get rid of the damn interface
237 * descriptions in the drop-down list, have just the names in the
238 * drop-down list, and have a "Browse..." button to browse for interfaces,
239 * with names, descriptions, IP addresses, blah blah blah available when
242 * So we search backwards for a colon. If we don't find it, just
243 * return the entire string; otherwise, skip the colon and any blanks
244 * after it, and return that string.
246 if_name = if_text + strlen(if_text);
248 if (if_name == if_text) {
249 /* We're at the beginning of the string; return it. */
253 if (*if_name == ':') {
255 * We've found a colon.
256 * Unfortunately, a colon is used in the string "rpcap://",
257 * which is used in case of a remote capture.
258 * So we'll check to make sure the colon isn't followed by "//";
259 * it'll be followed by a blank if it separates the description
260 * and the interface name. (We don't wire in "rpcap", in case we
261 * support other protocols in the same syntax.)
263 if (strncmp(if_name, "://", 3) != 0) {
265 * OK, we've found a colon not followed by "//". Skip blanks
269 while (*if_name == ' ')
274 /* Keep looking for a colon not followed by "//". */
278 * There's a space between the interface description and name, and
279 * the interface name shouldn't have a space in it (it doesn't, on
280 * UNIX systems); look backwards in the string for a space.
282 * (An interface name might, however, contain a colon in it, which
283 * is why we don't use the colon search on UNIX.)
285 if_name = strrchr(if_text, ' ');
286 if (if_name == NULL) {
295 #endif /* HAVE_LIBPCAP */