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