Move gtk to ui/gtk.
[metze/wireshark/wip.git] / ui / gtk / main_airpcap_toolbar.c
1 /* main_airpcap_toolbar.c
2  * The airpcap toolbar
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 /*
26  * This file implements the wireless toolbar for Wireshark.
27  */
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #ifdef HAVE_AIRPCAP
34
35 #include <gtk/gtk.h>
36 #include "ui/gtk/old-gtk-compat.h"
37
38 #include <epan/epan.h>
39 #include <epan/frequency-utils.h>
40
41 #include "simple_dialog.h"
42 #include "main.h"
43 #include "main_airpcap_toolbar.h"
44
45 #include "recent.h"
46 #include "keys.h"
47
48 #include <airpcap.h>
49 #include "airpcap_loader.h"
50 #include "airpcap_dlg.h"
51 #include "airpcap_gui_utils.h"
52
53 #include <epan/crypt/airpdcap_ws.h>
54
55
56 gboolean block_toolbar_signals = FALSE;
57 static GtkWidget *driver_warning_dialog;
58
59
60 /*
61  * Callback for the wrong crc combo
62  */
63 static void
64 airpcap_toolbar_fcs_filter_combo_cb(GtkWidget *fcs_filter_cb, gpointer user_data _U_)
65 {
66     PAirpcapHandle ad;
67     gchar ebuf[AIRPCAP_ERRBUF_SIZE];
68     gchar *fcs_filter_str;
69
70     if (fcs_filter_cb != NULL && !block_toolbar_signals && (airpcap_if_active != NULL)) {
71         fcs_filter_str = gtk_combo_box_text_get_active_text (GTK_COMBO_BOX_TEXT(fcs_filter_cb));
72         ad = airpcap_if_open(airpcap_if_active->name, ebuf);
73
74         if (fcs_filter_str && (g_ascii_strcasecmp("", fcs_filter_str)) && ad) {
75             airpcap_if_selected->CrcValidationOn = airpcap_get_validation_type(fcs_filter_str);
76             airpcap_if_selected->saved = FALSE;
77             airpcap_if_set_fcs_validation(ad,airpcap_if_active->CrcValidationOn);
78             /* Save configuration */
79             airpcap_if_store_cur_config_as_adapter_default(ad);
80             airpcap_if_close(ad);
81         }
82         g_free(fcs_filter_str);
83     }
84 }
85
86 void
87 airpcap_toolbar_encryption_cb(GtkWidget *entry _U_, gpointer user_data _U_)
88 {
89   /* We need to directly access the .dll functions here... */
90   gchar ebuf[AIRPCAP_ERRBUF_SIZE];
91   PAirpcapHandle ad;
92
93   gint n = 0;
94   gint i = 0;
95   airpcap_if_info_t* curr_if = NULL;
96
97   /* Apply changes to the current adapter */
98   if( (airpcap_if_active != NULL)) {
99     ad = airpcap_if_open(airpcap_if_active->name, ebuf);
100
101     if(ad) {
102       if(airpcap_if_active->DecryptionOn == AIRPCAP_DECRYPTION_ON) {
103         airpcap_if_active->DecryptionOn = AIRPCAP_DECRYPTION_OFF;
104         airpcap_if_set_decryption_state(ad,airpcap_if_active->DecryptionOn);
105         /* Save configuration */
106         if(!airpcap_if_store_cur_config_as_adapter_default(ad)) {
107           simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "Cannot save configuration!!!\nRemember that in order to store the configuration in the registry you have to:\n\n- Close all the airpcap-based applications.\n- Be sure to have administrative privileges.");
108         }
109         airpcap_if_close(ad);
110       } else {
111         airpcap_if_active->DecryptionOn = AIRPCAP_DECRYPTION_ON;
112         airpcap_if_set_decryption_state(ad,airpcap_if_active->DecryptionOn);
113         /* Save configuration */
114         if(!airpcap_if_store_cur_config_as_adapter_default(ad)) {
115           simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "Cannot save configuration!!!\nRemember that in order to store the configuration in the registry you have to:\n\n- Close all the airpcap-based applications.\n- Be sure to have administrative privileges.");
116         }
117         airpcap_if_close(ad);
118       }
119     }
120   } else {
121     simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "No active AirPcap Adapter selected!");
122     return;
123   }
124
125   if (!(airpcap_if_list == NULL)) {
126     n = g_list_length(airpcap_if_list);
127
128     /* The same kind of settings should be propagated to all the adapters */
129     /* Apply this change to all the adapters !!! */
130     for(i = 0; i < n; i++) {
131       curr_if = (airpcap_if_info_t*)g_list_nth_data(airpcap_if_list,i);
132
133       if( (curr_if != NULL) && (curr_if != airpcap_if_selected) ) {
134         ad = airpcap_if_open(curr_if->name, ebuf);
135         if(ad) {
136           curr_if->DecryptionOn = airpcap_if_selected->DecryptionOn;
137           airpcap_if_set_decryption_state(ad,curr_if->DecryptionOn);
138           /* Save configuration for the curr_if */
139           if(!airpcap_if_store_cur_config_as_adapter_default(ad))       {
140             simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "Cannot save configuration!!!\nRemember that in order to store the configuration in the registry you have to:\n\n- Close all the airpcap-based applications.\n- Be sure to have administrative privileges.");
141           }
142           airpcap_if_close(ad);
143         }
144       }
145     }
146   } else {
147     simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "AirPcap Adapter Error!");
148     return;
149   }
150 }
151
152 /*
153  * Callback for the Advanced Wireless Settings button
154  */
155 static void
156 toolbar_display_airpcap_advanced_cb(GtkWidget *w, gpointer data)
157 {
158     int *from_widget;
159
160     from_widget = (gint*)g_malloc(sizeof(gint));
161     *from_widget = AIRPCAP_ADVANCED_FROM_TOOLBAR;
162     g_object_set_data(G_OBJECT(airpcap_tb),AIRPCAP_ADVANCED_FROM_KEY,from_widget);
163
164     display_airpcap_advanced_cb(w,data);
165 }
166
167 /*
168  * Callback for the Decryption Key Management button
169  */
170 static void
171 toolbar_display_airpcap_key_management_cb(GtkWidget *w, gpointer data)
172 {
173     int *from_widget;
174
175     from_widget = (gint*)g_malloc(sizeof(gint));
176     *from_widget = AIRPCAP_ADVANCED_FROM_TOOLBAR;
177     g_object_set_data(G_OBJECT(airpcap_tb), AIRPCAP_ADVANCED_FROM_KEY, from_widget);
178
179     display_airpcap_key_management_cb(w,data);
180 }
181
182 GtkWidget *airpcap_toolbar_new(void)
183 {
184     GtkWidget     *channel_lb = NULL,
185                   *channel_cb = NULL,
186                   *channel_offset_lb = NULL,
187                   *channel_offset_cb = NULL,
188                   *fcs_filter_lb = NULL,
189                   *fcs_filter_cb = NULL;
190     GtkWidget     *airpcap_tb;
191
192     GtkWidget     *decryption_mode_lb;
193     GtkWidget     *decryption_mode_cb;
194
195     GtkToolItem   *key_management_bt = NULL,
196                   *advanced_bt = NULL,
197                   *tool_item;
198
199     /* airpcap toolbar */
200     airpcap_tb = gtk_toolbar_new();
201     gtk_orientable_set_orientation(GTK_ORIENTABLE(airpcap_tb),
202                                 GTK_ORIENTATION_HORIZONTAL);
203
204     /* Create the "802.11 Channel:" label */
205     channel_lb = gtk_label_new("802.11 Channel: ");
206     g_object_set_data(G_OBJECT(airpcap_tb), AIRPCAP_TOOLBAR_CHANNEL_LABEL_KEY, channel_lb);
207     gtk_widget_show(channel_lb);
208
209     gtk_widget_set_size_request(channel_lb, 85, -1);
210
211     tool_item = gtk_tool_item_new ();
212     gtk_container_add (GTK_CONTAINER (tool_item), channel_lb);
213     gtk_widget_show (GTK_WIDGET (tool_item));
214     gtk_toolbar_insert(GTK_TOOLBAR(airpcap_tb), tool_item, -1);
215
216         gtk_widget_set_tooltip_text(GTK_WIDGET(tool_item), "Current 802.11 Channel");
217
218     /* Create the channel combo box */
219     channel_cb = gtk_combo_box_text_new();
220     g_object_set_data(G_OBJECT(airpcap_tb), AIRPCAP_TOOLBAR_CHANNEL_KEY, channel_cb);
221
222     /* Select the current channel */
223     airpcap_update_channel_combo(GTK_WIDGET(channel_cb), airpcap_if_selected);
224
225     gtk_widget_set_size_request(channel_cb, 120, -1);
226
227     gtk_widget_show(channel_cb);
228
229     tool_item = gtk_tool_item_new ();
230     gtk_container_add (GTK_CONTAINER (tool_item), channel_cb);
231     gtk_widget_show (GTK_WIDGET (tool_item));
232     gtk_toolbar_insert(GTK_TOOLBAR(airpcap_tb), tool_item, -1);
233
234         gtk_widget_set_tooltip_text(GTK_WIDGET(tool_item), "802.11 Channel");
235
236     /* Create the "Channel Offset:" label */
237     channel_offset_lb = gtk_label_new("Channel Offset: ");
238     g_object_set_data(G_OBJECT(airpcap_tb), AIRPCAP_TOOLBAR_CHANNEL_OFFSET_LABEL_KEY, channel_offset_lb);
239     gtk_widget_show(channel_offset_lb);
240
241     gtk_widget_set_size_request(channel_offset_lb, 90, -1);
242     tool_item = gtk_tool_item_new ();
243     gtk_container_add (GTK_CONTAINER (tool_item), channel_offset_lb);
244     gtk_widget_show (GTK_WIDGET (tool_item));
245     gtk_toolbar_insert(GTK_TOOLBAR(airpcap_tb), tool_item, -1);
246
247         gtk_widget_set_tooltip_text(GTK_WIDGET(tool_item), "Current 802.11 Channel Offset");
248
249     /* Start: Channel offset combo box */
250     channel_offset_cb = gtk_combo_box_text_new();
251     g_object_set_data(G_OBJECT(airpcap_tb), AIRPCAP_TOOLBAR_CHANNEL_OFFSET_KEY, channel_offset_cb);
252
253     if(airpcap_if_active != NULL){
254         airpcap_update_channel_offset_combo(airpcap_if_active, airpcap_if_active->channelInfo.Frequency, channel_offset_cb, FALSE);
255     } else {
256         gtk_combo_box_set_active(GTK_COMBO_BOX(channel_offset_cb), -1);
257     }
258
259         gtk_widget_set_tooltip_text(channel_offset_cb, "Current 802.11 Channel Offset");
260
261     gtk_widget_set_size_request(channel_offset_cb, 50, -1);
262
263     gtk_widget_show(channel_offset_cb);
264
265     tool_item = gtk_tool_item_new ();
266     gtk_container_add (GTK_CONTAINER (tool_item), channel_offset_cb);
267     gtk_widget_show (GTK_WIDGET (tool_item));
268     gtk_toolbar_insert(GTK_TOOLBAR(airpcap_tb), tool_item, -1);
269
270     /* callback for channel combo box */
271     g_signal_connect(channel_cb,"changed", G_CALLBACK(airpcap_channel_changed_set_cb), channel_offset_cb);
272     /* callback for channel offset combo box */
273     g_signal_connect(channel_offset_cb, "changed", G_CALLBACK(airpcap_channel_offset_changed_cb), NULL);
274     /* End: Channel offset combo box */
275
276     /* Wrong CRC Label */
277     fcs_filter_lb = gtk_label_new(" FCS Filter: ");
278     g_object_set_data(G_OBJECT(airpcap_tb), AIRPCAP_TOOLBAR_FCS_FILTER_LABEL_KEY, fcs_filter_lb);
279     gtk_widget_show(fcs_filter_lb);
280     tool_item = gtk_tool_item_new ();
281     gtk_container_add (GTK_CONTAINER (tool_item), fcs_filter_lb);
282     gtk_widget_show (GTK_WIDGET (tool_item));
283     gtk_toolbar_insert(GTK_TOOLBAR(airpcap_tb), tool_item, -1);
284
285     /* FCS filter combo box */
286     fcs_filter_cb = gtk_combo_box_text_new();
287     g_object_set_data(G_OBJECT(airpcap_tb), AIRPCAP_TOOLBAR_FCS_FILTER_KEY, fcs_filter_cb);
288
289     gtk_widget_set_size_request(fcs_filter_cb, 100, -1);
290
291      gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT(fcs_filter_cb), airpcap_get_validation_name(AIRPCAP_VT_ACCEPT_EVERYTHING));
292      gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT(fcs_filter_cb), airpcap_get_validation_name(AIRPCAP_VT_ACCEPT_CORRECT_FRAMES));
293      gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT(fcs_filter_cb), airpcap_get_validation_name(AIRPCAP_VT_ACCEPT_CORRUPT_FRAMES));
294     gtk_combo_box_set_active(GTK_COMBO_BOX(fcs_filter_cb), 0);
295
296         gtk_widget_set_tooltip_text(fcs_filter_cb, "Select the 802.11 FCS filter that the wireless adapter will apply.");
297
298     if (airpcap_if_selected != NULL) {
299         airpcap_validation_type_combo_set_by_type(fcs_filter_cb, airpcap_if_selected->CrcValidationOn);
300     }
301
302     g_signal_connect (fcs_filter_cb, "changed", G_CALLBACK(airpcap_toolbar_fcs_filter_combo_cb), NULL);
303     gtk_widget_show(fcs_filter_cb);
304
305     tool_item = gtk_tool_item_new ();
306     gtk_container_add (GTK_CONTAINER (tool_item), fcs_filter_cb);
307     gtk_widget_show (GTK_WIDGET (tool_item));
308     gtk_toolbar_insert(GTK_TOOLBAR(airpcap_tb), tool_item, -1);
309
310     /* Decryption mode combo box */
311     decryption_mode_lb = gtk_label_new ("Decryption Mode: ");
312     g_object_set_data(G_OBJECT(airpcap_tb), AIRPCAP_TOOLBAR_DECRYPTION_LABEL_KEY, decryption_mode_lb);
313     gtk_widget_set_name (decryption_mode_lb, "decryption_mode_lb");
314     gtk_widget_show (decryption_mode_lb);
315
316     decryption_mode_cb = gtk_combo_box_text_new();
317     gtk_widget_set_name (decryption_mode_cb, "decryption_mode_cb");
318     gtk_widget_show (decryption_mode_cb);
319     gtk_widget_set_size_request(decryption_mode_cb, 83, -1);
320     update_decryption_mode_list(decryption_mode_cb);
321
322     tool_item = gtk_tool_item_new ();
323     gtk_container_add (GTK_CONTAINER (tool_item), decryption_mode_cb);
324     gtk_widget_show (GTK_WIDGET (tool_item));
325     gtk_toolbar_insert(GTK_TOOLBAR(airpcap_tb), tool_item, -1);
326
327         gtk_widget_set_tooltip_text(fcs_filter_lb, "Choose a Decryption Mode");
328     /* Set current decryption mode!!!! */
329     update_decryption_mode(decryption_mode_cb);
330     g_signal_connect(decryption_mode_cb, "changed", G_CALLBACK(on_decryption_mode_cb_changed), NULL);
331     g_object_set_data(G_OBJECT(airpcap_tb), AIRPCAP_TOOLBAR_DECRYPTION_KEY, decryption_mode_cb);
332
333     /* Advanced button */
334     advanced_bt = gtk_tool_button_new(NULL, /* a widget that will be used as icon widget, or NULL */
335             "Wireless Settings...");
336     g_object_set_data(G_OBJECT(airpcap_tb), AIRPCAP_TOOLBAR_ADVANCED_KEY, advanced_bt);
337
338     g_signal_connect(advanced_bt, "clicked", G_CALLBACK(toolbar_display_airpcap_advanced_cb), airpcap_tb);
339     gtk_widget_show(GTK_WIDGET(advanced_bt));
340     gtk_toolbar_insert(GTK_TOOLBAR(airpcap_tb), advanced_bt, -1);
341
342         gtk_widget_set_tooltip_text(GTK_WIDGET(advanced_bt), "Set Advanced Wireless Settings");
343     /* Key Management button */
344     key_management_bt = gtk_tool_button_new(NULL, /* a widget that will be used as icon widget, or NULL */
345             "Decryption Keys...");
346
347     g_object_set_data(G_OBJECT(airpcap_tb), AIRPCAP_TOOLBAR_KEY_MANAGEMENT_KEY, key_management_bt);
348
349     g_signal_connect(key_management_bt, "clicked", G_CALLBACK(toolbar_display_airpcap_key_management_cb), airpcap_tb);
350     gtk_widget_show(GTK_WIDGET(key_management_bt));
351     gtk_toolbar_insert(GTK_TOOLBAR(airpcap_tb), key_management_bt, -1);
352         gtk_widget_set_tooltip_text(GTK_WIDGET(key_management_bt), "Manage Decryption Keys");
353
354     /* If no airpcap interface is present, gray everything */
355     if(airpcap_if_active == NULL) {
356         if(airpcap_if_list == NULL || g_list_length(airpcap_if_list) == 0) {
357             /* No airpcap device found */
358             airpcap_enable_toolbar_widgets(airpcap_tb, FALSE);
359             /* recent.airpcap_toolbar_show = TRUE; */
360         } else {
361             /* default adapter is not airpcap... or is airpcap but is not found*/
362             airpcap_set_toolbar_stop_capture(airpcap_if_active);
363             airpcap_enable_toolbar_widgets(airpcap_tb, FALSE);
364             /* recent.airpcap_toolbar_show = TRUE; */
365         }
366     } else {
367         airpcap_set_toolbar_stop_capture(airpcap_if_active);
368         /* recent.airpcap_toolbar_show = TRUE; */
369     }
370
371     return airpcap_tb;
372 }
373
374 static void
375 driver_warning_dialog_cb(gpointer dialog, gint btn _U_, gpointer data _U_)
376 {
377     gboolean r;
378
379     r = simple_dialog_check_get(dialog);
380     recent.airpcap_driver_check_show = !r;
381 }
382
383 void airpcap_toolbar_show(GtkWidget *airpcap_tb _U_)
384 {
385   /*
386    * This will read the decryption keys from the preferences file, and will
387    * store them into the registry...
388    */
389   if(airpcap_if_list != NULL && g_list_length(airpcap_if_list) > 0){
390     if (!airpcap_check_decryption_keys(airpcap_if_list)) {
391       /* Ask the user what to do ...*/
392       airpcap_keys_check_w(NULL,NULL);
393     } else {
394       /* Keys from lists are equals, or Wireshark has got no keys */
395       airpcap_load_decryption_keys(airpcap_if_list);
396     }
397   }
398
399   switch (airpcap_dll_ret_val) {
400
401   case AIRPCAP_DLL_OK:
402     break;
403
404   case AIRPCAP_DLL_OLD:
405     if(recent.airpcap_driver_check_show) {
406       driver_warning_dialog = simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s",
407                         "WARNING: The version of AirPcap on this system\n"
408                         "does not support driver-level decryption.  Please\n"
409                         "download a more recent version from\n" "http://www.cacetech.com/support/downloads.htm \n");
410       simple_dialog_check_set(driver_warning_dialog,"Don't show this message again.");
411       simple_dialog_set_cb(driver_warning_dialog, driver_warning_dialog_cb, NULL);
412     }
413     break;
414
415 #if 0
416   /*
417    * XXX - Maybe we need to warn the user if one of the following happens???
418    */
419   case AIRPCAP_DLL_ERROR:
420     simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s","AIRPCAP_DLL_ERROR\n");
421     break;
422
423   case AIRPCAP_DLL_NOT_FOUND:
424     simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s","AIRPCAP_DDL_NOT_FOUND\n");
425     break;
426 #endif
427   }
428 }
429
430 #endif /* HAVE_AIRPCAP */