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