Remove a phrase which implies that tab widths other than 8 are OK.
[metze/wireshark/wip.git] / airpcap_loader.c
1 /* airpcap_loader.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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25  */
26
27 #include "config.h"
28
29 #ifdef HAVE_AIRPCAP
30
31 #ifdef HAVE_LIBPCAP
32 #include <glib.h>
33 #include <gmodule.h>
34
35
36 #include <wtap.h>
37 #include <pcap.h>
38 #endif
39
40 #include <epan/packet.h>
41 #include <epan/prefs.h>
42 #include <epan/prefs-int.h>
43 #include <epan/uat-int.h>
44 #include <epan/dissectors/packet-ieee80211.h>
45 #include <epan/crypt/wep-wpadefs.h>
46 #include <epan/crypt/airpdcap_ws.h>
47 #include <epan/strutil.h>
48 #include <epan/frequency-utils.h>
49 #include "capture_ui_utils.h"
50 #include <wsutil/file_util.h>
51
52 #include "ui/simple_dialog.h"
53
54 #include <airpcap.h>
55 #include "airpcap_loader.h"
56
57
58 /*
59  * Set to TRUE if the DLL was successfully loaded AND all functions
60  * are present.
61  */
62 static gboolean AirpcapLoaded = FALSE;
63
64 #ifdef _WIN32
65 /*
66  * We load dynamically the dag library in order link it only when
67  * it's present on the system
68  */
69 static void * AirpcapLib = NULL;
70
71 static AirpcapGetLastErrorHandler g_PAirpcapGetLastError;
72 static AirpcapSetKernelBufferHandler g_PAirpcapSetKernelBuffer;
73 static AirpcapSetFilterHandler g_PAirpcapSetFilter;
74 static AirpcapGetMacAddressHandler g_PAirpcapGetMacAddress;
75 static AirpcapSetMinToCopyHandler g_PAirpcapSetMinToCopy;
76 static AirpcapGetReadEventHandler g_PAirpcapGetReadEvent;
77 static AirpcapReadHandler g_PAirpcapRead;
78 static AirpcapGetStatsHandler g_PAirpcapGetStats;
79 #endif
80
81 static int AirpcapVersion = 3;
82
83 static AirpcapGetDeviceListHandler g_PAirpcapGetDeviceList;
84 static AirpcapFreeDeviceListHandler g_PAirpcapFreeDeviceList;
85 static AirpcapOpenHandler g_PAirpcapOpen;
86 static AirpcapCloseHandler g_PAirpcapClose;
87 static AirpcapGetLinkTypeHandler g_PAirpcapGetLinkType;
88 static AirpcapSetLinkTypeHandler g_PAirpcapSetLinkType;
89 static AirpcapTurnLedOnHandler g_PAirpcapTurnLedOn;
90 static AirpcapTurnLedOffHandler g_PAirpcapTurnLedOff;
91 static AirpcapGetDeviceChannelHandler g_PAirpcapGetDeviceChannel;
92 static AirpcapSetDeviceChannelHandler g_PAirpcapSetDeviceChannel;
93 static AirpcapGetFcsPresenceHandler g_PAirpcapGetFcsPresence;
94 static AirpcapSetFcsPresenceHandler g_PAirpcapSetFcsPresence;
95 static AirpcapGetFcsValidationHandler g_PAirpcapGetFcsValidation;
96 static AirpcapSetFcsValidationHandler g_PAirpcapSetFcsValidation;
97 static AirpcapGetDeviceKeysHandler g_PAirpcapGetDeviceKeys;
98 static AirpcapSetDeviceKeysHandler g_PAirpcapSetDeviceKeys;
99 static AirpcapGetDriverKeysHandler g_PAirpcapGetDriverKeys;
100 static AirpcapSetDriverKeysHandler g_PAirpcapSetDriverKeys;
101 static AirpcapGetDecryptionStateHandler g_PAirpcapGetDecryptionState;
102 static AirpcapSetDecryptionStateHandler g_PAirpcapSetDecryptionState;
103 static AirpcapGetDriverDecryptionStateHandler g_PAirpcapGetDriverDecryptionState;
104 static AirpcapSetDriverDecryptionStateHandler g_PAirpcapSetDriverDecryptionState;
105 static AirpcapStoreCurConfigAsAdapterDefaultHandler g_PAirpcapStoreCurConfigAsAdapterDefault;
106 static AirpcapGetVersionHandler g_PAirpcapGetVersion;
107 static AirpcapSetDeviceChannelExHandler g_PAirpcapSetDeviceChannelEx;
108 static AirpcapGetDeviceChannelExHandler g_PAirpcapGetDeviceChannelEx;
109 static AirpcapGetDeviceSupportedChannelsHandler g_PAirpcapGetDeviceSupportedChannels;
110
111 /* Airpcap interface list */
112 GList *airpcap_if_list = NULL;
113
114 /* Airpcap current selected interface */
115 airpcap_if_info_t *airpcap_if_selected = NULL;
116
117 /* Airpcap current active interface */
118 airpcap_if_info_t *airpcap_if_active = NULL;
119
120 /* WLAN preferences pointer */
121 module_t *wlan_prefs = NULL;
122
123 Dot11Channel *pSupportedChannels;
124 guint numSupportedChannels;
125
126 static AirpcapChannelInfo LegacyChannels[] =
127 {
128     {2412, 0, {0,0,0}},
129     {2417, 0, {0,0,0}},
130     {2422, 0, {0,0,0}},
131     {2427, 0, {0,0,0}},
132     {2432, 0, {0,0,0}},
133     {2437, 0, {0,0,0}},
134     {2442, 0, {0,0,0}},
135     {2447, 0, {0,0,0}},
136     {2452, 0, {0,0,0}},
137     {2457, 0, {0,0,0}},
138     {2462, 0, {0,0,0}},
139     {2467, 0, {0,0,0}},
140     {2472, 0, {0,0,0}},
141     {2484, 0, {0,0,0}},
142 };
143
144 static guint num_legacy_channels = 14;
145
146 /*
147  * Callback used by the load_wlan_keys() routine in order to read a WEP decryption key
148  */
149 static guint
150 get_wep_key(pref_t *pref, gpointer ud)
151 {
152     gchar *key_string = NULL;
153     guint8 key_type = AIRPDCAP_KEY_TYPE_WEP;
154     keys_cb_data_t* user_data;
155     uat_t *uat;
156     guint i;
157     const char* err = NULL;
158     uat_wep_key_record_t* wep_keys;
159     decryption_key_t* new_key;
160
161     /* Retrieve user data info */
162     user_data = (keys_cb_data_t*)ud;
163
164     if (g_ascii_strcasecmp(pref->name, "wep_key_table") == 0 && pref->type == PREF_UAT)
165     {
166         uat = pref->varp.uat;
167         /* This is just a sanity check.  UAT should be loaded */
168         if (!uat->loaded)
169         {
170             uat_load(uat, &err);
171             if (err != NULL)
172                 return 1;
173         }
174
175         for (i = 0, wep_keys = (uat_wep_key_record_t*)*uat->user_ptr; i < *uat->nrows_p; i++, wep_keys++)
176         {
177             /* strip out key type if present */
178             if (g_ascii_strncasecmp(wep_keys->string, STRING_KEY_TYPE_WEP ":", 4) == 0) {
179                 key_type = AIRPDCAP_KEY_TYPE_WEP;
180                 key_string = (gchar*)wep_keys->string+4;
181             }
182             else if (g_ascii_strncasecmp(wep_keys->string, STRING_KEY_TYPE_WPA_PWD ":", 8) == 0) {
183                 key_string = (gchar*)wep_keys->string+8;
184                 key_type = AIRPDCAP_KEY_TYPE_WPA_PWD;
185             }
186             else if (g_ascii_strncasecmp(wep_keys->string, STRING_KEY_TYPE_WPA_PSK ":", 8) == 0) {
187                 key_string = (gchar*)wep_keys->string+8;
188                 key_type = AIRPDCAP_KEY_TYPE_WPA_PSK;
189             }
190             else {
191                 key_type = wep_keys->key;
192                 key_string = (gchar*)wep_keys->string;
193             }
194
195             /* Here we have the string describing the key... */
196             new_key = parse_key_string(key_string, key_type);
197
198             if (new_key != NULL)
199             {
200                 /* Key is added only if not null ... */
201                 user_data->list = g_list_append(user_data->list,new_key);
202                 user_data->number_of_keys++;
203                 user_data->current_index++;
204             }
205         }
206     }
207     return 0;
208 }
209
210 /* Returs TRUE if the WEP key is valid, false otherwise */
211 gboolean
212 wep_key_is_valid(char* key)
213 {
214     GString *new_key_string;
215     guint i=0;
216
217     if (key == NULL)
218         return FALSE;
219
220     new_key_string = g_string_new(key);
221
222     if ( ((new_key_string->len) > WEP_KEY_MAX_CHAR_SIZE) || ((new_key_string->len) < 2))
223     {
224         g_string_free(new_key_string,FALSE);
225         return FALSE;
226     }
227     if ((new_key_string->len % 2) != 0)
228     {
229         g_string_free(new_key_string,FALSE);
230         return FALSE;
231     }
232     for(i = 0; i < new_key_string->len; i++)
233     {
234         if (!g_ascii_isxdigit(new_key_string->str[i]))
235         {
236             g_string_free(new_key_string,FALSE);
237             return FALSE;
238         }
239     }
240
241     g_string_free(new_key_string,FALSE);
242     return TRUE;
243 }
244
245 /* Callback used by the save_wlan_keys() routine in order to write a decryption key */
246 static guint
247 set_wep_key(pref_t *pref, gpointer ud _U_)
248 {
249     keys_cb_data_t*  user_data;
250     uat_t *uat;
251     gint i;
252     const char* err = NULL;
253     uat_wep_key_record_t uat_key;
254
255     decryption_key_t* new_key;
256
257     /* Retrieve user data info */
258     user_data = (keys_cb_data_t*)ud;
259
260     if (g_ascii_strcasecmp(pref->name, "wep_key_table") == 0 && pref->type == PREF_UAT)
261     {
262         uat = pref->varp.uat;
263         if (!uat->loaded)
264         {
265             /* UAT will only be loaded if previous keys exist, so it may need
266                to be loaded now */
267             uat_load(uat, &err);
268             if (err != NULL)
269                 return 1;
270
271             uat->loaded = 1;
272         }
273         /* Free the old records */
274         uat_clear(uat);
275
276         for (i = 0; i < user_data->number_of_keys; i++)
277         {
278             new_key = (decryption_key_t*)g_list_nth_data(user_data->list,i);
279
280             uat_key.string = get_key_string(new_key);
281             uat_key.key = new_key->type;
282             uat_add_record(uat, &uat_key, TRUE);
283         }
284
285         uat_save(uat, &err);
286         if (err != NULL)
287             return 1;
288     }
289
290     return 0;
291 }
292
293 /*
294  * Function used to read the Decryption Keys from the preferences and store them
295  * properly into the airpcap adapter.
296  */
297 gboolean
298 load_wlan_driver_wep_keys(void)
299 {
300     keys_cb_data_t* user_data;
301     guint i;
302
303     /* Retrieve the wlan preferences */
304     wlan_prefs = prefs_find_module("wlan");
305
306     /* Allocate a structure used to keep infos  between the callbacks */
307     user_data = (keys_cb_data_t*)g_malloc(sizeof(keys_cb_data_t));
308
309     /* Fill the structure */
310     user_data->list = NULL;
311     user_data->current_index = 0;
312     user_data->number_of_keys= 0; /* Still unknown */
313
314     /* Run the callback on each 802.11 preference */
315     prefs_pref_foreach(wlan_prefs, get_wep_key, (gpointer)user_data);
316
317     /* Now the key list should be filled */
318
319     /*
320      * Signal that we've changed things, and run the 802.11 dissector's
321      * callback
322      */
323     wlan_prefs->prefs_changed = TRUE;
324
325     prefs_apply(wlan_prefs);
326
327     write_wlan_driver_wep_keys_to_registry(user_data->list);
328
329     /* FREE MEMORY */
330     /* free the WEP key string */
331     for(i=0;i<g_list_length(user_data->list);i++)
332     {
333         g_free(g_list_nth(user_data->list,i)->data);
334     }
335
336     /* free the (empty) list */
337     g_list_free(user_data->list);
338
339     /* free the user_data structure */
340     g_free(user_data);
341
342     /* airpcap_if_info_free(fake_info_if); */
343
344     return TRUE;
345 }
346
347 /*
348  * This function will tell the airpcap driver the key list to use
349  * This will be stored into the registry...
350  */
351 gboolean
352 write_wlan_wep_keys_to_registry(airpcap_if_info_t* info_if, GList* key_list)
353 {
354     guint i,j;
355     GString *new_key;
356     gchar s[3];
357     PAirpcapKeysCollection KeysCollection;
358     guint KeysCollectionSize;
359     guint8 KeyByte;
360     guint keys_in_list = 0;
361     decryption_key_t* key_item = NULL;
362
363     keys_in_list = g_list_length(key_list);
364
365     /*
366      * Calculate the size of the keys collection
367      */
368     KeysCollectionSize = (guint)AirpcapKeysCollectionSize(keys_in_list);
369
370     /*
371      * Allocate the collection
372      */
373     KeysCollection = (PAirpcapKeysCollection)g_malloc(KeysCollectionSize);
374     if (!KeysCollection)
375     {
376         return FALSE;
377     }
378
379     /*
380      * Populate the key collection
381      */
382     KeysCollection->nKeys = keys_in_list;
383
384     for(i = 0; i < keys_in_list; i++)
385     {
386         KeysCollection->Keys[i].KeyType = AIRPDCAP_KEY_TYPE_WEP;
387
388         /* Retrieve the Item corresponding to the i-th key */
389         key_item = (decryption_key_t*)g_list_nth_data(key_list,i);
390         new_key = g_string_new(key_item->key->str);
391
392         KeysCollection->Keys[i].KeyLen = (guint) new_key->len / 2;
393         memset(&KeysCollection->Keys[i].KeyData, 0, sizeof(KeysCollection->Keys[i].KeyData));
394
395         for(j = 0 ; j < new_key->len; j += 2)
396         {
397             s[0] = new_key->str[j];
398             s[1] = new_key->str[j+1];
399             s[2] = '\0';
400             KeyByte = (guint8)strtol(s, NULL, 16);
401             KeysCollection->Keys[i].KeyData[j / 2] = KeyByte;
402         }
403
404         g_string_free(new_key,TRUE);
405
406     }
407     /*
408      * Free the old adapter key collection!
409      */
410     if (info_if->keysCollection != NULL)
411         g_free(info_if->keysCollection);
412
413     /*
414      * Set this collection ad the new one
415      */
416     info_if->keysCollection = KeysCollection;
417     info_if->keysCollectionSize = KeysCollectionSize;
418
419     /*
420      * Configuration must be saved
421      */
422     info_if->saved = FALSE;
423
424     /*
425      * Write down the changes to the registry
426      */
427     airpcap_save_selected_if_configuration(info_if);
428
429     return TRUE;
430 }
431
432 /*
433  * This function will tell the airpcap driver the key list to use
434  * This will be stored into the registry...
435  */
436 gboolean
437 write_wlan_driver_wep_keys_to_registry(GList* key_list)
438 {
439     guint                   i,j,k,n,y;
440     GString                *new_key;
441     gchar                   s[3];
442     PAirpcapKeysCollection  KeysCollection;
443     guint                   KeysCollectionSize;
444     guint8                  KeyByte;
445     guint                   keys_in_list = 0;
446     decryption_key_t*       key_item     = NULL;
447     airpcap_if_info_t*      fake_info_if = NULL;
448
449     /* Create the fake_info_if from the first adapter of the list */
450     fake_info_if = airpcap_driver_fake_if_info_new();
451
452     if (fake_info_if == NULL)
453         return FALSE;
454
455     /*
456      * XXX - When WPA will be supported, change this to: keys_in_list = g_list_length(key_list);
457      * but right now we will have to count only the WEP keys (or we will have a malloc-mess :-) )
458      */
459     n = g_list_length(key_list);
460     for(k = 0; k < n; k++ )
461         if (((decryption_key_t*)g_list_nth_data(key_list,k))->type == AIRPDCAP_KEY_TYPE_WEP)
462             keys_in_list++;
463
464     /*
465      * Calculate the size of the keys collection
466      */
467     KeysCollectionSize = (guint)AirpcapKeysCollectionSize(keys_in_list);
468
469     /*
470      * Allocate the collection
471      */
472     KeysCollection = (PAirpcapKeysCollection)g_malloc(KeysCollectionSize);
473     if (!KeysCollection)
474     {
475         return FALSE;
476     }
477
478     /*
479      * Populate the key collection
480      */
481     KeysCollection->nKeys = keys_in_list;
482
483     /*
484      * XXX - If we have, let's say, six keys, the first three are WEP, then two are WPA, and the
485      * last is WEP, we have to scroll the whole list (n) but increment the array counter only
486      * when a WEP key is found (y) .. When WPA will be supported by the driver, I'll have to change
487      * this
488      */
489     y = 0; /* Current position in the key list */
490
491     for(i = 0; i < n; i++)
492     {
493         /* Retrieve the Item corresponding to the i-th key */
494         key_item = (decryption_key_t*)g_list_nth_data(key_list,i);
495
496         /*
497          * XXX - The AIRPDCAP_KEY_TYPE_WEP is the only supported right now!
498          * We will have to modify the AirpcapKey structure in order to
499          * support the other two types! What happens now, is that simply the
500          * not supported keys will just be discarded (they will be saved in Wireshark though)
501          */
502         if (key_item->type == AIRPDCAP_KEY_TYPE_WEP)
503         {
504             KeysCollection->Keys[y].KeyType = AIRPDCAP_KEY_TYPE_WEP;
505
506             new_key = g_string_new(key_item->key->str);
507
508             KeysCollection->Keys[y].KeyLen = (guint) new_key->len / 2;
509             memset(&KeysCollection->Keys[y].KeyData, 0, sizeof(KeysCollection->Keys[y].KeyData));
510
511             for(j = 0 ; j < new_key->len; j += 2)
512             {
513                 s[0] = new_key->str[j];
514                 s[1] = new_key->str[j+1];
515                 s[2] = '\0';
516                 KeyByte = (guint8)strtol(s, NULL, 16);
517                 KeysCollection->Keys[y].KeyData[j / 2] = KeyByte;
518             }
519             /* XXX - Change when WPA will be supported!!! */
520             y++;
521             g_string_free(new_key,TRUE);
522         }
523         else if (key_item->type == AIRPDCAP_KEY_TYPE_WPA_PWD)
524         {
525             /* XXX - The driver cannot deal with this kind of key yet... */
526         }
527         else if (key_item->type == AIRPDCAP_KEY_TYPE_WPA_PMK)
528         {
529             /* XXX - The driver cannot deal with this kind of key yet... */
530         }
531     }
532
533     /*
534      * Free the old adapter key collection!
535      */
536     if (fake_info_if->keysCollection != NULL)
537         g_free(fake_info_if->keysCollection);
538
539     /*
540      * Set this collection ad the new one
541      */
542     fake_info_if->keysCollection = KeysCollection;
543     fake_info_if->keysCollectionSize = KeysCollectionSize;
544
545     /*
546      * Configuration must be saved
547      */
548     fake_info_if->saved = FALSE;
549
550     /*
551      * Write down the changes to the registry
552      */
553     airpcap_save_driver_if_configuration(fake_info_if);
554
555     airpcap_if_info_free(fake_info_if);
556
557     return TRUE;
558 }
559
560 /*
561  *  Function used to save to the preference file the Decryption Keys.
562  */
563 int
564 save_wlan_driver_wep_keys(void)
565 {
566     GList*             key_list     = NULL;
567     char*              tmp_key      = NULL;
568     guint              keys_in_list,i;
569     keys_cb_data_t*    user_data;
570     airpcap_if_info_t* fake_info_if = NULL;
571
572     /* Create the fake_info_if from the first adapter of the list */
573     fake_info_if = airpcap_driver_fake_if_info_new();
574
575     if (fake_info_if == NULL)
576         return 0;
577
578     /* Retrieve the wlan preferences */
579     wlan_prefs = prefs_find_module("wlan");
580
581     /* Allocate a structure used to keep infos  between the callbacks */
582     user_data = (keys_cb_data_t*)g_malloc(sizeof(keys_cb_data_t));
583
584     /* Number of keys in key list */
585     if (fake_info_if->keysCollectionSize != 0)
586         keys_in_list = AirpcapKeysCollectionSizeToKeyCount(fake_info_if->keysCollectionSize);
587     else
588         keys_in_list = 0;
589
590     for(i=0; i<keys_in_list; i++)
591     {
592     /* Only if it is a WEP key... */
593         if (fake_info_if->keysCollection->Keys[i].KeyType == AIRPDCAP_KEY_TYPE_WEP)
594         {
595             tmp_key = airpcap_get_key_string(fake_info_if->keysCollection->Keys[i]);
596             key_list = g_list_append(key_list,g_strdup(tmp_key));
597             g_free(tmp_key);
598         }
599     }
600
601     /* Now we know the exact number of WEP keys in the list, so store it ... */
602     keys_in_list = g_list_length(key_list);
603
604     /* Fill the structure */
605     user_data->list = key_list;
606     user_data->current_index = 0;
607     user_data->number_of_keys= keys_in_list;
608
609     /* Retrieve the wlan preferences */
610     wlan_prefs = prefs_find_module("wlan");
611
612     /* Run the callback on each 802.11 preference */
613     prefs_pref_foreach(wlan_prefs, set_wep_key,  (gpointer)user_data);
614
615     /* Signal that we've changed things, and run the 802.11 dissector's
616      * callback */
617     wlan_prefs->prefs_changed = TRUE;
618
619     /* Apply changes for the specified preference */
620     prefs_apply(wlan_prefs);
621
622     /* FREE MEMORY */
623     /* free the WEP key string */
624     for(i=0;i<g_list_length(user_data->list);i++)
625     {
626         g_free(g_list_nth(user_data->list,i)->data);
627     }
628
629     /* free the (empty) list */
630     g_list_free(user_data->list);
631
632     /* free the user_data structure */
633     g_free(user_data);
634
635     airpcap_if_info_free(fake_info_if);
636
637     return keys_in_list;
638 }
639
640 /*
641  *  Function used to save to the preference file the Decryption Keys.
642  */
643 int
644 save_wlan_wireshark_wep_keys(GList* key_ls)
645 {
646     GList* key_list = NULL;
647     guint keys_in_list,i;
648     keys_cb_data_t* user_data;
649     decryption_key_t* tmp_dk;
650
651     /* Retrieve the wlan preferences */
652     wlan_prefs = prefs_find_module("wlan");
653
654     /* Allocate a structure used to keep infos  between the callbacks */
655     user_data = (keys_cb_data_t*)g_malloc(sizeof(keys_cb_data_t));
656
657     keys_in_list = g_list_length(key_ls);
658
659     key_list = key_ls;
660
661     /* Fill the structure */
662     user_data->list = key_list;
663     user_data->current_index = 0;
664     user_data->number_of_keys= keys_in_list;
665
666     /* Retrieve the wlan preferences */
667     wlan_prefs = prefs_find_module("wlan");
668
669     /* Run the callback on each 802.11 preference */
670     prefs_pref_foreach(wlan_prefs, set_wep_key,  (gpointer)user_data);
671
672     /* Signal that we've changed things, and run the 802.11 dissector's
673      * callback */
674     wlan_prefs->prefs_changed = TRUE;
675
676     /* Apply changes for the specified preference */
677     prefs_apply(wlan_prefs);
678
679     /* FREE MEMORY */
680     /* free the WEP key string */
681     for(i=0;i<g_list_length(user_data->list);i++)
682     {
683         tmp_dk = (decryption_key_t*)g_list_nth(user_data->list,i)->data;
684         g_string_free(tmp_dk->key,TRUE);
685         if (tmp_dk->ssid != NULL) g_byte_array_free(tmp_dk->ssid,TRUE);
686     }
687
688     /* free the (empty) list */
689     g_list_free(user_data->list);
690
691     /* free the user_data structure */
692     g_free(user_data);
693
694     return keys_in_list;
695 }
696
697 /*
698  * Get an error message string for a CANT_GET_INTERFACE_LIST error from
699  * "get_airpcap_interface_list()".
700  */
701 static gchar *
702 cant_get_airpcap_if_list_error_message(const char *err_str)
703 {
704     return g_strdup_printf("Can't get list of Wireless interfaces: %s", err_str);
705 }
706
707 /*
708  * Airpcap wrapper, used to store the current settings for the selected adapter
709  */
710 gboolean
711 airpcap_if_store_cur_config_as_adapter_default(PAirpcapHandle ah)
712 {
713     if (!AirpcapLoaded) return FALSE;
714     return g_PAirpcapStoreCurConfigAsAdapterDefault(ah);
715 }
716
717 /*
718  * Airpcap wrapper, used to open an airpcap adapter
719  */
720 PAirpcapHandle
721 airpcap_if_open(gchar * name, gchar * err)
722 {
723     if (!AirpcapLoaded) return NULL;
724     if (name == NULL) return NULL;
725     return g_PAirpcapOpen(name,err);
726 }
727
728 /*
729  * Airpcap wrapper, used to close an airpcap adapter
730  */
731 void
732 airpcap_if_close(PAirpcapHandle handle)
733 {
734     if (!AirpcapLoaded) return;
735     g_PAirpcapClose(handle);
736 }
737
738 /*
739  * Retrieve the state of the Airpcap DLL
740  */
741 int
742 airpcap_get_dll_state(void)
743 {
744     return AirpcapVersion;
745 }
746
747 /*
748  * Airpcap wrapper, used to turn on the led of an airpcap adapter
749  */
750 gboolean
751 airpcap_if_turn_led_on(PAirpcapHandle AdapterHandle, guint LedNumber)
752 {
753     if (!AirpcapLoaded) return FALSE;
754     return g_PAirpcapTurnLedOn(AdapterHandle,LedNumber);
755 }
756
757 /*
758  * Airpcap wrapper, used to turn off the led of an airpcap adapter
759  */
760 gboolean
761 airpcap_if_turn_led_off(PAirpcapHandle AdapterHandle, guint LedNumber)
762 {
763     if (!AirpcapLoaded) return FALSE;
764     return g_PAirpcapTurnLedOff(AdapterHandle,LedNumber);
765 }
766
767 /*
768  * Airpcap wrapper, used to get the channel of an airpcap adapter
769  */
770 gboolean
771 airpcap_if_get_device_channel(PAirpcapHandle ah, guint * ch)
772 {
773     if (!AirpcapLoaded) return FALSE;
774     return g_PAirpcapGetDeviceChannel(ah,ch);
775 }
776
777 /*
778  * Airpcap wrapper, used to get the supported channels of an airpcap adapter
779  */
780 gboolean
781 airpcap_if_get_device_supported_channels(PAirpcapHandle ah, AirpcapChannelInfo **cInfo, guint * nInfo)
782 {
783     if (!AirpcapLoaded) return FALSE;
784     if (airpcap_get_dll_state() == AIRPCAP_DLL_OLD) {
785         *nInfo = num_legacy_channels;
786         *cInfo = (AirpcapChannelInfo*)&LegacyChannels;
787
788         return TRUE;
789     } else if (airpcap_get_dll_state() == AIRPCAP_DLL_OK) {
790         return g_PAirpcapGetDeviceSupportedChannels(ah, cInfo, nInfo);
791     }
792     return FALSE;
793 }
794
795 /*
796  * Airpcap wrapper, used to get the supported channels of an airpcap adapter
797  */
798 Dot11Channel*
799 airpcap_if_get_device_supported_channels_array(PAirpcapHandle ah, guint * pNumSupportedChannels)
800 {
801     AirpcapChannelInfo *chanInfo;
802     guint i=0, j=0, numInfo = 0;
803
804     if (!AirpcapLoaded)
805         return NULL;
806     if (airpcap_if_get_device_supported_channels(ah, &chanInfo, &numInfo) == FALSE)
807         return NULL;
808     numSupportedChannels = 0;
809
810     /*
811      * allocate a bigger array
812      */
813     if (numInfo == 0)
814         return NULL;
815
816     pSupportedChannels = (Dot11Channel *)g_malloc(numInfo * (sizeof *pSupportedChannels));
817
818     for (i = 0; i < numInfo; i++)
819     {
820         guint supportedChannel = G_MAXUINT;
821
822         /*
823          * search if we have it already
824          */
825         for (j = 0; j < numSupportedChannels; j++)
826         {
827             if (pSupportedChannels[j].Frequency == chanInfo[i].Frequency)
828             {
829                 supportedChannel = j;
830                 break;
831             }
832         }
833
834         if (supportedChannel == G_MAXUINT)
835         {
836             /*
837              * not found, create a new item
838              */
839             pSupportedChannels[numSupportedChannels].Frequency = chanInfo[i].Frequency;
840
841             switch(chanInfo[i].ExtChannel)
842             {
843                 case -1:
844                     pSupportedChannels[numSupportedChannels].Flags = FLAG_CAN_BE_LOW;
845                     break;
846                 case +1:
847                     pSupportedChannels[numSupportedChannels].Flags = FLAG_CAN_BE_HIGH;
848                     break;
849                 case 0:
850                 default:
851                     pSupportedChannels[numSupportedChannels].Flags = 0;
852             }
853
854             /*
855              * Gather channel information
856              */
857
858             pSupportedChannels[numSupportedChannels].Flags |=
859                 FREQ_IS_BG(pSupportedChannels[numSupportedChannels].Frequency) ?
860                     FLAG_IS_BG_CHANNEL : FLAG_IS_A_CHANNEL;
861             pSupportedChannels[numSupportedChannels].Channel =
862                 ieee80211_mhz_to_chan(pSupportedChannels[numSupportedChannels].Frequency);
863             numSupportedChannels++;
864         }
865         else
866         {
867             /*
868              * just update the ext channel flags
869              */
870             switch(chanInfo[i].ExtChannel)
871             {
872                 case -1:
873                     pSupportedChannels[supportedChannel].Flags |= FLAG_CAN_BE_LOW;
874                     break;
875                 case +1:
876                     pSupportedChannels[supportedChannel].Flags |= FLAG_CAN_BE_HIGH;
877                     break;
878                 case 0:
879                 default:
880                     break;
881             }
882         }
883     }
884
885     if (numSupportedChannels < 1)
886         return NULL;
887     /*
888      * Now sort the list by frequency
889      */
890     for (i = 0 ; i < numSupportedChannels - 1; i++)
891     {
892         for (j = i + 1; j < numSupportedChannels; j++)
893         {
894             if (pSupportedChannels[i].Frequency > pSupportedChannels[j].Frequency)
895             {
896                 Dot11Channel temp = pSupportedChannels[i];
897                 pSupportedChannels[i] = pSupportedChannels[j];
898                 pSupportedChannels[j] = temp;
899             }
900         }
901     }
902
903     *pNumSupportedChannels = numSupportedChannels;
904     return pSupportedChannels;
905 }
906
907 /*
908  * Airpcap wrapper, used to set the channel of an airpcap adapter
909  */
910 gboolean
911 airpcap_if_set_device_channel(PAirpcapHandle ah, guint ch)
912 {
913     if (!AirpcapLoaded) return FALSE;
914     return g_PAirpcapSetDeviceChannel(ah,ch);
915 }
916
917 /*
918  * Airpcap wrapper, used to set the frequency of an airpcap adapter
919  */
920 gboolean
921 airpcap_if_set_device_channel_ex(PAirpcapHandle ah, AirpcapChannelInfo ChannelInfo)
922 {
923     if (!AirpcapLoaded) return FALSE;
924     if (airpcap_get_dll_state() == AIRPCAP_DLL_OLD){
925         gint channel = 0;
926         channel = ieee80211_mhz_to_chan(ChannelInfo.Frequency);
927
928         if (channel < 0){
929             return FALSE;
930         } else {
931             return airpcap_if_set_device_channel(ah, channel);
932         }
933     } else if (airpcap_get_dll_state() == AIRPCAP_DLL_OK){
934         return g_PAirpcapSetDeviceChannelEx (ah, ChannelInfo);
935     }
936
937     return FALSE;
938 }
939
940 /*
941  * Airpcap wrapper, used to get the frequency of an airpcap adapter
942  */
943 gboolean
944 airpcap_if_get_device_channel_ex(PAirpcapHandle ah, PAirpcapChannelInfo pChannelInfo)
945 {
946     if (!AirpcapLoaded) return FALSE;
947
948     pChannelInfo->Frequency = 0;
949     pChannelInfo->ExtChannel = 0;
950     pChannelInfo->Reserved[0] = 0;
951     pChannelInfo->Reserved[1] = 0;
952     pChannelInfo->Reserved[2] = 0;
953
954     if (airpcap_get_dll_state() == AIRPCAP_DLL_OLD){
955         guint channel = 0;
956         guint chan_freq = 0;
957
958         if (!airpcap_if_get_device_channel(ah, &channel)) return FALSE;
959
960         chan_freq = ieee80211_chan_to_mhz(channel, TRUE);
961         if (chan_freq == 0) return FALSE;
962         pChannelInfo->Frequency = chan_freq;
963
964         return TRUE;
965     } else if (airpcap_get_dll_state() == AIRPCAP_DLL_OK){
966         return g_PAirpcapGetDeviceChannelEx (ah, pChannelInfo);
967     }
968     return FALSE;
969 }
970
971 /*
972  * Airpcap wrapper, used to get the link type of an airpcap adapter
973  */
974 gboolean
975 airpcap_if_get_link_type(PAirpcapHandle ah, PAirpcapLinkType lt)
976 {
977     if (!AirpcapLoaded) return FALSE;
978     return g_PAirpcapGetLinkType(ah,lt);
979 }
980
981 /*
982  * Airpcap wrapper, used to set the link type of an airpcap adapter
983  */
984 gboolean
985 airpcap_if_set_link_type(PAirpcapHandle ah, AirpcapLinkType lt)
986 {
987     if (!AirpcapLoaded) return FALSE;
988     return g_PAirpcapSetLinkType(ah,lt);
989 }
990
991 /*
992  * Airpcap wrapper, used to get the fcs presence of an airpcap adapter
993  */
994 gboolean
995 airpcap_if_get_fcs_presence(PAirpcapHandle ah, gboolean * fcs)
996 {
997     if (!AirpcapLoaded) return FALSE;
998     return g_PAirpcapGetFcsPresence(ah,fcs);
999 }
1000
1001 /*
1002  * Airpcap wrapper, used to set the fcs presence of an airpcap adapter
1003  */
1004 gboolean
1005 airpcap_if_set_fcs_presence(PAirpcapHandle ah, gboolean fcs)
1006 {
1007     if (!AirpcapLoaded) return FALSE;
1008     return g_PAirpcapSetFcsPresence(ah,fcs);
1009 }
1010
1011 /*
1012  * Airpcap wrapper, used to get the decryption enabling of an airpcap adapter
1013  */
1014 gboolean
1015 airpcap_if_get_decryption_state(PAirpcapHandle ah, PAirpcapDecryptionState PEnable)
1016 {
1017     if (!AirpcapLoaded) return FALSE;
1018     return g_PAirpcapGetDecryptionState(ah,PEnable);
1019 }
1020
1021 /*
1022  * Airpcap wrapper, used to set the decryption enabling of an airpcap adapter
1023  */
1024 gboolean
1025 airpcap_if_set_decryption_state(PAirpcapHandle ah, AirpcapDecryptionState Enable)
1026 {
1027     if (!AirpcapLoaded) return FALSE;
1028     return g_PAirpcapSetDecryptionState(ah,Enable);
1029 }
1030
1031 /*
1032  * Airpcap wrapper, used to get the decryption enabling of an airpcap driver
1033  */
1034 gboolean
1035 airpcap_if_get_driver_decryption_state(PAirpcapHandle ah, PAirpcapDecryptionState PEnable)
1036 {
1037     if (!AirpcapLoaded || (g_PAirpcapGetDriverDecryptionState==NULL)) return FALSE;
1038     return g_PAirpcapGetDriverDecryptionState(ah,PEnable);
1039 }
1040
1041 /*
1042  * Airpcap wrapper, used to set the decryption enabling of an airpcap driver
1043  */
1044 gboolean
1045 airpcap_if_set_driver_decryption_state(PAirpcapHandle ah, AirpcapDecryptionState Enable)
1046 {
1047     if (!AirpcapLoaded || (g_PAirpcapSetDriverDecryptionState==NULL)) return FALSE;
1048     return g_PAirpcapSetDriverDecryptionState(ah,Enable);
1049 }
1050
1051 /*
1052  * Airpcap wrapper, used to get the fcs validation of an airpcap adapter
1053  */
1054 gboolean
1055 airpcap_if_get_fcs_validation(PAirpcapHandle ah, PAirpcapValidationType val)
1056 {
1057     if (!AirpcapLoaded) return FALSE;
1058     return g_PAirpcapGetFcsValidation(ah,val);
1059 }
1060
1061 /*
1062  * Airpcap wrapper, used to set the fcs validation of an airpcap adapter
1063  */
1064 gboolean
1065 airpcap_if_set_fcs_validation(PAirpcapHandle ah, AirpcapValidationType val)
1066 {
1067     if (!AirpcapLoaded) return FALSE;
1068     return g_PAirpcapSetFcsValidation(ah,val);
1069 }
1070
1071 /*
1072  * Airpcap wrapper, used to save the settings for the selected_if
1073  */
1074 gboolean
1075 airpcap_if_set_device_keys(PAirpcapHandle AdapterHandle, PAirpcapKeysCollection KeysCollection)
1076 {
1077     if (!AirpcapLoaded) return FALSE;
1078     return g_PAirpcapSetDeviceKeys(AdapterHandle,KeysCollection);
1079 }
1080
1081 /*
1082  * Airpcap wrapper, used to save the settings for the selected_if
1083  */
1084 gboolean
1085 airpcap_if_get_device_keys(PAirpcapHandle AdapterHandle, PAirpcapKeysCollection KeysCollection, guint * PKeysCollectionSize)
1086 {
1087     if (!AirpcapLoaded) return FALSE;
1088     return g_PAirpcapGetDeviceKeys(AdapterHandle,KeysCollection,PKeysCollectionSize);
1089 }
1090
1091 /*
1092  * Airpcap wrapper, used to save the driver's set of keys
1093  */
1094 gboolean
1095 airpcap_if_set_driver_keys(PAirpcapHandle AdapterHandle, PAirpcapKeysCollection KeysCollection)
1096 {
1097     if (!AirpcapLoaded || (g_PAirpcapSetDriverKeys==NULL)) return FALSE;
1098     return g_PAirpcapSetDriverKeys(AdapterHandle,KeysCollection);
1099 }
1100
1101 /*
1102  * Airpcap wrapper, used to load the driver's set of keys
1103  */
1104 gboolean
1105 airpcap_if_get_driver_keys(PAirpcapHandle AdapterHandle, PAirpcapKeysCollection KeysCollection, guint * PKeysCollectionSize)
1106 {
1107     if (!AirpcapLoaded || (g_PAirpcapGetDriverKeys==NULL)) return FALSE;
1108     return g_PAirpcapGetDriverKeys(AdapterHandle,KeysCollection,PKeysCollectionSize);
1109 }
1110
1111 /*
1112  * This function will create a new airpcap_if_info_t using a name and a description
1113  */
1114 airpcap_if_info_t *
1115 airpcap_if_info_new(char *name, char *description)
1116 {
1117     PAirpcapHandle ad;
1118     gchar ebuf[AIRPCAP_ERRBUF_SIZE];
1119
1120     airpcap_if_info_t *if_info = NULL;
1121
1122     /* Probably I have to switch on the leds!!! */
1123     ad = airpcap_if_open(name, ebuf);
1124     if (ad)
1125     {
1126         if_info = (airpcap_if_info_t *)g_malloc0(sizeof (airpcap_if_info_t));
1127         if_info->name = g_strdup(name);
1128         if (description == NULL){
1129             if_info->description = NULL;
1130         }else{
1131             if_info->description = g_strdup(description);
1132         }
1133
1134         if_info->ip_addr = NULL;
1135         if_info->loopback = FALSE;
1136         airpcap_if_get_fcs_validation(ad,&(if_info->CrcValidationOn));
1137         airpcap_if_get_fcs_presence(ad,&(if_info->IsFcsPresent));
1138         airpcap_if_get_link_type(ad,&(if_info->linkType));
1139         airpcap_if_get_device_channel_ex(ad,&(if_info->channelInfo));
1140         if_info->pSupportedChannels = airpcap_if_get_device_supported_channels_array(ad, &(if_info->numSupportedChannels));
1141         airpcap_if_turn_led_on(ad, 0);
1142         airpcap_if_get_decryption_state(ad, &(if_info->DecryptionOn));
1143         if_info->led = TRUE;
1144         if_info->blinking = FALSE;
1145         if_info->saved = TRUE; /* NO NEED TO BE SAVED */
1146
1147         /* get the keys, if everything is ok, close the adapter */
1148         if (airpcap_if_load_keys(ad,if_info))
1149         {
1150             airpcap_if_close(ad);
1151         }
1152     }
1153     return if_info;
1154 }
1155
1156 /*
1157  * This function will create a new fake drivers' interface, to load global keys...
1158  */
1159 airpcap_if_info_t*
1160 airpcap_driver_fake_if_info_new(void)
1161 {
1162     PAirpcapHandle ad;
1163     gchar ebuf[AIRPCAP_ERRBUF_SIZE];
1164
1165     airpcap_if_info_t *if_info = NULL;
1166     airpcap_if_info_t *fake_if_info = NULL;
1167
1168     /* Maybe for some reason no airpcap adapter is found */
1169     if (airpcap_if_list == NULL)
1170         return NULL;
1171
1172     /*
1173      * Retrieve the first AirPcap adapter available. If no interface is found,
1174      * it is not possible to retrieve the driver's settings, so return NULL.
1175      */
1176     if_info = (airpcap_if_info_t *)g_list_nth_data(airpcap_if_list,0);
1177     if (if_info == NULL)
1178         return NULL;
1179
1180     /* Open the 'fake' adapter */
1181     ad = airpcap_if_open(if_info->name, ebuf);
1182     if (ad)
1183     {
1184         fake_if_info = (airpcap_if_info_t *)g_malloc(sizeof (airpcap_if_info_t));
1185         fake_if_info->name = g_strdup(if_info->name);
1186         fake_if_info->description = g_strdup(if_info->description);
1187         fake_if_info->loopback = FALSE;
1188         fake_if_info->ip_addr = NULL;
1189         airpcap_if_get_driver_decryption_state(ad, &(fake_if_info->DecryptionOn));
1190         airpcap_if_get_fcs_validation(ad,&(fake_if_info->CrcValidationOn));
1191         airpcap_if_get_fcs_presence(ad,&(fake_if_info->IsFcsPresent));
1192         airpcap_if_get_link_type(ad,&(fake_if_info->linkType));
1193         airpcap_if_get_device_channel_ex(ad,&(fake_if_info->channelInfo));
1194         airpcap_if_turn_led_on(ad, 0);
1195         fake_if_info->led = TRUE;
1196         fake_if_info->blinking = FALSE;
1197         fake_if_info->saved = TRUE; /* NO NEED TO BE SAVED */
1198
1199         /* get the keys, if everything is ok, close the adapter */
1200         if (airpcap_if_load_driver_keys(ad,fake_if_info))
1201         {
1202             airpcap_if_close(ad);
1203         }
1204     }
1205
1206     return fake_if_info;
1207 }
1208
1209 /*
1210  * USED FOR DEBUG ONLY... PRINTS AN AirPcap ADAPTER STRUCTURE in a fancy way.
1211  */
1212 void
1213 airpcap_if_info_print(airpcap_if_info_t* if_info)
1214 {
1215     guint i;
1216     if (if_info == NULL)
1217     {
1218         g_print("\nWARNING : AirPcap Interface pointer is NULL!\n");
1219         return;
1220     }
1221
1222     g_print("\n----------------- AirPcap Interface \n");
1223     g_print("                      NAME: %s\n",if_info->name);
1224     g_print("               DESCRIPTION: %s\n",if_info->description);
1225     g_print("                  BLINKING: %s\n",if_info->blinking ? "TRUE" : "FALSE");
1226     g_print("     channelInfo.Frequency: %u\n",if_info->channelInfo.Frequency);
1227     g_print("    channelInfo.ExtChannel: %d\n",if_info->channelInfo.ExtChannel);
1228     g_print("             CRCVALIDATION: %s\n",if_info->CrcValidationOn ? "ON" : "OFF");
1229     g_print("                DECRYPTION: %s\n",if_info->DecryptionOn ? "ON" : "OFF");
1230     g_print("                   IP ADDR: %s\n",if_info->ip_addr!=NULL ? "NOT NULL" : "NULL");
1231     g_print("                FCSPRESENT: %s\n",if_info->IsFcsPresent ? "TRUE" : "FALSE");
1232     g_print("            KEYSCOLLECTION: %s\n",if_info->keysCollection!=NULL ? "NOT NULL" : "NULL");
1233     g_print("        KEYSCOLLECTIONSIZE: %u\n",if_info->keysCollectionSize);
1234     g_print("                       LED: %s\n",if_info->led ? "ON" : "OFF");
1235     g_print("                  LINKTYPE: %d\n",if_info->linkType);
1236     g_print("                  LOOPBACK: %s\n",if_info->loopback ? "YES" : "NO");
1237     g_print("                 (GTK) TAG: %d\n",if_info->tag);
1238     g_print("SUPPORTED CHANNELS POINTER: %p\n",if_info->pSupportedChannels);
1239     g_print("    NUM SUPPORTED CHANNELS: %u\n",if_info->numSupportedChannels);
1240
1241     for(i=0; i<(if_info->numSupportedChannels); i++){
1242         g_print("\n        SUPPORTED CHANNEL #%u\n",i+1);
1243         g_print("                   CHANNEL: %u\n",if_info->pSupportedChannels[i].Channel);
1244         g_print("                 FREQUENCY: %u\n",if_info->pSupportedChannels[i].Frequency);
1245         g_print("                     FLAGS: %u\n",if_info->pSupportedChannels[i].Flags);
1246     }
1247     g_print("\n\n");
1248 }
1249
1250 /*
1251  * Function used to load the WEP keys for a selected interface
1252  */
1253 gboolean
1254 airpcap_if_load_keys(PAirpcapHandle ad, airpcap_if_info_t *if_info)
1255 {
1256     if (!if_info) return FALSE;
1257
1258     if_info->keysCollectionSize = 0;
1259     if_info->keysCollection = NULL;
1260
1261     if (!airpcap_if_get_device_keys(ad, NULL, &(if_info->keysCollectionSize)))
1262     {
1263         if (if_info->keysCollectionSize == 0)
1264         {
1265             if_info->keysCollection = NULL;
1266             airpcap_if_close(ad);
1267             return FALSE;
1268         }
1269
1270         if_info->keysCollection = (PAirpcapKeysCollection)g_malloc(if_info->keysCollectionSize);
1271         if (!if_info->keysCollection)
1272         {
1273             if_info->keysCollectionSize = 0;
1274             if_info->keysCollection = NULL;
1275             airpcap_if_close(ad);
1276             return FALSE;
1277         }
1278
1279         airpcap_if_get_device_keys(ad, if_info->keysCollection, &(if_info->keysCollectionSize));
1280         return TRUE;
1281     }
1282
1283     airpcap_if_close(ad);
1284     return FALSE;
1285 }
1286
1287 /*
1288  * Function used to load the WEP keys for a selected interface
1289  */
1290 gboolean
1291 airpcap_if_load_driver_keys(PAirpcapHandle ad, airpcap_if_info_t *if_info)
1292 {
1293     if_info->keysCollectionSize = 0;
1294     if_info->keysCollection = NULL;
1295
1296     if (!airpcap_if_get_driver_keys(ad, NULL, &(if_info->keysCollectionSize)))
1297     {
1298         if (if_info->keysCollectionSize == 0)
1299         {
1300             if_info->keysCollection = NULL;
1301             airpcap_if_close(ad);
1302             return FALSE;
1303         }
1304
1305         if_info->keysCollection = (PAirpcapKeysCollection)g_malloc(if_info->keysCollectionSize);
1306         if (!if_info->keysCollection)
1307         {
1308             if_info->keysCollectionSize = 0;
1309             if_info->keysCollection = NULL;
1310             airpcap_if_close(ad);
1311             return FALSE;
1312         }
1313
1314         airpcap_if_get_driver_keys(ad, if_info->keysCollection, &(if_info->keysCollectionSize));
1315         return TRUE;
1316     }
1317
1318     airpcap_if_close(ad);
1319     return FALSE;
1320 }
1321
1322 /*
1323  * Function used to save the WEP keys for a selected interface
1324  */
1325 void
1326 airpcap_if_save_keys(PAirpcapHandle ad, airpcap_if_info_t *if_info)
1327 {
1328     if (!if_info || !AirpcapLoaded) return;
1329
1330     if (if_info->keysCollection != NULL)
1331         g_PAirpcapSetDeviceKeys(ad,if_info->keysCollection);
1332 }
1333
1334 /*
1335  * Function used to save the WEP keys for a selected interface
1336  */
1337 void
1338 airpcap_if_save_driver_keys(PAirpcapHandle ad, airpcap_if_info_t *if_info)
1339 {
1340     if (if_info->keysCollection != NULL)
1341         airpcap_if_set_driver_keys(ad,if_info->keysCollection);
1342 }
1343
1344 /*
1345  * Callback used to free an instance of airpcap_if_info_t
1346  */
1347 static void
1348 free_airpcap_if_cb(gpointer data, gpointer user_data _U_)
1349 {
1350     airpcap_if_info_t *if_info = (airpcap_if_info_t *)data;
1351
1352     if (NULL == if_info)
1353         return;
1354
1355     if (if_info->name != NULL)
1356         g_free(if_info->name);
1357
1358     if (if_info->description != NULL)
1359         g_free(if_info->description);
1360
1361     /* XXX - FREE THE WEP KEY LIST HERE!!!*/
1362     if (if_info->keysCollection != NULL)
1363     {
1364         g_free(if_info->keysCollection);
1365         if_info->keysCollection = NULL;
1366     }
1367
1368     if (if_info->ip_addr != NULL)
1369         g_slist_free(if_info->ip_addr);
1370
1371     g_free(if_info);
1372 }
1373
1374 /*
1375  * Function used to free the airpcap interface list
1376  */
1377 void
1378 free_airpcap_interface_list(GList *if_list)
1379 {
1380     g_list_foreach(if_list, free_airpcap_if_cb, NULL);
1381     g_list_free(if_list);
1382     if_list = NULL;
1383 }
1384
1385 /*
1386  * This function will use the airpcap.dll to find all the airpcap devices.
1387  * Will return null if no device is found.
1388  */
1389 GList*
1390 get_airpcap_interface_list(int *err, char **err_str)
1391 {
1392     GList  *il = NULL;
1393     airpcap_if_info_t *if_info;
1394     int n_adapts;
1395     AirpcapDeviceDescription *devsList, *adListEntry;
1396     char errbuf[AIRPCAP_ERRBUF_SIZE];
1397
1398     *err = 0;
1399
1400     if (!AirpcapLoaded)
1401     {
1402         *err = AIRPCAP_NOT_LOADED;
1403         return il;
1404     }
1405
1406     if (!g_PAirpcapGetDeviceList(&devsList, errbuf))
1407     {
1408         /* No interfaces, return il = NULL; */
1409         *err = CANT_GET_AIRPCAP_INTERFACE_LIST;
1410         if (err_str != NULL)
1411             *err_str = cant_get_airpcap_if_list_error_message(errbuf);
1412         return il;
1413     }
1414
1415     /*
1416      * Count the adapters
1417      */
1418     adListEntry = devsList;
1419     n_adapts = 0;
1420     while(adListEntry)
1421     {
1422         n_adapts++;
1423         adListEntry = adListEntry->next;
1424     }
1425
1426     if (n_adapts == 0)
1427     {
1428         /* No interfaces, return il= NULL */
1429         g_PAirpcapFreeDeviceList(devsList);
1430         *err = NO_AIRPCAP_INTERFACES_FOUND;
1431         if (err_str != NULL)
1432             *err_str = NULL;
1433         return il;
1434     }
1435
1436     /*
1437      * Insert the adapters in our list
1438      */
1439     adListEntry = devsList;
1440     while(adListEntry)
1441     {
1442         if_info = airpcap_if_info_new(adListEntry->Name, adListEntry->Description);
1443         if (if_info != NULL){
1444             il = g_list_append(il, if_info);
1445         }
1446
1447         adListEntry = adListEntry->next;
1448     }
1449
1450     g_PAirpcapFreeDeviceList(devsList);
1451
1452     return il;
1453 }
1454
1455 /*
1456  * Used to retrieve the interface given the name
1457  * (the name is used in AirpcapOpen)
1458  */
1459 airpcap_if_info_t* get_airpcap_if_from_name(GList* if_list, const gchar* name)
1460 {
1461     GList* curr;
1462     airpcap_if_info_t* if_info;
1463
1464     for (curr = g_list_first(if_list); curr; curr = g_list_next(curr)) {
1465         if_info = (airpcap_if_info_t *)curr->data;
1466         if (if_info && (g_ascii_strcasecmp(if_info->name, name) == 0)) {
1467             return (if_info);
1468         }
1469     }
1470     return (NULL);
1471 }
1472
1473 /*
1474  * Returns the ASCII string of a key given the key bytes
1475  */
1476 gchar*
1477 airpcap_get_key_string(AirpcapKey key)
1478 {
1479     unsigned int j = 0;
1480     gchar *dst,*src;
1481
1482     dst = NULL;
1483     src = NULL;
1484
1485     if (key.KeyType == AIRPDCAP_KEY_TYPE_WEP)
1486     {
1487         if (key.KeyLen != 0)
1488         {
1489             /* Allocate the string used to store the ASCII representation of the WEP key */
1490             dst = (gchar*)g_malloc(sizeof(gchar)*WEP_KEY_MAX_CHAR_SIZE + 1);
1491             /* Make sure that the first char is '\0' in order to make g_strlcat() work */
1492             dst[0]='\0';
1493
1494             for(j = 0; j < key.KeyLen; j++)
1495             {
1496                 src = g_strdup_printf("%.2x", key.KeyData[j]);
1497                 /*
1498                  * XXX - use g_strconcat() or GStrings instead ???
1499                  */
1500                 g_strlcat(dst, src, WEP_KEY_MAX_CHAR_SIZE+1);
1501             }
1502             g_free(src);
1503         }
1504     }
1505     else if (key.KeyType == AIRPDCAP_KEY_TYPE_WPA_PWD)
1506     {
1507         /* XXX - Add code here */
1508     }
1509     else if (key.KeyType == AIRPDCAP_KEY_TYPE_WPA_PMK)
1510     {
1511         /* XXX - Add code here */
1512     }
1513     else
1514     {
1515         /* XXX - Add code here */
1516     }
1517
1518     return dst;
1519 }
1520
1521 /*
1522  * Clear keys and decryption status for the specified interface
1523  */
1524 void
1525 airpcap_if_clear_decryption_settings(airpcap_if_info_t* info_if)
1526 {
1527     if (info_if != NULL)
1528     {
1529         if (info_if->keysCollection != NULL)
1530         {
1531             g_free(info_if->keysCollection);
1532             info_if->keysCollection = NULL;
1533         }
1534
1535         info_if->keysCollectionSize = 0;
1536
1537         info_if->DecryptionOn = AIRPCAP_DECRYPTION_OFF;
1538         info_if->saved = FALSE;
1539     }
1540 }
1541
1542 /*
1543  * Used to retrieve the two chars string from interface
1544  */
1545 gchar*
1546 airpcap_get_if_string_number(airpcap_if_info_t* if_info)
1547 {
1548     gchar* number;
1549     guint n;
1550     int a;
1551
1552     a = sscanf(if_info->name,AIRPCAP_DEVICE_NUMBER_EXTRACT_STRING,&n);
1553
1554     /* If sscanf() returned 1, it means that has read a number, so interface is not "Any"
1555      * Otherwise, check if it is the "Any" adapter...
1556      */
1557     if (a == 0)
1558     {
1559         if (g_ascii_strcasecmp(if_info->name,AIRPCAP_DEVICE_ANY_EXTRACT_STRING)!=0)
1560             number = g_strdup_printf("??");
1561         else
1562             number = g_strdup_printf(AIRPCAP_CHANNEL_ANY_NAME);
1563     }
1564     else
1565     {
1566         number = g_strdup_printf("%.2u",n);
1567     }
1568
1569     return number;
1570 }
1571
1572 /*
1573  * Used to retrieve the two chars string from interface
1574  */
1575 gchar*
1576 airpcap_get_if_string_number_from_description(gchar* description)
1577 {
1578     gchar* number;
1579     gchar* pointer;
1580
1581     number = (gchar*)g_malloc(sizeof(gchar)*3);
1582
1583     pointer = g_strrstr(description,"#\0");
1584
1585     number[0] = *(pointer+1);
1586     number[1] = *(pointer+2);
1587     number[2] = '\0';
1588
1589     return number;
1590 }
1591
1592 /*
1593  * Returns the default airpcap interface of a list, NULL if list is empty
1594  */
1595 airpcap_if_info_t*
1596 airpcap_get_default_if(GList* airpcap_if_list_p)
1597 {
1598     airpcap_if_info_t* if_info = NULL;
1599
1600     if ((prefs.capture_device != NULL) && (*prefs.capture_device != '\0'))
1601     {
1602         if_info = get_airpcap_if_from_name(airpcap_if_list_p,
1603                                            get_if_name(prefs.capture_device));
1604     }
1605     return if_info;
1606 }
1607
1608 /*
1609  * Load the configuration for the specified interface
1610  */
1611 void
1612 airpcap_load_selected_if_configuration(airpcap_if_info_t* if_info)
1613 {
1614     gchar ebuf[AIRPCAP_ERRBUF_SIZE];
1615     PAirpcapHandle ad;
1616
1617     if (if_info != NULL)
1618     {
1619         ad = airpcap_if_open(if_info->name, ebuf);
1620
1621         if (ad)
1622         {
1623             /* Stop blinking (if it was blinking!)*/
1624             if (if_info->blinking)
1625             {
1626                 /* Turn on the light (if it was off) */
1627                 if (!(if_info->led)) airpcap_if_turn_led_on(ad, 0);
1628             }
1629
1630             /* Apply settings... */
1631             airpcap_if_get_device_channel_ex(ad,&(if_info->channelInfo));
1632             airpcap_if_get_fcs_validation(ad,&(if_info->CrcValidationOn));
1633             airpcap_if_get_fcs_presence(ad,&(if_info->IsFcsPresent));
1634             airpcap_if_get_link_type(ad,&(if_info->linkType));
1635             airpcap_if_get_decryption_state(ad, &(if_info->DecryptionOn));
1636             /* get the keys, if everything is ok, close the adapter */
1637             if (airpcap_if_load_keys(ad,if_info))
1638                 airpcap_if_close(ad);
1639
1640             if_info->saved = TRUE;
1641         }
1642         else
1643         {
1644             simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, " Error in opening adapter for %s",if_info->description);
1645         }
1646     }
1647 }
1648
1649 /*
1650  * Save the configuration for the specified interface
1651  */
1652 void
1653 airpcap_save_selected_if_configuration(airpcap_if_info_t* if_info)
1654 {
1655     gchar ebuf[AIRPCAP_ERRBUF_SIZE];
1656     PAirpcapHandle ad;
1657
1658     if (if_info != NULL)
1659     {
1660         ad = airpcap_if_open(if_info->name, ebuf);
1661
1662         if (ad)
1663         {
1664             /* Stop blinking (if it was blinking!)*/
1665             if (if_info->blinking)
1666             {
1667                 /* Turn on the light (if it was off) */
1668                 if (!(if_info->led)) airpcap_if_turn_led_on(ad, 0);
1669             }
1670
1671             /* Apply settings... */
1672             airpcap_if_set_device_channel_ex(ad,if_info->channelInfo);
1673             airpcap_if_set_fcs_validation(ad,if_info->CrcValidationOn);
1674             airpcap_if_set_fcs_presence(ad,if_info->IsFcsPresent);
1675             airpcap_if_set_link_type(ad,if_info->linkType);
1676             airpcap_if_set_decryption_state(ad, if_info->DecryptionOn);
1677             airpcap_if_save_keys(ad,if_info);
1678
1679             /* ... and save them */
1680             if (!airpcap_if_store_cur_config_as_adapter_default(ad))
1681             {
1682                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "Cannot save Wireless configuration!!!\nRemember that in order to store the configuration in the registry you have to:\n\n- Close all the airpcap-based applications.\n- Be sure to have administrative privileges.");
1683                 if_info->saved = FALSE;
1684                 airpcap_if_close(ad);
1685                 return;
1686             }
1687
1688             if_info->saved = TRUE;
1689             airpcap_if_close(ad);
1690         }
1691         else
1692         {
1693             simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, " Error in opening adapter for %s",if_info->description);
1694         }
1695     }
1696 }
1697
1698 /*
1699  * Save the configuration for the specified interface
1700  */
1701 void
1702 airpcap_save_driver_if_configuration(airpcap_if_info_t* fake_if_info)
1703 {
1704     gchar ebuf[AIRPCAP_ERRBUF_SIZE];
1705     PAirpcapHandle ad;
1706
1707     if (fake_if_info != NULL)
1708     {
1709         ad = airpcap_if_open(fake_if_info->name, ebuf);
1710
1711         if (ad)
1712         {
1713             /* Apply decryption settings... */
1714             airpcap_if_set_driver_decryption_state(ad, fake_if_info->DecryptionOn);
1715             airpcap_if_save_driver_keys(ad,fake_if_info);
1716             airpcap_if_close(ad);
1717         }
1718         else
1719         {
1720             simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, " Error in opening adapter for %s",fake_if_info->description);
1721         }
1722     }
1723
1724     return;
1725 }
1726
1727 /*
1728  * DECRYPTION KEYS FUNCTIONS
1729  */
1730 /*
1731  * This function is used for DEBUG POURPOSES ONLY!!!
1732  */
1733 void
1734 print_key_list(GList* key_list)
1735 {
1736     gint n,i;
1737     decryption_key_t* tmp;
1738
1739     if (key_list == NULL)
1740     {
1741         g_print("\n\n******* KEY LIST NULL *******\n\n");
1742         return;
1743     }
1744
1745     n = g_list_length(key_list);
1746
1747     g_print("\n\n********* KEY LIST **********\n\n");
1748
1749     g_print("NUMBER OF KEYS IN LIST : %d\n\n",n);
1750
1751     for(i =0; i < n; i++)
1752     {
1753         g_print("[%d] :\n",i+1);
1754         tmp = (decryption_key_t*)(g_list_nth_data(key_list,i));
1755         g_print("KEY : %s\n",tmp->key->str);
1756
1757         g_print("BITS: %d\n",tmp->bits);
1758
1759         if (tmp->type == AIRPDCAP_KEY_TYPE_WEP)
1760             g_print("TYPE: %s\n",AIRPCAP_WEP_KEY_STRING);
1761         else if (tmp->type == AIRPDCAP_KEY_TYPE_WPA_PWD)
1762             g_print("TYPE: %s\n",AIRPCAP_WPA_PWD_KEY_STRING);
1763         else if (tmp->type == AIRPDCAP_KEY_TYPE_WPA_PMK)
1764             g_print("TYPE: %s\n",AIRPCAP_WPA_BIN_KEY_STRING);
1765         else
1766             g_print("TYPE: %s\n","???");
1767
1768         g_print("SSID: %s\n",(tmp->ssid != NULL) ?
1769                 format_text((guchar *)tmp->ssid->data, tmp->ssid->len) : "---");
1770         g_print("\n");
1771     }
1772
1773     g_print("\n*****************************\n\n");
1774 }
1775
1776 /*
1777  * Retrieves a GList of decryption_key_t structures containing infos about the
1778  * keys for the given adapter... returns NULL if no keys are found.
1779  */
1780 GList *
1781 get_airpcap_device_keys(airpcap_if_info_t* info_if)
1782 {
1783     /* tmp vars */
1784     char* tmp_key = NULL;
1785     guint i,keys_in_list = 0;
1786
1787     /* real vars*/
1788     decryption_key_t *new_key  = NULL;
1789     GList            *key_list = NULL;
1790
1791     /* Number of keys in key list */
1792     if (info_if->keysCollectionSize != 0)
1793         keys_in_list = AirpcapKeysCollectionSizeToKeyCount(info_if->keysCollectionSize);
1794     else
1795         keys_in_list = 0;
1796
1797     for(i=0; i<keys_in_list; i++)
1798     {
1799         /* Different things to do depending on the key type  */
1800         if (info_if->keysCollection->Keys[i].KeyType == AIRPDCAP_KEY_TYPE_WEP)
1801         {
1802             /* allocate memory for the new key item */
1803             new_key = (decryption_key_t*)g_malloc(sizeof(decryption_key_t));
1804
1805             /* fill the fields */
1806             /* KEY */
1807             tmp_key = airpcap_get_key_string(info_if->keysCollection->Keys[i]);
1808             new_key->key = g_string_new(tmp_key);
1809             g_free(tmp_key);
1810
1811             /* BITS */
1812             new_key->bits = (guint) new_key->key->len *4; /* every char is 4 bits in WEP keys (it is an hexadecimal number) */
1813
1814             /* SSID not used in WEP keys */
1815             new_key->ssid = NULL;
1816
1817             /* TYPE (WEP in this case) */
1818             new_key->type = info_if->keysCollection->Keys[i].KeyType;
1819
1820             /* Append the new element in the list */
1821             key_list = g_list_append(key_list,(gpointer)new_key);
1822         }
1823         else if (info_if->keysCollection->Keys[i].KeyType == AIRPDCAP_KEY_TYPE_WPA_PWD)
1824         {
1825             /* XXX - Not supported yet */
1826         }
1827         else if (info_if->keysCollection->Keys[i].KeyType == AIRPDCAP_KEY_TYPE_WPA_PMK)
1828         {
1829             /* XXX - Not supported yet */
1830         }
1831     }
1832
1833     return key_list;
1834 }
1835
1836 /*
1837  * Retrieves a GList of decryption_key_t structures containing infos about the
1838  * keys for the global AirPcap driver... returns NULL if no keys are found.
1839  */
1840 GList *
1841 get_airpcap_driver_keys(void)
1842 {
1843     /* tmp vars */
1844     char  *tmp_key        = NULL;
1845     guint  i,keys_in_list = 0;
1846
1847     /* real vars*/
1848     decryption_key_t *new_key  = NULL;
1849     GList            *key_list = NULL;
1850
1851     /*
1852      * To read the drivers general settings we need to create and use one airpcap adapter...
1853      * The only way to do that is to instantiate a fake adapter, and then close it and delete it.
1854      */
1855     airpcap_if_info_t* fake_info_if = NULL;
1856
1857     /* Create the fake_info_if from the first adapter of the list */
1858     fake_info_if = airpcap_driver_fake_if_info_new();
1859
1860     if (fake_info_if == NULL)
1861         return NULL;
1862
1863     /* Number of keys in key list */
1864     if (fake_info_if->keysCollectionSize != 0)
1865         keys_in_list = AirpcapKeysCollectionSizeToKeyCount(fake_info_if->keysCollectionSize);
1866     else
1867         keys_in_list = 0;
1868
1869     for(i=0; i<keys_in_list; i++)
1870     {
1871         /* Different things to do depending on the key type  */
1872         if (fake_info_if->keysCollection->Keys[i].KeyType == AIRPDCAP_KEY_TYPE_WEP)
1873         {
1874             /* allocate memory for the new key item */
1875             new_key = (decryption_key_t*)g_malloc(sizeof(decryption_key_t));
1876
1877             /* fill the fields */
1878             /* KEY */
1879             tmp_key = airpcap_get_key_string(fake_info_if->keysCollection->Keys[i]);
1880             new_key->key = g_string_new(tmp_key);
1881             if (tmp_key != NULL) g_free(tmp_key);
1882
1883             /* BITS */
1884             new_key->bits = (guint) new_key->key->len *4; /* every char is 4 bits in WEP keys (it is an hexadecimal number) */
1885
1886             /* SSID not used in WEP keys */
1887             new_key->ssid = NULL;
1888
1889             /* TYPE (WEP in this case) */
1890             new_key->type = fake_info_if->keysCollection->Keys[i].KeyType;
1891
1892             /* Append the new element in the list */
1893             key_list = g_list_append(key_list,(gpointer)new_key);
1894         }
1895         else if (fake_info_if->keysCollection->Keys[i].KeyType == AIRPDCAP_KEY_TYPE_WPA_PWD)
1896         {
1897             /* XXX - Not supported yet */
1898         }
1899         else if (fake_info_if->keysCollection->Keys[i].KeyType == AIRPDCAP_KEY_TYPE_WPA_PMK)
1900         {
1901             /* XXX - Not supported yet */
1902         }
1903     }
1904
1905     airpcap_if_info_free(fake_info_if);
1906
1907     return key_list;
1908 }
1909
1910 /*
1911  * Returns the list of the decryption keys specified for wireshark, NULL if
1912  * no key is found
1913  */
1914 GList *
1915 get_wireshark_keys(void)
1916 {
1917     keys_cb_data_t *wep_user_data = NULL;
1918
1919     GList *final_list     = NULL;
1920     GList *wep_final_list = NULL;
1921
1922     /* Retrieve the wlan preferences */
1923     wlan_prefs = prefs_find_module("wlan");
1924
1925     /* Allocate a structure used to keep infos  between the callbacks */
1926     wep_user_data = (keys_cb_data_t*)g_malloc(sizeof(keys_cb_data_t));
1927
1928     /* Fill the structure */
1929     wep_user_data->list = NULL;
1930     wep_user_data->current_index = 0;
1931     wep_user_data->number_of_keys= 0; /* Still unknown */
1932
1933     /* Run the callback on each 802.11 preference */
1934     /* XXX - Right now, only WEP keys will be loaded */
1935     prefs_pref_foreach(wlan_prefs, get_wep_key, (gpointer)wep_user_data);
1936
1937     /* Copy the list field in the user data structure pointer into the final_list */
1938     wep_final_list = wep_user_data->list;
1939
1940     /* XXX - Merge the three lists!!!!! */
1941     final_list = wep_final_list;
1942
1943     /* free the wep_user_data structure */
1944     g_free(wep_user_data);
1945
1946     return final_list;
1947 }
1948
1949 /*
1950  * Merges two lists of keys and return a newly created GList. If a key is
1951  * found multiple times, it will just appear once!
1952  * list1 and list 2 pointer will have to be freed manually if needed!!!
1953  * If the total number of keys exceeeds the maximum number allowed,
1954  * exceeding keys will be discarded...
1955  */
1956 GList *
1957 merge_key_list(GList* list1, GList* list2)
1958 {
1959     guint n1=0,n2=0;
1960     guint i;
1961     decryption_key_t *dk1=NULL,
1962                      *dk2=NULL,
1963                      *new_dk=NULL;
1964
1965     GList* merged_list = NULL;
1966
1967     if ( (list1 == NULL) && (list2 == NULL) )
1968         return NULL;
1969
1970     if (list1 == NULL)
1971     {
1972         n2 = g_list_length(list2);
1973
1974         for(i=0;i<n2;i++)
1975         {
1976             new_dk = (decryption_key_t*)g_malloc(sizeof(decryption_key_t));
1977             dk2 = (decryption_key_t *)g_list_nth_data(list2,i);
1978
1979             new_dk->bits = dk2->bits;
1980             new_dk->type = dk2->type;
1981             new_dk->key  = g_string_new(dk2->key->str);
1982             new_dk->ssid = byte_array_dup(dk2->ssid);
1983
1984             /* Check the total length of the merged list */
1985             if (g_list_length(merged_list) < MAX_ENCRYPTION_KEYS)
1986                 merged_list = g_list_append(merged_list,(gpointer)new_dk);
1987         }
1988     }
1989     else if (list2 == NULL)
1990     {
1991         n1 = g_list_length(list1);
1992
1993         for(i=0;i<n1;i++)
1994         {
1995             new_dk = (decryption_key_t*)g_malloc(sizeof(decryption_key_t));
1996             dk1 = (decryption_key_t*)g_list_nth_data(list1,i);
1997
1998             new_dk->bits = dk1->bits;
1999             new_dk->type = dk1->type;
2000             new_dk->key  = g_string_new(dk1->key->str);
2001             new_dk->ssid = byte_array_dup(dk1->ssid);
2002
2003             /* Check the total length of the merged list */
2004             if (g_list_length(merged_list) < MAX_ENCRYPTION_KEYS)
2005                 merged_list = g_list_append(merged_list,(gpointer)new_dk);
2006         }
2007     }
2008     else
2009     {
2010         n1 = g_list_length(list1);
2011         n2 = g_list_length(list2);
2012
2013         /* Copy the whole list1 into merged_list */
2014         for(i=0;i<n1;i++)
2015         {
2016             new_dk = (decryption_key_t*)g_malloc(sizeof(decryption_key_t));
2017             dk1 = (decryption_key_t *)g_list_nth_data(list1,i);
2018
2019             new_dk->bits = dk1->bits;
2020             new_dk->type = dk1->type;
2021             new_dk->key  = g_string_new(dk1->key->str);
2022             new_dk->ssid = byte_array_dup(dk1->ssid);
2023
2024             /* Check the total length of the merged list */
2025             if (g_list_length(merged_list) < MAX_ENCRYPTION_KEYS)
2026                 merged_list = g_list_append(merged_list,(gpointer)new_dk);
2027         }
2028
2029         /* Look for keys that are present in list2 but aren't in list1 yet...
2030          * Add them to merged_list
2031          */
2032         for(i=0;i<n2;i++)
2033         {
2034             dk2 = (decryption_key_t *)g_list_nth_data(list2,i);
2035
2036             if (!key_is_in_list(dk2,merged_list))
2037             {
2038                 new_dk = (decryption_key_t*)g_malloc(sizeof(decryption_key_t));
2039
2040                 new_dk->bits = dk2->bits;
2041                 new_dk->type = dk2->type;
2042                 new_dk->key  = g_string_new(dk2->key->str);
2043                 new_dk->ssid = byte_array_dup(dk2->ssid);
2044
2045                 /* Check the total length of the merged list */
2046                 if (g_list_length(merged_list) < MAX_ENCRYPTION_KEYS)
2047                     merged_list = g_list_append(merged_list,(gpointer)new_dk);
2048             }
2049         }
2050     }
2051
2052     return merged_list;
2053 }
2054
2055 /*
2056  * Use this function to free a key list.
2057  */
2058 void
2059 free_key_list(GList *list)
2060 {
2061     guint i,n;
2062     decryption_key_t *curr_key;
2063
2064     if (list == NULL)
2065         return;
2066
2067     n = g_list_length(list);
2068
2069     for(i = 0; i < n; i++)
2070     {
2071         curr_key = (decryption_key_t*)g_list_nth_data(list,i);
2072
2073         /* Free all the strings */
2074         if (curr_key->key != NULL)
2075             g_string_free(curr_key->key, TRUE);
2076
2077         if (curr_key->ssid != NULL)
2078         g_byte_array_free(curr_key->ssid, TRUE);
2079
2080         /* free the decryption_key_t structure*/
2081         g_free(curr_key);
2082         curr_key = NULL;
2083     }
2084
2085     /* Free the list */
2086     g_list_free(list);
2087
2088     return;
2089 }
2090
2091
2092 /*
2093  * If the given key is contained in the list, returns TRUE.
2094  * Returns FALSE otherwise.
2095  */
2096 gboolean
2097 key_is_in_list(decryption_key_t *dk,GList *list)
2098 {
2099     guint i,n;
2100     decryption_key_t *curr_key = NULL;
2101     gboolean found = FALSE;
2102
2103     if ( (list == NULL) || (dk == NULL) )
2104         return FALSE;
2105
2106     n = g_list_length(list);
2107
2108     if (n < 1)
2109         return FALSE;
2110
2111     for(i = 0; i < n; i++)
2112     {
2113         curr_key = (decryption_key_t*)g_list_nth_data(list,i);
2114         if (keys_are_equals(dk,curr_key))
2115             found = TRUE;
2116     }
2117
2118     return found;
2119 }
2120
2121 /*
2122  * Returns TRUE if keys are equals, FALSE otherwise
2123  */
2124 gboolean
2125 keys_are_equals(decryption_key_t *k1,decryption_key_t *k2)
2126 {
2127
2128     if ((k1==NULL) || (k2==NULL))
2129         return FALSE;
2130
2131     /* XXX - Remove this check when we will have the WPA/WPA2 decryption in the Driver! */
2132     /** if ( (k1->type == AIRPDCAP_KEY_TYPE_WPA_PWD) || (k2->type == AIRPDCAP_KEY_TYPE_WPA_PWD) || (k1->type == AIRPDCAP_KEY_TYPE_WPA_PMK) || (k2->type == AIRPDCAP_KEY_TYPE_WPA_PMK) ) **/
2133     /**         return TRUE;  **/
2134
2135     if (g_string_equal(k1->key,k2->key) &&
2136         (k1->bits == k2->bits) && /* If the previous is TRUE, this must be TRUE as well */
2137         (k1->type == k2->type))
2138     {
2139         /* Check the ssid... if the key type is WEP, the two fields should be NULL */
2140         if ((k1->ssid == NULL) && (k2->ssid == NULL))
2141             return TRUE;
2142
2143         /* If they are not null, they must share the same ssid */
2144         return byte_array_equal(k1->ssid,k2->ssid);
2145     }
2146
2147     /* Some field is not equal ... */
2148     return FALSE;
2149 }
2150
2151 /*
2152  * Tests if two collection of keys are equal or not, to be considered equals, they have to
2153  * contain the same keys in the SAME ORDER! (If both lists are NULL, which means empty will
2154  * return TRUE)
2155  */
2156 gboolean
2157 key_lists_are_equal(GList* list1, GList* list2)
2158 {
2159     guint  n1        = 0,n2=0;
2160     /* XXX - Remove */
2161     guint  wep_n1    = 0,wep_n2=0;
2162     GList *wep_list1 = NULL;
2163     GList *wep_list2 = NULL;
2164     /* XXX - END*/
2165     guint i/*,j*/;
2166     decryption_key_t *dk1=NULL,*dk2=NULL;
2167
2168     n1 = g_list_length(list1);
2169     n2 = g_list_length(list2);
2170
2171     /*
2172      * XXX - START : Retrieve the aublists of WEP keys!!! This is needed only 'till Driver WPA decryption
2173      * is implemented.
2174      */
2175     for(i=0;i<n1;i++)
2176     {
2177         dk1=(decryption_key_t*)g_list_nth_data(list1,i);
2178         if (dk1->type == AIRPDCAP_KEY_TYPE_WEP)
2179         {
2180             wep_list1 = g_list_append(wep_list1,(gpointer)dk1);
2181             wep_n1++;
2182         }
2183     }
2184     for(i=0;i<n2;i++)
2185     {
2186         dk2=(decryption_key_t*)g_list_nth_data(list2,i);
2187         if (dk2->type == AIRPDCAP_KEY_TYPE_WEP)
2188         {
2189             wep_list2 = g_list_append(wep_list2,(gpointer)dk2);
2190             wep_n2++;
2191         }
2192     }
2193
2194     /*
2195      * XXX - END : Remove from START to END when the WPA/WPA2 decryption will be implemented in
2196      * the Driver
2197      */
2198
2199     /*
2200      * Commented, because in the new AirPcap version all the keys will be saved
2201      * into the driver, and all the keys for every specific adapter will be
2202      * removed. This means that this check will always fail... and the user will
2203      * always be asked what to do... and it doesn't make much sense.
2204      */
2205     /* if (n1 != n2) return FALSE; */
2206     if (wep_n1 != wep_n2) return FALSE;
2207
2208     n2 = wep_n2;
2209
2210     /*for(i=0;i<n1;i++)
2211     {
2212     dk1=(decryption_key_t*)g_list_nth_data(list1,i);
2213     dk2=(decryption_key_t*)g_list_nth_data(list2,i);
2214
2215     if (!g_string_equal(dk1->key,dk2->key)) return FALSE;
2216     }*/
2217     for(i=0;i<n2;i++)
2218     {
2219         dk2=(decryption_key_t*)g_list_nth_data(wep_list2,i);
2220         if (!key_is_in_list(dk2,wep_list1)) return FALSE;
2221     }
2222
2223     return TRUE;
2224 }
2225
2226 static guint
2227 test_if_on(pref_t *pref, gpointer ud)
2228 {
2229     gboolean *is_on;
2230     gboolean  number;
2231
2232     /* Retrieve user data info */
2233     is_on = (gboolean*)ud;
2234
2235
2236     if (g_ascii_strncasecmp(pref->name, "enable_decryption", 17) == 0 && pref->type == PREF_BOOL)
2237     {
2238         number = *pref->varp.boolp;
2239
2240         if (number) *is_on = TRUE;
2241         else *is_on = FALSE;
2242
2243         return 1;
2244     }
2245     return 0;
2246 }
2247
2248 /*
2249  * Returns TRUE if the Wireshark decryption is active, false otherwise
2250  * XXX - Should we just add a routine to packet-ieee80211.c to grab this directly?
2251  */
2252 gboolean
2253 wireshark_decryption_on(void)
2254 {
2255     gboolean is_on;
2256
2257     /* Retrieve the wlan preferences */
2258     wlan_prefs = prefs_find_module("wlan");
2259
2260     /* Run the callback on each 802.11 preference */
2261     prefs_pref_foreach(wlan_prefs, test_if_on, (gpointer)&is_on);
2262
2263     return is_on;
2264 }
2265
2266 /*
2267  * Returns TRUE if the AirPcap decryption for the current adapter is active, false otherwise
2268  */
2269 gboolean
2270 airpcap_decryption_on(void)
2271 {
2272     gboolean is_on = FALSE;
2273
2274     airpcap_if_info_t* fake_if_info = NULL;
2275
2276     fake_if_info = airpcap_driver_fake_if_info_new();
2277
2278     if (fake_if_info != NULL)
2279     {
2280         if (fake_if_info->DecryptionOn == AIRPCAP_DECRYPTION_ON)
2281             is_on = TRUE;
2282         else if (fake_if_info->DecryptionOn == AIRPCAP_DECRYPTION_OFF)
2283             is_on = FALSE;
2284     }
2285
2286     airpcap_if_info_free(fake_if_info);
2287
2288     return is_on;
2289 }
2290
2291 /*
2292  * Free an instance of airpcap_if_info_t
2293  */
2294 void
2295 airpcap_if_info_free(airpcap_if_info_t *if_info)
2296 {
2297     if (if_info != NULL)
2298     {
2299         if (if_info->name != NULL)
2300             g_free(if_info->name);
2301
2302         if (if_info->description != NULL)
2303             g_free(if_info->description);
2304
2305         if (if_info->keysCollection != NULL)
2306         {
2307             g_free(if_info->keysCollection);
2308             if_info->keysCollection = NULL;
2309         }
2310
2311         if (if_info->ip_addr != NULL)
2312         {
2313             g_slist_free(if_info->ip_addr);
2314             if_info->ip_addr = NULL;
2315         }
2316
2317         if (if_info != NULL)
2318         {
2319             g_free(if_info);
2320             if_info = NULL;
2321         }
2322     }
2323 }
2324
2325 static guint
2326 set_on_off(pref_t *pref, gpointer ud)
2327 {
2328     gboolean *is_on;
2329
2330     /* Retrieve user data info */
2331     is_on = (gboolean*)ud;
2332
2333     if (g_ascii_strncasecmp(pref->name, "enable_decryption", 17) == 0 && pref->type == PREF_BOOL)
2334     {
2335
2336         if (*is_on)
2337             *pref->varp.boolp = TRUE;
2338         else
2339             *pref->varp.boolp = FALSE;
2340
2341         return 1;
2342     }
2343     return 0;
2344 }
2345
2346 /*
2347  * Enables decryption for Wireshark if on_off is TRUE, disables it otherwise.
2348  */
2349 void
2350 set_wireshark_decryption(gboolean on_off)
2351 {
2352     gboolean is_on;
2353
2354     is_on = on_off;
2355
2356     /* Retrieve the wlan preferences */
2357     wlan_prefs = prefs_find_module("wlan");
2358
2359     /* Run the callback on each 802.11 preference */
2360     prefs_pref_foreach(wlan_prefs, set_on_off, (gpointer)&is_on);
2361
2362     /*
2363      * Signal that we've changed things, and run the 802.11 dissector's
2364      * callback
2365      */
2366     wlan_prefs->prefs_changed = TRUE;
2367
2368     prefs_apply(wlan_prefs);
2369 }
2370
2371 /*
2372  * Enables decryption for all the adapters if on_off is TRUE, disables it otherwise.
2373  */
2374 gboolean
2375 set_airpcap_decryption(gboolean on_off)
2376 {
2377     /* We need to directly access the .dll functions here... */
2378     gchar ebuf[AIRPCAP_ERRBUF_SIZE];
2379     PAirpcapHandle ad,ad_driver;
2380
2381     gboolean success = TRUE;
2382
2383     gint n = 0;
2384     gint i = 0;
2385     airpcap_if_info_t* curr_if = NULL;
2386     airpcap_if_info_t* fake_if_info = NULL;
2387
2388     fake_if_info = airpcap_driver_fake_if_info_new();
2389
2390     if (fake_if_info == NULL)
2391         /* We apparently don't have any adapters installed.
2392          * This isn't a failure, so return TRUE
2393          */
2394         return TRUE;
2395
2396     /* Set the driver decryption */
2397     ad_driver = airpcap_if_open(fake_if_info->name, ebuf);
2398     if (ad_driver)
2399     {
2400         if (on_off)
2401             airpcap_if_set_driver_decryption_state(ad_driver,AIRPCAP_DECRYPTION_ON);
2402         else
2403             airpcap_if_set_driver_decryption_state(ad_driver,AIRPCAP_DECRYPTION_OFF);
2404
2405         airpcap_if_close(ad_driver);
2406     }
2407
2408     airpcap_if_info_free(fake_if_info);
2409
2410     n = g_list_length(airpcap_if_list);
2411
2412     /* Set to FALSE the decryption for all the adapters */
2413     /* Apply this change to all the adapters !!! */
2414     for(i = 0; i < n; i++)
2415     {
2416         curr_if = (airpcap_if_info_t*)g_list_nth_data(airpcap_if_list,i);
2417
2418         if (curr_if != NULL)
2419         {
2420             ad = airpcap_if_open(curr_if->name, ebuf);
2421             if (ad)
2422             {
2423                 curr_if->DecryptionOn = AIRPCAP_DECRYPTION_OFF;
2424                 airpcap_if_set_decryption_state(ad,curr_if->DecryptionOn);
2425                 /* Save configuration for the curr_if */
2426                 if (!airpcap_if_store_cur_config_as_adapter_default(ad))
2427                 {
2428                     success = FALSE;
2429                 }
2430                 airpcap_if_close(ad);
2431             }
2432         }
2433     }
2434
2435     return success;
2436 }
2437
2438
2439 /* DYNAMIC LIBRARY LOADER */
2440 /*
2441  *  Used to dynamically load the airpcap library in order link it only when
2442  *  it's present on the system
2443  */
2444 int load_airpcap(void)
2445 {
2446 #ifdef _WIN32
2447     gboolean base_functions     = TRUE;
2448     gboolean eleven_n_functions = TRUE;
2449
2450     if ((AirpcapLib = ws_load_library("airpcap.dll")) == NULL)
2451     {
2452         /* Report the error but go on */
2453         AirpcapVersion = AIRPCAP_DLL_NOT_FOUND;
2454         return AirpcapVersion;
2455     }
2456     else
2457     {
2458         if ((g_PAirpcapGetLastError = (AirpcapGetLastErrorHandler) GetProcAddress(AirpcapLib, "AirpcapGetLastError")) == NULL) base_functions = FALSE;
2459         if ((g_PAirpcapGetDeviceList = (AirpcapGetDeviceListHandler) GetProcAddress(AirpcapLib, "AirpcapGetDeviceList")) == NULL) base_functions = FALSE;
2460         if ((g_PAirpcapFreeDeviceList = (AirpcapFreeDeviceListHandler) GetProcAddress(AirpcapLib, "AirpcapFreeDeviceList")) == NULL) base_functions = FALSE;
2461         if ((g_PAirpcapOpen = (AirpcapOpenHandler) GetProcAddress(AirpcapLib, "AirpcapOpen")) == NULL) base_functions = FALSE;
2462         if ((g_PAirpcapClose = (AirpcapCloseHandler) GetProcAddress(AirpcapLib, "AirpcapClose")) == NULL) base_functions = FALSE;
2463         if ((g_PAirpcapGetLinkType = (AirpcapGetLinkTypeHandler) GetProcAddress(AirpcapLib, "AirpcapGetLinkType")) == NULL) base_functions = FALSE;
2464         if ((g_PAirpcapSetLinkType = (AirpcapSetLinkTypeHandler) GetProcAddress(AirpcapLib, "AirpcapSetLinkType")) == NULL) base_functions = FALSE;
2465         if ((g_PAirpcapSetKernelBuffer = (AirpcapSetKernelBufferHandler) GetProcAddress(AirpcapLib, "AirpcapSetKernelBuffer")) == NULL) base_functions = FALSE;
2466         if ((g_PAirpcapSetFilter = (AirpcapSetFilterHandler) GetProcAddress(AirpcapLib, "AirpcapSetFilter")) == NULL) base_functions = FALSE;
2467         if ((g_PAirpcapGetMacAddress = (AirpcapGetMacAddressHandler) GetProcAddress(AirpcapLib, "AirpcapGetMacAddress")) == NULL) base_functions = FALSE;
2468         if ((g_PAirpcapSetMinToCopy = (AirpcapSetMinToCopyHandler) GetProcAddress(AirpcapLib, "AirpcapSetMinToCopy")) == NULL) base_functions = FALSE;
2469         if ((g_PAirpcapGetReadEvent = (AirpcapGetReadEventHandler) GetProcAddress(AirpcapLib, "AirpcapGetReadEvent")) == NULL) base_functions = FALSE;
2470         if ((g_PAirpcapRead = (AirpcapReadHandler) GetProcAddress(AirpcapLib, "AirpcapRead")) == NULL) base_functions = FALSE;
2471         if ((g_PAirpcapGetStats = (AirpcapGetStatsHandler) GetProcAddress(AirpcapLib, "AirpcapGetStats")) == NULL) base_functions = FALSE;
2472         if ((g_PAirpcapTurnLedOn = (AirpcapTurnLedOnHandler) GetProcAddress(AirpcapLib, "AirpcapTurnLedOn")) == NULL) base_functions = FALSE;
2473         if ((g_PAirpcapTurnLedOff = (AirpcapTurnLedOffHandler) GetProcAddress(AirpcapLib, "AirpcapTurnLedOff")) == NULL) base_functions = FALSE;
2474         if ((g_PAirpcapGetDeviceChannel = (AirpcapGetDeviceChannelHandler) GetProcAddress(AirpcapLib, "AirpcapGetDeviceChannel")) == NULL) base_functions = FALSE;
2475         if ((g_PAirpcapSetDeviceChannel = (AirpcapSetDeviceChannelHandler) GetProcAddress(AirpcapLib, "AirpcapSetDeviceChannel")) == NULL) base_functions = FALSE;
2476         if ((g_PAirpcapGetFcsPresence = (AirpcapGetFcsPresenceHandler) GetProcAddress(AirpcapLib, "AirpcapGetFcsPresence")) == NULL) base_functions = FALSE;
2477         if ((g_PAirpcapSetFcsPresence = (AirpcapSetFcsPresenceHandler) GetProcAddress(AirpcapLib, "AirpcapSetFcsPresence")) == NULL) base_functions = FALSE;
2478         if ((g_PAirpcapGetFcsValidation = (AirpcapGetFcsValidationHandler) GetProcAddress(AirpcapLib, "AirpcapGetFcsValidation")) == NULL) base_functions = FALSE;
2479         if ((g_PAirpcapSetFcsValidation = (AirpcapSetFcsValidationHandler) GetProcAddress(AirpcapLib, "AirpcapSetFcsValidation")) == NULL) base_functions = FALSE;
2480         if ((g_PAirpcapGetDeviceKeys = (AirpcapGetDeviceKeysHandler) GetProcAddress(AirpcapLib, "AirpcapGetDeviceKeys")) == NULL) base_functions = FALSE;
2481         if ((g_PAirpcapSetDeviceKeys = (AirpcapSetDeviceKeysHandler) GetProcAddress(AirpcapLib, "AirpcapSetDeviceKeys")) == NULL) base_functions = FALSE;
2482         if ((g_PAirpcapGetDecryptionState = (AirpcapGetDecryptionStateHandler) GetProcAddress(AirpcapLib, "AirpcapGetDecryptionState")) == NULL) base_functions = FALSE;
2483         if ((g_PAirpcapSetDecryptionState = (AirpcapSetDecryptionStateHandler) GetProcAddress(AirpcapLib, "AirpcapSetDecryptionState")) == NULL) base_functions = FALSE;
2484         if ((g_PAirpcapStoreCurConfigAsAdapterDefault = (AirpcapStoreCurConfigAsAdapterDefaultHandler) GetProcAddress(AirpcapLib, "AirpcapStoreCurConfigAsAdapterDefault")) == NULL) base_functions = FALSE;
2485         if ((g_PAirpcapGetVersion = (AirpcapGetVersionHandler) GetProcAddress(AirpcapLib, "AirpcapGetVersion")) == NULL) base_functions = FALSE;
2486         if ((g_PAirpcapGetDriverDecryptionState = (AirpcapGetDriverDecryptionStateHandler) GetProcAddress(AirpcapLib, "AirpcapGetDriverDecryptionState")) == NULL) base_functions = FALSE;
2487         if ((g_PAirpcapSetDriverDecryptionState = (AirpcapSetDriverDecryptionStateHandler) GetProcAddress(AirpcapLib, "AirpcapSetDriverDecryptionState")) == NULL) base_functions = FALSE;
2488         if ((g_PAirpcapGetDriverKeys = (AirpcapGetDriverKeysHandler) GetProcAddress(AirpcapLib, "AirpcapGetDriverKeys")) == NULL) base_functions = FALSE;
2489         if ((g_PAirpcapSetDriverKeys = (AirpcapSetDriverKeysHandler) GetProcAddress(AirpcapLib, "AirpcapSetDriverKeys")) == NULL) base_functions = FALSE;
2490
2491         /* TEST IF AIRPCAP SUPPORTS 11N */
2492         if ((g_PAirpcapSetDeviceChannelEx = (AirpcapSetDeviceChannelExHandler) GetProcAddress(AirpcapLib, "AirpcapSetDeviceChannelEx")) == NULL) eleven_n_functions = FALSE;
2493         if ((g_PAirpcapGetDeviceChannelEx = (AirpcapGetDeviceChannelExHandler) GetProcAddress(AirpcapLib, "AirpcapGetDeviceChannelEx")) == NULL) eleven_n_functions = FALSE;
2494         if ((g_PAirpcapGetDeviceSupportedChannels = (AirpcapGetDeviceSupportedChannelsHandler) GetProcAddress(AirpcapLib, "AirpcapGetDeviceSupportedChannels")) == NULL) eleven_n_functions = FALSE;
2495
2496         if (base_functions && eleven_n_functions){
2497             AirpcapLoaded = TRUE;
2498             AirpcapVersion = AIRPCAP_DLL_OK;
2499         } else if (base_functions){
2500             AirpcapLoaded = TRUE;
2501             AirpcapVersion = AIRPCAP_DLL_OLD;
2502             return AIRPCAP_DLL_OK;
2503         }else{
2504             AirpcapLoaded = FALSE;
2505             AirpcapVersion = AIRPCAP_DLL_ERROR;
2506         }
2507     }
2508     return AirpcapVersion;
2509 #else /* _WIN32 */
2510     return AIRPCAP_DLL_NOT_FOUND;
2511 #endif /* _WIN32 */
2512 }
2513
2514 /*
2515  * Append the version of AirPcap with which we were compiled to a GString.
2516  */
2517 void
2518 get_compiled_airpcap_version(GString *str)
2519 {
2520     g_string_append(str, "with AirPcap");
2521 }
2522
2523 /*
2524  * Append the version of AirPcap with which we we're running to a GString.
2525  */
2526 void
2527 get_runtime_airpcap_version(GString *str)
2528 {
2529     guint vmaj, vmin, vrev, build;
2530
2531     /* See if the DLL has been loaded successfully.  Bail if it hasn't */
2532     if (AirpcapLoaded == FALSE) {
2533         g_string_append(str, "without AirPcap");
2534         return;
2535     }
2536
2537     g_PAirpcapGetVersion(&vmaj, &vmin, &vrev, &build);
2538     g_string_append_printf(str, "with AirPcap %d.%d.%d build %d", vmaj, vmin,
2539         vrev, build);
2540 }
2541 #endif /* HAVE_AIRPCAP */
2542
2543 /*
2544  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
2545  *
2546  * Local variables:
2547  * c-basic-offset: 4
2548  * tab-width: 8
2549  * indent-tabs-mode: nil
2550  * End:
2551  *
2552  * vi: set shiftwidth=4 tabstop=8 expandtab:
2553  * :indentSize=4:tabSize=8:noTabs=true:
2554  */