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