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