Add a much better workaround for bug #8382 and some expert info.
[metze/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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  */
24
25 #include "config.h"
26
27 #ifdef HAVE_LIBPCAP
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <ctype.h>
33 #include <glib.h>
34
35 #include "epan/prefs.h"
36 #include "epan/ex-opt.h"
37 #include "capture_ifinfo.h"
38 #include "capture_ui_utils.h"
39 #include "ui/simple_dialog.h"
40 #include "wiretap/wtap.h"
41 #include "epan/to_str.h"
42
43 /*
44  * Find user-specified capture device description that matches interface
45  * name, if any.
46  */
47 char *
48 capture_dev_user_descr_find(const gchar *if_name)
49 {
50   char *p;
51   char *p2 = NULL;
52   char *descr = NULL;
53   int lp = 0;
54   int ct = 0;
55
56   if ((prefs.capture_devices_descr == NULL) ||
57       (*prefs.capture_devices_descr == '\0')) {
58     /* There are no descriptions. */
59     return NULL;
60   }
61
62   if ((p = strstr(prefs.capture_devices_descr, if_name)) == NULL) {
63     /* There are, but there isn't one for this interface. */
64     return NULL;
65   }
66
67   while (*p != '\0') {
68     /* error: ran into next interface description */
69     if (*p == ',')
70       return NULL;
71     /* found left parenthesis, start of description */
72     else if (*p == '(') {
73       ct = 0;
74       lp++;
75       /* skip over left parenthesis */
76       p++;
77       /* save pointer to beginning of description */
78       p2 = p;
79       continue;
80     }
81     else if (*p == ')') {
82       /* end of description */
83       break;
84     }
85     else {
86         p++;
87         ct++;
88     }
89   }
90
91   if ((lp == 1) && (ct > 0) && (p2 != NULL)) {
92     /* Allocate enough space to return the string,
93        which runs from p2 to p, plus a terminating
94        '\0'. */
95     descr = g_malloc(p - p2 + 1);
96     memcpy(descr, p2, p - p2);
97     descr[p - p2] = '\0';
98     return descr;
99   }
100   else
101     return NULL;
102 }
103
104 gint
105 capture_dev_user_linktype_find(const gchar *if_name)
106 {
107   gchar *p, *next;
108   long linktype;
109
110   if ((prefs.capture_devices_linktypes == NULL) ||
111       (*prefs.capture_devices_linktypes == '\0')) {
112     /* There are no link-layer header types */
113     return -1;
114   }
115
116   if ((p = strstr(prefs.capture_devices_linktypes, if_name)) == NULL) {
117     /* There are, but there isn't one for this interface. */
118     return -1;
119   }
120
121   p += strlen(if_name) + 1;
122   linktype = strtol(p, &next, 10);
123   if (next == p || *next != ')' || linktype < 0) {
124     /* Syntax error */
125     return -1;
126   }
127   if (linktype > G_MAXINT) {
128     /* Value doesn't fit in a gint */
129     return -1;
130   }
131
132   return (gint)linktype;
133 }
134
135 /*
136  * Return as descriptive a name for an interface as we can get.
137  * If the user has specified a comment, use that.  Otherwise,
138  * if capture_interface_list() supplies a description, use that,
139  * otherwise use the interface name.
140  *
141  * The result must be g_free()'d when you're done with it.
142  *
143  * Note: given that this calls capture_interface_list(), which attempts to
144  * open all adapters it finds in order to check whether they can be
145  * captured on, this is an expensive routine to call, so don't call it
146  * frequently.
147  */
148 char *
149 get_interface_descriptive_name(const char *if_name)
150 {
151   char *descr;
152   GList *if_list;
153   GList *if_entry;
154   if_info_t *if_info;
155   int err;
156
157   /* Do we have a user-supplied description? */
158   descr = capture_dev_user_descr_find(if_name);
159   if (descr != NULL) {
160     /* Yes - make a copy of that. */
161     descr = g_strdup(descr);
162   } else if (strcmp(if_name, "-") == 0) {
163     /*
164      * Strictly speaking, -X (extension) options are for modules, e.g. Lua
165      * and using one here stretches that definition. However, this doesn't
166      * waste a single-letter option on something that might be rarely used
167      * and is backward-compatible to 1.0.
168      */
169     descr = g_strdup(ex_opt_get_nth("stdin_descr", 0));
170     if (!descr) {
171       descr = g_strdup("Standard input");
172     }
173   } else {
174     /* No, we don't have a user-supplied description; did we get
175        one from the OS or libpcap? */
176     descr = NULL;
177     if_list = capture_interface_list(&err, NULL);
178     if (if_list != NULL) {
179       if_entry = if_list;
180       do {
181         if_info = if_entry->data;
182         if (strcmp(if_info->name, if_name) == 0) {
183           if (if_info->friendly_name != NULL) {
184               /* We have a "friendly name"; return a copy of that
185                  as the description - when we free the interface
186                  list, that'll also free up the strings to which
187                  it refers. */
188               descr = g_strdup(if_info->friendly_name);
189           } else if (if_info->vendor_description != NULL) {
190             /* We have no "friendly name", but we have a vendor
191                description; return a copy of that - when we free
192                the interface list, that'll also free up the strings
193                to which it refers. */
194             descr = g_strdup(if_info->vendor_description);
195           }
196           break;
197         }
198       } while ((if_entry = g_list_next(if_entry)) != NULL);
199     }
200     free_interface_list(if_list);
201
202     if (descr == NULL) {
203       /* The interface name is all we have, so just return a copy of that. */
204       descr = g_strdup(if_name);
205     }
206   }
207
208   return descr;
209 }
210
211
212 /* search interface info by interface name */
213 static if_info_t *
214 search_info(GList *if_list, gchar *if_name)
215 {
216   GList *if_entry;
217   if_info_t *if_info;
218
219
220   for (if_entry = if_list; if_entry != NULL; if_entry = g_list_next(if_entry)) {
221     if_info = if_entry->data;
222
223     if(strcmp(if_name, if_info->name) == 0) {
224       return if_info;
225     }
226   }
227
228   return NULL;
229 }
230
231
232 /* build the string to display in the combo box for the given interface */
233 char *
234 build_capture_combo_name(GList *if_list, gchar *if_name)
235 {
236   gchar *descr;
237   char *if_string;
238   if_info_t *if_info;
239
240   /* Do we have a user-supplied description? */
241   descr = capture_dev_user_descr_find(if_name);
242   if (descr != NULL) {
243     /* Yes, we have a user-supplied description; use it. */
244     if_string = g_strdup_printf("%s: %s", descr, if_name);
245     g_free(descr);
246   } else {
247     /* No, we don't have a user-supplied description; did we get
248      one from the OS or libpcap? */
249     if_info = search_info(if_list, if_name);
250     if (if_info != NULL && if_info->vendor_description != NULL) {
251       /* Yes - use it. */
252       if_string = g_strdup_printf("%s: %s", if_info->vendor_description,
253                                   if_info->name);
254     } else {
255       /* No. */
256       if_string = g_strdup(if_name);
257     }
258   }
259
260   return if_string;
261 }
262
263
264 GList *
265 build_capture_combo_list(GList *if_list, gboolean do_hide)
266 {
267   GList *combo_list;
268   GList *if_entry;
269   if_info_t *if_info;
270   char *if_string;
271   gchar *descr;
272
273   combo_list = NULL;
274   if (if_list != NULL) {
275     /* Scan through the list and build a list of strings to display. */
276     for (if_entry = if_list; if_entry != NULL;
277          if_entry = g_list_next(if_entry)) {
278       if_info = if_entry->data;
279
280       /* Is this interface hidden and, if so, should we include it
281          anyway? */
282       if (!prefs_is_capture_device_hidden(if_info->name) || !do_hide) {
283         /* It's not hidden, or it is but we should include it in the list. */
284
285         /* Do we have a user-supplied description? */
286         descr = capture_dev_user_descr_find(if_info->name);
287         if (descr != NULL) {
288           /* Yes, we have a user-supplied description; use it. */
289           if_string = g_strdup_printf("%s: %s", descr, if_info->name);
290           g_free(descr);
291         } else {
292           /* No, we don't have a user-supplied description; did we get
293              one from the OS or libpcap? */
294           if (if_info->vendor_description != NULL) {
295             /* Yes - use it. */
296             if_string = g_strdup_printf("%s: %s",
297                                         if_info->vendor_description,
298                                         if_info->name);
299           } else {
300             /* No. */
301             if_string = g_strdup(if_info->name);
302           }
303         }
304         combo_list = g_list_append(combo_list, if_string);
305       }
306     }/*for*/
307   }
308   return combo_list;
309 }
310
311 static void
312 free_if_string(gpointer data, gpointer user_data _U_)
313 {
314   g_free(data);
315 }
316
317 void
318 free_capture_combo_list(GList *combo_list)
319 {
320   if (combo_list != NULL) {
321     g_list_foreach(combo_list, free_if_string, NULL);
322     g_list_free(combo_list);
323   }
324 }
325
326 /*
327  * Given text that contains an interface name possibly prefixed by an
328  * interface description, extract the interface name.
329  */
330 const char *
331 get_if_name(const char *if_text)
332 {
333   const char *if_name;
334
335 #ifdef _WIN32
336   /*
337    * We cannot assume that the interface name doesn't contain a space;
338    * some names on Windows OT do.
339    *
340    * We also can't assume it begins with "\Device\", either, as, on
341    * Windows OT, WinPcap doesn't put "\Device\" in front of the name.
342    *
343    * As I remember, we can't assume that the interface description
344    * doesn't contain a colon, either; I think some do.
345    *
346    * We can probably assume that the interface *name* doesn't contain
347    * a colon, however; if any interface name does contain a colon on
348    * Windows, it'll be time to just get rid of the damn interface
349    * descriptions in the drop-down list, have just the names in the
350    * drop-down list, and have a "Browse..." button to browse for interfaces,
351    * with names, descriptions, IP addresses, blah blah blah available when
352    * possible.
353    *
354    * So we search backwards for a colon.  If we don't find it, just
355    * return the entire string; otherwise, skip the colon and any blanks
356    * after it, and return that string.
357    */
358    if_name = if_text + strlen(if_text);
359    for (;;) {
360      if (if_name == if_text) {
361        /* We're at the beginning of the string; return it. */
362        break;
363      }
364      if_name--;
365      if (*if_name == ':') {
366        /*
367         * We've found a colon.
368         * Unfortunately, a colon is used in the string "rpcap://",
369         * which is used in case of a remote capture.
370         * So we'll check to make sure the colon isn't followed by "//";
371         * it'll be followed by a blank if it separates the description
372         * and the interface name.  (We don't wire in "rpcap", in case we
373         * support other protocols in the same syntax.)
374         * Unfortunately, another colon can be used in "rpcap://host:port/"
375         * before port. Check if colon is followed by digit.
376         */
377        if ((strncmp(if_name, "://", 3) != 0) && !isdigit(if_name[1])) {
378          /*
379           * OK, we've found a colon followed neither by "//" nor by digit.  
380           * Skip blanks following it.
381           */
382          if_name++;
383          while (*if_name == ' ')
384            if_name++;
385          break;
386        }
387      }
388      /* Keep looking for a colon not followed by "//". */
389    }
390 #else
391   /*
392    * There's a space between the interface description and name, and
393    * the interface name shouldn't have a space in it (it doesn't, on
394    * UNIX systems); look backwards in the string for a space.
395    *
396    * (An interface name might, however, contain a colon in it, which
397    * is why we don't use the colon search on UNIX.)
398    */
399   if_name = strrchr(if_text, ' ');
400   if (if_name == NULL) {
401     if_name = if_text;
402   } else {
403     if_name++;
404   }
405 #endif
406   return if_name;
407 }
408
409 /*  Return interface_opts->descr (after setting it if it is not set)
410  *  This is necessary because capture_opts.c can't set descr (at least
411  *  not without adding significant dependencies there).
412  */
413 const char *
414 get_iface_description_for_interface(capture_options *capture_opts, guint i)
415 {
416   interface_options interface_opts;
417
418   if (i < capture_opts->ifaces->len) {
419     interface_opts = g_array_index(capture_opts->ifaces, interface_options, i);
420     if (!interface_opts.descr && interface_opts.name) {
421       interface_opts.descr = get_interface_descriptive_name(interface_opts.name);
422       capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, i);
423       g_array_insert_val(capture_opts->ifaces, i, interface_opts);
424     }
425     return (interface_opts.descr);
426   } else {
427     return (NULL);
428   }
429 }
430
431 #endif /* HAVE_LIBPCAP */