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