Added an option to set default link-layer header type for each interface.
[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 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 gint
100 capture_dev_user_linktype_find(const gchar *if_name)
101 {
102         gchar *p, *next;
103         gint linktype;
104
105         if (prefs.capture_devices_linktypes == NULL) {
106                 /* There are no link-layer header types */
107                 return -1;
108         }
109
110         if ((p = strstr(prefs.capture_devices_linktypes, if_name)) == NULL) {
111                 /* There are, but there isn't one for this interface. */
112                 return -1;
113         }
114
115         p += strlen(if_name) + 1;
116         linktype = strtol(p, &next, 10);
117         if (next == p || *next != ')' || linktype < 0) {
118                 /* Syntax error */
119                 return -1;
120         }
121
122         return linktype;
123 }
124
125 /*
126  * Return as descriptive a name for an interface as we can get.
127  * If the user has specified a comment, use that.  Otherwise,
128  * if get_interface_list() supplies a description, use that,
129  * otherwise use the interface name.
130  *
131  * The result must be g_free()'d when you're done with it.
132  *
133  * Note: given that this calls get_interface_list(), which attempts to
134  * open all adapters it finds in order to check whether they can be
135  * captured on, this is an expensive routine to call, so don't call it
136  * frequently.
137  */
138 char *
139 get_interface_descriptive_name(const char *if_name)
140 {
141   char *descr;
142   GList *if_list;
143   GList *if_entry;
144   if_info_t *if_info;
145   int err;
146
147   /* Do we have a user-supplied description? */
148   descr = capture_dev_user_descr_find(if_name);
149   if (descr != NULL) {
150     /* Yes - make a copy of that. */
151     descr = g_strdup(descr);
152   } else {
153     /* No, we don't have a user-supplied description; did we get
154        one from the OS or libpcap? */
155     descr = NULL;
156     if_list = get_interface_list(&err, NULL);
157     if (if_list != NULL && if_name != NULL) {
158       if_entry = if_list;
159       do {
160         if_info = if_entry->data;
161         if (strcmp(if_info->name, if_name) == 0) {
162           if (if_info->description != NULL) {
163             /* Return a copy of that - when we free the interface
164                list, that'll also free up the strings to which
165                it refers. */
166             descr = g_strdup(if_info->description);
167           }
168           break;
169         }
170       } while ((if_entry = g_list_next(if_entry)) != NULL);
171     }
172     free_interface_list(if_list);
173
174     if (descr == NULL) {
175       /* The interface name is all we have, so just return a copy of that. */
176       descr = g_strdup(if_name);
177     }
178   }
179
180   return descr;
181 }
182
183
184 /* search interface info by interface name */
185 static if_info_t *
186 search_info(GList *if_list, gchar *if_name)
187 {
188     GList *if_entry;
189     if_info_t *if_info;
190
191
192     for (if_entry = if_list; if_entry != NULL; if_entry = g_list_next(if_entry)) {
193         if_info = if_entry->data;
194
195         if(strcmp(if_name, if_info->name) == 0) {
196             return if_info;
197         }
198     }
199
200     return NULL;
201 }
202
203
204 /* build the string to display in the combo box for the given interface */
205 char *
206 build_capture_combo_name(GList *if_list, gchar *if_name)
207 {
208     gchar *descr;
209     char *if_string;
210     if_info_t *if_info;
211
212
213         /* Do we have a user-supplied description? */
214         descr = capture_dev_user_descr_find(if_name);
215         if (descr != NULL) {
216           /* Yes, we have a user-supplied description; use it. */
217           if_string = g_strdup_printf("%s: %s", descr, if_name);
218           g_free(descr);
219         } else {
220           /* No, we don't have a user-supplied description; did we get
221              one from the OS or libpcap? */
222       if_info = search_info(if_list, if_name);
223           if (if_info && if_info->description != NULL) {
224             /* Yes - use it. */
225             if_string = g_strdup_printf("%s: %s", if_info->description,
226                                         if_info->name);
227           } else {
228             /* No. */
229             if_string = g_strdup(if_name);
230           }
231         }
232
233     return if_string;
234 }
235
236
237 GList *
238 build_capture_combo_list(GList *if_list, gboolean do_hide)
239 {
240   GList *combo_list;
241   GList *if_entry;
242   if_info_t *if_info;
243   char *if_string;
244   gchar *descr;
245
246   combo_list = NULL;
247   if (if_list != NULL) {
248     /* Scan through the list and build a list of strings to display. */
249     for (if_entry = if_list; if_entry != NULL;
250          if_entry = g_list_next(if_entry)) {
251       if_info = if_entry->data;
252
253       /* Is this interface hidden and, if so, should we include it
254          anyway? */
255       if (!prefs_is_capture_device_hidden(if_info->name) || !do_hide) {
256         /* It's not hidden, or it is but we should include it in the list. */
257
258         /* Do we have a user-supplied description? */
259         descr = capture_dev_user_descr_find(if_info->name);
260         if (descr != NULL) {
261           /* Yes, we have a user-supplied description; use it. */
262           if_string = g_strdup_printf("%s: %s", descr, if_info->name);
263           g_free(descr);
264         } else {
265           /* No, we don't have a user-supplied description; did we get
266              one from the OS or libpcap? */
267           if (if_info->description != NULL) {
268             /* Yes - use it. */
269             if_string = g_strdup_printf("%s: %s", if_info->description,
270                                         if_info->name);
271           } else {
272             /* No. */
273             if_string = g_strdup(if_info->name);
274           }
275         }
276         combo_list = g_list_append(combo_list, if_string);
277       }
278     }
279   }
280   return combo_list;
281 }
282
283 static void
284 free_if_string(gpointer data, gpointer user_data _U_)
285 {
286   g_free(data);
287 }
288
289 void
290 free_capture_combo_list(GList *combo_list)
291 {
292   if (combo_list != NULL) {
293     g_list_foreach(combo_list, free_if_string, NULL);
294     g_list_free(combo_list);
295   }
296 }
297
298 /*
299  * Given text that contains an interface name possibly prefixed by an
300  * interface description, extract the interface name.
301  */
302 const char *
303 get_if_name(const char *if_text)
304 {
305   const char *if_name;
306
307 #ifdef _WIN32
308   /*
309    * We cannot assume that the interface name doesn't contain a space;
310    * some names on Windows OT do.
311    *
312    * We also can't assume it begins with "\Device\", either, as, on
313    * Windows OT, WinPcap doesn't put "\Device\" in front of the name.
314    *
315    * As I remember, we can't assume that the interface description
316    * doesn't contain a colon, either; I think some do.
317    *
318    * We can probably assume that the interface *name* doesn't contain
319    * a colon, however; if any interface name does contain a colon on
320    * Windows, it'll be time to just get rid of the damn interface
321    * descriptions in the drop-down list, have just the names in the
322    * drop-down list, and have a "Browse..." button to browse for interfaces,
323    * with names, descriptions, IP addresses, blah blah blah available when
324    * possible.
325    *
326    * So we search backwards for a colon.  If we don't find it, just
327    * return the entire string; otherwise, skip the colon and any blanks
328    * after it, and return that string.
329    */
330    if_name = if_text + strlen(if_text);
331    for (;;) {
332      if (if_name == if_text) {
333        /* We're at the beginning of the string; return it. */
334        break;
335      }
336      if_name--;
337      if (*if_name == ':') {
338        /*
339         * We've found a colon.
340         * Unfortunately, a colon is used in the string "rpcap://",
341         * which is used in case of a remote capture.
342         * So we'll check to make sure the colon isn't followed by "//";
343         * it'll be followed by a blank if it separates the description
344         * and the interface name.  (We don't wire in "rpcap", in case we
345         * support other protocols in the same syntax.)
346         * Unfortunately, another colon can be used in "rpcap://host:port/"
347         * before port. Check if colon is followed by digit.
348         */
349        if ((strncmp(if_name, "://", 3) != 0) && !isdigit(if_name[1])) {
350          /*
351           * OK, we've found a colon followed neither by "//" nor by digit.  
352           * Skip blanks following it.
353           */
354          if_name++;
355          while (*if_name == ' ')
356            if_name++;
357          break;
358        }
359      }
360      /* Keep looking for a colon not followed by "//". */
361    }
362 #else
363   /*
364    * There's a space between the interface description and name, and
365    * the interface name shouldn't have a space in it (it doesn't, on
366    * UNIX systems); look backwards in the string for a space.
367    *
368    * (An interface name might, however, contain a colon in it, which
369    * is why we don't use the colon search on UNIX.)
370    */
371   if_name = strrchr(if_text, ' ');
372   if (if_name == NULL) {
373     if_name = if_text;
374   } else {
375     if_name++;
376   }
377 #endif
378   return if_name;
379 }
380
381 /*  Return capture_opts->iface_descr (after setting it if it is not set)
382  *  This is necessary because capture_opts.c can't set iface_descr (at least
383  *  not without adding significant dependencies there).
384  */
385 const char *
386 get_iface_description(capture_options *capture_opts)
387 {
388         if (!capture_opts->iface_descr && capture_opts->iface)
389                 capture_opts->iface_descr = get_interface_descriptive_name(capture_opts->iface);
390
391         return(capture_opts->iface_descr);
392
393 }
394 #endif /* HAVE_LIBPCAP */