Suppress invalid or non-meaningful statistics - for example, without
[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 <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <ctype.h>
35 #include <glib.h>
36
37 #include "epan/prefs.h"
38 #include "epan/ex-opt.h"
39 #include "capture_ifinfo.h"
40 #include "capture_ui_utils.h"
41 #include "ui/simple_dialog.h"
42 #include "wiretap/wtap.h"
43 #include "epan/to_str.h"
44
45 /*
46  * Find user-specified capture device description that matches interface
47  * name, if any.
48  */
49 char *
50 capture_dev_user_descr_find(const gchar *if_name)
51 {
52         char    *p;
53         char    *p2 = NULL;
54         char    *descr = NULL;
55         int     lp = 0;
56         int     ct = 0;
57
58         if (prefs.capture_devices_descr == NULL) {
59                 /* There are no descriptions. */
60                 return NULL;
61         }
62
63         if ((p = strstr(prefs.capture_devices_descr, if_name)) == NULL) {
64                 /* There are, but there isn't one for this interface. */
65                 return NULL;
66         }
67
68         while (*p != '\0') {
69                 /* error: ran into next interface description */
70                 if (*p == ',')
71                         return NULL;
72                 /* found left parenthesis, start of description */
73                 else if (*p == '(') {
74                         ct = 0;
75                         lp++;
76                         /* skip over left parenthesis */
77                         p++;
78                         /* save pointer to beginning of description */
79                         p2 = p;
80                         continue;
81                 }
82                 else if (*p == ')') {
83                         /* end of description */
84                         break;
85                 }
86                 else {
87                         p++;
88                         ct++;
89                 }
90         }
91
92         if ((lp == 1) && (ct > 0) && (p2 != NULL)) {
93                 /* Allocate enough space to return the string,
94                    which runs from p2 to p, plus a terminating
95                    '\0'. */
96                 descr = g_malloc(p - p2 + 1);
97                 memcpy(descr, p2, p - p2);
98                 descr[p - p2] = '\0';
99                 return descr;
100         }
101         else
102                 return NULL;
103 }
104
105 gint
106 capture_dev_user_linktype_find(const gchar *if_name)
107 {
108         gchar *p, *next;
109         long linktype;
110
111         if (prefs.capture_devices_linktypes == NULL) {
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->description != NULL) {
184             /* Return a copy of that - when we free the interface
185                list, that'll also free up the strings to which
186                it refers. */
187             descr = g_strdup(if_info->description);
188           }
189           break;
190         }
191       } while ((if_entry = g_list_next(if_entry)) != NULL);
192     }
193     free_interface_list(if_list);
194
195     if (descr == NULL) {
196       /* The interface name is all we have, so just return a copy of that. */
197       descr = g_strdup(if_name);
198     }
199   }
200
201   return descr;
202 }
203
204
205 /* search interface info by interface name */
206 static if_info_t *
207 search_info(GList *if_list, gchar *if_name)
208 {
209   GList *if_entry;
210   if_info_t *if_info;
211
212
213   for (if_entry = if_list; if_entry != NULL; if_entry = g_list_next(if_entry)) {
214     if_info = if_entry->data;
215
216     if(strcmp(if_name, if_info->name) == 0) {
217       return if_info;
218     }
219   }
220
221   return NULL;
222 }
223
224
225 /* build the string to display in the combo box for the given interface */
226 char *
227 build_capture_combo_name(GList *if_list, gchar *if_name)
228 {
229   gchar *descr;
230   char *if_string;
231   if_info_t *if_info;
232
233   /* Do we have a user-supplied description? */
234   descr = capture_dev_user_descr_find(if_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_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_info = search_info(if_list, if_name);
243     if (if_info != NULL && if_info->description != NULL) {
244       /* Yes - use it. */
245       if_string = g_strdup_printf("%s: %s", if_info->description,
246                                   if_info->name);
247     } else {
248       /* No. */
249       if_string = g_strdup(if_name);
250     }
251   }
252
253   return if_string;
254 }
255
256
257 GList *
258 build_capture_combo_list(GList *if_list, gboolean do_hide)
259 {
260   GList *combo_list;
261   GList *if_entry;
262   if_info_t *if_info;
263   char *if_string;
264   gchar *descr;
265
266   combo_list = NULL;
267   if (if_list != NULL) {
268     /* Scan through the list and build a list of strings to display. */
269     for (if_entry = if_list; if_entry != NULL;
270          if_entry = g_list_next(if_entry)) {
271       if_info = if_entry->data;
272
273       /* Is this interface hidden and, if so, should we include it
274          anyway? */
275       if (!prefs_is_capture_device_hidden(if_info->name) || !do_hide) {
276         /* It's not hidden, or it is but we should include it in the list. */
277
278         /* Do we have a user-supplied description? */
279         descr = capture_dev_user_descr_find(if_info->name);
280         if (descr != NULL) {
281               /* Yes, we have a user-supplied description; use it. */
282               if_string = g_strdup_printf("%s: %s", descr, if_info->name);
283           g_free(descr);
284         } else {
285           /* No, we don't have a user-supplied description; did we get
286                  one from the OS or libpcap? */
287           if (if_info->description != NULL) {
288             /* Yes - use it. */
289                 if_string = g_strdup_printf("%s: %s", if_info->description,
290                                         if_info->name);
291           } else {
292             /* No. */
293             if_string = g_strdup(if_info->name);
294           }
295         }
296         combo_list = g_list_append(combo_list, if_string);
297       }
298     }/*for*/
299   }
300   return combo_list;
301 }
302
303 static void
304 free_if_string(gpointer data, gpointer user_data _U_)
305 {
306   g_free(data);
307 }
308
309 void
310 free_capture_combo_list(GList *combo_list)
311 {
312   if (combo_list != NULL) {
313     g_list_foreach(combo_list, free_if_string, NULL);
314     g_list_free(combo_list);
315   }
316 }
317
318 /*
319  * Given text that contains an interface name possibly prefixed by an
320  * interface description, extract the interface name.
321  */
322 const char *
323 get_if_name(const char *if_text)
324 {
325   const char *if_name;
326
327 #ifdef _WIN32
328   /*
329    * We cannot assume that the interface name doesn't contain a space;
330    * some names on Windows OT do.
331    *
332    * We also can't assume it begins with "\Device\", either, as, on
333    * Windows OT, WinPcap doesn't put "\Device\" in front of the name.
334    *
335    * As I remember, we can't assume that the interface description
336    * doesn't contain a colon, either; I think some do.
337    *
338    * We can probably assume that the interface *name* doesn't contain
339    * a colon, however; if any interface name does contain a colon on
340    * Windows, it'll be time to just get rid of the damn interface
341    * descriptions in the drop-down list, have just the names in the
342    * drop-down list, and have a "Browse..." button to browse for interfaces,
343    * with names, descriptions, IP addresses, blah blah blah available when
344    * possible.
345    *
346    * So we search backwards for a colon.  If we don't find it, just
347    * return the entire string; otherwise, skip the colon and any blanks
348    * after it, and return that string.
349    */
350    if_name = if_text + strlen(if_text);
351    for (;;) {
352      if (if_name == if_text) {
353        /* We're at the beginning of the string; return it. */
354        break;
355      }
356      if_name--;
357      if (*if_name == ':') {
358        /*
359         * We've found a colon.
360         * Unfortunately, a colon is used in the string "rpcap://",
361         * which is used in case of a remote capture.
362         * So we'll check to make sure the colon isn't followed by "//";
363         * it'll be followed by a blank if it separates the description
364         * and the interface name.  (We don't wire in "rpcap", in case we
365         * support other protocols in the same syntax.)
366         * Unfortunately, another colon can be used in "rpcap://host:port/"
367         * before port. Check if colon is followed by digit.
368         */
369        if ((strncmp(if_name, "://", 3) != 0) && !isdigit(if_name[1])) {
370          /*
371           * OK, we've found a colon followed neither by "//" nor by digit.  
372           * Skip blanks following it.
373           */
374          if_name++;
375          while (*if_name == ' ')
376            if_name++;
377          break;
378        }
379      }
380      /* Keep looking for a colon not followed by "//". */
381    }
382 #else
383   /*
384    * There's a space between the interface description and name, and
385    * the interface name shouldn't have a space in it (it doesn't, on
386    * UNIX systems); look backwards in the string for a space.
387    *
388    * (An interface name might, however, contain a colon in it, which
389    * is why we don't use the colon search on UNIX.)
390    */
391   if_name = strrchr(if_text, ' ');
392   if (if_name == NULL) {
393     if_name = if_text;
394   } else {
395     if_name++;
396   }
397 #endif
398   return if_name;
399 }
400
401 /*  Return interface_opts->descr (after setting it if it is not set)
402  *  This is necessary because capture_opts.c can't set descr (at least
403  *  not without adding significant dependencies there).
404  */
405 const char *
406 get_iface_description_for_interface(capture_options *capture_opts, guint i)
407 {
408   interface_options interface_opts;
409
410   if (i < capture_opts->ifaces->len) {
411     interface_opts = g_array_index(capture_opts->ifaces, interface_options, i);
412     if (!interface_opts.descr && interface_opts.name) {
413       interface_opts.descr = get_interface_descriptive_name(interface_opts.name);
414       capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, i);
415       g_array_insert_val(capture_opts->ifaces, i, interface_opts);
416     }
417     return (interface_opts.descr);
418   } else {
419     return (NULL);
420   }
421 }
422
423 #endif /* HAVE_LIBPCAP */