Rename capture_combo_utils.{c,h} to capture_ui_utils.{c,h}, as the code
[metze/wireshark/wip.git] / capture_ui_utils.c
1 /* capture_ui_utils.c
2  * Utilities for capture user interfaces
3  *
4  * $Id$
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@ethereal.com>
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 /* With MSVC and a libethereal.dll this file needs to import some variables
26    in a special way. Therefore _NEED_VAR_IMPORT_ is defined. */
27 #define _NEED_VAR_IMPORT_
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #ifdef HAVE_LIBPCAP
34
35 #include <pcap.h>
36 #include <string.h>
37 #include <glib.h>
38
39 #include <epan/prefs.h>
40 #include "pcap-util.h"
41 #include "capture_ui_utils.h"
42
43 /*
44  * Find user-specified capture device description that matches interface
45  * name, if any.
46  */
47 static 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                 /* There are no descriptions. */
58                 return NULL;
59         }
60
61         if ((p = strstr(prefs.capture_devices_descr, if_name)) == NULL) {
62                 /* There are, but there isn't one for this interface. */
63                 return NULL;
64         }
65
66         while (*p != '\0') {
67                 /* error: ran into next interface description */
68                 if (*p == ',')
69                         return NULL;
70                 /* found left parenthesis, start of description */
71                 else if (*p == '(') {
72                         ct = 0;
73                         lp++;
74                         /* skip over left parenthesis */
75                         p++;
76                         /* save pointer to beginning of description */
77                         p2 = p;
78                         continue;
79                 }
80                 else if (*p == ')') {
81                         /* end of description */
82                         break;
83                 }
84                 else {
85                         p++;
86                         ct++;
87                 }
88         }
89
90         if ((lp == 1) && (ct > 0) && (p2 != NULL)) {
91                 /* Allocate enough space to return the string,
92                    which runs from p2 to p, plus a terminating
93                    '\0'. */
94                 descr = g_malloc(p - p2 + 1);
95                 memcpy(descr, p2, p - p2);
96                 descr[p - p2] = '\0';
97                 return descr;
98         }
99         else
100                 return NULL;
101 }
102
103 /*
104  * Return as descriptive a name for an interface as we can get.
105  * If the user has specified a comment, use that.  Otherwise,
106  * if get_interface_list() supplies a description, use that,
107  * otherwise use the interface name.
108  */
109 char *
110 get_interface_descriptive_name(const char *if_name)
111 {
112   char *descr;
113   GList *if_list;
114   GList *if_entry;
115   if_info_t *if_info;
116   int err;
117   char err_buf[PCAP_ERRBUF_SIZE];
118
119   /* Do we have a user-supplied description? */
120   descr = capture_dev_user_descr_find(if_name);
121   if (descr != NULL) {
122     /* Yes - make a copy of that. */
123     descr = g_strdup(descr);
124   } else {
125     /* No, we don't have a user-supplied description; did we get
126        one from the OS or libpcap? */
127     descr = NULL;
128     if_list = get_interface_list(&err, err_buf);
129     if (if_list != NULL) {
130       if_entry = if_list;
131       do {
132         if_info = if_entry->data;
133         if (strcmp(if_info->name, if_name) == 0) {
134           if (if_info->description != NULL) {
135             /* Return a copy of that - when we free the interface
136                list, that'll also free up the strings to which
137                it refers. */
138             descr = g_strdup(if_info->description);
139           }
140           break;
141         }
142       } while ((if_entry = g_list_next(if_entry)) != NULL);
143     }
144     free_interface_list(if_list);
145
146     if (descr == NULL) {
147       /* The interface name is all we have, so just return a copy of that. */
148       descr = g_strdup(if_name);
149     }
150   }
151
152   return descr;
153 }
154
155 GList *
156 build_capture_combo_list(GList *if_list, gboolean do_hide)
157 {
158   GList *combo_list;
159   GList *if_entry;
160   if_info_t *if_info;
161   char *if_string;
162   gchar *descr;
163
164   combo_list = NULL;
165   if (if_list != NULL) {
166     /* Scan through the list and build a list of strings to display. */
167     for (if_entry = if_list; if_entry != NULL;
168          if_entry = g_list_next(if_entry)) {
169       if_info = if_entry->data;
170
171       /* Is this interface hidden and, if so, should we include it
172          anyway? */
173       if (prefs.capture_devices_hide == NULL ||
174           strstr(prefs.capture_devices_hide, if_info->name) == NULL ||
175           !do_hide) {
176         /* It's not hidden, or it is but we should include it in the list. */
177
178         /* Do we have a user-supplied description? */
179         descr = capture_dev_user_descr_find(if_info->name);
180         if (descr != NULL) {
181           /* Yes, we have a user-supplied description; use it. */
182           if_string = g_strdup_printf("%s: %s", descr, if_info->name);
183           g_free(descr);
184         } else {
185           /* No, we don't have a user-supplied description; did we get
186              one from the OS or libpcap? */
187           if (if_info->description != NULL) {
188             /* Yes - use it. */
189             if_string = g_strdup_printf("%s: %s", if_info->description,
190                                         if_info->name);
191           } else {
192             /* No. */
193             if_string = g_strdup(if_info->name);
194           }
195         }
196         combo_list = g_list_append(combo_list, if_string);
197       }
198     }
199   }
200   return combo_list;
201 }
202
203 static void
204 free_if_string(gpointer data, gpointer user_data _U_)
205 {
206   g_free(data);
207 }
208
209 void
210 free_capture_combo_list(GList *combo_list)
211 {
212   if (combo_list != NULL) {
213     g_list_foreach(combo_list, free_if_string, NULL);
214     g_list_free(combo_list);
215   }
216 }
217
218 /*
219  * Given text that contains an interface name possibly prefixed by an
220  * interface description, extract the interface name.
221  */
222 char *
223 get_if_name(char *if_text)
224 {
225   char *if_name;
226
227 #ifdef _WIN32
228   /*
229    * We cannot assume that the interface name doesn't contain a space;
230    * some names on Windows OT do.
231    *
232    * We also can't assume it begins with "\Device\", either, as, on
233    * Windows OT, WinPcap doesn't put "\Device\" in front of the name.
234    *
235    * As I remember, we can't assume that the interface description
236    * doesn't contain a colon, either; I think some do.
237    *
238    * We can probably assume that the interface *name* doesn't contain
239    * a colon, however; if any interface name does contain a colon on
240    * Windows, it'll be time to just get rid of the damn interface
241    * descriptions in the drop-down list, have just the names in the
242    * drop-down list, and have a "Browse..." button to browse for interfaces,
243    * with names, descriptions, IP addresses, blah blah blah available when
244    * possible.
245    *
246    * So we search backwards for a colon.  If we don't find it, just
247    * return the entire string; otherwise, skip the colon and any blanks
248    * after it, and return that string.
249    */
250    if_name = if_text + strlen(if_text);
251    for (;;) {
252      if (if_name == if_text) {
253        /* We're at the beginning of the string; return it. */
254        break;
255      }
256      if_name--;
257      if (*if_name == ':') {
258        /*
259         * We've found a colon.
260         * Unfortunately, a colon is used in the string "rpcap://",
261         * which is used in case of a remote capture.
262         * So we'll check to make sure the colon isn't followed by "//";
263         * it'll be followed by a blank if it separates the description
264         * and the interface name.  (We don't wire in "rpcap", in case we
265         * support other protocols in the same syntax.)
266         */
267        if (strncmp(if_name, "://", 3) != 0) {
268          /*
269           * OK, we've found a colon not followed by "//".  Skip blanks
270           * following it.
271           */
272          if_name++;
273          while (*if_name == ' ')
274            if_name++;
275          break;
276        }
277      }
278      /* Keep looking for a colon not followed by "//". */
279    }
280 #else
281   /*
282    * There's a space between the interface description and name, and
283    * the interface name shouldn't have a space in it (it doesn't, on
284    * UNIX systems); look backwards in the string for a space.
285    *
286    * (An interface name might, however, contain a colon in it, which
287    * is why we don't use the colon search on UNIX.)
288    */
289   if_name = strrchr(if_text, ' ');
290   if (if_name == NULL) {
291     if_name = if_text;
292   } else {
293     if_name++;
294   }
295 #endif
296   return if_name;
297 }
298
299 #endif /* HAVE_LIBPCAP */