update expert info LED also while doing live capturing
[obnox/wireshark/wip.git] / gtk / capture_prefs.c
1 /* capture_prefs.c
2  * Dialog box for capture preferences
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 <string.h>
32 #include <gtk/gtk.h>
33
34 #include "globals.h"
35 #include "capture_prefs.h"
36 #include "gtkglobals.h"
37 #include <epan/prefs.h>
38 #include "prefs_dlg.h"
39 #include "gui_utils.h"
40 #include "dlg_utils.h"
41 #include "simple_dialog.h"
42 #include "capture-pcap-util.h"
43 #include "capture_ui_utils.h"
44 #include "main.h"
45 #include "compat_macros.h"
46 #include "capture.h"
47 #include <epan/strutil.h>
48
49 #define DEVICE_KEY                              "device"
50 #define PROM_MODE_KEY                   "prom_mode"
51 #define CAPTURE_REAL_TIME_KEY   "capture_real_time"
52 #define AUTO_SCROLL_KEY                 "auto_scroll"
53 #define SHOW_INFO_KEY           "show_info"
54
55 #define CAPTURE_TABLE_ROWS 6
56
57 #define IFOPTS_CALLER_PTR_KEY   "ifopts_caller_ptr"
58 #define IFOPTS_DIALOG_PTR_KEY   "ifopts_dialog_ptr"
59 #define IFOPTS_TABLE_ROWS 2
60 #define IFOPTS_CLIST_COLS 4
61 #define IFOPTS_MAX_DESCR_LEN 128
62 #define IFOPTS_IF_NOSEL -1
63
64 /* interface options dialog */
65 static GtkWidget *cur_clist, *if_dev_lb, *if_name_lb, *if_descr_te, *if_hide_cb;
66 static gint ifrow;                                              /* current interface row selected */
67
68 static void ifopts_edit_cb(GtkWidget *w, gpointer data);
69 static void ifopts_edit_ok_cb(GtkWidget *w, gpointer parent_w);
70 static void ifopts_edit_destroy_cb(GtkWidget *win, gpointer data);
71 static void ifopts_edit_ifsel_cb(GtkWidget *clist, gint row, gint column,
72     GdkEventButton *event, gpointer data);
73 static void ifopts_edit_descr_changed_cb(GtkEditable *ed, gpointer udata);
74 static void ifopts_edit_hide_changed_cb(GtkToggleButton *tbt, gpointer udata);
75 static void ifopts_options_add(GtkCList *clist, if_info_t *if_info);
76 static void ifopts_options_free(gchar *text[]);
77 static void ifopts_if_clist_add(void);
78 static void ifopts_write_new_descr(void);
79 static void ifopts_write_new_hide(void);
80
81 GtkWidget*
82 capture_prefs_show(void)
83 {
84         GtkWidget       *main_tb, *main_vb;
85         GtkWidget       *if_cb, *if_lb, *promisc_cb, *sync_cb, *auto_scroll_cb, *show_info_cb;
86         GtkWidget       *ifopts_lb, *ifopts_bt;
87         GList           *if_list, *combo_list;
88         int             err;
89         int             row = 0;
90         GtkTooltips *tooltips = gtk_tooltips_new();
91
92         /* Main vertical box */
93         main_vb = gtk_vbox_new(FALSE, 7);
94         gtk_container_border_width(GTK_CONTAINER(main_vb), 5);
95
96         /* Main table */
97         main_tb = gtk_table_new(CAPTURE_TABLE_ROWS, 2, FALSE);
98         gtk_box_pack_start(GTK_BOX(main_vb), main_tb, FALSE, FALSE, 0);
99         gtk_table_set_row_spacings(GTK_TABLE(main_tb), 10);
100         gtk_table_set_col_spacings(GTK_TABLE(main_tb), 15);
101         gtk_widget_show(main_tb);
102
103         /* Default device */
104         if_lb = gtk_label_new("Default interface:");
105         gtk_table_attach_defaults(GTK_TABLE(main_tb), if_lb, 0, 1, row, row+1);
106         gtk_misc_set_alignment(GTK_MISC(if_lb), 1.0, 0.5);
107         gtk_widget_show(if_lb);
108
109         if_cb = gtk_combo_new();
110         /*
111          * XXX - what if we can't get the list?
112          */
113         if_list = capture_interface_list(&err, NULL);
114         combo_list = build_capture_combo_list(if_list, FALSE);
115         free_interface_list(if_list);
116         if (combo_list != NULL) {
117                 gtk_combo_set_popdown_strings(GTK_COMBO(if_cb), combo_list);
118                 free_capture_combo_list(combo_list);
119         }
120         if (prefs.capture_device)
121                 gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(if_cb)->entry),
122                     prefs.capture_device);
123         gtk_table_attach_defaults(GTK_TABLE(main_tb), if_cb, 1, 2, row, row+1);
124         gtk_tooltips_set_tip(tooltips, GTK_COMBO(if_cb)->entry,
125             "The default interface to be captured from.", NULL);
126         gtk_widget_show(if_cb);
127         OBJECT_SET_DATA(main_vb, DEVICE_KEY, if_cb);
128         row++;
129
130         /* Interface properties */
131         ifopts_lb = gtk_label_new("Interfaces:");
132         gtk_table_attach_defaults(GTK_TABLE(main_tb), ifopts_lb, 0, 1, row, row+1);
133         gtk_misc_set_alignment(GTK_MISC(ifopts_lb), 1.0, 0.5);
134         gtk_widget_show(ifopts_lb);
135
136         ifopts_bt = BUTTON_NEW_FROM_STOCK(WIRESHARK_STOCK_EDIT);
137         gtk_tooltips_set_tip(tooltips, ifopts_bt,
138             "Open a dialog box to set various interface options.", NULL);
139         SIGNAL_CONNECT(ifopts_bt, "clicked", ifopts_edit_cb, NULL);
140         gtk_table_attach_defaults(GTK_TABLE(main_tb), ifopts_bt, 1, 2, row, row+1);
141         row++;
142
143         /* Promiscuous mode */
144         promisc_cb = create_preference_check_button(main_tb, row++,
145             "Capture packets in promiscuous mode:", NULL,
146             prefs.capture_prom_mode);
147         gtk_tooltips_set_tip(tooltips, promisc_cb,
148             "Usually a network card will only capture the traffic sent to its own network address. "
149             "If you want to capture all traffic that the network card can \"see\", mark this option. "
150             "See the FAQ for some more details of capturing packets from a switched network.", NULL);
151         OBJECT_SET_DATA(main_vb, PROM_MODE_KEY, promisc_cb);
152
153         /* Real-time capture */
154         sync_cb = create_preference_check_button(main_tb, row++,
155             "Update list of packets in real time:", NULL,
156             prefs.capture_real_time);
157         gtk_tooltips_set_tip(tooltips, sync_cb,
158         "Update the list of packets while capture is in progress. "
159         "Don't use this option if you notice packet drops.", NULL);
160         OBJECT_SET_DATA(main_vb, CAPTURE_REAL_TIME_KEY, sync_cb);
161
162         /* Auto-scroll real-time capture */
163         auto_scroll_cb = create_preference_check_button(main_tb, row++,
164             "Automatic scrolling in live capture:", NULL,
165             prefs.capture_auto_scroll);
166         gtk_tooltips_set_tip(tooltips, auto_scroll_cb,
167         "Automatic scrolling of the packet list while live capture is in progress. ", NULL);
168         OBJECT_SET_DATA(main_vb, AUTO_SCROLL_KEY, auto_scroll_cb);
169
170         /* Show capture info dialog */
171         show_info_cb = create_preference_check_button(main_tb, row++,
172             "Hide capture info dialog:", NULL,
173             !prefs.capture_show_info);
174         gtk_tooltips_set_tip(tooltips, show_info_cb,
175             "Hide the capture info dialog while capturing. ", NULL);
176         OBJECT_SET_DATA(main_vb, SHOW_INFO_KEY, show_info_cb);
177
178         /* Show 'em what we got */
179         gtk_widget_show_all(main_vb);
180
181         return(main_vb);
182 }
183
184 void
185 capture_prefs_fetch(GtkWidget *w)
186 {
187         GtkWidget *if_cb, *promisc_cb, *sync_cb, *auto_scroll_cb, *show_info_cb;
188         gchar   *if_text;
189
190         if_cb = (GtkWidget *)OBJECT_GET_DATA(w, DEVICE_KEY);
191         promisc_cb = (GtkWidget *)OBJECT_GET_DATA(w, PROM_MODE_KEY);
192         sync_cb = (GtkWidget *)OBJECT_GET_DATA(w, CAPTURE_REAL_TIME_KEY);
193         auto_scroll_cb = (GtkWidget *)OBJECT_GET_DATA(w, AUTO_SCROLL_KEY);
194     show_info_cb = (GtkWidget *)OBJECT_GET_DATA(w, SHOW_INFO_KEY);
195
196         if (prefs.capture_device != NULL) {
197                 g_free(prefs.capture_device);
198                 prefs.capture_device = NULL;
199         }
200         if_text = g_strdup(gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(if_cb)->entry)));
201         /* Strip out white space */
202         g_strstrip(if_text);
203         /* If there was nothing but white space, treat that as an
204            indication that the user doesn't want to wire in a default
205            device, and just wants the first device in the list chosen. */
206         if (*if_text == '\0') {
207                 g_free(if_text);
208                 if_text = NULL;
209         }
210         prefs.capture_device = if_text;
211
212         prefs.capture_prom_mode = GTK_TOGGLE_BUTTON (promisc_cb)->active;
213
214         prefs.capture_real_time = GTK_TOGGLE_BUTTON (sync_cb)->active;
215
216         prefs.capture_auto_scroll = GTK_TOGGLE_BUTTON (auto_scroll_cb)->active;
217
218     prefs.capture_show_info = !(GTK_TOGGLE_BUTTON (show_info_cb)->active);
219 }
220
221 void
222 capture_prefs_apply(GtkWidget *w _U_)
223 {
224 }
225
226 void
227 capture_prefs_destroy(GtkWidget *w)
228 {
229         GtkWidget *caller = gtk_widget_get_toplevel(w);
230         GtkWidget *dlg;
231
232         /* Is there an interface descriptions dialog associated with this
233            Preferences dialog? */
234         dlg = OBJECT_GET_DATA(caller, IFOPTS_DIALOG_PTR_KEY);
235
236         if (dlg != NULL) {
237                 /* Yes.  Destroy it. */
238                 window_destroy(dlg);
239         }
240 }
241
242 /*
243  * Create an edit interface options dialog.
244  */
245 static void
246 ifopts_edit_cb(GtkWidget *w, gpointer data _U_)
247 {
248         GtkWidget       *ifopts_edit_dlg, *cur_scr_win, *main_hb, *main_tb,
249                                 *cur_opts_fr, *ed_opts_fr, *main_vb,
250                                 *if_descr_lb, *if_hide_lb,
251                                 *bbox, *ok_bt, *cancel_bt;
252         const gchar *cur_titles[] = { "Device", "Description", "Comment", "Hide?" };
253         int row = 0;
254
255         GtkWidget *caller = gtk_widget_get_toplevel(w);
256
257         /* Has an edit dialog box already been opened for that top-level
258            widget? */
259         ifopts_edit_dlg = OBJECT_GET_DATA(caller, IFOPTS_DIALOG_PTR_KEY);
260         if (ifopts_edit_dlg != NULL) {
261                 /* Yes.  Just re-activate that dialog box. */
262                 reactivate_window(ifopts_edit_dlg);
263                 return;
264         }
265
266         /* create a new dialog */
267         ifopts_edit_dlg = dlg_window_new("Wireshark: Preferences: Interface Options");
268     gtk_window_set_default_size(GTK_WINDOW(ifopts_edit_dlg), DEF_WIDTH, 300);
269
270     main_vb = gtk_vbox_new(FALSE, 1);
271         gtk_container_border_width(GTK_CONTAINER(main_vb), 5);
272         gtk_container_add(GTK_CONTAINER(ifopts_edit_dlg), main_vb);
273         gtk_widget_show(main_vb);
274
275         /* create current options frame */
276         cur_opts_fr = gtk_frame_new("Interfaces");
277         gtk_container_add(GTK_CONTAINER(main_vb), cur_opts_fr);
278         gtk_widget_show(cur_opts_fr);
279
280         /* create a scrolled window to pack the current options CList widget into */
281         cur_scr_win = scrolled_window_new(NULL, NULL);
282         gtk_container_border_width(GTK_CONTAINER(cur_scr_win), 3);
283         gtk_container_add(GTK_CONTAINER(cur_opts_fr), cur_scr_win);
284         gtk_widget_show(cur_scr_win);
285
286         /*
287          * Create current options CList.
288          */
289         cur_clist = gtk_clist_new_with_titles(IFOPTS_CLIST_COLS, (gchar **) cur_titles);
290         gtk_clist_set_column_width(GTK_CLIST(cur_clist), 1, 230);
291         gtk_clist_set_column_width(GTK_CLIST(cur_clist), 2, 260);
292         gtk_clist_set_column_width(GTK_CLIST(cur_clist), 3, 40);
293         gtk_clist_column_titles_passive(GTK_CLIST(cur_clist));
294         gtk_container_add(GTK_CONTAINER(cur_scr_win), cur_clist);
295         SIGNAL_CONNECT(cur_clist, "select_row", ifopts_edit_ifsel_cb, NULL);
296         gtk_widget_show(cur_clist);
297
298         /* add interface names to cell */
299         ifopts_if_clist_add();
300     gtk_clist_columns_autosize(GTK_CLIST(cur_clist));
301
302         /* initialize variable that saves currently selected row in "if_clist" */
303         ifrow = IFOPTS_IF_NOSEL;
304
305         /* create edit options frame */
306         ed_opts_fr = gtk_frame_new("Properties");
307         gtk_box_pack_start(GTK_BOX(main_vb), ed_opts_fr, FALSE, FALSE, 0);
308         gtk_widget_show(ed_opts_fr);
309
310         main_hb = gtk_hbox_new(TRUE, 5);
311         gtk_container_border_width(GTK_CONTAINER(main_hb), 3);
312         gtk_container_add(GTK_CONTAINER(ed_opts_fr), main_hb);
313         gtk_widget_show(main_hb);
314
315         /* table to hold description text entry and hide button */
316         main_tb = gtk_table_new(IFOPTS_TABLE_ROWS, 4, FALSE);
317         gtk_box_pack_start(GTK_BOX(main_hb), main_tb, TRUE, FALSE, 10);
318         gtk_table_set_row_spacings(GTK_TABLE(main_tb), 10);
319         gtk_table_set_col_spacings(GTK_TABLE(main_tb), 10);
320         gtk_widget_show(main_tb);
321
322         if_dev_lb = gtk_label_new("Device:");
323         gtk_table_attach_defaults(GTK_TABLE(main_tb), if_dev_lb, 0, 1, row, row+1);
324         gtk_misc_set_alignment(GTK_MISC(if_dev_lb), 1.0, 0.5);
325         gtk_widget_show(if_dev_lb);
326
327         if_dev_lb = gtk_label_new("");
328         gtk_table_attach_defaults(GTK_TABLE(main_tb), if_dev_lb, 1, 2, row, row+1);
329         gtk_misc_set_alignment(GTK_MISC(if_dev_lb), 0.0, 0.5);
330         gtk_widget_show(if_dev_lb);
331     row++;
332
333         if_name_lb = gtk_label_new("Description:");
334         gtk_table_attach_defaults(GTK_TABLE(main_tb), if_name_lb, 0, 1, row, row+1);
335         gtk_misc_set_alignment(GTK_MISC(if_name_lb), 1.0, 0.5);
336         gtk_widget_show(if_name_lb);
337
338         if_name_lb = gtk_label_new("");
339         gtk_table_attach_defaults(GTK_TABLE(main_tb), if_name_lb, 1, 2, row, row+1);
340         gtk_misc_set_alignment(GTK_MISC(if_name_lb), 0.0, 0.5);
341         gtk_widget_show(if_name_lb);
342     row++;
343
344         /* create interface description label and text entry */
345         if_descr_lb = gtk_label_new("Comment:");
346         gtk_table_attach_defaults(GTK_TABLE(main_tb), if_descr_lb, 0, 1, row, row+1);
347         gtk_misc_set_alignment(GTK_MISC(if_descr_lb), 1.0, 0.5);
348         gtk_widget_show(if_descr_lb);
349
350         if_descr_te = gtk_entry_new();
351         SIGNAL_CONNECT(if_descr_te, "changed", ifopts_edit_descr_changed_cb,
352                         cur_clist);
353         gtk_entry_set_max_length(GTK_ENTRY(if_descr_te), IFOPTS_MAX_DESCR_LEN);
354         gtk_table_attach_defaults(GTK_TABLE(main_tb), if_descr_te, 1, 2, row, row+1);
355         gtk_widget_show(if_descr_te);
356     row++;
357
358         /* create hide interface label and button */
359         if_hide_lb = gtk_label_new("Hide interface?:");
360         gtk_table_attach_defaults(GTK_TABLE(main_tb), if_hide_lb, 0, 1, row, row+1);
361         gtk_misc_set_alignment(GTK_MISC(if_hide_lb), 1.0, 0.5);
362         gtk_widget_show(if_hide_lb);
363
364         if_hide_cb = gtk_check_button_new();
365         SIGNAL_CONNECT(if_hide_cb, "toggled", ifopts_edit_hide_changed_cb,
366                         cur_clist);
367         gtk_table_attach_defaults(GTK_TABLE(main_tb), if_hide_cb, 1, 2, row, row+1);
368         gtk_widget_show(if_hide_cb);
369     row++;
370
371         /* button row: OK and Cancel buttons */
372         bbox = dlg_button_row_new(GTK_STOCK_OK, GTK_STOCK_CANCEL, NULL);
373         gtk_box_pack_start(GTK_BOX(main_vb), bbox, FALSE, FALSE, 0);
374         gtk_widget_show(bbox);
375
376         ok_bt = OBJECT_GET_DATA(bbox, GTK_STOCK_OK);
377         SIGNAL_CONNECT(ok_bt, "clicked", ifopts_edit_ok_cb, ifopts_edit_dlg);
378
379         cancel_bt = OBJECT_GET_DATA(bbox, GTK_STOCK_CANCEL);
380     window_set_cancel_button(ifopts_edit_dlg, cancel_bt, window_cancel_button_cb);
381
382         gtk_widget_grab_default(ok_bt);
383
384     SIGNAL_CONNECT(ifopts_edit_dlg, "delete_event", window_delete_event_cb,
385                  NULL);
386         /* Call a handler when we're destroyed, so we can inform
387            our caller, if any, that we've been destroyed. */
388         SIGNAL_CONNECT(ifopts_edit_dlg, "destroy", ifopts_edit_destroy_cb, NULL);
389
390         /* Set the key for the new dialog to point to our caller. */
391         OBJECT_SET_DATA(ifopts_edit_dlg, IFOPTS_CALLER_PTR_KEY, caller);
392         /* Set the key for the caller to point to us */
393         OBJECT_SET_DATA(caller, IFOPTS_DIALOG_PTR_KEY, ifopts_edit_dlg);
394
395     /* select the first row in if list, all option fields must exist for this */
396         gtk_clist_select_row(GTK_CLIST(cur_clist), 0, -1);
397
398         gtk_widget_show(ifopts_edit_dlg);
399     window_present(ifopts_edit_dlg);
400 }
401
402 /*
403  * User selected "OK". Create/write preferences strings.
404  */
405 static void
406 ifopts_edit_ok_cb(GtkWidget *w _U_, gpointer parent_w)
407 {
408         if (ifrow != IFOPTS_IF_NOSEL) {
409                 /* create/write new interfaces description string */
410                 ifopts_write_new_descr();
411
412                 /* create/write new "hidden" interfaces string */
413                 ifopts_write_new_hide();
414         }
415
416         /* Now nuke this window. */
417         gtk_grab_remove(GTK_WIDGET(parent_w));
418         window_destroy(GTK_WIDGET(parent_w));
419 }
420
421 static void
422 ifopts_edit_destroy_cb(GtkWidget *win, gpointer data _U_)
423 {
424         GtkWidget *caller;
425
426         /* Get the widget that requested that we be popped up, if any.
427            (It should arrange to destroy us if it's destroyed, so
428            that we don't get a pointer to a non-existent window here.) */
429         caller = OBJECT_GET_DATA(win, IFOPTS_CALLER_PTR_KEY);
430
431         if (caller != NULL) {
432                 /* Tell it we no longer exist. */
433                 OBJECT_SET_DATA(caller, IFOPTS_DIALOG_PTR_KEY, NULL);
434         }
435 }
436
437 /*
438  * Interface selected callback; update displayed widgets.
439  */
440 static void
441 ifopts_edit_ifsel_cb(GtkWidget          *clist _U_,
442                                          gint                   row,
443                                          gint                   column _U_,
444                                          GdkEventButton *event _U_,
445                                          gpointer               data _U_)
446 {
447         gchar *text;
448
449         /* save currently selected row */
450         ifrow = row;
451
452         /* get/display the interface device from current CList */
453         gtk_clist_get_text(GTK_CLIST(cur_clist), row, 0, &text);
454     /* is needed, as gtk_entry_set_text() will change text again (bug in GTK?) */
455     text = strdup(text);
456         gtk_label_set_text(GTK_LABEL(if_dev_lb), text);
457     g_free(text);
458
459         /* get/display the interface name from current CList */
460         gtk_clist_get_text(GTK_CLIST(cur_clist), row, 1, &text);
461     /* is needed, as gtk_entry_set_text() will change text again (bug in GTK?) */
462     text = strdup(text);
463         gtk_label_set_text(GTK_LABEL(if_name_lb), text);
464     g_free(text);
465
466         /* get/display the interface description from current CList */
467         gtk_clist_get_text(GTK_CLIST(cur_clist), row, 2, &text);
468     /* is needed, as gtk_entry_set_text() will change text again (bug in GTK?) */
469     text = strdup(text);
470         gtk_entry_set_text(GTK_ENTRY(if_descr_te), text);
471     g_free(text);
472
473         /* get/display the "hidden" button state from current CList */
474         gtk_clist_get_text(GTK_CLIST(cur_clist), row, 3, &text);
475         if (strcmp("Yes", text) == 0)
476                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(if_hide_cb), TRUE);
477         else
478                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(if_hide_cb), FALSE);
479 }
480
481 /*
482  * Comment text entry changed callback; update current CList.
483  */
484 static void
485 ifopts_edit_descr_changed_cb(GtkEditable *ed, gpointer udata)
486 {
487         gchar *text;
488
489         if (ifrow == IFOPTS_IF_NOSEL)
490                 return;
491
492         /* get current description text and set value in current CList */
493         text = gtk_editable_get_chars(GTK_EDITABLE(ed), 0, -1);
494         /* replace any reserved formatting characters "()," with spaces */
495         g_strdelimit(text, "(),", ' ');
496         gtk_clist_set_text(GTK_CLIST(udata), ifrow, 2, text);
497         g_free(text);
498 }
499
500 /*
501  * Hide toggle button changed callback; update current CList.
502  */
503 static void
504 ifopts_edit_hide_changed_cb(GtkToggleButton *tbt, gpointer udata)
505 {
506         if (ifrow == IFOPTS_IF_NOSEL)
507                 return;
508
509         /* get "hidden" button state and set text in current CList */
510         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(tbt)) == TRUE)
511                 gtk_clist_set_text(GTK_CLIST(udata), ifrow, 3, "Yes");
512         else
513                 gtk_clist_set_text(GTK_CLIST(udata), ifrow, 3, "No");
514 }
515
516 /*
517  * Add any saved options that apply to cells in current CList.
518  *
519  * NOTE:
520  *              Interfaces that have been removed from the machine or disabled and
521  *              no longer apply are ignored. Therefore, if the user subsequently
522  *              selects "OK", the options for these interfaces are lost (they're
523  *              lost permanently if "Save" is selected).
524  */
525 static void
526 ifopts_options_add(GtkCList *clist, if_info_t *if_info)
527 {
528         gint    row;
529         gchar   *p;
530         gchar   *ifnm;
531         gchar   *desc;
532         gchar   *pr_descr;
533         gchar   *text[] = { NULL, NULL, NULL, NULL };
534
535         /* add interface descriptions and "hidden" flag */
536         if (prefs.capture_devices_descr != NULL) {
537                 /* create working copy of device descriptions */
538                 pr_descr = g_strdup(prefs.capture_devices_descr);
539
540                 /* if we find a description for this interface */
541                 if ((ifnm = strstr(pr_descr, if_info->name)) != NULL) {
542                         p = ifnm;
543                         while (*p != '\0') {
544                                 /* found left parenthesis, start of description */
545                                 if (*p == '(') {
546                                         /* set device name text */
547                                         text[0] = g_strdup(if_info->name);
548                                         /* set OS description + device name text */
549                                         if (if_info->description != NULL)
550                                                 text[1] = g_strdup(if_info->description);
551                                         else
552                                                 text[1] = g_strdup("");
553                                         /* check if interface is "hidden" */
554                                         if (prefs.capture_devices_hide != NULL) {
555                                                 if (strstr(prefs.capture_devices_hide, if_info->name) != NULL)
556                                                         text[3] = g_strdup("Yes");
557                                                 else
558                                                         text[3] = g_strdup("No");
559                                         }
560                                         else
561                                                 text[3] = g_strdup("No");
562                                         p++;
563                                         /* if syntax error */
564                                         if ((*p == '\0') || (*p == ',') || (*p == '(') || (*p == ')')) {
565                                                 ifopts_options_free(text);
566                                                 break;
567                                         }
568                                         /* save pointer to beginning of description */
569                                         desc = p;
570                                         p++;
571                                         /* if syntax error */
572                                         if ((*p == '\0') || (*p == ',') || (*p == '(') || (*p == ')')) {
573                                                 ifopts_options_free(text);
574                                                 break;
575                                         }
576                                         /* skip to end of description */
577                                         while (*p != '\0') {
578                                                 /* end of description */
579                                                 if (*p == ')') {
580                                                         /* terminate and set description text */
581                                                         *p = '\0';
582                                                         text[2] = g_strdup(desc);
583                                                         /* add row to CList */
584                                                         row = gtk_clist_append(GTK_CLIST(clist), text);
585                                                         gtk_clist_set_selectable(GTK_CLIST(clist), row,
586                                                                         TRUE);
587                                                         ifopts_options_free(text);
588                                                         break;
589                                                 }
590                                                 p++;
591                                         }
592                                         /* get out */
593                                         break;
594                                 }
595                                 else
596                                         p++;
597                         }
598                 }
599                 /* if there's no description for this interface */
600                 else {
601                         /* set device name text */
602                         text[0] = g_strdup(if_info->name);
603                         /* set OS description + device name text */
604                         if (if_info->description != NULL)
605                                 text[1] = g_strdup(if_info->description);
606                         else
607                                 text[1] = g_strdup("");
608                         /* set empty description */
609                         text[2] = g_strdup("");
610                         /* check if interface is "hidden" */
611                         if (prefs.capture_devices_hide != NULL) {
612                                 if (strstr(prefs.capture_devices_hide, if_info->name) != NULL)
613                                         text[3] = g_strdup("Yes");
614                                 else
615                                         text[3] = g_strdup("No");
616                         }
617                         else
618                                 text[3] = g_strdup("No");
619
620                         /* add row to CList */
621                         row = gtk_clist_append(GTK_CLIST(clist), text);
622                         gtk_clist_set_selectable(GTK_CLIST(clist), row, TRUE);
623                         ifopts_options_free(text);
624                 }
625
626                 g_free(pr_descr);
627         }
628         /*
629          * If we do not have any descriptions, but have "hidden" interfaces.
630          */
631         else if (prefs.capture_devices_hide != NULL) {
632                 /* set device name text */
633                 text[0] = g_strdup(if_info->name);
634                 /* set OS description + device name text */
635                 if (if_info->description != NULL)
636                         text[1] = g_strdup(if_info->description);
637                 else
638                         text[1] = g_strdup("");
639                 /* set empty description */
640                 text[2] = g_strdup("");
641                 /* check if interface is "hidden" */
642                 if (strstr(prefs.capture_devices_hide, if_info->name) != NULL)
643                         text[3] = g_strdup("Yes");
644                 else
645                         text[3] = g_strdup("No");
646
647                 /* add row to CList */
648                 row = gtk_clist_append(GTK_CLIST(clist), text);
649                 gtk_clist_set_selectable(GTK_CLIST(clist), row, TRUE);
650                 ifopts_options_free(text);
651         }
652         /*
653          * If we have no descriptions and no "hidden" interfaces.
654          */
655         else {
656                 /* set device name text */
657                 text[0] = g_strdup(if_info->name);
658                 /* set OS description + device name text */
659                 if (if_info->description != NULL)
660                         text[1] = g_strdup(if_info->description);
661                 else
662                         text[1] = g_strdup("");
663                 /* set empty description */
664                 text[2] = g_strdup("");
665                 /* interface is not "hidden" */
666                 text[3] = g_strdup("No");
667
668                 /* add row to CList */
669                 row = gtk_clist_append(GTK_CLIST(clist), text);
670                 gtk_clist_set_selectable(GTK_CLIST(clist), row, TRUE);
671                 ifopts_options_free(text);
672         }
673 }
674
675 static void
676 ifopts_options_free(gchar *text[])
677 {
678         gint i;
679
680         for (i=0; i < IFOPTS_CLIST_COLS; i++) {
681                 if (text[i] != NULL) {
682                         g_free(text[i]);
683                         text[i] = NULL;
684                 }
685         }
686 }
687
688 /*
689  * Add all interfaces to interfaces CList.
690  */
691 static void
692 ifopts_if_clist_add(void)
693 {
694         GList           *if_list;
695         int             err;
696         gchar           *err_str;
697         if_info_t       *if_info;
698         guint           i;
699         guint           nitems;
700
701         if_list = capture_interface_list(&err, &err_str);
702         if (if_list == NULL && err == CANT_GET_INTERFACE_LIST) {
703                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s", err_str);
704                 g_free(err_str);
705                 return;
706         }
707
708         /* Seems we need to be at list head for g_list_length()? */
709         if_list = g_list_first(if_list);
710         nitems = g_list_length(if_list);
711
712         /* add OS description + interface name text to CList */
713         for (i=0; i < nitems; i++) {
714                 if_info = g_list_nth_data(if_list, i);
715                 /* should never happen, but just in case */
716                 if (if_info == NULL)
717                         continue;
718
719                 /* fill current options CList with current preference values */
720                 ifopts_options_add(GTK_CLIST(cur_clist), if_info);
721         }
722
723         free_interface_list(if_list);
724 }
725
726 /*
727  * Create/write new interfaces description string based on current CList.
728  * Put it into the preferences value.
729  */
730 static void
731 ifopts_write_new_descr(void)
732 {
733         gint    i;
734         gboolean        first_if = TRUE;                                /* flag to check if first in list */
735         gchar   *ifnm;
736         gchar   *desc;
737         gchar   *tmp_descr;
738         gchar   *new_descr;
739
740         /* new preferences interfaces description string */
741         new_descr = g_malloc0(MAX_VAL_LEN);
742
743         /* get description for each row (interface) */
744         for (i = 0; ;i++) {
745                 /* get description */
746                 if (gtk_clist_get_text(GTK_CLIST(cur_clist), i, 2, &desc) != 1)
747                         break;
748                 /* if no description, skip this interface */
749                 if (strlen(desc) == 0)
750                         continue;
751
752                 /* get interface name */
753                 gtk_clist_get_text(GTK_CLIST(cur_clist), i, 0, &ifnm);
754
755                 /*
756                  * create/cat interface description to new string
757                  * (leave space for parens, comma and terminator)
758                  */
759                 if (first_if != TRUE) {
760                         g_strlcat (new_descr, ",", MAX_VAL_LEN);
761                 }
762
763                 tmp_descr = g_strdup_printf("%s(%s)", ifnm, desc);
764                 g_strlcat(new_descr, tmp_descr, MAX_VAL_LEN);
765                 g_free(tmp_descr);
766
767                 /* set first-in-list flag to false */
768                 first_if = FALSE;
769         }
770
771         /* write new description string to preferences */
772         if (strlen(new_descr) > 0) {
773                 g_free(prefs.capture_devices_descr);
774                 prefs.capture_devices_descr = new_descr;
775         }
776         /* no descriptions */
777         else {
778                 g_free(prefs.capture_devices_descr);
779                 g_free(new_descr);
780                 prefs.capture_devices_descr = NULL;
781         }
782 }
783
784 /*
785  * Create/write new "hidden" interfaces string based on current CList.
786  * Put it into the preferences value.
787  */
788 static void
789 ifopts_write_new_hide(void)
790 {
791         gint    i;
792         gint    first_if = TRUE;                                /* flag to check if first in list */
793         gchar   *ifnm;
794         gchar   *hide;
795         gchar   *new_hide;
796
797         /* new preferences "hidden" interfaces string */
798         new_hide = g_malloc0(MAX_VAL_LEN);
799
800         /* get "hidden" flag text for each row (interface) */
801         for (i = 0; ;i++) {
802                 /* get flag */
803                 if (gtk_clist_get_text(GTK_CLIST(cur_clist), i, 3, &hide) != 1)
804                         break;
805                 /* if flag text is "No", skip this interface */
806                 if (strcmp("No", hide) == 0)
807                         continue;
808
809                 /* get interface name */
810                 gtk_clist_get_text(GTK_CLIST(cur_clist), i, 0, &ifnm);
811
812                 /*
813                  * create/cat interface to new string
814                  */
815                 if (first_if != TRUE)
816                         g_strlcat (new_hide, ",", MAX_VAL_LEN);
817                 g_strlcat (new_hide, ifnm, MAX_VAL_LEN);
818
819                 /* set first-in-list flag to false */
820                 first_if = FALSE;
821         }
822
823         /* write new "hidden" string to preferences */
824         if (strlen(new_hide) > 0) {
825                 g_free(prefs.capture_devices_hide);
826                 prefs.capture_devices_hide = new_hide;
827         }
828         /* no "hidden" interfaces */
829         else {
830                 g_free(prefs.capture_devices_hide);
831                 g_free(new_hide);
832                 prefs.capture_devices_hide = NULL;
833         }
834 }
835
836 #endif /* HAVE_LIBPCAP */