translate some german to english
[obnox/wireshark/wip.git] / capture_ui_utils.c
1 /* capture_ui_utils.c
2  * Utilities for capture user interfaces
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
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.
14  *
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.
19  *
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.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #ifdef HAVE_LIBPCAP
30
31 #include <string.h>
32 #include <glib.h>
33
34 #include <epan/prefs.h>
35 #include "capture-pcap-util.h"
36 #include "capture_ui_utils.h"
37
38 /*
39  * Find user-specified capture device description that matches interface
40  * name, if any.
41  */
42 static char *
43 capture_dev_user_descr_find(const gchar *if_name)
44 {
45         char    *p;
46         char    *p2 = NULL;
47         char    *descr = NULL;
48         int     lp = 0;
49         int     ct = 0;
50
51         if (prefs.capture_devices_descr == NULL) {
52                 /* There are no descriptions. */
53                 return NULL;
54         }
55
56         if ((p = strstr(prefs.capture_devices_descr, if_name)) == NULL) {
57                 /* There are, but there isn't one for this interface. */
58                 return NULL;
59         }
60
61         while (*p != '\0') {
62                 /* error: ran into next interface description */
63                 if (*p == ',')
64                         return NULL;
65                 /* found left parenthesis, start of description */
66                 else if (*p == '(') {
67                         ct = 0;
68                         lp++;
69                         /* skip over left parenthesis */
70                         p++;
71                         /* save pointer to beginning of description */
72                         p2 = p;
73                         continue;
74                 }
75                 else if (*p == ')') {
76                         /* end of description */
77                         break;
78                 }
79                 else {
80                         p++;
81                         ct++;
82                 }
83         }
84
85         if ((lp == 1) && (ct > 0) && (p2 != NULL)) {
86                 /* Allocate enough space to return the string,
87                    which runs from p2 to p, plus a terminating
88                    '\0'. */
89                 descr = g_malloc(p - p2 + 1);
90                 memcpy(descr, p2, p - p2);
91                 descr[p - p2] = '\0';
92                 return descr;
93         }
94         else
95                 return NULL;
96 }
97
98 /*
99  * Return as descriptive a name for an interface as we can get.
100  * If the user has specified a comment, use that.  Otherwise,
101  * if get_interface_list() supplies a description, use that,
102  * otherwise use the interface name.
103  *
104  * The result must be g_free()'d when you're done with it.
105  *
106  * Note: given that this calls get_interface_list(), which attempts to
107  * open all adapters it finds in order to check whether they can be
108  * captured on, this is an expensive routine to call, so don't call it
109  * frequently.
110  */
111 char *
112 get_interface_descriptive_name(const char *if_name)
113 {
114   char *descr;
115   GList *if_list;
116   GList *if_entry;
117   if_info_t *if_info;
118   int err;
119
120   /* Do we have a user-supplied description? */
121   descr = capture_dev_user_descr_find(if_name);
122   if (descr != NULL) {
123     /* Yes - make a copy of that. */
124     descr = g_strdup(descr);
125   } else {
126     /* No, we don't have a user-supplied description; did we get
127        one from the OS or libpcap? */
128     descr = NULL;
129     if_list = get_interface_list(&err, NULL);
130     if (if_list != NULL && if_name != NULL) {
131       if_entry = if_list;
132       do {
133         if_info = if_entry->data;
134         if (strcmp(if_info->name, if_name) == 0) {
135           if (if_info->description != NULL) {
136             /* Return a copy of that - when we free the interface
137                list, that'll also free up the strings to which
138                it refers. */
139             descr = g_strdup(if_info->description);
140           }
141           break;
142         }
143       } while ((if_entry = g_list_next(if_entry)) != NULL);
144     }
145     free_interface_list(if_list);
146
147     if (descr == NULL) {
148       /* The interface name is all we have, so just return a copy of that. */
149       descr = g_strdup(if_name);
150     }
151   }
152
153   return descr;
154 }
155
156
157 /* search interface info by interface name */
158 static if_info_t *
159 search_info(GList *if_list, gchar *if_name)
160 {
161     GList *if_entry;
162     if_info_t *if_info;
163
164
165     for (if_entry = if_list; if_entry != NULL; if_entry = g_list_next(if_entry)) {
166         if_info = if_entry->data;
167
168         if(strcmp(if_name, if_info->name) == 0) {
169             return if_info;
170         }
171     }
172
173     return NULL;
174 }
175
176
177 /* build the string to display in the combo box for the given interface */
178 char *
179 build_capture_combo_name(GList *if_list, gchar *if_name)
180 {
181     gchar *descr;
182     char *if_string;
183     if_info_t *if_info;
184
185
186         /* Do we have a user-supplied description? */
187         descr = capture_dev_user_descr_find(if_name);
188         if (descr != NULL) {
189           /* Yes, we have a user-supplied description; use it. */
190           if_string = g_strdup_printf("%s: %s", descr, if_name);
191           g_free(descr);
192         } else {
193           /* No, we don't have a user-supplied description; did we get
194              one from the OS or libpcap? */
195       if_info = search_info(if_list, if_name);
196           if (if_info && if_info->description != NULL) {
197             /* Yes - use it. */
198             if_string = g_strdup_printf("%s: %s", if_info->description,
199                                         if_info->name);
200           } else {
201             /* No. */
202             if_string = g_strdup(if_name);
203           }
204         }
205
206     return if_string;
207 }
208
209
210 GList *
211 build_capture_combo_list(GList *if_list, gboolean do_hide)
212 {
213   GList *combo_list;
214   GList *if_entry;
215   if_info_t *if_info;
216   char *if_string;
217   gchar *descr;
218
219   combo_list = NULL;
220   if (if_list != NULL) {
221     /* Scan through the list and build a list of strings to display. */
222     for (if_entry = if_list; if_entry != NULL;
223          if_entry = g_list_next(if_entry)) {
224       if_info = if_entry->data;
225
226       /* Is this interface hidden and, if so, should we include it
227          anyway? */
228       if (prefs.capture_devices_hide == NULL ||
229           strstr(prefs.capture_devices_hide, if_info->name) == NULL ||
230           !do_hide) {
231         /* It's not hidden, or it is but we should include it in the list. */
232
233         /* Do we have a user-supplied description? */
234         descr = capture_dev_user_descr_find(if_info->name);
235         if (descr != NULL) {
236           /* Yes, we have a user-supplied description; use it. */
237           if_string = g_strdup_printf("%s: %s", descr, if_info->name);
238           g_free(descr);
239         } else {
240           /* No, we don't have a user-supplied description; did we get
241              one from the OS or libpcap? */
242           if (if_info->description != NULL) {
243             /* Yes - use it. */
244             if_string = g_strdup_printf("%s: %s", if_info->description,
245                                         if_info->name);
246           } else {
247             /* No. */
248             if_string = g_strdup(if_info->name);
249           }
250         }
251         combo_list = g_list_append(combo_list, if_string);
252       }
253     }
254   }
255   return combo_list;
256 }
257
258 static void
259 free_if_string(gpointer data, gpointer user_data _U_)
260 {
261   g_free(data);
262 }
263
264 void
265 free_capture_combo_list(GList *combo_list)
266 {
267   if (combo_list != NULL) {
268     g_list_foreach(combo_list, free_if_string, NULL);
269     g_list_free(combo_list);
270   }
271 }
272
273 /*
274  * Given text that contains an interface name possibly prefixed by an
275  * interface description, extract the interface name.
276  */
277 const char *
278 get_if_name(const char *if_text)
279 {
280   const char *if_name;
281
282 #ifdef _WIN32
283   /*
284    * We cannot assume that the interface name doesn't contain a space;
285    * some names on Windows OT do.
286    *
287    * We also can't assume it begins with "\Device\", either, as, on
288    * Windows OT, WinPcap doesn't put "\Device\" in front of the name.
289    *
290    * As I remember, we can't assume that the interface description
291    * doesn't contain a colon, either; I think some do.
292    *
293    * We can probably assume that the interface *name* doesn't contain
294    * a colon, however; if any interface name does contain a colon on
295    * Windows, it'll be time to just get rid of the damn interface
296    * descriptions in the drop-down list, have just the names in the
297    * drop-down list, and have a "Browse..." button to browse for interfaces,
298    * with names, descriptions, IP addresses, blah blah blah available when
299    * possible.
300    *
301    * So we search backwards for a colon.  If we don't find it, just
302    * return the entire string; otherwise, skip the colon and any blanks
303    * after it, and return that string.
304    */
305    if_name = if_text + strlen(if_text);
306    for (;;) {
307      if (if_name == if_text) {
308        /* We're at the beginning of the string; return it. */
309        break;
310      }
311      if_name--;
312      if (*if_name == ':') {
313        /*
314         * We've found a colon.
315         * Unfortunately, a colon is used in the string "rpcap://",
316         * which is used in case of a remote capture.
317         * So we'll check to make sure the colon isn't followed by "//";
318         * it'll be followed by a blank if it separates the description
319         * and the interface name.  (We don't wire in "rpcap", in case we
320         * support other protocols in the same syntax.)
321         */
322        if (strncmp(if_name, "://", 3) != 0) {
323          /*
324           * OK, we've found a colon not followed by "//".  Skip blanks
325           * following it.
326           */
327          if_name++;
328          while (*if_name == ' ')
329            if_name++;
330          break;
331        }
332      }
333      /* Keep looking for a colon not followed by "//". */
334    }
335 #else
336   /*
337    * There's a space between the interface description and name, and
338    * the interface name shouldn't have a space in it (it doesn't, on
339    * UNIX systems); look backwards in the string for a space.
340    *
341    * (An interface name might, however, contain a colon in it, which
342    * is why we don't use the colon search on UNIX.)
343    */
344   if_name = strrchr(if_text, ' ');
345   if (if_name == NULL) {
346     if_name = if_text;
347   } else {
348     if_name++;
349   }
350 #endif
351   return if_name;
352 }
353
354 /*  Return capture_opts->iface_descr (after setting it if it is not set)
355  *  This is necessary because capture_opts.c can't set iface_descr (at least
356  *  not without adding significant dependencies there).
357  */
358 const char *
359 get_iface_description(capture_options *capture_opts)
360 {
361         if (!capture_opts->iface_descr && capture_opts->iface)
362                 capture_opts->iface_descr = get_interface_descriptive_name(capture_opts->iface);
363
364         return(capture_opts->iface_descr);
365
366 }
367 #endif /* HAVE_LIBPCAP */