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