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