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