From Shoichi Sakane via bug 5270: minor changes and remove an unused hf variable.
[obnox/wireshark/wip.git] / gtk / airpcap_gui_utils.c
1 /* airpcap_gui_utils.c
2  *
3  * $Id$
4  *
5  * Giorgio Tino <giorgio.tino@cacetech.com>
6  * Copyright (c) CACE Technologies, LLC 2006
7  *
8  * Wireshark - Network traffic analyzer
9  * By Gerald Combs <gerald@wireshark.org>
10  * Copyright 2000 Gerald Combs
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25  */
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #ifdef HAVE_AIRPCAP
32
33 #include <gtk/gtk.h>
34 #include <glib.h>
35
36 #include <stdio.h>
37 #include <string.h>
38
39 #include <epan/filesystem.h>
40 #include <epan/strutil.h>
41 #include <epan/frequency-utils.h>
42 #include <epan/crypt/airpdcap_ws.h>
43
44 #include "../simple_dialog.h"
45
46 #include "gtk/main.h"
47 #include "gtk/dlg_utils.h"
48 #include "gtk/gui_utils.h"
49 #include "gtk/dfilter_expr_dlg.h"
50 #include "gtk/help_dlg.h"
51 #include "gtk/keys.h"
52
53 #include <airpcap.h>
54 #include "airpcap_loader.h"
55 #include "airpcap_gui_utils.h"
56
57
58 /* Controls the releay of settings back to the adapter. */
59 gboolean change_airpcap_settings = FALSE;
60
61 /*
62  * Set up the airpcap toolbar for the new capture interface
63  */
64 void
65 airpcap_set_toolbar_start_capture(airpcap_if_info_t* if_info)
66 {
67     GtkWidget *airpcap_toolbar_label;
68     GtkWidget *toolbar_channel_cb;
69     GtkWidget *airpcap_toolbar_channel_lb;
70     GtkWidget *airpcap_toolbar_channel_offset;
71     GtkWidget *airpcap_toolbar_channel_offset_lb;
72     GtkWidget *airpcap_toolbar_button;
73     GtkWidget *airpcap_toolbar_fcs;
74     GtkWidget *airpcap_toolbar_fcs_lb;
75     GtkWidget *airpcap_toolbar_decryption;
76     GtkWidget *airpcap_toolbar_decryption_lb;
77     GtkWidget *airpcap_toolbar_keys_button;
78
79     gchar *if_label_text;
80
81     airpcap_toolbar_label    = g_object_get_data(G_OBJECT(airpcap_tb),AIRPCAP_TOOLBAR_INTERFACE_KEY);
82     toolbar_channel_cb  = g_object_get_data(G_OBJECT(airpcap_tb),AIRPCAP_TOOLBAR_CHANNEL_KEY);
83     airpcap_toolbar_channel_lb  = g_object_get_data(G_OBJECT(airpcap_tb),AIRPCAP_TOOLBAR_CHANNEL_LABEL_KEY);
84     airpcap_toolbar_channel_offset  = g_object_get_data(G_OBJECT(airpcap_tb),AIRPCAP_TOOLBAR_CHANNEL_OFFSET_KEY);
85     airpcap_toolbar_channel_offset_lb  = g_object_get_data(G_OBJECT(airpcap_tb),AIRPCAP_TOOLBAR_CHANNEL_OFFSET_LABEL_KEY);
86     airpcap_toolbar_fcs  = g_object_get_data(G_OBJECT(airpcap_tb),AIRPCAP_TOOLBAR_FCS_FILTER_KEY);
87     airpcap_toolbar_fcs_lb  = g_object_get_data(G_OBJECT(airpcap_tb),AIRPCAP_TOOLBAR_FCS_FILTER_LABEL_KEY);
88     airpcap_toolbar_button   = g_object_get_data(G_OBJECT(airpcap_tb),AIRPCAP_TOOLBAR_ADVANCED_KEY);
89     airpcap_toolbar_decryption = g_object_get_data(G_OBJECT(airpcap_tb),AIRPCAP_TOOLBAR_DECRYPTION_KEY);
90     airpcap_toolbar_decryption_lb = g_object_get_data(G_OBJECT(airpcap_tb),AIRPCAP_TOOLBAR_DECRYPTION_LABEL_KEY);
91     airpcap_toolbar_keys_button = g_object_get_data(G_OBJECT(airpcap_tb),AIRPCAP_TOOLBAR_KEY_MANAGEMENT_KEY);
92
93     /* The current interface is an airpcap interface */
94     if(if_info != NULL)
95     {
96                 gtk_widget_set_sensitive(airpcap_tb,TRUE);
97                 gtk_widget_set_sensitive(airpcap_toolbar_label,TRUE);
98                 gtk_widget_set_sensitive(toolbar_channel_cb,TRUE);
99                 gtk_widget_set_sensitive(airpcap_toolbar_channel_lb,TRUE);
100                 gtk_widget_set_sensitive(airpcap_toolbar_channel_offset,TRUE);
101                 gtk_widget_set_sensitive(airpcap_toolbar_channel_offset_lb,TRUE);
102                 gtk_widget_set_sensitive(airpcap_toolbar_fcs,FALSE);
103                 gtk_widget_set_sensitive(airpcap_toolbar_fcs_lb,FALSE);
104                 gtk_widget_set_sensitive(airpcap_toolbar_button,FALSE);
105                 gtk_widget_set_sensitive(airpcap_toolbar_button,FALSE);
106                 gtk_widget_set_sensitive(airpcap_toolbar_decryption,FALSE);
107                 gtk_widget_set_sensitive(airpcap_toolbar_decryption_lb,FALSE);
108                 gtk_widget_set_sensitive(airpcap_toolbar_keys_button,FALSE);
109
110                 /*decryption check box*/
111                 g_signal_handlers_block_by_func (airpcap_toolbar_decryption,airpcap_toolbar_encryption_cb, airpcap_tb);
112                 if(if_info->DecryptionOn == AIRPCAP_DECRYPTION_ON)
113                         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(airpcap_toolbar_decryption),TRUE);
114                 else
115                         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(airpcap_toolbar_decryption),FALSE);
116                 g_signal_handlers_unblock_by_func (airpcap_toolbar_decryption,airpcap_toolbar_encryption_cb, airpcap_tb);
117
118                 if_label_text = g_strdup_printf("Current Wireless Interface: #%s", airpcap_get_if_string_number(if_info));
119                 gtk_label_set_text(GTK_LABEL(airpcap_toolbar_label),if_label_text);
120                 g_free(if_label_text);
121
122                 change_airpcap_settings = FALSE;
123                 if (if_info->pSupportedChannels != NULL && if_info->numSupportedChannels > 0){
124                         guint i = 0;
125
126                         for (; i<if_info->numSupportedChannels; i++){
127                                 gtk_combo_box_append_text(GTK_COMBO_BOX(toolbar_channel_cb), ieee80211_mhz_to_str(if_info->pSupportedChannels[i].Frequency));
128                         }
129                 }
130
131                 airpcap_update_channel_combo(GTK_WIDGET(toolbar_channel_cb),if_info);
132                 airpcap_update_channel_offset_combo(if_info, if_info->channelInfo.Frequency, airpcap_toolbar_channel_offset, TRUE);
133                 change_airpcap_settings = TRUE;
134     }
135     else /* Current interface is NOT an AirPcap one... */
136     {
137                 gtk_widget_set_sensitive(airpcap_tb,FALSE);
138                 gtk_widget_set_sensitive(airpcap_toolbar_label,FALSE);
139                 gtk_widget_set_sensitive(toolbar_channel_cb,FALSE);
140                 gtk_widget_set_sensitive(airpcap_toolbar_channel_lb,FALSE);
141                 gtk_widget_set_sensitive(airpcap_toolbar_channel_offset,FALSE);
142                 gtk_widget_set_sensitive(airpcap_toolbar_channel_offset_lb,FALSE);
143                 gtk_widget_set_sensitive(airpcap_toolbar_fcs,FALSE);
144                 gtk_widget_set_sensitive(airpcap_toolbar_fcs_lb,FALSE);
145                 gtk_widget_set_sensitive(airpcap_toolbar_button,FALSE);
146                 gtk_widget_set_sensitive(airpcap_toolbar_button,FALSE);
147                 gtk_widget_set_sensitive(airpcap_toolbar_decryption,FALSE);
148                 gtk_widget_set_sensitive(airpcap_toolbar_decryption_lb,FALSE);
149                 gtk_widget_set_sensitive(airpcap_toolbar_keys_button,FALSE);
150     }
151 }
152
153 /*
154  * Set up the airpcap toolbar for the new capture interface
155  */
156 void
157 airpcap_set_toolbar_stop_capture(airpcap_if_info_t* if_info)
158 {
159     GtkWidget *airpcap_toolbar_crc_filter_combo;
160     GtkWidget *airpcap_toolbar_label;
161     GtkWidget *toolbar_channel_cb;
162     GtkWidget *airpcap_toolbar_channel_lb;
163     GtkWidget *airpcap_toolbar_channel_offset;
164     GtkWidget *airpcap_toolbar_channel_offset_lb;
165     GtkWidget *airpcap_toolbar_button;
166     GtkWidget *airpcap_toolbar_fcs;
167     GtkWidget *airpcap_toolbar_fcs_lb;
168     GtkWidget *airpcap_toolbar_decryption;
169     GtkWidget *airpcap_toolbar_decryption_lb;
170     GtkWidget *airpcap_toolbar_keys_button;
171
172     gchar *if_label_text;
173
174     airpcap_toolbar_crc_filter_combo = g_object_get_data(G_OBJECT(airpcap_tb),AIRPCAP_TOOLBAR_FCS_FILTER_KEY);
175     airpcap_toolbar_label    = g_object_get_data(G_OBJECT(airpcap_tb),AIRPCAP_TOOLBAR_INTERFACE_KEY);
176     toolbar_channel_cb  = g_object_get_data(G_OBJECT(airpcap_tb),AIRPCAP_TOOLBAR_CHANNEL_KEY);
177     airpcap_toolbar_channel_lb  = g_object_get_data(G_OBJECT(airpcap_tb),AIRPCAP_TOOLBAR_CHANNEL_LABEL_KEY);
178     airpcap_toolbar_channel_offset  = g_object_get_data(G_OBJECT(airpcap_tb),AIRPCAP_TOOLBAR_CHANNEL_OFFSET_KEY);
179     airpcap_toolbar_channel_offset_lb  = g_object_get_data(G_OBJECT(airpcap_tb),AIRPCAP_TOOLBAR_CHANNEL_OFFSET_LABEL_KEY);
180     airpcap_toolbar_fcs  = g_object_get_data(G_OBJECT(airpcap_tb),AIRPCAP_TOOLBAR_FCS_FILTER_KEY);
181     airpcap_toolbar_fcs_lb  = g_object_get_data(G_OBJECT(airpcap_tb),AIRPCAP_TOOLBAR_FCS_FILTER_LABEL_KEY);
182     airpcap_toolbar_button   = g_object_get_data(G_OBJECT(airpcap_tb),AIRPCAP_TOOLBAR_ADVANCED_KEY);
183     airpcap_toolbar_decryption = g_object_get_data(G_OBJECT(airpcap_tb),AIRPCAP_TOOLBAR_DECRYPTION_KEY);
184     airpcap_toolbar_decryption_lb = g_object_get_data(G_OBJECT(airpcap_tb),AIRPCAP_TOOLBAR_DECRYPTION_LABEL_KEY);
185     airpcap_toolbar_keys_button = g_object_get_data(G_OBJECT(airpcap_tb),AIRPCAP_TOOLBAR_KEY_MANAGEMENT_KEY);
186
187     /* The current interface is an airpcap interface */
188     if(if_info != NULL)
189     {
190                 gtk_widget_set_sensitive(airpcap_tb,TRUE);
191                 gtk_widget_set_sensitive(airpcap_toolbar_label,TRUE);
192                 gtk_widget_set_sensitive(toolbar_channel_cb,TRUE);
193                 gtk_widget_set_sensitive(airpcap_toolbar_channel_lb,TRUE);
194                 gtk_widget_set_sensitive(airpcap_toolbar_channel_offset,TRUE);
195                 gtk_widget_set_sensitive(airpcap_toolbar_channel_offset_lb,TRUE);
196                 gtk_widget_set_sensitive(airpcap_toolbar_fcs,TRUE);
197                 gtk_widget_set_sensitive(airpcap_toolbar_fcs_lb,TRUE);
198                 gtk_widget_set_sensitive(airpcap_toolbar_button,TRUE);
199                 gtk_widget_set_sensitive(airpcap_toolbar_crc_filter_combo,TRUE);
200                 gtk_widget_set_sensitive(airpcap_toolbar_decryption,TRUE);
201                 gtk_widget_set_sensitive(airpcap_toolbar_decryption_lb,TRUE);
202                 gtk_widget_set_sensitive(airpcap_toolbar_keys_button,TRUE);
203                 airpcap_validation_type_combo_set_by_type(airpcap_toolbar_crc_filter_combo, if_info->CrcValidationOn);
204
205                 /*decription check box*/
206                 g_signal_handlers_block_by_func (airpcap_toolbar_decryption,airpcap_toolbar_encryption_cb, airpcap_tb);
207                 if(if_info->DecryptionOn == AIRPCAP_DECRYPTION_ON)
208                         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(airpcap_toolbar_decryption),TRUE);
209                 else
210                         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(airpcap_toolbar_decryption),FALSE);
211                 g_signal_handlers_unblock_by_func (airpcap_toolbar_decryption,airpcap_toolbar_encryption_cb, airpcap_tb);
212
213                 if_label_text = g_strdup_printf("Current Wireless Interface: #%s", airpcap_get_if_string_number(if_info));
214                 gtk_label_set_text(GTK_LABEL(airpcap_toolbar_label),if_label_text);
215                 g_free(if_label_text);
216
217                 change_airpcap_settings = FALSE;
218                 if (if_info->pSupportedChannels != NULL && if_info->numSupportedChannels > 0){
219                         guint i = 0;
220
221                         for (; i<if_info->numSupportedChannels; i++){
222                                 gtk_combo_box_append_text(GTK_COMBO_BOX(toolbar_channel_cb), ieee80211_mhz_to_str(if_info->pSupportedChannels[i].Frequency));
223                         }
224                 }
225
226                 airpcap_update_channel_combo(GTK_WIDGET(toolbar_channel_cb),if_info);
227                 airpcap_update_channel_offset_combo(if_info, if_info->channelInfo.Frequency, airpcap_toolbar_channel_offset, TRUE);
228                 change_airpcap_settings = TRUE;
229         }
230     else
231     {
232                 gtk_widget_set_sensitive(airpcap_tb,TRUE);
233                 gtk_widget_set_sensitive(airpcap_toolbar_label,FALSE);
234                 gtk_widget_set_sensitive(toolbar_channel_cb,FALSE);
235                 gtk_widget_set_sensitive(airpcap_toolbar_channel_lb,FALSE);
236                 gtk_widget_set_sensitive(airpcap_toolbar_channel_offset,FALSE);
237                 gtk_widget_set_sensitive(airpcap_toolbar_channel_offset_lb,FALSE);
238                 gtk_widget_set_sensitive(airpcap_toolbar_fcs,FALSE);
239                 gtk_widget_set_sensitive(airpcap_toolbar_fcs_lb,FALSE);
240                 gtk_widget_set_sensitive(airpcap_toolbar_button,FALSE);
241                 gtk_widget_set_sensitive(airpcap_toolbar_crc_filter_combo,FALSE);
242                 gtk_widget_set_sensitive(airpcap_toolbar_decryption,TRUE);
243                 gtk_widget_set_sensitive(airpcap_toolbar_decryption_lb,TRUE);
244                 gtk_widget_set_sensitive(airpcap_toolbar_keys_button,TRUE);
245                 change_airpcap_settings = FALSE;
246     }
247 }
248
249 /*
250  * Add a key (string) to the given list
251  */
252 void
253 airpcap_add_key_to_list(GtkWidget *keylist, gchar* type, gchar* key, gchar* ssid)
254 {
255     gchar*       new_row[3];
256
257     new_row[0] = g_strdup(type);
258     new_row[1] = g_strdup(key);
259     new_row[2] = g_strdup(ssid);
260
261     gtk_clist_append(GTK_CLIST(keylist),new_row);
262
263     g_free(new_row[0]);
264     g_free(new_row[1]);
265     g_free(new_row[2]);
266 }
267
268 /*
269  * Modify a key given a list and a row
270  */
271 void
272 airpcap_modify_key_in_list(GtkWidget *keylist, gint row, gchar* type, gchar* key, gchar* ssid)
273 {
274     gchar*       new_row[3];
275
276     new_row[0] = g_strdup(type);
277     new_row[1] = g_strdup(key);
278     new_row[2] = g_strdup(ssid);
279
280     gtk_clist_set_text(GTK_CLIST(keylist),row,0,new_row[0]);
281     gtk_clist_set_text(GTK_CLIST(keylist),row,1,new_row[1]);
282     gtk_clist_set_text(GTK_CLIST(keylist),row,2,new_row[2]);
283
284     g_free(new_row[0]);
285     g_free(new_row[1]);
286     g_free(new_row[2]);
287 }
288
289 /*
290  * Fill the list with the keys. BEWARE! At this point, Wireshark and Drivers
291  * keys should be EQUALS! But is better to load keys from Wireshark, because
292  * the driver is not always present, and maybe that cannot support some keys
293  * (i.e. the WPA problem)
294  */
295 void
296 airpcap_fill_key_list(GtkWidget *keylist)
297 {
298     gchar*               s = NULL;
299     gchar*               s2 = NULL;
300     unsigned int i,n;
301     gchar*       new_row[3];
302     airpcap_if_info_t* fake_if_info;
303     GList*               wireshark_key_list=NULL;
304     decryption_key_t* curr_key = NULL;
305
306     n = 0;
307
308     fake_if_info = airpcap_driver_fake_if_info_new();
309
310     /* We can retrieve the driver's key list (i.e. we have the right .dll)*/
311     wireshark_key_list = get_wireshark_keys();
312     n = g_list_length(wireshark_key_list);
313
314     for(i = 0; i < n; i++)
315     {
316         curr_key = (decryption_key_t*)g_list_nth_data(wireshark_key_list,i);
317
318         if(curr_key->type == AIRPDCAP_KEY_TYPE_WEP)
319         {
320             s = g_strdup(curr_key->key->str);
321
322             new_row[0] = g_strdup(AIRPCAP_WEP_KEY_STRING);
323             new_row[1] = g_strdup(s);
324             new_row[2] = g_strdup("");
325
326             gtk_clist_append(GTK_CLIST(keylist),new_row);
327
328             g_free(new_row[0]);
329             g_free(new_row[1]);
330             g_free(new_row[2]);
331
332             g_free(s);
333         }
334         else if(curr_key->type == AIRPDCAP_KEY_TYPE_WPA_PWD)
335         {
336             s = g_strdup(curr_key->key->str);
337             if(curr_key->ssid != NULL)
338                 s2= g_strdup(format_uri(curr_key->ssid, ":"));
339             else
340                 s2 = NULL;
341
342             new_row[0] = g_strdup(AIRPCAP_WPA_PWD_KEY_STRING);
343             new_row[1] = g_strdup(s);
344
345             if(curr_key->ssid != NULL)
346                 new_row[2] = g_strdup(s2);
347             else
348                 new_row[2] = g_strdup("");
349
350             gtk_clist_append(GTK_CLIST(keylist),new_row);
351
352             g_free(new_row[0]);
353             g_free(new_row[1]);
354             g_free(new_row[2]);
355
356             g_free(s);
357             if(s2 != NULL)
358                 g_free(s2);
359         }
360         else if(curr_key->type == AIRPDCAP_KEY_TYPE_WPA_PMK)
361         {
362             s = g_strdup(curr_key->key->str);
363
364             new_row[0] = g_strdup(AIRPCAP_WPA_BIN_KEY_STRING);
365             new_row[1] = g_strdup(s);
366             new_row[2] = g_strdup("");
367
368             gtk_clist_append(GTK_CLIST(keylist),new_row);
369
370             g_free(new_row[0]);
371             g_free(new_row[1]);
372             g_free(new_row[2]);
373
374             g_free(s);
375         }
376     }
377
378     airpcap_if_info_free(fake_if_info);
379     return;
380 }
381
382 /*
383  * Function used to retrieve the AirpcapValidationType given the string name.
384  */
385 AirpcapValidationType
386 airpcap_get_validation_type(const gchar* name)
387 {
388         if(!(g_ascii_strcasecmp(AIRPCAP_VALIDATION_TYPE_NAME_ALL,name)))
389         {
390                 return AIRPCAP_VT_ACCEPT_EVERYTHING;
391         }
392         else if(!(g_ascii_strcasecmp(AIRPCAP_VALIDATION_TYPE_NAME_CORRECT,name)))
393         {
394                 return AIRPCAP_VT_ACCEPT_CORRECT_FRAMES;
395         }
396         else if(!(g_ascii_strcasecmp(AIRPCAP_VALIDATION_TYPE_NAME_CORRUPT,name)))
397         {
398                 return AIRPCAP_VT_ACCEPT_CORRUPT_FRAMES;
399         }
400         return AIRPCAP_VT_UNKNOWN;
401 }
402
403 /*
404  * Function used to retrieve the string name given an AirpcapValidationType,
405  * or NULL in case of error
406  */
407 gchar*
408 airpcap_get_validation_name(AirpcapValidationType vt)
409 {
410         if(vt == AIRPCAP_VT_ACCEPT_EVERYTHING)
411         {
412                 return AIRPCAP_VALIDATION_TYPE_NAME_ALL;
413         }
414         else if(vt == AIRPCAP_VT_ACCEPT_CORRECT_FRAMES)
415         {
416                 return AIRPCAP_VALIDATION_TYPE_NAME_CORRECT;
417         }
418         else if(vt == AIRPCAP_VT_ACCEPT_CORRUPT_FRAMES)
419         {
420                 return AIRPCAP_VALIDATION_TYPE_NAME_CORRUPT;
421         }
422         else if(vt == AIRPCAP_VT_UNKNOWN)
423         {
424                 return AIRPCAP_VALIDATION_TYPE_NAME_UNKNOWN;
425         }
426         return NULL;
427 }
428
429 /*
430  * Return an appropriate combo box entry number for the given an AirpcapValidationType,
431  * defaulting to 0
432  */
433 gint
434 airpcap_get_validation_combo_entry(AirpcapValidationType vt)
435 {
436     switch (vt) {
437         case AIRPCAP_VT_ACCEPT_CORRECT_FRAMES:
438             return 1;
439             break;
440         case AIRPCAP_VT_ACCEPT_CORRUPT_FRAMES:
441             return 2;
442             break;
443         default:
444             return 0;
445             break;
446     }
447 }
448
449 /*
450  * Returns the AirpcapLinkType corresponding to the given string name.
451  */
452 AirpcapLinkType
453 airpcap_get_link_type(const gchar* name)
454 {
455     if(!(g_ascii_strcasecmp(AIRPCAP_LINK_TYPE_NAME_802_11_ONLY,name))){
456                 return AIRPCAP_LT_802_11;
457     }else if(!(g_ascii_strcasecmp(AIRPCAP_LINK_TYPE_NAME_802_11_PLUS_RADIO,name))){
458                 return AIRPCAP_LT_802_11_PLUS_RADIO;
459     }else if(!(g_ascii_strcasecmp(AIRPCAP_LINK_TYPE_NAME_802_11_PLUS_PPI,name))){
460                 return AIRPCAP_LT_802_11_PLUS_PPI;
461     }else{
462                 return AIRPCAP_LT_UNKNOWN;
463     }
464 }
465
466 /*
467  * Returns the string name corresponding to the given AirpcapLinkType, or
468  * NULL in case of error.
469  */
470 gchar*
471 airpcap_get_link_name(AirpcapLinkType lt)
472 {
473     if(lt == AIRPCAP_LT_802_11){
474                 return AIRPCAP_LINK_TYPE_NAME_802_11_ONLY;
475     }else if(lt == AIRPCAP_LT_802_11_PLUS_RADIO){
476                 return AIRPCAP_LINK_TYPE_NAME_802_11_PLUS_RADIO;
477     }else if(lt == AIRPCAP_LT_802_11_PLUS_PPI){
478                 return AIRPCAP_LINK_TYPE_NAME_802_11_PLUS_PPI;
479     }else if(lt == AIRPCAP_LT_UNKNOWN){
480                 return AIRPCAP_LINK_TYPE_NAME_UNKNOWN;
481     }
482     return NULL;
483 }
484
485 /*
486  * Sets the entry of the validation combo using the AirpcapValidationType.
487  */
488 void
489 airpcap_validation_type_combo_set_by_type(GtkWidget* c, AirpcapValidationType type)
490 {
491     gtk_combo_box_set_active(GTK_COMBO_BOX(c), airpcap_get_validation_combo_entry(type));
492 }
493
494 /*
495  * Returns the string corresponding to the given guint (1-14, for channel only)
496  */
497 gchar*
498 airpcap_get_channel_name(guint n)
499 {
500     return g_strdup_printf("%d",n);
501 }
502
503
504 /*
505  * Set the combo box entry string given a channel frequency
506  */
507 void
508 airpcap_channel_combo_set_by_frequency(GtkWidget* cb, guint chan_freq)
509 {
510     guint i;
511
512     for (i = 0; i < airpcap_if_selected->numSupportedChannels; i++) {
513         if (airpcap_if_selected->pSupportedChannels[i].Frequency == chan_freq) {
514             gtk_combo_box_set_active(GTK_COMBO_BOX(cb), i);
515             break;
516         }
517     }
518 }
519
520 /*
521  * Change channel of Airpcap Adapter
522  */
523 static gboolean
524 airpcap_update_frequency_and_offset(airpcap_if_info_t* if_info)
525 {
526     gchar ebuf[AIRPCAP_ERRBUF_SIZE];
527     PAirpcapHandle ad;
528     gboolean return_value = FALSE;
529
530     if (if_info != NULL){
531         ad = airpcap_if_open(if_info->name, ebuf);
532
533         if(ad != NULL) {
534             return_value = airpcap_if_set_device_channel_ex(ad,if_info->channelInfo);
535             airpcap_if_close(ad);
536         }
537     }
538
539     return return_value;
540 }
541
542 /*
543  * Changed callback for the channel combobox - common routine
544  */
545 static void
546 airpcap_channel_changed_common(GtkWidget *channel_cb, gpointer channel_offset_cb, gboolean set)
547 {
548     gint cur_chan_idx;
549
550     if (channel_cb && channel_offset_cb && change_airpcap_settings && airpcap_if_active) {
551         cur_chan_idx = gtk_combo_box_get_active(GTK_COMBO_BOX(channel_cb));
552         if (cur_chan_idx >= 0 && cur_chan_idx < (gint) airpcap_if_active->numSupportedChannels) {
553             if (set) {
554                 airpcap_if_active->channelInfo.Frequency = airpcap_if_active->pSupportedChannels[cur_chan_idx].Frequency;
555             }
556             airpcap_update_channel_offset_combo(airpcap_if_active,
557                     airpcap_if_active->channelInfo.Frequency,
558                     GTK_WIDGET(channel_offset_cb), set);
559         }
560     }
561 }
562
563 /*
564  * Changed callback for the channel combobox - set channel and offset
565  */
566 void
567 airpcap_channel_changed_set_cb(GtkWidget *channel_cb, gpointer channel_offset_cb)
568 {
569     airpcap_channel_changed_common(channel_cb, channel_offset_cb, TRUE);
570 }
571
572 /*
573  * Changed callback for the channel combobox - don't set channel and offset
574  */
575 void
576 airpcap_channel_changed_noset_cb(GtkWidget *channel_cb, gpointer channel_offset_cb)
577 {
578     airpcap_channel_changed_common(channel_cb, channel_offset_cb, FALSE);
579 }
580
581 static int
582 airpcap_get_selected_channel_offset(GtkWidget *channel_offset_cb) {
583     int offset;
584     gchar *off_str;
585     int retval = 0;
586     
587
588     if (channel_offset_cb == NULL || !GTK_WIDGET_SENSITIVE(channel_offset_cb)) {
589         return 0;
590     }
591     
592     off_str = gtk_combo_box_get_active_text(GTK_COMBO_BOX(channel_offset_cb));
593     if (off_str && (g_ascii_strcasecmp("", off_str)))
594     {
595         if (airpcap_if_selected != NULL)
596         {
597             sscanf(off_str, "%d", &offset);
598             if (offset >= -1 && offset <= 1) {
599                 retval = offset;
600             }
601         }
602     }
603     g_free(off_str);
604     return retval;
605 }
606
607 /*
608  * Changed callback for the channel offset combobox
609  */
610 void
611 airpcap_channel_offset_changed_cb(GtkWidget *channel_offset_cb, gpointer data _U_)
612 {
613     airpcap_if_selected->channelInfo.ExtChannel = airpcap_get_selected_channel_offset(channel_offset_cb);
614     airpcap_if_selected->saved = FALSE;
615         change_airpcap_settings = TRUE;
616         if (!airpcap_update_frequency_and_offset(airpcap_if_selected)){
617                 simple_dialog(ESD_TYPE_ERROR,ESD_BTN_OK,
618                                           "Unable to set extension channel %d",
619                                           airpcap_if_selected->channelInfo.ExtChannel);
620         }
621 }
622
623
624 /*
625  * Update the channel offset of the given combobox according to the given frequency.
626  */
627 void
628 airpcap_update_channel_offset_combo(airpcap_if_info_t* if_info, guint chan_freq, GtkWidget *channel_offset_cb, gboolean set)
629 {
630     gint current_offset;
631     gint new_offset;
632     guint i;
633     gint active_idx = 0;
634     gint idx_count = -1;
635
636     if (!if_info || airpcap_if_is_any(if_info) || if_info->pSupportedChannels == NULL || if_info->numSupportedChannels < 1) {
637         gtk_widget_set_sensitive(GTK_WIDGET(channel_offset_cb),FALSE);
638         gtk_combo_box_set_active(GTK_COMBO_BOX(channel_offset_cb), -1);
639         return;
640     }
641
642     new_offset = current_offset = if_info->channelInfo.ExtChannel;
643
644     /* Clear out the list */
645     while (gtk_tree_model_iter_n_children(gtk_combo_box_get_model(GTK_COMBO_BOX(channel_offset_cb)), NULL) > 0) {
646         gtk_combo_box_remove_text(GTK_COMBO_BOX(channel_offset_cb), 0);
647     }
648
649     gtk_widget_set_sensitive(GTK_WIDGET(channel_offset_cb), TRUE);
650
651     for (i = 0; i < if_info->numSupportedChannels; i++) {
652         if (if_info->pSupportedChannels[i].Frequency == chan_freq) {
653
654             /* If we can't be low or high, nudge the offset to 0 */
655             if (current_offset == -1 && !(if_info->pSupportedChannels[i].Flags & FLAG_CAN_BE_LOW)) {
656                 new_offset = 0;
657             } else if (current_offset == 1 && !(if_info->pSupportedChannels[i].Flags & FLAG_CAN_BE_HIGH)) {
658                 new_offset = 0;
659             }
660
661             if ((if_info->pSupportedChannels[i].Flags & FLAG_CAN_BE_LOW)) {
662                 gtk_combo_box_append_text(GTK_COMBO_BOX(channel_offset_cb), "-1");
663                 idx_count++;
664                 if (new_offset == -1) {
665                     active_idx = idx_count;
666                 }
667             }
668             gtk_combo_box_append_text(GTK_COMBO_BOX(channel_offset_cb), "0");
669             idx_count++;
670             if (new_offset == 0) {
671                 active_idx = idx_count;
672             }
673             if ((if_info->pSupportedChannels[i].Flags & FLAG_CAN_BE_HIGH)){
674                 gtk_combo_box_append_text(GTK_COMBO_BOX(channel_offset_cb), "+1");
675                 idx_count++;
676                 if (new_offset == 1) {
677                     active_idx = idx_count;
678                 }
679             }
680             break;
681         }
682     }
683
684     gtk_combo_box_set_active(GTK_COMBO_BOX(channel_offset_cb), active_idx);
685
686
687     if (set) {
688         change_airpcap_settings = TRUE;
689     
690         if_info->channelInfo.ExtChannel = new_offset;
691         if (!airpcap_update_frequency_and_offset(if_info)){
692             simple_dialog(ESD_TYPE_ERROR,ESD_BTN_OK,"Adapter failed to be set with the following settings: Frequency - %d   Extension Channel - %d", if_info->channelInfo.Frequency, if_info->channelInfo.ExtChannel);
693         }
694     }
695     
696     if (idx_count < 1) {
697         gtk_widget_set_sensitive(channel_offset_cb, FALSE);
698     }
699 }
700
701 /*
702  * Returns '1' if this is the "Any" adapter, '0' otherwise
703  */
704 int
705 airpcap_if_is_any(airpcap_if_info_t* if_info)
706 {
707     if(g_ascii_strcasecmp(if_info->name,AIRPCAP_DEVICE_ANY_EXTRACT_STRING)==0)
708         return 1;
709     else
710         return 0;
711 }
712
713 /*
714  * Update channel combo box. If the airpcap interface is "Any", the combo box will be disabled.
715  */
716 void
717 airpcap_update_channel_combo(GtkWidget* channel_cb, airpcap_if_info_t* if_info)
718 {
719     if(!if_info || airpcap_if_is_any(if_info) || !airpcap_if_selected)
720     {
721         gtk_combo_box_set_active(GTK_COMBO_BOX(channel_cb), -1);
722         change_airpcap_settings = FALSE;
723         gtk_widget_set_sensitive(GTK_WIDGET(channel_cb),FALSE);
724     }
725     else
726     {
727         while (gtk_tree_model_iter_n_children(gtk_combo_box_get_model(GTK_COMBO_BOX(channel_cb)), NULL) > 0) {
728             gtk_combo_box_remove_text(GTK_COMBO_BOX(channel_cb), 0);
729         }
730         
731         if (if_info != NULL && if_info->pSupportedChannels != NULL && if_info->numSupportedChannels > 0){
732             guint i;
733             for (i = 0; i<(if_info->numSupportedChannels); i++){
734                 gtk_combo_box_append_text(GTK_COMBO_BOX(channel_cb), ieee80211_mhz_to_str(airpcap_if_selected->pSupportedChannels[i].Frequency));
735             }
736         }    
737
738         airpcap_channel_combo_set_by_frequency(channel_cb, if_info->channelInfo.Frequency);
739         change_airpcap_settings = TRUE;
740         gtk_widget_set_sensitive(GTK_WIDGET(channel_cb), TRUE);
741     }
742 }
743
744
745 /*
746  * Takes the keys from the GtkList widget, and add them to the interface list
747  */
748 void
749 airpcap_add_keys_from_list(GtkWidget *key_ls, airpcap_if_info_t *if_info _U_)
750 {
751     GString             *new_key;
752
753     /* airpcap stuff */
754     guint i, j;
755     gchar s[3];
756     PAirpcapKeysCollection KeysCollection;
757     guint KeysCollectionSize;
758     guint8 KeyByte;
759
760     guint keys_in_list = 0;
761
762     gchar *row_type,
763           *row_key,
764           *row_ssid;
765
766     keys_in_list = GTK_CLIST(key_ls)->rows;
767
768     /*
769      * Save the encryption keys, if we have any of them
770      */
771     KeysCollectionSize = 0;
772
773     /*
774      * Calculate the size of the keys collection
775      */
776     KeysCollectionSize = sizeof(AirpcapKeysCollection) + keys_in_list * sizeof(AirpcapKey);
777
778     /*
779      * Allocate the collection
780      */
781     KeysCollection = (PAirpcapKeysCollection)g_malloc(KeysCollectionSize);
782     if(!KeysCollection)
783     {
784         /* Simple dialog ERROR */
785         simple_dialog(ESD_TYPE_ERROR,ESD_BTN_OK,"%s","Failed memory allocation for KeysCollection!");
786         return;
787     }
788
789     /*
790      * Populate the key collection
791      */
792     KeysCollection->nKeys = keys_in_list;
793
794     for(i = 0; i < keys_in_list; i++)
795     {
796         /* Retrieve the row infos */
797         gtk_clist_get_text(GTK_CLIST(key_ls),i,0,&row_type);
798         gtk_clist_get_text(GTK_CLIST(key_ls),i,1,&row_key);
799         gtk_clist_get_text(GTK_CLIST(key_ls),i,2,&row_ssid);
800
801         if(g_ascii_strcasecmp(row_type,AIRPCAP_WEP_KEY_STRING) == 0)
802         KeysCollection->Keys[i].KeyType = AIRPDCAP_KEY_TYPE_WEP;
803         else if(g_ascii_strcasecmp(row_type,AIRPCAP_WPA_PWD_KEY_STRING) == 0)
804         KeysCollection->Keys[i].KeyType = AIRPCAP_KEYTYPE_TKIP;
805         else if(g_ascii_strcasecmp(row_type,AIRPCAP_WPA_BIN_KEY_STRING) == 0)
806         KeysCollection->Keys[i].KeyType = AIRPCAP_KEYTYPE_CCMP;
807
808         /* Retrieve the Item corresponding to the i-th key */
809         new_key = g_string_new(row_key);
810
811         KeysCollection->Keys[i].KeyLen = (guint) new_key->len / 2;
812         memset(&KeysCollection->Keys[i].KeyData, 0, sizeof(KeysCollection->Keys[i].KeyData));
813
814         for(j = 0 ; j < new_key->len; j += 2)
815         {
816             s[0] = new_key->str[j];
817             s[1] = new_key->str[j+1];
818             s[2] = '\0';
819             KeyByte = (guint8)strtol(s, NULL, 16);
820             KeysCollection->Keys[i].KeyData[j / 2] = KeyByte;
821         }
822     }
823
824     /*
825      * Free the old adapter key collection!
826      */
827     if(airpcap_if_selected->keysCollection != NULL)
828             g_free(airpcap_if_selected->keysCollection);
829
830     /*
831      * Set this collection ad the new one
832      */
833     airpcap_if_selected->keysCollection = KeysCollection;
834     airpcap_if_selected->keysCollectionSize = KeysCollectionSize;
835
836     return;
837 }
838
839 /*
840  * Takes the keys from the GtkList widget, and add them to the interface list
841  */
842 void
843 airpcap_add_keys_to_driver_from_list(GtkWidget *key_ls,airpcap_if_info_t *fake_if_info)
844 {
845     GString             *new_key;
846
847     /* airpcap stuff */
848     guint i, j;
849     gchar s[3];
850     PAirpcapKeysCollection KeysCollection;
851     guint KeysCollectionSize;
852     guint8 KeyByte;
853
854     guint keys_in_list = 0;
855
856     gchar *row_type,
857           *row_key,
858           *row_ssid;
859
860     if(fake_if_info == NULL)
861             return;
862
863     keys_in_list = GTK_CLIST(key_ls)->rows;
864
865     /*
866      * Save the encryption keys, if we have any of them
867      */
868     KeysCollectionSize = 0;
869
870     /*
871      * Calculate the size of the keys collection
872      */
873     KeysCollectionSize = sizeof(AirpcapKeysCollection) + keys_in_list * sizeof(AirpcapKey);
874
875     /*
876      * Allocate the collection
877      */
878     KeysCollection = (PAirpcapKeysCollection)g_malloc(KeysCollectionSize);
879     if(!KeysCollection)
880     {
881         /* Simple dialog ERROR */
882         simple_dialog(ESD_TYPE_ERROR,ESD_BTN_OK,"%s","Failed memory allocation for KeysCollection!");
883         return;
884     }
885
886     /*
887      * Populate the key collection
888      */
889     KeysCollection->nKeys = keys_in_list;
890
891     for(i = 0; i < keys_in_list; i++)
892     {
893         /* Retrieve the row infos */
894         gtk_clist_get_text(GTK_CLIST(key_ls),i,0,&row_type);
895         gtk_clist_get_text(GTK_CLIST(key_ls),i,1,&row_key);
896         gtk_clist_get_text(GTK_CLIST(key_ls),i,2,&row_ssid);
897
898         if(g_ascii_strcasecmp(row_type,AIRPCAP_WEP_KEY_STRING) == 0)
899         KeysCollection->Keys[i].KeyType = AIRPDCAP_KEY_TYPE_WEP;
900         else if(g_ascii_strcasecmp(row_type,AIRPCAP_WPA_PWD_KEY_STRING) == 0)
901         KeysCollection->Keys[i].KeyType = AIRPDCAP_KEY_TYPE_WPA_PWD;
902         else if(g_ascii_strcasecmp(row_type,AIRPCAP_WPA_BIN_KEY_STRING) == 0)
903         KeysCollection->Keys[i].KeyType = AIRPDCAP_KEY_TYPE_WPA_PMK;
904
905         /* Retrieve the Item corresponding to the i-th key */
906         new_key = g_string_new(row_key);
907
908         KeysCollection->Keys[i].KeyLen = (guint) new_key->len / 2;
909         memset(&KeysCollection->Keys[i].KeyData, 0, sizeof(KeysCollection->Keys[i].KeyData));
910
911         /* Key must be saved in adifferent way, depending on its type... */
912         if(KeysCollection->Keys[i].KeyType == AIRPDCAP_KEY_TYPE_WEP)
913         {
914             for(j = 0 ; j < new_key->len; j += 2)
915             {
916                     s[0] = new_key->str[j];
917                     s[1] = new_key->str[j+1];
918                     s[2] = '\0';
919                     KeyByte = (guint8)strtol(s, NULL, 16);
920                     KeysCollection->Keys[i].KeyData[j / 2] = KeyByte;
921             }
922         }
923         /* XXX - Save the keys that are not WEP!!! */
924     }
925
926     /*
927      * Free the old adapter key collection!
928      */
929     if(fake_if_info->keysCollection != NULL)
930             g_free(fake_if_info->keysCollection);
931
932     /*
933      * Set this collection ad the new one
934      */
935     fake_if_info->keysCollection = KeysCollection;
936     fake_if_info->keysCollectionSize = KeysCollectionSize;
937     return;
938 }
939
940 /*
941  * This function will take the current keys (widget list), specified for the
942  * current adapter, and save them as default for ALL the others.
943  */
944 void
945 airpcap_read_and_save_decryption_keys_from_clist(GtkWidget* key_ls, airpcap_if_info_t* info_if, GList* if_list)
946 {
947     gint if_n = 0;
948     gint i = 0;
949     gint r = 0;
950     gint n = 0;
951     airpcap_if_info_t* curr_if = NULL;
952     airpcap_if_info_t* fake_info_if = NULL;
953     GList* key_list=NULL;
954
955     char* tmp_type = NULL;
956     char* tmp_key = NULL;
957     char* tmp_ssid = NULL;
958
959     decryption_key_t* tmp_dk=NULL;
960
961     /*
962      * Save the keys for Wireshark...
963      */
964
965     /* Create a list of keys from the list widget... */
966     n = GTK_CLIST(key_ls)->rows;
967
968     for(i = 0; i < n; i++)
969     {
970         /* XXX - Create a decryption_key_t struct, and pass a list of those structs!!! */
971         gtk_clist_get_text(GTK_CLIST(key_ls),i,0,&tmp_type);
972         gtk_clist_get_text(GTK_CLIST(key_ls),i,1,&tmp_key);
973         gtk_clist_get_text(GTK_CLIST(key_ls),i,2,&tmp_ssid);
974
975         if(g_ascii_strcasecmp(tmp_type,AIRPCAP_WEP_KEY_STRING) == 0)
976         {
977             tmp_dk = (decryption_key_t*)g_malloc(sizeof(decryption_key_t));
978             tmp_dk->key = g_string_new(tmp_key);
979             tmp_dk->ssid = NULL;
980             tmp_dk->type = AIRPDCAP_KEY_TYPE_WEP;
981             tmp_dk->bits = (guint) tmp_dk->key->len * 4;
982             key_list = g_list_append(key_list,tmp_dk);
983         }
984         else if(g_ascii_strcasecmp(tmp_type,AIRPCAP_WPA_PWD_KEY_STRING) == 0)
985         {
986             tmp_dk = (decryption_key_t*)g_malloc(sizeof(decryption_key_t));
987             tmp_dk->key = g_string_new(tmp_key);
988             tmp_dk->ssid = g_byte_array_new();
989             uri_str_to_bytes(tmp_ssid, tmp_dk->ssid);
990             tmp_dk->type = AIRPDCAP_KEY_TYPE_WPA_PWD;
991             tmp_dk->bits = 256;
992             key_list = g_list_append(key_list,tmp_dk);
993         }
994         else if(g_ascii_strcasecmp(tmp_type,AIRPCAP_WPA_BIN_KEY_STRING) == 0)
995         {
996             tmp_dk = (decryption_key_t*)g_malloc(sizeof(decryption_key_t));
997             tmp_dk->key = g_string_new(tmp_key);
998             tmp_dk->ssid = NULL; /* No SSID in this case */
999             tmp_dk->type = AIRPDCAP_KEY_TYPE_WPA_PMK;
1000             tmp_dk->bits = 256;
1001             key_list = g_list_append(key_list,tmp_dk);
1002         }
1003     }
1004
1005     r = save_wlan_wireshark_wep_keys(key_list);
1006     /* The key_list has been freed!!! */
1007
1008     /*
1009      * Save the key list for driver.
1010      */
1011     if( (if_list == NULL) || (info_if == NULL) ) return;
1012
1013     fake_info_if = airpcap_driver_fake_if_info_new();
1014
1015     airpcap_add_keys_to_driver_from_list(key_ls,fake_info_if);
1016     airpcap_save_driver_if_configuration(fake_info_if);
1017     airpcap_if_info_free(fake_info_if);
1018
1019     if_n = g_list_length(if_list);
1020
1021     /* For all the adapters in the list, empty the key list */
1022     for(i = 0; i < if_n; i++)
1023     {
1024         curr_if = (airpcap_if_info_t*)g_list_nth_data(if_list,i);
1025
1026         if(curr_if != NULL)
1027         {
1028             /* XXX - Set an empty collection */
1029             airpcap_if_clear_decryption_settings(curr_if);
1030
1031             /* Save to registry */
1032             airpcap_save_selected_if_configuration(curr_if);
1033         }
1034     }
1035 }
1036
1037 /*
1038  * This function will load from the preferences file ALL the
1039  * keys (WEP, WPA and WPA_BIN) and will set them as default for
1040  * each adapter. To do this, it will save the keys in the registry...
1041  * A check will be performed, to make sure that keys found in
1042  * registry and keys found in Wireshark preferences are the same. If not,
1043  * the user will be asked to choose if use all keys (merge them),
1044  * or use Wireshark preferences ones. In the last case, registry keys will
1045  * be overwritten for all the connected AirPcap adapters.
1046  * In the first case, adapters will use their own keys, but those
1047  * keys will not be accessible via Wireshark...
1048  */
1049 gboolean
1050 airpcap_check_decryption_keys(GList* if_list)
1051 {
1052     gint if_n = 0;
1053     gint i = 0;
1054     gint n_adapters_keys = 0;
1055     gint n_driver_keys = 0;
1056     gint n_wireshark_keys = 0;
1057     airpcap_if_info_t* curr_if = NULL;
1058
1059     GList* wireshark_key_list;
1060     GList* driver_key_list;
1061     GList* curr_adapter_key_list;
1062
1063     gboolean equals = TRUE;
1064     gboolean adapters_keys_equals=TRUE;
1065
1066     /*
1067      * If no AirPcap interface is found, return TRUE, so Wireshark
1068      * will use HIS OWN keys.
1069      */
1070     if(if_list == NULL)
1071         return TRUE;
1072
1073     if_n = g_list_length(if_list);
1074
1075     /* Get Wireshark preferences keys */
1076     wireshark_key_list = get_wireshark_keys();
1077     n_wireshark_keys = g_list_length(wireshark_key_list);
1078
1079     /* Retrieve AirPcap driver's keys */
1080     driver_key_list = get_airpcap_driver_keys();
1081     n_driver_keys = g_list_length(driver_key_list);
1082
1083     equals &= key_lists_are_equal(wireshark_key_list,driver_key_list);
1084
1085     for(i = 0; i < if_n; i++)
1086     {
1087         curr_if = (airpcap_if_info_t*)g_list_nth_data(if_list,i);
1088         curr_adapter_key_list = get_airpcap_device_keys(curr_if);
1089         n_adapters_keys += g_list_length(curr_adapter_key_list);
1090         adapters_keys_equals &= key_lists_are_equal(wireshark_key_list,curr_adapter_key_list);
1091     }
1092
1093     if(n_adapters_keys != 0) /* If for some reason at least one specific key has been found */
1094         equals &= adapters_keys_equals; /* */
1095
1096     if(n_driver_keys == 0) /* No keys set in any of the AirPcap adapters... */
1097         return TRUE; /* Use Wireshark keys and set them ad default for airpcap devices */
1098
1099     return equals;
1100 }
1101
1102 /*
1103  * This function will load from the preferences file ALL the
1104  * keys (WEP, WPA_PWD and WPA_BIN) and will set them as default for
1105  * each adapter. To do this, it will save the keys in the registry...
1106  * A check will be performed, to make sure that keys found in
1107  * registry and keys found in Wireshark preferences are the same. If not,
1108  * the user will be asked to choose if use all keys (merge them),
1109  * or use Wireshark preferences ones. In the last case, registry keys will
1110  * be overwritten for all the connected AirPcap adapters.
1111  * In the first case, adapters will use their own keys, but those
1112  * keys will not be accessible via Wireshark...
1113  */
1114 void
1115 airpcap_load_decryption_keys(GList* if_list)
1116 {
1117     gint if_n = 0;
1118     gint i = 0;
1119     airpcap_if_info_t* curr_if = NULL;
1120
1121     if(if_list == NULL) return;
1122
1123     if_n = g_list_length(if_list);
1124
1125     for(i = 0; i < if_n; i++)
1126     {
1127         curr_if = (airpcap_if_info_t*)g_list_nth_data(if_list,i);
1128         load_wlan_driver_wep_keys();
1129     }
1130 }
1131
1132 /*
1133  * This function will set the gibven GList of decryption_key_t structures
1134  * as the defoult for both Wireshark and the AirPcap adapters...
1135  */
1136 void
1137 airpcap_save_decryption_keys(GList* key_list, GList* adapters_list)
1138 {
1139     gint if_n = 0;
1140     gint key_n = 0;
1141     gint i = 0;
1142     airpcap_if_info_t* curr_if = NULL;
1143     GList* empty_key_list = NULL;
1144
1145     if( (key_list == NULL) || (adapters_list == NULL)) return;
1146
1147     if_n = g_list_length(adapters_list);
1148     key_n = g_list_length(key_list);
1149
1150     /* Set the driver's global list of keys. */
1151     write_wlan_driver_wep_keys_to_registry(key_list);
1152
1153     /* Empty the key list for each interface */
1154     for(i = 0; i < if_n; i++)
1155     {
1156         curr_if = (airpcap_if_info_t*)g_list_nth_data(adapters_list,i);
1157         write_wlan_wep_keys_to_registry(curr_if,empty_key_list);
1158     }
1159
1160     /*
1161      * This will set the keys of the current adapter as Wireshark default...
1162      * Now all the adapters have the same keys, so curr_if is ok as any other...
1163      */
1164     save_wlan_wireshark_wep_keys(key_list);
1165 }
1166
1167 /*
1168  * This function is used to enable/disable the toolbar widgets
1169  * depending on the type of interface selected... Not the whole
1170  * toolbar must be grayed/enabled ... Only some widgets...
1171  */
1172 void
1173 airpcap_enable_toolbar_widgets(GtkWidget* w, gboolean en)
1174 {
1175     GtkWidget   *toolbar_tb,
1176                 *if_description_lb,
1177                 *toolbar_channel_cb,
1178                 *channel_lb,
1179                 *channel_offset_cb,
1180     *channel_offset_lb,
1181                 *fcs_cb,
1182                 *fcs_lb,
1183                 *advanced_bt;
1184
1185     if(w == NULL)
1186         return;
1187
1188     toolbar_tb = w;
1189
1190     if_description_lb   = g_object_get_data(G_OBJECT(toolbar_tb),AIRPCAP_TOOLBAR_INTERFACE_KEY);
1191     channel_lb                  = g_object_get_data(G_OBJECT(toolbar_tb),AIRPCAP_TOOLBAR_CHANNEL_LABEL_KEY);
1192     toolbar_channel_cb  = g_object_get_data(G_OBJECT(toolbar_tb),AIRPCAP_TOOLBAR_CHANNEL_KEY);
1193     channel_offset_cb   = g_object_get_data(G_OBJECT(toolbar_tb),AIRPCAP_TOOLBAR_CHANNEL_OFFSET_KEY);
1194     channel_offset_lb   = g_object_get_data(G_OBJECT(toolbar_tb),AIRPCAP_TOOLBAR_CHANNEL_OFFSET_LABEL_KEY);
1195     fcs_lb                              = g_object_get_data(G_OBJECT(toolbar_tb),AIRPCAP_TOOLBAR_FCS_FILTER_LABEL_KEY);
1196     fcs_cb                              = g_object_get_data(G_OBJECT(toolbar_tb),AIRPCAP_TOOLBAR_FCS_FILTER_KEY);
1197     advanced_bt                 = g_object_get_data(G_OBJECT(toolbar_tb),AIRPCAP_TOOLBAR_ADVANCED_KEY);
1198
1199
1200     if(if_description_lb != NULL)       gtk_widget_set_sensitive(if_description_lb,en);
1201     if(channel_lb != NULL)                      gtk_widget_set_sensitive(channel_lb,en);
1202     if(toolbar_channel_cb != NULL)              gtk_widget_set_sensitive(toolbar_channel_cb,en);
1203     if(channel_offset_cb != NULL)       gtk_widget_set_sensitive(channel_offset_cb,en);
1204     if(channel_offset_lb != NULL)       gtk_widget_set_sensitive(channel_offset_lb,en);
1205     if(fcs_lb != NULL)                          gtk_widget_set_sensitive(fcs_lb,en);
1206     if(fcs_cb != NULL)                          gtk_widget_set_sensitive(fcs_cb,en);
1207     if(advanced_bt != NULL)                     gtk_widget_set_sensitive(advanced_bt,en);
1208
1209     return;
1210 }
1211
1212 #endif /* HAVE_AIRPCAP */
1213
1214 /*
1215  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
1216  *
1217  * Local variables:
1218  * c-basic-offset: 4
1219  * tab-width: 4
1220  * indent-tabs-mode: nil
1221  * End:
1222  *
1223  * vi: set shiftwidth=4 tabstop=4 expandtab
1224  * :indentSize=4:tabSize=4:noTabs=true:
1225  */