Get rid of value_strings that appear to be a throwback to when ROHC appeared in this...
[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 "wiretap/wtap.h"
40 #include "epan/to_str.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       (*prefs.capture_devices_descr == '\0')) {
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 = (char *)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 gint
104 capture_dev_user_linktype_find(const gchar *if_name)
105 {
106   gchar *p, *next;
107   long linktype;
108
109   if ((prefs.capture_devices_linktypes == NULL) ||
110       (*prefs.capture_devices_linktypes == '\0')) {
111     /* There are no link-layer header types */
112     return -1;
113   }
114
115   if ((p = strstr(prefs.capture_devices_linktypes, if_name)) == NULL) {
116     /* There are, but there isn't one for this interface. */
117     return -1;
118   }
119
120   p += strlen(if_name) + 1;
121   linktype = strtol(p, &next, 10);
122   if (next == p || *next != ')' || linktype < 0) {
123     /* Syntax error */
124     return -1;
125   }
126   if (linktype > G_MAXINT) {
127     /* Value doesn't fit in a gint */
128     return -1;
129   }
130
131   return (gint)linktype;
132 }
133
134 #if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
135 gint
136 capture_dev_user_buffersize_find(const gchar *if_name)
137 {
138   gchar *p, *next;
139   gint buffersize;
140
141   if ((prefs.capture_devices_buffersize == NULL) ||
142       (*prefs.capture_devices_buffersize == '\0')) {
143     /* There are no buffersizes defined */
144     return -1;
145   }
146
147   if ((p = strstr(prefs.capture_devices_buffersize, if_name)) == NULL) {
148     /* There are, but there isn't one for this interface. */
149     return -1;
150   }
151
152   p += strlen(if_name) + 1;
153   buffersize = (gint)strtol(p, &next, 10);
154   if (next == p || *next != ')' || buffersize < 0) {
155     /* Syntax error */
156     return -1;
157   }
158   if (buffersize > G_MAXINT) {
159     /* Value doesn't fit in a gint */
160     return -1;
161   }
162
163   return (gint)buffersize;
164 }
165 #endif
166
167 gint
168 capture_dev_user_snaplen_find(const gchar *if_name)
169 {
170   gchar *p, *next;
171   gint snaplen;
172
173   if ((prefs.capture_devices_snaplen == NULL) ||
174       (*prefs.capture_devices_snaplen == '\0')) {
175     /* There is no snap length defined */
176     return -1;
177   }
178
179   if ((p = strstr(prefs.capture_devices_snaplen, if_name)) == NULL) {
180     /* There are, but there isn't one for this interface. */
181     return -1;
182   }
183
184   p += strlen(if_name) + 3;
185   snaplen = (gint)strtol(p, &next, 10);
186   if (next == p || *next != ')' || snaplen < 0) {
187     /* Syntax error */
188     return -1;
189   }
190   if (snaplen > WTAP_MAX_PACKET_SIZE) {
191     /* Value doesn't fit in a gint */
192     return -1;
193   }
194
195   return (gint)snaplen;
196 }
197
198 gboolean
199 capture_dev_user_hassnap_find(const gchar *if_name)
200 {
201   gchar *p, *next;
202   gboolean hassnap;
203
204   if ((prefs.capture_devices_snaplen == NULL) ||
205       (*prefs.capture_devices_snaplen == '\0')) {
206     /* There is no snap length defined */
207     return -1;
208   }
209
210   if ((p = strstr(prefs.capture_devices_snaplen, if_name)) == NULL) {
211     /* There are, but there isn't one for this interface. */
212     return -1;
213   }
214
215   p += strlen(if_name) + 1;
216   hassnap = (gboolean)strtol(p, &next, 10);
217   if (next == p || *next != '(') {
218     /* Syntax error */
219     return -1;
220   }
221
222   return (gboolean)hassnap;
223 }
224
225 gboolean
226 capture_dev_user_pmode_find(const gchar *if_name)
227 {
228   gchar *p, *next;
229   gboolean pmode;
230
231   if ((prefs.capture_devices_pmode == NULL) ||
232       (*prefs.capture_devices_pmode == '\0')) {
233     /* There is no promiscuous mode defined */
234     return -1;
235   }
236
237   if ((p = strstr(prefs.capture_devices_pmode, if_name)) == NULL) {
238     /* There are, but there isn't one for this interface. */
239     return -1;
240   }
241
242   p += strlen(if_name) + 1;
243   pmode = (gboolean)strtol(p, &next, 10);
244   if (next == p || *next != ')') {
245     /* Syntax error */
246     return -1;
247   }
248   return (gboolean)pmode;
249 }
250
251 /*
252  * Return as descriptive a name for an interface as we can get.
253  * If the user has specified a comment, use that.  Otherwise,
254  * if capture_interface_list() supplies a description, use that,
255  * otherwise use the interface name.
256  *
257  * The result must be g_free()'d when you're done with it.
258  *
259  * Note: given that this calls capture_interface_list(), which attempts to
260  * open all adapters it finds in order to check whether they can be
261  * captured on, this is an expensive routine to call, so don't call it
262  * frequently.
263  */
264 char *
265 get_interface_descriptive_name(const char *if_name)
266 {
267   char *descr;
268   GList *if_list;
269   GList *if_entry;
270   if_info_t *if_info;
271   int err;
272
273   /* Do we have a user-supplied description? */
274   descr = capture_dev_user_descr_find(if_name);
275   if (descr != NULL) {
276     /* Yes - make a copy of that. */
277     descr = g_strdup(descr);
278   } else if (strcmp(if_name, "-") == 0) {
279     /*
280      * Strictly speaking, -X (extension) options are for modules, e.g. Lua
281      * and using one here stretches that definition. However, this doesn't
282      * waste a single-letter option on something that might be rarely used
283      * and is backward-compatible to 1.0.
284      */
285     descr = g_strdup(ex_opt_get_nth("stdin_descr", 0));
286     if (!descr) {
287       descr = g_strdup("Standard input");
288     }
289   } else {
290     /* No, we don't have a user-supplied description; did we get
291        one from the OS or libpcap? */
292     descr = NULL;
293     if_list = capture_interface_list(&err, NULL, NULL);
294     if (if_list != NULL) {
295       if_entry = if_list;
296       do {
297         if_info = (if_info_t *)if_entry->data;
298         if (strcmp(if_info->name, if_name) == 0) {
299           if (if_info->friendly_name != NULL) {
300               /* We have a "friendly name"; return a copy of that
301                  as the description - when we free the interface
302                  list, that'll also free up the strings to which
303                  it refers. */
304               descr = g_strdup(if_info->friendly_name);
305           } else if (if_info->vendor_description != NULL) {
306             /* We have no "friendly name", but we have a vendor
307                description; return a copy of that - when we free
308                the interface list, that'll also free up the strings
309                to which it refers. */
310             descr = g_strdup(if_info->vendor_description);
311           }
312           break;
313         }
314       } while ((if_entry = g_list_next(if_entry)) != NULL);
315     }
316     free_interface_list(if_list);
317
318     if (descr == NULL) {
319       /* The interface name is all we have, so just return a copy of that. */
320       descr = g_strdup(if_name);
321     }
322   }
323
324   return descr;
325 }
326
327
328 /* search interface info by interface name */
329 static if_info_t *
330 search_info(GList *if_list, gchar *if_name)
331 {
332   GList *if_entry;
333   if_info_t *if_info;
334
335
336   for (if_entry = if_list; if_entry != NULL; if_entry = g_list_next(if_entry)) {
337     if_info = (if_info_t *)if_entry->data;
338
339     if(strcmp(if_name, if_info->name) == 0) {
340       return if_info;
341     }
342   }
343
344   return NULL;
345 }
346
347
348 /* build the string to display in the combo box for the given interface */
349 char *
350 build_capture_combo_name(GList *if_list, gchar *if_name)
351 {
352   gchar *descr;
353   char *if_string;
354   if_info_t *if_info;
355
356   /* Do we have a user-supplied description? */
357   descr = capture_dev_user_descr_find(if_name);
358   if (descr != NULL) {
359     /* Yes, we have a user-supplied description; use it. */
360     if_string = g_strdup_printf("%s: %s", descr, if_name);
361     g_free(descr);
362   } else {
363     /* No, we don't have a user-supplied description; did we get
364      one from the OS or libpcap? */
365     if_info = search_info(if_list, if_name);
366     if (if_info != NULL && if_info->vendor_description != NULL) {
367       /* Yes - use it. */
368       if_string = g_strdup_printf("%s: %s", if_info->vendor_description,
369                                   if_info->name);
370     } else {
371       /* No. */
372       if_string = g_strdup(if_name);
373     }
374   }
375
376   return if_string;
377 }
378
379
380 GList *
381 build_capture_combo_list(GList *if_list, gboolean do_hide)
382 {
383   GList *combo_list;
384   GList *if_entry;
385   if_info_t *if_info;
386   char *if_string;
387   gchar *descr;
388
389   combo_list = NULL;
390   if (if_list != NULL) {
391     /* Scan through the list and build a list of strings to display. */
392     for (if_entry = if_list; if_entry != NULL;
393          if_entry = g_list_next(if_entry)) {
394       if_info = (if_info_t *)if_entry->data;
395
396       /* Is this interface hidden and, if so, should we include it
397          anyway? */
398       if (!prefs_is_capture_device_hidden(if_info->name) || !do_hide) {
399         /* It's not hidden, or it is but we should include it in the list. */
400
401         /* Do we have a user-supplied description? */
402         descr = capture_dev_user_descr_find(if_info->name);
403         if (descr != NULL) {
404           /* Yes, we have a user-supplied description; use it. */
405           if_string = g_strdup_printf("%s: %s", descr, if_info->name);
406           g_free(descr);
407         } else {
408           /* No, we don't have a user-supplied description; did we get
409              one from the OS or libpcap? */
410           if (if_info->vendor_description != NULL) {
411             /* Yes - use it. */
412             if_string = g_strdup_printf("%s: %s",
413                                         if_info->vendor_description,
414                                         if_info->name);
415           } else {
416             /* No. */
417             if_string = g_strdup(if_info->name);
418           }
419         }
420         combo_list = g_list_append(combo_list, if_string);
421       }
422     }/*for*/
423   }
424   return combo_list;
425 }
426
427 static void
428 free_if_string(gpointer data, gpointer user_data _U_)
429 {
430   g_free(data);
431 }
432
433 void
434 free_capture_combo_list(GList *combo_list)
435 {
436   if (combo_list != NULL) {
437     g_list_foreach(combo_list, free_if_string, NULL);
438     g_list_free(combo_list);
439   }
440 }
441
442 /*
443  * Given text that contains an interface name possibly prefixed by an
444  * interface description, extract the interface name.
445  */
446 const char *
447 get_if_name(const char *if_text)
448 {
449   const char *if_name;
450
451 #ifdef _WIN32
452   /*
453    * We cannot assume that the interface name doesn't contain a space;
454    * some names on Windows OT do.
455    *
456    * We also can't assume it begins with "\Device\", either, as, on
457    * Windows OT, WinPcap doesn't put "\Device\" in front of the name.
458    *
459    * As I remember, we can't assume that the interface description
460    * doesn't contain a colon, either; I think some do.
461    *
462    * We can probably assume that the interface *name* doesn't contain
463    * a colon, however; if any interface name does contain a colon on
464    * Windows, it'll be time to just get rid of the damn interface
465    * descriptions in the drop-down list, have just the names in the
466    * drop-down list, and have a "Browse..." button to browse for interfaces,
467    * with names, descriptions, IP addresses, blah blah blah available when
468    * possible.
469    *
470    * So we search backwards for a colon.  If we don't find it, just
471    * return the entire string; otherwise, skip the colon and any blanks
472    * after it, and return that string.
473    */
474    if_name = if_text + strlen(if_text);
475    for (;;) {
476      if (if_name == if_text) {
477        /* We're at the beginning of the string; return it. */
478        break;
479      }
480      if_name--;
481      if (*if_name == ':') {
482        /*
483         * We've found a colon.
484         * Unfortunately, a colon is used in the string "rpcap://",
485         * which is used in case of a remote capture.
486         * So we'll check to make sure the colon isn't followed by "//";
487         * it'll be followed by a blank if it separates the description
488         * and the interface name.  (We don't wire in "rpcap", in case we
489         * support other protocols in the same syntax.)
490         * Unfortunately, another colon can be used in "rpcap://host:port/"
491         * before port. Check if colon is followed by digit.
492         */
493        if ((strncmp(if_name, "://", 3) != 0) && !isdigit(if_name[1])) {
494          /*
495           * OK, we've found a colon followed neither by "//" nor by digit.  
496           * Skip blanks following it.
497           */
498          if_name++;
499          while (*if_name == ' ')
500            if_name++;
501          break;
502        }
503      }
504      /* Keep looking for a colon not followed by "//". */
505    }
506 #else
507   /*
508    * There's a space between the interface description and name, and
509    * the interface name shouldn't have a space in it (it doesn't, on
510    * UNIX systems); look backwards in the string for a space.
511    *
512    * (An interface name might, however, contain a colon in it, which
513    * is why we don't use the colon search on UNIX.)
514    */
515   if_name = strrchr(if_text, ' ');
516   if (if_name == NULL) {
517     if_name = if_text;
518   } else {
519     if_name++;
520   }
521 #endif
522   return if_name;
523 }
524
525 /*  Return interface_opts->descr (after setting it if it is not set)
526  *  This is necessary because capture_opts.c can't set descr (at least
527  *  not without adding significant dependencies there).
528  */
529 const char *
530 get_iface_description_for_interface(capture_options *capture_opts, guint i)
531 {
532   interface_options interface_opts;
533
534   if (i < capture_opts->ifaces->len) {
535     interface_opts = g_array_index(capture_opts->ifaces, interface_options, i);
536     if (!interface_opts.descr && interface_opts.name) {
537       interface_opts.descr = get_interface_descriptive_name(interface_opts.name);
538       capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, i);
539       g_array_insert_val(capture_opts->ifaces, i, interface_opts);
540     }
541     return (interface_opts.descr);
542   } else {
543     return (NULL);
544   }
545 }
546
547 #endif /* HAVE_LIBPCAP */