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