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