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