replace *a lot* of file related calls by their GLib counterparts. This is necessary...
[obnox/wireshark/wip.git] / gtk / capture_if_dlg.c
1 /* capture_if_dlg.c
2  * Routines for the capture interface dialog
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 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #ifdef HAVE_LIBPCAP
30
31 #ifdef HAVE_SYS_WAIT_H
32 # include <sys/wait.h>
33 #endif
34
35
36 #include <pcap.h>
37
38 #include <gtk/gtk.h>
39
40 #include "globals.h"
41 #include "pcap-util.h"
42
43 #ifdef _WIN32
44 #include "capture-wpcap.h"
45 #endif
46
47 #include "compat_macros.h"
48 #include "simple_dialog.h"
49 #include "capture.h"
50 #include "capture_dlg.h"
51 #include "capture_if_details_dlg.h"
52 #include "capture_errs.h"
53
54 #include "gui_utils.h"
55 #include "dlg_utils.h"
56
57 #include "wtap.h"
58 #include "main.h"
59 #include "help_dlg.h"
60 #include "toolbar.h"
61
62 /*
63  * Keep a static pointer to the current "Capture Interfaces" window, if
64  * any, so that if somebody tries to do "Capture:Start" while there's
65  * already a "Capture Interfaces" window up, we just pop up the existing
66  * one, rather than creating a new one.
67  */
68 static GtkWidget *cap_if_w;
69
70 GList           *if_data = NULL;
71
72 guint           timer_id;
73
74 GtkWidget       *stop_bt;
75
76 GList           *if_list;
77
78 /*
79  * Timeout, in milliseconds, for reads from the stream of captured packets.
80  */
81 #define CAP_READ_TIMEOUT        250
82
83
84 /* the "runtime" data of one interface */
85 typedef struct if_dlg_data_s {
86     pcap_t      *pch;
87     GtkWidget   *device_lb;
88     GtkWidget   *descr_lb;
89     GtkWidget   *ip_lb;
90     GtkWidget   *curr_lb;
91     GtkWidget   *last_lb;
92     GtkWidget   *capture_bt;
93     GtkWidget   *prepare_bt;
94 #ifdef _WIN32
95     GtkWidget   *details_bt;
96 #endif
97     guint32     last_packets;
98     gchar       *device;
99 } if_dlg_data_t;
100
101 void update_if(if_dlg_data_t *if_dlg_data);
102
103
104 /* start capture button was pressed */
105 static void
106 capture_do_cb(GtkWidget *capture_bt _U_, gpointer if_data)
107 {
108   if_dlg_data_t *if_dlg_data = if_data;
109
110   if (capture_opts->iface)
111     g_free(capture_opts->iface);
112
113   capture_opts->iface = g_strdup(if_dlg_data->device);
114
115   /* XXX - remove this? */
116   if (capture_opts->save_file) {
117     g_free(capture_opts->save_file);
118     capture_opts->save_file = NULL;
119   }
120
121   /* stop capturing from all interfaces, we are going to do real work now ... */
122   window_destroy(cap_if_w);
123
124   capture_start_cb(NULL, NULL);
125 }
126
127
128 /* prepare capture button was pressed */
129 static void
130 capture_prepare_cb(GtkWidget *prepare_bt _U_, gpointer if_data)
131 {
132   if_dlg_data_t *if_dlg_data = if_data;
133
134   if (capture_opts->iface)
135     g_free(capture_opts->iface);
136
137   capture_opts->iface = g_strdup(if_dlg_data->device);
138
139   /* stop capturing from all interfaces, we are going to do real work now ... */
140   window_destroy(cap_if_w);
141
142   capture_prep_cb(NULL, NULL);
143 }
144
145
146 #ifdef _WIN32
147 /* capture details button was pressed */
148 static void
149 capture_details_cb(GtkWidget *details_bt _U_, gpointer if_data)
150 {
151   if_dlg_data_t *if_dlg_data = if_data;
152
153
154   capture_if_details_open(if_dlg_data->device);
155 }
156 #endif
157
158
159 /* open a single interface */
160 static void
161 open_if(gchar *name, if_dlg_data_t *if_dlg_data)
162 {
163   gchar       open_err_str[PCAP_ERRBUF_SIZE];
164
165   /*
166    * XXX - on systems with BPF, the number of BPF devices limits the
167    * number of devices on which you can capture simultaneously.
168    *
169    * This means that
170    *
171    *    1) this might fail if you run out of BPF devices
172    *
173    * and
174    *
175    *    2) opening every interface could leave too few BPF devices
176    *       for *other* programs.
177    *
178    * It also means the system could end up getting a lot of traffic
179    * that it has to pass through the networking stack and capture
180    * mechanism, so opening all the devices and presenting packet
181    * counts might not always be a good idea.
182    */
183   if_dlg_data->pch = pcap_open_live(name,
184                        MIN_PACKET_SIZE,
185                        capture_opts->promisc_mode, CAP_READ_TIMEOUT,
186                        open_err_str);
187
188   if (if_dlg_data->pch != NULL) {
189     update_if(if_dlg_data);
190   } else {
191     printf("open_if: %s\n", open_err_str);
192     gtk_label_set_text(GTK_LABEL(if_dlg_data->curr_lb), "error");
193     gtk_label_set_text(GTK_LABEL(if_dlg_data->last_lb), "error");
194   }
195 }
196
197 /* update a single interface */
198 void
199 update_if(if_dlg_data_t *if_dlg_data)
200 {
201   struct pcap_stat stats;
202   gchar *str;
203   guint diff;
204
205
206   /* pcap_stats() stats values differ on libpcap and winpcap!
207    * libpcap: returns the number of packets since pcap_open_live
208    * winpcap: returns the number of packets since the last pcap_stats call
209    * XXX - if that's true, that's a bug, and should be fixed; "pcap_stats()"
210    * is supposed to work the same way on all platforms, including Windows.
211    * Note that the WinPcap 3.0 documentation says "The values represent
212    * packet statistics from the start of the run to the time of the call."
213    * (Note also that some versions of libpcap, on some versions of UN*X,
214    * have the same bug.)
215    */
216   if (if_dlg_data->pch) {
217     if(pcap_stats(if_dlg_data->pch, &stats) >= 0) {
218 #ifdef _WIN32
219       diff = stats.ps_recv - if_dlg_data->last_packets;
220       if_dlg_data->last_packets = stats.ps_recv;
221 #else
222       diff = stats.ps_recv;
223       if_dlg_data->last_packets = stats.ps_recv + if_dlg_data->last_packets;
224 #endif    
225
226       str = g_strdup_printf("%u", if_dlg_data->last_packets);
227       gtk_label_set_text(GTK_LABEL(if_dlg_data->curr_lb), str);
228       g_free(str);
229       str = g_strdup_printf("%u", diff);
230       gtk_label_set_text(GTK_LABEL(if_dlg_data->last_lb), str);
231       g_free(str);
232
233       gtk_widget_set_sensitive(if_dlg_data->curr_lb, diff);
234       gtk_widget_set_sensitive(if_dlg_data->last_lb, diff);
235     } else {
236       gtk_label_set_text(GTK_LABEL(if_dlg_data->curr_lb), "error");
237       gtk_label_set_text(GTK_LABEL(if_dlg_data->last_lb), "error");
238     }
239   }
240 }
241
242
243 /* close a single interface */
244 static void
245 close_if(if_dlg_data_t *if_dlg_data)
246 {
247     if(if_dlg_data->pch)
248         pcap_close(if_dlg_data->pch);
249 }
250
251
252
253 /* update all interfaces */
254 static gboolean
255 update_all(gpointer data)
256 {
257     GList *curr;
258     int ifs;
259
260
261     if(!cap_if_w) {
262         return FALSE;
263     }
264
265     for(ifs = 0; (curr = g_list_nth(data, ifs)); ifs++) {
266         update_if(curr->data);
267     }
268
269     return TRUE;
270 }
271
272
273 /* a live capture has started or stopped */
274 void
275 set_capture_if_dialog_for_capture_in_progress(gboolean capture_in_progress)
276 {
277     GList *curr;
278     int ifs;
279
280     if(cap_if_w) {
281         gtk_widget_set_sensitive(stop_bt, capture_in_progress);
282         
283         for(ifs = 0; (curr = g_list_nth(if_data, ifs)); ifs++) {
284             if_dlg_data_t *if_dlg_data = curr->data;
285
286             gtk_widget_set_sensitive(if_dlg_data->capture_bt, !capture_in_progress);
287             gtk_widget_set_sensitive(if_dlg_data->prepare_bt, !capture_in_progress);
288         }        
289     }
290 }
291
292
293 /* the window was closed, cleanup things */
294 static void
295 capture_if_destroy_cb(GtkWidget *win _U_, gpointer user_data _U_)
296 {
297     GList *curr;
298     int ifs;
299
300     gtk_timeout_remove(timer_id);
301
302     for(ifs = 0; (curr = g_list_nth(if_data, ifs)); ifs++) {
303         if_dlg_data_t *if_dlg_data = curr->data;
304
305         close_if(if_dlg_data);
306         g_free(curr->data);
307     }
308
309     if_data = NULL;
310
311     free_interface_list(if_list);
312
313     /* Note that we no longer have a "Capture Options" dialog box. */
314     cap_if_w = NULL;
315 }
316
317
318 /* start getting capture stats from all interfaces */
319 void
320 capture_if_cb(GtkWidget *w _U_, gpointer d _U_)
321 {
322   GtkWidget     *main_vb, *bbox, *close_bt, *help_bt;
323
324   GtkWidget     *if_tb;
325   GtkWidget     *if_lb;
326 #if GTK_MAJOR_VERSION < 2
327   GtkAccelGroup *accel_group;
328 #endif
329   GtkTooltips   *tooltips;
330   int           err;
331   char          err_str[PCAP_ERRBUF_SIZE];
332   gchar         *cant_get_if_list_errstr;
333   int           row;
334   if_dlg_data_t *if_dlg_data;
335   int           ifs;
336   GList         *curr;
337   if_info_t     *if_info;
338   GSList        *curr_ip;
339   if_addr_t     *ip_addr;
340   GString       *if_tool_str = g_string_new("");
341   gchar         *tmp_str;
342
343   if (cap_if_w != NULL) {
344     /* There's already a "Capture Interfaces" dialog box; reactivate it. */
345     reactivate_window(cap_if_w);
346     return;
347   }
348
349 #ifdef _WIN32
350   /* Is WPcap loaded? */
351   if (!has_wpcap) {
352     char *detailed_err;
353
354     detailed_err = cant_load_winpcap_err("Ethereal");
355     simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s", detailed_err);
356     g_free(detailed_err);
357     return;
358   }
359 #endif
360
361   if_list = get_interface_list(&err, err_str);
362   if (if_list == NULL && err == CANT_GET_INTERFACE_LIST) {
363     cant_get_if_list_errstr = cant_get_if_list_error_message(err_str);
364     simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s",
365                   cant_get_if_list_errstr);
366     g_free(cant_get_if_list_errstr);
367     return;
368   }
369
370   cap_if_w = window_new(GTK_WINDOW_TOPLEVEL, "Ethereal: Capture Interfaces");
371
372   tooltips = gtk_tooltips_new();
373
374 #if GTK_MAJOR_VERSION < 2
375   /* Accelerator group for the accelerators (or, as they're called in
376      Windows and, I think, in Motif, "mnemonics"; Alt+<key> is a mnemonic,
377      Ctrl+<key> is an accelerator). */
378   accel_group = gtk_accel_group_new();
379   gtk_window_add_accel_group(GTK_WINDOW(cap_if_w), accel_group);
380 #endif
381
382   main_vb = gtk_vbox_new(FALSE, 0);
383   gtk_container_border_width(GTK_CONTAINER(main_vb), 5);
384   gtk_container_add(GTK_CONTAINER(cap_if_w), main_vb);
385
386
387   if_tb = gtk_table_new(6,1, FALSE);
388   gtk_table_set_row_spacings(GTK_TABLE(if_tb), 3);
389   gtk_table_set_col_spacings(GTK_TABLE(if_tb), 3);
390   gtk_container_add(GTK_CONTAINER(main_vb), if_tb);
391
392   row = 0;
393
394 #ifndef _WIN32
395   /*
396    * On Windows, device names are generally not meaningful - NT 5
397    * uses long blobs with GUIDs in them, for example - so we don't
398    * bother showing them.
399    */
400   if_lb = gtk_label_new("Device");
401   gtk_table_attach_defaults(GTK_TABLE(if_tb), if_lb, 0, 1, row, row+1);
402 #endif
403
404   if_lb = gtk_label_new("Description");
405   gtk_table_attach_defaults(GTK_TABLE(if_tb), if_lb, 1, 2, row, row+1);
406
407   if_lb = gtk_label_new(" IP ");
408   gtk_table_attach_defaults(GTK_TABLE(if_tb), if_lb, 2, 3, row, row+1);
409
410   if_lb = gtk_label_new("Packets");
411   gtk_table_attach_defaults(GTK_TABLE(if_tb), if_lb, 3, 4, row, row+1);
412
413   if_lb = gtk_label_new(" Packets/s ");
414   gtk_table_attach_defaults(GTK_TABLE(if_tb), if_lb, 4, 5, row, row+1);
415
416   stop_bt = BUTTON_NEW_FROM_STOCK(GTK_STOCK_STOP);
417   gtk_tooltips_set_tip(tooltips, stop_bt, 
418           "Stop a running capture.", NULL);
419 #ifdef _WIN32
420   gtk_table_attach_defaults(GTK_TABLE(if_tb), stop_bt, 5, 8, row, row+1);
421 #else
422   gtk_table_attach_defaults(GTK_TABLE(if_tb), stop_bt, 5, 7, row, row+1);
423 #endif
424   SIGNAL_CONNECT(stop_bt, "clicked", capture_stop_cb, NULL);
425
426   row++;
427
428   for(ifs = 0; (curr = g_list_nth(if_list, ifs)); ifs++) {
429       g_string_assign(if_tool_str, "");
430       if_info = curr->data;
431       if_dlg_data = g_malloc0(sizeof(if_dlg_data_t));
432
433       /* device name */
434       if_dlg_data->device_lb = gtk_label_new(if_info->name);
435       if_dlg_data->device = if_info->name;
436 #ifndef _WIN32
437       gtk_misc_set_alignment(GTK_MISC(if_dlg_data->device_lb), 0.0, 0.5);
438       gtk_table_attach_defaults(GTK_TABLE(if_tb), if_dlg_data->device_lb, 0, 1, row, row+1);
439 #endif
440       g_string_append(if_tool_str, "Device: ");
441       g_string_append(if_tool_str, if_info->name);
442       g_string_append(if_tool_str, "\n");
443
444       /* description */
445       if (if_info->description != NULL)
446         if_dlg_data->descr_lb = gtk_label_new(if_info->description);
447       else
448         if_dlg_data->descr_lb = gtk_label_new("");
449       gtk_misc_set_alignment(GTK_MISC(if_dlg_data->descr_lb), 0.0, 0.5);
450       gtk_table_attach_defaults(GTK_TABLE(if_tb), if_dlg_data->descr_lb, 1, 2, row, row+1);
451
452       if (if_info->description) {
453         g_string_append(if_tool_str, "Description: ");
454         g_string_append(if_tool_str, if_info->description);
455         g_string_append(if_tool_str, "\n");
456       }
457
458       /* IP address */
459       /* only the first IP address will be shown */
460       g_string_append(if_tool_str, "IP: ");
461       curr_ip = g_slist_nth(if_info->ip_addr, 0);
462       if(curr_ip) {
463         ip_addr = (if_addr_t *)curr_ip->data;
464         switch (ip_addr->type) {
465
466         case AT_IPv4:
467           tmp_str = ip_to_str((guint8 *)&ip_addr->ip_addr.ip4_addr);
468           break;
469
470         case AT_IPv6:
471           tmp_str = ip6_to_str((struct e_in6_addr *)&ip_addr->ip_addr.ip6_addr);
472           break;
473
474         default:
475           g_assert_not_reached();
476           tmp_str = NULL;
477         }
478         if_dlg_data->ip_lb = gtk_label_new(tmp_str);
479         gtk_widget_set_sensitive(if_dlg_data->ip_lb, TRUE);
480         g_string_append(if_tool_str, tmp_str);
481       } else {
482         if_dlg_data->ip_lb = gtk_label_new("unknown");
483         gtk_widget_set_sensitive(if_dlg_data->ip_lb, FALSE);
484         g_string_append(if_tool_str, "unknown");
485       }
486       gtk_table_attach_defaults(GTK_TABLE(if_tb), if_dlg_data->ip_lb, 2, 3, row, row+1);
487       g_string_append(if_tool_str, "\n");
488
489       /* packets */
490       if_dlg_data->curr_lb = gtk_label_new("-");
491       gtk_table_attach_defaults(GTK_TABLE(if_tb), if_dlg_data->curr_lb, 3, 4, row, row+1);
492
493       /* packets/s */
494       if_dlg_data->last_lb = gtk_label_new("-");
495       gtk_table_attach_defaults(GTK_TABLE(if_tb), if_dlg_data->last_lb, 4, 5, row, row+1);
496
497       /* capture button */
498       if_dlg_data->capture_bt = gtk_button_new_with_label("Capture");
499       SIGNAL_CONNECT(if_dlg_data->capture_bt, "clicked", capture_do_cb, if_dlg_data);
500       tmp_str = g_strdup_printf("Immediately start a capture from this interface:\n\n%s", if_tool_str->str);
501       gtk_tooltips_set_tip(tooltips, if_dlg_data->capture_bt, 
502           tmp_str, NULL);
503       g_free(tmp_str);
504       gtk_table_attach_defaults(GTK_TABLE(if_tb), if_dlg_data->capture_bt, 5, 6, row, row+1);
505
506       /* prepare button */
507       if_dlg_data->prepare_bt = gtk_button_new_with_label("Prepare");
508       SIGNAL_CONNECT(if_dlg_data->prepare_bt, "clicked", capture_prepare_cb, if_dlg_data);
509       gtk_tooltips_set_tip(tooltips, if_dlg_data->prepare_bt, 
510           "Open the capture options dialog with this interface selected.", NULL);
511       gtk_table_attach_defaults(GTK_TABLE(if_tb), if_dlg_data->prepare_bt, 6, 7, row, row+1);
512
513       /* details button */
514 #ifdef _WIN32
515       if_dlg_data->details_bt = gtk_button_new_with_label("Details");
516       SIGNAL_CONNECT(if_dlg_data->details_bt, "clicked", capture_details_cb, if_dlg_data);
517       gtk_tooltips_set_tip(tooltips, if_dlg_data->details_bt, 
518           "Open the capture details dialog of this interface.", NULL);
519       gtk_table_attach_defaults(GTK_TABLE(if_tb), if_dlg_data->details_bt, 7, 8, row, row+1);
520 #endif
521
522       open_if(if_info->name, if_dlg_data);
523
524       if_data = g_list_append(if_data, if_dlg_data);
525
526       row++;
527   }
528
529   g_string_free(if_tool_str, TRUE);
530
531   /* Button row: close button */
532   if(topic_available(HELP_CAPTURE_INTERFACES_DIALOG)) {
533     bbox = dlg_button_row_new(GTK_STOCK_CLOSE, GTK_STOCK_HELP, NULL);
534   } else {
535     bbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
536   }
537   gtk_box_pack_start(GTK_BOX(main_vb), bbox, FALSE, FALSE, 5);
538
539   close_bt = OBJECT_GET_DATA(bbox, GTK_STOCK_CLOSE);
540   window_set_cancel_button(cap_if_w, close_bt, window_cancel_button_cb);
541   gtk_tooltips_set_tip(tooltips, close_bt, "Close this window.", NULL);
542
543   if(topic_available(HELP_CAPTURE_INTERFACES_DIALOG)) {
544     help_bt = OBJECT_GET_DATA(bbox, GTK_STOCK_HELP);
545     SIGNAL_CONNECT(help_bt, "clicked", topic_cb, HELP_CAPTURE_INTERFACES_DIALOG);
546   }
547
548   gtk_widget_grab_default(close_bt);
549
550   SIGNAL_CONNECT(cap_if_w, "delete_event", window_delete_event_cb, NULL);
551   SIGNAL_CONNECT(cap_if_w, "destroy", capture_if_destroy_cb, NULL);
552
553   gtk_widget_show_all(cap_if_w);
554   window_present(cap_if_w);
555
556   set_capture_if_dialog_for_capture_in_progress(is_capture_in_progress());
557
558     /* update the interface list every 1000ms */
559   timer_id = gtk_timeout_add(1000, update_all, if_data);
560 }
561
562
563 #endif /* HAVE_LIBPCAP */