From Mike Garratt:
[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               /* use the friendly name */
185               descr = g_strdup(if_info->friendly_name);
186           }else if (if_info->description != NULL) {
187             /* Return a copy of that - when we free the interface
188                list, that'll also free up the strings to which
189                it refers. */
190             descr = g_strdup(if_info->description);
191           }
192           break;
193         }
194       } while ((if_entry = g_list_next(if_entry)) != NULL);
195     }
196     free_interface_list(if_list);
197
198     if (descr == NULL) {
199       /* The interface name is all we have, so just return a copy of that. */
200       descr = g_strdup(if_name);
201     }
202   }
203
204   return descr;
205 }
206
207
208 /* search interface info by interface name */
209 static if_info_t *
210 search_info(GList *if_list, gchar *if_name)
211 {
212   GList *if_entry;
213   if_info_t *if_info;
214
215
216   for (if_entry = if_list; if_entry != NULL; if_entry = g_list_next(if_entry)) {
217     if_info = if_entry->data;
218
219     if(strcmp(if_name, if_info->name) == 0) {
220       return if_info;
221     }
222   }
223
224   return NULL;
225 }
226
227
228 /* build the string to display in the combo box for the given interface */
229 char *
230 build_capture_combo_name(GList *if_list, gchar *if_name)
231 {
232   gchar *descr;
233   char *if_string;
234   if_info_t *if_info;
235
236   /* Do we have a user-supplied description? */
237   descr = capture_dev_user_descr_find(if_name);
238   if (descr != NULL) {
239     /* Yes, we have a user-supplied description; use it. */
240     if_string = g_strdup_printf("%s: %s", descr, if_name);
241     g_free(descr);
242   } else {
243     /* No, we don't have a user-supplied description; did we get
244      one from the OS or libpcap? */
245     if_info = search_info(if_list, if_name);
246     if (if_info != NULL && if_info->description != NULL) {
247       /* Yes - use it. */
248       if_string = g_strdup_printf("%s: %s", if_info->description,
249                                   if_info->name);
250     } else {
251       /* No. */
252       if_string = g_strdup(if_name);
253     }
254   }
255
256   return if_string;
257 }
258
259
260 GList *
261 build_capture_combo_list(GList *if_list, gboolean do_hide)
262 {
263   GList *combo_list;
264   GList *if_entry;
265   if_info_t *if_info;
266   char *if_string;
267   gchar *descr;
268
269   combo_list = NULL;
270   if (if_list != NULL) {
271     /* Scan through the list and build a list of strings to display. */
272     for (if_entry = if_list; if_entry != NULL;
273          if_entry = g_list_next(if_entry)) {
274       if_info = if_entry->data;
275
276       /* Is this interface hidden and, if so, should we include it
277          anyway? */
278       if (!prefs_is_capture_device_hidden(if_info->name) || !do_hide) {
279         /* It's not hidden, or it is but we should include it in the list. */
280
281         /* Do we have a user-supplied description? */
282         descr = capture_dev_user_descr_find(if_info->name);
283         if (descr != NULL) {
284               /* Yes, we have a user-supplied description; use it. */
285               if_string = g_strdup_printf("%s: %s", descr, if_info->name);
286           g_free(descr);
287         } else {
288           /* No, we don't have a user-supplied description; did we get
289                  one from the OS or libpcap? */
290           if (if_info->description != NULL) {
291             /* Yes - use it. */
292                 if_string = g_strdup_printf("%s: %s", if_info->description,
293                                         if_info->name);
294           } else {
295             /* No. */
296             if_string = g_strdup(if_info->name);
297           }
298         }
299         combo_list = g_list_append(combo_list, if_string);
300       }
301     }/*for*/
302   }
303   return combo_list;
304 }
305
306 static void
307 free_if_string(gpointer data, gpointer user_data _U_)
308 {
309   g_free(data);
310 }
311
312 void
313 free_capture_combo_list(GList *combo_list)
314 {
315   if (combo_list != NULL) {
316     g_list_foreach(combo_list, free_if_string, NULL);
317     g_list_free(combo_list);
318   }
319 }
320
321 /*
322  * Given text that contains an interface name possibly prefixed by an
323  * interface description, extract the interface name.
324  */
325 const char *
326 get_if_name(const char *if_text)
327 {
328   const char *if_name;
329
330 #ifdef _WIN32
331   /*
332    * We cannot assume that the interface name doesn't contain a space;
333    * some names on Windows OT do.
334    *
335    * We also can't assume it begins with "\Device\", either, as, on
336    * Windows OT, WinPcap doesn't put "\Device\" in front of the name.
337    *
338    * As I remember, we can't assume that the interface description
339    * doesn't contain a colon, either; I think some do.
340    *
341    * We can probably assume that the interface *name* doesn't contain
342    * a colon, however; if any interface name does contain a colon on
343    * Windows, it'll be time to just get rid of the damn interface
344    * descriptions in the drop-down list, have just the names in the
345    * drop-down list, and have a "Browse..." button to browse for interfaces,
346    * with names, descriptions, IP addresses, blah blah blah available when
347    * possible.
348    *
349    * So we search backwards for a colon.  If we don't find it, just
350    * return the entire string; otherwise, skip the colon and any blanks
351    * after it, and return that string.
352    */
353    if_name = if_text + strlen(if_text);
354    for (;;) {
355      if (if_name == if_text) {
356        /* We're at the beginning of the string; return it. */
357        break;
358      }
359      if_name--;
360      if (*if_name == ':') {
361        /*
362         * We've found a colon.
363         * Unfortunately, a colon is used in the string "rpcap://",
364         * which is used in case of a remote capture.
365         * So we'll check to make sure the colon isn't followed by "//";
366         * it'll be followed by a blank if it separates the description
367         * and the interface name.  (We don't wire in "rpcap", in case we
368         * support other protocols in the same syntax.)
369         * Unfortunately, another colon can be used in "rpcap://host:port/"
370         * before port. Check if colon is followed by digit.
371         */
372        if ((strncmp(if_name, "://", 3) != 0) && !isdigit(if_name[1])) {
373          /*
374           * OK, we've found a colon followed neither by "//" nor by digit.  
375           * Skip blanks following it.
376           */
377          if_name++;
378          while (*if_name == ' ')
379            if_name++;
380          break;
381        }
382      }
383      /* Keep looking for a colon not followed by "//". */
384    }
385 #else
386   /*
387    * There's a space between the interface description and name, and
388    * the interface name shouldn't have a space in it (it doesn't, on
389    * UNIX systems); look backwards in the string for a space.
390    *
391    * (An interface name might, however, contain a colon in it, which
392    * is why we don't use the colon search on UNIX.)
393    */
394   if_name = strrchr(if_text, ' ');
395   if (if_name == NULL) {
396     if_name = if_text;
397   } else {
398     if_name++;
399   }
400 #endif
401   return if_name;
402 }
403
404 /*  Return interface_opts->descr (after setting it if it is not set)
405  *  This is necessary because capture_opts.c can't set descr (at least
406  *  not without adding significant dependencies there).
407  */
408 const char *
409 get_iface_description_for_interface(capture_options *capture_opts, guint i)
410 {
411   interface_options interface_opts;
412
413   if (i < capture_opts->ifaces->len) {
414     interface_opts = g_array_index(capture_opts->ifaces, interface_options, i);
415     if (!interface_opts.descr && interface_opts.name) {
416       interface_opts.descr = get_interface_descriptive_name(interface_opts.name);
417       capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, i);
418       g_array_insert_val(capture_opts->ifaces, i, interface_opts);
419     }
420     return (interface_opts.descr);
421   } else {
422     return (NULL);
423   }
424 }
425
426 #endif /* HAVE_LIBPCAP */