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