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