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