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