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