Tighten the dump-glossary test.
[metze/wireshark/wip.git] / capture_ui_utils.c
1 /* capture_ui_utils.c
2  * Utilities for capture user interfaces
3  *
4  * Wireshark - Network traffic analyzer
5  * By Gerald Combs <gerald@wireshark.org>
6  * Copyright 1998 Gerald Combs
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22
23 #include "config.h"
24
25 #ifdef HAVE_LIBPCAP
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <ctype.h>
31 #include <glib.h>
32
33 #include "epan/prefs.h"
34 #include "epan/ex-opt.h"
35 #include <capchild/capture_ifinfo.h>
36 #include "capture_ui_utils.h"
37 #include "wiretap/wtap.h"
38 #include "epan/to_str.h"
39
40 /*
41  * Find user-specified capture device description that matches interface
42  * name, if any.
43  */
44 char *
45 capture_dev_user_descr_find(const gchar *if_name)
46 {
47   char *p;
48   char *p2 = NULL;
49   char *descr = NULL;
50   int lp = 0;
51   int ct = 0;
52
53   if ((prefs.capture_devices_descr == NULL) ||
54       (*prefs.capture_devices_descr == '\0')) {
55     /* There are no descriptions. */
56     return NULL;
57   }
58
59   if ((p = strstr(prefs.capture_devices_descr, if_name)) == NULL) {
60     /* There are, but there isn't one for this interface. */
61     return NULL;
62   }
63
64   while (*p != '\0') {
65     /* error: ran into next interface description */
66     if (*p == ',')
67       return NULL;
68     /* found left parenthesis, start of description */
69     else if (*p == '(') {
70       ct = 0;
71       lp++;
72       /* skip over left parenthesis */
73       p++;
74       /* save pointer to beginning of description */
75       p2 = p;
76       continue;
77     }
78     else if (*p == ')') {
79       /* end of description */
80       break;
81     }
82     else {
83         p++;
84         ct++;
85     }
86   }
87
88   if ((lp == 1) && (ct > 0) && (p2 != NULL)) {
89     /* Allocate enough space to return the string,
90        which runs from p2 to p, plus a terminating
91        '\0'. */
92     descr = (char *)g_malloc(p - p2 + 1);
93     memcpy(descr, p2, p - p2);
94     descr[p - p2] = '\0';
95     return descr;
96   }
97   else
98     return NULL;
99 }
100
101 gint
102 capture_dev_user_linktype_find(const gchar *if_name)
103 {
104   gchar *p, *next, *tmpname;
105   long linktype;
106
107   if ((prefs.capture_devices_linktypes == NULL) ||
108       (*prefs.capture_devices_linktypes == '\0')) {
109     /* There are no link-layer header types */
110     return -1;
111   }
112   tmpname = g_strdup_printf(",%s(", if_name);
113   if ((p = strstr(prefs.capture_devices_linktypes, tmpname)) == NULL) {
114     /* There are, but there isn't one for this interface. */
115     return -1;
116   }
117
118   p += strlen(if_name) + 2;
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 #if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
133 gint
134 capture_dev_user_buffersize_find(const gchar *if_name)
135 {
136   gchar *p, *next, *tmpname;
137   gint buffersize;
138
139   if ((prefs.capture_devices_buffersize == NULL) ||
140       (*prefs.capture_devices_buffersize == '\0')) {
141     /* There are no buffersizes defined */
142     return -1;
143   }
144   tmpname = g_strdup_printf(",%s(", if_name);
145   if ((p = strstr(prefs.capture_devices_buffersize, tmpname)) == NULL) {
146     /* There are, but there isn't one for this interface. */
147     return -1;
148   }
149
150   p += strlen(if_name) + 2;
151   buffersize = (gint)strtol(p, &next, 10);
152   if (next == p || *next != ')' || buffersize < 0) {
153     /* Syntax error */
154     return -1;
155   }
156   if (buffersize > G_MAXINT) {
157     /* Value doesn't fit in a gint */
158     return -1;
159   }
160
161   return (gint)buffersize;
162 }
163 #endif
164
165 gint
166 capture_dev_user_snaplen_find(const gchar *if_name)
167 {
168   gchar *p, *next, *tmpname;
169   gint snaplen;
170
171   if ((prefs.capture_devices_snaplen == NULL) ||
172       (*prefs.capture_devices_snaplen == '\0')) {
173     /* There is no snap length defined */
174     return -1;
175   }
176   tmpname = g_strdup_printf(",%s:", if_name);
177   if ((p = strstr(prefs.capture_devices_snaplen, tmpname)) == NULL) {
178     /* There are, but there isn't one for this interface. */
179     return -1;
180   }
181
182   p += strlen(if_name) + 4;
183   snaplen = (gint)strtol(p, &next, 10);
184   if (next == p || *next != ')' || snaplen < 0) {
185     /* Syntax error */
186     return -1;
187   }
188   if (snaplen > WTAP_MAX_PACKET_SIZE) {
189     /* Value doesn't fit in a gint */
190     return -1;
191   }
192
193   return (gint)snaplen;
194 }
195
196 gboolean
197 capture_dev_user_hassnap_find(const gchar *if_name)
198 {
199   gchar *p, *next, *tmpname;
200   gboolean hassnap;
201
202   if ((prefs.capture_devices_snaplen == NULL) ||
203       (*prefs.capture_devices_snaplen == '\0')) {
204     /* There is no snap length defined */
205     return -1;
206   }
207   tmpname = g_strdup_printf(",%s:", if_name);
208   if ((p = strstr(prefs.capture_devices_snaplen, tmpname)) == NULL) {
209     /* There are, but there isn't one for this interface. */
210     return -1;
211   }
212
213   p += strlen(if_name) + 2;
214   hassnap = (gboolean)strtol(p, &next, 10);
215   if (next == p || *next != '(') {
216     /* Syntax error */
217     return -1;
218   }
219
220   return (gboolean)hassnap;
221 }
222
223 gboolean
224 capture_dev_user_pmode_find(const gchar *if_name)
225 {
226   gchar *p, *next, *tmpname;
227   gboolean pmode;
228
229   if ((prefs.capture_devices_pmode == NULL) ||
230       (*prefs.capture_devices_pmode == '\0')) {
231     /* There is no promiscuous mode defined */
232     return -1;
233   }
234   tmpname = g_strdup_printf(",%s(", if_name);
235   if ((p = strstr(prefs.capture_devices_pmode, tmpname)) == NULL) {
236     /* There are, but there isn't one for this interface. */
237     return -1;
238   }
239
240   p += strlen(if_name) + 2;
241   pmode = (gboolean)strtol(p, &next, 10);
242   if (next == p || *next != ')') {
243     /* Syntax error */
244     return -1;
245   }
246   return (gboolean)pmode;
247 }
248
249 gchar*
250 capture_dev_user_cfilter_find(const gchar *if_name)
251 {
252   gchar *p, q[MAX_VAL_LEN], *tmpname;
253   int i = 0;
254
255   if ((prefs.capture_devices_filter == NULL) ||
256       (*prefs.capture_devices_filter == '\0')) {
257     /* There is no capture filter defined */
258     return NULL;
259   }
260   tmpname = g_strdup_printf(",%s(", if_name);
261   if ((p = strstr(prefs.capture_devices_filter, tmpname)) == NULL) {
262     /* There are, but there isn't one for this interface. */
263     return NULL;
264   }
265
266   p += strlen(if_name) + 2;
267   while (p[i+1] != ',' && p[i+1] != '\0') {
268     q[i] = p[i];
269     i++;
270   }
271   q[i] = '\0';
272   return g_strdup(q);
273 }
274
275 /*
276  * Return as descriptive a name for an interface as we can get.
277  * If the user has specified a comment, use that.  Otherwise,
278  * if capture_interface_list() supplies a description, use that,
279  * otherwise use the interface name.
280  *
281  * The result must be g_free()'d when you're done with it.
282  *
283  * Note: given that this calls capture_interface_list(), which attempts to
284  * open all adapters it finds in order to check whether they can be
285  * captured on, this is an expensive routine to call, so don't call it
286  * frequently.
287  */
288 char *
289 get_interface_descriptive_name(const char *if_name)
290 {
291   char *descr;
292   GList *if_list;
293   GList *if_entry;
294   if_info_t *if_info;
295   int err;
296
297   /* Do we have a user-supplied description? */
298   descr = capture_dev_user_descr_find(if_name);
299   if (descr != NULL) {
300     /* Yes - make a copy of that. */
301     descr = g_strdup(descr);
302   } else if (strcmp(if_name, "-") == 0) {
303     /*
304      * Strictly speaking, -X (extension) options are for modules, e.g. Lua
305      * and using one here stretches that definition. However, this doesn't
306      * waste a single-letter option on something that might be rarely used
307      * and is backward-compatible to 1.0.
308      */
309     descr = g_strdup(ex_opt_get_nth("stdin_descr", 0));
310     if (!descr) {
311       descr = g_strdup("Standard input");
312     }
313   } else {
314     /* No, we don't have a user-supplied description; did we get
315        one from the OS or libpcap? */
316     descr = NULL;
317     if_list = capture_interface_list(&err, NULL, NULL);
318     if (if_list != NULL) {
319       if_entry = if_list;
320       do {
321         if_info = (if_info_t *)if_entry->data;
322         if (strcmp(if_info->name, if_name) == 0) {
323           if (if_info->friendly_name != NULL) {
324               /* We have a "friendly name"; return a copy of that
325                  as the description - when we free the interface
326                  list, that'll also free up the strings to which
327                  it refers. */
328               descr = g_strdup(if_info->friendly_name);
329           } else if (if_info->vendor_description != NULL) {
330             /* We have no "friendly name", but we have a vendor
331                description; return a copy of that - when we free
332                the interface list, that'll also free up the strings
333                to which it refers. */
334             descr = g_strdup(if_info->vendor_description);
335           }
336           break;
337         }
338       } while ((if_entry = g_list_next(if_entry)) != NULL);
339     }
340     free_interface_list(if_list);
341
342     if (descr == NULL) {
343       /* The interface name is all we have, so just return a copy of that. */
344       descr = g_strdup(if_name);
345     }
346   }
347
348   return descr;
349 }
350
351
352 /* search interface info by interface name */
353 static if_info_t *
354 search_info(GList *if_list, gchar *if_name)
355 {
356   GList *if_entry;
357   if_info_t *if_info;
358
359
360   for (if_entry = if_list; if_entry != NULL; if_entry = g_list_next(if_entry)) {
361     if_info = (if_info_t *)if_entry->data;
362
363     if(strcmp(if_name, if_info->name) == 0) {
364       return if_info;
365     }
366   }
367
368   return NULL;
369 }
370
371
372 /* build the string to display in the combo box for the given interface */
373 char *
374 build_capture_combo_name(GList *if_list, gchar *if_name)
375 {
376   gchar *descr;
377   char *if_string;
378   if_info_t *if_info;
379
380   /* Do we have a user-supplied description? */
381   descr = capture_dev_user_descr_find(if_name);
382   if (descr != NULL) {
383     /* Yes, we have a user-supplied description; use it. */
384     if_string = g_strdup_printf("%s: %s", descr, if_name);
385     g_free(descr);
386   } else {
387     /* No, we don't have a user-supplied description; did we get
388      one from the OS or libpcap? */
389     if_info = search_info(if_list, if_name);
390     if (if_info != NULL && if_info->vendor_description != NULL) {
391       /* Yes - use it. */
392       if_string = g_strdup_printf("%s: %s", if_info->vendor_description,
393                                   if_info->name);
394     } else {
395       /* No. */
396       if_string = g_strdup(if_name);
397     }
398   }
399
400   return if_string;
401 }
402
403
404 GList *
405 build_capture_combo_list(GList *if_list, gboolean do_hide)
406 {
407   GList *combo_list;
408   GList *if_entry;
409   if_info_t *if_info;
410   char *if_string;
411   gchar *descr;
412
413   combo_list = NULL;
414   if (if_list != NULL) {
415     /* Scan through the list and build a list of strings to display. */
416     for (if_entry = if_list; if_entry != NULL;
417          if_entry = g_list_next(if_entry)) {
418       if_info = (if_info_t *)if_entry->data;
419
420       /* Is this interface hidden and, if so, should we include it
421          anyway? */
422       if (!prefs_is_capture_device_hidden(if_info->name) || !do_hide) {
423         /* It's not hidden, or it is but we should include it in the list. */
424
425         /* Do we have a user-supplied description? */
426         descr = capture_dev_user_descr_find(if_info->name);
427         if (descr != NULL) {
428           /* Yes, we have a user-supplied description; use it. */
429           if_string = g_strdup_printf("%s: %s", descr, if_info->name);
430           g_free(descr);
431         } else {
432           /* No, we don't have a user-supplied description; did we get
433              one from the OS or libpcap? */
434           if (if_info->vendor_description != NULL) {
435             /* Yes - use it. */
436             if_string = g_strdup_printf("%s: %s",
437                                         if_info->vendor_description,
438                                         if_info->name);
439           } else {
440             /* No. */
441             if_string = g_strdup(if_info->name);
442           }
443         }
444         combo_list = g_list_append(combo_list, if_string);
445       }
446     }/*for*/
447   }
448   return combo_list;
449 }
450
451 static void
452 free_if_string(gpointer data, gpointer user_data _U_)
453 {
454   g_free(data);
455 }
456
457 void
458 free_capture_combo_list(GList *combo_list)
459 {
460   if (combo_list != NULL) {
461     g_list_foreach(combo_list, free_if_string, NULL);
462     g_list_free(combo_list);
463   }
464 }
465
466 /*
467  * Given text that contains an interface name possibly prefixed by an
468  * interface description, extract the interface name.
469  */
470 const char *
471 get_if_name(const char *if_text)
472 {
473   const char *if_name;
474
475 #ifdef _WIN32
476   /*
477    * We cannot assume that the interface name doesn't contain a space;
478    * some names on Windows OT do.
479    *
480    * We also can't assume it begins with "\Device\", either, as, on
481    * Windows OT, WinPcap doesn't put "\Device\" in front of the name.
482    *
483    * As I remember, we can't assume that the interface description
484    * doesn't contain a colon, either; I think some do.
485    *
486    * We can probably assume that the interface *name* doesn't contain
487    * a colon, however; if any interface name does contain a colon on
488    * Windows, it'll be time to just get rid of the damn interface
489    * descriptions in the drop-down list, have just the names in the
490    * drop-down list, and have a "Browse..." button to browse for interfaces,
491    * with names, descriptions, IP addresses, blah blah blah available when
492    * possible.
493    *
494    * So we search backwards for a colon.  If we don't find it, just
495    * return the entire string; otherwise, skip the colon and any blanks
496    * after it, and return that string.
497    */
498    if_name = if_text + strlen(if_text);
499    for (;;) {
500      if (if_name == if_text) {
501        /* We're at the beginning of the string; return it. */
502        break;
503      }
504      if_name--;
505      if (*if_name == ':') {
506        /*
507         * We've found a colon.
508         * Unfortunately, a colon is used in the string "rpcap://",
509         * which is used in case of a remote capture.
510         * So we'll check to make sure the colon isn't followed by "//";
511         * it'll be followed by a blank if it separates the description
512         * and the interface name.  (We don't wire in "rpcap", in case we
513         * support other protocols in the same syntax.)
514         * Unfortunately, another colon can be used in "rpcap://host:port/"
515         * before port. Check if colon is followed by digit.
516         */
517        if ((strncmp(if_name, "://", 3) != 0) && !isdigit(if_name[1])) {
518          /*
519           * OK, we've found a colon followed neither by "//" nor by digit.
520           * Skip blanks following it.
521           */
522          if_name++;
523          while (*if_name == ' ')
524            if_name++;
525          break;
526        }
527      }
528      /* Keep looking for a colon not followed by "//". */
529    }
530 #else
531   /*
532    * There's a space between the interface description and name, and
533    * the interface name shouldn't have a space in it (it doesn't, on
534    * UNIX systems); look backwards in the string for a space.
535    *
536    * (An interface name might, however, contain a colon in it, which
537    * is why we don't use the colon search on UNIX.)
538    */
539   if_name = strrchr(if_text, ' ');
540   if (if_name == NULL) {
541     if_name = if_text;
542   } else {
543     if_name++;
544   }
545 #endif
546   return if_name;
547 }
548
549 /*  Return interface_opts->descr (after setting it if it is not set)
550  *  This is necessary because capture_opts.c can't set descr (at least
551  *  not without adding significant dependencies there).
552  */
553 const char *
554 get_iface_description_for_interface(capture_options *capture_opts, guint i)
555 {
556   interface_options interface_opts;
557
558   if (i < capture_opts->ifaces->len) {
559     interface_opts = g_array_index(capture_opts->ifaces, interface_options, i);
560     if (!interface_opts.descr && interface_opts.name) {
561       interface_opts.descr = get_interface_descriptive_name(interface_opts.name);
562       capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, i);
563       g_array_insert_val(capture_opts->ifaces, i, interface_opts);
564     }
565     return (interface_opts.descr);
566   } else {
567     return (NULL);
568   }
569 }
570
571 #endif /* HAVE_LIBPCAP */