aboutsummaryrefslogtreecommitdiffstats
path: root/gtk/capture_if_dlg.c
blob: f4d7eb21ae196ad7051b251867bff7f3e7dd18d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
/* capture_if_dlg.c
 * Routines for the capture interface dialog
 *
 * $Id: capture_if_dlg.c,v 1.254 2004/06/30 06:58:56 guy Exp $
 *
 * Ethereal - Network traffic analyzer
 * By Gerald Combs <gerald@ethereal.com>
 * Copyright 1998 Gerald Combs
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

/* With MSVC and a libethereal.dll this file needs to import some variables 
   in a special way. Therefore _NEED_VAR_IMPORT_ is defined. */
/*#define _NEED_VAR_IMPORT_*/

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#ifdef HAVE_LIBPCAP

#ifdef HAVE_SYS_STAT_H
# include <sys/stat.h>
#endif

#ifdef HAVE_SYS_WAIT_H
# include <sys/wait.h>
#endif


#include <pcap.h>

#include <gtk/gtk.h>

#include "globals.h"
#include "pcap-util.h"

#ifdef _WIN32
#include "capture-wpcap.h"
#endif

#include "compat_macros.h"
#include "simple_dialog.h"
#include "capture_dlg.h"

#include "ui_util.h"
#include "dlg_utils.h"

#include "wtap.h"
#include "capture.h"


extern char *iptos(u_long in);

extern gboolean is_capture_in_progress(void);

/*
 * Keep a static pointer to the current "Capture Interfaces" window, if
 * any, so that if somebody tries to do "Capture:Start" while there's
 * already a "Capture Interfaces" window up, we just pop up the existing
 * one, rather than creating a new one.
 */
static GtkWidget *cap_if_w;

GList           *if_data = NULL;

guint           timer_id;

GtkWidget       *stop_bt;

GList           *if_list;

/*
 * Timeout, in milliseconds, for reads from the stream of captured packets.
 */
#define	CAP_READ_TIMEOUT	250


/* the "runtime" data of one interface */
typedef struct if_dlg_data_s {
    pcap_t      *pch;
    GtkWidget   *device_lb;
    GtkWidget   *descr_lb;
    GtkWidget   *ip_lb;
    GtkWidget   *curr_lb;
    GtkWidget   *last_lb;
    GtkWidget   *capture_bt;
    GtkWidget   *prepare_bt;
    guint32     last_packets;
    gchar       *device;
} if_dlg_data_t;

void update_if(if_dlg_data_t *if_dlg_data);


/* start capture button was pressed */
static void
capture_do_cb(GtkWidget *capture_bt _U_, gpointer if_data)
{
  if_dlg_data_t *if_dlg_data = if_data;

  if (cfile.iface)
    g_free(cfile.iface);

  cfile.iface = g_strdup(if_dlg_data->device);

  do_capture(NULL /* save_file */);
}


/* prepare capture button was pressed */
static void
capture_prepare_cb(GtkWidget *prepare_bt _U_, gpointer if_data)
{
  if_dlg_data_t *if_dlg_data = if_data;

  if (cfile.iface)
    g_free(cfile.iface);

  cfile.iface = g_strdup(if_dlg_data->device);

  capture_prep_cb(NULL, NULL);
}


/* open a single interface */
void
open_if(gchar *name, if_dlg_data_t *if_dlg_data)
{
  gchar       open_err_str[PCAP_ERRBUF_SIZE];

  if_dlg_data->pch = pcap_open_live(name,
		       WTAP_MAX_PACKET_SIZE,
		       capture_opts.promisc_mode, CAP_READ_TIMEOUT,
		       open_err_str);

  if (if_dlg_data->pch != NULL) {
      update_if(if_dlg_data);
  } else {
    printf("open_if: %s\n", open_err_str);
    gtk_label_set_text(GTK_LABEL(if_dlg_data->curr_lb), "error");
    gtk_label_set_text(GTK_LABEL(if_dlg_data->last_lb), "error");
  }
}

/* update a single interface */
void
update_if(if_dlg_data_t *if_dlg_data)
{
  struct pcap_stat stats;
  gchar *str;
  guint diff;


  /* pcap_stats() stats values differ on libpcap and winpcap!
   * libpcap: returns the number of packets since pcap_open_live
   * winpcap: returns the number of packets since the last pcap_stats call
   */
  if (if_dlg_data->pch) {
    if(pcap_stats(if_dlg_data->pch, &stats) >= 0) {
#if WIN32
    diff = stats.ps_recv - if_dlg_data->last_packets;
    if_dlg_data->last_packets = stats.ps_recv;
#else
    diff = stats.ps_recv;
    if_dlg_data->last_packets = stats.ps_recv + if_dlg_data->last_packets;
#endif    

    str = g_strdup_printf("%u", if_dlg_data->last_packets);
    gtk_label_set_text(GTK_LABEL(if_dlg_data->curr_lb), str);
    g_free(str);
    str = g_strdup_printf("%u", diff);
    gtk_label_set_text(GTK_LABEL(if_dlg_data->last_lb), str);
    g_free(str);

    gtk_widget_set_sensitive(if_dlg_data->curr_lb, diff);
    gtk_widget_set_sensitive(if_dlg_data->last_lb, diff);
  } else {
    gtk_label_set_text(GTK_LABEL(if_dlg_data->curr_lb), "error");
    gtk_label_set_text(GTK_LABEL(if_dlg_data->last_lb), "error");
  }
  }
}


/* close a single interface */
void
close_if(if_dlg_data_t *if_dlg_data)
{
    if(if_dlg_data->pch)
        pcap_close(if_dlg_data->pch);
}



/* update all interfaces */
gboolean
update_all(gpointer data)
{
    GList *curr;
    int ifs;


    if(!cap_if_w) {
        return FALSE;
    }

    for(ifs = 0; (curr = g_list_nth(data, ifs)); ifs++) {
        update_if(curr->data);
    }

    return TRUE;
}


/* a live capture has started or stopped */
void
set_capture_if_dialog_for_capture_in_progress(gboolean capture_in_progress)
{
    GList *curr;
    int ifs;

    if(cap_if_w) {
        gtk_widget_set_sensitive(stop_bt, capture_in_progress);
        
        for(ifs = 0; (curr = g_list_nth(if_data, ifs)); ifs++) {
            if_dlg_data_t *if_dlg_data = curr->data;

            gtk_widget_set_sensitive(if_dlg_data->capture_bt, !capture_in_progress);
            gtk_widget_set_sensitive(if_dlg_data->prepare_bt, !capture_in_progress);
        }        
    }
}


/* the window was closed, cleanup things */
static void
capture_if_destroy_cb(GtkWidget *win _U_, gpointer user_data _U_)
{
    GList *curr;
    int ifs;

    gtk_timeout_remove(timer_id);

    for(ifs = 0; (curr = g_list_nth(if_data, ifs)); ifs++) {
        if_dlg_data_t *if_dlg_data = curr->data;

        close_if(if_dlg_data);
        g_free(curr->data);
    }

    if_data = NULL;

    free_interface_list(if_list);

    /* Note that we no longer have a "Capture Options" dialog box. */
    cap_if_w = NULL;
}


/* start getting capture stats from all interfaces */
void
capture_if_cb(GtkWidget *w _U_, gpointer d _U_)
{
  GtkWidget     *main_vb, *bbox, *close_bt;

  GtkWidget     *if_tb;
  GtkWidget     *if_lb;
#if GTK_MAJOR_VERSION < 2
  GtkAccelGroup *accel_group;
#endif
  GtkTooltips   *tooltips;
  int           err;
  char          err_str[PCAP_ERRBUF_SIZE];
  gchar         *cant_get_if_list_errstr;
  int           row;
  if_dlg_data_t *if_dlg_data;
  int ifs;
  GList *curr;
  if_info_t *if_info;
  GSList *curr_ip;
  guint32       ip_addr;
  GString       *if_tool_str = g_string_new("");
  gchar         *tmp_str;


  if (cap_if_w != NULL) {
    /* There's already a "Capture Interfaces" dialog box; reactivate it. */
    reactivate_window(cap_if_w);
    return;
  }

#ifdef _WIN32
  /* Is WPcap loaded? */
  if (!has_wpcap) {
	  simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
		  "Unable to load WinPcap (wpcap.dll); Ethereal will not be able\n"
		  "to capture packets.\n\n"
		  "In order to capture packets, WinPcap must be installed; see\n"
		  "\n"
		  "        http://winpcap.polito.it/\n"
		  "\n"
		  "or the mirror at\n"
		  "\n"
		  "        http://winpcap.mirror.ethereal.com/\n"
		  "\n"
		  "or the mirror at\n"
		  "\n"
		  "        http://www.mirrors.wiretapped.net/security/packet-capture/winpcap/\n"
		  "\n"
		  "for a downloadable version of WinPcap and for instructions\n"
		  "on how to install WinPcap.");
	  return;
  }
#endif

  if_list = get_interface_list(&err, err_str);
  if (if_list == NULL && err == CANT_GET_INTERFACE_LIST) {
    cant_get_if_list_errstr = cant_get_if_list_error_message(err_str);
    simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s",
                  cant_get_if_list_errstr);
    g_free(cant_get_if_list_errstr);
    return;
  }

  cap_if_w = window_new(GTK_WINDOW_TOPLEVEL, "Ethereal: Capture Interfaces");

  tooltips = gtk_tooltips_new();

#if GTK_MAJOR_VERSION < 2
  /* Accelerator group for the accelerators (or, as they're called in
     Windows and, I think, in Motif, "mnemonics"; Alt+<key> is a mnemonic,
     Ctrl+<key> is an accelerator). */
  accel_group = gtk_accel_group_new();
  gtk_window_add_accel_group(GTK_WINDOW(cap_if_w), accel_group);
#endif

  main_vb = gtk_vbox_new(FALSE, 0);
  gtk_container_border_width(GTK_CONTAINER(main_vb), 5);
  gtk_container_add(GTK_CONTAINER(cap_if_w), main_vb);


  if_tb = gtk_table_new(6,1, FALSE);
  gtk_table_set_row_spacings(GTK_TABLE(if_tb), 3);
  gtk_table_set_col_spacings(GTK_TABLE(if_tb), 3);
  gtk_container_add(GTK_CONTAINER(main_vb), if_tb);

  row = 0;

#ifndef WIN32
  if_lb = gtk_label_new("Device");
  gtk_table_attach_defaults(GTK_TABLE(if_tb), if_lb, 0, 1, row, row+1);
#endif

  if_lb = gtk_label_new("Description");
  gtk_table_attach_defaults(GTK_TABLE(if_tb), if_lb, 1, 2, row, row+1);

  if_lb = gtk_label_new(" IP ");
  gtk_table_attach_defaults(GTK_TABLE(if_tb), if_lb, 2, 3, row, row+1);

  if_lb = gtk_label_new("Packets");
  gtk_table_attach_defaults(GTK_TABLE(if_tb), if_lb, 3, 4, row, row+1);

  if_lb = gtk_label_new(" Packets/s ");
  gtk_table_attach_defaults(GTK_TABLE(if_tb), if_lb, 4, 5, row, row+1);

  stop_bt = BUTTON_NEW_FROM_STOCK(GTK_STOCK_STOP);
  gtk_tooltips_set_tip(tooltips, stop_bt, 
          "Stop a running capture.", NULL);
  gtk_table_attach_defaults(GTK_TABLE(if_tb), stop_bt, 5, 7, row, row+1);
  SIGNAL_CONNECT(stop_bt, "clicked", capture_stop_cb, NULL);

  row++;

  for(ifs = 0; (curr = g_list_nth(if_list, ifs)); ifs++) {
      g_string_assign(if_tool_str, "");
      if_info = curr->data;
      if_dlg_data = g_malloc0(sizeof(if_dlg_data_t));

      /* device name */
      if_dlg_data->device_lb = gtk_label_new(if_info->name);
      if_dlg_data->device = if_info->name;
#ifndef WIN32
      gtk_misc_set_alignment(GTK_MISC(if_dlg_data->device_lb), 0.0, 0.5);
      gtk_table_attach_defaults(GTK_TABLE(if_tb), if_dlg_data->device_lb, 0, 1, row, row+1);
#endif
      g_string_append(if_tool_str, "Device: ");
      g_string_append(if_tool_str, if_info->name);
      g_string_append(if_tool_str, "\n");

      /* description */
      if_dlg_data->descr_lb = gtk_label_new(if_info->description);
      gtk_misc_set_alignment(GTK_MISC(if_dlg_data->descr_lb), 0.0, 0.5);
      gtk_table_attach_defaults(GTK_TABLE(if_tb), if_dlg_data->descr_lb, 1, 2, row, row+1);

      g_string_append(if_tool_str, "Description: ");
      g_string_append(if_tool_str, if_info->description);
      g_string_append(if_tool_str, "\n");

      /* IP address */
      /* only one IP address will be shown */
      g_string_append(if_tool_str, "IP: ");
      curr_ip = g_slist_nth(if_info->ip_addr, 0);
      if(curr_ip) {
        ip_addr = *((guint32 *)curr_ip->data);
        if_dlg_data->ip_lb = gtk_label_new(iptos(ip_addr));
        g_string_append(if_tool_str, iptos(ip_addr));
      } else {
        ip_addr = 0;
        if_dlg_data->ip_lb = gtk_label_new("unknown");
        g_string_append(if_tool_str, "unknown");
      }
      gtk_widget_set_sensitive(if_dlg_data->ip_lb, ip_addr);
      gtk_table_attach_defaults(GTK_TABLE(if_tb), if_dlg_data->ip_lb, 2, 3, row, row+1);
      g_string_append(if_tool_str, "\n");

      /* packets */
      if_dlg_data->curr_lb = gtk_label_new("-");
      gtk_table_attach_defaults(GTK_TABLE(if_tb), if_dlg_data->curr_lb, 3, 4, row, row+1);

      /* packets/s */
      if_dlg_data->last_lb = gtk_label_new("-");
      gtk_table_attach_defaults(GTK_TABLE(if_tb), if_dlg_data->last_lb, 4, 5, row, row+1);

      /* capture button */
      if_dlg_data->capture_bt = gtk_button_new_with_label("Capture");
      SIGNAL_CONNECT(if_dlg_data->capture_bt, "clicked", capture_do_cb, if_dlg_data);
      tmp_str = g_strdup_printf("Immediately start a capture from this interface:\n\n%s", if_tool_str->str);
      gtk_tooltips_set_tip(tooltips, if_dlg_data->capture_bt, 
          tmp_str, NULL);
      g_free(tmp_str);
      gtk_table_attach_defaults(GTK_TABLE(if_tb), if_dlg_data->capture_bt, 5, 6, row, row+1);

      /* prepare button */
      if_dlg_data->prepare_bt = gtk_button_new_with_label("Prepare");
      SIGNAL_CONNECT(if_dlg_data->prepare_bt, "clicked", capture_prepare_cb, if_dlg_data);
      gtk_tooltips_set_tip(tooltips, if_dlg_data->prepare_bt, 
          "Open the capture options dialog with this interface selected.", NULL);
      gtk_table_attach_defaults(GTK_TABLE(if_tb), if_dlg_data->prepare_bt, 6, 7, row, row+1);

      open_if(if_info->name, if_dlg_data);

      if_data = g_list_append(if_data, if_dlg_data);

      row++;
  }

  g_string_free(if_tool_str, TRUE);

  /* Button row: close button */
  bbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
  gtk_box_pack_start(GTK_BOX(main_vb), bbox, FALSE, FALSE, 5);

  close_bt = OBJECT_GET_DATA(bbox, GTK_STOCK_CLOSE);
  window_set_cancel_button(cap_if_w, close_bt, window_cancel_button_cb);
  gtk_tooltips_set_tip(tooltips, close_bt, "Close this window.", NULL);

  gtk_widget_grab_default(close_bt);

  SIGNAL_CONNECT(cap_if_w, "delete_event", window_delete_event_cb, NULL);
  SIGNAL_CONNECT(cap_if_w, "destroy", capture_if_destroy_cb, NULL);

  gtk_widget_show_all(cap_if_w);
  window_present(cap_if_w);

  set_capture_if_dialog_for_capture_in_progress(is_capture_in_progress());

    /* update the interface list every 1000ms */
  timer_id = gtk_timeout_add(1000, update_all, if_data);
}


#endif /* HAVE_LIBPCAP */